diff --git a/qelectrotech.pro b/qelectrotech.pro index f182f8587..997e6511f 100644 --- a/qelectrotech.pro +++ b/qelectrotech.pro @@ -160,7 +160,10 @@ HEADERS += $$files(sources/*.h) $$files(sources/ui/*.h) \ $$files(sources/dataBase/*.h) \ $$files(sources/dataBase/ui/*.h) \ $$files(sources/factory/ui/*.h) \ - $$files(sources/print/*.h) + $$files(sources/print/*.h) \ + $$files(sources/TerminalStrip/*.h) \ + $$files(sources/TerminalStrip/ui/*.h) \ + $$files(sources/TerminalStrip/UndoCommand/*.h) SOURCES += $$files(sources/*.cpp) \ $$files(sources/editor/*.cpp) \ @@ -193,7 +196,13 @@ SOURCES += $$files(sources/*.cpp) \ $$files(sources/dataBase/*.cpp) \ $$files(sources/dataBase/ui/*.cpp) \ $$files(sources/factory/ui/*.cpp) \ - $$files(sources/print/*.cpp) + $$files(sources/print/*.cpp) \ + $$files(sources/TerminalStrip/*.cpp) \ + $$files(sources/TerminalStrip/ui/*.cpp) \ + $$files(sources/TerminalStrip/UndoCommand/*.cpp) + +# Needed for use promote QTreeWidget in terminalstripeditor.ui +INCLUDEPATH += sources/TerminalStrip/ui # Liste des fichiers qui seront incorpores au binaire en tant que ressources Qt RESOURCES += qelectrotech.qrc @@ -219,7 +228,8 @@ FORMS += $$files(sources/richtext/*.ui) \ $$files(sources/qetgraphicsitem/ViewItem/ui/*.ui) \ $$files(sources/dataBase/ui/*.ui) \ $$files(sources/factory/ui/*.ui) \ - $$files(sources/print/*.ui) + $$files(sources/print/*.ui) \ + $$files(sources/TerminalStrip/ui/*.ui) UI_SOURCES_DIR = sources/ui/ UI_HEADERS_DIR = sources/ui/ diff --git a/sources/TerminalStrip/UndoCommand/addterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/addterminalstripcommand.cpp new file mode 100644 index 000000000..46fe13bdd --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/addterminalstripcommand.cpp @@ -0,0 +1,87 @@ +/* + 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 "addterminalstripcommand.h" +#include "../../qetproject.h" +#include "../terminalstrip.h" +#include "../qetgraphicsitem/element.h" + +#include + +/** + * @brief AddTerminalStripCommand::AddTerminalStripCommand + * @param strip + * @param parent + */ +AddTerminalStripCommand::AddTerminalStripCommand(TerminalStrip *strip, + QETProject *project, + QUndoCommand *parent) : + QUndoCommand(parent), + m_strip(strip), + m_project(project) +{ + setText(QObject::tr("Ajouter un groupe de bornes")); +} + +AddTerminalStripCommand::~AddTerminalStripCommand() +{} + +void AddTerminalStripCommand::undo() { + if (m_project && m_strip) { + m_project->removeTerminalStrip(m_strip); + } +} + +void AddTerminalStripCommand::redo() { + if (m_project && m_strip) { + m_project->addTerminalStrip(m_strip); + } +} + +RemoveTerminalStripCommand::RemoveTerminalStripCommand(TerminalStrip *strip, + QETProject *project, + QUndoCommand *parent) : + QUndoCommand(parent), + m_strip(strip), + m_project(project), + m_elements(strip->terminalElement()) +{ + setText(QObject::tr("Supprimer un groupe de bornes")); +} + +RemoveTerminalStripCommand::~RemoveTerminalStripCommand() +{} + +void RemoveTerminalStripCommand::undo() +{ + if (m_project && m_strip) { + for (auto elmt : m_elements) { + m_strip->addTerminal(elmt); + } + m_project->addTerminalStrip(m_strip); + } +} + +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 new file mode 100644 index 000000000..8317479e4 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/addterminalstripcommand.h @@ -0,0 +1,57 @@ +/* + 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 ADDTERMINALSTRIPCOMMAND_H +#define ADDTERMINALSTRIPCOMMAND_H + +#include +#include + +class TerminalStrip; +class QETProject; +class Element; + +class AddTerminalStripCommand : public QUndoCommand +{ + public: + AddTerminalStripCommand(TerminalStrip *strip, QETProject *project, QUndoCommand *parent = nullptr); + ~AddTerminalStripCommand() override; + + void undo() override; + void redo() override; + + private: + QPointer m_strip; + QPointer m_project; +}; + +class RemoveTerminalStripCommand : public QUndoCommand +{ + public: + RemoveTerminalStripCommand(TerminalStrip *strip, QETProject *project, QUndoCommand *parent = nullptr); + ~RemoveTerminalStripCommand() override; + + void undo() override; + void redo() override; + + private: + QPointer m_strip; + QPointer m_project; + QVector> m_elements; +}; + +#endif // ADDTERMINALSTRIPCOMMAND_H diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp new file mode 100644 index 000000000..f5e59239f --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.cpp @@ -0,0 +1,156 @@ +/* + 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); +} + +/** + * @brief RemoveTerminalFromStripCommand::RemoveTerminalFromStripCommand + * @param terminal + * @param strip + * @param parent + */ +RemoveTerminalFromStripCommand::RemoveTerminalFromStripCommand(TerminalElement *terminal, + TerminalStrip *strip, + QUndoCommand *parent) : + QUndoCommand(parent), + m_terminal(terminal), + m_strip(strip) +{ + auto t_label = terminal->actualLabel(); + auto strip_name = strip->name(); + + auto str_1 = t_label.isEmpty() ? QObject::tr("Enlever une borne") : + QObject::tr("Enlever 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); + setText(str_1 + " " + str_2); +} + +void RemoveTerminalFromStripCommand::undo() +{ + if (m_terminal && m_strip) { + m_strip->addTerminal(m_terminal); + } +} + +void RemoveTerminalFromStripCommand::redo() +{ + if (m_terminal && m_strip) { + m_strip->removeTerminal(m_terminal); + } +} + diff --git a/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h new file mode 100644 index 000000000..bf2313fb1 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/addterminaltostripcommand.h @@ -0,0 +1,79 @@ +/* + 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; + + +}; + +/** + * @brief The RemoveTerminalFromStripCommand class + * Remove a terminal from a terminal strip. + * The removed terminal become free. + */ +class RemoveTerminalFromStripCommand : public QUndoCommand +{ + public: + RemoveTerminalFromStripCommand (TerminalElement *terminal, TerminalStrip *strip, QUndoCommand *parent = nullptr); + ~RemoveTerminalFromStripCommand() override {} + + void undo() override; + void redo() override; + + private: + QPointer m_terminal; + QPointer m_strip; +}; + +#endif // ADDTERMINALTOSTRIPCOMMAND_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 new file mode 100644 index 000000000..04aadd0d6 --- /dev/null +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -0,0 +1,634 @@ +/* + 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 "terminalstrip.h" +#include "../qetproject.h" +#include "../qetgraphicsitem/element.h" +#include "../qetgraphicsitem/terminalelement.h" +#include "../elementprovider.h" +#include "../qetxml.h" + +using shared_real_terminal = QSharedPointer; +using shared_physical_terminal = QSharedPointer; + + +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ + +/** + * @brief The RealTerminal class + * Represent a real terminal. + * A real terminal can be a drawed terminal in a folio + * or a terminal set by user but not present + * on any folio (for example a reserved terminal). + */ +class RealTerminal +{ + public : + + /** + * @brief RealTerminal + * @param parent_strip : parent terminal strip + * @param terminal : terminal element (if any) in a folio + */ + RealTerminal(TerminalStrip *parent_strip, Element *terminal = nullptr) : + m_element(terminal), + m_parent_terminal_strip(parent_strip) + {} + + /** + * @brief isElement + * @return true if this real terminal is linked to a terminal element + */ + bool isElement() const { + return m_element.isNull() ? false : true; + } + + /** + * @brief element + * @return the element linked to this real terminal + * or nullptr if not linked to an Element. + */ + Element *element() const { + return m_element.data(); + } + + /** + * @brief label + * @return the label of this real terminal + */ + QString label() const { + if (!m_element.isNull()) { + return m_element->actualLabel(); + } else { + return QStringLiteral(""); + } + } + + /** + * @brief elementUuid + * @return if this real terminal is an element + * in a folio, return the uuid of the element + * else return a null uuid. + */ + QUuid elementUuid() const { + if (!m_element.isNull()) { + return m_element->uuid(); + } else { + return QUuid(); + } + } + + /** + * @brief uuid + * @return the uuid of this real terminal + */ + QUuid uuid() const { + return m_uuid; + } + + static QString xmlTagName() { + return QStringLiteral("real_terminal"); + } + + /** + * @brief toXml + * @param parent_document + * @return this real terminal to xml + */ + QDomElement toXml(QDomDocument &parent_document) const + { + auto root_elmt = parent_document.createElement(this->xmlTagName()); + root_elmt.setAttribute("is_draw", m_element ? "true" : "false"); + root_elmt.setAttribute("uuid", m_element ? m_element->uuid().toString() : + m_uuid.toString()); + + return root_elmt; + } + + /** + * @brief fromXml + * @param xml_element + * @return + */ + bool fromXml(QDomElement xml_element, const QVector &terminal_vector) + { + if (xml_element.tagName() != xmlTagName()) { + return true; + } + + auto is_draw = xml_element.attribute(QStringLiteral("is_draw")) == QLatin1String("true") + ? true : false; + auto uuid_ = QUuid::fromString(xml_element.attribute(QStringLiteral("uuid"))); + + if (is_draw) { + for (auto terminal : terminal_vector) { + if (terminal->uuid() == uuid_) + { + m_element = terminal; + break; + } + } + } else { + m_uuid = uuid_; + } + + return true; + } + + private : + QPointer m_element; + QPointer m_parent_terminal_strip; + QUuid m_uuid = QUuid::createUuid(); +}; + + +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ + + + + +/** + * @brief The PhysicalTerminal class + * Represent a physical terminal. + * A physical terminal is composed a least by one real terminal. + * When a physical terminal have more than one real terminal + * that mean the physical terminal have levels (one by real terminal). + * The index of terminals returned by the function terminals() + * is the same as the real level of the physical terminal, the index are from back to front. + * + * Example for a 3 levels terminal. + * index 0 = back (mounting plate) + * index 1 = middle + * index 2 = front (electrical cabinet door) + * + * m + * o _ + * u | | + * n | | _ + * t | || | + * i | || | _ + * n | || || | d + * g |0||1||2| o + * | || ||_| o + * p | || | r + * l | ||_| + * a | | + * t |_| + * e + * + */ +class PhysicalTerminal +{ + public: + /** + * @brief PhysicalTerminal + * @param parent_strip : Parent terminal strip + * @param terminals : A vector of real terminals + * who compose this physical terminal. + * \p terminals must have at least one terminal + */ + PhysicalTerminal(TerminalStrip *parent_strip, + QVector terminals) : + m_parent_terminal_strip(parent_strip), + m_real_terminal(terminals) + {} + + /** + * @brief setTerminals + * Set the real terminal of this physical terminal + * the order of the terminal in \p terminals represent + * the level index. + * @param terminals + */ + void setTerminals(QVector terminals) { + m_real_terminal = terminals; + } + + /** + * @brief levelCount + * @return the number of level of this terminal + */ + int levelCount() const { + return m_real_terminal.size(); + } + + /** + * @brief terminals + * @return A vector of real terminal who compose this physical terminal + */ + QVector terminals() const { + return m_real_terminal; + } + + static QString xmlTagName() { + return QStringLiteral("physical_terminal"); + } + + /** + * @brief toXml + * @param parent_document + * @return this physical terminal to xml + */ + QDomElement toXml(QDomDocument &parent_document) const + { + auto root_elmt = parent_document.createElement(this->xmlTagName()); + for (auto &real_t : m_real_terminal) { + root_elmt.appendChild(real_t->toXml(parent_document)); + } + + return root_elmt; + } + + private: + QPointer m_parent_terminal_strip; + QVector m_real_terminal; +}; + + +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ + +/** + * @brief TerminalStrip::TerminalStrip + * @param project + */ +TerminalStrip::TerminalStrip(QETProject *project) : + QObject(project), + m_project(project) +{} + +/** + * @brief TerminalStrip::TerminalStrip + * @param installation + * @param location + * @param name + * @param project + */ +TerminalStrip::TerminalStrip(const QString &installation, const QString &location, const QString &name, QETProject *project) : + QObject(project), + m_project(project) +{ + m_data.m_installation = installation; + m_data.m_location = location; + m_data.m_name = name; +} + +void TerminalStrip::setInstallation(const QString &installation) { + m_data.m_installation = installation; +} + +void TerminalStrip::setLocation(const QString &location) { + m_data.m_location = location; +} + +void TerminalStrip::setName(const QString &name) { + m_data.m_name = name; +} + +void TerminalStrip::setComment(const QString &comment) { + m_data.m_comment = comment; +} + +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 + * @param terminal + * @return true if the terminal was successfully added. + * Return false, if terminal already exist. + * Return false, if terminal is not a terminal element. + */ +bool TerminalStrip::addTerminal(Element *terminal) +{ + if (m_terminal_elements_vector.contains(terminal)) { + return false; + } + if (terminal->elementData().m_type != ElementData::Terminale) { + 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); + + //Create a new single level physical terminal + shared_physical_terminal physical_terminal( + new PhysicalTerminal(this, + QVector{real_terminal})); + + m_physical_terminals.append(physical_terminal); + + static_cast(terminal)->setParentTerminalStrip(this); + + return true; +} + +/** + * @brief TerminalStrip::removeTerminal + * Remove terminal from this terminal strip + * @param terminal + * @return true if terminal was successfully removed + */ +bool TerminalStrip::removeTerminal(Element *terminal) +{ + if (m_terminal_elements_vector.contains(terminal)) + { + m_terminal_elements_vector.removeOne(terminal); + + //Get the real and physical terminal associated to @terminal + if (auto real_terminal = realTerminal(terminal)) + { + 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; + } + + //There is no reason to be here, but in case of.... + return false; + } + return false; +} + +/** + * @brief TerminalStrip::haveTerminal + * @param terminal + * @return true if \p terminal belong to this strip + */ +bool TerminalStrip::haveTerminal(Element *terminal) { + return m_terminal_elements_vector.contains(terminal); +} + +/** + * @brief TerminalStrip::physicalTerminalCount + * @return the number of physical terminal. + * A physical terminal is the representation of a real electrical terminal. + * Notice that a physical terminal can have level (like in real life) + */ +int TerminalStrip::physicalTerminalCount() const { + return m_physical_terminals.size(); +} + +TerminalStripIndex TerminalStrip::index(int index) +{ + TerminalStripIndex tsi_; + + if (index < 0 || + index >= m_physical_terminals.size()) { + return tsi_; + } + + auto phy_term = m_physical_terminals.at(index); + + for(auto &real_term : phy_term->terminals()) { + tsi_.m_label.append(real_term->label()); + tsi_.m_uuid.append(real_term->elementUuid()); + tsi_.m_is_element.append(real_term->isElement()); + tsi_.m_element.append(static_cast(real_term->element())); + } + + tsi_.m_valid = true; + 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::toXml + * @param parent_document + * @return + */ +QDomElement TerminalStrip::toXml(QDomDocument &parent_document) +{ + auto root_elmt = parent_document.createElement(this->xmlTagName()); + + root_elmt.appendChild(m_data.toXml(parent_document)); + + //Undrawed terminals + auto xml_layout = parent_document.createElement("layout"); + for (auto &phy_t : m_physical_terminals) { + xml_layout.appendChild(phy_t->toXml(parent_document)); + } + root_elmt.appendChild(xml_layout); + + return root_elmt; +} + +/** + * @brief TerminalStrip::fromXml + * @param xml_element + * @return Set up this terminal strip from the xml description \p xml_element + */ +bool TerminalStrip::fromXml(QDomElement &xml_element) +{ + if (xml_element.tagName() != xmlTagName()) { + return false; + } + + //Read terminal strip data + auto xml_data = xml_element.firstChildElement(m_data.xmlTagName()); + if (!xml_data.isNull()) { + m_data.fromXml(xml_data); + } + + //Read layout + auto xml_layout = xml_element.firstChildElement(QStringLiteral("layout")); + if (!xml_layout.isNull()) + { + //Get all free elements terminal of the project + ElementProvider ep(m_project); + auto free_terminals = ep.freeTerminal(); + + //Read each physical terminal + for(auto &xml_physical : QETXML::findInDomElement(xml_layout, PhysicalTerminal::xmlTagName())) + { + QVector real_t_vector; + + //Read each real terminal of the current physical terminal of the loop + for (auto &xml_real : QETXML::findInDomElement(xml_physical, RealTerminal::xmlTagName())) + { + shared_real_terminal real_t(new RealTerminal(this)); + real_t->fromXml(xml_real, free_terminals); + if(real_t->isElement()) + { + m_terminal_elements_vector.append(real_t->element()); + static_cast(real_t->element())->setParentTerminalStrip(this); + } + real_t_vector.append(real_t); + } + + shared_physical_terminal phy_t(new PhysicalTerminal(this, real_t_vector)); + m_physical_terminals.append(phy_t); + m_real_terminals.append(real_t_vector); + } + + } + + return true; +} + +/** + * @brief TerminalStrip::realTerminal + * @param terminal + * @return the real terminal linked to \p terminal + * the returned QSharedPointer can be null. + */ +QSharedPointer TerminalStrip::realTerminal(Element *terminal) +{ + shared_real_terminal rt; + + for (auto &real : qAsConst(m_real_terminals)) { + if (real->element() == terminal) { + return real; + } + } + + return rt; +} + +/** + * @brief TerminalStrip::physicalTerminal + * @param terminal + * @return the physical terminal linked to \p terminal. + * The returned QSharedPointer can be null. + */ +QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer terminal) +{ + shared_physical_terminal pt; + + for (auto &physical : qAsConst(m_physical_terminals)) + { + if (physical->terminals().contains(terminal)) + { + pt = physical; + break; + } + } + + return pt; +} + +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ + + + + + +bool TerminalStripIndex::isValid() const +{ + return m_valid; +} + +QString TerminalStripIndex::label(int level) const +{ + if (level<0 || + level >= m_label.size()) { + return QStringLiteral(""); + } + + return m_label.at(level); +} + +QUuid TerminalStripIndex::uuid(int level) const +{ + if (level<0 || + level >= m_uuid.size()) { + return QUuid(); + } + + return m_uuid.at(level); +} + +bool TerminalStripIndex::isElement(int level) const +{ + if (level<0 || + level >= m_is_element.size()) { + return false; + } + + return m_is_element.at(level); +} + +TerminalElement *TerminalStripIndex::element(int level) const +{ + if (level<0 || + level >= m_element.size()) { + return nullptr; + } + + return m_element.at(level); +} diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h new file mode 100644 index 000000000..c147fb14b --- /dev/null +++ b/sources/TerminalStrip/terminalstrip.h @@ -0,0 +1,113 @@ +/* + 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 TERMINALSTRIP_H +#define TERMINALSTRIP_H + +#include +#include +#include "terminalstripdata.h" + +class Element; +class RealTerminal; +class QETProject; +class PhysicalTerminal; +class TerminalStripIndex; +class TerminalElement; + +/** + * @brief The TerminalStrip class + * This class hold all the datas and configurations + * of a terminal strip (but the not the visual aspect). + * A terminal strip have some informations (name comment etc...) + * and is composed by terminals (draw in a diagram or described in the terminal strip) + */ +class TerminalStrip : public QObject +{ + Q_OBJECT + public: + TerminalStrip(QETProject *project); + + TerminalStrip(const QString &installation, + const QString &location, + const QString &name, + QETProject *project); + + void setInstallation(const QString &installation); + QString installation() const {return m_data.m_installation;} + void setLocation(const QString &location); + QString location() const {return m_data.m_location;} + void setName(const QString &name); + QString name() const {return m_data.m_name;} + void setComment(const QString &comment); + QString comment() const {return m_data.m_comment;} + void setDescription(const QString &description); + 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); + + int physicalTerminalCount() const; + TerminalStripIndex index(int index = 0); + + QVector> terminalElement() const; + + static QString xmlTagName() {return QStringLiteral("terminal_strip");} + QDomElement toXml(QDomDocument &parent_document); + bool fromXml(QDomElement &xml_element); + + private: + QSharedPointer realTerminal(Element *terminal); + QSharedPointer physicalTerminal(QSharedPointer terminal); + + private: + TerminalStripData m_data; + QPointer m_project; + QVector> m_terminal_elements_vector; + QVector> m_real_terminals; + QVector> m_physical_terminals; +}; + +class TerminalStripIndex +{ + friend class TerminalStrip; + + private : + TerminalStripIndex () {} + TerminalStripIndex (TerminalStripIndex *) {} + + public: + bool isValid() const; + QString label(int level = 0) const; + QUuid uuid(int level = 0) const; + bool isElement(int level = 0) const; + TerminalElement *element(int level = 0) const; + + private: + QVector m_label; + QVector m_uuid; + bool m_valid = false; + QVector m_is_element; + QVector m_element; +}; + +#endif // TERMINALSTRIP_H diff --git a/sources/TerminalStrip/terminalstripdata.cpp b/sources/TerminalStrip/terminalstripdata.cpp new file mode 100644 index 000000000..89e59a1ca --- /dev/null +++ b/sources/TerminalStrip/terminalstripdata.cpp @@ -0,0 +1,102 @@ +/* + 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 "terminalstripdata.h" +#include "../qetxml.h" +#include + +TerminalStripData::TerminalStripData() +{ + +} + +QDomElement TerminalStripData::toXml(QDomDocument &xml_document) const +{ + auto root_elmt = xml_document.createElement(this->xmlTagName()); + + root_elmt.setAttribute(QStringLiteral("uuid"), m_uuid.toString()); + + auto info_elmt = xml_document.createElement("informations"); + root_elmt.appendChild(info_elmt); + + if (!m_installation.isEmpty()) { + info_elmt.appendChild(infoToXml(xml_document, QStringLiteral("installation"), m_installation)); + } + if (!m_location.isEmpty()) { + info_elmt.appendChild(infoToXml(xml_document, QStringLiteral("location"), m_location)); + } + if (!m_name.isEmpty()) { + info_elmt.appendChild(infoToXml(xml_document, QStringLiteral("name"), m_name)); + } + if (!m_comment.isEmpty()) { + info_elmt.appendChild(infoToXml(xml_document, QStringLiteral("comment"), m_comment)); + } + if (!m_description.isEmpty()) { + info_elmt.appendChild(infoToXml(xml_document, QStringLiteral("description"), m_description)); + } + + return root_elmt; +} + +bool TerminalStripData::fromXml(const QDomElement &xml_element) +{ + if (xml_element.tagName() != this->xmlTagName()) + { + qDebug() << "TerminalStripData::fromXml : failed to load from xml " \ + "due to wrong tag name. Expected " << this->xmlTagName() << " used " << xml_element.tagName(); + return false; + } + + m_uuid.fromString(xml_element.attribute("uuid")); + + for (auto &xml_info : + QETXML::findInDomElement(xml_element.firstChildElement("informations"), + QStringLiteral("information"))) + { + auto name = xml_info.attribute("name"); + auto value = xml_info.text(); + + if (name == QStringLiteral("installation")) { m_installation = value;} + else if (name == QStringLiteral("location")) {m_location = value;} + else if (name == QStringLiteral("name")) {m_name = value;} + else if (name == QStringLiteral("comment")) {m_comment = value;} + else if (name == QStringLiteral("description")) {m_description = value;} + } + + 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"); + xml_elmt.setAttribute(QStringLiteral("name"), name); + xml_elmt.appendChild(xml_doc.createTextNode(value)); + + return xml_elmt; +} diff --git a/sources/TerminalStrip/terminalstripdata.h b/sources/TerminalStrip/terminalstripdata.h new file mode 100644 index 000000000..2dfdc6216 --- /dev/null +++ b/sources/TerminalStrip/terminalstripdata.h @@ -0,0 +1,55 @@ +/* + 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 TERMINALSTRIPDATA_H +#define TERMINALSTRIPDATA_H + +#include "../properties/propertiesinterface.h" + +#include + +class TerminalStripData : public PropertiesInterface +{ + friend class TerminalStrip; + friend class TerminalStripEditor; + + public: + TerminalStripData(); + + void toSettings(QSettings &/*settings*/, const QString = QString()) const override {} + void fromSettings (const QSettings &/*settings*/, const QString = QString()) override {} + + QDomElement toXml (QDomDocument &xml_document) const override; + bool fromXml (const QDomElement &xml_element) override; + + 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); + + QString m_installation = QStringLiteral("="), + m_location = QStringLiteral("+"), + m_name, + m_comment, + m_description; + QUuid m_uuid = QUuid::createUuid(); + +}; + +#endif // TERMINALSTRIPDATA_H diff --git a/sources/TerminalStrip/ui/terminalstripcreatordialog.cpp b/sources/TerminalStrip/ui/terminalstripcreatordialog.cpp new file mode 100644 index 000000000..8b048e935 --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstripcreatordialog.cpp @@ -0,0 +1,107 @@ +/* + 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 "terminalstripcreatordialog.h" +#include "ui_terminalstripcreatordialog.h" +#include "../terminalstrip.h" +#include "../../qetproject.h" + +/** + * @brief TerminalStripCreatorDialog::TerminalStripCreatorDialog + * @param project : Project to add a new terminal strip + * @param parent : parent widget + */ +TerminalStripCreatorDialog::TerminalStripCreatorDialog(QETProject *project, QWidget *parent) : + QDialog(parent), + ui(new Ui::TerminalStripCreatorDialog), + m_project(project) +{ + ui->setupUi(this); +} + +/** + * @brief TerminalStripCreatorDialog::~TerminalStripCreatorDialog + */ +TerminalStripCreatorDialog::~TerminalStripCreatorDialog() { + delete ui; +} + +/** + * @brief TerminalStripCreatorDialog::setInstallation + * Set the installation field string + * @param installation + */ +void TerminalStripCreatorDialog::setInstallation(const QString &installation) { + ui->m_installation_le->setText(installation); + setCursorToEmptyLine(); +} + +/** + * @brief TerminalStripCreatorDialog::setLocation + * Set the location field string + * @param location + */ +void TerminalStripCreatorDialog::setLocation(const QString &location) { + ui->m_location_le->setText(location); + setCursorToEmptyLine(); +} + +/** + * @brief TerminalStripCreatorDialog::generatedTerminalStrip + * @return A new terminal Strip according to the value set by user. + * The terminal strip is already added to the terminalStrip list of the project + * so it's ready to use. + */ +TerminalStrip *TerminalStripCreatorDialog::generatedTerminalStrip() const +{ + QString installation_ = ui->m_installation_le->text(); + QString location_ = ui->m_location_le->text(); + QString name_ = ui->m_name_le->text(); + + if (installation_.isEmpty()) { + installation_ = QStringLiteral("=INST"); } + if (location_.isEmpty()) { + location_ = QStringLiteral("+LOC"); } + if (name_.isEmpty()) { + name_ = QStringLiteral("X"); } + + auto strip = m_project->newTerminalStrip(installation_, + location_, + name_); + strip->setComment(ui->m_comment_le->text()); + strip->setDescription(ui->m_description_te->toPlainText()); + + return strip; +} + +/** + * @brief TerminalStripCreatorDialog::setCursorToEmptyLine + * Set the cursor to the first empty field. + * It's usefull when user create a new terminal strip + * with some value prefilled, to increase productivity. + */ +void TerminalStripCreatorDialog::setCursorToEmptyLine() +{ + if (ui->m_installation_le->text().isEmpty()) { + return; + } + if (ui->m_location_le->text().isEmpty()) { + ui->m_location_le->setFocus(); + return; + } + ui->m_name_le->setFocus(); +} diff --git a/sources/TerminalStrip/ui/terminalstripcreatordialog.h b/sources/TerminalStrip/ui/terminalstripcreatordialog.h new file mode 100644 index 000000000..22deb7ddf --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstripcreatordialog.h @@ -0,0 +1,54 @@ +/* + 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 TERMINALSTRIPCREATORDIALOG_H +#define TERMINALSTRIPCREATORDIALOG_H + +#include + +class TerminalStrip; +class QETProject; + +namespace Ui { + class TerminalStripCreatorDialog; +} + +/** + * @brief The TerminalStripCreatorDialog class + * A simple dialog for create a new terminal strip + */ +class TerminalStripCreatorDialog : public QDialog +{ + Q_OBJECT + + public: + explicit TerminalStripCreatorDialog(QETProject *project, QWidget *parent = nullptr); + ~TerminalStripCreatorDialog() override; + + void setInstallation(const QString &installation); + void setLocation(const QString &location); + TerminalStrip *generatedTerminalStrip() const; + + private: + void setCursorToEmptyLine(); + + private: + Ui::TerminalStripCreatorDialog *ui; + QETProject *m_project = nullptr; +}; + +#endif // TERMINALSTRIPCREATORDIALOG_H diff --git a/sources/TerminalStrip/ui/terminalstripcreatordialog.ui b/sources/TerminalStrip/ui/terminalstripcreatordialog.ui new file mode 100644 index 000000000..803e37f0d --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstripcreatordialog.ui @@ -0,0 +1,146 @@ + + + TerminalStripCreatorDialog + + + + 0 + 0 + 744 + 321 + + + + Création groupe de bornes + + + + + + Localisation : + + + + + + + Nom : + + + + + + + Installation : + + + + + + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Description : + + + + + + + Qt::Horizontal + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + Commentaire : + + + + + + + + + + m_installation_le + m_location_le + m_name_le + m_comment_le + m_description_te + + + + + buttonBox + accepted() + TerminalStripCreatorDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + TerminalStripCreatorDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp new file mode 100644 index 000000000..d7c21fbc7 --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -0,0 +1,383 @@ +/* + 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 "terminalstripeditor.h" +#include "ui_terminalstripeditor.h" +#include "terminalstripcreatordialog.h" +#include "../../qetproject.h" +#include "../terminalstrip.h" +#include "../elementprovider.h" +#include "../qetgraphicsitem/terminalelement.h" +#include "../UndoCommand/addterminalstripcommand.h" +#include "../UndoCommand/addterminaltostripcommand.h" +#include "../UndoCommand/changeterminalstripdata.h" +#include "terminalstriptreewidget.h" +#include "../../qeticons.h" + +#include + +/** + * @brief TerminalStripEditor::TerminalStripEditor + * @param project : Project to manage the terminal strip + * @param parent : paent widget + */ +TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : + QDialog(parent), + ui(new Ui::TerminalStripEditor), + m_project(project) +{ + ui->setupUi(this); + ui->m_remove_terminal_strip_pb->setDisabled(true); + buildTree(); + ui->m_terminal_strip_tw->expandRecursively(ui->m_terminal_strip_tw->rootIndex()); + setUpUndoConnections(); +} + +/** + * @brief TerminalStripEditor::~TerminalStripEditor + */ +TerminalStripEditor::~TerminalStripEditor() { + delete ui; +} + +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); + }); + + 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 (!terminal_ || !strip_) { + return; + } + + auto undo = new RemoveTerminalFromStripCommand(terminal_, strip_); + m_project->undoStack()->push(undo); + }); +} + +/** + * @brief TerminalStripEditor::buildTree + * Build the tree widget use to explore terminal strip + */ +void TerminalStripEditor::buildTree() +{ + ui->m_terminal_strip_tw->clear(); + + auto title = m_project->title(); + if (title.isEmpty()) { + title = tr("Projet sans titre"); + } + + QStringList strl{title}; + new QTreeWidgetItem(ui->m_terminal_strip_tw, strl, TerminalStripTreeWidget::Root); + + QStringList ftstrl(tr("Bornes indépendante")); + new QTreeWidgetItem(ui->m_terminal_strip_tw, ftstrl, TerminalStripTreeWidget::FreeTerminal); + + auto ts_vector = m_project->terminalStrip(); + std::sort(ts_vector.begin(), ts_vector.end(), [](TerminalStrip *a, TerminalStrip *b) { + return a->name() < b->name();}); + + for (const auto ts : qAsConst(ts_vector)) { + addTerminalStrip(ts); + } + addFreeTerminal(); +} + +/** + * @brief TerminalStripEditor::addTerminalStrip + * Add a new terminal strip to the list of displayed terminal strip + * in the tree widget + * @param terminal_strip + * @return the QTreeWidgetItem who represent the terminal strip + * both if created or already exist + */ +QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_strip) +{ + if (auto item = m_item_strip_H.key(terminal_strip)) { + return item; + } + + auto root_item = ui->m_terminal_strip_tw->topLevelItem(0); + + //Check if installation already exist + //if not create a new one + auto installation_str = terminal_strip->installation(); + QTreeWidgetItem *inst_qtwi = nullptr; + for (int i = 0 ; ichildCount() ; ++i) { + auto child_inst = root_item->child(i); + if (child_inst->data(0, Qt::DisplayRole).toString() == installation_str) { + inst_qtwi = child_inst; + break; + } + } + if (!inst_qtwi) { + QStringList inst_strl{installation_str}; + inst_qtwi = new QTreeWidgetItem(root_item, inst_strl, TerminalStripTreeWidget::Installation); + } + + //Check if location already exist + //if not create a new one + auto location_str = terminal_strip->location(); + QTreeWidgetItem *loc_qtwi = nullptr; + for (int i = 0 ; ichildCount() ; ++i) { + auto child_loc = inst_qtwi->child(i); + if (child_loc->data(0, Qt::DisplayRole).toString() == location_str) { + loc_qtwi = child_loc; + break; + } + } + if (!loc_qtwi) { + QStringList loc_strl{location_str}; + loc_qtwi = new QTreeWidgetItem(inst_qtwi, loc_strl, TerminalStripTreeWidget::Location); + } + + //Add the terminal strip + QStringList name{terminal_strip->name()}; + auto strip_item = new QTreeWidgetItem(loc_qtwi, name, TerminalStripTreeWidget::Strip); + strip_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, terminal_strip->uuid()); + strip_item->setIcon(0, QET::Icons::TerminalStrip); + + //Add child terminal of the strip + for (auto i=0 ; iphysicalTerminalCount() ; ++i) + { + auto index = terminal_strip->index(i); + auto term_item = new QTreeWidgetItem(strip_item, QStringList(index.label()), TerminalStripTreeWidget::Terminal); + term_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, index.uuid().toString()); + term_item->setIcon(0, QET::Icons::ElementTerminal); + + if (index.isElement()) { + m_uuid_terminal_H.insert(index.uuid(), index.element()); + } + } + + m_item_strip_H.insert(strip_item, terminal_strip); + m_uuid_strip_H.insert(terminal_strip->uuid(), terminal_strip); + return strip_item; +} + +/** + * @brief TerminalStripEditor::addFreeTerminal + * Add free terminal (aka terminal which not belong to a terminal strip) + * in the tree widget + */ +void TerminalStripEditor::addFreeTerminal() +{ + ElementProvider ep(m_project); + auto vector_ = ep.freeTerminal(); + + if (vector_.isEmpty()) { + return; + } + + //Sort the terminal element by label + std::sort(vector_.begin(), vector_.end(), [](TerminalElement *a, TerminalElement *b) { + return a->actualLabel() < b->actualLabel(); + }); + + auto free_terminal_item = ui->m_terminal_strip_tw->topLevelItem(1); + + 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, uuid_.toString()); + item->setIcon(0, QET::Icons::ElementTerminal); + + m_uuid_terminal_H.insert(uuid_, terminal); + } +} + +/** + * @brief TerminalStripEditor::clearDataTab + */ +void TerminalStripEditor::clearDataTab() +{ + ui->m_installation_le ->clear(); + ui->m_location_le ->clear(); + ui->m_name_le ->clear(); + ui->m_comment_le ->clear(); + ui->m_description_te ->clear(); + m_current_strip = nullptr; +} + +/** + * @brief TerminalStripEditor::on_m_add_terminal_strip_pb_clicked + * Action when user click on add terminal strip button + */ +void TerminalStripEditor::on_m_add_terminal_strip_pb_clicked() +{ + QScopedPointer dialog(new TerminalStripCreatorDialog(m_project, this)); + + if (auto item = ui->m_terminal_strip_tw->currentItem()) + { + if (item->type() == TerminalStripTreeWidget::Strip) { + item = item->parent(); + } + if (item->type() == TerminalStripTreeWidget::Location) { + dialog->setLocation(item->data(0, Qt::DisplayRole).toString()); + item = item->parent(); + } + if (item->type() == TerminalStripTreeWidget::Installation) { + dialog->setInstallation(item->data(0, Qt::DisplayRole).toString()); + } + } + + if (dialog->exec() == QDialog::Accepted) + { + auto ts = dialog->generatedTerminalStrip(); + m_project->undoStack()->push(new AddTerminalStripCommand(ts, m_project)); + + auto item = addTerminalStrip(ts); + ui->m_terminal_strip_tw->setCurrentItem(item); + } +} + +/** + * @brief TerminalStripEditor::on_m_remove_terminal_strip_pb_clicked + * Action when user click on remove terminal strip button + */ +void TerminalStripEditor::on_m_remove_terminal_strip_pb_clicked() +{ + auto item = ui->m_terminal_strip_tw->currentItem(); + if (auto strip = m_item_strip_H.value(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)); + } + + on_m_reload_pb_clicked(); +} + +/** + * @brief TerminalStripEditor::on_m_reload_pb_clicked + */ +void TerminalStripEditor::on_m_reload_pb_clicked() +{ + auto current_ = m_current_strip; + + ui->m_terminal_strip_tw->clear(); + m_item_strip_H.clear(); + m_uuid_terminal_H.clear(); + m_uuid_strip_H.clear(); + + qDeleteAll(m_item_strip_H.keys()); + + buildTree(); + ui->m_terminal_strip_tw->expandRecursively(ui->m_terminal_strip_tw->rootIndex()); + + //Reselect the tree widget item of the current edited strip + auto item = m_item_strip_H.key(current_); + if (item) { + ui->m_terminal_strip_tw->setCurrentItem(item); + } +} + +/** + * @brief TerminalStripEditor::on_m_terminal_strip_tw_currentItemChanged + * @param current + * @param previous + */ +void TerminalStripEditor::on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) +{ + Q_UNUSED(previous) + + if (!current) { + clearDataTab(); + ui->m_remove_terminal_strip_pb->setDisabled(true); + return; + } + + TerminalStrip *strip_ = nullptr; + if (current->type() == TerminalStripTreeWidget::Strip) { + strip_ = m_item_strip_H.value(current); + ui->m_remove_terminal_strip_pb->setEnabled(true); + } + else if (current->type() == TerminalStripTreeWidget::Terminal + && current->parent() + && current->parent()->type() == TerminalStripTreeWidget::Strip) { + strip_ = m_item_strip_H.value(current->parent()); + ui->m_remove_terminal_strip_pb->setDisabled(true); + } else { + ui->m_remove_terminal_strip_pb->setDisabled(true); + } + + 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()); + ui->m_comment_le ->setText(strip_->comment()); + ui->m_description_te ->setPlainText(strip_->description()); + } else { + 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 new file mode 100644 index 000000000..efdf91d7a --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -0,0 +1,70 @@ +/* + 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 TERMINALSTRIPEDITOR_H +#define TERMINALSTRIPEDITOR_H + +#include + +namespace Ui { + class TerminalStripEditor; +} + +class QETProject; +class TerminalStrip; +class QTreeWidgetItem; +class TerminalElement; +class QAbstractButton; + +/** + * @brief The TerminalStripEditor class + * Main dialog used to edit terminal strip + * of a project + */ +class TerminalStripEditor : public QDialog +{ + Q_OBJECT + + public: + explicit TerminalStripEditor(QETProject *project, QWidget *parent = nullptr); + ~TerminalStripEditor() override; + + private: + void setUpUndoConnections(); + void buildTree(); + QTreeWidgetItem* addTerminalStrip(TerminalStrip *terminal_strip); + void addFreeTerminal(); + void clearDataTab(); + + private slots: + void on_m_add_terminal_strip_pb_clicked(); + 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; + QETProject *m_project = nullptr; + + 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 new file mode 100644 index 000000000..7db32253b --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -0,0 +1,271 @@ + + + TerminalStripEditor + + + + 0 + 0 + 951 + 491 + + + + Gestionnaire de borniers + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Ajouter un bornier + + + + :/ico/16x16/list-add.png:/ico/16x16/list-add.png + + + + + + + Supprimer le bornier + + + + :/ico/16x16/list-remove.png:/ico/16x16/list-remove.png + + + + + + + + + + + :/ico/16x16/view-refresh.png:/ico/16x16/view-refresh.png + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + Qt::Horizontal + + + 4 + + + false + + + + + 0 + 0 + + + + false + + + QAbstractItemView::InternalMove + + + 500 + + + true + + + + Explorateur de bornier + + + + + + + 1 + 0 + + + + 0 + + + + Disposition + + + + + + + 0 + 0 + + + + + + + + + Propriétés + + + + + + Description + + + + + + + Installation : + + + + + + + Commentaire + + + + + + + + + + Qt::Horizontal + + + + + + + + + + Nom : + + + + + + + + + + + + + Localisation : + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + QDialogButtonBox::Apply + + + + + + + + + + + + + + + + TerminalStripTreeWidget + QTreeWidget +
terminalstriptreewidget.h
+
+
+ + m_tab_widget + m_installation_le + m_location_le + m_name_le + m_comment_le + m_description_te + tableView + + + + + +
diff --git a/sources/TerminalStrip/ui/terminalstriptreewidget.cpp b/sources/TerminalStrip/ui/terminalstriptreewidget.cpp new file mode 100644 index 000000000..666436078 --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstriptreewidget.cpp @@ -0,0 +1,139 @@ +/* + 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 "terminalstriptreewidget.h" +#include "../../qeticons.h" + +#include +#include +#include +#include + +int TerminalStripTreeWidget::UUID_USER_ROLE = Qt::UserRole + 1; + +TerminalStripTreeWidget::TerminalStripTreeWidget(QWidget *parent) : + QTreeWidget(parent) +{} + +QStringList TerminalStripTreeWidget::mimeTypes() const +{ + QStringList strl(QStringLiteral("application/x-qet-terminal-strip-tree-terminal-uuid")); + + return strl; +} + +void TerminalStripTreeWidget::startDrag(Qt::DropActions supportedActions) +{ + Q_UNUSED(supportedActions) + + auto item = currentItem(); + + if (!item || + item->type() != TerminalStripTreeWidget::Terminal) { + return; + } + + QDrag drag(this); + auto mime_data = new QMimeData(); + mime_data->setData("application/x-qet-terminal-strip-tree-terminal-uuid", item->data(0, UUID_USER_ROLE).toString().toLatin1()); + + drag.setMimeData(mime_data); + drag.setPixmap(QET::Icons::ElementTerminal.pixmap(16,16)); + drag.exec(Qt::MoveAction); +} + +void TerminalStripTreeWidget::dragMoveEvent(QDragMoveEvent *event) +{ + auto strl = event->mimeData()->formats(); + if (strl.size() != 1 || + strl.first() != "application/x-qet-terminal-strip-tree-terminal-uuid") { + event->ignore(); + return; + } + //Accepted move are : + //free terminal to terminal strip + //terminal strip to another terminal strip + //terminal strip to free terminal + //All other other move is ignored + QTreeWidget::dragMoveEvent(event); + + auto overred_item = itemAt(event->pos()); + auto dragged_item = currentItem(); + if (!overred_item || + !dragged_item || + !dragged_item->parent()) { + return; + } + //Ignore the event by default, we confirm it bellow if needed. + event->ignore(); + + //Move terminal + if (dragged_item->parent()->type() == FreeTerminal && //From free to strip + overred_item->type() == Strip) { + event->accept(); + } + else if (dragged_item->parent()->type() == Strip) //From strip to ... + { + if (overred_item->type() == FreeTerminal) { //Free terminal + event->accept(); + } else if (overred_item->type() == Strip && //Another strip + dragged_item->parent() != overred_item) { + event->accept(); + } + } +} + +void TerminalStripTreeWidget::dropEvent(QDropEvent *event) +{ + auto overred_item = itemAt(event->pos()); + auto dragged_item = currentItem(); + if (!overred_item || + !dragged_item || + !dragged_item->parent()) { + return; + } + + auto old_parent = dragged_item->parent(); + old_parent->removeChild(dragged_item); + overred_item->addChild(dragged_item); + + //Move terminal + 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 (old_parent->type() == Strip) //From strip to ... + { + if (overred_item->type() == FreeTerminal) //Free terminal + { + emit terminalRemovedFromStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()), + QUuid::fromString(old_parent->data(0, UUID_USER_ROLE).toString())); + } + else if (overred_item->type() == Strip) //To another strip + { + emit terminalMovedFromStripToStrip(QUuid::fromString(dragged_item->data(0, UUID_USER_ROLE).toString()), + QUuid::fromString(old_parent->data(0, UUID_USER_ROLE).toString()), + QUuid::fromString(overred_item->data(0, UUID_USER_ROLE).toString())); + } + } + +} + +Qt::DropActions TerminalStripTreeWidget::supportedDropActions() const { + return Qt::MoveAction; +} diff --git a/sources/TerminalStrip/ui/terminalstriptreewidget.h b/sources/TerminalStrip/ui/terminalstriptreewidget.h new file mode 100644 index 000000000..fc1218bc7 --- /dev/null +++ b/sources/TerminalStrip/ui/terminalstriptreewidget.h @@ -0,0 +1,73 @@ +/* + 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 TERMINALSTRIPTREEWIDGET_H +#define TERMINALSTRIPTREEWIDGET_H + +#include +#include + +/** + * @brief The TerminalStripTreeWidget class + * Derived class use to implement custom drag and drop + */ +class TerminalStripTreeWidget : public QTreeWidget +{ + Q_OBJECT + public : + enum TreeWidgetType{ + Root, + Terminal, + FreeTerminal, + Installation, + Location, + Strip + }; + + //Role used for data in QTreeWidgetItem + static int UUID_USER_ROLE; + + signals: + /** + * @brief terminalAddedToStrip + * Signal emited when a terminal is moved from free terminal to a terminals trip + */ + void terminalAddedToStrip(QUuid terminal_uuid, QUuid strip_uuid); + /** + * @brief terminalMovedFromStripToStrip + * Signam emitted when a terminal is moved from from a terminal stip to another one + */ + void terminalMovedFromStripToStrip(QUuid terminal_uuid, QUuid old_strip_uuid, QUuid new_strip_uuid); + /** + * @brief terminalRemovedFromStrip + * Signal emitted when a terminal is moved from a terminal strip to free terminal + */ + void terminalRemovedFromStrip(QUuid terminal_uuid, QUuid old_strip_uuid); + + + public: + TerminalStripTreeWidget(QWidget *parent = nullptr); + + protected: + QStringList mimeTypes() const override; + void startDrag(Qt::DropActions supportedActions) override; + void dragMoveEvent(QDragMoveEvent *event) override; + void dropEvent(QDropEvent *event) override; + Qt::DropActions supportedDropActions() const override; +}; + +#endif // TERMINALSTRIPTREEWIDGET_H diff --git a/sources/elementprovider.cpp b/sources/elementprovider.cpp index 83c98dab0..d530f32ae 100644 --- a/sources/elementprovider.cpp +++ b/sources/elementprovider.cpp @@ -20,6 +20,7 @@ #include "diagram.h" #include "qetgraphicsitem/ViewItem/qetgraphicstableitem.h" #include "qetgraphicsitem/element.h" +#include "qetgraphicsitem/terminalelement.h" #include "qetproject.h" #include @@ -181,3 +182,28 @@ QetGraphicsTableItem *ElementProvider::tableFromUuid(const QUuid &uuid) return nullptr; } + +/** + * @brief ElementProvider::freeTerminal + * @return a vector of every terminals element who doesn't + * belong to a terminal strip. + */ +QVector ElementProvider::freeTerminal() const +{ + QVector vector_; + + for (const auto diagram : m_diagram_list) { + DiagramContent dc(diagram, false); + for (const auto element : qAsConst(dc.m_elements)) { + if (element->elementData().m_type == ElementData::Terminale) + { + auto te = static_cast(element); + if (!te->parentTerminalStrip()) { + vector_.append(static_cast(element)); + } + } + } + } + + return vector_; +} diff --git a/sources/elementprovider.h b/sources/elementprovider.h index 15954dd45..9fbbabd4a 100644 --- a/sources/elementprovider.h +++ b/sources/elementprovider.h @@ -26,6 +26,7 @@ class QETProject; class Diagram; class Element; class QetGraphicsTableItem; +class TerminalElement; /** this class can search in the given diagram or project some kind of element @@ -43,6 +44,7 @@ class ElementProvider QList find(const int filter) const; QVector table(QetGraphicsTableItem *table = nullptr, QAbstractItemModel *model = nullptr); QetGraphicsTableItem *tableFromUuid(const QUuid &uuid); + QVector freeTerminal() const; private: QList m_diagram_list; diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index 69e392ea8..baa6f69ab 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -42,6 +42,7 @@ #include "undocommand/rotateselectioncommand.h" #include "undocommand/rotatetextscommand.h" #include "diagram.h" +#include "TerminalStrip/ui/terminalstripeditor.h" #ifdef BUILD_WITHOUT_KF5 #else @@ -436,6 +437,16 @@ void QETDiagramEditor::setUpActions() } }); + m_terminal_strip_dialog = new QAction(QET::Icons::TerminalStrip, tr("Gestionnaire de borniers (DEV)"), this); + connect(m_terminal_strip_dialog, &QAction::triggered, [this]() + { + if (auto project = this->currentProject()) + { + auto str = new TerminalStripEditor(project, this); + str->show(); + } + }); + //Lauch the plugin of terminal generator m_project_terminalBloc = new QAction(QET::Icons::TerminalStrip, tr("Lancer le plugin de création de borniers"), this); connect(m_project_terminalBloc, &QAction::triggered, this, &QETDiagramEditor::generateTerminalBlock); @@ -809,6 +820,7 @@ void QETDiagramEditor::setUpMenu() menu_project -> addAction(m_add_nomenclature); menu_project -> addAction(m_csv_export); menu_project -> addAction(m_project_export_conductor_num); + menu_project -> addAction(m_terminal_strip_dialog); menu_project -> addAction(m_project_terminalBloc); #ifdef QET_EXPORT_PROJECT_DB menu_project -> addSeparator(); @@ -1502,16 +1514,7 @@ void QETDiagramEditor::slot_updateActions() m_close_file-> setEnabled(opened_project); m_save_file-> setEnabled(opened_project); m_save_file_as-> setEnabled(opened_project); - m_project_edit_properties-> setEnabled(opened_project); - m_project_export_conductor_num->setEnabled(opened_project); - //prj_terminalBloc -> setEnabled(opened_project); m_rotate_texts-> setEnabled(editable_project); - m_project_add_diagram-> setEnabled(editable_project); - m_remove_diagram_from_project-> setEnabled(editable_project); - m_clean_project-> setEnabled(editable_project); - m_add_nomenclature-> setEnabled(editable_project); - m_add_summary-> setEnabled(editable_project); - m_csv_export-> setEnabled(editable_project); m_export_to_images-> setEnabled(opened_diagram); m_print-> setEnabled(opened_diagram); m_export_to_pdf-> setEnabled(opened_diagram); @@ -1522,6 +1525,17 @@ void QETDiagramEditor::slot_updateActions() m_row_column_actions_group. setEnabled(editable_project); m_grey_background-> setEnabled(opened_diagram); + //Project menu + m_project_edit_properties -> setEnabled(opened_project); + m_project_add_diagram -> setEnabled(editable_project); + m_remove_diagram_from_project -> setEnabled(editable_project); + m_clean_project -> setEnabled(editable_project); + m_add_summary -> setEnabled(editable_project); + m_add_nomenclature -> setEnabled(editable_project); + m_csv_export -> setEnabled(editable_project); + m_project_export_conductor_num-> setEnabled(opened_project); + m_terminal_strip_dialog -> setEnabled(editable_project); + slot_updateUndoStack(); slot_updateModeActions(); diff --git a/sources/qetdiagrameditor.h b/sources/qetdiagrameditor.h index db17ea877..34d9da9f1 100644 --- a/sources/qetdiagrameditor.h +++ b/sources/qetdiagrameditor.h @@ -192,6 +192,7 @@ class QETDiagramEditor : public QETMainWindow *m_csv_export, ///< generate nomenclature *m_add_nomenclature, ///< Add nomenclature graphics item; *m_add_summary, /// m_parent_terminal_strip; }; #endif // TERMINALELEMENT_H diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 748b7bfbc..ee187509b 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -31,6 +31,8 @@ #include "titleblocktemplate.h" #include "ui/dialogwaiting.h" #include "ui/importelementdialog.h" +#include "TerminalStrip/terminalstrip.h" +#include "qetxml.h" #include #include @@ -912,6 +914,16 @@ QDomDocument QETProject::toXml() appended_diagram.toElement().setAttribute("order", order_num ++); } + //Write terminal strip to xml + if (m_terminal_strip_vector.count()) + { + auto xml_strip = xml_doc.createElement(QStringLiteral("terminal_strips")); + for (auto &strip : m_terminal_strip_vector) { + xml_strip.appendChild(strip->toXml(xml_doc)); + } + project_root.appendChild(xml_strip); + } + // Write the elements collection. project_root.appendChild(m_elements_collection->root().cloneNode(true)); @@ -1341,16 +1353,26 @@ void QETProject::readProjectXml(QDomDocument &xml_project) } m_data_base.blockSignals(true); + //Load the project-wide properties readProjectPropertiesXml(xml_project); + //Load the default properties for the new diagrams readDefaultPropertiesXml(xml_project); + //load the embedded titleblock templates m_titleblocks_collection.fromXml(xml_project.documentElement()); + //Load the embedded elements collection readElementsCollectionXml(xml_project); + //Load the diagrams readDiagramsXml(xml_project); + + //Load the terminal strip + readTerminalStripXml(xml_project); + + m_data_base.blockSignals(false); m_data_base.updateDB(); @@ -1568,6 +1590,26 @@ void QETProject::readDefaultPropertiesXml(QDomDocument &xml_project) } } +/** + * @brief QETProject::readTerminalStripXml + * Read the terminal strips of this project + * @param xml_project + */ +void QETProject::readTerminalStripXml(const QDomDocument &xml_project) +{ + auto xml_elmt = xml_project.documentElement(); + auto xml_strips = xml_elmt.firstChildElement(QStringLiteral("terminal_strips")); + if (!xml_strips.isNull()) + { + for (auto xml_strip : QETXML::findInDomElement(xml_strips, TerminalStrip::xmlTagName())) + { + auto terminal_strip = new TerminalStrip(this); + terminal_strip->fromXml(xml_strip); + addTerminalStrip(terminal_strip); + } + } +} + /** Export project properties under the \a xml_element XML element. */ @@ -1816,6 +1858,64 @@ void QETProject::setProjectProperties(const DiagramContext &context) { updateDiagramsFolioData(); } +/** + * @brief QETProject::terminalStrip + * @return a QVector who contain all terminal strip owned by this project + */ +QVector QETProject::terminalStrip() const { + return m_terminal_strip_vector; +} + +/** + * @brief QETProject::newTerminalStrip + * @param installation : installation of the terminal strip + * @param location : location of the terminal strip + * @param name : name of the terminal strip + * @return Create a new terminal strip with this project as parent. + * You can retrieve this terminal strip at any time by calling the function + * QETProject::terminalStrip() + */ +TerminalStrip *QETProject::newTerminalStrip(QString installation, QString location, QString name) +{ + auto ts = new TerminalStrip(installation, + location, + name, + this); + + m_terminal_strip_vector.append(ts); + return ts; +} + +/** + * @brief QETProject::addTerminalStrip + * Add \p strip to the terminal strip list of the project. + * The project of \p strip must this project + * @param strip + * @return true if successfully added + */ +bool QETProject::addTerminalStrip(TerminalStrip *strip) +{ + if (strip->parent() != this) + return false; + + if (m_terminal_strip_vector.contains(strip)) + return true; + + m_terminal_strip_vector.append(strip); + return true; +} + +/** + * @brief QETProject::removeTerminalStrip + * Remove \p strip from the terminal strip list of this project. + * Strip is removed from the list but not deleted. + * @param strip + * @return true if successfully removed. + */ +bool QETProject::removeTerminalStrip(TerminalStrip *strip) { + return m_terminal_strip_vector.removeOne(strip); +} + /** Cette methode sert a reperer un projet vide, c-a-d un projet identique a ce que l'on obtient en faisant Fichier > Nouveau. diff --git a/sources/qetproject.h b/sources/qetproject.h index a4561de4e..09dfa2825 100644 --- a/sources/qetproject.h +++ b/sources/qetproject.h @@ -42,6 +42,8 @@ class NumerotationContext; class QUndoStack; class XmlElementCollection; class QTimer; +class TerminalStrip; + #ifdef BUILD_WITHOUT_KF5 #else class KAutoSaveFile; @@ -176,6 +178,11 @@ class QETProject : public QObject void setProjectProperties(const DiagramContext &); QUndoStack* undoStack() {return m_undo_stack;} + QVector terminalStrip() const; + TerminalStrip * newTerminalStrip(QString installation = QString(), QString location = QString(), QString name = QString()); + bool addTerminalStrip(TerminalStrip *strip); + bool removeTerminalStrip(TerminalStrip *strip); + public slots: Diagram *addNewDiagram(int pos = -1); void removeDiagram(Diagram *); @@ -218,6 +225,7 @@ class QETProject : public QObject void readElementsCollectionXml(QDomDocument &xml_project); void readProjectPropertiesXml(QDomDocument &xml_project); void readDefaultPropertiesXml(QDomDocument &xml_project); + void readTerminalStripXml(const QDomDocument &xml_project); void writeProjectPropertiesXml(QDomElement &); void writeDefaultPropertiesXml(QDomElement &); @@ -281,6 +289,7 @@ class QETProject : public QObject #endif QUuid m_uuid = QUuid::createUuid(); projectDataBase m_data_base; + QVector m_terminal_strip_vector; }; Q_DECLARE_METATYPE(QETProject *)