From ded4ea555fefe609f8e1aca06ec3080ccdd93096 Mon Sep 17 00:00:00 2001 From: joshua Date: Tue, 4 May 2021 19:04:06 +0200 Subject: [PATCH] Terminal element can be removed from a terminal strip --- .../UndoCommand/addterminaltostripcommand.cpp | 39 +++++++++++++++++++ .../UndoCommand/addterminaltostripcommand.h | 21 +++++++++- .../TerminalStrip/ui/terminalstripeditor.cpp | 13 +++++++ .../ui/terminalstriptreewidget.cpp | 4 +- 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp index 985f11473..f5e59239f 100644 --- a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp @@ -115,3 +115,42 @@ void AddTerminalToStripCommand::redo() m_new_strip->addTerminal(m_terminal); } + +/** + * @brief RemoveTerminalFromStripCommand::RemoveTerminalFromStripCommand + * @param terminal + * @param strip + * @param parent + */ +RemoveTerminalFromStripCommand::RemoveTerminalFromStripCommand(TerminalElement *terminal, + TerminalStrip *strip, + QUndoCommand *parent) : + QUndoCommand(parent), + m_terminal(terminal), + m_strip(strip) +{ + auto t_label = terminal->actualLabel(); + auto strip_name = strip->name(); + + auto str_1 = t_label.isEmpty() ? QObject::tr("Enlever une borne") : + QObject::tr("Enlever la borne %1").arg(t_label); + + auto str_2 = strip_name.isEmpty() ? QObject::tr("d'un groupe de bornes") : + QObject::tr("du groupe de bornes %1").arg(strip_name); + setText(str_1 + " " + str_2); +} + +void RemoveTerminalFromStripCommand::undo() +{ + if (m_terminal && m_strip) { + m_strip->addTerminal(m_terminal); + } +} + +void RemoveTerminalFromStripCommand::redo() +{ + if (m_terminal && m_strip) { + m_strip->removeTerminal(m_terminal); + } +} + diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h index aff25905c..bf2313fb1 100644 --- a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -57,4 +57,23 @@ class AddTerminalToStripCommand : public QUndoCommand }; +/** + * @brief The RemoveTerminalFromStripCommand class + * Remove a terminal from a terminal strip. + * The removed terminal become free. + */ +class RemoveTerminalFromStripCommand : public QUndoCommand +{ + public: + RemoveTerminalFromStripCommand (TerminalElement *terminal, TerminalStrip *strip, QUndoCommand *parent = nullptr); + ~RemoveTerminalFromStripCommand() override {} + + void undo() override; + void redo() override; + + private: + QPointer m_terminal; + QPointer m_strip; +}; + #endif // ADDTERMINALTOSTRIPCOMMAND_H diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 1a129f643..943ff8f7a 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -83,6 +83,19 @@ void TerminalStripEditor::setUpUndoConnections() m_project->undoStack()->push(undo); }); + connect(ui->m_terminal_strip_tw, &TerminalStripTreeWidget::terminalRemovedFromStrip, + [this] (QUuid terminal_uuid, QUuid old_strip_uuid) + { + auto terminal_ = m_uuid_terminal_H.value(terminal_uuid); + auto strip_ = m_uuid_strip_H.value(old_strip_uuid); + + if (!terminal_ || !strip_) { + return; + } + + auto undo = new RemoveTerminalFromStripCommand(terminal_, strip_); + m_project->undoStack()->push(undo); + }); } /** diff --git a/sources/TerminalStrip/ui/terminalstriptreewidget.cpp b/sources/TerminalStrip/ui/terminalstriptreewidget.cpp index 3577695bd..666436078 100644 --- a/sources/TerminalStrip/ui/terminalstriptreewidget.cpp +++ b/sources/TerminalStrip/ui/terminalstriptreewidget.cpp @@ -122,9 +122,9 @@ void TerminalStripTreeWidget::dropEvent(QDropEvent *event) if (overred_item->type() == FreeTerminal) //Free terminal { emit terminalRemovedFromStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()), - QUuid::fromString(overred_item->data(0, UUID_USER_ROLE).toString())); + QUuid::fromString(old_parent->data(0, UUID_USER_ROLE).toString())); } - else if (overred_item->type() == Strip) + else if (overred_item->type() == Strip) //To another strip { emit terminalMovedFromStripToStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()), QUuid::fromString(old_parent->data(0, UUID_USER_ROLE).toString()),