diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp new file mode 100644 index 000000000..985f11473 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp @@ -0,0 +1,117 @@ +/* + Copyright 2006-2021 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "addterminaltostripcommand.h" +#include "../../qetgraphicsitem/terminalelement.h" + +/** + * @brief AddTerminalToStripCommand::AddTerminalToStripCommand + * Add \p terminal to \p strip + * @param terminal : terminal to add to strip + * @param strip : terminal strip where terminal must be added + * @param parent : parent undo command + */ +AddTerminalToStripCommand::AddTerminalToStripCommand(TerminalElement *terminal, TerminalStrip *strip, QUndoCommand *parent) : + QUndoCommand(parent), + m_terminal(terminal), + m_new_strip(strip), + m_operation(Operation::add) +{ + auto t_label = terminal->actualLabel(); + auto ts_name = strip->name(); + + auto str_1 = t_label.isEmpty() ? QObject::tr("Ajouter une borne") : + QObject::tr("Ajouter la borne %1").arg(t_label); + + auto str_2 = ts_name.isEmpty() ? QObject::tr("à un groupe de bornes") : + QObject::tr("au groupe de bornes %1").arg(ts_name); + + 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() +{} + +/** + * @brief AddTerminalToStripCommand::undo + * Reimplemented from QUndoCommand + */ +void AddTerminalToStripCommand::undo() +{ + if (!m_terminal || + !m_new_strip) { + return; + } + + m_new_strip->removeTerminal(m_terminal); + + if ( m_operation == Operation::move && + m_old_strip) { + m_old_strip->addTerminal(m_terminal); + } +} + +/** + * @brief AddTerminalToStripCommand::redo + * Reimplemented from QUndoCommand + */ +void AddTerminalToStripCommand::redo() +{ + if (!m_terminal || + !m_new_strip) { + return; + } + + if (m_operation == Operation::move && + m_old_strip) { + m_old_strip->removeTerminal(m_terminal); + } + + m_new_strip->addTerminal(m_terminal); +} diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h new file mode 100644 index 000000000..aff25905c --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h @@ -0,0 +1,60 @@ +/* + Copyright 2006-2021 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef ADDTERMINALTOSTRIPCOMMAND_H +#define ADDTERMINALTOSTRIPCOMMAND_H + +#include +#include + +class TerminalElement; +class TerminalStrip; + +/** + * @brief The AddTerminalToStripCommand class + * Add a terminal element to a terminal strip + * Two cases are handled : + * Add free terminal to strip, + * Move terminal from strip to another strip + */ +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() override; + + void undo() override; + void redo() override; + + private: + enum Operation{ + none, + add, + move, + }; + + QPointer m_terminal; + QPointer m_old_strip; + QPointer m_new_strip; + Operation m_operation = Operation::none; + + +}; + +#endif // ADDTERMINALTOSTRIPCOMMAND_H diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 605c09686..b2c46c7e7 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -23,6 +23,7 @@ #include "../elementprovider.h" #include "../qetgraphicsitem/terminalelement.h" #include "../UndoCommand/addterminalstripcommand.h" +#include "../UndoCommand/addterminaltostripcommand.h" #include "terminalstriptreewidget.h" #include "../../qeticons.h" @@ -40,6 +41,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : { ui->setupUi(this); buildTree(); + setUpUndoConnections(); } /** @@ -49,6 +51,40 @@ TerminalStripEditor::~TerminalStripEditor() { delete ui; } +#include +void TerminalStripEditor::setUpUndoConnections() +{ + connect(ui->m_terminal_strip_tw, &TerminalStripTreeWidget::terminalAddedToStrip, + [this](QUuid terminal_uuid, QUuid strip_uuid) + { + auto terminal = m_uuid_terminal_H.value(terminal_uuid); + auto strip = m_uuid_strip_H.value(strip_uuid); + + if (!terminal || !strip) { + return; + } + + auto undo = new AddTerminalToStripCommand(terminal, strip); + m_project->undoStack()->push(undo); + }); + + 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) { + return; + } + + auto undo = new AddTerminalToStripCommand(terminal, old_strip, new_strip); + m_project->undoStack()->push(undo); + }); + +} + /** * @brief TerminalStripEditor::buildTree * Build the tree widget use to explore terminal strip @@ -88,7 +124,7 @@ void TerminalStripEditor::buildTree() */ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_strip) { - if (auto item = m_H_item_strip.key(terminal_strip)) { + if (auto item = m_item_strip_H.key(terminal_strip)) { return item; } @@ -131,7 +167,12 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s auto item = new QTreeWidgetItem(loc_qtwi, name, TerminalStripTreeWidget::Strip); item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal_strip->uuid()); item->setIcon(0, QET::Icons::TerminalStrip); - m_H_item_strip.insert(item, terminal_strip); + + //Add terminal owned by the strip + //for (auto terminal : terminal_strip->ter) + + m_item_strip_H.insert(item, terminal_strip); + m_uuid_strip_H.insert(terminal_strip->uuid(), terminal_strip); return item; } @@ -158,10 +199,13 @@ void TerminalStripEditor::addFreeTerminal() for (const auto terminal : qAsConst(vector_)) { + QUuid uuid_ = terminal->uuid(); QStringList strl{terminal->actualLabel()}; auto item = new QTreeWidgetItem(free_terminal_item, strl, TerminalStripTreeWidget::Terminal); - item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal->uuid().toString()); + item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, uuid_.toString()); item->setIcon(0, QET::Icons::ElementTerminal); + + m_uuid_terminal_H.insert(uuid_, terminal); } } @@ -204,9 +248,10 @@ void TerminalStripEditor::on_m_add_terminal_strip_pb_clicked() void TerminalStripEditor::on_m_remove_terminal_strip_pb_clicked() { auto item = ui->m_terminal_strip_tw->currentItem(); - if (auto strip = m_H_item_strip.value(item)) + if (auto strip = m_item_strip_H.value(item)) { - m_H_item_strip.remove(item); + m_item_strip_H.remove(item); + m_uuid_strip_H.remove(strip->uuid()); delete item; m_project->undoStack()->push(new RemoveTerminalStripCommand(strip, m_project)); diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index dc86fcded..651f4d2e7 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -43,6 +43,7 @@ class TerminalStripEditor : public QDialog ~TerminalStripEditor() override; private: + void setUpUndoConnections(); void buildTree(); QTreeWidgetItem* addTerminalStrip(TerminalStrip *terminal_strip); void addFreeTerminal(); @@ -55,7 +56,9 @@ class TerminalStripEditor : public QDialog Ui::TerminalStripEditor *ui; QETProject *m_project = nullptr; - QHash m_H_item_strip; + QHash m_item_strip_H; + QHash> m_uuid_terminal_H; + QHash> m_uuid_strip_H; }; #endif // TERMINALSTRIPEDITOR_H diff --git a/sources/TerminalStrip/ui/terminalstriptreewidget.cpp b/sources/TerminalStrip/ui/terminalstriptreewidget.cpp index 1904cdca9..58a996531 100644 --- a/sources/TerminalStrip/ui/terminalstriptreewidget.cpp +++ b/sources/TerminalStrip/ui/terminalstriptreewidget.cpp @@ -107,16 +107,18 @@ void TerminalStripTreeWidget::dropEvent(QDropEvent *event) return; } + auto old_parent_type = dragged_item->parent()->type(); + dragged_item->parent()->removeChild(dragged_item); overred_item->addChild(dragged_item); //Move terminal - if (dragged_item->parent()->type() == FreeTerminal && //From free to strip + if (old_parent_type == FreeTerminal && //From free to strip overred_item->type() == Strip) { emit terminalAddedToStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()), QUuid::fromString(overred_item->data(0, UUID_USER_ROLE).toString())); } - else if (dragged_item->parent()->type() == Strip) //From strip to ... + else if (old_parent_type == Strip) //From strip to ... { if (overred_item->type() == FreeTerminal) //Free terminal {