From 580ebe8b9711648905cab2ba3947d1f2a64fccaa Mon Sep 17 00:00:00 2001 From: joshua Date: Wed, 5 May 2021 22:34:11 +0200 Subject: [PATCH] Terminal strip can now be edited and managed by undo command --- .../UndoCommand/changeterminalstripdata.cpp | 43 +++++++++ .../UndoCommand/changeterminalstripdata.h | 41 +++++++++ sources/TerminalStrip/terminalstrip.cpp | 23 +++++ sources/TerminalStrip/terminalstrip.h | 3 + sources/TerminalStrip/terminalstripdata.cpp | 12 +++ sources/TerminalStrip/terminalstripdata.h | 3 + .../TerminalStrip/ui/terminalstripeditor.cpp | 22 +++++ .../TerminalStrip/ui/terminalstripeditor.h | 3 + .../TerminalStrip/ui/terminalstripeditor.ui | 92 +++++++++++++------ 9 files changed, 215 insertions(+), 27 deletions(-) create mode 100644 sources/TerminalStrip/UndoCommand/changeterminalstripdata.cpp create mode 100644 sources/TerminalStrip/UndoCommand/changeterminalstripdata.h diff --git a/sources/TerminalStrip/UndoCommand/changeterminalstripdata.cpp b/sources/TerminalStrip/UndoCommand/changeterminalstripdata.cpp new file mode 100644 index 000000000..90840d4f6 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/changeterminalstripdata.cpp @@ -0,0 +1,43 @@ +/* + 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 "changeterminalstripdata.h" + +ChangeTerminalStripData::ChangeTerminalStripData(TerminalStrip *strip, + const TerminalStripData &data, + QUndoCommand *parent) : + QUndoCommand(parent), + m_strip(strip), + m_new_data(data) +{ + setText(QObject::tr("Modifier les proriétés d'un groupe de bornes")); + m_old_data = strip->data(); +} + +void ChangeTerminalStripData::undo() +{ + if (m_strip) { + m_strip->setData(m_old_data); + } +} + +void ChangeTerminalStripData::redo() +{ + if (m_strip) { + m_strip->setData(m_new_data); + } +} diff --git a/sources/TerminalStrip/UndoCommand/changeterminalstripdata.h b/sources/TerminalStrip/UndoCommand/changeterminalstripdata.h new file mode 100644 index 000000000..1cec87d72 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/changeterminalstripdata.h @@ -0,0 +1,41 @@ +/* + 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 CHANGETERMINALSTRIPDATA_H +#define CHANGETERMINALSTRIPDATA_H + +#include +#include "../terminalstripdata.h" +#include "../terminalstrip.h" + +/** + * @brief The ChangeTerminalStripData class + */ +class ChangeTerminalStripData : public QUndoCommand +{ + public: + ChangeTerminalStripData(TerminalStrip *strip, const TerminalStripData &data, QUndoCommand *parent = nullptr); + + void undo() override; + void redo() override; + + private: + QPointer m_strip; + TerminalStripData m_old_data, m_new_data; +}; + +#endif // CHANGETERMINALSTRIPDATA_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 9bd675569..04aadd0d6 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -318,6 +318,29 @@ void TerminalStrip::setDescription(const QString &description) { m_data.m_description = description; } +/** + * @brief TerminalStrip::data + * @return The internal data of this strip + */ +TerminalStripData TerminalStrip::data() const { + return m_data; +} + +/** + * @brief TerminalStrip::setData + * The internal data of this strip to data. + * the uuid of the new data is set to the uuid + * of the previous data to keep the uuid + * of the terminal strip unchanged + * @param data + */ +void TerminalStrip::setData(const TerminalStripData &data) +{ + auto uuid_ = m_data.m_uuid; + m_data = data; + m_data.m_uuid = uuid_; +} + /** * @brief TerminalStrip::addTerminal * Add terminal to this terminal strip diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 5c7cbf136..c147fb14b 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -59,6 +59,9 @@ class TerminalStrip : public QObject QString description() const {return m_data.m_description;} QUuid uuid() const {return m_data.m_uuid;} + TerminalStripData data() const; + void setData(const TerminalStripData &data); + bool addTerminal (Element *terminal); bool removeTerminal (Element *terminal); bool haveTerminal (Element *terminal); diff --git a/sources/TerminalStrip/terminalstripdata.cpp b/sources/TerminalStrip/terminalstripdata.cpp index 809a65fc2..89e59a1ca 100644 --- a/sources/TerminalStrip/terminalstripdata.cpp +++ b/sources/TerminalStrip/terminalstripdata.cpp @@ -80,6 +80,18 @@ bool TerminalStripData::fromXml(const QDomElement &xml_element) return true; } +TerminalStripData &TerminalStripData::operator=(const TerminalStripData &other) +{ + m_installation = other.m_installation; + m_location = other.m_location; + m_name = other.m_name; + m_comment = other.m_comment; + m_description = other.m_description; + m_uuid = other.m_uuid; + + return *this; +} + QDomElement TerminalStripData::infoToXml(QDomDocument &xml_doc, const QString &name, const QString &value) { auto xml_elmt = xml_doc.createElement("information"); diff --git a/sources/TerminalStrip/terminalstripdata.h b/sources/TerminalStrip/terminalstripdata.h index d1c6bc337..2dfdc6216 100644 --- a/sources/TerminalStrip/terminalstripdata.h +++ b/sources/TerminalStrip/terminalstripdata.h @@ -25,6 +25,7 @@ class TerminalStripData : public PropertiesInterface { friend class TerminalStrip; + friend class TerminalStripEditor; public: TerminalStripData(); @@ -37,6 +38,8 @@ class TerminalStripData : public PropertiesInterface static QString xmlTagName() {return QStringLiteral("terminal_strip_data");} + TerminalStripData &operator= (const TerminalStripData &other); + private : static QDomElement infoToXml(QDomDocument &xml_doc, const QString &name, const QString &value); diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index b4a0c04f1..aace60a66 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -24,6 +24,7 @@ #include "../qetgraphicsitem/terminalelement.h" #include "../UndoCommand/addterminalstripcommand.h" #include "../UndoCommand/addterminaltostripcommand.h" +#include "../UndoCommand/changeterminalstripdata.h" #include "terminalstriptreewidget.h" #include "../../qeticons.h" @@ -242,6 +243,7 @@ void TerminalStripEditor::clearDataTab() ui->m_name_le ->clear(); ui->m_comment_le ->clear(); ui->m_description_te ->clear(); + m_current_strip = nullptr; } /** @@ -336,6 +338,7 @@ void TerminalStripEditor::on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetI } if (strip_) { + m_current_strip = strip_; ui->m_installation_le ->setText(strip_->installation()); ui->m_location_le ->setText(strip_->location()); ui->m_name_le ->setText(strip_->name()); @@ -345,3 +348,22 @@ void TerminalStripEditor::on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetI clearDataTab(); } } + +void TerminalStripEditor::on_m_apply_data_pb_clicked(QAbstractButton *button) +{ + Q_UNUSED(button) + + if (m_current_strip) + { + TerminalStripData data; + data.m_installation = ui->m_installation_le->text(); + data.m_location = ui->m_location_le->text(); + data.m_name = ui->m_name_le->text(); + data.m_comment = ui->m_comment_le->text(); + data.m_description = ui->m_description_te->toPlainText(); + + m_project->undoStack()->push(new ChangeTerminalStripData(m_current_strip, data, nullptr)); + } + + on_m_reload_pb_clicked(); +} diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index 3cafcabd1..efdf91d7a 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -28,6 +28,7 @@ class QETProject; class TerminalStrip; class QTreeWidgetItem; class TerminalElement; +class QAbstractButton; /** * @brief The TerminalStripEditor class @@ -54,6 +55,7 @@ class TerminalStripEditor : public QDialog void on_m_remove_terminal_strip_pb_clicked(); void on_m_reload_pb_clicked(); void on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); + void on_m_apply_data_pb_clicked(QAbstractButton *button); private: Ui::TerminalStripEditor *ui; @@ -62,6 +64,7 @@ class TerminalStripEditor : public QDialog QHash m_item_strip_H; QHash> m_uuid_terminal_H; QHash> m_uuid_strip_H; + TerminalStrip *m_current_strip = nullptr; }; #endif // TERMINALSTRIPEDITOR_H diff --git a/sources/TerminalStrip/ui/terminalstripeditor.ui b/sources/TerminalStrip/ui/terminalstripeditor.ui index f2bef4c26..7db32253b 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.ui +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -6,8 +6,8 @@ 0 0 - 706 - 396 + 951 + 491 @@ -146,6 +146,13 @@ Propriétés + + + + Description + + + @@ -160,12 +167,8 @@ - - - - Nom : - - + + @@ -174,6 +177,22 @@ + + + + + + + Nom : + + + + + + + + + @@ -181,28 +200,47 @@ - - - - Description - - - - - - - - - - - - - - - + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + QDialogButtonBox::Apply + + + + + +