diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp index f5e59239f..f63afc516 100644 --- a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp @@ -17,6 +17,8 @@ */ #include "addterminaltostripcommand.h" #include "../../qetgraphicsitem/terminalelement.h" +#include "../realterminal.h" +#include "../physicalterminal.h" /** * @brief AddTerminalToStripCommand::AddTerminalToStripCommand @@ -25,13 +27,12 @@ * @param strip : terminal strip where terminal must be added * @param parent : parent undo command */ -AddTerminalToStripCommand::AddTerminalToStripCommand(TerminalElement *terminal, TerminalStrip *strip, QUndoCommand *parent) : +AddTerminalToStripCommand::AddTerminalToStripCommand(QSharedPointer terminal, TerminalStrip *strip, QUndoCommand *parent) : QUndoCommand(parent), m_terminal(terminal), - m_new_strip(strip), - m_operation(Operation::add) + m_new_strip(strip) { - auto t_label = terminal->actualLabel(); + auto t_label = terminal->label(); auto ts_name = strip->name(); auto str_1 = t_label.isEmpty() ? QObject::tr("Ajouter une borne") : @@ -43,37 +44,6 @@ AddTerminalToStripCommand::AddTerminalToStripCommand(TerminalElement *terminal, setText(str_1 + " " + str_2); } -/** - * @brief AddTerminalToStripCommand::AddTerminalToStripCommand - * Move \p terminal from \p old_strip to \p new_strip - * @param terminal : terminal to move - * @param old_strip : terminal where start the move - * @param new_strip : terminal where finish the move - * @param parent : parent undo command - */ -AddTerminalToStripCommand::AddTerminalToStripCommand(TerminalElement *terminal, TerminalStrip *old_strip, - TerminalStrip *new_strip, QUndoCommand *parent) : - QUndoCommand(parent), - m_terminal(terminal), - m_old_strip(old_strip), - m_new_strip(new_strip), - m_operation(Operation::move) -{ - auto t_label = terminal->actualLabel(); - auto old_ts_name = old_strip->name(); - auto new_ts_name = new_strip->name(); - - auto str_1 = t_label.isEmpty() ? QObject::tr("Déplacer une borne") : - QObject::tr("Déplacer la borne %1").arg(t_label); - - auto str_2 = old_ts_name.isEmpty() ? QObject::tr("d'un groupe de bornes") : - QObject::tr("du groupe de bornes %1").arg(old_ts_name); - - auto str_3 = new_ts_name.isEmpty() ? QObject::tr("à un autre groupe de bornes") : - QObject::tr("au groupe de bornes %1").arg(new_ts_name); - - setText(str_1 + " " + str_2 + " " + str_3); -} AddTerminalToStripCommand::~AddTerminalToStripCommand() {} @@ -88,13 +58,7 @@ void AddTerminalToStripCommand::undo() !m_new_strip) { return; } - m_new_strip->removeTerminal(m_terminal); - - if ( m_operation == Operation::move && - m_old_strip) { - m_old_strip->addTerminal(m_terminal); - } } /** @@ -107,12 +71,6 @@ void AddTerminalToStripCommand::redo() !m_new_strip) { return; } - - if (m_operation == Operation::move && - m_old_strip) { - m_old_strip->removeTerminal(m_terminal); - } - m_new_strip->addTerminal(m_terminal); } @@ -122,14 +80,20 @@ void AddTerminalToStripCommand::redo() * @param strip * @param parent */ -RemoveTerminalFromStripCommand::RemoveTerminalFromStripCommand(TerminalElement *terminal, +RemoveTerminalFromStripCommand::RemoveTerminalFromStripCommand(QSharedPointer terminal, TerminalStrip *strip, QUndoCommand *parent) : QUndoCommand(parent), - m_terminal(terminal), + m_terminals(terminal->realTerminals()), m_strip(strip) -{ - auto t_label = terminal->actualLabel(); +{ + QString t_label; + for (const auto &real_t : m_terminals) { + if (!t_label.isEmpty()) + t_label.append(", "); + t_label.append(real_t->label()); + } + auto strip_name = strip->name(); auto str_1 = t_label.isEmpty() ? QObject::tr("Enlever une borne") : @@ -142,15 +106,85 @@ RemoveTerminalFromStripCommand::RemoveTerminalFromStripCommand(TerminalElement * void RemoveTerminalFromStripCommand::undo() { - if (m_terminal && m_strip) { - m_strip->addTerminal(m_terminal); + if (m_strip) + { + for (const auto &real_t : m_terminals) { + m_strip->addTerminal(real_t); + } + auto phy_t = m_terminals.first()->physicalTerminal(); + if (phy_t) { + m_strip->groupTerminals(phy_t, m_terminals); + } } } void RemoveTerminalFromStripCommand::redo() { - if (m_terminal && m_strip) { - m_strip->removeTerminal(m_terminal); + if (m_strip) + { + for (const auto & real_t : m_terminals) { + m_strip->removeTerminal(real_t); + } } } +/** + * @brief MoveTerminalCommand::MoveTerminalCommand + * @param terminal + * @param old_strip + * @param new_strip + * @param parent + */ +MoveTerminalCommand::MoveTerminalCommand(QSharedPointer terminal, TerminalStrip *old_strip, + TerminalStrip *new_strip, QUndoCommand *parent) : + QUndoCommand (parent), + m_terminal(terminal), + m_old_strip(old_strip), + m_new_strip(new_strip) +{ + QString t_label; + for (auto real_t : terminal->realTerminals()) { + if (!t_label.isEmpty()) + t_label.append(", "); + t_label.append(real_t->label()); + } + + auto strip_name = old_strip->name(); + auto new_strip_name = new_strip->name(); + + auto str_1 = t_label.isEmpty() ? QObject::tr("Déplacer une borne") : + QObject::tr("Déplacer 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); + + auto str_3 = new_strip_name.isEmpty() ? QObject::tr("vers un groupe de bornes") : + QObject::tr("vers le groupe de bornes %1").arg(new_strip_name); + setText(str_1 + " " + str_2 + " " + str_3); +} + +void MoveTerminalCommand::undo() +{ + if (m_terminal) + { + if (m_new_strip) { + m_new_strip->removeTerminal(m_terminal); + } + if (m_old_strip) { + m_old_strip->addTerminal(m_terminal); + } + } +} + +void MoveTerminalCommand::redo() +{ + if (m_terminal) + { + if (m_old_strip) { + m_old_strip->removeTerminal(m_terminal); + } + if (m_new_strip) { + m_new_strip->addTerminal(m_terminal); + } + } +} diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h index bf2313fb1..31f75d7c7 100644 --- a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h @@ -23,6 +23,8 @@ class TerminalElement; class TerminalStrip; +class RealTerminal; +class PhysicalTerminal; /** * @brief The AddTerminalToStripCommand class @@ -34,27 +36,15 @@ class TerminalStrip; class AddTerminalToStripCommand : public QUndoCommand { public: - AddTerminalToStripCommand(TerminalElement *terminal, TerminalStrip *strip, QUndoCommand *parent = nullptr); - AddTerminalToStripCommand(TerminalElement *terminal, TerminalStrip *old_strip, - TerminalStrip *new_strip, QUndoCommand *parent = nullptr); + AddTerminalToStripCommand(QSharedPointer terminal, TerminalStrip *strip, QUndoCommand *parent = nullptr); ~AddTerminalToStripCommand() override; void undo() override; void redo() override; private: - enum Operation{ - none, - add, - move, - }; - - QPointer m_terminal; - QPointer m_old_strip; + QSharedPointer m_terminal; QPointer m_new_strip; - Operation m_operation = Operation::none; - - }; /** @@ -65,15 +55,30 @@ class AddTerminalToStripCommand : public QUndoCommand class RemoveTerminalFromStripCommand : public QUndoCommand { public: - RemoveTerminalFromStripCommand (TerminalElement *terminal, TerminalStrip *strip, QUndoCommand *parent = nullptr); + RemoveTerminalFromStripCommand (QSharedPointer terminal, TerminalStrip *strip, QUndoCommand *parent = nullptr); ~RemoveTerminalFromStripCommand() override {} void undo() override; void redo() override; private: - QPointer m_terminal; + QVector> m_terminals; QPointer m_strip; }; +class MoveTerminalCommand : public QUndoCommand +{ + public: + MoveTerminalCommand (QSharedPointer terminal, TerminalStrip *old_strip, + TerminalStrip *new_strip, QUndoCommand *parent = nullptr); + + void undo() override; + void redo() override; + + private: + const QSharedPointer m_terminal; + QPointer m_old_strip, m_new_strip; + +}; + #endif // ADDTERMINALTOSTRIPCOMMAND_H diff --git a/sources/TerminalStrip/physicalterminal.cpp b/sources/TerminalStrip/physicalterminal.cpp index 934d3ae88..f29e7bfa3 100644 --- a/sources/TerminalStrip/physicalterminal.cpp +++ b/sources/TerminalStrip/physicalterminal.cpp @@ -141,6 +141,11 @@ bool PhysicalTerminal::setLevelOf(const QSharedPointer &terminal, return false; } +void PhysicalTerminal::setParentStrip(TerminalStrip *strip) +{ + m_parent_terminal_strip = strip; +} + PhysicalTerminal::~PhysicalTerminal() { for (const auto &real_t : m_real_terminal) { diff --git a/sources/TerminalStrip/physicalterminal.h b/sources/TerminalStrip/physicalterminal.h index 501d3c1d4..13f1cd70b 100644 --- a/sources/TerminalStrip/physicalterminal.h +++ b/sources/TerminalStrip/physicalterminal.h @@ -76,6 +76,8 @@ class PhysicalTerminal bool setLevelOf(const QSharedPointer &terminal, int level); + void setParentStrip(TerminalStrip *strip); + public: PhysicalTerminal(){} ~PhysicalTerminal(); diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 6c067ccee..41ef4ba5d 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -94,6 +94,49 @@ void TerminalStrip::setData(const TerminalStripData &data) { m_data = data; } +bool TerminalStrip::addTerminal(QSharedPointer real_t) +{ + //Check if terminal is already owned by a strip + //return if this strip or false if another strip + if (real_t->parentStrip()) { + if (real_t->parentStrip() != this) + return false; + else + return true; + } + + //Create a new single level physical terminal + auto raw_phy_ptr = new PhysicalTerminal(this, QVector>{real_t}); + m_physical_terminals.append(raw_phy_ptr->sharedRef()); + + emit orderChanged(); + return true; +} + +bool TerminalStrip::removeTerminal(QSharedPointer real_t) +{ + if (real_t->parentStrip() != this) { + return false; + } + + if (auto bridge_ = real_t->bridge()) { + bridge_->removeTerminal(real_t); + } + + if (auto phy_t = real_t->physicalTerminal()) + { + phy_t->removeTerminal(real_t); + if (phy_t->realTerminalCount() == 0) { + m_physical_terminals.removeOne(phy_t); + } + + emit orderChanged(); + return true; + } + + return false; +} + /** * @brief TerminalStrip::addTerminal * Add terminal to this terminal strip @@ -150,6 +193,55 @@ bool TerminalStrip::removeTerminal(Element *terminal) return false; } +/** + * @brief TerminalStrip::addTerminal + * Add @a phy_t in this terminal strip. + * @param phy_t + * @return true if successfully added. A terminal can't be added is already + * belong to another terminal strip. + */ +bool TerminalStrip::addTerminal(QSharedPointer phy_t) +{ + if (phy_t->terminalStrip()) { + if (phy_t->terminalStrip() == this) + return true; + else + return false; + } + + m_physical_terminals.append(phy_t); + phy_t->setParentStrip(this); + + emit orderChanged(); + return true; + +} + +/** + * @brief TerminalStrip::removeTerminal + * Remove @a phy_t from this terminal strip. + * @param phy_t + * @return true if successfully removed. Return false if can't be removed + * because not owned by this strip + */ +bool TerminalStrip::removeTerminal(QSharedPointer phy_t) +{ + if (m_physical_terminals.removeOne(phy_t)) + { + for (const auto &real_t : phy_t->realTerminals()) { + if (auto bridge_ = real_t->bridge()) { + bridge_->removeTerminal(real_t); + } + } + + phy_t->setParentStrip(nullptr); + emit orderChanged(); + return true; + } + + return false; +} + /** * @brief TerminalStrip::pos * @param terminal @@ -206,6 +298,24 @@ QSharedPointer TerminalStrip::physicalTerminal (const QSharedP return QSharedPointer(); } +/** + * @brief TerminalStrip::physicalTerminal + * @param uuid + * @return the the physicalTerminal with the uuid @a uuid or a empty + * QSharedPointer if empty. + */ +QSharedPointer TerminalStrip::physicalTerminal(const QUuid &uuid) const +{ + for (const auto &phy_t : m_physical_terminals) + { + if (phy_t->uuid() == uuid) { + return phy_t; + } + } + + return QSharedPointer(); +} + /** * @brief TerminalStrip::physicalTerminal * @return A vector of all physical terminal owned by this terminal strip. diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 3a6f88789..e4b5df484 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -77,13 +77,18 @@ class TerminalStrip : public QObject TerminalStripData data() const; void setData(const TerminalStripData &data); + bool addTerminal (QSharedPointer real_t); + bool removeTerminal (QSharedPointer real_t); bool addTerminal (Element *terminal); bool removeTerminal (Element *terminal); + bool addTerminal (QSharedPointer phy_t); + bool removeTerminal(QSharedPointer phy_t); int pos(const QSharedPointer &terminal) const; int physicalTerminalCount() const; QSharedPointer physicalTerminal(int index) const; - QSharedPointer physicalTerminal (const QSharedPointer &real_terminal) const; + QSharedPointer physicalTerminal(const QSharedPointer &real_terminal) const; + QSharedPointer physicalTerminal(const QUuid &uuid) const; QVector> physicalTerminal() const; QSharedPointer realTerminalForUuid(const QUuid &uuid) const; QVector> realTerminals() const; diff --git a/sources/TerminalStrip/terminalstripbridge.cpp b/sources/TerminalStrip/terminalstripbridge.cpp index d48fc0d56..368bf956c 100644 --- a/sources/TerminalStrip/terminalstripbridge.cpp +++ b/sources/TerminalStrip/terminalstripbridge.cpp @@ -163,3 +163,7 @@ void TerminalStripBridge::removeTerminals(const QVector &real_terminal) { + m_real_terminals.removeOne(real_terminal); +} diff --git a/sources/TerminalStrip/terminalstripbridge.h b/sources/TerminalStrip/terminalstripbridge.h index 61be0cdd7..ef53db5f2 100644 --- a/sources/TerminalStrip/terminalstripbridge.h +++ b/sources/TerminalStrip/terminalstripbridge.h @@ -50,6 +50,7 @@ class TerminalStripBridge private: bool addTerminals(const QVector> &real_terminals); void removeTerminals(const QVector> &real_terminals); + void removeTerminal(const QSharedPointer &real_terminal); private: diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 5230446e6..4292d5625 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -122,25 +122,31 @@ void TerminalStripEditor::setUpUndoConnections() connect(ui->m_terminal_strip_tw, &TerminalStripTreeWidget::terminalMovedFromStripToStrip, this, [=] (QUuid terminal_uuid, QUuid old_strip_uuid, QUuid new_strip_uuid) { - auto terminal = m_uuid_terminal_H.value(terminal_uuid); auto old_strip = m_uuid_strip_H.value(old_strip_uuid); auto new_strip = m_uuid_strip_H.value(new_strip_uuid); - if (!terminal || !old_strip || !new_strip) { + if (!old_strip || !new_strip) { + return; + } + auto terminal = old_strip->physicalTerminal(terminal_uuid); + if (!terminal) { return; } - auto undo = new AddTerminalToStripCommand(terminal, old_strip, new_strip); + auto undo = new MoveTerminalCommand(terminal, old_strip, new_strip); 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 (!strip_) { + return; + } - if (!terminal_ || !strip_) { + auto terminal_ = strip_->physicalTerminal(terminal_uuid); + if (!terminal_) { return; } @@ -248,12 +254,8 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s } const auto real_t = phy_t->realTerminals().at(0); auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(text_), TerminalStripTreeWidget::Terminal); - terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t->elementUuid()); + terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, phy_t->uuid()); terminal_item->setIcon(0, QET::Icons::ElementTerminal); - - if (real_t->element()) { - m_uuid_terminal_H.insert(real_t->elementUuid(), qgraphicsitem_cast(real_t->element())); - } } } @@ -291,7 +293,7 @@ void TerminalStripEditor::addFreeTerminal() item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, uuid_.toString()); item->setIcon(0, QET::Icons::ElementTerminal); - m_uuid_terminal_H.insert(uuid_, terminal); + m_uuid_terminal_H.insert(uuid_, terminal->realTerminal()); } } diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index ed7a21c7c..ac622380d 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -79,7 +79,7 @@ class TerminalStripEditor : public QDialog QETProject *m_project = nullptr; QHash m_item_strip_H; - QHash> m_uuid_terminal_H; + QHash> m_uuid_terminal_H; QHash> m_uuid_strip_H; TerminalStrip *m_current_strip = nullptr; TerminalStripModel *m_model = nullptr;