diff --git a/sources/TerminalStrip/UndoCommand/addterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/addterminalstripcommand.cpp index aa8d4b8fe..46fe13bdd 100644 --- a/sources/TerminalStrip/UndoCommand/addterminalstripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/addterminalstripcommand.cpp @@ -18,6 +18,7 @@ #include "addterminalstripcommand.h" #include "../../qetproject.h" #include "../terminalstrip.h" +#include "../qetgraphicsitem/element.h" #include @@ -56,7 +57,8 @@ RemoveTerminalStripCommand::RemoveTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) : QUndoCommand(parent), m_strip(strip), - m_project(project) + m_project(project), + m_elements(strip->terminalElement()) { setText(QObject::tr("Supprimer un groupe de bornes")); } @@ -67,6 +69,9 @@ RemoveTerminalStripCommand::~RemoveTerminalStripCommand() void RemoveTerminalStripCommand::undo() { if (m_project && m_strip) { + for (auto elmt : m_elements) { + m_strip->addTerminal(elmt); + } m_project->addTerminalStrip(m_strip); } } @@ -74,6 +79,9 @@ void RemoveTerminalStripCommand::undo() void RemoveTerminalStripCommand::redo() { if (m_project && m_strip) { + for (auto elmt : m_elements) { + m_strip->removeTerminal(elmt); + } m_project->removeTerminalStrip(m_strip); } } diff --git a/sources/TerminalStrip/UndoCommand/addterminalstripcommand.h b/sources/TerminalStrip/UndoCommand/addterminalstripcommand.h index 753ee3a90..8317479e4 100644 --- a/sources/TerminalStrip/UndoCommand/addterminalstripcommand.h +++ b/sources/TerminalStrip/UndoCommand/addterminalstripcommand.h @@ -23,6 +23,7 @@ class TerminalStrip; class QETProject; +class Element; class AddTerminalStripCommand : public QUndoCommand { @@ -50,6 +51,7 @@ class RemoveTerminalStripCommand : public QUndoCommand private: QPointer m_strip; QPointer m_project; + QVector> m_elements; }; #endif // ADDTERMINALSTRIPCOMMAND_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 8eb21588d..e765c0a5c 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -247,6 +247,8 @@ bool TerminalStrip::addTerminal(Element *terminal) return false; } + m_terminal_elements_vector.append(terminal); + //Create the real terminal shared_real_terminal real_terminal(new RealTerminal(this, terminal)); m_real_terminals.append(real_terminal); @@ -271,25 +273,33 @@ bool TerminalStrip::addTerminal(Element *terminal) */ bool TerminalStrip::removeTerminal(Element *terminal) { - if (auto real_terminal = realTerminal(terminal)) + if (m_terminal_elements_vector.contains(terminal)) { - if (auto physical_terminal = physicalTerminal(real_terminal)) + m_terminal_elements_vector.removeOne(terminal); + + //Get the real and physical terminal associated to @terminal + if (auto real_terminal = realTerminal(terminal)) { - if (physical_terminal->levelCount() == 1) { - m_physical_terminals.removeOne(physical_terminal); - } else { - auto v = physical_terminal->terminals(); - v.removeOne(real_terminal); - physical_terminal->setTerminals(v); + if (auto physical_terminal = physicalTerminal(real_terminal)) + { + if (physical_terminal->levelCount() == 1) { + m_physical_terminals.removeOne(physical_terminal); + } else { + auto v = physical_terminal->terminals(); + v.removeOne(real_terminal); + physical_terminal->setTerminals(v); + } } + m_real_terminals.removeOne(real_terminal); + + static_cast(terminal)->setParentTerminalStrip(nullptr); + + return true; } - m_real_terminals.removeOne(real_terminal); - static_cast(terminal)->setParentTerminalStrip(nullptr); - - return true; + //There is no reason to be here, but in case of.... + return false; } - return false; } @@ -332,6 +342,14 @@ TerminalStripIndex TerminalStrip::index(int index) return tsi_; } +/** + * @brief TerminalStrip::terminalElement + * @return A vector of all terminal element owned by this strip + */ +QVector > TerminalStrip::terminalElement() const { + return m_terminal_elements_vector; +} + /** * @brief TerminalStrip::realTerminal * @param terminal diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index b43d7460f..3f13698fd 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -58,6 +58,8 @@ class TerminalStrip : public QObject int physicalTerminalCount() const; TerminalStripIndex index(int index = 0); + QVector> terminalElement() const; + private: QSharedPointer realTerminal(Element *terminal); QSharedPointer physicalTerminal(QSharedPointer terminal);