Add and move terminal element to strip is managed by undo

This commit is contained in:
joshua
2021-04-28 20:35:39 +02:00
parent 171e95fb17
commit 256d42d030
5 changed files with 235 additions and 8 deletions

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef ADDTERMINALTOSTRIPCOMMAND_H
#define ADDTERMINALTOSTRIPCOMMAND_H
#include <QUndoCommand>
#include <QPointer>
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<TerminalElement> m_terminal;
QPointer<TerminalStrip> m_old_strip;
QPointer<TerminalStrip> m_new_strip;
Operation m_operation = Operation::none;
};
#endif // ADDTERMINALTOSTRIPCOMMAND_H

View File

@@ -23,6 +23,7 @@
#include "../elementprovider.h" #include "../elementprovider.h"
#include "../qetgraphicsitem/terminalelement.h" #include "../qetgraphicsitem/terminalelement.h"
#include "../UndoCommand/addterminalstripcommand.h" #include "../UndoCommand/addterminalstripcommand.h"
#include "../UndoCommand/addterminaltostripcommand.h"
#include "terminalstriptreewidget.h" #include "terminalstriptreewidget.h"
#include "../../qeticons.h" #include "../../qeticons.h"
@@ -40,6 +41,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
buildTree(); buildTree();
setUpUndoConnections();
} }
/** /**
@@ -49,6 +51,40 @@ TerminalStripEditor::~TerminalStripEditor() {
delete ui; delete ui;
} }
#include <QHashIterator>
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 * @brief TerminalStripEditor::buildTree
* Build the tree widget use to explore terminal strip * Build the tree widget use to explore terminal strip
@@ -88,7 +124,7 @@ void TerminalStripEditor::buildTree()
*/ */
QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_strip) 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; return item;
} }
@@ -131,7 +167,12 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s
auto item = new QTreeWidgetItem(loc_qtwi, name, TerminalStripTreeWidget::Strip); auto item = new QTreeWidgetItem(loc_qtwi, name, TerminalStripTreeWidget::Strip);
item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal_strip->uuid()); item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal_strip->uuid());
item->setIcon(0, QET::Icons::TerminalStrip); 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; return item;
} }
@@ -158,10 +199,13 @@ void TerminalStripEditor::addFreeTerminal()
for (const auto terminal : qAsConst(vector_)) for (const auto terminal : qAsConst(vector_))
{ {
QUuid uuid_ = terminal->uuid();
QStringList strl{terminal->actualLabel()}; QStringList strl{terminal->actualLabel()};
auto item = new QTreeWidgetItem(free_terminal_item, strl, TerminalStripTreeWidget::Terminal); 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); 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() void TerminalStripEditor::on_m_remove_terminal_strip_pb_clicked()
{ {
auto item = ui->m_terminal_strip_tw->currentItem(); 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; delete item;
m_project->undoStack()->push(new RemoveTerminalStripCommand(strip, m_project)); m_project->undoStack()->push(new RemoveTerminalStripCommand(strip, m_project));

View File

@@ -43,6 +43,7 @@ class TerminalStripEditor : public QDialog
~TerminalStripEditor() override; ~TerminalStripEditor() override;
private: private:
void setUpUndoConnections();
void buildTree(); void buildTree();
QTreeWidgetItem* addTerminalStrip(TerminalStrip *terminal_strip); QTreeWidgetItem* addTerminalStrip(TerminalStrip *terminal_strip);
void addFreeTerminal(); void addFreeTerminal();
@@ -55,7 +56,9 @@ class TerminalStripEditor : public QDialog
Ui::TerminalStripEditor *ui; Ui::TerminalStripEditor *ui;
QETProject *m_project = nullptr; QETProject *m_project = nullptr;
QHash<QTreeWidgetItem *, TerminalStrip *> m_H_item_strip; QHash<QTreeWidgetItem *, TerminalStrip *> m_item_strip_H;
QHash<QUuid, QPointer<TerminalElement>> m_uuid_terminal_H;
QHash<QUuid, QPointer<TerminalStrip>> m_uuid_strip_H;
}; };
#endif // TERMINALSTRIPEDITOR_H #endif // TERMINALSTRIPEDITOR_H

View File

@@ -107,16 +107,18 @@ void TerminalStripTreeWidget::dropEvent(QDropEvent *event)
return; return;
} }
auto old_parent_type = dragged_item->parent()->type();
dragged_item->parent()->removeChild(dragged_item); dragged_item->parent()->removeChild(dragged_item);
overred_item->addChild(dragged_item); overred_item->addChild(dragged_item);
//Move terminal //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) { overred_item->type() == Strip) {
emit terminalAddedToStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()), emit terminalAddedToStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()),
QUuid::fromString(overred_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 if (overred_item->type() == FreeTerminal) //Free terminal
{ {