From ce8bd7fae33cd1a35c03f0d32166924b5149f1df Mon Sep 17 00:00:00 2001 From: joshua Date: Wed, 1 Dec 2021 21:27:04 +0100 Subject: [PATCH 01/14] Add terminal bridge feature --- .../UndoCommand/bridgeterminalscommand.cpp | 77 +++++ .../UndoCommand/bridgeterminalscommand.h | 69 +++++ sources/TerminalStrip/terminalstrip.cpp | 278 +++++++++++++++++- sources/TerminalStrip/terminalstrip.h | 42 ++- .../TerminalStrip/ui/terminalstripeditor.cpp | 203 +++++++++++-- .../TerminalStrip/ui/terminalstripeditor.h | 8 +- .../TerminalStrip/ui/terminalstripeditor.ui | 170 ++++++----- .../TerminalStrip/ui/terminalstripmodel.cpp | 122 +++++++- sources/TerminalStrip/ui/terminalstripmodel.h | 30 +- 9 files changed, 884 insertions(+), 115 deletions(-) create mode 100644 sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp create mode 100644 sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp new file mode 100644 index 000000000..663683a7e --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp @@ -0,0 +1,77 @@ +/* + 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 "bridgeterminalscommand.h" +#include "../terminalstrip.h" + +BridgeTerminalsCommand::BridgeTerminalsCommand(TerminalStrip *strip, + QVector real_terminal_uuid, + QUndoCommand *parent): + QUndoCommand(parent), + m_strip(strip), + m_uuid_vector(real_terminal_uuid) +{ + setText(QObject::tr("Ponter des bornes entre-elles")); +} + +void BridgeTerminalsCommand::undo() +{ + if (m_strip) { + m_strip->unBridge(m_uuid_vector); + } +} + +void BridgeTerminalsCommand::redo() +{ + if (m_strip) { + m_strip->setBridge(m_uuid_vector); + } +} + +UnBridgeTerminalsCommand::UnBridgeTerminalsCommand(TerminalStrip *strip, + QVector real_terminal_uuid, + QUndoCommand *parent): + QUndoCommand(parent), + m_strip(strip) +{ + setText(QObject::tr("Supprimer des ponts de bornes")); + + for (const auto &t_uuid : real_terminal_uuid) + { + auto bridge = m_strip->bridgeFor(t_uuid); + if (bridge) { + m_bridge_terminal_map.insert(bridge->uuid_, t_uuid); + } + } +} + +void UnBridgeTerminalsCommand::undo() +{ + if (m_strip) { + for (const auto &bridge_uuid : m_bridge_terminal_map.uniqueKeys()) { + auto terminal_list = m_bridge_terminal_map.values(bridge_uuid); + m_strip->setBridge(bridge_uuid, terminal_list.toVector()); + } + } +} + +void UnBridgeTerminalsCommand::redo() +{ + if (m_strip) { + m_strip->unBridge(m_bridge_terminal_map.values().toVector()); + } +} diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h new file mode 100644 index 000000000..03a6e8b17 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h @@ -0,0 +1,69 @@ +/* + 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 BRIDGETERMINALSCOMMAND_H +#define BRIDGETERMINALSCOMMAND_H + +#include +#include +#include +#include +#include + +class TerminalStrip; + +/** + * @brief The BridgeTerminalsCommand class + * UndoCommand use to create bridge betwen terminals + * of a terminals strip + */ +class BridgeTerminalsCommand : public QUndoCommand +{ + public: + BridgeTerminalsCommand(TerminalStrip *strip, QVector real_terminal_uuid, QUndoCommand *parent = nullptr); + ~BridgeTerminalsCommand() override {} + + void undo() override; + void redo() override; + + private: + QPointer m_strip; + QVector m_uuid_vector; +}; + + +/** + * @brief The UnBridgeTerminalsCommand class + * UndoCommand use to remove bridge betwen terminals + * of a terminals strip + */ +class UnBridgeTerminalsCommand : public QUndoCommand +{ + public: + UnBridgeTerminalsCommand(TerminalStrip *strip, QVector real_terminal_uuid, QUndoCommand *parent = nullptr); + ~UnBridgeTerminalsCommand() override{} + + void undo() override; + void redo() override; + + private: + QPointer m_strip; + QMultiMap m_bridge_terminal_map; ///Key is bridge uuid, value is real terminal uuid + +}; + +#endif // BRIDGETERMINALSCOMMAND_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 02fac72e9..657f0c013 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -39,6 +39,7 @@ using shared_physical_terminal = QSharedPointer; * 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). + * @sa RealTerminalData */ class RealTerminal { @@ -79,7 +80,7 @@ class RealTerminal if (!m_element.isNull()) { return m_element->actualLabel(); } else { - return QStringLiteral(""); + return QLatin1String(); } } @@ -198,11 +199,11 @@ class RealTerminal /** * @brief The PhysicalTerminal class * Represent a physical terminal. - * A physical terminal is composed a least by one real terminal. + * A physical terminal is composed a least by one RealTerminal. * 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. + * The index of real terminals returned by the function terminals() + * is the same as the real level of the real terminal, the index are from back to front. * * Example for a 3 levels terminal. * index 0 = back (mounting plate) @@ -224,6 +225,8 @@ class RealTerminal * t |_| * e * + * @sa PhysicalTerminalData + * */ class PhysicalTerminal { @@ -243,9 +246,9 @@ class PhysicalTerminal /** * @brief setTerminals - * Set the real terminal of this physical terminal - * the order of the terminal in \p terminals represent - * the level index. + * Set the RealTerminal who compose this physical terminal. + * The position of the RealTerminal in @a terminals + * represent the level of these in this physical terminal. * @param terminals */ void setTerminals(QVector terminals) { @@ -254,7 +257,7 @@ class PhysicalTerminal /** * @brief addTerminals - * Append the real terminal \p terminal + * Append the real terminal @a terminal * to this physical terminal. * @param terminal */ @@ -264,7 +267,7 @@ class PhysicalTerminal /** * @brief removeTerminal - * Remove \p terminal from the list of real terminal + * Remove @a terminal from the list of real terminal * @param terminal * @return true if sucessfully removed */ @@ -314,7 +317,7 @@ class PhysicalTerminal /** * @brief terminals - * @return A vector of real terminal who compose this physical terminal + * @return A vector of RealTerminal who compose this PhysicalTerminal */ QVector terminals() const { return m_real_terminal; @@ -415,7 +418,7 @@ TerminalStripData TerminalStrip::data() const { /** * @brief TerminalStrip::setData - * The internal data of this strip to data. + * Set the internal data of this strip to @a 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 @@ -726,6 +729,192 @@ bool TerminalStrip::setLevel(const RealTerminalData &real_terminal_data, int lev return false; } +/** + * @brief TerminalStrip::isBridgeable + * Check if all realTerminal represented by the uuid of @a real_terminals_uuid are bridgeable together. + * To be bridgeable, each real terminal must belong to this terminal strip + * be at the same level, be consecutive and not belong to the same physicalTerminal + * and at least one terminal must be not bridged + * @param real_terminals_uuid : a vector of RealTerminal uuid + * @sa member real_terminal_uuid of struct RealTerminalData + * @return + */ +bool TerminalStrip::isBridgeable(const QVector &real_terminals_uuid) const +{ + if (real_terminals_uuid.size() < 2) { + return false; + } + + // Check if first terminal belong to this strip + auto first_real_terminal = realTerminalForUuid(real_terminals_uuid.first()); + if (!first_real_terminal) { + return false; + } + // Get the level of the first terminal + int level_ = realTerminalData(first_real_terminal).level_; + // Get the physical terminal and pos + auto first_physical_terminal = physicalTerminal(first_real_terminal); + QVector physical_vector{first_physical_terminal}; + QVector pos_vector{m_physical_terminals.indexOf(first_physical_terminal)}; + + auto bridge_ = isBridged(first_real_terminal); + //bool to know at the end of this function if at least one terminal is not bridged + bool no_bridged = bridge_ ? false : true; + + // Check for each terminals + for (int i=1 ; i &real_terminals_uuid) +{ + if (!isBridgeable(real_terminals_uuid)) { + return false; + } + QVector real_terminals_vector; + + for (const auto & uuid_ : real_terminals_uuid) { + auto real_t = realTerminalForUuid(uuid_); + if (real_t) + real_terminals_vector << real_t; + } + + auto bridge = bridgeFor(real_terminals_vector); + if (bridge.isNull()) { + bridge = QSharedPointer::create(); + m_bridge.append(bridge); + } + + for (const auto &real_t : qAsConst(real_terminals_vector)) + { + if (!bridge->real_terminals.contains(real_t)) + bridge->real_terminals.append(real_t); + } + + emit bridgeChanged(); + return true; +} + +/** + * @brief TerminalStrip::setBridge + * Bridge the RealTerminal with uuid in @a real_terminals_uuid to + * the bridge with uuid @a bridge_uuid. + * @param bridge_uuid + * @param real_terminals_uuid + * @return true if all RealTerminal was successfully bridged + */ +bool TerminalStrip::setBridge(const QUuid &bridge_uuid, const QVector &real_terminals_uuid) +{ + auto bridge_ = bridgeForUuid(bridge_uuid); + if (bridge_) + { + if (!isBridgeable(real_terminals_uuid)) { + return false; + } + + bool b_ = false; + for (const auto & uuid_ : real_terminals_uuid) + { + auto real_t = realTerminalForUuid(uuid_); + if (real_t && + !bridge_->real_terminals.contains(real_t)) + { + bridge_->real_terminals.append(real_t); + b_ = true; + } + } + + if (b_) { + emit bridgeChanged(); + return true; + } + } + + return false; +} + +/** + * @brief TerminalStrip::unBridge + * Unbridge all real terminal represented by they uuid + * @param real_terminals_uuid + */ +void TerminalStrip::unBridge(const QVector &real_terminals_uuid) +{ + for (const auto & uuid_ : real_terminals_uuid) + { + auto real_t = realTerminalForUuid(uuid_); + if (real_t) + { + auto bridge_ = isBridged(real_t); + if (bridge_) + bridge_->real_terminals.removeOne(real_t); + } + } + + emit bridgeChanged(); +} + +/** + * @brief TerminalStrip::bridgeFor + * @param real_terminal_uuid + * @return + */ +QSharedPointer TerminalStrip::bridgeFor(const QUuid &real_terminal_uuid) const +{ + auto rt = realTerminalForUuid(real_terminal_uuid); + return bridgeFor(QVector{rt}); +} + /** * @brief TerminalStrip::terminalElement * @return A vector of all terminal element owned by this strip @@ -849,7 +1038,7 @@ QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer< return pt; } -RealTerminalData TerminalStrip::realTerminalData(QSharedPointer real_terminal) const +RealTerminalData TerminalStrip::realTerminalData(const QSharedPointer real_terminal) const { RealTerminalData rtd; @@ -868,6 +1057,7 @@ RealTerminalData TerminalStrip::realTerminalData(QSharedPointer re rtd.function_ = real_terminal->function(); rtd.led_ = real_terminal->led(); rtd.is_element = real_terminal->isElement(); + rtd.is_bridged = isBridged(real_terminal); return rtd; } @@ -912,3 +1102,67 @@ QSharedPointer TerminalStrip::realTerminalForUuid(const QUuid &uui return return_rt; } + +/** + * @brief TerminalStrip::isBridged + * Check if @a real_terminal is bridged + * @param real_terminal + * @return a pointer of TerminalStripBridge if bridget or a null QSharedPointer. + */ +QSharedPointer TerminalStrip::isBridged(const QSharedPointer real_terminal) const +{ + if (real_terminal) + { + for (const auto &bridge_ : qAsConst(m_bridge)) { + if (bridge_->real_terminals.contains(real_terminal)) + return bridge_; + } + } + return QSharedPointer(); +} + +/** + * @brief TerminalStrip::bridgeFor + * Return the bridge where at least one terminal of @a terminal_vector belong. + * If several terminals are bridged but not to the same bridge return + * a TerminalStripBridge with 0 real_terminals_uuid_vector + * @sa TerminalStripBridge + * @param terminal_vector + * @return + */ +QSharedPointer TerminalStrip::bridgeFor(const QVector > &terminal_vector) const +{ + QSharedPointer return_bridge; + + for (const auto &terminal : terminal_vector) + { + auto bridge_ = isBridged(terminal); + if (!bridge_.isNull()) + { + if (return_bridge.isNull()) { + return_bridge = bridge_; + } + else if (return_bridge != bridge_) { + return QSharedPointer(); + } + } + } + + return return_bridge; +} + +/** + * @brief TerminalStrip::bridgeForUuid + * @param bridge_uuid + * @return the bridge with uuid @a bridge_uuid or null QSharedPointer if not exist + */ +QSharedPointer TerminalStrip::bridgeForUuid(const QUuid &bridge_uuid) +{ + for (const auto &bridge : qAsConst(m_bridge)) { + if (bridge->uuid_ == bridge_uuid) { + return bridge; + } + } + + return QSharedPointer(); +} diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index dd24cd5cb..837a01dc0 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -20,6 +20,8 @@ #include #include +#include + #include "terminalstripdata.h" #include "../properties/elementdata.h" @@ -31,7 +33,11 @@ class TerminalStripIndex; class TerminalElement; - +/** + * @brief The RealTerminalData struct + * Conveniant struct to quickly get some values + * of a RealTerminal + */ struct RealTerminalData { int level_ = -1; @@ -43,17 +49,24 @@ struct RealTerminalData conductor_; QUuid element_uuid, - real_terminal_uuid; + real_terminal_uuid, + bridge_uuid; ElementData::TerminalType type_; ElementData::TerminalFunction function_; bool led_ = false, - is_element = false; + is_element = false, + is_bridged = false; QPointer element_; }; +/** + * @brief The PhysicalTerminalData struct + * Conveniant struct to quickly get some values + * of a PhysicalTerminal + */ struct PhysicalTerminalData { QVector real_terminals_vector; @@ -70,12 +83,19 @@ inline uint qHash(const PhysicalTerminalData &key, uint seed) { return qHash(key.uuid_, seed); } +struct TerminalStripBridge +{ + QVector> real_terminals; + QColor color_ = Qt::gray; + QUuid uuid_ = QUuid::createUuid(); +}; + /** * @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) + * and is composed by one or several PhysicalTerminal. */ class TerminalStrip : public QObject { @@ -86,6 +106,7 @@ class TerminalStrip : public QObject public: signals: void orderChanged(); //Emitted when the order of the physical terminal is changed + void bridgeChanged(); public: TerminalStrip(QETProject *project); @@ -121,6 +142,11 @@ class TerminalStrip : public QObject bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector &added_terminals); void unGroupTerminals(const QVector &terminals_to_ungroup); bool setLevel(const RealTerminalData &real_terminal_data, int level); + bool isBridgeable(const QVector &real_terminals_uuid) const; + bool setBridge(const QVector &real_terminals_uuid); + bool setBridge(const QUuid &bridge_uuid, const QVector &real_terminals_uuid); + void unBridge(const QVector &real_terminals_uuid); + QSharedPointer bridgeFor(const QUuid &real_terminal_uuid) const; QVector> terminalElement() const; @@ -131,9 +157,12 @@ class TerminalStrip : public QObject private: QSharedPointer realTerminal(Element *terminal); QSharedPointer physicalTerminal(QSharedPointer terminal) const; - RealTerminalData realTerminalData(QSharedPointer real_terminal) const; + RealTerminalData realTerminalData(const QSharedPointer real_terminal) const; QSharedPointer physicalTerminalForUuid (const QUuid &uuid) const; QSharedPointer realTerminalForUuid(const QUuid &uuid) const; + QSharedPointer isBridged(const QSharedPointer real_terminal) const; + QSharedPointer bridgeFor (const QVector> &terminal_vector) const; + QSharedPointer bridgeForUuid (const QUuid &bridge_uuid); private: TerminalStripData m_data; @@ -141,6 +170,7 @@ class TerminalStrip : public QObject QVector> m_terminal_elements_vector; QVector> m_real_terminals; QVector> m_physical_terminals; + QVector> m_bridge; }; #endif // TERMINALSTRIP_H diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 31aec27d5..fd9410a58 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -33,6 +33,7 @@ #include "../UndoCommand/sortterminalstripcommand.h" #include "../UndoCommand/groupterminalscommand.h" #include "../UndoCommand/changeterminallevel.h" +#include "../UndoCommand/bridgeterminalscommand.h" #include @@ -49,6 +50,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : ui->setupUi(this); ui->m_table_widget->setItemDelegate(new TerminalStripModelDelegate(ui->m_terminal_strip_tw)); + ui->m_remove_terminal_strip_pb->setDisabled(true); buildTree(); #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) @@ -56,13 +58,18 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : #else ui->m_terminal_strip_tw->expandAll(); #endif + + //Setup the bridge color + QList bridge_color{Qt::red, Qt::blue, Qt::white, Qt::gray, Qt::black}; + ui->m_bridge_color_cb->setColors(bridge_color); + setUpUndoConnections(); //Call for update the state of child widgets selectionChanged(); //Go the diagram of double clicked terminal - connect(ui->m_table_widget, &QAbstractItemView::doubleClicked, [this](const QModelIndex &index) + connect(ui->m_table_widget, &QAbstractItemView::doubleClicked, this, [=](const QModelIndex &index) { Element *elmt = nullptr; if (this->m_model->isXrefCell(index, &elmt)) @@ -75,7 +82,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : { auto fit_view = elmt->sceneBoundingRect(); fit_view.adjust(-200,-200,200,200); - diagram->views().first()->fitInView(fit_view, Qt::KeepAspectRatioByExpanding); + diagram->views().at(0)->fitInView(fit_view, Qt::KeepAspectRatioByExpanding); } } } @@ -91,8 +98,8 @@ TerminalStripEditor::~TerminalStripEditor() { void TerminalStripEditor::setUpUndoConnections() { - connect(ui->m_terminal_strip_tw, &TerminalStripTreeWidget::terminalAddedToStrip, - [this](QUuid terminal_uuid, QUuid strip_uuid) + 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); @@ -105,8 +112,8 @@ void TerminalStripEditor::setUpUndoConnections() 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) + 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); @@ -120,8 +127,8 @@ void TerminalStripEditor::setUpUndoConnections() m_project->undoStack()->push(undo); }); - connect(ui->m_terminal_strip_tw, &TerminalStripTreeWidget::terminalRemovedFromStrip, - [this] (QUuid terminal_uuid, QUuid old_strip_uuid) + 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); @@ -286,6 +293,7 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_) if (m_current_strip) { disconnect(m_current_strip, &TerminalStrip::orderChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked); + disconnect(m_current_strip, &TerminalStrip::bridgeChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked); } if (!strip_) @@ -317,10 +325,12 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_) m_model = new TerminalStripModel(strip_, this); ui->m_table_widget->setModel(m_model); + setUpBridgeCellWidth(); spanMultiLevelTerminals(); selectionChanged(); //Used to update child widgets connect(m_current_strip, &TerminalStrip::orderChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked); + connect(m_current_strip, &TerminalStrip::bridgeChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked); connect(ui->m_table_widget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TerminalStripEditor::selectionChanged); } } @@ -361,6 +371,10 @@ void TerminalStripEditor::selectionChanged() ui->m_type_cb ->setDisabled(true); ui->m_function_cb ->setDisabled(true); ui->m_led_cb ->setDisabled(true); + + ui->m_bridge_terminals_pb ->setDisabled(true); + ui->m_unbridge_terminals_pb->setDisabled(true); + ui->m_bridge_color_cb ->setDisabled(true); return; } @@ -378,13 +392,14 @@ void TerminalStripEditor::selectionChanged() ui->m_led_cb ->setEnabled(true); } - const auto terminal_vector = m_model->physicalTerminalDataForIndex(index_list); + const auto physical_terminal_vector = m_model->physicalTerminalDataForIndex(index_list); + const auto real_terminal_vector = m_model->realTerminalDataForIndex(index_list); //Enable/disable group button - ui->m_group_terminals_pb->setEnabled(terminal_vector.size() > 1 ? true : false); + ui->m_group_terminals_pb->setEnabled(physical_terminal_vector.size() > 1 ? true : false); //Enable/disable ungroup button - auto it_= std::find_if(terminal_vector.constBegin(), terminal_vector.constEnd(), [](const PhysicalTerminalData &data) + auto it_= std::find_if(physical_terminal_vector.constBegin(), physical_terminal_vector.constEnd(), [](const PhysicalTerminalData &data) { if (data.real_terminals_vector.size() >= 2) { return true; @@ -392,11 +407,11 @@ void TerminalStripEditor::selectionChanged() return false; } }); - ui->m_ungroup_pb->setDisabled(it_ == terminal_vector.constEnd()); + ui->m_ungroup_pb->setDisabled(it_ == physical_terminal_vector.constEnd()); //Enable/disable level spinbox bool enable_ = false; - for (const auto &physical : terminal_vector) + for (const auto &physical : physical_terminal_vector) { if (physical.real_terminals_vector.size() > 1) { enable_ = true; @@ -404,6 +419,91 @@ void TerminalStripEditor::selectionChanged() } } ui->m_level_sb->setEnabled(enable_); + + //Enable/disable bridge and unbridge + bool enable_bridge = false; + bool enable_unbridge = false; + + //One column must be selected and the column must be a level column + int level_ = TerminalStripModel::levelForColumn(isSingleColumnSelected()); + if (level_ >= 0) + { + //Select only terminals of corresponding level cell selection + QVector real_terminal_level_vector; + for (const auto &rtd : real_terminal_vector) { + if (rtd.level_ == level_) { + real_terminal_level_vector.append(rtd); + } + } + + QVector uuid_v; + for (const auto &rtd : real_terminal_level_vector) { + uuid_v << rtd.real_terminal_uuid; + } + if (m_current_strip) { + enable_bridge = m_current_strip->isBridgeable(uuid_v); + } + + for (const auto &rtd : real_terminal_level_vector) + { + if (rtd.is_bridged && + rtd.level_ == level_) { + enable_unbridge = true; + break; + } + } + } + ui->m_bridge_terminals_pb->setEnabled(enable_bridge); + ui->m_unbridge_terminals_pb->setEnabled(enable_unbridge); +} + +void TerminalStripEditor::setUpBridgeCellWidth() +{ + if (ui->m_table_widget->verticalHeader() && + m_model) + { + auto section_size = ui->m_table_widget->verticalHeader()->defaultSectionSize(); + auto h_header = ui->m_table_widget->horizontalHeader(); + + h_header->setSectionResizeMode(2, QHeaderView::Fixed); + h_header->resizeSection(2, section_size); + h_header->setSectionResizeMode(3, QHeaderView::Fixed); + h_header->resizeSection(3, section_size); + h_header->setSectionResizeMode(4, QHeaderView::Fixed); + h_header->resizeSection(4, section_size); + h_header->setSectionResizeMode(5, QHeaderView::Fixed); + h_header->resizeSection(5, section_size); + } +} + +/** + * @brief TerminalStripEditor::isSingleColumnSelected + * If all current QModelIndex are in the same column + * return the column type + * @sa TerminalStripModel::Column + * @return + */ +TerminalStripModel::Column TerminalStripEditor::isSingleColumnSelected() const +{ + if (m_current_strip && + ui->m_table_widget->selectionModel()) + { + const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); + if (index_list.isEmpty()) { + return TerminalStripModel::Invalid; + } + + auto column_ = index_list.first().column(); + for (const auto &index : index_list) { + if (index.column() != column_) { + return TerminalStripModel::Invalid; + } + } + + return TerminalStripModel::columnTypeForIndex(index_list.first()); + } + + return TerminalStripModel::Invalid; } /** @@ -617,7 +717,7 @@ void TerminalStripEditor::on_m_level_sb_valueChanged(int arg1) for (auto index : index_list) { - auto level_index = m_model->index(index.row(), 1, index.parent()); + auto level_index = m_model->index(index.row(), TerminalStripModel::Level, index.parent()); if (level_index.isValid()) { m_model->setData(level_index, arg1); @@ -634,7 +734,7 @@ void TerminalStripEditor::on_m_type_cb_activated(int index) for (auto model_index : index_list) { - auto type_index = m_model->index(model_index.row(), 6, model_index.parent()); + auto type_index = m_model->index(model_index.row(), TerminalStripModel::Type, model_index.parent()); if (type_index.isValid()) { ElementData::TerminalType override_type; @@ -667,7 +767,7 @@ void TerminalStripEditor::on_m_function_cb_activated(int index) for (auto model_index : index_list) { - auto function_index = m_model->index(model_index.row(), 7, model_index.parent()); + auto function_index = m_model->index(model_index.row(), TerminalStripModel::Function, model_index.parent()); if (function_index.isValid()) { ElementData::TerminalFunction override_function; @@ -696,7 +796,7 @@ void TerminalStripEditor::on_m_led_cb_activated(int index) for (auto model_index : index_list) { - auto led_index = m_model->index(model_index.row(), 8, model_index.parent()); + auto led_index = m_model->index(model_index.row(), TerminalStripModel::Led, model_index.parent()); if (led_index.isValid()) { m_model->setData(led_index, @@ -706,3 +806,72 @@ void TerminalStripEditor::on_m_led_cb_activated(int index) } } +/** + * @brief TerminalStripEditor::on_m_bridge_terminals_pb_clicked + */ +void TerminalStripEditor::on_m_bridge_terminals_pb_clicked() +{ + if (m_current_strip) + { + int level_ = isSingleColumnSelected(); + if (level_ >= TerminalStripModel::Level0 && + level_ <= TerminalStripModel::Level3) + { + if(level_ == TerminalStripModel::Level0){level_ = 0;} + else if(level_ == TerminalStripModel::Level1){level_ = 1;} + else if(level_ == TerminalStripModel::Level2){level_ = 2;} + else if(level_ == TerminalStripModel::Level3){level_ = 3;} + + const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); + const auto rtd_vector = m_model->realTerminalDataForIndex(index_list); + QVector uuid_vector; + for (const auto &rtd : rtd_vector) + { + if (rtd.level_ == level_) { + uuid_vector.append(rtd.real_terminal_uuid); + } + } + if (m_current_strip->isBridgeable(uuid_vector)) { + m_project->undoStack()->push(new BridgeTerminalsCommand(m_current_strip, uuid_vector)); + } + } + } +} + +/** + * @brief TerminalStripEditor::on_m_unbridge_terminals_pb_clicked + */ +void TerminalStripEditor::on_m_unbridge_terminals_pb_clicked() +{ + if (m_current_strip) + { + int level_ = isSingleColumnSelected(); + if (level_ >= TerminalStripModel::Level0 && + level_ <= TerminalStripModel::Level3) + { + if(level_ == TerminalStripModel::Level0){level_ = 0;} + else if(level_ == TerminalStripModel::Level1){level_ = 1;} + else if(level_ == TerminalStripModel::Level2){level_ = 2;} + else if(level_ == TerminalStripModel::Level3){level_ = 3;} + + const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); + const auto rtd_vector = m_model->realTerminalDataForIndex(index_list); + QVector uuid_vector; + for (const auto &rtd : rtd_vector) + { + if (rtd.level_ == level_ + && rtd.is_bridged) { + uuid_vector.append(rtd.real_terminal_uuid); + } + } + m_project->undoStack()->push(new UnBridgeTerminalsCommand(m_current_strip, uuid_vector)); + } + } +} + + +void TerminalStripEditor::on_m_bridge_color_cb_activated(const QColor &col) +{ + +} + diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index 05f9eaa1e..df07b5acf 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -20,6 +20,8 @@ #include +#include "terminalstripmodel.h" + namespace Ui { class TerminalStripEditor; } @@ -29,7 +31,6 @@ class TerminalStrip; class QTreeWidgetItem; class TerminalElement; class QAbstractButton; -class TerminalStripModel; /** * @brief The TerminalStripEditor class @@ -52,6 +53,8 @@ class TerminalStripEditor : public QDialog void setCurrentStrip(TerminalStrip *strip_); void spanMultiLevelTerminals(); void selectionChanged(); + void setUpBridgeCellWidth(); + TerminalStripModel::Column isSingleColumnSelected() const; private slots: void on_m_add_terminal_strip_pb_clicked(); @@ -66,6 +69,9 @@ class TerminalStripEditor : public QDialog void on_m_type_cb_activated(int index); void on_m_function_cb_activated(int index); void on_m_led_cb_activated(int index); + void on_m_bridge_terminals_pb_clicked(); + void on_m_unbridge_terminals_pb_clicked(); + void on_m_bridge_color_cb_activated(const QColor &col); private: Ui::TerminalStripEditor *ui; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.ui b/sources/TerminalStrip/ui/terminalstripeditor.ui index e31862a37..410adace0 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.ui +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -178,34 +178,14 @@ - - + + - Type : + Étage : - - - - Degrouper les bornes - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - + @@ -234,10 +214,96 @@ - - + + + + LED : + + + + + + + + Qt::Horizontal + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Position automatique + + + + + + + Fonction : + + + + + + + Couleur pont : + + + + + + + Grouper les bornes + + + + + + + Degrouper les bornes + + + + + + + Type : + + + + + + + + Sans + + + + + Avec + + + + + + + + @@ -256,52 +322,17 @@ - - + + - Position automatique + Ponter les bornes - - + + - Étage : - - - - - - - Grouper les bornes - - - - - - - Fonction : - - - - - - - - Sans - - - - - Avec - - - - - - - - LED : + Déponter les bornes @@ -375,6 +406,11 @@ + + KColorCombo + QComboBox +
kcolorcombo.h
+
TerminalStripTreeWidget QTreeWidget diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index fb873db10..5bed80079 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -23,25 +23,77 @@ #include #include #include +#include /** * Some const int who describe what a column contain */ const int POS_CELL = 0; const int LEVEL_CELL = 1; -const int LABEL_CELL = 2; -const int XREF_CELL = 3; -const int CABLE_CELL = 4; -const int CABLE_WIRE_CELL = 5; -const int TYPE_CELL = 6; -const int FUNCTION_CELL = 7; -const int LED_CELL = 8; -const int CONDUCTOR_CELL = 9; +const int LEVEL_0_CELL = 2; +const int LEVEL_1_CELL = 3; +const int LEVEL_2_CELL = 4; +const int LEVEL_3_CELL = 5; +const int LABEL_CELL = 6; +const int XREF_CELL = 7; +const int CABLE_CELL = 8; +const int CABLE_WIRE_CELL = 9; +const int TYPE_CELL = 10; +const int FUNCTION_CELL = 11; +const int LED_CELL = 12; +const int CONDUCTOR_CELL = 13; -const int ROW_COUNT = 9; +const int ROW_COUNT = 13; -static QVector UNMODIFIED_CELL_VECTOR{false, false, false, false, false, false, false, false, false, false}; +static QVector UNMODIFIED_CELL_VECTOR{false, false, false, false, false, false, false, false, false, false, false, false, false, false}; +/** + * @brief TerminalStripModel::levelForColumn + * Return the terminal level for column @a column + * or -1 if column is not a level column + * @param column + * @return + */ +int TerminalStripModel::levelForColumn(Column column) +{ + switch (column) { + case Level0: return 0; + case Level1: return 1; + case Level2: return 2; + case Level3: return 3; + default: return -1; + } +} + +/** + * @brief TerminalStripModel::columnTypeForIndex + * @param index + * @return the thing (pos, level, type, function etc...) for @a index + */ +TerminalStripModel::Column TerminalStripModel::columnTypeForIndex(const QModelIndex &index) +{ + if (index.isValid()) + { + switch (index.column()) { + case 0: return Pos; + case 1: return Level; + case 2 : return Level0; + case 3 : return Level1; + case 4 : return Level2; + case 5 : return Level3; + case 6 : return Label; + case 7 : return XRef; + case 8 : return Cable; + case 9 : return CableWire; + case 10 : return Type; + case 11 : return Function; + case 12 : return Led; + case 13 : return Conductor; + default : return Invalid; + } + } + return Invalid; +} /** * @brief TerminalStripModel::TerminalStripModel @@ -91,6 +143,18 @@ QVariant TerminalStripModel::data(const QModelIndex &index, int role) const switch (index.column()) { case POS_CELL : return physicalDataAtIndex(index.row()).pos_; case LEVEL_CELL : return rtd.level_; + case LEVEL_0_CELL : + if (rtd.level_ == 0 && rtd.is_bridged) return "0"; + break; + case LEVEL_1_CELL : + if (rtd.level_ == 1 && rtd.is_bridged) return "0"; + break; + case LEVEL_2_CELL : + if (rtd.level_ == 2 && rtd.is_bridged) return "0"; + break; + case LEVEL_3_CELL : + if (rtd.level_ == 3 && rtd.is_bridged) return "0"; + break; case LABEL_CELL : return rtd.label_; case XREF_CELL : return rtd.Xref_; case CABLE_CELL : return rtd.cable_; @@ -202,6 +266,10 @@ QVariant TerminalStripModel::headerData(int section, Qt::Orientation orientation switch (section) { case POS_CELL: return tr("Position"); case LEVEL_CELL: return tr("Étage"); + case LEVEL_0_CELL: return QStringLiteral("0"); + case LEVEL_1_CELL: return QStringLiteral("1"); + case LEVEL_2_CELL: return QStringLiteral("2"); + case LEVEL_3_CELL: return QStringLiteral("3"); case LABEL_CELL: return tr("Label"); case XREF_CELL: return tr("Référence croisé"); case CABLE_CELL: return tr("Câble"); @@ -284,6 +352,40 @@ bool TerminalStripModel::isXrefCell(const QModelIndex &index, Element **element) return false; } +/** + * @brief TerminalStripModel::levelCellCount + * Check for each index of @a index_list if the cell represented by the index + * is a level cell (level 0 to level 3) and if the corresponding real terminal is in the same level + * + * The returned vector contain how many index has matched + * the vector have 4 int, + * the first int is the number of matched level 0 + * the second int is the number of matched level 1 + * the third int is the number of matched level 2 + * the fourth int is the number of matched level 4 + * @param index_list + * @return + */ +QVector TerminalStripModel::levelCellCount(const QModelIndexList &index_list) const +{ + QVector vector_(4,0); + + for (const auto &index : index_list) + { + if(index.isValid()) + { + const auto rtd_ = realDataAtIndex(index.row()); + const auto level_ = rtd_.level_; + const auto index_column = index.column(); + if (level_ + 2 == index_column) { + vector_.replace(level_, vector_.at(level_)+1); + } + } + } + + return vector_; +} + /** * @brief TerminalStripModel::terminalsForIndex * @param index_list diff --git a/sources/TerminalStrip/ui/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 25a06a057..4eb1d1f0c 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -30,6 +30,28 @@ class TerminalStrip; class TerminalStripModel : public QAbstractTableModel { + public: + enum Column { + Pos = 0, + Level = 1, + Level0 = 2, + Level1 = 3, + Level2 = 4, + Level3 = 5, + Label = 6, + XRef = 7, + Cable = 8, + CableWire = 9, + Type = 10, + Function = 11, + Led = 12, + Conductor = 13, + Invalid = 99 + }; + + static int levelForColumn(TerminalStripModel::Column column); + static TerminalStripModel::Column columnTypeForIndex(const QModelIndex &index); + Q_OBJECT public: TerminalStripModel(TerminalStrip *terminal_strip, QObject *parent = nullptr); @@ -40,10 +62,10 @@ class TerminalStripModel : public QAbstractTableModel virtual bool setData (const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; virtual Qt::ItemFlags flags (const QModelIndex &index) const override; - QVector> modifiedRealTerminalData() const; bool isXrefCell(const QModelIndex &index, Element **element = nullptr); + QVector levelCellCount(const QModelIndexList &index_list) const; QVector physicalTerminalDataForIndex(QModelIndexList index_list) const; QVector realTerminalDataForIndex(QModelIndexList index_list) const; @@ -58,6 +80,10 @@ class TerminalStripModel : public QAbstractTableModel QPointer m_terminal_strip; QVector m_edited_terminal_data, m_original_terminal_data; QHash> m_modified_cell; + QPixmap m_bridge_top, + m_bride_bottom, + m_bridge, + m_bride_both; }; class TerminalStripModelDelegate : public QStyledItemDelegate From 6e68e6047ab6a8d2a3410d43284fb01c1b8e7cf5 Mon Sep 17 00:00:00 2001 From: joshua Date: Thu, 2 Dec 2021 18:54:45 +0100 Subject: [PATCH 02/14] Remove isXrefCell method... and use instead columnTypeForIndex method --- .../TerminalStrip/ui/terminalstripeditor.cpp | 22 ++++++----- .../TerminalStrip/ui/terminalstripmodel.cpp | 39 +++++++------------ sources/TerminalStrip/ui/terminalstripmodel.h | 4 +- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index fd9410a58..4706dc227 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -71,18 +71,22 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : //Go the diagram of double clicked terminal connect(ui->m_table_widget, &QAbstractItemView::doubleClicked, this, [=](const QModelIndex &index) { - Element *elmt = nullptr; - if (this->m_model->isXrefCell(index, &elmt)) + if (m_model->columnTypeForIndex(index) == TerminalStripModel::XRef) { - auto diagram = elmt->diagram(); - if (diagram) + auto rtd = m_model->realTerminalDataForIndex(index); + if (rtd.element_) { - diagram->showMe(); - if (diagram->views().size()) + auto elmt = rtd.element_; + auto diagram = elmt->diagram(); + if (diagram) { - auto fit_view = elmt->sceneBoundingRect(); - fit_view.adjust(-200,-200,200,200); - diagram->views().at(0)->fitInView(fit_view, Qt::KeepAspectRatioByExpanding); + diagram->showMe(); + if (diagram->views().size()) + { + auto fit_view = elmt->sceneBoundingRect(); + fit_view.adjust(-200,-200,200,200); + diagram->views().at(0)->fitInView(fit_view, Qt::KeepAspectRatioByExpanding); + } } } } diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 5bed80079..c5d48d352 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -327,31 +327,6 @@ QVector> TerminalStripModel::modifiedR return returned_vector; } -/** - * @brief TerminalStripModel::isXrefCell - * @param index - * @param elmt : Pointer of a pointer element - * @return true if the index is the Xref cell, if true the pointer \p element - * will be set to the element associated to the cell. - */ -bool TerminalStripModel::isXrefCell(const QModelIndex &index, Element **element) -{ - if (index.model() == this - && index.isValid() - && index.column() == XREF_CELL) - { - if (index.row() < rowCount()) - { - if (auto data = dataAtRow(index.row()) ; data.element_) { - *element = data.element_.data(); - } - } - return true; - } - - return false; -} - /** * @brief TerminalStripModel::levelCellCount * Check for each index of @a index_list if the cell represented by the index @@ -448,6 +423,20 @@ QVector TerminalStripModel::realTerminalDataForIndex(QModelInd return vector_; } +/** + * @brief TerminalStripModel::realTerminalDataForIndex + * @param index + * @return RealTerminalData at index @a index or null RealTerminalData if invalid + */ +RealTerminalData TerminalStripModel::realTerminalDataForIndex(const QModelIndex &index) const +{ + if (index.isValid()) { + return realDataAtIndex(index.row()); + } else { + return RealTerminalData(); + } +} + void TerminalStripModel::fillPhysicalTerminalData() { //Get all physical terminal diff --git a/sources/TerminalStrip/ui/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 4eb1d1f0c..2b2804909 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -64,10 +64,10 @@ class TerminalStripModel : public QAbstractTableModel virtual Qt::ItemFlags flags (const QModelIndex &index) const override; QVector> modifiedRealTerminalData() const; - bool isXrefCell(const QModelIndex &index, Element **element = nullptr); QVector levelCellCount(const QModelIndexList &index_list) const; QVector physicalTerminalDataForIndex(QModelIndexList index_list) const; QVector realTerminalDataForIndex(QModelIndexList index_list) const; + RealTerminalData realTerminalDataForIndex(const QModelIndex &index) const; private: void fillPhysicalTerminalData(); From 291e163ee2bee991c424d90e5369555cdc56ea04 Mon Sep 17 00:00:00 2001 From: joshua Date: Thu, 2 Dec 2021 18:55:56 +0100 Subject: [PATCH 03/14] REmove unused method : levelCellCount --- .../TerminalStrip/ui/terminalstripmodel.cpp | 34 ------------------- sources/TerminalStrip/ui/terminalstripmodel.h | 1 - 2 files changed, 35 deletions(-) diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index c5d48d352..e0035a487 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -327,40 +327,6 @@ QVector> TerminalStripModel::modifiedR return returned_vector; } -/** - * @brief TerminalStripModel::levelCellCount - * Check for each index of @a index_list if the cell represented by the index - * is a level cell (level 0 to level 3) and if the corresponding real terminal is in the same level - * - * The returned vector contain how many index has matched - * the vector have 4 int, - * the first int is the number of matched level 0 - * the second int is the number of matched level 1 - * the third int is the number of matched level 2 - * the fourth int is the number of matched level 4 - * @param index_list - * @return - */ -QVector TerminalStripModel::levelCellCount(const QModelIndexList &index_list) const -{ - QVector vector_(4,0); - - for (const auto &index : index_list) - { - if(index.isValid()) - { - const auto rtd_ = realDataAtIndex(index.row()); - const auto level_ = rtd_.level_; - const auto index_column = index.column(); - if (level_ + 2 == index_column) { - vector_.replace(level_, vector_.at(level_)+1); - } - } - } - - return vector_; -} - /** * @brief TerminalStripModel::terminalsForIndex * @param index_list diff --git a/sources/TerminalStrip/ui/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 2b2804909..78e356739 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -64,7 +64,6 @@ class TerminalStripModel : public QAbstractTableModel virtual Qt::ItemFlags flags (const QModelIndex &index) const override; QVector> modifiedRealTerminalData() const; - QVector levelCellCount(const QModelIndexList &index_list) const; QVector physicalTerminalDataForIndex(QModelIndexList index_list) const; QVector realTerminalDataForIndex(QModelIndexList index_list) const; RealTerminalData realTerminalDataForIndex(const QModelIndex &index) const; From beee4a06c833b9d9db4813c62cb6b8183a238c92 Mon Sep 17 00:00:00 2001 From: joshua Date: Sat, 11 Dec 2021 21:25:40 +0100 Subject: [PATCH 04/14] Draw bridge pixmap in the tableview (wip) --- sources/TerminalStrip/terminalstrip.cpp | 107 +++++++- sources/TerminalStrip/terminalstrip.h | 14 +- .../TerminalStrip/ui/terminalstripeditor.cpp | 23 +- .../TerminalStrip/ui/terminalstripeditor.h | 2 +- .../TerminalStrip/ui/terminalstripmodel.cpp | 258 +++++++++++++++++- sources/TerminalStrip/ui/terminalstripmodel.h | 30 +- 6 files changed, 400 insertions(+), 34 deletions(-) diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 657f0c013..adce9d1c0 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -618,6 +618,7 @@ bool TerminalStrip::setOrderTo(const QVector &sorted_vecto } m_physical_terminals = new_order; + rebuildRealVector(); emit orderChanged(); return true; } @@ -909,12 +910,94 @@ void TerminalStrip::unBridge(const QVector &real_terminals_uuid) * @param real_terminal_uuid * @return */ -QSharedPointer TerminalStrip::bridgeFor(const QUuid &real_terminal_uuid) const +const QSharedPointer TerminalStrip::bridgeFor(const QUuid &real_terminal_uuid) const { auto rt = realTerminalForUuid(real_terminal_uuid); return bridgeFor(QVector{rt}); } +/** + * @brief TerminalStrip::previousTerminalInLevel + * @param real_terminal_uuid + * @return The previous real terminal at the samne level + * as the real terminal with uuid @a real_terminal_uuid + * If there is not a previous terminal, return a null RealTerminalData + */ +RealTerminalData TerminalStrip::previousTerminalInLevel(const QUuid &real_terminal_uuid) const +{ + auto real_t = realTerminalForUuid(real_terminal_uuid); + if (real_t) + { + auto phy_t = physicalTerminal(real_t); + if (phy_t) + { + auto level_ = phy_t->levelOf(real_t); + auto index = m_physical_terminals.indexOf(phy_t); + if (index >= 1) + { + auto t_vector = m_physical_terminals.at(index-1)->terminals(); + if (t_vector.size() > level_) + { + return realTerminalData(t_vector.at(level_)); + } + } + } + } + + return RealTerminalData(); +} + +/** + * @brief TerminalStrip::nextTerminalInLevel + * @param real_terminal_uuid + * @return The next real terminal at the samne level + * as the real terminal with uuid @a real_terminal_uuid + * If there is not a next terminal, return a null RealTerminalData + */ +RealTerminalData TerminalStrip::nextTerminalInLevel(const QUuid &real_terminal_uuid) const +{ + auto real_t = realTerminalForUuid(real_terminal_uuid); + if (real_t) + { + auto phy_t = physicalTerminal(real_t); + if (phy_t) + { + auto level_ = phy_t->levelOf(real_t); + auto index = m_physical_terminals.indexOf(phy_t); + if (index < m_physical_terminals.size()-1) + { + auto t_vector = m_physical_terminals.at(index+1)->terminals(); + if (t_vector.size() > level_) + { + return realTerminalData(t_vector.at(level_)); + } + } + } + } + + return RealTerminalData(); +} + +RealTerminalData TerminalStrip::previousRealTerminal(const QUuid &real_terminal_uuid) const +{ + auto real = realTerminalForUuid(real_terminal_uuid); + auto index = m_real_terminals.indexOf(real); + if (index) { + return realTerminalData(m_real_terminals.at(index-1)); + } + return RealTerminalData(); +} + +RealTerminalData TerminalStrip::nextRealTerminal(const QUuid &real_terminal_uuid) const +{ + auto real = realTerminalForUuid(real_terminal_uuid); + auto index = m_real_terminals.indexOf(real); + if (index != m_real_terminals.size()-1) { + return realTerminalData(m_real_terminals.at(index+1)); + } + return RealTerminalData(); +} + /** * @brief TerminalStrip::terminalElement * @return A vector of all terminal element owned by this strip @@ -1058,6 +1141,14 @@ RealTerminalData TerminalStrip::realTerminalData(const QSharedPointerled(); rtd.is_element = real_terminal->isElement(); rtd.is_bridged = isBridged(real_terminal); + if (rtd.is_bridged) { + for (auto bridge : m_bridge) { + if (bridge->real_terminals.contains(real_terminal)) { + rtd.bridge_uuid = bridge->uuid_; + break; + } + } + } return rtd; } @@ -1166,3 +1257,17 @@ QSharedPointer TerminalStrip::bridgeForUuid(const QUuid &br return QSharedPointer(); } + +/** + * @brief TerminalStrip::rebuildRealVector + * Rebuild the real terminal vector + * to be ordered + */ +void TerminalStrip::rebuildRealVector() +{ + m_real_terminals.clear(); + for (const auto phy : m_physical_terminals) { + for (const auto &real : phy->terminals()) + m_real_terminals.append(real); + } +} diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 837a01dc0..da16cca39 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -37,6 +37,8 @@ class TerminalElement; * @brief The RealTerminalData struct * Conveniant struct to quickly get some values * of a RealTerminal + * A RealTerminalData with level_ = -1 + * is considered as a null data */ struct RealTerminalData { @@ -86,7 +88,7 @@ inline uint qHash(const PhysicalTerminalData &key, uint seed) { struct TerminalStripBridge { QVector> real_terminals; - QColor color_ = Qt::gray; + QColor color_ = Qt::darkGray; QUuid uuid_ = QUuid::createUuid(); }; @@ -104,6 +106,8 @@ class TerminalStrip : public QObject Q_OBJECT public: + static QVector bridgeColor() {return QVector{Qt::red, Qt::blue, Qt::white, Qt::darkGray, Qt::black};} + signals: void orderChanged(); //Emitted when the order of the physical terminal is changed void bridgeChanged(); @@ -146,7 +150,12 @@ class TerminalStrip : public QObject bool setBridge(const QVector &real_terminals_uuid); bool setBridge(const QUuid &bridge_uuid, const QVector &real_terminals_uuid); void unBridge(const QVector &real_terminals_uuid); - QSharedPointer bridgeFor(const QUuid &real_terminal_uuid) const; + const QSharedPointer bridgeFor(const QUuid &real_terminal_uuid) const; + + RealTerminalData previousTerminalInLevel(const QUuid &real_terminal_uuid) const; + RealTerminalData nextTerminalInLevel(const QUuid &real_terminal_uuid) const; + RealTerminalData previousRealTerminal(const QUuid &real_terminal_uuid) const; + RealTerminalData nextRealTerminal(const QUuid &real_terminal_uuid) const; QVector> terminalElement() const; @@ -163,6 +172,7 @@ class TerminalStrip : public QObject QSharedPointer isBridged(const QSharedPointer real_terminal) const; QSharedPointer bridgeFor (const QVector> &terminal_vector) const; QSharedPointer bridgeForUuid (const QUuid &bridge_uuid); + void rebuildRealVector(); private: TerminalStripData m_data; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 4706dc227..eb193c1ea 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -60,8 +60,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : #endif //Setup the bridge color - QList bridge_color{Qt::red, Qt::blue, Qt::white, Qt::gray, Qt::black}; - ui->m_bridge_color_cb->setColors(bridge_color); + ui->m_bridge_color_cb->setColors(TerminalStrip::bridgeColor().toList()); setUpUndoConnections(); @@ -329,7 +328,7 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_) m_model = new TerminalStripModel(strip_, this); ui->m_table_widget->setModel(m_model); - setUpBridgeCellWidth(); + m_model->buildBridgePixmap(setUpBridgeCellWidth()); spanMultiLevelTerminals(); selectionChanged(); //Used to update child widgets @@ -461,7 +460,7 @@ void TerminalStripEditor::selectionChanged() ui->m_unbridge_terminals_pb->setEnabled(enable_unbridge); } -void TerminalStripEditor::setUpBridgeCellWidth() +QSize TerminalStripEditor::setUpBridgeCellWidth() { if (ui->m_table_widget->verticalHeader() && m_model) @@ -469,15 +468,15 @@ void TerminalStripEditor::setUpBridgeCellWidth() auto section_size = ui->m_table_widget->verticalHeader()->defaultSectionSize(); auto h_header = ui->m_table_widget->horizontalHeader(); - h_header->setSectionResizeMode(2, QHeaderView::Fixed); - h_header->resizeSection(2, section_size); - h_header->setSectionResizeMode(3, QHeaderView::Fixed); - h_header->resizeSection(3, section_size); - h_header->setSectionResizeMode(4, QHeaderView::Fixed); - h_header->resizeSection(4, section_size); - h_header->setSectionResizeMode(5, QHeaderView::Fixed); - h_header->resizeSection(5, section_size); + for (int i = TerminalStripModel::Level0 ; i<(TerminalStripModel::Level3+1) ; ++i) { + ui->m_table_widget->setColumnWidth(i, section_size); + h_header->setSectionResizeMode(i, QHeaderView::Fixed); + } + + return QSize(section_size, section_size); } + + return QSize(0,0); } /** diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index df07b5acf..7f73e1f17 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -53,7 +53,7 @@ class TerminalStripEditor : public QDialog void setCurrentStrip(TerminalStrip *strip_); void spanMultiLevelTerminals(); void selectionChanged(); - void setUpBridgeCellWidth(); + QSize setUpBridgeCellWidth(); TerminalStripModel::Column isSingleColumnSelected() const; private slots: diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index e0035a487..a5dc20747 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -24,6 +24,7 @@ #include #include #include +#include /** * Some const int who describe what a column contain @@ -143,18 +144,6 @@ QVariant TerminalStripModel::data(const QModelIndex &index, int role) const switch (index.column()) { case POS_CELL : return physicalDataAtIndex(index.row()).pos_; case LEVEL_CELL : return rtd.level_; - case LEVEL_0_CELL : - if (rtd.level_ == 0 && rtd.is_bridged) return "0"; - break; - case LEVEL_1_CELL : - if (rtd.level_ == 1 && rtd.is_bridged) return "0"; - break; - case LEVEL_2_CELL : - if (rtd.level_ == 2 && rtd.is_bridged) return "0"; - break; - case LEVEL_3_CELL : - if (rtd.level_ == 3 && rtd.is_bridged) return "0"; - break; case LABEL_CELL : return rtd.label_; case XREF_CELL : return rtd.Xref_; case CABLE_CELL : return rtd.cable_; @@ -186,6 +175,18 @@ QVariant TerminalStripModel::data(const QModelIndex &index, int role) const return QBrush(Qt::yellow); } } + else if (role == Qt::DecorationRole && + (index.column() == LEVEL_0_CELL || + index.column() == LEVEL_1_CELL || + index.column() == LEVEL_2_CELL || + index.column() == LEVEL_3_CELL)) + { + return bridgePixmapFor(index); + auto pixmap_ = bridgePixmapFor(index); + if (!pixmap_.isNull()) { + return pixmap_; + } + } return QVariant(); } @@ -403,6 +404,78 @@ RealTerminalData TerminalStripModel::realTerminalDataForIndex(const QModelIndex } } +/** + * @brief TerminalStripModel::buildBridgePixmap + * Build the pixmap of bridge. + * You should call this method when you know the + * size to use in the bridge cell of a QTableView. + * @param pixmap_size + */ +void TerminalStripModel::buildBridgePixmap(const QSize &pixmap_size) +{ + m_bridges_pixmaps.clear(); + for (auto color_ : TerminalStrip::bridgeColor()) + { + QPen pen; + pen.setColor(color_); + pen.setWidth(1); + + QBrush brush; + brush.setColor(color_); + brush.setStyle(Qt::SolidPattern); + + QPixmap top_(pixmap_size); + top_.fill(Qt::lightGray); + QPainter top_p(&top_); + top_p.setPen(pen); + top_p.setBrush(brush); + + QPixmap middle_(pixmap_size); + middle_.fill(Qt::lightGray); + QPainter middle_p(&middle_); + middle_p.setPen(pen); + middle_p.setBrush(brush); + + QPixmap bottom_(pixmap_size); + bottom_.fill(Qt::lightGray); + QPainter bottom_p(&bottom_); + bottom_p.setPen(pen); + bottom_p.setBrush(brush); + + QPixmap none_(pixmap_size); + none_.fill(Qt::lightGray); + QPainter none_p(&none_); + none_p.setPen(pen); + none_p.setBrush(brush); + + + auto w_ = pixmap_size.width(); + auto h_ = pixmap_size.height(); + + //Draw circle + top_p.drawEllipse(QPoint(w_/2, h_/2), w_/4, h_/4); + middle_p.drawEllipse(QPoint(w_/2, h_/2), w_/4, h_/4); + bottom_p.drawEllipse(QPoint(w_/2, h_/2), w_/4, h_/4); + + //Draw top line + middle_p.drawRect((w_/2)-(w_/8), 0, w_/4, h_/2); + bottom_p.drawRect((w_/2)-(w_/8), 0, w_/4, h_/2); + none_p.drawRect((w_/2)-(w_/8), 0, w_/4, h_/2); + + //Draw bottom line + top_p.drawRect((w_/2)-(w_/8), h_/2, w_/4, h_/2); + middle_p.drawRect((w_/2)-(w_/8), h_/2, w_/4, h_/2); + none_p.drawRect((w_/2)-(w_/8), h_/2, w_/4, h_/2); + + BridgePixmap bpxm; + bpxm.top_ = top_; + bpxm.middle_ = middle_; + bpxm.bottom_ = bottom_; + bpxm.none_ = none_; + m_bridges_pixmaps.insert(color_, bpxm); + } +} + void TerminalStripModel::fillPhysicalTerminalData() { //Get all physical terminal @@ -537,6 +610,127 @@ RealTerminalData TerminalStripModel::realDataAtIndex(int index) const return RealTerminalData(); } +QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const +{ + if (!index.isValid() || m_terminal_strip.isNull()) { + return QPixmap(); + } + + auto level_column = levelForColumn(columnTypeForIndex(index)); + if (level_column == -1) { + return QPixmap(); + } + + auto rtd = realTerminalDataForIndex(index); + + //Terminal level correspond to the column level of index + if (level_column == rtd.level_) + { + if (rtd.is_bridged) + { + auto bridge_uuid = rtd.bridge_uuid; + auto previous_bridge_uuid = m_terminal_strip->previousTerminalInLevel(rtd.real_terminal_uuid).bridge_uuid; + auto next_bridge_uuid = m_terminal_strip->nextTerminalInLevel(rtd.real_terminal_uuid).bridge_uuid; + + auto color_ = m_terminal_strip->bridgeFor(rtd.real_terminal_uuid)->color_; + auto pixmap_ = m_bridges_pixmaps.value(color_); + + //Current real terminal between two bridged terminal + if ((bridge_uuid == previous_bridge_uuid) && + (bridge_uuid == next_bridge_uuid)) { + return pixmap_.middle_; + } else if (bridge_uuid == previous_bridge_uuid) { + return pixmap_.bottom_; + } else if (bridge_uuid == next_bridge_uuid) { + return pixmap_.top_; + } + } + } + //Terminal level ins't in the same column level of index + //Check if we need to draw a none bridge pixmap + + //Check previous + auto physical_data = m_terminal_strip->physicalTerminalData(rtd); + auto current_real_uuid = rtd.real_terminal_uuid; + auto current_phy_uuid = physical_data.uuid_; + bool already_jumped_to_previous = false; + RealTerminalData previous_data; + + do { + auto previous_rtd = m_terminal_strip->previousRealTerminal(current_real_uuid); + current_real_uuid = previous_rtd.real_terminal_uuid; + + if (previous_rtd.level_ == -1) { + break; + } + + //We are in the same physical terminal as previous loop + if (current_phy_uuid == m_terminal_strip->physicalTerminalData(previous_rtd).uuid_) + { + if (previous_rtd.is_bridged && + previous_rtd.level_ == level_column) { + previous_data = previous_rtd; + break; + } + } + else if (already_jumped_to_previous) { //We are not in same physical terminal as previous loop + break; + } else { + already_jumped_to_previous = true; + current_phy_uuid = m_terminal_strip->physicalTerminalData(previous_rtd).uuid_; + if (previous_rtd.is_bridged && + previous_rtd.level_ == level_column) { + previous_data = previous_rtd; + break; + } + } + } while(true); + + //Check next + current_real_uuid = rtd.real_terminal_uuid; + current_phy_uuid = physical_data.uuid_; + bool already_jumped_to_next = false; + RealTerminalData next_data; + do { + auto next_rtd = m_terminal_strip->nextRealTerminal(current_real_uuid); + current_real_uuid = next_rtd.real_terminal_uuid; + + if (next_rtd.level_ == -1) { + break; + } + + //We are in the same physical terminal as previous loop + if (current_phy_uuid == m_terminal_strip->physicalTerminalData(next_rtd).uuid_) + { + if (next_rtd.is_bridged && + next_rtd.level_ == level_column) { + next_data = next_rtd; + break; + } + } + else if (already_jumped_to_next) { //We are not in same physical terminal as previous loop + break; + } else { + already_jumped_to_next = true; + current_phy_uuid = m_terminal_strip->physicalTerminalData(next_rtd).uuid_; + if (next_rtd.is_bridged && + next_rtd.level_ == level_column) { + next_data = next_rtd; + break; + } + } + } while(true); + + if (previous_data.bridge_uuid == next_data.bridge_uuid) { + auto bridge_ = m_terminal_strip->bridgeFor(previous_data.real_terminal_uuid); + if (bridge_) { + return m_bridges_pixmaps.value(bridge_->color_).none_; + } + } + + return QPixmap(); +} + /*********************************************************** * Alittle delegate for add a combobox to edit type * and a spinbox to edit the level of a terminal @@ -593,3 +787,43 @@ void TerminalStripModelDelegate::setModelData(QWidget *editor, QAbstractItemMode } } } + +/** + * @brief TerminalStripModelDelegate::paint + * By default on a QTableView, Qt draw pixmap in cell with a little margin at left. + * Override the function to draw the pixmap of bridge without the margin at left. + * @param painter + * @param option + * @param index + */ +void TerminalStripModelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + auto column = index.column(); + if (column == LEVEL_0_CELL || + column == LEVEL_1_CELL || + column == LEVEL_2_CELL || + column == LEVEL_3_CELL) + { + auto variant = index.data(Qt::DecorationRole); + if (variant.isNull()) { + QStyledItemDelegate::paint(painter, option, index); + } + else + { + if (option.state & QStyle::State_Selected) + { + QStyleOptionViewItem opt_ = option; + initStyleOption(&opt_, index); + QStyle *style = QApplication::style(); + auto px = style->generatedIconPixmap(QIcon::Selected, variant.value(), &opt_); + style->drawItemPixmap(painter, option.rect, Qt::AlignLeft, px); + } + else { + painter->drawPixmap(option.rect, variant.value()); + } + } + } + else { + QStyledItemDelegate::paint(painter, option, index); + } +} diff --git a/sources/TerminalStrip/ui/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 78e356739..7c8ea6d70 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -22,10 +22,16 @@ #include #include #include -#include +#include +#include #include "../terminalstrip.h" +//Code to use QColor as key for QHash +inline uint qHash(const QColor &key, uint seed) { + return qHash(key.name(), seed); +} + class TerminalStrip; class TerminalStripModel : public QAbstractTableModel @@ -68,21 +74,31 @@ class TerminalStripModel : public QAbstractTableModel QVector realTerminalDataForIndex(QModelIndexList index_list) const; RealTerminalData realTerminalDataForIndex(const QModelIndex &index) const; + void buildBridgePixmap(const QSize &pixmap_size); + private: void fillPhysicalTerminalData(); RealTerminalData dataAtRow(int row) const; void replaceDataAtRow(RealTerminalData data, int row); PhysicalTerminalData physicalDataAtIndex(int index) const; RealTerminalData realDataAtIndex(int index) const; + QPixmap bridgePixmapFor(const QModelIndex &index) const; private: QPointer m_terminal_strip; QVector m_edited_terminal_data, m_original_terminal_data; QHash> m_modified_cell; - QPixmap m_bridge_top, - m_bride_bottom, - m_bridge, - m_bride_both; + + struct BridgePixmap + { + QPixmap top_, + middle_, + bottom_, + none_; + }; + + QHash m_bridges_pixmaps; + }; class TerminalStripModelDelegate : public QStyledItemDelegate @@ -100,6 +116,8 @@ class TerminalStripModelDelegate : public QStyledItemDelegate QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; }; #endif // TERMINALSTRIPMODEL_H From 6505330b5fbfb2ec8155e58e82291e30dfa18d53 Mon Sep 17 00:00:00 2001 From: joshua Date: Sun, 19 Dec 2021 14:55:02 +0100 Subject: [PATCH 05/14] Make code less spaghetti Not finished yet. --- .../UndoCommand/bridgeterminalscommand.cpp | 33 +- .../UndoCommand/bridgeterminalscommand.h | 11 +- .../UndoCommand/changeterminallevel.cpp | 4 +- .../UndoCommand/changeterminallevel.h | 4 +- .../UndoCommand/groupterminalscommand.cpp | 12 +- .../UndoCommand/groupterminalscommand.h | 10 +- .../UndoCommand/sortterminalstripcommand.cpp | 4 +- sources/TerminalStrip/terminalstrip.cpp | 394 ++++++++++++------ sources/TerminalStrip/terminalstrip.h | 110 +++-- .../TerminalStrip/ui/terminalstripeditor.cpp | 132 +++--- .../TerminalStrip/ui/terminalstripmodel.cpp | 278 ++++++------ sources/TerminalStrip/ui/terminalstripmodel.h | 63 ++- 12 files changed, 649 insertions(+), 406 deletions(-) diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp index 663683a7e..7c3a19b61 100644 --- a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp @@ -16,14 +16,13 @@ along with QElectroTech. If not, see . */ #include "bridgeterminalscommand.h" -#include "../terminalstrip.h" BridgeTerminalsCommand::BridgeTerminalsCommand(TerminalStrip *strip, - QVector real_terminal_uuid, + QVector> real_terminal, QUndoCommand *parent): QUndoCommand(parent), m_strip(strip), - m_uuid_vector(real_terminal_uuid) + m_real_terminal_vector(real_terminal) { setText(QObject::tr("Ponter des bornes entre-elles")); } @@ -31,40 +30,44 @@ BridgeTerminalsCommand::BridgeTerminalsCommand(TerminalStrip *strip, void BridgeTerminalsCommand::undo() { if (m_strip) { - m_strip->unBridge(m_uuid_vector); + m_strip->unBridge(m_real_terminal_vector); } } void BridgeTerminalsCommand::redo() { if (m_strip) { - m_strip->setBridge(m_uuid_vector); + m_strip->setBridge(m_real_terminal_vector); } } UnBridgeTerminalsCommand::UnBridgeTerminalsCommand(TerminalStrip *strip, - QVector real_terminal_uuid, + QVector> real_terminal, QUndoCommand *parent): QUndoCommand(parent), m_strip(strip) { setText(QObject::tr("Supprimer des ponts de bornes")); - for (const auto &t_uuid : real_terminal_uuid) + for (const auto &real_t : real_terminal) { - auto bridge = m_strip->bridgeFor(t_uuid); - if (bridge) { - m_bridge_terminal_map.insert(bridge->uuid_, t_uuid); + auto bridge_ = strip->bridgeFor(real_t); + if (bridge_) { + m_bridge_terminal_hash.insert(bridge_.toWeakRef(), real_t); } } } void UnBridgeTerminalsCommand::undo() { - if (m_strip) { - for (const auto &bridge_uuid : m_bridge_terminal_map.uniqueKeys()) { - auto terminal_list = m_bridge_terminal_map.values(bridge_uuid); - m_strip->setBridge(bridge_uuid, terminal_list.toVector()); + if (m_strip) + { + for (const auto &bridge_ : m_bridge_terminal_hash.uniqueKeys()) + { + if (!bridge_.isNull()) { + auto terminal_list = m_bridge_terminal_hash.values(bridge_); + m_strip->setBridge(bridge_.toStrongRef() , terminal_list.toVector()); + } } } } @@ -72,6 +75,6 @@ void UnBridgeTerminalsCommand::undo() void UnBridgeTerminalsCommand::redo() { if (m_strip) { - m_strip->unBridge(m_bridge_terminal_map.values().toVector()); + m_strip->unBridge(m_bridge_terminal_hash.values().toVector()); } } diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h index 03a6e8b17..96f1c795a 100644 --- a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h @@ -20,11 +20,10 @@ #include #include -#include #include #include -class TerminalStrip; +#include "../terminalstrip.h" /** * @brief The BridgeTerminalsCommand class @@ -34,7 +33,7 @@ class TerminalStrip; class BridgeTerminalsCommand : public QUndoCommand { public: - BridgeTerminalsCommand(TerminalStrip *strip, QVector real_terminal_uuid, QUndoCommand *parent = nullptr); + BridgeTerminalsCommand(TerminalStrip *strip, QVector> real_terminal, QUndoCommand *parent = nullptr); ~BridgeTerminalsCommand() override {} void undo() override; @@ -42,7 +41,7 @@ class BridgeTerminalsCommand : public QUndoCommand private: QPointer m_strip; - QVector m_uuid_vector; + QVector> m_real_terminal_vector; }; @@ -54,7 +53,7 @@ class BridgeTerminalsCommand : public QUndoCommand class UnBridgeTerminalsCommand : public QUndoCommand { public: - UnBridgeTerminalsCommand(TerminalStrip *strip, QVector real_terminal_uuid, QUndoCommand *parent = nullptr); + UnBridgeTerminalsCommand(TerminalStrip *strip, QVector> real_terminal, QUndoCommand *parent = nullptr); ~UnBridgeTerminalsCommand() override{} void undo() override; @@ -62,7 +61,7 @@ class UnBridgeTerminalsCommand : public QUndoCommand private: QPointer m_strip; - QMultiMap m_bridge_terminal_map; ///Key is bridge uuid, value is real terminal uuid + QMultiHash, QWeakPointer> m_bridge_terminal_hash; ///Key a is bridge, value is real terminal }; diff --git a/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp b/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp index 42c5dceba..004a3e46e 100644 --- a/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp +++ b/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp @@ -18,14 +18,14 @@ #include "changeterminallevel.h" ChangeTerminalLevel::ChangeTerminalLevel(TerminalStrip *strip, - const RealTerminalData &real_terminal, + const QWeakPointer &real_terminal, int level, QUndoCommand *parent) : QUndoCommand(parent), m_strip(strip), m_real_terminal(real_terminal), m_new_level(level), - m_old_level(real_terminal.level_) + m_old_level(m_strip->realTerminalDataFor(real_terminal).level()) {} void ChangeTerminalLevel::undo() diff --git a/sources/TerminalStrip/UndoCommand/changeterminallevel.h b/sources/TerminalStrip/UndoCommand/changeterminallevel.h index 39b9a3c1a..248695161 100644 --- a/sources/TerminalStrip/UndoCommand/changeterminallevel.h +++ b/sources/TerminalStrip/UndoCommand/changeterminallevel.h @@ -26,7 +26,7 @@ class ChangeTerminalLevel : public QUndoCommand { public: ChangeTerminalLevel(TerminalStrip *strip, - const RealTerminalData &real_terminal, + const QWeakPointer &real_terminal, int level, QUndoCommand *parent = nullptr); @@ -35,7 +35,7 @@ class ChangeTerminalLevel : public QUndoCommand private: QPointer m_strip; - RealTerminalData m_real_terminal; + QWeakPointer m_real_terminal; int m_new_level, m_old_level; }; diff --git a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp index 1ded81741..a2857de50 100644 --- a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp @@ -25,7 +25,7 @@ */ GroupTerminalsCommand::GroupTerminalsCommand(TerminalStrip *strip, const PhysicalTerminalData &receiver_, - const QVector &to_group, + const QVector> &to_group, QUndoCommand *parent): QUndoCommand(parent), m_terminal_strip(strip), @@ -48,7 +48,7 @@ void GroupTerminalsCommand::redo() { } UnGroupTerminalsCommand::UnGroupTerminalsCommand(TerminalStrip *strip, - const QVector &to_ungroup, + const QVector> &to_ungroup, QUndoCommand *parent) : QUndoCommand(parent), m_terminal_strip(strip) @@ -77,11 +77,11 @@ void UnGroupTerminalsCommand::redo() } } -void UnGroupTerminalsCommand::setUp(const QVector &to_ungroup) +void UnGroupTerminalsCommand::setUp(const QVector> &to_ungroup) { - for (auto rtd_ : to_ungroup) + for (const auto &rt_ : to_ungroup) { - auto ptd_ = m_terminal_strip->physicalTerminalData(rtd_); + auto ptd_ = m_terminal_strip->physicalTerminalData(rt_); //Physical have only one real terminal, no need to ungroup it if (ptd_.real_terminals_vector.size() <= 1) { @@ -89,7 +89,7 @@ void UnGroupTerminalsCommand::setUp(const QVector &to_ungroup) } auto vector_ = m_physical_real_H.value(ptd_); - vector_.append(rtd_); + vector_.append(rt_); m_physical_real_H.insert(ptd_, vector_); } } diff --git a/sources/TerminalStrip/UndoCommand/groupterminalscommand.h b/sources/TerminalStrip/UndoCommand/groupterminalscommand.h index 9ef37b19d..55b250319 100644 --- a/sources/TerminalStrip/UndoCommand/groupterminalscommand.h +++ b/sources/TerminalStrip/UndoCommand/groupterminalscommand.h @@ -32,7 +32,7 @@ class GroupTerminalsCommand : public QUndoCommand public: GroupTerminalsCommand(TerminalStrip *strip, const PhysicalTerminalData &receiver_, - const QVector &to_group, + const QVector> &to_group, QUndoCommand *parent = nullptr); void undo() override; @@ -41,7 +41,7 @@ class GroupTerminalsCommand : public QUndoCommand private: QPointer m_terminal_strip; PhysicalTerminalData m_receiver; - QVector m_to_group; + QVector> m_to_group; }; /** @@ -52,18 +52,18 @@ class UnGroupTerminalsCommand : public QUndoCommand { public: UnGroupTerminalsCommand(TerminalStrip *strip, - const QVector &to_ungroup, + const QVector> &to_ungroup, QUndoCommand *parent = nullptr); void undo() override; void redo() override; private: - void setUp(const QVector &to_ungroup); + void setUp(const QVector> &to_ungroup); private: QPointer m_terminal_strip; - QHash > m_physical_real_H; + QHash >> m_physical_real_H; }; #endif // GROUPTERMINALSCOMMAND_H diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp index 538a5e516..51287faca 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp @@ -54,7 +54,7 @@ void SortTerminalStripCommand::sort() if (arg1.real_terminals_vector.count()) { - str1 = arg1.real_terminals_vector.constLast().label_; + str1 = arg1.real_terminals_vector.constLast().label(); auto match = rx.match(str1); if (match.hasMatch()) { @@ -64,7 +64,7 @@ void SortTerminalStripCommand::sort() if (arg2.real_terminals_vector.count()) { - str2 = arg2.real_terminals_vector.constLast().label_; + str2 = arg2.real_terminals_vector.constLast().label(); auto match = rx.match(str2); if (match.hasMatch()) { diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index adce9d1c0..f5e394c89 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -55,6 +55,13 @@ class RealTerminal m_parent_terminal_strip(parent_strip) {} + /** + * @brief parentStrip + * @return parent terminal strip + */ + TerminalStrip *parentStrip() const { + return m_parent_terminal_strip.data(); + } /** * @brief isElement * @return true if this real terminal is linked to a terminal element @@ -528,7 +535,7 @@ PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) const auto physical_terminal = m_physical_terminals.at(index); ptd.pos_ = index; for (auto real_terminal : physical_terminal->terminals()) { - auto rtd = realTerminalData(real_terminal); + auto rtd = RealTerminalData(real_terminal); ptd.real_terminals_vector.append(rtd); } ptd.uuid_ = physical_terminal->uuid(); @@ -539,15 +546,15 @@ PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) const /** * @brief TerminalStrip::physicalTerminalData - * @param real_data - * @return the parent PhysicalTerminalData of \p real_data. - * the PhysicalTerminalData can be invalid if \p real_data don't belong to this strip + * @param real_terminal + * @return the parent PhysicalTerminalData of \p real_terminal. + * the PhysicalTerminalData can be invalid if \p real_terminal don't belong to this strip */ -PhysicalTerminalData TerminalStrip::physicalTerminalData(const RealTerminalData &real_data) const +PhysicalTerminalData TerminalStrip::physicalTerminalData (QWeakPointer real_terminal) const { PhysicalTerminalData ptd_; - const auto real_t = realTerminalForUuid(real_data.real_terminal_uuid); + const auto real_t = real_terminal.toStrongRef(); if (real_t.isNull()) { return ptd_; } @@ -559,7 +566,7 @@ PhysicalTerminalData TerminalStrip::physicalTerminalData(const RealTerminalData ptd_.pos_ = m_physical_terminals.indexOf(phy_t); for (auto real_terminal : phy_t->terminals()) { - auto rtd = realTerminalData(real_terminal); + auto rtd = RealTerminalData(real_terminal); ptd_.real_terminals_vector.append(rtd); } ptd_.uuid_ = phy_t->uuid(); @@ -634,7 +641,7 @@ bool TerminalStrip::setOrderTo(const QVector &sorted_vecto * @param receiver_terminal * @return true if success */ -bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector &added_terminals) +bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector> &added_terminals) { const auto receiver_ = physicalTerminalForUuid(receiver_terminal.uuid_); if (receiver_.isNull()) { @@ -645,7 +652,7 @@ bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal bool have_grouped = false; for (const auto &added : added_terminals) { - const auto added_terminal = realTerminalForUuid(added.real_terminal_uuid); + const auto added_terminal = added.toStrongRef(); if (added_terminal.isNull()) { continue; @@ -677,12 +684,12 @@ bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal * Ungroup all real terminals of \p terminals_to_ungroup * @param terminals_to_ungroup */ -void TerminalStrip::unGroupTerminals(const QVector &terminals_to_ungroup) +void TerminalStrip::unGroupTerminals(const QVector> &terminals_to_ungroup) { bool ungrouped = false; - for (const auto &rtd_ : terminals_to_ungroup) + for (const auto &rt_ : terminals_to_ungroup) { - if (auto real_terminal = realTerminalForUuid(rtd_.real_terminal_uuid)) //Get the shared real terminal + if (auto real_terminal = rt_.toStrongRef()) //Get the shared real terminal { if (auto physical_terminal = physicalTerminal(real_terminal)) //Get the physical terminal { @@ -710,16 +717,16 @@ void TerminalStrip::unGroupTerminals(const QVector &terminals_ * @param level * @return */ -bool TerminalStrip::setLevel(const RealTerminalData &real_terminal_data, int level) +bool TerminalStrip::setLevel(const QWeakPointer &real_terminal, int level) { - auto real_terminal = realTerminalForUuid(real_terminal_data.real_terminal_uuid); - if (real_terminal) + auto real_t = real_terminal.toStrongRef(); + if (real_t) { - auto physical_terminal = physicalTerminal(real_terminal); + auto physical_terminal = physicalTerminal(real_t); if (physical_terminal) { if (physical_terminal->terminals().size() > 1 && - physical_terminal->setLevelOf(real_terminal, level)) + physical_terminal->setLevelOf(real_t, level)) { emit orderChanged(); return true; @@ -732,27 +739,26 @@ bool TerminalStrip::setLevel(const RealTerminalData &real_terminal_data, int lev /** * @brief TerminalStrip::isBridgeable - * Check if all realTerminal represented by the uuid of @a real_terminals_uuid are bridgeable together. + * Check if all realTerminal in @a real_terminals_data are bridgeable together. * To be bridgeable, each real terminal must belong to this terminal strip * be at the same level, be consecutive and not belong to the same physicalTerminal * and at least one terminal must be not bridged - * @param real_terminals_uuid : a vector of RealTerminal uuid - * @sa member real_terminal_uuid of struct RealTerminalData + * @param real_terminals_data : a vector of RealTerminalData * @return */ -bool TerminalStrip::isBridgeable(const QVector &real_terminals_uuid) const +bool TerminalStrip::isBridgeable(const QVector &real_terminals_data) const { - if (real_terminals_uuid.size() < 2) { + if (real_terminals_data.size() < 2) { return false; } // Check if first terminal belong to this strip - auto first_real_terminal = realTerminalForUuid(real_terminals_uuid.first()); + auto first_real_terminal = real_terminals_data.first().m_real_terminal.toStrongRef(); if (!first_real_terminal) { return false; } // Get the level of the first terminal - int level_ = realTerminalData(first_real_terminal).level_; + int level_ = real_terminals_data.first().level(); // Get the physical terminal and pos auto first_physical_terminal = physicalTerminal(first_real_terminal); QVector physical_vector{first_physical_terminal}; @@ -763,16 +769,16 @@ bool TerminalStrip::isBridgeable(const QVector &real_terminals_uuid) cons bool no_bridged = bridge_ ? false : true; // Check for each terminals - for (int i=1 ; i &real_terminals_uuid) cons return no_bridged; } +bool TerminalStrip::isBridgeable(const QVector> &real_terminals) const +{ + QVector data; + + for (const auto &wrt : real_terminals) + { + data.append(RealTerminalData(wrt.toStrongRef())); + } + return isBridgeable(data); +} + /** * @brief TerminalStrip::setBridge - * Set a bridge betwen all real terminal represented by they uuid - * @param real_terminals_uuid + * Set a bridge betwen all real terminal of @a real_terminals * @sa TerminalStrip::isBridgeable * @return true if bridge was successfully created */ -bool TerminalStrip::setBridge(const QVector &real_terminals_uuid) +bool TerminalStrip::setBridge(const QVector> &real_terminals) { - if (!isBridgeable(real_terminals_uuid)) { + if (!isBridgeable(real_terminals)) { return false; } QVector real_terminals_vector; - for (const auto & uuid_ : real_terminals_uuid) { - auto real_t = realTerminalForUuid(uuid_); - if (real_t) - real_terminals_vector << real_t; + for (const auto &real_terminal : real_terminals) + { + auto real_t = real_terminal.toStrongRef(); + if (real_t) { + real_terminals_vector.append(real_t); + } } auto bridge = bridgeFor(real_terminals_vector); @@ -848,29 +866,27 @@ bool TerminalStrip::setBridge(const QVector &real_terminals_uuid) /** * @brief TerminalStrip::setBridge - * Bridge the RealTerminal with uuid in @a real_terminals_uuid to - * the bridge with uuid @a bridge_uuid. - * @param bridge_uuid - * @param real_terminals_uuid + * Bridge the RealTerminal of @a real_terminals_data to @a bridge + * @param bridge + * @param real_terminals_data * @return true if all RealTerminal was successfully bridged */ -bool TerminalStrip::setBridge(const QUuid &bridge_uuid, const QVector &real_terminals_uuid) +bool TerminalStrip::setBridge(QSharedPointer bridge, const QVector> &real_terminals) { - auto bridge_ = bridgeForUuid(bridge_uuid); - if (bridge_) + if (bridge) { - if (!isBridgeable(real_terminals_uuid)) { + if (!isBridgeable(real_terminals)) { return false; } bool b_ = false; - for (const auto & uuid_ : real_terminals_uuid) + for (const auto & rt_ : real_terminals) { - auto real_t = realTerminalForUuid(uuid_); + auto real_t = rt_.toStrongRef(); if (real_t && - !bridge_->real_terminals.contains(real_t)) + !bridge->real_terminals.contains(real_t)) { - bridge_->real_terminals.append(real_t); + bridge->real_terminals.append(real_t); b_ = true; } } @@ -886,46 +902,36 @@ bool TerminalStrip::setBridge(const QUuid &bridge_uuid, const QVector &re /** * @brief TerminalStrip::unBridge - * Unbridge all real terminal represented by they uuid + * Unbridge all real terminal of @a real_terminals * @param real_terminals_uuid */ -void TerminalStrip::unBridge(const QVector &real_terminals_uuid) +void TerminalStrip::unBridge(const QVector> &real_terminals) { - for (const auto & uuid_ : real_terminals_uuid) + for (const auto &real_t : qAsConst(real_terminals)) { - auto real_t = realTerminalForUuid(uuid_); - if (real_t) - { - auto bridge_ = isBridged(real_t); - if (bridge_) - bridge_->real_terminals.removeOne(real_t); + auto bridge_ = bridgeFor(real_t); + if (bridge_) { + bridge_->real_terminals.removeOne(real_t.toStrongRef()); } } emit bridgeChanged(); } -/** - * @brief TerminalStrip::bridgeFor - * @param real_terminal_uuid - * @return - */ -const QSharedPointer TerminalStrip::bridgeFor(const QUuid &real_terminal_uuid) const +QSharedPointer TerminalStrip::bridgeFor(QWeakPointer real_terminal) const { - auto rt = realTerminalForUuid(real_terminal_uuid); - return bridgeFor(QVector{rt}); + return this->isBridged(real_terminal.toStrongRef()); } /** * @brief TerminalStrip::previousTerminalInLevel - * @param real_terminal_uuid - * @return The previous real terminal at the samne level - * as the real terminal with uuid @a real_terminal_uuid - * If there is not a previous terminal, return a null RealTerminalData + * @param real_terminal + * @return The previous real terminal at the samne level of @a real_t + * or a null RealTerminalData if there not a previous real terminal */ -RealTerminalData TerminalStrip::previousTerminalInLevel(const QUuid &real_terminal_uuid) const +RealTerminalData TerminalStrip::previousTerminalInLevel(const QWeakPointer &real_terminal) const { - auto real_t = realTerminalForUuid(real_terminal_uuid); + auto real_t = real_terminal.toStrongRef(); if (real_t) { auto phy_t = physicalTerminal(real_t); @@ -938,7 +944,7 @@ RealTerminalData TerminalStrip::previousTerminalInLevel(const QUuid &real_termin auto t_vector = m_physical_terminals.at(index-1)->terminals(); if (t_vector.size() > level_) { - return realTerminalData(t_vector.at(level_)); + return RealTerminalData(t_vector.at(level_)); } } } @@ -949,14 +955,13 @@ RealTerminalData TerminalStrip::previousTerminalInLevel(const QUuid &real_termin /** * @brief TerminalStrip::nextTerminalInLevel - * @param real_terminal_uuid - * @return The next real terminal at the samne level - * as the real terminal with uuid @a real_terminal_uuid - * If there is not a next terminal, return a null RealTerminalData + * @param real_terminal + * @return The next real terminal at the same level of @a real_t + * or a null RealTerminalData if there not a next real terminal */ -RealTerminalData TerminalStrip::nextTerminalInLevel(const QUuid &real_terminal_uuid) const +RealTerminalData TerminalStrip::nextTerminalInLevel(const QWeakPointer &real_terminal) const { - auto real_t = realTerminalForUuid(real_terminal_uuid); + auto real_t = real_terminal.toStrongRef(); if (real_t) { auto phy_t = physicalTerminal(real_t); @@ -969,7 +974,7 @@ RealTerminalData TerminalStrip::nextTerminalInLevel(const QUuid &real_terminal_u auto t_vector = m_physical_terminals.at(index+1)->terminals(); if (t_vector.size() > level_) { - return realTerminalData(t_vector.at(level_)); + return RealTerminalData(t_vector.at(level_)); } } } @@ -978,26 +983,35 @@ RealTerminalData TerminalStrip::nextTerminalInLevel(const QUuid &real_terminal_u return RealTerminalData(); } -RealTerminalData TerminalStrip::previousRealTerminal(const QUuid &real_terminal_uuid) const +RealTerminalData TerminalStrip::previousRealTerminal(const QWeakPointer &real_terminal) const { - auto real = realTerminalForUuid(real_terminal_uuid); + auto real = real_terminal.toStrongRef(); auto index = m_real_terminals.indexOf(real); if (index) { - return realTerminalData(m_real_terminals.at(index-1)); + return RealTerminalData(m_real_terminals.at(index-1)); } return RealTerminalData(); } -RealTerminalData TerminalStrip::nextRealTerminal(const QUuid &real_terminal_uuid) const +RealTerminalData TerminalStrip::nextRealTerminal(const QWeakPointer &real_terminal) const { - auto real = realTerminalForUuid(real_terminal_uuid); + auto real = real_terminal.toStrongRef(); auto index = m_real_terminals.indexOf(real); if (index != m_real_terminals.size()-1) { - return realTerminalData(m_real_terminals.at(index+1)); + return RealTerminalData(m_real_terminals.at(index+1)); } return RealTerminalData(); } +RealTerminalData TerminalStrip::realTerminalDataFor(const QWeakPointer &real_terminal) const +{ + auto rt = real_terminal.toStrongRef(); + if (rt && m_real_terminals.contains(rt)) + return RealTerminalData(rt); + else + return RealTerminalData(); +} + /** * @brief TerminalStrip::terminalElement * @return A vector of all terminal element owned by this strip @@ -1121,38 +1135,6 @@ QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer< return pt; } -RealTerminalData TerminalStrip::realTerminalData(const QSharedPointer real_terminal) const -{ - RealTerminalData rtd; - - auto physical_terminal = physicalTerminal(real_terminal); - - rtd.real_terminal_uuid = real_terminal->uuid(); - rtd.level_ = physical_terminal->levelOf(real_terminal); - rtd.label_ = real_terminal->label(); - - if (real_terminal->isElement()) { - rtd.Xref_ = autonum::AssignVariables::genericXref(real_terminal->element()); - rtd.element_uuid = real_terminal->elementUuid(); - rtd.element_ = real_terminal->element(); - } - rtd.type_ = real_terminal->type(); - rtd.function_ = real_terminal->function(); - rtd.led_ = real_terminal->led(); - rtd.is_element = real_terminal->isElement(); - rtd.is_bridged = isBridged(real_terminal); - if (rtd.is_bridged) { - for (auto bridge : m_bridge) { - if (bridge->real_terminals.contains(real_terminal)) { - rtd.bridge_uuid = bridge->uuid_; - break; - } - } - } - - return rtd; -} - /** * @brief TerminalStrip::physicalTerminalForUuid * Return the PhysicalTerminal with uuid \p uuid or a null @@ -1242,22 +1224,6 @@ QSharedPointer TerminalStrip::bridgeFor(const QVector TerminalStrip::bridgeForUuid(const QUuid &bridge_uuid) -{ - for (const auto &bridge : qAsConst(m_bridge)) { - if (bridge->uuid_ == bridge_uuid) { - return bridge; - } - } - - return QSharedPointer(); -} - /** * @brief TerminalStrip::rebuildRealVector * Rebuild the real terminal vector @@ -1267,7 +1233,169 @@ void TerminalStrip::rebuildRealVector() { m_real_terminals.clear(); for (const auto phy : m_physical_terminals) { - for (const auto &real : phy->terminals()) + for (const auto real : phy->terminals()) m_real_terminals.append(real); } } + +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ + + +/** + * @brief RealTerminalData::RealTerminalData + * @param real_terminal + */ +RealTerminalData::RealTerminalData(QSharedPointer real_terminal) +{ + m_real_terminal = real_terminal.toWeakRef(); +} + +bool RealTerminalData::isNull() const +{ + return m_real_terminal.isNull(); +} + +int RealTerminalData::level() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + auto strip = shared_->parentStrip(); + if (strip) { + auto phys = strip->physicalTerminal(shared_); + if (phys) { + return phys->levelOf(shared_); + } + } + } + + return -1; +} + +QString RealTerminalData::label() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + return shared_->label(); + } else { + return QString(); + } +} + +QString RealTerminalData::Xref() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_ && shared_->isElement()) { + return autonum::AssignVariables::genericXref(shared_->element()); + } else { + return QString(); + } +} + +QString RealTerminalData::cable() const { + return QString(); +} + +QString RealTerminalData::cableWire() const { + return QString(); +} + +QString RealTerminalData::conductor() const { + return QString(); +} + +ElementData::TerminalType RealTerminalData::type() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + return shared_->type(); + } + + return ElementData::TerminalType::TTGeneric; +} + +ElementData::TerminalFunction RealTerminalData::function() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + return shared_->function(); + } + + return ElementData::TerminalFunction::TFGeneric; +} + +bool RealTerminalData::isLed() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + return shared_->led(); + } + + return false; +} + +bool RealTerminalData::isElement() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + return shared_->isElement(); + } + + return false; +} + +bool RealTerminalData::isBridged() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + auto strip = shared_->parentStrip(); + if (strip) { + return !strip->isBridged(shared_).isNull(); + } + } + return false; +} + +/** + * @brief RealTerminalData::element + * @return The element represented by this real + * terminal, or nullptr + */ +Element *RealTerminalData::element() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + return shared_->element(); + } + + return nullptr; +} + +QUuid RealTerminalData::elementUuid() const +{ + auto element_ = element(); + if (element_) { + return element_->uuid(); + } + return QUuid(); +} + +QSharedPointer RealTerminalData::bridge() const +{ + auto shared_ = m_real_terminal.toStrongRef(); + if (shared_) { + auto strip = shared_->parentStrip(); + if (strip) { + return strip->isBridged(shared_); + } + } + return QSharedPointer(); +} + +QWeakPointer RealTerminalData::realTerminal() const +{ + return m_real_terminal; +} diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index da16cca39..5d68bba6c 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -32,36 +32,59 @@ class PhysicalTerminal; class TerminalStripIndex; class TerminalElement; - -/** - * @brief The RealTerminalData struct - * Conveniant struct to quickly get some values - * of a RealTerminal - * A RealTerminalData with level_ = -1 - * is considered as a null data - */ -struct RealTerminalData +struct TerminalStripBridge { - int level_ = -1; + QVector> real_terminals; + QColor color_ = Qt::darkGray; + QUuid uuid_ = QUuid::createUuid(); +}; - QString label_, - Xref_, - cable_, - cable_wire_, - conductor_; +inline bool operator == (const TerminalStripBridge &bridge_1, const TerminalStripBridge &bridge_2) { + return (bridge_1.uuid_ == bridge_2.uuid_); +} - QUuid element_uuid, - real_terminal_uuid, - bridge_uuid; +inline uint qHash(const QWeakPointer &key, uint seed) +{ + const auto bridge = key.toStrongRef(); + if (bridge) { + return qHash(bridge->uuid_, seed); + } else { + return qHash(QUuid (), seed); + } +} - ElementData::TerminalType type_; - ElementData::TerminalFunction function_; +class RealTerminalData +{ + friend class TerminalStrip; + private: + RealTerminalData(QSharedPointer real_terminal); - bool led_ = false, - is_element = false, - is_bridged = false; + public: + RealTerminalData() {} - QPointer element_; + bool isNull() const; + int level() const; + QString label() const; + QString Xref() const; + QString cable() const; + QString cableWire() const; + QString conductor() const; + + ElementData::TerminalType type() const; + ElementData::TerminalFunction function() const; + + bool isLed() const; + bool isElement() const; + bool isBridged() const; + + Element* element() const; + QUuid elementUuid() const; + + QSharedPointer bridge() const; + QWeakPointer realTerminal() const; + + private: + QWeakPointer m_real_terminal; }; /** @@ -85,13 +108,6 @@ inline uint qHash(const PhysicalTerminalData &key, uint seed) { return qHash(key.uuid_, seed); } -struct TerminalStripBridge -{ - QVector> real_terminals; - QColor color_ = Qt::darkGray; - QUuid uuid_ = QUuid::createUuid(); -}; - /** * @brief The TerminalStrip class * This class hold all the datas and configurations @@ -102,6 +118,7 @@ struct TerminalStripBridge class TerminalStrip : public QObject { friend class TerminalStripModel; + friend class RealTerminalData; Q_OBJECT @@ -140,22 +157,25 @@ class TerminalStrip : public QObject int physicalTerminalCount() const; PhysicalTerminalData physicalTerminalData(int index) const; - PhysicalTerminalData physicalTerminalData (const RealTerminalData &real_data) const; + PhysicalTerminalData physicalTerminalData (QWeakPointer real_terminal) const; QVector physicalTerminalData() const; bool setOrderTo(const QVector &sorted_vector); - bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector &added_terminals); - void unGroupTerminals(const QVector &terminals_to_ungroup); - bool setLevel(const RealTerminalData &real_terminal_data, int level); - bool isBridgeable(const QVector &real_terminals_uuid) const; - bool setBridge(const QVector &real_terminals_uuid); - bool setBridge(const QUuid &bridge_uuid, const QVector &real_terminals_uuid); - void unBridge(const QVector &real_terminals_uuid); - const QSharedPointer bridgeFor(const QUuid &real_terminal_uuid) const; + bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector> &added_terminals); + void unGroupTerminals(const QVector> &terminals_to_ungroup); + bool setLevel(const QWeakPointer &real_terminal, int level); - RealTerminalData previousTerminalInLevel(const QUuid &real_terminal_uuid) const; - RealTerminalData nextTerminalInLevel(const QUuid &real_terminal_uuid) const; - RealTerminalData previousRealTerminal(const QUuid &real_terminal_uuid) const; - RealTerminalData nextRealTerminal(const QUuid &real_terminal_uuid) const; + bool isBridgeable(const QVector &real_terminals_data) const; + bool isBridgeable(const QVector> &real_terminals) const; + bool setBridge(const QVector> &real_terminals); + bool setBridge(QSharedPointer bridge, const QVector> &real_terminals); + void unBridge(const QVector> &real_terminals); + QSharedPointer bridgeFor(QWeakPointer real_terminal) const; + + RealTerminalData previousTerminalInLevel(const QWeakPointer &real_terminal) const; + RealTerminalData nextTerminalInLevel(const QWeakPointer &real_terminal) const; + RealTerminalData previousRealTerminal(const QWeakPointer &real_terminal) const; + RealTerminalData nextRealTerminal(const QWeakPointer &real_terminal) const; + RealTerminalData realTerminalDataFor(const QWeakPointer &real_terminal) const; QVector> terminalElement() const; @@ -166,12 +186,10 @@ class TerminalStrip : public QObject private: QSharedPointer realTerminal(Element *terminal); QSharedPointer physicalTerminal(QSharedPointer terminal) const; - RealTerminalData realTerminalData(const QSharedPointer real_terminal) const; QSharedPointer physicalTerminalForUuid (const QUuid &uuid) const; QSharedPointer realTerminalForUuid(const QUuid &uuid) const; QSharedPointer isBridged(const QSharedPointer real_terminal) const; QSharedPointer bridgeFor (const QVector> &terminal_vector) const; - QSharedPointer bridgeForUuid (const QUuid &bridge_uuid); void rebuildRealVector(); private: diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index eb193c1ea..f6753534d 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -72,10 +72,10 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : { if (m_model->columnTypeForIndex(index) == TerminalStripModel::XRef) { - auto rtd = m_model->realTerminalDataForIndex(index); - if (rtd.element_) + auto mrtd = m_model->modelRealTerminalDataForIndex(index); + if (mrtd.element_) { - auto elmt = rtd.element_; + auto elmt = mrtd.element_; auto diagram = elmt->diagram(); if (diagram) { @@ -235,12 +235,12 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s if (ptd.real_terminals_vector.size()) { auto real_t = ptd.real_terminals_vector.first(); - auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(real_t.label_), TerminalStripTreeWidget::Terminal); - terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t.element_uuid.toString()); + auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(real_t.label()), TerminalStripTreeWidget::Terminal); + terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t.elementUuid()); terminal_item->setIcon(0, QET::Icons::ElementTerminal); - if (real_t.element_) { - m_uuid_terminal_H.insert(real_t.element_uuid, qgraphicsitem_cast(real_t.element_)); + if (real_t.element()) { + m_uuid_terminal_H.insert(real_t.elementUuid(), qgraphicsitem_cast(real_t.element())); } } } @@ -395,28 +395,28 @@ void TerminalStripEditor::selectionChanged() ui->m_led_cb ->setEnabled(true); } - const auto physical_terminal_vector = m_model->physicalTerminalDataForIndex(index_list); - const auto real_terminal_vector = m_model->realTerminalDataForIndex(index_list); + const auto model_physical_terminal_vector = m_model->modelPhysicalTerminalDataForIndex(index_list); + const auto model_real_terminal_vector = m_model->modelRealTerminalDataForIndex(index_list); //Enable/disable group button - ui->m_group_terminals_pb->setEnabled(physical_terminal_vector.size() > 1 ? true : false); + ui->m_group_terminals_pb->setEnabled(model_physical_terminal_vector.size() > 1 ? true : false); //Enable/disable ungroup button - auto it_= std::find_if(physical_terminal_vector.constBegin(), physical_terminal_vector.constEnd(), [](const PhysicalTerminalData &data) + auto it_= std::find_if(model_physical_terminal_vector.constBegin(), model_physical_terminal_vector.constEnd(), [](const modelPhysicalTerminalData &data) { - if (data.real_terminals_vector.size() >= 2) { + if (data.real_data.size() >= 2) { return true; } else { return false; } }); - ui->m_ungroup_pb->setDisabled(it_ == physical_terminal_vector.constEnd()); + ui->m_ungroup_pb->setDisabled(it_ == model_physical_terminal_vector.constEnd()); //Enable/disable level spinbox bool enable_ = false; - for (const auto &physical : physical_terminal_vector) + for (const auto &physical : model_physical_terminal_vector) { - if (physical.real_terminals_vector.size() > 1) { + if (physical.real_data.size() > 1) { enable_ = true; break; } @@ -432,25 +432,30 @@ void TerminalStripEditor::selectionChanged() if (level_ >= 0) { //Select only terminals of corresponding level cell selection - QVector real_terminal_level_vector; - for (const auto &rtd : real_terminal_vector) { - if (rtd.level_ == level_) { - real_terminal_level_vector.append(rtd); + QVector model_real_terminal_level_vector; + for (const auto &mrtd : model_real_terminal_vector) { + if (mrtd.level_ == level_) { + model_real_terminal_level_vector.append(mrtd); } } - QVector uuid_v; - for (const auto &rtd : real_terminal_level_vector) { - uuid_v << rtd.real_terminal_uuid; + QVector model_rtd_vector; + for (const auto &mrtd : model_real_terminal_level_vector) { + model_rtd_vector << mrtd; } - if (m_current_strip) { - enable_bridge = m_current_strip->isBridgeable(uuid_v); + if (m_current_strip) + { + QVector> vector_; + for (const auto &mrtd : model_rtd_vector) { + vector_.append(mrtd.real_terminal); + } + enable_bridge = m_current_strip->isBridgeable(vector_); } - for (const auto &rtd : real_terminal_level_vector) + for (const auto &mrtd : model_real_terminal_level_vector) { - if (rtd.is_bridged && - rtd.level_ == level_) { + if (mrtd.bridged_ && + mrtd.level_ == level_) { enable_unbridge = true; break; } @@ -643,22 +648,21 @@ void TerminalStripEditor::on_m_dialog_button_box_clicked(QAbstractButton *button if (m_model) { - for (const auto &data_ : m_model->modifiedRealTerminalData()) + for (const auto &data_ : m_model->modifiedmodelRealTerminalData()) { - auto original_ = data_.first; - auto edited_ = data_.second; - auto element = original_.element_; - if (element) { + auto element = data_.element_; + if (element) + { auto current_data = element->elementData(); - current_data.setTerminalType(edited_.type_); - current_data.setTerminalFunction(edited_.function_); - current_data.setTerminalLED(edited_.led_); - current_data.m_informations.addValue(QStringLiteral("label"), edited_.label_); + current_data.setTerminalType(data_.type_); + current_data.setTerminalFunction(data_.function_); + current_data.setTerminalLED(data_.led_); + current_data.m_informations.addValue(QStringLiteral("label"), data_.label_); if (element->elementData() != current_data) m_project->undoStack()->push(new ChangeElementDataCommand(element, current_data)); - if (edited_.level_) - m_project->undoStack()->push(new ChangeTerminalLevel(m_current_strip, original_, edited_.level_)); + if (data_.level_ != m_current_strip->realTerminalDataFor(data_.real_terminal).level()) + m_project->undoStack()->push(new ChangeTerminalLevel(m_current_strip, data_.real_terminal, data_.level_)); } } } @@ -687,11 +691,16 @@ void TerminalStripEditor::on_m_group_terminals_pb_clicked() { if (m_model && m_current_strip && m_project) { - auto rtd_vector = m_model->realTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); - if (rtd_vector.size() >= 2) + auto mrtd_vector = m_model->modelRealTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); + if (mrtd_vector.size() >= 2) { - auto receiver_ = m_current_strip->physicalTerminalData(rtd_vector.takeFirst()); - m_project->undoStack()->push(new GroupTerminalsCommand(m_current_strip, receiver_, rtd_vector)); + auto receiver_ = m_current_strip->physicalTerminalData(mrtd_vector.takeFirst().real_terminal); + + QVector> vector_; + for (const auto & mrtd : mrtd_vector) { + vector_.append(mrtd.real_terminal); + } + m_project->undoStack()->push(new GroupTerminalsCommand(m_current_strip, receiver_, vector_)); } } } @@ -703,8 +712,13 @@ void TerminalStripEditor::on_m_ungroup_pb_clicked() { if (m_model && m_current_strip) { - const auto rtd_vector = m_model->realTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); - m_project->undoStack()->push(new UnGroupTerminalsCommand(m_current_strip, rtd_vector)); + const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); + + QVector> vector_; + for (const auto &mrtd : mrtd_vector) { + vector_.append(mrtd.real_terminal); + } + m_project->undoStack()->push(new UnGroupTerminalsCommand(m_current_strip, vector_)); } } @@ -826,16 +840,18 @@ void TerminalStripEditor::on_m_bridge_terminals_pb_clicked() else if(level_ == TerminalStripModel::Level3){level_ = 3;} const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); - const auto rtd_vector = m_model->realTerminalDataForIndex(index_list); - QVector uuid_vector; - for (const auto &rtd : rtd_vector) + const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); + + QVector > match_vector; + for (const auto &mrtd : mrtd_vector) { - if (rtd.level_ == level_) { - uuid_vector.append(rtd.real_terminal_uuid); + if (mrtd.level_ == level_) { + match_vector.append(mrtd.real_terminal); } } - if (m_current_strip->isBridgeable(uuid_vector)) { - m_project->undoStack()->push(new BridgeTerminalsCommand(m_current_strip, uuid_vector)); + + if (m_current_strip->isBridgeable(match_vector)) { + m_project->undoStack()->push(new BridgeTerminalsCommand(m_current_strip, match_vector)); } } } @@ -858,16 +874,16 @@ void TerminalStripEditor::on_m_unbridge_terminals_pb_clicked() else if(level_ == TerminalStripModel::Level3){level_ = 3;} const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); - const auto rtd_vector = m_model->realTerminalDataForIndex(index_list); - QVector uuid_vector; - for (const auto &rtd : rtd_vector) + const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); + QVector> match_vector; + for (const auto &mrtd : mrtd_vector) { - if (rtd.level_ == level_ - && rtd.is_bridged) { - uuid_vector.append(rtd.real_terminal_uuid); + if (mrtd.level_ == level_ + && mrtd.bridged_) { + match_vector.append(mrtd.real_terminal); } } - m_project->undoStack()->push(new UnBridgeTerminalsCommand(m_current_strip, uuid_vector)); + m_project->undoStack()->push(new UnBridgeTerminalsCommand(m_current_strip, match_vector)); } } } diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index a5dc20747..2d14ec95a 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -117,8 +117,8 @@ int TerminalStripModel::rowCount(const QModelIndex &parent) const } auto count = 0; - for (const auto &ptd : m_edited_terminal_data) { - count += ptd.real_terminals_vector.size(); + for (const auto &mptd : m_physical_data) { + count += mptd.real_data.size(); } return count; @@ -136,28 +136,28 @@ QVariant TerminalStripModel::data(const QModelIndex &index, int role) const return QVariant(); } - const auto rtd = dataAtRow(index.row()); + const auto mrtd = dataAtRow(index.row()); if (role == Qt::DisplayRole) { switch (index.column()) { case POS_CELL : return physicalDataAtIndex(index.row()).pos_; - case LEVEL_CELL : return rtd.level_; - case LABEL_CELL : return rtd.label_; - case XREF_CELL : return rtd.Xref_; - case CABLE_CELL : return rtd.cable_; - case CABLE_WIRE_CELL : return rtd.cable_wire_; - case TYPE_CELL : return ElementData::translatedTerminalType(rtd.type_); - case FUNCTION_CELL : return ElementData::translatedTerminalFunction(rtd.function_); - case CONDUCTOR_CELL : return rtd.conductor_; + case LEVEL_CELL : return mrtd.level_; + case LABEL_CELL : return mrtd.label_; + case XREF_CELL : return mrtd.Xref_; + case CABLE_CELL : return mrtd.cable_; + case CABLE_WIRE_CELL : return mrtd.cable_wire; + case TYPE_CELL : return ElementData::translatedTerminalType(mrtd.type_); + case FUNCTION_CELL : return ElementData::translatedTerminalFunction(mrtd.function_); + case CONDUCTOR_CELL : return mrtd.conductor_; default : return QVariant(); } } else if (role == Qt::EditRole) { switch (index.column()) { - case LABEL_CELL : return rtd.label_; + case LABEL_CELL : return mrtd.label_; default: return QVariant(); } @@ -165,12 +165,12 @@ QVariant TerminalStripModel::data(const QModelIndex &index, int role) const else if (role == Qt::CheckStateRole && index.column() == LED_CELL) { - return rtd.led_ ? Qt::Checked : Qt::Unchecked; + return mrtd.led_ ? Qt::Checked : Qt::Unchecked; } else if (role == Qt::BackgroundRole && index.column() <= CONDUCTOR_CELL ) { - if (m_modified_cell.contains(rtd.element_) && - m_modified_cell.value(rtd.element_).at(index.column())) + if (m_modified_cell.contains(mrtd.element_) && + m_modified_cell.value(mrtd.element_).at(index.column())) { return QBrush(Qt::yellow); } @@ -193,7 +193,7 @@ QVariant TerminalStripModel::data(const QModelIndex &index, int role) const bool TerminalStripModel::setData(const QModelIndex &index, const QVariant &value, int role) { - auto rtd = dataAtRow(index.row()); + auto mrtd = dataAtRow(index.row()); bool modified_ = false; int modified_cell = -1; auto column_ = index.column(); @@ -201,35 +201,35 @@ bool TerminalStripModel::setData(const QModelIndex &index, const QVariant &value if (column_ == LEVEL_CELL && role == Qt::EditRole) { - rtd.level_ = value.toInt(); + mrtd.level_ = value.toInt(); modified_ = true; modified_cell = LEVEL_CELL; } else if (column_ == LED_CELL) { - rtd.led_ = value.toBool(); + mrtd.led_ = value.toBool(); modified_ = true; modified_cell = LED_CELL; } else if (column_ == TYPE_CELL && role == Qt::EditRole) { - rtd.type_ = value.value(); + mrtd.type_ = value.value(); modified_ = true; modified_cell = TYPE_CELL; } else if (column_ == FUNCTION_CELL && role == Qt::EditRole) { - rtd.function_ = value.value(); + mrtd.function_ = value.value(); modified_ = true; modified_cell = FUNCTION_CELL; } else if (column_ == LABEL_CELL && role == Qt::EditRole && - rtd.label_ != value.toString()) + mrtd.label_ != value.toString()) { - rtd.label_ = value.toString(); + mrtd.label_ = value.toString(); modified_ = true; modified_cell = LABEL_CELL; } @@ -237,19 +237,19 @@ bool TerminalStripModel::setData(const QModelIndex &index, const QVariant &value //Set the modification to the terminal data if (modified_) { - replaceDataAtRow(rtd, index.row()); + replaceDataAtRow(mrtd, index.row()); - if (rtd.element_) + if (mrtd.element_) { QVector vector_; - if (m_modified_cell.contains(rtd.element_)) { - vector_ = m_modified_cell.value(rtd.element_); + if (m_modified_cell.contains(mrtd.element_)) { + vector_ = m_modified_cell.value(mrtd.element_); } else { vector_ = UNMODIFIED_CELL_VECTOR; } vector_.replace(modified_cell, true); - m_modified_cell.insert(rtd.element_, vector_); + m_modified_cell.insert(mrtd.element_, vector_); } emit dataChanged(index, index); return true; @@ -305,22 +305,21 @@ Qt::ItemFlags TerminalStripModel::flags(const QModelIndex &index) const * @return a vector of QPair of modified terminal. * the first value of the QPair is the original data, the second value is the edited data */ -QVector> TerminalStripModel::modifiedRealTerminalData() const +QVector TerminalStripModel::modifiedmodelRealTerminalData() const { - QVector> returned_vector; + QVector returned_vector; const auto modified_real_terminal = m_modified_cell.keys(); - for (auto i = 0 ; i> TerminalStripModel::modifiedR * @return A vector of PhysicalTerminalData represented by index_list. * If sereval index point to the same terminal the vector have only one PhysicalTerminalData */ -QVector TerminalStripModel::physicalTerminalDataForIndex(QModelIndexList index_list) const +QVector TerminalStripModel::modelPhysicalTerminalDataForIndex(QModelIndexList index_list) const { - QVector vector_; + QVector vector_; if (index_list.isEmpty()) { return vector_; } @@ -350,7 +349,8 @@ QVector TerminalStripModel::physicalTerminalDataForIndex(Q } } - for (auto i : set_) { + for (const auto i : set_) + { const auto phy = physicalDataAtIndex(i); if (!vector_.contains(phy)) { vector_.append(phy); @@ -365,9 +365,9 @@ QVector TerminalStripModel::physicalTerminalDataForIndex(Q * @param index_list * @return */ -QVector TerminalStripModel::realTerminalDataForIndex(QModelIndexList index_list) const +QVector TerminalStripModel::modelRealTerminalDataForIndex(QModelIndexList index_list) const { - QVector vector_; + QVector vector_; if (index_list.isEmpty()) { return vector_; } @@ -393,14 +393,14 @@ QVector TerminalStripModel::realTerminalDataForIndex(QModelInd /** * @brief TerminalStripModel::realTerminalDataForIndex * @param index - * @return RealTerminalData at index @a index or null RealTerminalData if invalid + * @return modelRealTerminalData at index @a index or null modelRealTerminalData if invalid */ -RealTerminalData TerminalStripModel::realTerminalDataForIndex(const QModelIndex &index) const +modelRealTerminalData TerminalStripModel::modelRealTerminalDataForIndex(const QModelIndex &index) const { if (index.isValid()) { return realDataAtIndex(index.row()); } else { - return RealTerminalData(); + return modelRealTerminalData(); } } @@ -479,25 +479,38 @@ void TerminalStripModel::buildBridgePixmap(const QSize &pixmap_size) void TerminalStripModel::fillPhysicalTerminalData() { //Get all physical terminal - if (m_terminal_strip) { - for (auto i=0 ; i < m_terminal_strip->physicalTerminalCount() ; ++i) { - m_original_terminal_data.append(m_terminal_strip->physicalTerminalData(i)); + if (m_terminal_strip) + { + for (const auto &ptd : m_terminal_strip->physicalTerminalData()) + { + modelPhysicalTerminalData mptd; + mptd.pos_ = ptd.pos_; + mptd.uuid_ = ptd.uuid_; + + for (const auto &rtd : ptd.real_terminals_vector) + { + if (!rtd.isNull()) + { + mptd.real_data.append(modelRealData(rtd)); + } + } + + m_physical_data.append(mptd); } - m_edited_terminal_data = m_original_terminal_data; } } -RealTerminalData TerminalStripModel::dataAtRow(int row) const +modelRealTerminalData TerminalStripModel::dataAtRow(int row) const { if (row > rowCount(QModelIndex())) { - return RealTerminalData(); + return modelRealTerminalData(); } else { auto current_row = 0; - for (const auto &physical_data : m_edited_terminal_data) + for (const auto &physical_data : qAsConst(m_physical_data)) { - for (const auto &real_data : physical_data.real_terminals_vector) + for (const auto &real_data : physical_data.real_data) { if (current_row == row) { return real_data; @@ -508,7 +521,7 @@ RealTerminalData TerminalStripModel::dataAtRow(int row) const } } - return RealTerminalData(); + return modelRealTerminalData(); } /** @@ -517,7 +530,7 @@ RealTerminalData TerminalStripModel::dataAtRow(int row) const * @param data * @param row */ -void TerminalStripModel::replaceDataAtRow(RealTerminalData data, int row) +void TerminalStripModel::replaceDataAtRow(modelRealTerminalData data, int row) { if (row > rowCount(QModelIndex())) { return; @@ -527,15 +540,15 @@ void TerminalStripModel::replaceDataAtRow(RealTerminalData data, int row) auto current_row = 0; auto current_physical = 0; - for (const auto &physical_data : qAsConst(m_edited_terminal_data)) + for (const auto &physical_data : qAsConst(m_physical_data)) { auto current_real = 0; - for (int i=0 ; i= index) { @@ -578,9 +591,9 @@ PhysicalTerminalData TerminalStripModel::physicalDataAtIndex(int index) const } if (match_) { - return m_edited_terminal_data.at(current_phy); + return m_physical_data.at(current_phy); } else { - return PhysicalTerminalData(); + return modelPhysicalTerminalData(); } } @@ -589,17 +602,17 @@ PhysicalTerminalData TerminalStripModel::physicalDataAtIndex(int index) const * @param index * @return the realTerminalData at index \p index. */ -RealTerminalData TerminalStripModel::realDataAtIndex(int index) const +modelRealTerminalData TerminalStripModel::realDataAtIndex(int index) const { - if (m_edited_terminal_data.isEmpty()) { - return RealTerminalData(); + if (m_physical_data.isEmpty()) { + return modelRealTerminalData(); } int current_checked_index = -1; - for (const auto & ptd_ : qAsConst(m_edited_terminal_data)) + for (const auto & ptd_ : qAsConst(m_physical_data)) { - for (const auto & rtd_ : qAsConst(ptd_.real_terminals_vector)) { + for (const auto & rtd_ : qAsConst(ptd_.real_data)) { ++current_checked_index; if (current_checked_index == index) { return rtd_; @@ -607,7 +620,7 @@ RealTerminalData TerminalStripModel::realDataAtIndex(int index) const } } - return RealTerminalData(); + return modelRealTerminalData(); } QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const @@ -621,28 +634,31 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const return QPixmap(); } - auto rtd = realTerminalDataForIndex(index); + auto mrtd = modelRealTerminalDataForIndex(index); //Terminal level correspond to the column level of index - if (level_column == rtd.level_) + if (level_column == mrtd.level_) { - if (rtd.is_bridged) + if (mrtd.bridged_) { - auto bridge_uuid = rtd.bridge_uuid; - auto previous_bridge_uuid = m_terminal_strip->previousTerminalInLevel(rtd.real_terminal_uuid).bridge_uuid; - auto next_bridge_uuid = m_terminal_strip->nextTerminalInLevel(rtd.real_terminal_uuid).bridge_uuid; + auto bridge_ = m_terminal_strip->bridgeFor(mrtd.real_terminal); + if (bridge_) + { + auto previous_bridge = m_terminal_strip->previousTerminalInLevel(mrtd.real_terminal).bridge(); + auto next_bridge = m_terminal_strip->nextTerminalInLevel(mrtd.real_terminal).bridge(); - auto color_ = m_terminal_strip->bridgeFor(rtd.real_terminal_uuid)->color_; - auto pixmap_ = m_bridges_pixmaps.value(color_); + auto color_ = bridge_->color_; + auto pixmap_ = m_bridges_pixmaps.value(color_); //Current real terminal between two bridged terminal - if ((bridge_uuid == previous_bridge_uuid) && - (bridge_uuid == next_bridge_uuid)) { - return pixmap_.middle_; - } else if (bridge_uuid == previous_bridge_uuid) { - return pixmap_.bottom_; - } else if (bridge_uuid == next_bridge_uuid) { - return pixmap_.top_; + if ((bridge_ == previous_bridge) && + (bridge_ == next_bridge)) { + return pixmap_.middle_; + } else if (bridge_ == previous_bridge) { + return pixmap_.bottom_; + } else if (bridge_ == next_bridge) { + return pixmap_.top_; + } } } } @@ -650,26 +666,25 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const //Check if we need to draw a none bridge pixmap //Check previous - auto physical_data = m_terminal_strip->physicalTerminalData(rtd); - auto current_real_uuid = rtd.real_terminal_uuid; + auto physical_data = m_terminal_strip->physicalTerminalData(mrtd.real_terminal); + auto current_real_terminal = mrtd; auto current_phy_uuid = physical_data.uuid_; bool already_jumped_to_previous = false; - RealTerminalData previous_data; + modelRealTerminalData previous_data; do { - auto previous_rtd = m_terminal_strip->previousRealTerminal(current_real_uuid); - current_real_uuid = previous_rtd.real_terminal_uuid; + current_real_terminal = modelRealData(m_terminal_strip->previousRealTerminal(current_real_terminal.real_terminal)); - if (previous_rtd.level_ == -1) { + if (current_real_terminal.level_ == -1) { break; } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminalData(previous_rtd).uuid_) + if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_) { - if (previous_rtd.is_bridged && - previous_rtd.level_ == level_column) { - previous_data = previous_rtd; + if (current_real_terminal.bridged_ && + current_real_terminal.level_ == level_column) { + previous_data = current_real_terminal; break; } } @@ -677,34 +692,34 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_previous = true; - current_phy_uuid = m_terminal_strip->physicalTerminalData(previous_rtd).uuid_; - if (previous_rtd.is_bridged && - previous_rtd.level_ == level_column) { - previous_data = previous_rtd; + current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_; + if (current_real_terminal.bridged_ && + current_real_terminal.level_ == level_column) { + previous_data = current_real_terminal; break; } } } while(true); //Check next - current_real_uuid = rtd.real_terminal_uuid; + current_real_terminal = mrtd; current_phy_uuid = physical_data.uuid_; bool already_jumped_to_next = false; - RealTerminalData next_data; - do { - auto next_rtd = m_terminal_strip->nextRealTerminal(current_real_uuid); - current_real_uuid = next_rtd.real_terminal_uuid; + modelRealTerminalData next_data; - if (next_rtd.level_ == -1) { + do { + current_real_terminal = modelRealData(m_terminal_strip->nextRealTerminal(current_real_terminal.real_terminal)); + + if (current_real_terminal.level_ == -1) { break; } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminalData(next_rtd).uuid_) + if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_) { - if (next_rtd.is_bridged && - next_rtd.level_ == level_column) { - next_data = next_rtd; + if (current_real_terminal.bridged_ && + current_real_terminal.level_ == level_column) { + next_data = current_real_terminal; break; } } @@ -712,27 +727,50 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_next = true; - current_phy_uuid = m_terminal_strip->physicalTerminalData(next_rtd).uuid_; - if (next_rtd.is_bridged && - next_rtd.level_ == level_column) { - next_data = next_rtd; + current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_; + if (current_real_terminal.bridged_ && + current_real_terminal.level_ == level_column) { + next_data = current_real_terminal; break; } } } while(true); - if (previous_data.bridge_uuid == next_data.bridge_uuid) { - auto bridge_ = m_terminal_strip->bridgeFor(previous_data.real_terminal_uuid); - if (bridge_) { - return m_bridges_pixmaps.value(bridge_->color_).none_; + auto previous_bridge = m_terminal_strip->bridgeFor(previous_data.real_terminal); + if (previous_bridge == m_terminal_strip->bridgeFor(next_data.real_terminal)) + { + if (previous_bridge) { + return m_bridges_pixmaps.value(previous_bridge->color_).none_; } } return QPixmap(); } +modelRealTerminalData TerminalStripModel::modelRealData(const RealTerminalData &data) +{ + modelRealTerminalData mrtd; + if (!data.isNull()) + { + mrtd.level_ = data.level(); + mrtd.label_ = data.label(); + mrtd.Xref_ = data.Xref(); + mrtd.cable_ = data.cable(); + mrtd.cable_wire = data.cableWire(); + mrtd.conductor_ = data.conductor(); + mrtd.led_ = data.isLed(); + mrtd.type_ = data.type(); + mrtd.function_ = data.function(); + mrtd.element_ = data.element(); + mrtd.real_terminal = data.realTerminal(); + mrtd.bridged_ = data.isBridged(); + } + + return mrtd; +} + /*********************************************************** - * Alittle delegate for add a combobox to edit type + * A little delegate for add a combobox to edit type * and a spinbox to edit the level of a terminal **********************************************************/ diff --git a/sources/TerminalStrip/ui/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 7c8ea6d70..245727a07 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -26,14 +26,54 @@ #include #include "../terminalstrip.h" +#include "../../qetgraphicsitem/element.h" //Code to use QColor as key for QHash inline uint qHash(const QColor &key, uint seed) { return qHash(key.name(), seed); } +//needed to use QPointer as key of QHash +inline uint qHash(const QPointer &key, uint seed) { + if (key) + return qHash(key->uuid(), seed); + else + return qHash(nullptr, seed); +} + class TerminalStrip; + +struct modelRealTerminalData +{ + int level_ = -1; + QString label_; + QString Xref_; + QString cable_; + QString cable_wire; + QString conductor_; + bool led_ = false; + bool bridged_ = false; + + ElementData::TerminalType type_ = ElementData::TerminalType::TTGeneric; + ElementData::TerminalFunction function_ = ElementData::TerminalFunction::TFGeneric; + QPointer element_; + + QWeakPointer real_terminal; + +}; + +struct modelPhysicalTerminalData +{ + QVector real_data; + int pos_ = -1; + QUuid uuid_; +}; + +inline bool operator == (const modelPhysicalTerminalData &data_1, const modelPhysicalTerminalData &data_2) { + return data_1.uuid_ == data_2.uuid_; +} + class TerminalStripModel : public QAbstractTableModel { public: @@ -68,27 +108,28 @@ class TerminalStripModel : public QAbstractTableModel virtual bool setData (const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; virtual Qt::ItemFlags flags (const QModelIndex &index) const override; - QVector> modifiedRealTerminalData() const; + QVector modifiedmodelRealTerminalData() const; - QVector physicalTerminalDataForIndex(QModelIndexList index_list) const; - QVector realTerminalDataForIndex(QModelIndexList index_list) const; - RealTerminalData realTerminalDataForIndex(const QModelIndex &index) const; + QVector modelPhysicalTerminalDataForIndex(QModelIndexList index_list) const; + QVector modelRealTerminalDataForIndex(QModelIndexList index_list) const; + modelRealTerminalData modelRealTerminalDataForIndex(const QModelIndex &index) const; void buildBridgePixmap(const QSize &pixmap_size); private: void fillPhysicalTerminalData(); - RealTerminalData dataAtRow(int row) const; - void replaceDataAtRow(RealTerminalData data, int row); - PhysicalTerminalData physicalDataAtIndex(int index) const; - RealTerminalData realDataAtIndex(int index) const; + modelRealTerminalData dataAtRow(int row) const; + void replaceDataAtRow(modelRealTerminalData data, int row); + modelPhysicalTerminalData physicalDataAtIndex(int index) const; + modelRealTerminalData realDataAtIndex(int index) const; QPixmap bridgePixmapFor(const QModelIndex &index) const; + static modelRealTerminalData modelRealData(const RealTerminalData &data); + private: QPointer m_terminal_strip; - QVector m_edited_terminal_data, m_original_terminal_data; - QHash> m_modified_cell; - + QHash, QVector> m_modified_cell; + QVector m_physical_data; struct BridgePixmap { QPixmap top_, From 57e80e7b5e29022948aff439e96fe9b8f3903230 Mon Sep 17 00:00:00 2001 From: joshua Date: Sun, 19 Dec 2021 20:03:26 +0100 Subject: [PATCH 06/14] Fix copy constructo warning --- sources/TerminalStrip/terminalstripdata.cpp | 4 ++++ sources/TerminalStrip/terminalstripdata.h | 1 + 2 files changed, 5 insertions(+) diff --git a/sources/TerminalStrip/terminalstripdata.cpp b/sources/TerminalStrip/terminalstripdata.cpp index 1a44f2065..bd86b1a14 100644 --- a/sources/TerminalStrip/terminalstripdata.cpp +++ b/sources/TerminalStrip/terminalstripdata.cpp @@ -24,6 +24,10 @@ TerminalStripData::TerminalStripData() } +TerminalStripData::TerminalStripData(const TerminalStripData &other) { + *this = other; +} + QDomElement TerminalStripData::toXml(QDomDocument &xml_document) const { auto root_elmt = xml_document.createElement(this->xmlTagName()); diff --git a/sources/TerminalStrip/terminalstripdata.h b/sources/TerminalStrip/terminalstripdata.h index 2dfdc6216..2b59bff3f 100644 --- a/sources/TerminalStrip/terminalstripdata.h +++ b/sources/TerminalStrip/terminalstripdata.h @@ -29,6 +29,7 @@ class TerminalStripData : public PropertiesInterface public: TerminalStripData(); + TerminalStripData(const TerminalStripData &other); void toSettings(QSettings &/*settings*/, const QString = QString()) const override {} void fromSettings (const QSettings &/*settings*/, const QString = QString()) override {} From e2454faa36b100f34d0a10b2ee313e7140ed8d6d Mon Sep 17 00:00:00 2001 From: joshua Date: Sun, 19 Dec 2021 21:05:48 +0100 Subject: [PATCH 07/14] Improve code readability --- .../UndoCommand/groupterminalscommand.cpp | 2 +- .../UndoCommand/sortterminalstripcommand.cpp | 8 +- .../UndoCommand/sortterminalstripcommand.h | 2 +- sources/TerminalStrip/terminalstrip.cpp | 331 +++++++++--------- sources/TerminalStrip/terminalstrip.h | 43 ++- .../TerminalStrip/ui/terminalstripeditor.cpp | 11 +- .../TerminalStrip/ui/terminalstripmodel.cpp | 18 +- 7 files changed, 215 insertions(+), 200 deletions(-) diff --git a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp index a2857de50..1681b4073 100644 --- a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp @@ -84,7 +84,7 @@ void UnGroupTerminalsCommand::setUp(const QVector> &t auto ptd_ = m_terminal_strip->physicalTerminalData(rt_); //Physical have only one real terminal, no need to ungroup it - if (ptd_.real_terminals_vector.size() <= 1) { + if (ptd_.realTerminalCount() <= 1) { continue; } diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp index 51287faca..9157445ca 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp @@ -52,9 +52,9 @@ void SortTerminalStripCommand::sort() int int1 =-1; int int2 =-1; - if (arg1.real_terminals_vector.count()) + if (arg1.realTerminalCount()) { - str1 = arg1.real_terminals_vector.constLast().label(); + str1 = arg1.realTerminalDatas().constLast().label(); auto match = rx.match(str1); if (match.hasMatch()) { @@ -62,9 +62,9 @@ void SortTerminalStripCommand::sort() } } - if (arg2.real_terminals_vector.count()) + if (arg2.realTerminalCount()) { - str2 = arg2.real_terminals_vector.constLast().label(); + str2 = arg2.realTerminalDatas().constLast().label(); auto match = rx.match(str2); if (match.hasMatch()) { diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h index fe14c2364..6531563d2 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h @@ -23,7 +23,7 @@ #include class TerminalStrip; -struct PhysicalTerminalData; +class PhysicalTerminalData; /** * @brief The SortTerminalStripCommand class diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index f5e394c89..6dc4c2a7a 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -431,11 +431,8 @@ TerminalStripData TerminalStrip::data() const { * of the terminal strip unchanged * @param data */ -void TerminalStrip::setData(const TerminalStripData &data) -{ - auto uuid_ = m_data.m_uuid; +void TerminalStrip::setData(const TerminalStripData &data) { m_data = data; - m_data.m_uuid = uuid_; } /** @@ -458,11 +455,11 @@ bool TerminalStrip::addTerminal(Element *terminal) m_terminal_elements_vector.append(terminal); //Create the real terminal - shared_real_terminal real_terminal(new RealTerminal(this, terminal)); + const 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( + const shared_physical_terminal physical_terminal( new PhysicalTerminal(this, QVector{real_terminal})); @@ -511,6 +508,16 @@ bool TerminalStrip::removeTerminal(Element *terminal) return false; } +/** + * @brief TerminalStrip::pos + * @param terminal + * @return the position of the physical terminal + * or -1 if not found + */ +int TerminalStrip::pos(const QWeakPointer &terminal) const { + return m_physical_terminals.indexOf(terminal.toStrongRef()); +} + /** * @brief TerminalStrip::physicalTerminalCount * @return the number of physical terminal. @@ -528,20 +535,11 @@ int TerminalStrip::physicalTerminalCount() const { */ PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) const { - PhysicalTerminalData ptd; - - if (index < m_physical_terminals.size()) - { - auto physical_terminal = m_physical_terminals.at(index); - ptd.pos_ = index; - for (auto real_terminal : physical_terminal->terminals()) { - auto rtd = RealTerminalData(real_terminal); - ptd.real_terminals_vector.append(rtd); - } - ptd.uuid_ = physical_terminal->uuid(); + if (index < m_physical_terminals.size()) { + return PhysicalTerminalData(this, m_physical_terminals.at(index)); + } else { + return PhysicalTerminalData(); } - - return ptd; } /** @@ -550,28 +548,19 @@ PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) const * @return the parent PhysicalTerminalData of \p real_terminal. * the PhysicalTerminalData can be invalid if \p real_terminal don't belong to this strip */ -PhysicalTerminalData TerminalStrip::physicalTerminalData (QWeakPointer real_terminal) const +PhysicalTerminalData TerminalStrip::physicalTerminalData (const QWeakPointer &real_terminal) const { - PhysicalTerminalData ptd_; - const auto real_t = real_terminal.toStrongRef(); if (real_t.isNull()) { - return ptd_; + return PhysicalTerminalData(); } const auto phy_t = physicalTerminal(real_t); - if (phy_t.isNull()) { - return ptd_; + if (phy_t) { + return PhysicalTerminalData(this, phy_t); } - ptd_.pos_ = m_physical_terminals.indexOf(phy_t); - for (auto real_terminal : phy_t->terminals()) { - auto rtd = RealTerminalData(real_terminal); - ptd_.real_terminals_vector.append(rtd); - } - ptd_.uuid_ = phy_t->uuid(); - - return ptd_; + return PhysicalTerminalData(); } /** @@ -612,7 +601,7 @@ bool TerminalStrip::setOrderTo(const QVector &sorted_vecto QVector> new_order; for (const auto &ptd : sorted_vector) { - const auto physical_t = physicalTerminalForUuid(ptd.uuid_); + const auto physical_t = ptd.physicalTerminal().toStrongRef(); if (physical_t.isNull()) { continue; } @@ -643,7 +632,7 @@ bool TerminalStrip::setOrderTo(const QVector &sorted_vecto */ bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector> &added_terminals) { - const auto receiver_ = physicalTerminalForUuid(receiver_terminal.uuid_); + const auto receiver_ = receiver_terminal.physicalTerminal().toStrongRef(); if (receiver_.isNull()) { qDebug() << "TerminalStrip::groupTerminal : Arguments terminals don't belong to this strip. Operation aborted."; return false; @@ -696,7 +685,7 @@ void TerminalStrip::unGroupTerminals(const QVector> & if (physical_terminal->terminals().size() > 1) //Check if physical have more than one real terminal { physical_terminal->removeTerminal(real_terminal); - shared_physical_terminal new_physical_terminal ( + const shared_physical_terminal new_physical_terminal ( new PhysicalTerminal(this, QVector{real_terminal})); m_physical_terminals.append(new_physical_terminal); @@ -719,7 +708,7 @@ void TerminalStrip::unGroupTerminals(const QVector> & */ bool TerminalStrip::setLevel(const QWeakPointer &real_terminal, int level) { - auto real_t = real_terminal.toStrongRef(); + const auto real_t = real_terminal.toStrongRef(); if (real_t) { auto physical_terminal = physicalTerminal(real_t); @@ -739,26 +728,29 @@ bool TerminalStrip::setLevel(const QWeakPointer &real_terminal, in /** * @brief TerminalStrip::isBridgeable - * Check if all realTerminal in @a real_terminals_data are bridgeable together. + * Check if all realTerminal in @a real_terminals are bridgeable together. * To be bridgeable, each real terminal must belong to this terminal strip * be at the same level, be consecutive and not belong to the same physicalTerminal * and at least one terminal must be not bridged - * @param real_terminals_data : a vector of RealTerminalData + * @param real_terminals : a vector of realterminal * @return */ -bool TerminalStrip::isBridgeable(const QVector &real_terminals_data) const +bool TerminalStrip::isBridgeable(const QVector> &real_terminals) const { - if (real_terminals_data.size() < 2) { + if (real_terminals.size() < 2) { return false; } // Check if first terminal belong to this strip - auto first_real_terminal = real_terminals_data.first().m_real_terminal.toStrongRef(); + const auto first_real_terminal = real_terminals.first().toStrongRef(); if (!first_real_terminal) { return false; } + // Get the level of the first terminal - int level_ = real_terminals_data.first().level(); + const auto rtd_ = realTerminalDataFor(real_terminals.first()); + const int level_ = rtd_.level(); + // Get the physical terminal and pos auto first_physical_terminal = physicalTerminal(first_real_terminal); QVector physical_vector{first_physical_terminal}; @@ -768,17 +760,17 @@ bool TerminalStrip::isBridgeable(const QVector &real_terminals //bool to know at the end of this function if at least one terminal is not bridged bool no_bridged = bridge_ ? false : true; - // Check for each terminals - for (int i=1 ; i &real_terminals } // Not in another bridge of a previous checked real terminal - auto checked_bridge = isBridged(real_terminal); + const auto checked_bridge = isBridged(real_terminal); if (checked_bridge) { if (bridge_.isNull()) { @@ -807,8 +799,8 @@ bool TerminalStrip::isBridgeable(const QVector &real_terminals } // Check if concecutive - auto count_ = pos_vector.size(); - auto min_max = std::minmax_element(pos_vector.constBegin(), pos_vector.constEnd()); + const auto count_ = pos_vector.size(); + const auto min_max = std::minmax_element(pos_vector.constBegin(), pos_vector.constEnd()); if ((*min_max.second - *min_max.first) + 1 != count_) { return false; } @@ -816,17 +808,6 @@ bool TerminalStrip::isBridgeable(const QVector &real_terminals return no_bridged; } -bool TerminalStrip::isBridgeable(const QVector> &real_terminals) const -{ - QVector data; - - for (const auto &wrt : real_terminals) - { - data.append(RealTerminalData(wrt.toStrongRef())); - } - return isBridgeable(data); -} - /** * @brief TerminalStrip::setBridge * Set a bridge betwen all real terminal of @a real_terminals @@ -842,7 +823,7 @@ bool TerminalStrip::setBridge(const QVector> &real_te for (const auto &real_terminal : real_terminals) { - auto real_t = real_terminal.toStrongRef(); + const auto real_t = real_terminal.toStrongRef(); if (real_t) { real_terminals_vector.append(real_t); } @@ -871,7 +852,7 @@ bool TerminalStrip::setBridge(const QVector> &real_te * @param real_terminals_data * @return true if all RealTerminal was successfully bridged */ -bool TerminalStrip::setBridge(QSharedPointer bridge, const QVector> &real_terminals) +bool TerminalStrip::setBridge(const QSharedPointer &bridge, const QVector> &real_terminals) { if (bridge) { @@ -882,7 +863,7 @@ bool TerminalStrip::setBridge(QSharedPointer bridge, const bool b_ = false; for (const auto & rt_ : real_terminals) { - auto real_t = rt_.toStrongRef(); + const auto real_t = rt_.toStrongRef(); if (real_t && !bridge->real_terminals.contains(real_t)) { @@ -918,7 +899,7 @@ void TerminalStrip::unBridge(const QVector> &real_ter emit bridgeChanged(); } -QSharedPointer TerminalStrip::bridgeFor(QWeakPointer real_terminal) const +QSharedPointer TerminalStrip::bridgeFor(const QWeakPointer &real_terminal) const { return this->isBridged(real_terminal.toStrongRef()); } @@ -931,21 +912,17 @@ QSharedPointer TerminalStrip::bridgeFor(QWeakPointer &real_terminal) const { - auto real_t = real_terminal.toStrongRef(); - if (real_t) + const auto real_t = real_terminal.toStrongRef(); + const auto phy_t = physicalTerminal(real_t); + if (real_t && phy_t) { - auto phy_t = physicalTerminal(real_t); - if (phy_t) + const auto level_ = phy_t->levelOf(real_t); + const auto index = m_physical_terminals.indexOf(phy_t); + if (index >= 1) { - auto level_ = phy_t->levelOf(real_t); - auto index = m_physical_terminals.indexOf(phy_t); - if (index >= 1) - { - auto t_vector = m_physical_terminals.at(index-1)->terminals(); - if (t_vector.size() > level_) - { - return RealTerminalData(t_vector.at(level_)); - } + const auto t_vector = m_physical_terminals.at(index-1)->terminals(); + if (t_vector.size() > level_) { + return RealTerminalData(t_vector.at(level_)); } } } @@ -961,21 +938,17 @@ RealTerminalData TerminalStrip::previousTerminalInLevel(const QWeakPointer &real_terminal) const { - auto real_t = real_terminal.toStrongRef(); - if (real_t) + const auto real_t = real_terminal.toStrongRef(); + const auto phy_t = physicalTerminal(real_t); + if (real_t && phy_t) { - auto phy_t = physicalTerminal(real_t); - if (phy_t) + const auto level_ = phy_t->levelOf(real_t); + const auto index = m_physical_terminals.indexOf(phy_t); + if (index < m_physical_terminals.size()-1) { - auto level_ = phy_t->levelOf(real_t); - auto index = m_physical_terminals.indexOf(phy_t); - if (index < m_physical_terminals.size()-1) - { - auto t_vector = m_physical_terminals.at(index+1)->terminals(); - if (t_vector.size() > level_) - { - return RealTerminalData(t_vector.at(level_)); - } + const auto t_vector = m_physical_terminals.at(index+1)->terminals(); + if (t_vector.size() > level_) { + return RealTerminalData(t_vector.at(level_)); } } } @@ -985,8 +958,8 @@ RealTerminalData TerminalStrip::nextTerminalInLevel(const QWeakPointer &real_terminal) const { - auto real = real_terminal.toStrongRef(); - auto index = m_real_terminals.indexOf(real); + const auto real = real_terminal.toStrongRef(); + const auto index = m_real_terminals.indexOf(real); if (index) { return RealTerminalData(m_real_terminals.at(index-1)); } @@ -995,8 +968,8 @@ RealTerminalData TerminalStrip::previousRealTerminal(const QWeakPointer &real_terminal) const { - auto real = real_terminal.toStrongRef(); - auto index = m_real_terminals.indexOf(real); + const auto real = real_terminal.toStrongRef(); + const auto index = m_real_terminals.indexOf(real); if (index != m_real_terminals.size()-1) { return RealTerminalData(m_real_terminals.at(index+1)); } @@ -1005,7 +978,7 @@ RealTerminalData TerminalStrip::nextRealTerminal(const QWeakPointer &real_terminal) const { - auto rt = real_terminal.toStrongRef(); + const auto rt = real_terminal.toStrongRef(); if (rt && m_real_terminals.contains(rt)) return RealTerminalData(rt); else @@ -1063,8 +1036,8 @@ bool TerminalStrip::fromXml(QDomElement &xml_element) if (!xml_layout.isNull()) { //Get all free elements terminal of the project - ElementProvider ep(m_project); - auto free_terminals = ep.freeTerminal(); + const ElementProvider ep(m_project); + const auto free_terminals = ep.freeTerminal(); //Read each physical terminal for(auto &xml_physical : QETXML::findInDomElement(xml_layout, PhysicalTerminal::xmlTagName())) @@ -1084,7 +1057,7 @@ bool TerminalStrip::fromXml(QDomElement &xml_element) real_t_vector.append(real_t); } - shared_physical_terminal phy_t(new PhysicalTerminal(this, real_t_vector)); + const shared_physical_terminal phy_t(new PhysicalTerminal(this, real_t_vector)); m_physical_terminals.append(phy_t); m_real_terminals.append(real_t_vector); } @@ -1102,15 +1075,13 @@ bool TerminalStrip::fromXml(QDomElement &xml_element) */ QSharedPointer TerminalStrip::realTerminal(Element *terminal) { - shared_real_terminal rt; - for (auto &real : qAsConst(m_real_terminals)) { if (real->element() == terminal) { return real; } } - return rt; + return shared_real_terminal(); } /** @@ -1123,10 +1094,12 @@ QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer< { shared_physical_terminal pt; - for (auto &physical : qAsConst(m_physical_terminals)) - { - if (physical->terminals().contains(terminal)) - { + if (terminal.isNull()) { + return pt; + } + + for (auto &physical : qAsConst(m_physical_terminals)) { + if (physical->terminals().contains(terminal)) { pt = physical; break; } @@ -1135,47 +1108,6 @@ QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer< return pt; } -/** - * @brief TerminalStrip::physicalTerminalForUuid - * Return the PhysicalTerminal with uuid \p uuid or a null - * PhysicalTerminal if uuid don't match - * @param uuid - * @return - */ -QSharedPointer TerminalStrip::physicalTerminalForUuid(const QUuid &uuid) const -{ - shared_physical_terminal return_pt; - - for (const auto &pt_ : qAsConst(m_physical_terminals)) { - if (pt_->uuid() == uuid) { - return_pt = pt_; - break; - } - } - - return return_pt; -} - -/** - * @brief TerminalStrip::realTerminalForUuid - * @param uuid - * @return the RealTerminal with uuid \p uuid or a null - * RealTerminal if uuid don't match - */ -QSharedPointer TerminalStrip::realTerminalForUuid(const QUuid &uuid) const -{ - shared_real_terminal return_rt; - - for (const auto &rt_ : qAsConst(m_real_terminals)) { - if (rt_->uuid() == uuid) { - return_rt = rt_; - break; - } - } - - return return_rt; -} - /** * @brief TerminalStrip::isBridged * Check if @a real_terminal is bridged @@ -1209,7 +1141,7 @@ QSharedPointer TerminalStrip::bridgeFor(const QVector TerminalStrip::bridgeFor(const QVectorterminals()) - m_real_terminals.append(real); + for (const auto &phy : qAsConst(m_physical_terminals)) { + m_real_terminals.append(phy->terminals()); } } @@ -1249,10 +1180,13 @@ void TerminalStrip::rebuildRealVector() * @brief RealTerminalData::RealTerminalData * @param real_terminal */ -RealTerminalData::RealTerminalData(QSharedPointer real_terminal) -{ - m_real_terminal = real_terminal.toWeakRef(); -} +RealTerminalData::RealTerminalData(QSharedPointer real_terminal) : + m_real_terminal(real_terminal.toWeakRef()) +{} + +RealTerminalData::RealTerminalData(QWeakPointer real_terminal) : + m_real_terminal(real_terminal) +{} bool RealTerminalData::isNull() const { @@ -1261,7 +1195,7 @@ bool RealTerminalData::isNull() const int RealTerminalData::level() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { auto strip = shared_->parentStrip(); if (strip) { @@ -1277,7 +1211,7 @@ int RealTerminalData::level() const QString RealTerminalData::label() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { return shared_->label(); } else { @@ -1287,7 +1221,7 @@ QString RealTerminalData::label() const QString RealTerminalData::Xref() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_ && shared_->isElement()) { return autonum::AssignVariables::genericXref(shared_->element()); } else { @@ -1309,7 +1243,7 @@ QString RealTerminalData::conductor() const { ElementData::TerminalType RealTerminalData::type() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { return shared_->type(); } @@ -1319,7 +1253,7 @@ ElementData::TerminalType RealTerminalData::type() const ElementData::TerminalFunction RealTerminalData::function() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { return shared_->function(); } @@ -1329,7 +1263,7 @@ ElementData::TerminalFunction RealTerminalData::function() const bool RealTerminalData::isLed() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { return shared_->led(); } @@ -1339,7 +1273,7 @@ bool RealTerminalData::isLed() const bool RealTerminalData::isElement() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { return shared_->isElement(); } @@ -1349,7 +1283,7 @@ bool RealTerminalData::isElement() const bool RealTerminalData::isBridged() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { auto strip = shared_->parentStrip(); if (strip) { @@ -1366,7 +1300,7 @@ bool RealTerminalData::isBridged() const */ Element *RealTerminalData::element() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { return shared_->element(); } @@ -1376,7 +1310,7 @@ Element *RealTerminalData::element() const QUuid RealTerminalData::elementUuid() const { - auto element_ = element(); + const auto element_ = element(); if (element_) { return element_->uuid(); } @@ -1385,7 +1319,7 @@ QUuid RealTerminalData::elementUuid() const QSharedPointer RealTerminalData::bridge() const { - auto shared_ = m_real_terminal.toStrongRef(); + const auto shared_ = m_real_terminal.toStrongRef(); if (shared_) { auto strip = shared_->parentStrip(); if (strip) { @@ -1399,3 +1333,68 @@ QWeakPointer RealTerminalData::realTerminal() const { return m_real_terminal; } + +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ +/************************************************************************************/ + + +PhysicalTerminalData::PhysicalTerminalData(const TerminalStrip *strip, QSharedPointer terminal) : + m_strip(strip), + m_physical_terminal(terminal.toWeakRef()) +{} + +bool PhysicalTerminalData::isNull() const +{ + return m_physical_terminal.isNull(); +} + +int PhysicalTerminalData::pos() const +{ + if (m_strip) { + return m_strip->pos(m_physical_terminal); + } else { + return -1; + } +} + +QUuid PhysicalTerminalData::uuid() const +{ + const auto pt_ = m_physical_terminal.toStrongRef(); + if (pt_) { + return pt_->uuid(); + } else { + return QUuid(); + } +} + +int PhysicalTerminalData::realTerminalCount() const +{ + const auto pt_ = m_physical_terminal.toStrongRef(); + if (pt_) { + return pt_->terminals().size(); + } else { + return 0; + } + +} + +QVector PhysicalTerminalData::realTerminalDatas() const +{ + QVector rtd_vector; + const auto pt_ = m_physical_terminal.toStrongRef(); + if (pt_ && m_strip) + { + for (const auto & rt_ : pt_->terminals()) { + rtd_vector.append(m_strip->realTerminalDataFor(rt_.toWeakRef())); + } + } + + return rtd_vector; +} + +QWeakPointer PhysicalTerminalData::physicalTerminal() const { + return m_physical_terminal; +} diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 5d68bba6c..0eccf21b3 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -31,6 +31,7 @@ class QETProject; class PhysicalTerminal; class TerminalStripIndex; class TerminalElement; +class TerminalStrip; struct TerminalStripBridge { @@ -56,8 +57,10 @@ inline uint qHash(const QWeakPointer &key, uint seed) class RealTerminalData { friend class TerminalStrip; + friend class PhysicalTerminalData; private: RealTerminalData(QSharedPointer real_terminal); + RealTerminalData(QWeakPointer real_terminal); public: RealTerminalData() {} @@ -88,24 +91,39 @@ class RealTerminalData }; /** - * @brief The PhysicalTerminalData struct + * @brief The PhysicalTerminalData * Conveniant struct to quickly get some values * of a PhysicalTerminal */ -struct PhysicalTerminalData +class PhysicalTerminalData { - QVector real_terminals_vector; - int pos_ = -1; - QUuid uuid_; + friend class TerminalStrip; + + private: + PhysicalTerminalData(const TerminalStrip *strip, QSharedPointer terminal); + + public: + PhysicalTerminalData(){} + + bool isNull() const; + int pos() const; + QUuid uuid() const; + int realTerminalCount() const; + QVector realTerminalDatas() const; + QWeakPointer physicalTerminal() const; + + private: + QPointer m_strip; + QWeakPointer m_physical_terminal; }; //Code to use PhysicalTerminalData as key for QHash inline bool operator == (const PhysicalTerminalData &phy_1, const PhysicalTerminalData &phy_2) { - return phy_1.uuid_ == phy_2.uuid_; + return phy_1.uuid() == phy_2.uuid(); } inline uint qHash(const PhysicalTerminalData &key, uint seed) { - return qHash(key.uuid_, seed); + return qHash(key.uuid(), seed); } /** @@ -155,21 +173,22 @@ class TerminalStrip : public QObject bool addTerminal (Element *terminal); bool removeTerminal (Element *terminal); + int pos(const QWeakPointer &terminal) const; int physicalTerminalCount() const; PhysicalTerminalData physicalTerminalData(int index) const; - PhysicalTerminalData physicalTerminalData (QWeakPointer real_terminal) const; + PhysicalTerminalData physicalTerminalData (const QWeakPointer &real_terminal) const; QVector physicalTerminalData() const; + bool setOrderTo(const QVector &sorted_vector); bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector> &added_terminals); void unGroupTerminals(const QVector> &terminals_to_ungroup); bool setLevel(const QWeakPointer &real_terminal, int level); - bool isBridgeable(const QVector &real_terminals_data) const; bool isBridgeable(const QVector> &real_terminals) const; bool setBridge(const QVector> &real_terminals); - bool setBridge(QSharedPointer bridge, const QVector> &real_terminals); + bool setBridge(const QSharedPointer &bridge, const QVector> &real_terminals); void unBridge(const QVector> &real_terminals); - QSharedPointer bridgeFor(QWeakPointer real_terminal) const; + QSharedPointer bridgeFor(const QWeakPointer &real_terminal) const; RealTerminalData previousTerminalInLevel(const QWeakPointer &real_terminal) const; RealTerminalData nextTerminalInLevel(const QWeakPointer &real_terminal) const; @@ -186,8 +205,6 @@ class TerminalStrip : public QObject private: QSharedPointer realTerminal(Element *terminal); QSharedPointer physicalTerminal(QSharedPointer terminal) const; - QSharedPointer physicalTerminalForUuid (const QUuid &uuid) const; - QSharedPointer realTerminalForUuid(const QUuid &uuid) const; QSharedPointer isBridged(const QSharedPointer real_terminal) const; QSharedPointer bridgeFor (const QVector> &terminal_vector) const; void rebuildRealVector(); diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index f6753534d..4f0b6d396 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -232,9 +232,9 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s for (auto i=0 ; iphysicalTerminalCount() ; ++i) { auto ptd = terminal_strip->physicalTerminalData(i); - if (ptd.real_terminals_vector.size()) + if (ptd.realTerminalCount()) { - auto real_t = ptd.real_terminals_vector.first(); + const auto real_t = ptd.realTerminalDatas().at(0); auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(real_t.label()), TerminalStripTreeWidget::Terminal); terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t.elementUuid()); terminal_item->setIcon(0, QET::Icons::ElementTerminal); @@ -352,7 +352,7 @@ void TerminalStripEditor::spanMultiLevelTerminals() auto current_row = 0; for (auto i = 0 ; i < m_current_strip->physicalTerminalCount() ; ++i) { - const auto level_count = m_current_strip->physicalTerminalData(i).real_terminals_vector.size(); + const auto level_count = m_current_strip->physicalTerminalData(i).realTerminalCount(); if (level_count > 1) { ui->m_table_widget->setSpan(current_row, 0, level_count, 1); } @@ -446,7 +446,7 @@ void TerminalStripEditor::selectionChanged() if (m_current_strip) { QVector> vector_; - for (const auto &mrtd : model_rtd_vector) { + for (const auto &mrtd : qAsConst(model_rtd_vector)) { vector_.append(mrtd.real_terminal); } enable_bridge = m_current_strip->isBridgeable(vector_); @@ -650,8 +650,7 @@ void TerminalStripEditor::on_m_dialog_button_box_clicked(QAbstractButton *button { for (const auto &data_ : m_model->modifiedmodelRealTerminalData()) { - auto element = data_.element_; - if (element) + if (auto element = data_.element_) { auto current_data = element->elementData(); current_data.setTerminalType(data_.type_); diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 2d14ec95a..ec05636ba 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -484,10 +484,10 @@ void TerminalStripModel::fillPhysicalTerminalData() for (const auto &ptd : m_terminal_strip->physicalTerminalData()) { modelPhysicalTerminalData mptd; - mptd.pos_ = ptd.pos_; - mptd.uuid_ = ptd.uuid_; + mptd.pos_ = ptd.pos(); + mptd.uuid_ = ptd.uuid(); - for (const auto &rtd : ptd.real_terminals_vector) + for (const auto &rtd : ptd.realTerminalDatas()) { if (!rtd.isNull()) { @@ -668,7 +668,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const //Check previous auto physical_data = m_terminal_strip->physicalTerminalData(mrtd.real_terminal); auto current_real_terminal = mrtd; - auto current_phy_uuid = physical_data.uuid_; + auto current_phy_uuid = physical_data.uuid(); bool already_jumped_to_previous = false; modelRealTerminalData previous_data; @@ -680,7 +680,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_) + if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid()) { if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { @@ -692,7 +692,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_previous = true; - current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_; + current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid(); if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { previous_data = current_real_terminal; @@ -703,7 +703,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const //Check next current_real_terminal = mrtd; - current_phy_uuid = physical_data.uuid_; + current_phy_uuid = physical_data.uuid(); bool already_jumped_to_next = false; modelRealTerminalData next_data; @@ -715,7 +715,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_) + if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid()) { if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { @@ -727,7 +727,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_next = true; - current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid_; + current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid(); if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { next_data = current_real_terminal; From 02b385e0b714fc4931a3dd510c508abe462b1723 Mon Sep 17 00:00:00 2001 From: joshua Date: Wed, 22 Dec 2021 19:21:54 +0100 Subject: [PATCH 08/14] Improve bridge edition --- .../UndoCommand/bridgeterminalscommand.cpp | 21 ++--- .../UndoCommand/bridgeterminalscommand.h | 4 +- sources/TerminalStrip/terminalstrip.cpp | 77 +++++++++++++++++-- sources/TerminalStrip/terminalstrip.h | 15 +--- .../TerminalStrip/ui/terminalstripeditor.cpp | 34 +++----- 5 files changed, 89 insertions(+), 62 deletions(-) diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp index 7c3a19b61..ebab33488 100644 --- a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp @@ -49,32 +49,23 @@ UnBridgeTerminalsCommand::UnBridgeTerminalsCommand(TerminalStrip *strip, { setText(QObject::tr("Supprimer des ponts de bornes")); - for (const auto &real_t : real_terminal) + if (strip->canUnBridge(real_terminal)) { - auto bridge_ = strip->bridgeFor(real_t); - if (bridge_) { - m_bridge_terminal_hash.insert(bridge_.toWeakRef(), real_t); - } + m_terminals = real_terminal; + m_bridge = strip->bridgeFor(real_terminal.first()); } } void UnBridgeTerminalsCommand::undo() { - if (m_strip) - { - for (const auto &bridge_ : m_bridge_terminal_hash.uniqueKeys()) - { - if (!bridge_.isNull()) { - auto terminal_list = m_bridge_terminal_hash.values(bridge_); - m_strip->setBridge(bridge_.toStrongRef() , terminal_list.toVector()); - } - } + if (m_strip && m_bridge) { + m_strip->setBridge(m_bridge.toStrongRef(), m_terminals); } } void UnBridgeTerminalsCommand::redo() { if (m_strip) { - m_strip->unBridge(m_bridge_terminal_hash.values().toVector()); + m_strip->unBridge(m_terminals); } } diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h index 96f1c795a..424384b05 100644 --- a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h @@ -61,8 +61,8 @@ class UnBridgeTerminalsCommand : public QUndoCommand private: QPointer m_strip; - QMultiHash, QWeakPointer> m_bridge_terminal_hash; ///Key a is bridge, value is real terminal - + QWeakPointer m_bridge; + QVector> m_terminals; }; #endif // BRIDGETERMINALSCOMMAND_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 6dc4c2a7a..7f22aa964 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -883,20 +883,85 @@ bool TerminalStrip::setBridge(const QSharedPointer &bridge, /** * @brief TerminalStrip::unBridge - * Unbridge all real terminal of @a real_terminals - * @param real_terminals_uuid + * Unbridge all real terminals of @a real_terminals + * @sa TerminalStrip::canUnBridge + * @param real_terminals */ void TerminalStrip::unBridge(const QVector> &real_terminals) { - for (const auto &real_t : qAsConst(real_terminals)) + if (canUnBridge(real_terminals)) { - auto bridge_ = bridgeFor(real_t); - if (bridge_) { + auto bridge_ = isBridged(real_terminals.first().toStrongRef()); + for (const auto &real_t : qAsConst(real_terminals)) { bridge_->real_terminals.removeOne(real_t.toStrongRef()); } + + emit bridgeChanged(); + } +} + +/** + * @brief TerminalStrip::canUnBridge + * @param m_real_terminals + * @return True if all terminals of @a real_terminals can be unbridged. + * For this method return True, all terminals must be bridged together, + * be consecutive and in an one or the both extremities of the bridge. + */ +bool TerminalStrip::canUnBridge(const QVector > &real_terminals) const +{ + if (real_terminals.isEmpty()) { + return false; + } + + //Get the bridge of first terminal + const auto compar_bridge = isBridged(real_terminals.first().toStrongRef()); + if (compar_bridge) + { + QMap> sorted_terminal; + + //Check if all terminals are bridged and if it's the same bridge. + //If true insert the terminal in sorted_terminal QMap + //with for key the position of the parent physical terminal + for (const auto &real_t : real_terminals) { + if (compar_bridge != isBridged(real_t.toStrongRef())) { + return false; + } else { + sorted_terminal.insert(m_physical_terminals.indexOf(physicalTerminal(real_t.toStrongRef())), + real_t); + } + } + + //Check if consecutive + const auto count_ = sorted_terminal.size(); + const auto min_max = std::minmax_element(sorted_terminal.keyBegin(), sorted_terminal.keyEnd()); + if ((*min_max.second - *min_max.first) + 1 != count_) { + return false; + } + + //Check if first terminal is the begin of bridge + const auto previous_real_t = previousTerminalInLevel(sorted_terminal.first()); + if (previous_real_t.isNull()) + return true; + else { + const auto previous_bridge = isBridged(previous_real_t.realTerminal()); + if (compar_bridge != previous_bridge) { + return true; + } + } + + //Check if last terminal is the end of bridge + const auto next_real_t = nextTerminalInLevel(sorted_terminal.last()); + if (next_real_t.isNull()) { + return true; + } else { + const auto next_bridge = isBridged(next_real_t.realTerminal()); + if (compar_bridge != next_bridge) { + return true; + } + } } - emit bridgeChanged(); + return false; } QSharedPointer TerminalStrip::bridgeFor(const QWeakPointer &real_terminal) const diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 0eccf21b3..d4b36cc6b 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -40,20 +40,6 @@ struct TerminalStripBridge QUuid uuid_ = QUuid::createUuid(); }; -inline bool operator == (const TerminalStripBridge &bridge_1, const TerminalStripBridge &bridge_2) { - return (bridge_1.uuid_ == bridge_2.uuid_); -} - -inline uint qHash(const QWeakPointer &key, uint seed) -{ - const auto bridge = key.toStrongRef(); - if (bridge) { - return qHash(bridge->uuid_, seed); - } else { - return qHash(QUuid (), seed); - } -} - class RealTerminalData { friend class TerminalStrip; @@ -188,6 +174,7 @@ class TerminalStrip : public QObject bool setBridge(const QVector> &real_terminals); bool setBridge(const QSharedPointer &bridge, const QVector> &real_terminals); void unBridge(const QVector> &real_terminals); + bool canUnBridge(const QVector > &real_terminals) const; QSharedPointer bridgeFor(const QWeakPointer &real_terminal) const; RealTerminalData previousTerminalInLevel(const QWeakPointer &real_terminal) const; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 4f0b6d396..02a9533bb 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -429,37 +429,21 @@ void TerminalStripEditor::selectionChanged() //One column must be selected and the column must be a level column int level_ = TerminalStripModel::levelForColumn(isSingleColumnSelected()); - if (level_ >= 0) + if (level_ >= 0 && m_current_strip) { //Select only terminals of corresponding level cell selection QVector model_real_terminal_level_vector; - for (const auto &mrtd : model_real_terminal_vector) { - if (mrtd.level_ == level_) { + QVector> real_terminal_in_level_vector; + for (const auto &mrtd : model_real_terminal_vector) + { + if (mrtd.level_ == level_) + { model_real_terminal_level_vector.append(mrtd); + real_terminal_in_level_vector.append(mrtd.real_terminal); } } - - QVector model_rtd_vector; - for (const auto &mrtd : model_real_terminal_level_vector) { - model_rtd_vector << mrtd; - } - if (m_current_strip) - { - QVector> vector_; - for (const auto &mrtd : qAsConst(model_rtd_vector)) { - vector_.append(mrtd.real_terminal); - } - enable_bridge = m_current_strip->isBridgeable(vector_); - } - - for (const auto &mrtd : model_real_terminal_level_vector) - { - if (mrtd.bridged_ && - mrtd.level_ == level_) { - enable_unbridge = true; - break; - } - } + enable_bridge = m_current_strip->isBridgeable(real_terminal_in_level_vector); + enable_unbridge = m_current_strip->canUnBridge(real_terminal_in_level_vector); } ui->m_bridge_terminals_pb->setEnabled(enable_bridge); ui->m_unbridge_terminals_pb->setEnabled(enable_unbridge); From a2e5989f3bc1a0bcd57d5dc4b312098127befd0a Mon Sep 17 00:00:00 2001 From: joshua Date: Thu, 23 Dec 2021 18:37:17 +0100 Subject: [PATCH 09/14] Revamp RealTerminal class...... again I don't know what I want, I'm crazy :D. Next commit will also revamp PhysicalTerminal and TerminalStripBridge class, code will be more clear and easy to understand. --- .../UndoCommand/changeterminallevel.cpp | 2 +- .../UndoCommand/sortterminalstripcommand.cpp | 4 +- sources/TerminalStrip/terminalstrip.cpp | 611 ++++++++---------- sources/TerminalStrip/terminalstrip.h | 52 +- .../TerminalStrip/ui/terminalstripeditor.cpp | 12 +- .../TerminalStrip/ui/terminalstripmodel.cpp | 46 +- sources/TerminalStrip/ui/terminalstripmodel.h | 2 +- 7 files changed, 342 insertions(+), 387 deletions(-) diff --git a/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp b/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp index 004a3e46e..47901eafb 100644 --- a/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp +++ b/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp @@ -25,7 +25,7 @@ ChangeTerminalLevel::ChangeTerminalLevel(TerminalStrip *strip, m_strip(strip), m_real_terminal(real_terminal), m_new_level(level), - m_old_level(m_strip->realTerminalDataFor(real_terminal).level()) + m_old_level(real_terminal.toStrongRef()->level()) {} void ChangeTerminalLevel::undo() diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp index 9157445ca..fca830941 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp @@ -54,7 +54,7 @@ void SortTerminalStripCommand::sort() if (arg1.realTerminalCount()) { - str1 = arg1.realTerminalDatas().constLast().label(); + str1 = arg1.realTerminals().constLast().toStrongRef()->label(); auto match = rx.match(str1); if (match.hasMatch()) { @@ -64,7 +64,7 @@ void SortTerminalStripCommand::sort() if (arg2.realTerminalCount()) { - str2 = arg2.realTerminalDatas().constLast().label(); + str2 = arg2.realTerminals().constLast().toStrongRef()->label(); auto match = rx.match(str2); if (match.hasMatch()) { diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 7f22aa964..fc748ea2d 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -26,7 +26,6 @@ using shared_real_terminal = QSharedPointer; using shared_physical_terminal = QSharedPointer; - /************************************************************************************/ /************************************************************************************/ /************************************************************************************/ @@ -34,165 +33,268 @@ 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). - * @sa RealTerminalData + * @brief RealTerminal + * @param parent_strip : parent terminal strip + * @param terminal : terminal element (if any) in a folio */ -class RealTerminal +RealTerminal::RealTerminal(TerminalStrip *parent_strip, + Element *terminal) : + m_element(terminal), + m_parent_terminal_strip(parent_strip) +{} + +/** + * @brief RealTerminal::sharedRef + * @return a QSharedPointer of this + */ +QSharedPointer RealTerminal::sharedRef() { - public : + QSharedPointer this_shared(this->weakRef()); + if (this_shared.isNull()) + { + this_shared = QSharedPointer(this); + m_this_weak = this_shared.toWeakRef(); + } - /** - * @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) - {} + return this_shared; +} - /** +/** + * @brief RealTerminal::weakRef + * @return a QWeakPointer of this + */ +QWeakPointer RealTerminal::weakRef() { + return m_this_weak; +} + +/** + * @brief fromXml + * @param xml_element + * @return + */ +bool RealTerminal::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; + + QUuid uuid_(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; +} + +/** + * @brief toXml + * @param parent_document + * @return this real terminal to xml + */ +QDomElement RealTerminal::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 parentStrip * @return parent terminal strip */ - TerminalStrip *parentStrip() const { - return m_parent_terminal_strip.data(); - } - /** - * @brief isElement - * @return true if this real terminal is linked to a terminal element - */ - bool isElement() const { - return m_element.isNull() ? false : true; +TerminalStrip *RealTerminal::parentStrip() const { + return m_parent_terminal_strip.data(); +} + +/** + * @brief RealTerminal::level + * @return + */ +int RealTerminal::level() const +{ + if (m_parent_terminal_strip) { + const auto phy_t = m_parent_terminal_strip->physicalTerminalData(m_this_weak); + if (!phy_t.isNull()) { + return phy_t.realTerminals().indexOf(m_this_weak); } + } - /** - * @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(); - } + return -1; +} - /** - * @brief label - * @return the label of this real terminal - */ - QString label() const { - if (!m_element.isNull()) { - return m_element->actualLabel(); - } else { - return QLatin1String(); - } - } +/** + * @brief label + * @return the label of this real terminal + */ +QString RealTerminal::label() const { + if (!m_element.isNull()) { + return m_element->actualLabel(); + } else { + return QLatin1String(); + } +} - ElementData::TerminalType type() const { - if (m_element) { - return m_element->elementData().terminalType(); - } else { - return ElementData::TTGeneric; - } - } +/** + * @brief RealTerminal::Xref + * @return Conveniant method to get the XRef + * formated to string + */ +QString RealTerminal::Xref() const +{ + if (!m_element.isNull()) { + return autonum::AssignVariables::genericXref(m_element.data()); + } else { + return QString(); + } +} - ElementData::TerminalFunction function() const { - if (m_element) { - return m_element->elementData().terminalFunction(); - } else { - return ElementData::TFGeneric; - } - } +/** + * @brief RealTerminal::cable + * @return + */ +QString RealTerminal::cable() const { + return QString(); +} - bool led() const { - if (m_element) { - return m_element->elementData().terminalLed(); - } else { - return false; - } - } +/** + * @brief RealTerminal::cableWire + * @return + */ +QString RealTerminal::cableWire() const { + return QString(); +} - /** - * @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 RealTerminal::conductor + * @return + */ +QString RealTerminal::conductor() const { + return QString(); +} - /** - * @brief uuid - * @return the uuid of this real terminal - */ - QUuid uuid() const { - return m_uuid; - } +/** + * @brief RealTerminal::type + * @return + */ +ElementData::TerminalType RealTerminal::type() const { + if (m_element) { + return m_element->elementData().terminalType(); + } else { + return ElementData::TTGeneric; + } +} - static QString xmlTagName() { - return QStringLiteral("real_terminal"); - } +/** + * @brief RealTerminal::function + * @return + */ +ElementData::TerminalFunction RealTerminal::function() const { + if (m_element) { + return m_element->elementData().terminalFunction(); + } else { + return ElementData::TFGeneric; + } +} - /** - * @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()); +/** + * @brief RealTerminal::isLed + * @return + */ +bool RealTerminal::isLed() const { + if (m_element) { + return m_element->elementData().terminalLed(); + } else { + return false; + } +} - return root_elmt; - } +/** + * @brief isElement + * @return true if this real terminal is linked to a terminal element + */ +bool RealTerminal::isElement() const { + return m_element.isNull() ? false : true; +} - /** - * @brief fromXml - * @param xml_element - * @return - */ - bool fromXml(QDomElement xml_element, const QVector &terminal_vector) - { - if (xml_element.tagName() != xmlTagName()) { - return true; - } +/** + * @brief RealTerminal::isBridged + * @return + */ +bool RealTerminal::isBridged() const +{ + if (m_parent_terminal_strip) { + return !m_parent_terminal_strip->bridgeFor(m_this_weak).isNull(); + } else { + return false; + } +} - auto is_draw = xml_element.attribute(QStringLiteral("is_draw")) == QLatin1String("true") - ? true : false; +/** + * @brief RealTerminal::bridge + * @return + */ +QSharedPointer RealTerminal::bridge() const +{ + if (m_parent_terminal_strip) { + return m_parent_terminal_strip->bridgeFor(m_this_weak); + } else { + return QSharedPointer(); + } +} - QUuid uuid_(xml_element.attribute(QStringLiteral("uuid"))); +/** + * @brief element + * @return the element linked to this real terminal + * or nullptr if not linked to an Element. + */ +Element *RealTerminal::element() const { + return m_element.data(); +} - if (is_draw) { - for (auto terminal : terminal_vector) { - if (terminal->uuid() == uuid_) - { - m_element = terminal; - break; - } - } - } else { - m_uuid = uuid_; - } +/** + * @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 RealTerminal::elementUuid() const { + if (!m_element.isNull()) { + return m_element->uuid(); + } else { + return QUuid(); + } +} - return true; - } - - private : - QPointer m_element; - QPointer m_parent_terminal_strip; - QUuid m_uuid = QUuid::createUuid(); -}; +/** + * @brief uuid + * @return the uuid of this real terminal + */ +QUuid RealTerminal::uuid() const { + return m_uuid; +} +/** + * @brief RealTerminal::RealTerminal::xmlTagName + * @return + */ +QString RealTerminal::RealTerminal::xmlTagName() { + return QStringLiteral("real_terminal"); +} /************************************************************************************/ /************************************************************************************/ @@ -200,9 +302,6 @@ class RealTerminal /************************************************************************************/ /************************************************************************************/ - - - /** * @brief The PhysicalTerminal class * Represent a physical terminal. @@ -455,7 +554,8 @@ bool TerminalStrip::addTerminal(Element *terminal) m_terminal_elements_vector.append(terminal); //Create the real terminal - const shared_real_terminal real_terminal(new RealTerminal(this, terminal)); + auto raw_ptr = new RealTerminal(this, terminal); + auto real_terminal = raw_ptr->sharedRef(); m_real_terminals.append(real_terminal); //Create a new single level physical terminal @@ -748,8 +848,7 @@ bool TerminalStrip::isBridgeable(const QVector> &real } // Get the level of the first terminal - const auto rtd_ = realTerminalDataFor(real_terminals.first()); - const int level_ = rtd_.level(); + const int level_ = real_terminals.first().toStrongRef()->level(); // Get the physical terminal and pos auto first_physical_terminal = physicalTerminal(first_real_terminal); @@ -770,7 +869,7 @@ bool TerminalStrip::isBridgeable(const QVector> &real } // at the same level - if (level_ != realTerminalDataFor(real_terminals.first()).level()) { + if (level_ != real_terminals.first().toStrongRef()->level()) { return false; } @@ -943,7 +1042,7 @@ bool TerminalStrip::canUnBridge(const QVector > &real if (previous_real_t.isNull()) return true; else { - const auto previous_bridge = isBridged(previous_real_t.realTerminal()); + const auto previous_bridge = isBridged(previous_real_t); if (compar_bridge != previous_bridge) { return true; } @@ -954,7 +1053,7 @@ bool TerminalStrip::canUnBridge(const QVector > &real if (next_real_t.isNull()) { return true; } else { - const auto next_bridge = isBridged(next_real_t.realTerminal()); + const auto next_bridge = isBridged(next_real_t); if (compar_bridge != next_bridge) { return true; } @@ -975,7 +1074,7 @@ QSharedPointer TerminalStrip::bridgeFor(const QWeakPointer< * @return The previous real terminal at the samne level of @a real_t * or a null RealTerminalData if there not a previous real terminal */ -RealTerminalData TerminalStrip::previousTerminalInLevel(const QWeakPointer &real_terminal) const +QWeakPointer TerminalStrip::previousTerminalInLevel(const QWeakPointer &real_terminal) const { const auto real_t = real_terminal.toStrongRef(); const auto phy_t = physicalTerminal(real_t); @@ -987,12 +1086,12 @@ RealTerminalData TerminalStrip::previousTerminalInLevel(const QWeakPointerterminals(); if (t_vector.size() > level_) { - return RealTerminalData(t_vector.at(level_)); + return t_vector.at(level_); } } } - return RealTerminalData(); + return QWeakPointer(); } /** @@ -1001,7 +1100,7 @@ RealTerminalData TerminalStrip::previousTerminalInLevel(const QWeakPointer &real_terminal) const +QWeakPointer TerminalStrip::nextTerminalInLevel(const QWeakPointer &real_terminal) const { const auto real_t = real_terminal.toStrongRef(); const auto phy_t = physicalTerminal(real_t); @@ -1013,41 +1112,32 @@ RealTerminalData TerminalStrip::nextTerminalInLevel(const QWeakPointerterminals(); if (t_vector.size() > level_) { - return RealTerminalData(t_vector.at(level_)); + return t_vector.at(level_); } } } - return RealTerminalData(); + return QWeakPointer(); } -RealTerminalData TerminalStrip::previousRealTerminal(const QWeakPointer &real_terminal) const +QWeakPointer TerminalStrip::previousRealTerminal(const QWeakPointer &real_terminal) const { const auto real = real_terminal.toStrongRef(); const auto index = m_real_terminals.indexOf(real); if (index) { - return RealTerminalData(m_real_terminals.at(index-1)); + return m_real_terminals.at(index-1); } - return RealTerminalData(); + return QWeakPointer(); } -RealTerminalData TerminalStrip::nextRealTerminal(const QWeakPointer &real_terminal) const +QWeakPointer TerminalStrip::nextRealTerminal(const QWeakPointer &real_terminal) const { const auto real = real_terminal.toStrongRef(); const auto index = m_real_terminals.indexOf(real); if (index != m_real_terminals.size()-1) { - return RealTerminalData(m_real_terminals.at(index+1)); + return m_real_terminals.at(index+1); } - return RealTerminalData(); -} - -RealTerminalData TerminalStrip::realTerminalDataFor(const QWeakPointer &real_terminal) const -{ - const auto rt = real_terminal.toStrongRef(); - if (rt && m_real_terminals.contains(rt)) - return RealTerminalData(rt); - else - return RealTerminalData(); + return QWeakPointer(); } /** @@ -1112,7 +1202,8 @@ bool TerminalStrip::fromXml(QDomElement &xml_element) //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)); + auto raw_ptr = new RealTerminal(this); + auto real_t = raw_ptr->sharedRef(); real_t->fromXml(xml_real, free_terminals); if(real_t->isElement()) { @@ -1241,171 +1332,6 @@ void TerminalStrip::rebuildRealVector() /************************************************************************************/ -/** - * @brief RealTerminalData::RealTerminalData - * @param real_terminal - */ -RealTerminalData::RealTerminalData(QSharedPointer real_terminal) : - m_real_terminal(real_terminal.toWeakRef()) -{} - -RealTerminalData::RealTerminalData(QWeakPointer real_terminal) : - m_real_terminal(real_terminal) -{} - -bool RealTerminalData::isNull() const -{ - return m_real_terminal.isNull(); -} - -int RealTerminalData::level() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - auto strip = shared_->parentStrip(); - if (strip) { - auto phys = strip->physicalTerminal(shared_); - if (phys) { - return phys->levelOf(shared_); - } - } - } - - return -1; -} - -QString RealTerminalData::label() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - return shared_->label(); - } else { - return QString(); - } -} - -QString RealTerminalData::Xref() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_ && shared_->isElement()) { - return autonum::AssignVariables::genericXref(shared_->element()); - } else { - return QString(); - } -} - -QString RealTerminalData::cable() const { - return QString(); -} - -QString RealTerminalData::cableWire() const { - return QString(); -} - -QString RealTerminalData::conductor() const { - return QString(); -} - -ElementData::TerminalType RealTerminalData::type() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - return shared_->type(); - } - - return ElementData::TerminalType::TTGeneric; -} - -ElementData::TerminalFunction RealTerminalData::function() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - return shared_->function(); - } - - return ElementData::TerminalFunction::TFGeneric; -} - -bool RealTerminalData::isLed() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - return shared_->led(); - } - - return false; -} - -bool RealTerminalData::isElement() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - return shared_->isElement(); - } - - return false; -} - -bool RealTerminalData::isBridged() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - auto strip = shared_->parentStrip(); - if (strip) { - return !strip->isBridged(shared_).isNull(); - } - } - return false; -} - -/** - * @brief RealTerminalData::element - * @return The element represented by this real - * terminal, or nullptr - */ -Element *RealTerminalData::element() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - return shared_->element(); - } - - return nullptr; -} - -QUuid RealTerminalData::elementUuid() const -{ - const auto element_ = element(); - if (element_) { - return element_->uuid(); - } - return QUuid(); -} - -QSharedPointer RealTerminalData::bridge() const -{ - const auto shared_ = m_real_terminal.toStrongRef(); - if (shared_) { - auto strip = shared_->parentStrip(); - if (strip) { - return strip->isBridged(shared_); - } - } - return QSharedPointer(); -} - -QWeakPointer RealTerminalData::realTerminal() const -{ - return m_real_terminal; -} - -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ - - PhysicalTerminalData::PhysicalTerminalData(const TerminalStrip *strip, QSharedPointer terminal) : m_strip(strip), m_physical_terminal(terminal.toWeakRef()) @@ -1446,18 +1372,21 @@ int PhysicalTerminalData::realTerminalCount() const } -QVector PhysicalTerminalData::realTerminalDatas() const +QVector> PhysicalTerminalData::realTerminals() const { - QVector rtd_vector; - const auto pt_ = m_physical_terminal.toStrongRef(); - if (pt_ && m_strip) + const auto phy_t = m_physical_terminal.toStrongRef(); + if (phy_t) { - for (const auto & rt_ : pt_->terminals()) { - rtd_vector.append(m_strip->realTerminalDataFor(rt_.toWeakRef())); + QVector> vector_; + for (const auto &real_t : phy_t->terminals()) { + vector_.append(real_t.toWeakRef()); } + return vector_; + } + else + { + return QVector>(); } - - return rtd_vector; } QWeakPointer PhysicalTerminalData::physicalTerminal() const { diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index d4b36cc6b..e47561457 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -40,18 +40,32 @@ struct TerminalStripBridge QUuid uuid_ = QUuid::createUuid(); }; -class RealTerminalData +/** + * @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). + * + * When create a new instance of RealTerminal you must + * call sharedRef() and only use the returned QSharedPointer + * instead of the raw pointer + */ +class RealTerminal { friend class TerminalStrip; - friend class PhysicalTerminalData; + friend class PhysicalTerminal; + private: - RealTerminalData(QSharedPointer real_terminal); - RealTerminalData(QWeakPointer real_terminal); + RealTerminal(TerminalStrip *strip, Element *element = nullptr); + QSharedPointer sharedRef(); + QWeakPointer weakRef(); + + bool fromXml(QDomElement xml_element, const QVector &terminal_vector); + QDomElement toXml(QDomDocument &parent_document) const; public: - RealTerminalData() {} - - bool isNull() const; + TerminalStrip *parentStrip() const; int level() const; QString label() const; QString Xref() const; @@ -66,14 +80,19 @@ class RealTerminalData bool isElement() const; bool isBridged() const; + QSharedPointer bridge() const; + Element* element() const; QUuid elementUuid() const; + QUuid uuid() const; - QSharedPointer bridge() const; - QWeakPointer realTerminal() const; + static QString xmlTagName(); - private: - QWeakPointer m_real_terminal; + private : + QPointer m_element; + QPointer m_parent_terminal_strip; + QUuid m_uuid = QUuid::createUuid(); + QWeakPointer m_this_weak; }; /** @@ -95,7 +114,7 @@ class PhysicalTerminalData int pos() const; QUuid uuid() const; int realTerminalCount() const; - QVector realTerminalDatas() const; + QVector> realTerminals() const; QWeakPointer physicalTerminal() const; private: @@ -177,11 +196,10 @@ class TerminalStrip : public QObject bool canUnBridge(const QVector > &real_terminals) const; QSharedPointer bridgeFor(const QWeakPointer &real_terminal) const; - RealTerminalData previousTerminalInLevel(const QWeakPointer &real_terminal) const; - RealTerminalData nextTerminalInLevel(const QWeakPointer &real_terminal) const; - RealTerminalData previousRealTerminal(const QWeakPointer &real_terminal) const; - RealTerminalData nextRealTerminal(const QWeakPointer &real_terminal) const; - RealTerminalData realTerminalDataFor(const QWeakPointer &real_terminal) const; + QWeakPointer previousTerminalInLevel(const QWeakPointer &real_terminal) const; + QWeakPointer nextTerminalInLevel(const QWeakPointer &real_terminal) const; + QWeakPointer previousRealTerminal(const QWeakPointer &real_terminal) const; + QWeakPointer nextRealTerminal(const QWeakPointer &real_terminal) const; QVector> terminalElement() const; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 02a9533bb..87c55cde8 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -234,13 +234,13 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s auto ptd = terminal_strip->physicalTerminalData(i); if (ptd.realTerminalCount()) { - const auto real_t = ptd.realTerminalDatas().at(0); - auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(real_t.label()), TerminalStripTreeWidget::Terminal); - terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t.elementUuid()); + const auto real_t = ptd.realTerminals().at(0).toStrongRef(); + auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(real_t->label()), TerminalStripTreeWidget::Terminal); + terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t->elementUuid()); terminal_item->setIcon(0, QET::Icons::ElementTerminal); - if (real_t.element()) { - m_uuid_terminal_H.insert(real_t.elementUuid(), qgraphicsitem_cast(real_t.element())); + if (real_t->element()) { + m_uuid_terminal_H.insert(real_t->elementUuid(), qgraphicsitem_cast(real_t->element())); } } } @@ -644,7 +644,7 @@ void TerminalStripEditor::on_m_dialog_button_box_clicked(QAbstractButton *button if (element->elementData() != current_data) m_project->undoStack()->push(new ChangeElementDataCommand(element, current_data)); - if (data_.level_ != m_current_strip->realTerminalDataFor(data_.real_terminal).level()) + if (data_.level_ != data_.real_terminal.toStrongRef()->level()) m_project->undoStack()->push(new ChangeTerminalLevel(m_current_strip, data_.real_terminal, data_.level_)); } } diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index ec05636ba..4ec7837d4 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -487,11 +487,11 @@ void TerminalStripModel::fillPhysicalTerminalData() mptd.pos_ = ptd.pos(); mptd.uuid_ = ptd.uuid(); - for (const auto &rtd : ptd.realTerminalDatas()) + for (const auto &real_t : ptd.realTerminals()) { - if (!rtd.isNull()) + if (!real_t.isNull()) { - mptd.real_data.append(modelRealData(rtd)); + mptd.real_data.append(modelRealData(real_t)); } } @@ -644,8 +644,15 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const auto bridge_ = m_terminal_strip->bridgeFor(mrtd.real_terminal); if (bridge_) { - auto previous_bridge = m_terminal_strip->previousTerminalInLevel(mrtd.real_terminal).bridge(); - auto next_bridge = m_terminal_strip->nextTerminalInLevel(mrtd.real_terminal).bridge(); + const auto previous_t = m_terminal_strip->previousTerminalInLevel(mrtd.real_terminal).toStrongRef(); + QSharedPointer previous_bridge; + if (previous_t) + previous_bridge = previous_t->bridge(); + + const auto next_t = m_terminal_strip->nextTerminalInLevel(mrtd.real_terminal).toStrongRef(); + QSharedPointer next_bridge; + if (next_t) + next_bridge = next_t->bridge(); auto color_ = bridge_->color_; auto pixmap_ = m_bridges_pixmaps.value(color_); @@ -747,23 +754,24 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const return QPixmap(); } -modelRealTerminalData TerminalStripModel::modelRealData(const RealTerminalData &data) +modelRealTerminalData TerminalStripModel::modelRealData(const QWeakPointer &real_terminal) { modelRealTerminalData mrtd; - if (!data.isNull()) + const auto real_t = real_terminal.toStrongRef(); + if (!real_terminal.isNull()) { - mrtd.level_ = data.level(); - mrtd.label_ = data.label(); - mrtd.Xref_ = data.Xref(); - mrtd.cable_ = data.cable(); - mrtd.cable_wire = data.cableWire(); - mrtd.conductor_ = data.conductor(); - mrtd.led_ = data.isLed(); - mrtd.type_ = data.type(); - mrtd.function_ = data.function(); - mrtd.element_ = data.element(); - mrtd.real_terminal = data.realTerminal(); - mrtd.bridged_ = data.isBridged(); + mrtd.level_ = real_t->level(); + mrtd.label_ = real_t->label(); + mrtd.Xref_ = real_t->Xref(); + mrtd.cable_ = real_t->cable(); + mrtd.cable_wire = real_t->cableWire(); + mrtd.conductor_ = real_t->conductor(); + mrtd.led_ = real_t->isLed(); + mrtd.type_ = real_t->type(); + mrtd.function_ = real_t->function(); + mrtd.element_ = real_t->element(); + mrtd.real_terminal = real_terminal; + mrtd.bridged_ = real_t->isBridged(); } return mrtd; diff --git a/sources/TerminalStrip/ui/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 245727a07..d3fa6d40c 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -124,7 +124,7 @@ class TerminalStripModel : public QAbstractTableModel modelRealTerminalData realDataAtIndex(int index) const; QPixmap bridgePixmapFor(const QModelIndex &index) const; - static modelRealTerminalData modelRealData(const RealTerminalData &data); + static modelRealTerminalData modelRealData(const QWeakPointer &real_terminal); private: QPointer m_terminal_strip; From 2ea9f8a2c6635c8df74536f5f3426f57e66a6f13 Mon Sep 17 00:00:00 2001 From: joshua Date: Thu, 23 Dec 2021 22:17:37 +0100 Subject: [PATCH 10/14] Revamp PhysicalTerminal class --- .../UndoCommand/groupterminalscommand.cpp | 35 +- .../UndoCommand/groupterminalscommand.h | 14 +- .../UndoCommand/sortterminalstripcommand.cpp | 18 +- .../UndoCommand/sortterminalstripcommand.h | 6 +- sources/TerminalStrip/terminalstrip.cpp | 421 ++++++++---------- sources/TerminalStrip/terminalstrip.h | 58 +-- .../TerminalStrip/ui/terminalstripeditor.cpp | 18 +- .../TerminalStrip/ui/terminalstripmodel.cpp | 23 +- sources/utils/qetutils.h | 25 +- 9 files changed, 303 insertions(+), 315 deletions(-) diff --git a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp index 1681b4073..8853c7bba 100644 --- a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp @@ -16,6 +16,7 @@ along with QElectroTech. If not, see . */ #include "groupterminalscommand.h" +#include "../../utils/qetutils.h" /** * @brief GroupTerminalsCommand::GroupTerminalsCommand @@ -24,8 +25,8 @@ * @param to_group : Terminals to group */ GroupTerminalsCommand::GroupTerminalsCommand(TerminalStrip *strip, - const PhysicalTerminalData &receiver_, - const QVector> &to_group, + const QSharedPointer &receiver_, + const QVector> &to_group, QUndoCommand *parent): QUndoCommand(parent), m_terminal_strip(strip), @@ -37,18 +38,18 @@ GroupTerminalsCommand::GroupTerminalsCommand(TerminalStrip *strip, void GroupTerminalsCommand::undo() { if (m_terminal_strip) { - m_terminal_strip->unGroupTerminals(m_to_group); + m_terminal_strip->unGroupTerminals(QETUtils::sharedVectorToWeak(m_to_group)); } } void GroupTerminalsCommand::redo() { if (m_terminal_strip) { - m_terminal_strip->groupTerminals(m_receiver, m_to_group); + m_terminal_strip->groupTerminals(m_receiver, QETUtils::sharedVectorToWeak(m_to_group)); } } UnGroupTerminalsCommand::UnGroupTerminalsCommand(TerminalStrip *strip, - const QVector> &to_ungroup, + const QVector> &to_ungroup, QUndoCommand *parent) : QUndoCommand(parent), m_terminal_strip(strip) @@ -62,7 +63,7 @@ void UnGroupTerminalsCommand::undo() if (m_terminal_strip) { for (const auto &key : m_physical_real_H.keys()) { - m_terminal_strip->groupTerminals(key, m_physical_real_H.value(key)); + m_terminal_strip->groupTerminals(key, QETUtils::sharedVectorToWeak(m_physical_real_H.value(key))); } } } @@ -72,24 +73,26 @@ void UnGroupTerminalsCommand::redo() if (m_terminal_strip) { for (const auto &value : qAsConst(m_physical_real_H)) { - m_terminal_strip->unGroupTerminals(value); + m_terminal_strip->unGroupTerminals(QETUtils::sharedVectorToWeak(value)); } } } -void UnGroupTerminalsCommand::setUp(const QVector> &to_ungroup) +void UnGroupTerminalsCommand::setUp(const QVector> &to_ungroup) { for (const auto &rt_ : to_ungroup) { - auto ptd_ = m_terminal_strip->physicalTerminalData(rt_); + auto phy_t = m_terminal_strip->physicalTerminal(rt_.toWeakRef()); + if (phy_t) + { + //Physical have only one real terminal, no need to ungroup it + if (phy_t.toStrongRef()->realTerminalCount() <= 1) { + continue; + } - //Physical have only one real terminal, no need to ungroup it - if (ptd_.realTerminalCount() <= 1) { - continue; + auto vector_ = m_physical_real_H.value(phy_t); + vector_.append(rt_); + m_physical_real_H.insert(phy_t, vector_); } - - auto vector_ = m_physical_real_H.value(ptd_); - vector_.append(rt_); - m_physical_real_H.insert(ptd_, vector_); } } diff --git a/sources/TerminalStrip/UndoCommand/groupterminalscommand.h b/sources/TerminalStrip/UndoCommand/groupterminalscommand.h index 55b250319..6267d3db6 100644 --- a/sources/TerminalStrip/UndoCommand/groupterminalscommand.h +++ b/sources/TerminalStrip/UndoCommand/groupterminalscommand.h @@ -31,8 +31,8 @@ class GroupTerminalsCommand : public QUndoCommand { public: GroupTerminalsCommand(TerminalStrip *strip, - const PhysicalTerminalData &receiver_, - const QVector> &to_group, + const QSharedPointer &receiver_, + const QVector> &to_group, QUndoCommand *parent = nullptr); void undo() override; @@ -40,8 +40,8 @@ class GroupTerminalsCommand : public QUndoCommand private: QPointer m_terminal_strip; - PhysicalTerminalData m_receiver; - QVector> m_to_group; + QSharedPointer m_receiver; + QVector> m_to_group; }; /** @@ -52,18 +52,18 @@ class UnGroupTerminalsCommand : public QUndoCommand { public: UnGroupTerminalsCommand(TerminalStrip *strip, - const QVector> &to_ungroup, + const QVector> &to_ungroup, QUndoCommand *parent = nullptr); void undo() override; void redo() override; private: - void setUp(const QVector> &to_ungroup); + void setUp(const QVector> &to_ungroup); private: QPointer m_terminal_strip; - QHash >> m_physical_real_H; + QHash , QVector>> m_physical_real_H; }; #endif // GROUPTERMINALSCOMMAND_H diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp index fca830941..278005301 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp @@ -17,33 +17,35 @@ */ #include "sortterminalstripcommand.h" #include "../terminalstrip.h" +#include "../../utils/qetutils.h" SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) : QUndoCommand(parent), m_strip(strip) { setText(QObject::tr("Trier le bornier %1").arg(m_strip->name())); - m_old_order = m_new_order = m_strip->physicalTerminalData(); + m_old_order = QETUtils::weakVectorToShared(m_strip->physicalTerminal()); + m_new_order = QETUtils::weakVectorToShared(m_strip->physicalTerminal()); sort(); } void SortTerminalStripCommand::undo() { if (m_strip) { - m_strip->setOrderTo(m_old_order); + m_strip->setOrderTo(QETUtils::sharedVectorToWeak(m_old_order)); } } void SortTerminalStripCommand::redo() { if (m_strip) { - m_strip->setOrderTo(m_new_order); + m_strip->setOrderTo(QETUtils::sharedVectorToWeak(m_new_order)); } } void SortTerminalStripCommand::sort() { - std::sort(m_new_order.begin(), m_new_order.end(), [](PhysicalTerminalData arg1, PhysicalTerminalData arg2) + std::sort(m_new_order.begin(), m_new_order.end(), [](QSharedPointer arg1, QSharedPointer arg2) { const QRegularExpression rx(QStringLiteral("^\\d+")); @@ -52,9 +54,9 @@ void SortTerminalStripCommand::sort() int int1 =-1; int int2 =-1; - if (arg1.realTerminalCount()) + if (arg1->realTerminalCount()) { - str1 = arg1.realTerminals().constLast().toStrongRef()->label(); + str1 = arg1->realTerminals().constLast().toStrongRef()->label(); auto match = rx.match(str1); if (match.hasMatch()) { @@ -62,9 +64,9 @@ void SortTerminalStripCommand::sort() } } - if (arg2.realTerminalCount()) + if (arg2->realTerminalCount()) { - str2 = arg2.realTerminals().constLast().toStrongRef()->label(); + str2 = arg2->realTerminals().constLast().toStrongRef()->label(); auto match = rx.match(str2); if (match.hasMatch()) { diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h index 6531563d2..5c2fd6d65 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h @@ -23,7 +23,7 @@ #include class TerminalStrip; -class PhysicalTerminalData; +class PhysicalTerminal; /** * @brief The SortTerminalStripCommand class @@ -43,8 +43,8 @@ class SortTerminalStripCommand : public QUndoCommand private: QPointer m_strip; - QVector m_old_order, - m_new_order; + QVector> m_old_order, + m_new_order; }; #endif // SORTTERMINALSTRIPCOMMAND_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index fc748ea2d..0763ee4a2 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -22,6 +22,7 @@ #include "../elementprovider.h" #include "../qetxml.h" #include "../autoNum/assignvariables.h" +#include "../../utils/qetutils.h" using shared_real_terminal = QSharedPointer; using shared_physical_terminal = QSharedPointer; @@ -61,7 +62,7 @@ QSharedPointer RealTerminal::sharedRef() /** * @brief RealTerminal::weakRef - * @return a QWeakPointer of this + * @return a QWeakPointer of this, weak pointer can be bull */ QWeakPointer RealTerminal::weakRef() { return m_this_weak; @@ -128,9 +129,9 @@ TerminalStrip *RealTerminal::parentStrip() const { int RealTerminal::level() const { if (m_parent_terminal_strip) { - const auto phy_t = m_parent_terminal_strip->physicalTerminalData(m_this_weak); - if (!phy_t.isNull()) { - return phy_t.realTerminals().indexOf(m_this_weak); + const auto phy_t = m_parent_terminal_strip->physicalTerminal(m_this_weak); + if (phy_t) { + return phy_t.toStrongRef()->levelOf(m_this_weak); } } @@ -334,133 +335,166 @@ QString RealTerminal::RealTerminal::xmlTagName() { * @sa PhysicalTerminalData * */ -class PhysicalTerminal +/** + * @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::PhysicalTerminal(TerminalStrip *parent_strip, + QVector> terminals) : + m_parent_terminal_strip(parent_strip), + m_real_terminal(terminals) +{} + +/** + * @brief PhysicalTerminal::sharedRef + * @return a QSharedPointer of this + */ +QSharedPointer PhysicalTerminal::sharedRef() { - 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) - {} + QSharedPointer this_shared(this->weakRef()); + if (this_shared.isNull()) + { + this_shared = QSharedPointer(this); + m_this_weak = this_shared.toWeakRef(); + } - /** - * @brief setTerminals - * Set the RealTerminal who compose this physical terminal. - * The position of the RealTerminal in @a terminals - * represent the level of these in this physical terminal. - * @param terminals - */ - void setTerminals(QVector terminals) { - m_real_terminal = terminals; - } + return this_shared; +} - /** - * @brief addTerminals - * Append the real terminal @a terminal - * to this physical terminal. - * @param terminal - */ - void addTerminal(shared_real_terminal terminal) { - m_real_terminal.append(terminal); - } +/** + * @brief PhysicalTerminal::weakRef + * @return a QWeakPointer of this, weak pointer can be null + */ +QWeakPointer PhysicalTerminal::weakRef() { + return m_this_weak; +} - /** - * @brief removeTerminal - * Remove @a terminal from the list of real terminal - * @param terminal - * @return true if sucessfully removed - */ - bool removeTerminal(shared_real_terminal terminal) { - return m_real_terminal.removeOne(terminal); - } +/** + * @brief toXml + * @param parent_document + * @return this physical terminal to xml + */ +QDomElement PhysicalTerminal::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)); + } - /** - * @brief levelCount - * @return the number of level of this terminal - */ - int levelCount() const { - return m_real_terminal.size(); - } + return root_elmt; +} - /** - * @brief levelOf - * @param terminal - * @return the level of real terminal \p terminal or - * -1 if \terminal is not a part of this physicalTerminal - */ - int levelOf(shared_real_terminal terminal) const { - return m_real_terminal.indexOf(terminal); - } +/** + * @brief setTerminals + * Set the RealTerminal who compose this physical terminal. + * The position of the RealTerminal in @a terminals + * represent the level of these in this physical terminal. + * @param terminals + */ +void PhysicalTerminal::setTerminals(const QVector> &terminals) { + m_real_terminal = terminals; +} - /** - * @brief setLevelOf - * Change the level of \p terminal - * @param terminal - * @param level - */ - bool setLevelOf(shared_real_terminal terminal, int level) - { - const int i = m_real_terminal.indexOf(terminal); - if (i >= 0) - { +/** + * @brief addTerminals + * Append the real terminal @a terminal + * to this physical terminal. + * @param terminal + */ +void PhysicalTerminal::addTerminal(const QSharedPointer &terminal) { + m_real_terminal.append(terminal); +} + +/** + * @brief removeTerminal + * Remove @a terminal from the list of real terminal + * @param terminal + * @return true if sucessfully removed + */ +bool PhysicalTerminal::removeTerminal(const QSharedPointer &terminal) { + return m_real_terminal.removeOne(terminal); +} + +/** + * @brief levelCount + * @return the number of level of this terminal + */ +int PhysicalTerminal::levelCount() const { + return m_real_terminal.size(); +} + +/** + * @brief levelOf + * @param terminal + * @return the level of real terminal \p terminal or + * -1 if \terminal is not a part of this physicalTerminal + */ +int PhysicalTerminal::levelOf(const QWeakPointer &terminal) const { + return m_real_terminal.indexOf(terminal.toStrongRef()); +} + +/** + * @brief setLevelOf + * Change the level of \p terminal + * @param terminal + * @param level + */ +bool PhysicalTerminal::setLevelOf(const QSharedPointer &terminal, int level) +{ + const int i = m_real_terminal.indexOf(terminal); + if (i >= 0) + { #if QT_VERSION >= QT_VERSION_CHECK(5,14,0) - m_real_terminal.swapItemsAt(i, std::min(level, m_real_terminal.size()-1)); + m_real_terminal.swapItemsAt(i, std::min(level, m_real_terminal.size()-1)); #else - auto j = std::min(level, m_real_terminal.size()-1); - std::swap(m_real_terminal.begin()[i], m_real_terminal.begin()[j]); + auto j = std::min(level, m_real_terminal.size()-1); + std::swap(m_real_terminal.begin()[i], m_real_terminal.begin()[j]); #endif - return true; - } - return false; - } + return true; + } + return false; +} - /** - * @brief terminals - * @return A vector of RealTerminal who compose this PhysicalTerminal - */ - QVector terminals() const { - return m_real_terminal; - } +/** + * @brief terminals + * @return A vector of RealTerminal who compose this PhysicalTerminal + */ +QVector> PhysicalTerminal::realTerminals() const +{ + QVector> vector_; + for (const auto &real_t : m_real_terminal) { + vector_.append(real_t.toWeakRef()); + } + return vector_; +} - /** - * @brief uuid - * @return the uuid of this physical terminal - */ - QUuid uuid() const { - return m_uuid; - } +/** + * @brief uuid + * @return the uuid of this physical terminal + */ +QUuid PhysicalTerminal::uuid() const { + return m_uuid; +} - static QString xmlTagName() { - return QStringLiteral("physical_terminal"); - } +int PhysicalTerminal::pos() const +{ + if (m_parent_terminal_strip) { + return m_parent_terminal_strip->pos(m_this_weak); + } else { + return -1; + } +} - /** - * @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)); - } +int PhysicalTerminal::realTerminalCount() const { + return m_real_terminal.size(); +} - return root_elmt; - } - - private: - QPointer m_parent_terminal_strip; - QVector m_real_terminal; - const QUuid m_uuid = QUuid::createUuid(); -}; +QString PhysicalTerminal::xmlTagName() { + return QStringLiteral("physical_terminal"); +} /************************************************************************************/ @@ -554,16 +588,13 @@ bool TerminalStrip::addTerminal(Element *terminal) m_terminal_elements_vector.append(terminal); //Create the real terminal - auto raw_ptr = new RealTerminal(this, terminal); - auto real_terminal = raw_ptr->sharedRef(); + auto raw_real_ptr = new RealTerminal(this, terminal); + auto real_terminal = raw_real_ptr->sharedRef(); m_real_terminals.append(real_terminal); //Create a new single level physical terminal - const shared_physical_terminal physical_terminal( - new PhysicalTerminal(this, - QVector{real_terminal})); - - m_physical_terminals.append(physical_terminal); + auto raw_phy_ptr = new PhysicalTerminal(this, QVector>{real_terminal}); + m_physical_terminals.append(raw_phy_ptr->sharedRef()); static_cast(terminal)->setParentTerminalStrip(this); @@ -581,7 +612,6 @@ 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)) { @@ -590,9 +620,9 @@ bool TerminalStrip::removeTerminal(Element *terminal) if (physical_terminal->levelCount() == 1) { m_physical_terminals.removeOne(physical_terminal); } else { - auto v = physical_terminal->terminals(); + auto v = physical_terminal->realTerminals(); v.removeOne(real_terminal); - physical_terminal->setTerminals(v); + physical_terminal->setTerminals(QETUtils::weakVectorToShared(v)); } } m_real_terminals.removeOne(real_terminal); @@ -632,50 +662,45 @@ int TerminalStrip::physicalTerminalCount() const { * @brief TerminalStrip::physicalTerminalData * @param index * @return The data of the physical terminal at index \p index + * returned QWeakPointer can be null */ -PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) const +QWeakPointer TerminalStrip::physicalTerminal(int index) const { if (index < m_physical_terminals.size()) { - return PhysicalTerminalData(this, m_physical_terminals.at(index)); + return m_physical_terminals.at(index).toWeakRef(); } else { - return PhysicalTerminalData(); + return QWeakPointer(); } } /** * @brief TerminalStrip::physicalTerminalData * @param real_terminal - * @return the parent PhysicalTerminalData of \p real_terminal. - * the PhysicalTerminalData can be invalid if \p real_terminal don't belong to this strip + * @return the parent PhysicalTerminal of \p real_terminal. + * the PhysicalTerminal can be null if \p real_terminal don't belong to this strip */ -PhysicalTerminalData TerminalStrip::physicalTerminalData (const QWeakPointer &real_terminal) const +QWeakPointer TerminalStrip::physicalTerminal (const QWeakPointer &real_terminal) const { const auto real_t = real_terminal.toStrongRef(); if (real_t.isNull()) { - return PhysicalTerminalData(); + return QWeakPointer(); } const auto phy_t = physicalTerminal(real_t); if (phy_t) { - return PhysicalTerminalData(this, phy_t); + return phy_t.toWeakRef(); + } else { + return QWeakPointer(); } - - return PhysicalTerminalData(); } /** - * @brief TerminalStrip::physicalTerminalData - * @return A vector of all physical terminal data owned by this terminal strip. + * @brief TerminalStrip::physicalTerminal + * @return A vector of all physical terminal owned by this terminal strip. * The order of the vector is the same as the order of the terminal in the strip */ -QVector TerminalStrip::physicalTerminalData() const -{ - QVector v_; - for (auto i = 0 ; i> TerminalStrip::physicalTerminal() const { + return QETUtils::sharedVectorToWeak(m_physical_terminals); } /** @@ -685,23 +710,23 @@ QVector TerminalStrip::physicalTerminalData() const * \p sorted_vector must contain exaclty the same physical terminal as this strip * else this function do nothing. * - * To avoid any mistake, you should call TerminalStrip::physicalTerminalData() + * To avoid any mistake, you should call TerminalStrip::physicalTerminal() * sort the returned vector and call this function with sorted vector, then you are sure * the vector contain the same values, no more no less. * * @param sorted_vector * @return true is successfully sorted. */ -bool TerminalStrip::setOrderTo(const QVector &sorted_vector) +bool TerminalStrip::setOrderTo(const QVector> &sorted_vector) { if (sorted_vector.size() != m_physical_terminals.size()) { return false; } QVector> new_order; - for (const auto &ptd : sorted_vector) + for (const auto &t_ : sorted_vector) { - const auto physical_t = ptd.physicalTerminal().toStrongRef(); + const auto physical_t = t_.toStrongRef(); if (physical_t.isNull()) { continue; } @@ -730,9 +755,9 @@ bool TerminalStrip::setOrderTo(const QVector &sorted_vecto * @param receiver_terminal * @return true if success */ -bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector> &added_terminals) +bool TerminalStrip::groupTerminals(const QWeakPointer &receiver_terminal, const QVector> &added_terminals) { - const auto receiver_ = receiver_terminal.physicalTerminal().toStrongRef(); + const auto receiver_ = receiver_terminal.toStrongRef(); if (receiver_.isNull()) { qDebug() << "TerminalStrip::groupTerminal : Arguments terminals don't belong to this strip. Operation aborted."; return false; @@ -758,7 +783,7 @@ bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal { const auto vector_ = m_physical_terminals; for (const auto &phys : vector_) { - if (phys->terminals().isEmpty()) { + if (phys->realTerminals().isEmpty()) { m_physical_terminals.removeOne(phys); } } @@ -782,13 +807,11 @@ void TerminalStrip::unGroupTerminals(const QVector> & { if (auto physical_terminal = physicalTerminal(real_terminal)) //Get the physical terminal { - if (physical_terminal->terminals().size() > 1) //Check if physical have more than one real terminal + if (physical_terminal->realTerminals().size() > 1) //Check if physical have more than one real terminal { physical_terminal->removeTerminal(real_terminal); - const shared_physical_terminal new_physical_terminal ( - new PhysicalTerminal(this, QVector{real_terminal})); - - m_physical_terminals.append(new_physical_terminal); + auto raw_ptr = new PhysicalTerminal(this, QVector>{real_terminal}); + m_physical_terminals.append(raw_ptr->sharedRef()); ungrouped = true; } } @@ -814,7 +837,7 @@ bool TerminalStrip::setLevel(const QWeakPointer &real_terminal, in auto physical_terminal = physicalTerminal(real_t); if (physical_terminal) { - if (physical_terminal->terminals().size() > 1 && + if (physical_terminal->realTerminals().size() > 1 && physical_terminal->setLevelOf(real_t, level)) { emit orderChanged(); @@ -1084,7 +1107,7 @@ QWeakPointer TerminalStrip::previousTerminalInLevel(const QWeakPoi const auto index = m_physical_terminals.indexOf(phy_t); if (index >= 1) { - const auto t_vector = m_physical_terminals.at(index-1)->terminals(); + const auto t_vector = m_physical_terminals.at(index-1)->realTerminals(); if (t_vector.size() > level_) { return t_vector.at(level_); } @@ -1110,7 +1133,7 @@ QWeakPointer TerminalStrip::nextTerminalInLevel(const QWeakPointer const auto index = m_physical_terminals.indexOf(phy_t); if (index < m_physical_terminals.size()-1) { - const auto t_vector = m_physical_terminals.at(index+1)->terminals(); + const auto t_vector = m_physical_terminals.at(index+1)->realTerminals(); if (t_vector.size() > level_) { return t_vector.at(level_); } @@ -1213,8 +1236,8 @@ bool TerminalStrip::fromXml(QDomElement &xml_element) real_t_vector.append(real_t); } - const shared_physical_terminal phy_t(new PhysicalTerminal(this, real_t_vector)); - m_physical_terminals.append(phy_t); + auto raw_ptr = new PhysicalTerminal(this, real_t_vector); + m_physical_terminals.append(raw_ptr->sharedRef()); m_real_terminals.append(real_t_vector); } @@ -1255,7 +1278,7 @@ QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer< } for (auto &physical : qAsConst(m_physical_terminals)) { - if (physical->terminals().contains(terminal)) { + if (physical->realTerminals().contains(terminal)) { pt = physical; break; } @@ -1321,74 +1344,6 @@ void TerminalStrip::rebuildRealVector() { m_real_terminals.clear(); for (const auto &phy : qAsConst(m_physical_terminals)) { - m_real_terminals.append(phy->terminals()); + m_real_terminals.append(QETUtils::weakVectorToShared(phy->realTerminals())); } } - -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ - - -PhysicalTerminalData::PhysicalTerminalData(const TerminalStrip *strip, QSharedPointer terminal) : - m_strip(strip), - m_physical_terminal(terminal.toWeakRef()) -{} - -bool PhysicalTerminalData::isNull() const -{ - return m_physical_terminal.isNull(); -} - -int PhysicalTerminalData::pos() const -{ - if (m_strip) { - return m_strip->pos(m_physical_terminal); - } else { - return -1; - } -} - -QUuid PhysicalTerminalData::uuid() const -{ - const auto pt_ = m_physical_terminal.toStrongRef(); - if (pt_) { - return pt_->uuid(); - } else { - return QUuid(); - } -} - -int PhysicalTerminalData::realTerminalCount() const -{ - const auto pt_ = m_physical_terminal.toStrongRef(); - if (pt_) { - return pt_->terminals().size(); - } else { - return 0; - } - -} - -QVector> PhysicalTerminalData::realTerminals() const -{ - const auto phy_t = m_physical_terminal.toStrongRef(); - if (phy_t) - { - QVector> vector_; - for (const auto &real_t : phy_t->terminals()) { - vector_.append(real_t.toWeakRef()); - } - return vector_; - } - else - { - return QVector>(); - } -} - -QWeakPointer PhysicalTerminalData::physicalTerminal() const { - return m_physical_terminal; -} diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index e47561457..96f4cdbb2 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -95,42 +95,42 @@ class RealTerminal QWeakPointer m_this_weak; }; -/** - * @brief The PhysicalTerminalData - * Conveniant struct to quickly get some values - * of a PhysicalTerminal - */ -class PhysicalTerminalData +class PhysicalTerminal { friend class TerminalStrip; private: - PhysicalTerminalData(const TerminalStrip *strip, QSharedPointer terminal); + PhysicalTerminal(TerminalStrip *parent_strip, QVector> terminals); + QSharedPointer sharedRef(); + QWeakPointer weakRef(); + + QDomElement toXml(QDomDocument &parent_document) const; + + void setTerminals(const QVector> &terminals); + void addTerminal(const QSharedPointer &terminal); + bool removeTerminal(const QSharedPointer &terminal); + + bool setLevelOf(const QSharedPointer &terminal, int level); public: - PhysicalTerminalData(){} + PhysicalTerminal(){} - bool isNull() const; - int pos() const; - QUuid uuid() const; - int realTerminalCount() const; + int levelCount() const; + int levelOf(const QWeakPointer &terminal) const; QVector> realTerminals() const; - QWeakPointer physicalTerminal() const; + QUuid uuid() const; + int pos() const; + int realTerminalCount() const; + + static QString xmlTagName(); private: - QPointer m_strip; - QWeakPointer m_physical_terminal; + QPointer m_parent_terminal_strip; + QVector> m_real_terminal; + QUuid m_uuid = QUuid::createUuid(); + QWeakPointer m_this_weak; }; -//Code to use PhysicalTerminalData as key for QHash -inline bool operator == (const PhysicalTerminalData &phy_1, const PhysicalTerminalData &phy_2) { - return phy_1.uuid() == phy_2.uuid(); -} - -inline uint qHash(const PhysicalTerminalData &key, uint seed) { - return qHash(key.uuid(), seed); -} - /** * @brief The TerminalStrip class * This class hold all the datas and configurations @@ -180,12 +180,12 @@ class TerminalStrip : public QObject int pos(const QWeakPointer &terminal) const; int physicalTerminalCount() const; - PhysicalTerminalData physicalTerminalData(int index) const; - PhysicalTerminalData physicalTerminalData (const QWeakPointer &real_terminal) const; - QVector physicalTerminalData() const; + QWeakPointer physicalTerminal(int index) const; + QWeakPointer physicalTerminal (const QWeakPointer &real_terminal) const; + QVector> physicalTerminal() const; - bool setOrderTo(const QVector &sorted_vector); - bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector> &added_terminals); + bool setOrderTo(const QVector> &sorted_vector); + bool groupTerminals(const QWeakPointer &receiver_terminal, const QVector> &added_terminals); void unGroupTerminals(const QVector> &terminals_to_ungroup); bool setLevel(const QWeakPointer &real_terminal, int level); diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 87c55cde8..4ca0e4ee7 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -34,6 +34,7 @@ #include "../UndoCommand/groupterminalscommand.h" #include "../UndoCommand/changeterminallevel.h" #include "../UndoCommand/bridgeterminalscommand.h" +#include "../../utils/qetutils.h" #include @@ -231,10 +232,10 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s //Add child terminal of the strip for (auto i=0 ; iphysicalTerminalCount() ; ++i) { - auto ptd = terminal_strip->physicalTerminalData(i); - if (ptd.realTerminalCount()) + auto phy_t = terminal_strip->physicalTerminal(i).toStrongRef(); + if (phy_t->realTerminalCount()) { - const auto real_t = ptd.realTerminals().at(0).toStrongRef(); + const auto real_t = phy_t->realTerminals().at(0).toStrongRef(); auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(real_t->label()), TerminalStripTreeWidget::Terminal); terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t->elementUuid()); terminal_item->setIcon(0, QET::Icons::ElementTerminal); @@ -352,7 +353,7 @@ void TerminalStripEditor::spanMultiLevelTerminals() auto current_row = 0; for (auto i = 0 ; i < m_current_strip->physicalTerminalCount() ; ++i) { - const auto level_count = m_current_strip->physicalTerminalData(i).realTerminalCount(); + const auto level_count = m_current_strip->physicalTerminal(i).toStrongRef()->realTerminalCount(); if (level_count > 1) { ui->m_table_widget->setSpan(current_row, 0, level_count, 1); } @@ -677,13 +678,15 @@ void TerminalStripEditor::on_m_group_terminals_pb_clicked() auto mrtd_vector = m_model->modelRealTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); if (mrtd_vector.size() >= 2) { - auto receiver_ = m_current_strip->physicalTerminalData(mrtd_vector.takeFirst().real_terminal); + auto receiver_ = m_current_strip->physicalTerminal(mrtd_vector.takeFirst().real_terminal); QVector> vector_; for (const auto & mrtd : mrtd_vector) { vector_.append(mrtd.real_terminal); } - m_project->undoStack()->push(new GroupTerminalsCommand(m_current_strip, receiver_, vector_)); + m_project->undoStack()->push(new GroupTerminalsCommand(m_current_strip, + receiver_, + QETUtils::weakVectorToShared(vector_))); } } } @@ -701,7 +704,8 @@ void TerminalStripEditor::on_m_ungroup_pb_clicked() for (const auto &mrtd : mrtd_vector) { vector_.append(mrtd.real_terminal); } - m_project->undoStack()->push(new UnGroupTerminalsCommand(m_current_strip, vector_)); + m_project->undoStack()->push(new UnGroupTerminalsCommand(m_current_strip, + QETUtils::weakVectorToShared(vector_))); } } diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 4ec7837d4..9c7be8841 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -481,13 +481,14 @@ void TerminalStripModel::fillPhysicalTerminalData() //Get all physical terminal if (m_terminal_strip) { - for (const auto &ptd : m_terminal_strip->physicalTerminalData()) + for (const auto &t_ : m_terminal_strip->physicalTerminal()) { + const auto phy_t = t_.toStrongRef(); modelPhysicalTerminalData mptd; - mptd.pos_ = ptd.pos(); - mptd.uuid_ = ptd.uuid(); + mptd.pos_ = phy_t->pos(); + mptd.uuid_ = phy_t->uuid(); - for (const auto &real_t : ptd.realTerminals()) + for (const auto &real_t : phy_t->realTerminals()) { if (!real_t.isNull()) { @@ -673,9 +674,9 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const //Check if we need to draw a none bridge pixmap //Check previous - auto physical_data = m_terminal_strip->physicalTerminalData(mrtd.real_terminal); + auto phy_t = m_terminal_strip->physicalTerminal(mrtd.real_terminal).toStrongRef(); auto current_real_terminal = mrtd; - auto current_phy_uuid = physical_data.uuid(); + auto current_phy_uuid = phy_t->uuid(); bool already_jumped_to_previous = false; modelRealTerminalData previous_data; @@ -687,7 +688,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid()) + if (current_phy_uuid == m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid()) { if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { @@ -699,7 +700,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_previous = true; - current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid(); + current_phy_uuid = m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid(); if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { previous_data = current_real_terminal; @@ -710,7 +711,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const //Check next current_real_terminal = mrtd; - current_phy_uuid = physical_data.uuid(); + current_phy_uuid = phy_t->uuid(); bool already_jumped_to_next = false; modelRealTerminalData next_data; @@ -722,7 +723,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid()) + if (current_phy_uuid == m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid()) { if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { @@ -734,7 +735,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_next = true; - current_phy_uuid = m_terminal_strip->physicalTerminalData(current_real_terminal.real_terminal).uuid(); + current_phy_uuid = m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid(); if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { next_data = current_real_terminal; diff --git a/sources/utils/qetutils.h b/sources/utils/qetutils.h index a997b4504..4dfb6f7c2 100644 --- a/sources/utils/qetutils.h +++ b/sources/utils/qetutils.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -19,7 +19,9 @@ #define QETUTILS_H #include +#include +class RealTerminal; /** Provide some small utils function */ @@ -27,6 +29,27 @@ namespace QETUtils { QString marginsToString(const QMargins &margins); QMargins marginsFromString(const QString &string); + + template + QVector> sharedVectorToWeak(const QVector> &vector) + { + QVector> return_vector; + for (const auto shared : vector) { + return_vector.append(shared.toWeakRef()); + } + return return_vector; + } + + + template + QVector> weakVectorToShared(const QVector> &vector) + { + QVector> return_vector; + for (const auto weak : vector) { + return_vector.append(weak.toStrongRef()); + } + return return_vector; + } } #endif // QETUTILS_H From 5709f469fcfa3594cdaa1836a6c9526a1cd9e970 Mon Sep 17 00:00:00 2001 From: joshua Date: Sun, 26 Dec 2021 17:26:00 +0100 Subject: [PATCH 11/14] Revamp terminalStrip feature code Move RealTerminal class in a new file Move PhysicalTerminal class in a new file. Remove the use of QWeakPointer and use instead QSharedPointer in a big part of the revamp. --- .../UndoCommand/bridgeterminalscommand.cpp | 8 +- .../UndoCommand/bridgeterminalscommand.h | 10 +- .../UndoCommand/changeterminallevel.cpp | 1 + .../UndoCommand/groupterminalscommand.cpp | 11 +- .../UndoCommand/sortterminalstripcommand.cpp | 14 +- sources/TerminalStrip/physicalterminal.cpp | 176 ++++ sources/TerminalStrip/physicalterminal.h | 96 +++ sources/TerminalStrip/realterminal.cpp | 286 +++++++ sources/TerminalStrip/realterminal.h | 86 ++ sources/TerminalStrip/terminalstrip.cpp | 777 +++--------------- sources/TerminalStrip/terminalstrip.h | 133 +-- .../TerminalStrip/ui/terminalstripeditor.cpp | 20 +- .../TerminalStrip/ui/terminalstripmodel.cpp | 25 +- 13 files changed, 844 insertions(+), 799 deletions(-) create mode 100644 sources/TerminalStrip/physicalterminal.cpp create mode 100644 sources/TerminalStrip/physicalterminal.h create mode 100644 sources/TerminalStrip/realterminal.cpp create mode 100644 sources/TerminalStrip/realterminal.h diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp index ebab33488..425adc29c 100644 --- a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.cpp @@ -18,7 +18,7 @@ #include "bridgeterminalscommand.h" BridgeTerminalsCommand::BridgeTerminalsCommand(TerminalStrip *strip, - QVector> real_terminal, + QVector> real_terminal, QUndoCommand *parent): QUndoCommand(parent), m_strip(strip), @@ -42,7 +42,7 @@ void BridgeTerminalsCommand::redo() } UnBridgeTerminalsCommand::UnBridgeTerminalsCommand(TerminalStrip *strip, - QVector> real_terminal, + QVector> real_terminal, QUndoCommand *parent): QUndoCommand(parent), m_strip(strip) @@ -52,14 +52,14 @@ UnBridgeTerminalsCommand::UnBridgeTerminalsCommand(TerminalStrip *strip, if (strip->canUnBridge(real_terminal)) { m_terminals = real_terminal; - m_bridge = strip->bridgeFor(real_terminal.first()); + m_bridge = strip->isBridged(real_terminal.first()); } } void UnBridgeTerminalsCommand::undo() { if (m_strip && m_bridge) { - m_strip->setBridge(m_bridge.toStrongRef(), m_terminals); + m_strip->setBridge(m_bridge, m_terminals); } } diff --git a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h index 424384b05..ec1fcfef4 100644 --- a/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h +++ b/sources/TerminalStrip/UndoCommand/bridgeterminalscommand.h @@ -33,7 +33,7 @@ class BridgeTerminalsCommand : public QUndoCommand { public: - BridgeTerminalsCommand(TerminalStrip *strip, QVector> real_terminal, QUndoCommand *parent = nullptr); + BridgeTerminalsCommand(TerminalStrip *strip, QVector> real_terminal, QUndoCommand *parent = nullptr); ~BridgeTerminalsCommand() override {} void undo() override; @@ -41,7 +41,7 @@ class BridgeTerminalsCommand : public QUndoCommand private: QPointer m_strip; - QVector> m_real_terminal_vector; + QVector> m_real_terminal_vector; }; @@ -53,7 +53,7 @@ class BridgeTerminalsCommand : public QUndoCommand class UnBridgeTerminalsCommand : public QUndoCommand { public: - UnBridgeTerminalsCommand(TerminalStrip *strip, QVector> real_terminal, QUndoCommand *parent = nullptr); + UnBridgeTerminalsCommand(TerminalStrip *strip, QVector> real_terminal, QUndoCommand *parent = nullptr); ~UnBridgeTerminalsCommand() override{} void undo() override; @@ -61,8 +61,8 @@ class UnBridgeTerminalsCommand : public QUndoCommand private: QPointer m_strip; - QWeakPointer m_bridge; - QVector> m_terminals; + QSharedPointer m_bridge; + QVector> m_terminals; }; #endif // BRIDGETERMINALSCOMMAND_H diff --git a/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp b/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp index 47901eafb..c00ba6d46 100644 --- a/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp +++ b/sources/TerminalStrip/UndoCommand/changeterminallevel.cpp @@ -16,6 +16,7 @@ along with QElectroTech. If not, see . */ #include "changeterminallevel.h" +#include "../realterminal.h" ChangeTerminalLevel::ChangeTerminalLevel(TerminalStrip *strip, const QWeakPointer &real_terminal, diff --git a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp index 8853c7bba..3ddb621e4 100644 --- a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp @@ -17,6 +17,7 @@ */ #include "groupterminalscommand.h" #include "../../utils/qetutils.h" +#include "../physicalterminal.h" /** * @brief GroupTerminalsCommand::GroupTerminalsCommand @@ -38,13 +39,13 @@ GroupTerminalsCommand::GroupTerminalsCommand(TerminalStrip *strip, void GroupTerminalsCommand::undo() { if (m_terminal_strip) { - m_terminal_strip->unGroupTerminals(QETUtils::sharedVectorToWeak(m_to_group)); + m_terminal_strip->unGroupTerminals(m_to_group); } } void GroupTerminalsCommand::redo() { if (m_terminal_strip) { - m_terminal_strip->groupTerminals(m_receiver, QETUtils::sharedVectorToWeak(m_to_group)); + m_terminal_strip->groupTerminals(m_receiver,m_to_group); } } @@ -63,7 +64,7 @@ void UnGroupTerminalsCommand::undo() if (m_terminal_strip) { for (const auto &key : m_physical_real_H.keys()) { - m_terminal_strip->groupTerminals(key, QETUtils::sharedVectorToWeak(m_physical_real_H.value(key))); + m_terminal_strip->groupTerminals(key, m_physical_real_H.value(key)); } } } @@ -73,7 +74,7 @@ void UnGroupTerminalsCommand::redo() if (m_terminal_strip) { for (const auto &value : qAsConst(m_physical_real_H)) { - m_terminal_strip->unGroupTerminals(QETUtils::sharedVectorToWeak(value)); + m_terminal_strip->unGroupTerminals(value); } } } @@ -86,7 +87,7 @@ void UnGroupTerminalsCommand::setUp(const QVector> if (phy_t) { //Physical have only one real terminal, no need to ungroup it - if (phy_t.toStrongRef()->realTerminalCount() <= 1) { + if (phy_t->realTerminalCount() <= 1) { continue; } diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp index 278005301..6ce4a0862 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp @@ -17,6 +17,8 @@ */ #include "sortterminalstripcommand.h" #include "../terminalstrip.h" +#include "../physicalterminal.h" +#include "../realterminal.h" #include "../../utils/qetutils.h" SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) : @@ -24,22 +26,22 @@ SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCo m_strip(strip) { setText(QObject::tr("Trier le bornier %1").arg(m_strip->name())); - m_old_order = QETUtils::weakVectorToShared(m_strip->physicalTerminal()); - m_new_order = QETUtils::weakVectorToShared(m_strip->physicalTerminal()); + m_old_order = m_strip->physicalTerminal(); + m_new_order = m_strip->physicalTerminal(); sort(); } void SortTerminalStripCommand::undo() { if (m_strip) { - m_strip->setOrderTo(QETUtils::sharedVectorToWeak(m_old_order)); + m_strip->setOrderTo(m_old_order); } } void SortTerminalStripCommand::redo() { if (m_strip) { - m_strip->setOrderTo(QETUtils::sharedVectorToWeak(m_new_order)); + m_strip->setOrderTo(m_new_order); } } @@ -56,7 +58,7 @@ void SortTerminalStripCommand::sort() if (arg1->realTerminalCount()) { - str1 = arg1->realTerminals().constLast().toStrongRef()->label(); + str1 = arg1->realTerminals().constLast()->label(); auto match = rx.match(str1); if (match.hasMatch()) { @@ -66,7 +68,7 @@ void SortTerminalStripCommand::sort() if (arg2->realTerminalCount()) { - str2 = arg2->realTerminals().constLast().toStrongRef()->label(); + str2 = arg2->realTerminals().constLast()->label(); auto match = rx.match(str2); if (match.hasMatch()) { diff --git a/sources/TerminalStrip/physicalterminal.cpp b/sources/TerminalStrip/physicalterminal.cpp new file mode 100644 index 000000000..f633d45f1 --- /dev/null +++ b/sources/TerminalStrip/physicalterminal.cpp @@ -0,0 +1,176 @@ +/* + 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 "physicalterminal.h" +#include "realterminal.h" +#include "terminalstrip.h" + +/** + * @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::PhysicalTerminal(TerminalStrip *parent_strip, + QVector> terminals) : + m_parent_terminal_strip(parent_strip), + m_real_terminal(terminals) +{} + +/** + * @brief PhysicalTerminal::sharedRef + * @return a QSharedPointer of this + */ +QSharedPointer PhysicalTerminal::sharedRef() +{ + QSharedPointer this_shared(this->weakRef()); + if (this_shared.isNull()) + { + this_shared = QSharedPointer(this); + m_this_weak = this_shared.toWeakRef(); + } + + return this_shared; +} + +/** + * @brief PhysicalTerminal::weakRef + * @return a QWeakPointer of this, weak pointer can be null + */ +QWeakPointer PhysicalTerminal::weakRef() { + return m_this_weak; +} + +/** + * @brief toXml + * @param parent_document + * @return this physical terminal to xml + */ +QDomElement PhysicalTerminal::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; +} + +/** + * @brief setTerminals + * Set the RealTerminal who compose this physical terminal. + * The position of the RealTerminal in @a terminals + * represent the level of these in this physical terminal. + * @param terminals + */ +void PhysicalTerminal::setTerminals(const QVector> &terminals) { + m_real_terminal = terminals; +} + +/** + * @brief addTerminals + * Append the real terminal @a terminal + * to this physical terminal. + * @param terminal + */ +void PhysicalTerminal::addTerminal(const QSharedPointer &terminal) { + m_real_terminal.append(terminal); +} + +/** + * @brief removeTerminal + * Remove @a terminal from the list of real terminal + * @param terminal + * @return true if sucessfully removed + */ +bool PhysicalTerminal::removeTerminal(const QSharedPointer &terminal) { + return m_real_terminal.removeOne(terminal); +} + +/** + * @brief setLevelOf + * Change the level of \p terminal + * @param terminal + * @param level + */ +bool PhysicalTerminal::setLevelOf(const QSharedPointer &terminal, int level) +{ + const int i = m_real_terminal.indexOf(terminal); + if (i >= 0) + { +#if QT_VERSION >= QT_VERSION_CHECK(5,14,0) + m_real_terminal.swapItemsAt(i, std::min(level, m_real_terminal.size()-1)); +#else + auto j = std::min(level, m_real_terminal.size()-1); + std::swap(m_real_terminal.begin()[i], m_real_terminal.begin()[j]); +#endif + return true; + } + return false; +} + +/** + * @brief levelCount + * @return the number of level of this terminal + */ +int PhysicalTerminal::levelCount() const { + return m_real_terminal.size(); +} + +/** + * @brief levelOf + * @param terminal + * @return the level of real terminal \p terminal or + * -1 if \terminal is not a part of this physicalTerminal + */ +int PhysicalTerminal::levelOf(const QSharedPointer &terminal) const { + return m_real_terminal.indexOf(terminal); +} + +/** + * @brief terminals + * @return A vector of RealTerminal who compose this PhysicalTerminal + */ +QVector> PhysicalTerminal::realTerminals() const { + return m_real_terminal; +} + +/** + * @brief uuid + * @return the uuid of this physical terminal + */ +QUuid PhysicalTerminal::uuid() const { + return m_uuid; +} + +int PhysicalTerminal::pos() const +{ + if (m_parent_terminal_strip) { + return m_parent_terminal_strip->pos(m_this_weak); + } else { + return -1; + } +} + +int PhysicalTerminal::realTerminalCount() const { + return m_real_terminal.size(); +} + +QString PhysicalTerminal::xmlTagName() { + return QStringLiteral("physical_terminal"); +} diff --git a/sources/TerminalStrip/physicalterminal.h b/sources/TerminalStrip/physicalterminal.h new file mode 100644 index 000000000..44e800dae --- /dev/null +++ b/sources/TerminalStrip/physicalterminal.h @@ -0,0 +1,96 @@ +/* + 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 PHYSICALTERMINAL_H +#define PHYSICALTERMINAL_H + +#include +#include +#include +#include + +class RealTerminal; +class TerminalStrip; + +/** + * @brief The PhysicalTerminal class + * Represent a physical terminal. + * A physical terminal is composed a least by one RealTerminal. + * When a physical terminal have more than one real terminal + * that mean the physical terminal have levels (one by real terminal). + * The index of real terminals returned by the function terminals() + * is the same as the real level of the real 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 +{ + friend class TerminalStrip; + + private: + PhysicalTerminal(TerminalStrip *parent_strip, QVector> terminals); + QSharedPointer sharedRef(); + QWeakPointer weakRef(); + + QDomElement toXml(QDomDocument &parent_document) const; + + void setTerminals(const QVector> &terminals); + void addTerminal(const QSharedPointer &terminal); + bool removeTerminal(const QSharedPointer &terminal); + + bool setLevelOf(const QSharedPointer &terminal, int level); + + public: + PhysicalTerminal(){} + + int levelCount() const; + int levelOf(const QSharedPointer &terminal) const; + QVector> realTerminals() const; + QUuid uuid() const; + int pos() const; + int realTerminalCount() const; + + static QString xmlTagName(); + + private: + QPointer m_parent_terminal_strip; + QVector> m_real_terminal; + QUuid m_uuid = QUuid::createUuid(); + QWeakPointer m_this_weak; +}; + +#endif // PHYSICALTERMINAL_H diff --git a/sources/TerminalStrip/realterminal.cpp b/sources/TerminalStrip/realterminal.cpp new file mode 100644 index 000000000..7c3f06356 --- /dev/null +++ b/sources/TerminalStrip/realterminal.cpp @@ -0,0 +1,286 @@ +/* + 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 "realterminal.h" +#include "terminalstrip.h" +#include "../qetgraphicsitem/terminalelement.h" +#include "physicalterminal.h" + +/** + * @brief RealTerminal + * @param parent_strip : parent terminal strip + * @param terminal : terminal element (if any) in a folio + */ +RealTerminal::RealTerminal(TerminalStrip *parent_strip, + Element *terminal) : + m_element(terminal), + m_parent_terminal_strip(parent_strip) +{} + +/** + * @brief RealTerminal::sharedRef + * @return a QSharedPointer of this + */ +QSharedPointer RealTerminal::sharedRef() +{ + QSharedPointer this_shared(this->weakRef()); + if (this_shared.isNull()) + { + this_shared = QSharedPointer(this); + m_this_weak = this_shared.toWeakRef(); + } + + return this_shared; +} + +/** + * @brief RealTerminal::weakRef + * @return a QWeakPointer of this, weak pointer can be bull + */ +QWeakPointer RealTerminal::weakRef() { + return m_this_weak; +} + +/** + * @brief fromXml + * @param xml_element + * @return + */ +bool RealTerminal::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; + + QUuid uuid_(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; +} + +/** + * @brief toXml + * @param parent_document + * @return this real terminal to xml + */ +QDomElement RealTerminal::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 parentStrip + * @return parent terminal strip + */ +TerminalStrip *RealTerminal::parentStrip() const { + return m_parent_terminal_strip.data(); +} + +/** + * @brief RealTerminal::level + * @return + */ +int RealTerminal::level() const +{ + if (m_parent_terminal_strip) { + const auto phy_t = m_parent_terminal_strip->physicalTerminal(m_this_weak); + if (phy_t) { + return phy_t->levelOf(m_this_weak); + } + } + + return -1; +} + +/** + * @brief label + * @return the label of this real terminal + */ +QString RealTerminal::label() const { + if (!m_element.isNull()) { + return m_element->actualLabel(); + } else { + return QLatin1String(); + } +} + +/** + * @brief RealTerminal::Xref + * @return Conveniant method to get the XRef + * formated to string + */ +QString RealTerminal::Xref() const +{ + if (!m_element.isNull()) { + return autonum::AssignVariables::genericXref(m_element.data()); + } else { + return QString(); + } +} + +/** + * @brief RealTerminal::cable + * @return + */ +QString RealTerminal::cable() const { + return QString(); +} + +/** + * @brief RealTerminal::cableWire + * @return + */ +QString RealTerminal::cableWire() const { + return QString(); +} + +/** + * @brief RealTerminal::conductor + * @return + */ +QString RealTerminal::conductor() const { + return QString(); +} + +/** + * @brief RealTerminal::type + * @return + */ +ElementData::TerminalType RealTerminal::type() const { + if (m_element) { + return m_element->elementData().terminalType(); + } else { + return ElementData::TTGeneric; + } +} + +/** + * @brief RealTerminal::function + * @return + */ +ElementData::TerminalFunction RealTerminal::function() const { + if (m_element) { + return m_element->elementData().terminalFunction(); + } else { + return ElementData::TFGeneric; + } +} + +/** + * @brief RealTerminal::isLed + * @return + */ +bool RealTerminal::isLed() const { + if (m_element) { + return m_element->elementData().terminalLed(); + } else { + return false; + } +} + +/** + * @brief isElement + * @return true if this real terminal is linked to a terminal element + */ +bool RealTerminal::isElement() const { + return m_element.isNull() ? false : true; +} + +/** + * @brief RealTerminal::isBridged + * @return true if is bridged. + * @sa TerminalStrip::isBridged + */ +bool RealTerminal::isBridged() const +{ + if (m_parent_terminal_strip) { + return !m_parent_terminal_strip->isBridged(m_this_weak.toStrongRef()).isNull(); + } else { + return false; + } +} + +/** + * @brief RealTerminal::bridge + * @return + */ +QSharedPointer RealTerminal::bridge() const +{ + if (m_parent_terminal_strip) { + return m_parent_terminal_strip->isBridged(m_this_weak.toStrongRef()); + } else { + return QSharedPointer(); + } +} + +/** + * @brief element + * @return the element linked to this real terminal + * or nullptr if not linked to an Element. + */ +Element *RealTerminal::element() const { + return m_element.data(); +} + +/** + * @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 RealTerminal::elementUuid() const { + if (!m_element.isNull()) { + return m_element->uuid(); + } else { + return QUuid(); + } +} + +/** + * @brief uuid + * @return the uuid of this real terminal + */ +QUuid RealTerminal::uuid() const { + return m_uuid; +} + +/** + * @brief RealTerminal::RealTerminal::xmlTagName + * @return + */ +QString RealTerminal::RealTerminal::xmlTagName() { + return QStringLiteral("real_terminal"); +} diff --git a/sources/TerminalStrip/realterminal.h b/sources/TerminalStrip/realterminal.h new file mode 100644 index 000000000..4a4ef07ad --- /dev/null +++ b/sources/TerminalStrip/realterminal.h @@ -0,0 +1,86 @@ +/* + 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 REALTERMINAL_H +#define REALTERMINAL_H + +#include +#include +#include "../properties/elementdata.h" + +class TerminalStrip; +class Element; +class TerminalElement; +class PhysicalTerminal; +struct TerminalStripBridge; + +/** + * @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). + * + * When create a new instance of RealTerminal you must + * call sharedRef() and only use the returned QSharedPointer + * instead of the raw pointer + */ +class RealTerminal +{ + friend class TerminalStrip; + friend class PhysicalTerminal; + + private: + RealTerminal(TerminalStrip *strip, Element *element = nullptr); + QSharedPointer sharedRef(); + QWeakPointer weakRef(); + + bool fromXml(QDomElement xml_element, const QVector &terminal_vector); + QDomElement toXml(QDomDocument &parent_document) const; + + public: + TerminalStrip *parentStrip() const; + int level() const; + QString label() const; + QString Xref() const; + QString cable() const; + QString cableWire() const; + QString conductor() const; + + ElementData::TerminalType type() const; + ElementData::TerminalFunction function() const; + + bool isLed() const; + bool isElement() const; + bool isBridged() const; + + QSharedPointer bridge() const; + + Element* element() const; + QUuid elementUuid() const; + QUuid uuid() const; + + static QString xmlTagName(); + + private : + QPointer m_element; + QPointer m_parent_terminal_strip; + QUuid m_uuid = QUuid::createUuid(); + QWeakPointer m_this_weak; +}; + +#endif // REALTERMINAL_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 0763ee4a2..abd09ee32 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -23,486 +23,12 @@ #include "../qetxml.h" #include "../autoNum/assignvariables.h" #include "../../utils/qetutils.h" +#include "physicalterminal.h" +#include "realterminal.h" using shared_real_terminal = QSharedPointer; using shared_physical_terminal = QSharedPointer; -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ - -/** - * @brief RealTerminal - * @param parent_strip : parent terminal strip - * @param terminal : terminal element (if any) in a folio - */ -RealTerminal::RealTerminal(TerminalStrip *parent_strip, - Element *terminal) : - m_element(terminal), - m_parent_terminal_strip(parent_strip) -{} - -/** - * @brief RealTerminal::sharedRef - * @return a QSharedPointer of this - */ -QSharedPointer RealTerminal::sharedRef() -{ - QSharedPointer this_shared(this->weakRef()); - if (this_shared.isNull()) - { - this_shared = QSharedPointer(this); - m_this_weak = this_shared.toWeakRef(); - } - - return this_shared; -} - -/** - * @brief RealTerminal::weakRef - * @return a QWeakPointer of this, weak pointer can be bull - */ -QWeakPointer RealTerminal::weakRef() { - return m_this_weak; -} - -/** - * @brief fromXml - * @param xml_element - * @return - */ -bool RealTerminal::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; - - QUuid uuid_(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; -} - -/** - * @brief toXml - * @param parent_document - * @return this real terminal to xml - */ -QDomElement RealTerminal::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 parentStrip - * @return parent terminal strip - */ -TerminalStrip *RealTerminal::parentStrip() const { - return m_parent_terminal_strip.data(); -} - -/** - * @brief RealTerminal::level - * @return - */ -int RealTerminal::level() const -{ - if (m_parent_terminal_strip) { - const auto phy_t = m_parent_terminal_strip->physicalTerminal(m_this_weak); - if (phy_t) { - return phy_t.toStrongRef()->levelOf(m_this_weak); - } - } - - return -1; -} - -/** - * @brief label - * @return the label of this real terminal - */ -QString RealTerminal::label() const { - if (!m_element.isNull()) { - return m_element->actualLabel(); - } else { - return QLatin1String(); - } -} - -/** - * @brief RealTerminal::Xref - * @return Conveniant method to get the XRef - * formated to string - */ -QString RealTerminal::Xref() const -{ - if (!m_element.isNull()) { - return autonum::AssignVariables::genericXref(m_element.data()); - } else { - return QString(); - } -} - -/** - * @brief RealTerminal::cable - * @return - */ -QString RealTerminal::cable() const { - return QString(); -} - -/** - * @brief RealTerminal::cableWire - * @return - */ -QString RealTerminal::cableWire() const { - return QString(); -} - -/** - * @brief RealTerminal::conductor - * @return - */ -QString RealTerminal::conductor() const { - return QString(); -} - -/** - * @brief RealTerminal::type - * @return - */ -ElementData::TerminalType RealTerminal::type() const { - if (m_element) { - return m_element->elementData().terminalType(); - } else { - return ElementData::TTGeneric; - } -} - -/** - * @brief RealTerminal::function - * @return - */ -ElementData::TerminalFunction RealTerminal::function() const { - if (m_element) { - return m_element->elementData().terminalFunction(); - } else { - return ElementData::TFGeneric; - } -} - -/** - * @brief RealTerminal::isLed - * @return - */ -bool RealTerminal::isLed() const { - if (m_element) { - return m_element->elementData().terminalLed(); - } else { - return false; - } -} - -/** - * @brief isElement - * @return true if this real terminal is linked to a terminal element - */ -bool RealTerminal::isElement() const { - return m_element.isNull() ? false : true; -} - -/** - * @brief RealTerminal::isBridged - * @return - */ -bool RealTerminal::isBridged() const -{ - if (m_parent_terminal_strip) { - return !m_parent_terminal_strip->bridgeFor(m_this_weak).isNull(); - } else { - return false; - } -} - -/** - * @brief RealTerminal::bridge - * @return - */ -QSharedPointer RealTerminal::bridge() const -{ - if (m_parent_terminal_strip) { - return m_parent_terminal_strip->bridgeFor(m_this_weak); - } else { - return QSharedPointer(); - } -} - -/** - * @brief element - * @return the element linked to this real terminal - * or nullptr if not linked to an Element. - */ -Element *RealTerminal::element() const { - return m_element.data(); -} - -/** - * @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 RealTerminal::elementUuid() const { - if (!m_element.isNull()) { - return m_element->uuid(); - } else { - return QUuid(); - } -} - -/** - * @brief uuid - * @return the uuid of this real terminal - */ -QUuid RealTerminal::uuid() const { - return m_uuid; -} - -/** - * @brief RealTerminal::RealTerminal::xmlTagName - * @return - */ -QString RealTerminal::RealTerminal::xmlTagName() { - return QStringLiteral("real_terminal"); -} - -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ - -/** - * @brief The PhysicalTerminal class - * Represent a physical terminal. - * A physical terminal is composed a least by one RealTerminal. - * When a physical terminal have more than one real terminal - * that mean the physical terminal have levels (one by real terminal). - * The index of real terminals returned by the function terminals() - * is the same as the real level of the real 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 - * - * @sa PhysicalTerminalData - * - */ -/** - * @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::PhysicalTerminal(TerminalStrip *parent_strip, - QVector> terminals) : - m_parent_terminal_strip(parent_strip), - m_real_terminal(terminals) -{} - -/** - * @brief PhysicalTerminal::sharedRef - * @return a QSharedPointer of this - */ -QSharedPointer PhysicalTerminal::sharedRef() -{ - QSharedPointer this_shared(this->weakRef()); - if (this_shared.isNull()) - { - this_shared = QSharedPointer(this); - m_this_weak = this_shared.toWeakRef(); - } - - return this_shared; -} - -/** - * @brief PhysicalTerminal::weakRef - * @return a QWeakPointer of this, weak pointer can be null - */ -QWeakPointer PhysicalTerminal::weakRef() { - return m_this_weak; -} - -/** - * @brief toXml - * @param parent_document - * @return this physical terminal to xml - */ -QDomElement PhysicalTerminal::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; -} - -/** - * @brief setTerminals - * Set the RealTerminal who compose this physical terminal. - * The position of the RealTerminal in @a terminals - * represent the level of these in this physical terminal. - * @param terminals - */ -void PhysicalTerminal::setTerminals(const QVector> &terminals) { - m_real_terminal = terminals; -} - -/** - * @brief addTerminals - * Append the real terminal @a terminal - * to this physical terminal. - * @param terminal - */ -void PhysicalTerminal::addTerminal(const QSharedPointer &terminal) { - m_real_terminal.append(terminal); -} - -/** - * @brief removeTerminal - * Remove @a terminal from the list of real terminal - * @param terminal - * @return true if sucessfully removed - */ -bool PhysicalTerminal::removeTerminal(const QSharedPointer &terminal) { - return m_real_terminal.removeOne(terminal); -} - -/** - * @brief levelCount - * @return the number of level of this terminal - */ -int PhysicalTerminal::levelCount() const { - return m_real_terminal.size(); -} - -/** - * @brief levelOf - * @param terminal - * @return the level of real terminal \p terminal or - * -1 if \terminal is not a part of this physicalTerminal - */ -int PhysicalTerminal::levelOf(const QWeakPointer &terminal) const { - return m_real_terminal.indexOf(terminal.toStrongRef()); -} - -/** - * @brief setLevelOf - * Change the level of \p terminal - * @param terminal - * @param level - */ -bool PhysicalTerminal::setLevelOf(const QSharedPointer &terminal, int level) -{ - const int i = m_real_terminal.indexOf(terminal); - if (i >= 0) - { -#if QT_VERSION >= QT_VERSION_CHECK(5,14,0) - m_real_terminal.swapItemsAt(i, std::min(level, m_real_terminal.size()-1)); -#else - auto j = std::min(level, m_real_terminal.size()-1); - std::swap(m_real_terminal.begin()[i], m_real_terminal.begin()[j]); -#endif - return true; - } - return false; -} - -/** - * @brief terminals - * @return A vector of RealTerminal who compose this PhysicalTerminal - */ -QVector> PhysicalTerminal::realTerminals() const -{ - QVector> vector_; - for (const auto &real_t : m_real_terminal) { - vector_.append(real_t.toWeakRef()); - } - return vector_; -} - -/** - * @brief uuid - * @return the uuid of this physical terminal - */ -QUuid PhysicalTerminal::uuid() const { - return m_uuid; -} - -int PhysicalTerminal::pos() const -{ - if (m_parent_terminal_strip) { - return m_parent_terminal_strip->pos(m_this_weak); - } else { - return -1; - } -} - -int PhysicalTerminal::realTerminalCount() const { - return m_real_terminal.size(); -} - -QString PhysicalTerminal::xmlTagName() { - return QStringLiteral("physical_terminal"); -} - - -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ -/************************************************************************************/ - /** * @brief TerminalStrip::TerminalStrip * @param project @@ -622,7 +148,7 @@ bool TerminalStrip::removeTerminal(Element *terminal) } else { auto v = physical_terminal->realTerminals(); v.removeOne(real_terminal); - physical_terminal->setTerminals(QETUtils::weakVectorToShared(v)); + physical_terminal->setTerminals(v); } } m_real_terminals.removeOne(real_terminal); @@ -644,8 +170,8 @@ bool TerminalStrip::removeTerminal(Element *terminal) * @return the position of the physical terminal * or -1 if not found */ -int TerminalStrip::pos(const QWeakPointer &terminal) const { - return m_physical_terminals.indexOf(terminal.toStrongRef()); +int TerminalStrip::pos(const QSharedPointer &terminal) const { + return m_physical_terminals.indexOf(terminal); } /** @@ -662,14 +188,14 @@ int TerminalStrip::physicalTerminalCount() const { * @brief TerminalStrip::physicalTerminalData * @param index * @return The data of the physical terminal at index \p index - * returned QWeakPointer can be null + * returned QSharedPointer can be null */ -QWeakPointer TerminalStrip::physicalTerminal(int index) const +QSharedPointer TerminalStrip::physicalTerminal(int index) const { if (index < m_physical_terminals.size()) { - return m_physical_terminals.at(index).toWeakRef(); + return m_physical_terminals.at(index); } else { - return QWeakPointer(); + return QSharedPointer(); } } @@ -679,19 +205,19 @@ QWeakPointer TerminalStrip::physicalTerminal(int index) const * @return the parent PhysicalTerminal of \p real_terminal. * the PhysicalTerminal can be null if \p real_terminal don't belong to this strip */ -QWeakPointer TerminalStrip::physicalTerminal (const QWeakPointer &real_terminal) const +QSharedPointer TerminalStrip::physicalTerminal (const QSharedPointer &real_terminal) const { - const auto real_t = real_terminal.toStrongRef(); - if (real_t.isNull()) { - return QWeakPointer(); + if (real_terminal.isNull()) { + return QSharedPointer(); } - const auto phy_t = physicalTerminal(real_t); - if (phy_t) { - return phy_t.toWeakRef(); - } else { - return QWeakPointer(); + for (auto &physical : qAsConst(m_physical_terminals)) { + if (physical->realTerminals().contains(real_terminal)) { + return physical; + } } + + return QSharedPointer(); } /** @@ -699,10 +225,28 @@ QWeakPointer TerminalStrip::physicalTerminal (const QWeakPoint * @return A vector of all physical terminal owned by this terminal strip. * The order of the vector is the same as the order of the terminal in the strip */ -QVector> TerminalStrip::physicalTerminal() const { - return QETUtils::sharedVectorToWeak(m_physical_terminals); +QVector> TerminalStrip::physicalTerminal() const { + return m_physical_terminals; } +/** + * @brief TerminalStrip::realTerminal + * @param terminal + * @return the real terminal linked to \p terminal + * the returned QSharedPointer can be null. + */ +QSharedPointer TerminalStrip::realTerminal(Element *terminal) const +{ + for (const auto &real : qAsConst(m_real_terminals)) { + if (real->element() == terminal) { + return real; + } + } + + return shared_real_terminal(); +} + + /** * @brief TerminalStrip::setSortedTo * Sort the physical terminal owned by this strip in the same order @@ -717,16 +261,15 @@ QVector> TerminalStrip::physicalTerminal() const * @param sorted_vector * @return true is successfully sorted. */ -bool TerminalStrip::setOrderTo(const QVector> &sorted_vector) +bool TerminalStrip::setOrderTo(const QVector> &sorted_vector) { if (sorted_vector.size() != m_physical_terminals.size()) { return false; } QVector> new_order; - for (const auto &t_ : sorted_vector) + for (const auto &physical_t : sorted_vector) { - const auto physical_t = t_.toStrongRef(); if (physical_t.isNull()) { continue; } @@ -755,19 +298,16 @@ bool TerminalStrip::setOrderTo(const QVector> &so * @param receiver_terminal * @return true if success */ -bool TerminalStrip::groupTerminals(const QWeakPointer &receiver_terminal, const QVector> &added_terminals) +bool TerminalStrip::groupTerminals(const QSharedPointer &receiver_terminal, const QVector> &added_terminals) { - const auto receiver_ = receiver_terminal.toStrongRef(); - if (receiver_.isNull()) { + if (receiver_terminal.isNull()) { qDebug() << "TerminalStrip::groupTerminal : Arguments terminals don't belong to this strip. Operation aborted."; return false; } bool have_grouped = false; - for (const auto &added : added_terminals) + for (const auto &added_terminal : added_terminals) { - const auto added_terminal = added.toStrongRef(); - if (added_terminal.isNull()) { continue; } @@ -775,7 +315,7 @@ bool TerminalStrip::groupTerminals(const QWeakPointer &receive auto physical_ = physicalTerminal(added_terminal); physical_->removeTerminal(added_terminal); - receiver_->addTerminal(added_terminal); + receiver_terminal->addTerminal(added_terminal); have_grouped = true; } @@ -798,12 +338,12 @@ bool TerminalStrip::groupTerminals(const QWeakPointer &receive * Ungroup all real terminals of \p terminals_to_ungroup * @param terminals_to_ungroup */ -void TerminalStrip::unGroupTerminals(const QVector> &terminals_to_ungroup) +void TerminalStrip::unGroupTerminals(const QVector> &terminals_to_ungroup) { bool ungrouped = false; - for (const auto &rt_ : terminals_to_ungroup) + for (const auto &real_terminal : terminals_to_ungroup) { - if (auto real_terminal = rt_.toStrongRef()) //Get the shared real terminal + if (real_terminal) { if (auto physical_terminal = physicalTerminal(real_terminal)) //Get the physical terminal { @@ -829,16 +369,15 @@ void TerminalStrip::unGroupTerminals(const QVector> & * @param level * @return */ -bool TerminalStrip::setLevel(const QWeakPointer &real_terminal, int level) +bool TerminalStrip::setLevel(const QSharedPointer &real_terminal, int level) { - const auto real_t = real_terminal.toStrongRef(); - if (real_t) + if (real_terminal) { - auto physical_terminal = physicalTerminal(real_t); + auto physical_terminal = physicalTerminal(real_terminal); if (physical_terminal) { if (physical_terminal->realTerminals().size() > 1 && - physical_terminal->setLevelOf(real_t, level)) + physical_terminal->setLevelOf(real_terminal, level)) { emit orderChanged(); return true; @@ -858,20 +397,20 @@ bool TerminalStrip::setLevel(const QWeakPointer &real_terminal, in * @param real_terminals : a vector of realterminal * @return */ -bool TerminalStrip::isBridgeable(const QVector> &real_terminals) const +bool TerminalStrip::isBridgeable(const QVector> &real_terminals) const { if (real_terminals.size() < 2) { return false; } // Check if first terminal belong to this strip - const auto first_real_terminal = real_terminals.first().toStrongRef(); + const auto first_real_terminal = real_terminals.first(); if (!first_real_terminal) { return false; } // Get the level of the first terminal - const int level_ = real_terminals.first().toStrongRef()->level(); + const int level_ = first_real_terminal->level(); // Get the physical terminal and pos auto first_physical_terminal = physicalTerminal(first_real_terminal); @@ -886,13 +425,13 @@ bool TerminalStrip::isBridgeable(const QVector> &real for (int i=1 ; ilevel()) { + if (level_ != real_terminal->level()) { return false; } @@ -936,18 +475,17 @@ bool TerminalStrip::isBridgeable(const QVector> &real * @sa TerminalStrip::isBridgeable * @return true if bridge was successfully created */ -bool TerminalStrip::setBridge(const QVector> &real_terminals) +bool TerminalStrip::setBridge(const QVector> &real_terminals) { if (!isBridgeable(real_terminals)) { return false; } - QVector real_terminals_vector; + QVector> real_terminals_vector; for (const auto &real_terminal : real_terminals) { - const auto real_t = real_terminal.toStrongRef(); - if (real_t) { - real_terminals_vector.append(real_t); + if (real_terminal) { + real_terminals_vector.append(real_terminal); } } @@ -974,7 +512,7 @@ bool TerminalStrip::setBridge(const QVector> &real_te * @param real_terminals_data * @return true if all RealTerminal was successfully bridged */ -bool TerminalStrip::setBridge(const QSharedPointer &bridge, const QVector> &real_terminals) +bool TerminalStrip::setBridge(const QSharedPointer &bridge, const QVector> &real_terminals) { if (bridge) { @@ -983,9 +521,8 @@ bool TerminalStrip::setBridge(const QSharedPointer &bridge, } bool b_ = false; - for (const auto & rt_ : real_terminals) + for (const auto &real_t : real_terminals) { - const auto real_t = rt_.toStrongRef(); if (real_t && !bridge->real_terminals.contains(real_t)) { @@ -1009,13 +546,13 @@ bool TerminalStrip::setBridge(const QSharedPointer &bridge, * @sa TerminalStrip::canUnBridge * @param real_terminals */ -void TerminalStrip::unBridge(const QVector> &real_terminals) +void TerminalStrip::unBridge(const QVector> &real_terminals) { if (canUnBridge(real_terminals)) { - auto bridge_ = isBridged(real_terminals.first().toStrongRef()); + auto bridge_ = isBridged(real_terminals.first()); for (const auto &real_t : qAsConst(real_terminals)) { - bridge_->real_terminals.removeOne(real_t.toStrongRef()); + bridge_->real_terminals.removeOne(real_t); } emit bridgeChanged(); @@ -1029,26 +566,26 @@ void TerminalStrip::unBridge(const QVector> &real_ter * For this method return True, all terminals must be bridged together, * be consecutive and in an one or the both extremities of the bridge. */ -bool TerminalStrip::canUnBridge(const QVector > &real_terminals) const +bool TerminalStrip::canUnBridge(const QVector > &real_terminals) const { if (real_terminals.isEmpty()) { return false; } //Get the bridge of first terminal - const auto compar_bridge = isBridged(real_terminals.first().toStrongRef()); + const auto compar_bridge = isBridged(real_terminals.first()); if (compar_bridge) { - QMap> sorted_terminal; + QMap> sorted_terminal; //Check if all terminals are bridged and if it's the same bridge. //If true insert the terminal in sorted_terminal QMap //with for key the position of the parent physical terminal for (const auto &real_t : real_terminals) { - if (compar_bridge != isBridged(real_t.toStrongRef())) { + if (compar_bridge != isBridged(real_t)) { return false; } else { - sorted_terminal.insert(m_physical_terminals.indexOf(physicalTerminal(real_t.toStrongRef())), + sorted_terminal.insert(m_physical_terminals.indexOf(physicalTerminal(real_t)), real_t); } } @@ -1086,9 +623,52 @@ bool TerminalStrip::canUnBridge(const QVector > &real return false; } -QSharedPointer TerminalStrip::bridgeFor(const QWeakPointer &real_terminal) const +/** + * @brief TerminalStrip::isBridged + * Check if @a real_terminal is bridged + * @param real_terminal + * @return a pointer of TerminalStripBridge if bridget or a null QSharedPointer. + */ +QSharedPointer TerminalStrip::isBridged(const QSharedPointer real_terminal) const { - return this->isBridged(real_terminal.toStrongRef()); + if (real_terminal) + { + for (const auto &bridge_ : qAsConst(m_bridge)) { + if (bridge_->real_terminals.contains(real_terminal)) + return bridge_; + } + } + return QSharedPointer(); +} + +/** + * @brief TerminalStrip::bridgeFor + * Return the bridge where at least one terminal of @a terminal_vector belong. + * If several terminals are bridged but not to the same bridge return + * a TerminalStripBridge with 0 real_terminals_uuid_vector + * @sa TerminalStripBridge + * @param terminal_vector + * @return + */ +QSharedPointer TerminalStrip::bridgeFor(const QVector > &terminal_vector) const +{ + QSharedPointer return_bridge; + + for (const auto &terminal : terminal_vector) + { + const auto bridge_ = isBridged(terminal); + if (!bridge_.isNull()) + { + if (return_bridge.isNull()) { + return_bridge = bridge_; + } + else if (return_bridge != bridge_) { + return QSharedPointer(); + } + } + } + + return return_bridge; } /** @@ -1097,13 +677,12 @@ QSharedPointer TerminalStrip::bridgeFor(const QWeakPointer< * @return The previous real terminal at the samne level of @a real_t * or a null RealTerminalData if there not a previous real terminal */ -QWeakPointer TerminalStrip::previousTerminalInLevel(const QWeakPointer &real_terminal) const +QSharedPointer TerminalStrip::previousTerminalInLevel(const QSharedPointer &real_terminal) const { - const auto real_t = real_terminal.toStrongRef(); - const auto phy_t = physicalTerminal(real_t); - if (real_t && phy_t) + const auto phy_t = physicalTerminal(real_terminal); + if (real_terminal && phy_t) { - const auto level_ = phy_t->levelOf(real_t); + const auto level_ = phy_t->levelOf(real_terminal); const auto index = m_physical_terminals.indexOf(phy_t); if (index >= 1) { @@ -1114,7 +693,7 @@ QWeakPointer TerminalStrip::previousTerminalInLevel(const QWeakPoi } } - return QWeakPointer(); + return QSharedPointer(); } /** @@ -1123,13 +702,12 @@ QWeakPointer TerminalStrip::previousTerminalInLevel(const QWeakPoi * @return The next real terminal at the same level of @a real_t * or a null RealTerminalData if there not a next real terminal */ -QWeakPointer TerminalStrip::nextTerminalInLevel(const QWeakPointer &real_terminal) const +QSharedPointer TerminalStrip::nextTerminalInLevel(const QSharedPointer &real_terminal) const { - const auto real_t = real_terminal.toStrongRef(); - const auto phy_t = physicalTerminal(real_t); - if (real_t && phy_t) + const auto phy_t = physicalTerminal(real_terminal); + if (real_terminal && phy_t) { - const auto level_ = phy_t->levelOf(real_t); + const auto level_ = phy_t->levelOf(real_terminal); const auto index = m_physical_terminals.indexOf(phy_t); if (index < m_physical_terminals.size()-1) { @@ -1140,27 +718,25 @@ QWeakPointer TerminalStrip::nextTerminalInLevel(const QWeakPointer } } - return QWeakPointer(); + return QSharedPointer(); } -QWeakPointer TerminalStrip::previousRealTerminal(const QWeakPointer &real_terminal) const +QSharedPointer TerminalStrip::previousRealTerminal(const QSharedPointer &real_terminal) const { - const auto real = real_terminal.toStrongRef(); - const auto index = m_real_terminals.indexOf(real); + const auto index = m_real_terminals.indexOf(real_terminal); if (index) { return m_real_terminals.at(index-1); } - return QWeakPointer(); + return QSharedPointer(); } -QWeakPointer TerminalStrip::nextRealTerminal(const QWeakPointer &real_terminal) const +QSharedPointer TerminalStrip::nextRealTerminal(const QSharedPointer &real_terminal) const { - const auto real = real_terminal.toStrongRef(); - const auto index = m_real_terminals.indexOf(real); + const auto index = m_real_terminals.indexOf(real_terminal); if (index != m_real_terminals.size()-1) { return m_real_terminals.at(index+1); } - return QWeakPointer(); + return QSharedPointer(); } /** @@ -1246,95 +822,6 @@ bool TerminalStrip::fromXml(QDomElement &xml_element) 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) -{ - for (auto &real : qAsConst(m_real_terminals)) { - if (real->element() == terminal) { - return real; - } - } - - return shared_real_terminal(); -} - -/** - * @brief TerminalStrip::physicalTerminal - * @param terminal - * @return the physical terminal linked to \p terminal. - * The returned QSharedPointer can be null. - */ -QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer terminal) const -{ - shared_physical_terminal pt; - - if (terminal.isNull()) { - return pt; - } - - for (auto &physical : qAsConst(m_physical_terminals)) { - if (physical->realTerminals().contains(terminal)) { - pt = physical; - break; - } - } - - return pt; -} - -/** - * @brief TerminalStrip::isBridged - * Check if @a real_terminal is bridged - * @param real_terminal - * @return a pointer of TerminalStripBridge if bridget or a null QSharedPointer. - */ -QSharedPointer TerminalStrip::isBridged(const QSharedPointer real_terminal) const -{ - if (real_terminal) - { - for (const auto &bridge_ : qAsConst(m_bridge)) { - if (bridge_->real_terminals.contains(real_terminal)) - return bridge_; - } - } - return QSharedPointer(); -} - -/** - * @brief TerminalStrip::bridgeFor - * Return the bridge where at least one terminal of @a terminal_vector belong. - * If several terminals are bridged but not to the same bridge return - * a TerminalStripBridge with 0 real_terminals_uuid_vector - * @sa TerminalStripBridge - * @param terminal_vector - * @return - */ -QSharedPointer TerminalStrip::bridgeFor(const QVector > &terminal_vector) const -{ - QSharedPointer return_bridge; - - for (const auto &terminal : terminal_vector) - { - const auto bridge_ = isBridged(terminal); - if (!bridge_.isNull()) - { - if (return_bridge.isNull()) { - return_bridge = bridge_; - } - else if (return_bridge != bridge_) { - return QSharedPointer(); - } - } - } - - return return_bridge; -} - /** * @brief TerminalStrip::rebuildRealVector * Rebuild the real terminal vector @@ -1344,6 +831,6 @@ void TerminalStrip::rebuildRealVector() { m_real_terminals.clear(); for (const auto &phy : qAsConst(m_physical_terminals)) { - m_real_terminals.append(QETUtils::weakVectorToShared(phy->realTerminals())); + m_real_terminals.append(phy->realTerminals()); } } diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 96f4cdbb2..3171149f2 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -40,97 +40,6 @@ struct TerminalStripBridge QUuid uuid_ = QUuid::createUuid(); }; -/** - * @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). - * - * When create a new instance of RealTerminal you must - * call sharedRef() and only use the returned QSharedPointer - * instead of the raw pointer - */ -class RealTerminal -{ - friend class TerminalStrip; - friend class PhysicalTerminal; - - private: - RealTerminal(TerminalStrip *strip, Element *element = nullptr); - QSharedPointer sharedRef(); - QWeakPointer weakRef(); - - bool fromXml(QDomElement xml_element, const QVector &terminal_vector); - QDomElement toXml(QDomDocument &parent_document) const; - - public: - TerminalStrip *parentStrip() const; - int level() const; - QString label() const; - QString Xref() const; - QString cable() const; - QString cableWire() const; - QString conductor() const; - - ElementData::TerminalType type() const; - ElementData::TerminalFunction function() const; - - bool isLed() const; - bool isElement() const; - bool isBridged() const; - - QSharedPointer bridge() const; - - Element* element() const; - QUuid elementUuid() const; - QUuid uuid() const; - - static QString xmlTagName(); - - private : - QPointer m_element; - QPointer m_parent_terminal_strip; - QUuid m_uuid = QUuid::createUuid(); - QWeakPointer m_this_weak; -}; - -class PhysicalTerminal -{ - friend class TerminalStrip; - - private: - PhysicalTerminal(TerminalStrip *parent_strip, QVector> terminals); - QSharedPointer sharedRef(); - QWeakPointer weakRef(); - - QDomElement toXml(QDomDocument &parent_document) const; - - void setTerminals(const QVector> &terminals); - void addTerminal(const QSharedPointer &terminal); - bool removeTerminal(const QSharedPointer &terminal); - - bool setLevelOf(const QSharedPointer &terminal, int level); - - public: - PhysicalTerminal(){} - - int levelCount() const; - int levelOf(const QWeakPointer &terminal) const; - QVector> realTerminals() const; - QUuid uuid() const; - int pos() const; - int realTerminalCount() const; - - static QString xmlTagName(); - - private: - QPointer m_parent_terminal_strip; - QVector> m_real_terminal; - QUuid m_uuid = QUuid::createUuid(); - QWeakPointer m_this_weak; -}; - /** * @brief The TerminalStrip class * This class hold all the datas and configurations @@ -178,28 +87,30 @@ class TerminalStrip : public QObject bool addTerminal (Element *terminal); bool removeTerminal (Element *terminal); - int pos(const QWeakPointer &terminal) const; + int pos(const QSharedPointer &terminal) const; int physicalTerminalCount() const; - QWeakPointer physicalTerminal(int index) const; - QWeakPointer physicalTerminal (const QWeakPointer &real_terminal) const; - QVector> physicalTerminal() const; + QSharedPointer physicalTerminal(int index) const; + QSharedPointer physicalTerminal (const QSharedPointer &real_terminal) const; + QVector> physicalTerminal() const; + QSharedPointer realTerminal(Element *terminal) const; - bool setOrderTo(const QVector> &sorted_vector); - bool groupTerminals(const QWeakPointer &receiver_terminal, const QVector> &added_terminals); - void unGroupTerminals(const QVector> &terminals_to_ungroup); - bool setLevel(const QWeakPointer &real_terminal, int level); + bool setOrderTo(const QVector> &sorted_vector); + bool groupTerminals(const QSharedPointer &receiver_terminal, const QVector> &added_terminals); + void unGroupTerminals(const QVector> &terminals_to_ungroup); + bool setLevel(const QSharedPointer &real_terminal, int level); - bool isBridgeable(const QVector> &real_terminals) const; - bool setBridge(const QVector> &real_terminals); - bool setBridge(const QSharedPointer &bridge, const QVector> &real_terminals); - void unBridge(const QVector> &real_terminals); - bool canUnBridge(const QVector > &real_terminals) const; - QSharedPointer bridgeFor(const QWeakPointer &real_terminal) const; + bool isBridgeable(const QVector> &real_terminals) const; + bool setBridge(const QVector> &real_terminals); + bool setBridge(const QSharedPointer &bridge, const QVector> &real_terminals); + void unBridge(const QVector> &real_terminals); + bool canUnBridge(const QVector > &real_terminals) const; + QSharedPointer isBridged(const QSharedPointer real_terminal) const; + QSharedPointer bridgeFor (const QVector> &terminal_vector) const; - QWeakPointer previousTerminalInLevel(const QWeakPointer &real_terminal) const; - QWeakPointer nextTerminalInLevel(const QWeakPointer &real_terminal) const; - QWeakPointer previousRealTerminal(const QWeakPointer &real_terminal) const; - QWeakPointer nextRealTerminal(const QWeakPointer &real_terminal) const; + QSharedPointer previousTerminalInLevel(const QSharedPointer &real_terminal) const; + QSharedPointer nextTerminalInLevel(const QSharedPointer &real_terminal) const; + QSharedPointer previousRealTerminal(const QSharedPointer &real_terminal) const; + QSharedPointer nextRealTerminal(const QSharedPointer &real_terminal) const; QVector> terminalElement() const; @@ -208,10 +119,6 @@ class TerminalStrip : public QObject bool fromXml(QDomElement &xml_element); private: - QSharedPointer realTerminal(Element *terminal); - QSharedPointer physicalTerminal(QSharedPointer terminal) const; - QSharedPointer isBridged(const QSharedPointer real_terminal) const; - QSharedPointer bridgeFor (const QVector> &terminal_vector) const; void rebuildRealVector(); private: diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 4ca0e4ee7..d9e941b6e 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -35,6 +35,8 @@ #include "../UndoCommand/changeterminallevel.h" #include "../UndoCommand/bridgeterminalscommand.h" #include "../../utils/qetutils.h" +#include "../physicalterminal.h" +#include "../realterminal.h" #include @@ -232,10 +234,10 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s //Add child terminal of the strip for (auto i=0 ; iphysicalTerminalCount() ; ++i) { - auto phy_t = terminal_strip->physicalTerminal(i).toStrongRef(); + auto phy_t = terminal_strip->physicalTerminal(i); if (phy_t->realTerminalCount()) { - const auto real_t = phy_t->realTerminals().at(0).toStrongRef(); + const auto real_t = phy_t->realTerminals().at(0); auto terminal_item = new QTreeWidgetItem(strip_item, QStringList(real_t->label()), TerminalStripTreeWidget::Terminal); terminal_item->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t->elementUuid()); terminal_item->setIcon(0, QET::Icons::ElementTerminal); @@ -353,7 +355,7 @@ void TerminalStripEditor::spanMultiLevelTerminals() auto current_row = 0; for (auto i = 0 ; i < m_current_strip->physicalTerminalCount() ; ++i) { - const auto level_count = m_current_strip->physicalTerminal(i).toStrongRef()->realTerminalCount(); + const auto level_count = m_current_strip->physicalTerminal(i)->realTerminalCount(); if (level_count > 1) { ui->m_table_widget->setSpan(current_row, 0, level_count, 1); } @@ -434,13 +436,13 @@ void TerminalStripEditor::selectionChanged() { //Select only terminals of corresponding level cell selection QVector model_real_terminal_level_vector; - QVector> real_terminal_in_level_vector; + QVector> real_terminal_in_level_vector; for (const auto &mrtd : model_real_terminal_vector) { if (mrtd.level_ == level_) { model_real_terminal_level_vector.append(mrtd); - real_terminal_in_level_vector.append(mrtd.real_terminal); + real_terminal_in_level_vector.append(mrtd.real_terminal.toStrongRef()); } } enable_bridge = m_current_strip->isBridgeable(real_terminal_in_level_vector); @@ -829,11 +831,11 @@ void TerminalStripEditor::on_m_bridge_terminals_pb_clicked() const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); - QVector > match_vector; + QVector > match_vector; for (const auto &mrtd : mrtd_vector) { if (mrtd.level_ == level_) { - match_vector.append(mrtd.real_terminal); + match_vector.append(mrtd.real_terminal.toStrongRef()); } } @@ -862,12 +864,12 @@ void TerminalStripEditor::on_m_unbridge_terminals_pb_clicked() const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); - QVector> match_vector; + QVector> match_vector; for (const auto &mrtd : mrtd_vector) { if (mrtd.level_ == level_ && mrtd.bridged_) { - match_vector.append(mrtd.real_terminal); + match_vector.append(mrtd.real_terminal.toStrongRef()); } } m_project->undoStack()->push(new UnBridgeTerminalsCommand(m_current_strip, match_vector)); diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 9c7be8841..007129e86 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -18,6 +18,8 @@ #include "terminalstripmodel.h" #include "../terminalstrip.h" #include "../../qetgraphicsitem/element.h" +#include "../physicalterminal.h" +#include "../realterminal.h" #include #include #include @@ -481,9 +483,8 @@ void TerminalStripModel::fillPhysicalTerminalData() //Get all physical terminal if (m_terminal_strip) { - for (const auto &t_ : m_terminal_strip->physicalTerminal()) + for (const auto &phy_t : m_terminal_strip->physicalTerminal()) { - const auto phy_t = t_.toStrongRef(); modelPhysicalTerminalData mptd; mptd.pos_ = phy_t->pos(); mptd.uuid_ = phy_t->uuid(); @@ -642,15 +643,15 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const { if (mrtd.bridged_) { - auto bridge_ = m_terminal_strip->bridgeFor(mrtd.real_terminal); + auto bridge_ = m_terminal_strip->isBridged(mrtd.real_terminal); if (bridge_) { - const auto previous_t = m_terminal_strip->previousTerminalInLevel(mrtd.real_terminal).toStrongRef(); + const auto previous_t = m_terminal_strip->previousTerminalInLevel(mrtd.real_terminal); QSharedPointer previous_bridge; if (previous_t) previous_bridge = previous_t->bridge(); - const auto next_t = m_terminal_strip->nextTerminalInLevel(mrtd.real_terminal).toStrongRef(); + const auto next_t = m_terminal_strip->nextTerminalInLevel(mrtd.real_terminal); QSharedPointer next_bridge; if (next_t) next_bridge = next_t->bridge(); @@ -674,7 +675,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const //Check if we need to draw a none bridge pixmap //Check previous - auto phy_t = m_terminal_strip->physicalTerminal(mrtd.real_terminal).toStrongRef(); + auto phy_t = m_terminal_strip->physicalTerminal(mrtd.real_terminal); auto current_real_terminal = mrtd; auto current_phy_uuid = phy_t->uuid(); bool already_jumped_to_previous = false; @@ -688,7 +689,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid()) + if (current_phy_uuid == m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal)->uuid()) { if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { @@ -700,7 +701,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_previous = true; - current_phy_uuid = m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid(); + current_phy_uuid = m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal)->uuid(); if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { previous_data = current_real_terminal; @@ -723,7 +724,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const } //We are in the same physical terminal as previous loop - if (current_phy_uuid == m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid()) + if (current_phy_uuid == m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal)->uuid()) { if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { @@ -735,7 +736,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const break; } else { already_jumped_to_next = true; - current_phy_uuid = m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal).toStrongRef()->uuid(); + current_phy_uuid = m_terminal_strip->physicalTerminal(current_real_terminal.real_terminal)->uuid(); if (current_real_terminal.bridged_ && current_real_terminal.level_ == level_column) { next_data = current_real_terminal; @@ -744,8 +745,8 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const } } while(true); - auto previous_bridge = m_terminal_strip->bridgeFor(previous_data.real_terminal); - if (previous_bridge == m_terminal_strip->bridgeFor(next_data.real_terminal)) + auto previous_bridge = m_terminal_strip->isBridged(previous_data.real_terminal); + if (previous_bridge == m_terminal_strip->isBridged(next_data.real_terminal)) { if (previous_bridge) { return m_bridges_pixmaps.value(previous_bridge->color_).none_; From 1572fafabe42cc415af047fd19df402e532f4fbc Mon Sep 17 00:00:00 2001 From: joshua Date: Sun, 26 Dec 2021 18:43:17 +0100 Subject: [PATCH 12/14] Change struct TerminalStripBridge to class And move it in a new file --- sources/TerminalStrip/realterminal.h | 2 +- sources/TerminalStrip/terminalstrip.cpp | 46 +++------- sources/TerminalStrip/terminalstrip.h | 12 +-- sources/TerminalStrip/terminalstripbridge.cpp | 84 +++++++++++++++++++ sources/TerminalStrip/terminalstripbridge.h | 51 +++++++++++ .../TerminalStrip/ui/terminalstripeditor.cpp | 3 +- .../TerminalStrip/ui/terminalstripmodel.cpp | 7 +- 7 files changed, 156 insertions(+), 49 deletions(-) create mode 100644 sources/TerminalStrip/terminalstripbridge.cpp create mode 100644 sources/TerminalStrip/terminalstripbridge.h diff --git a/sources/TerminalStrip/realterminal.h b/sources/TerminalStrip/realterminal.h index 4a4ef07ad..36f0cc924 100644 --- a/sources/TerminalStrip/realterminal.h +++ b/sources/TerminalStrip/realterminal.h @@ -26,7 +26,7 @@ class TerminalStrip; class Element; class TerminalElement; class PhysicalTerminal; -struct TerminalStripBridge; +class TerminalStripBridge; /** * @brief The RealTerminal class diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index abd09ee32..e913dfb7a 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -22,9 +22,9 @@ #include "../elementprovider.h" #include "../qetxml.h" #include "../autoNum/assignvariables.h" -#include "../../utils/qetutils.h" #include "physicalterminal.h" #include "realterminal.h" +#include "terminalstripbridge.h" using shared_real_terminal = QSharedPointer; using shared_physical_terminal = QSharedPointer; @@ -480,29 +480,20 @@ bool TerminalStrip::setBridge(const QVector> &real_ if (!isBridgeable(real_terminals)) { return false; } - QVector> real_terminals_vector; - for (const auto &real_terminal : real_terminals) + auto bridge = bridgeFor(real_terminals); + if (bridge.isNull()) { - if (real_terminal) { - real_terminals_vector.append(real_terminal); - } - } - - auto bridge = bridgeFor(real_terminals_vector); - if (bridge.isNull()) { - bridge = QSharedPointer::create(); + bridge = QSharedPointer(new TerminalStripBridge(this)); m_bridge.append(bridge); } - for (const auto &real_t : qAsConst(real_terminals_vector)) + if (bridge->addTerminals(real_terminals)) { - if (!bridge->real_terminals.contains(real_t)) - bridge->real_terminals.append(real_t); + emit bridgeChanged();; + return true; } - - emit bridgeChanged(); - return true; + return false; } /** @@ -520,18 +511,8 @@ bool TerminalStrip::setBridge(const QSharedPointer &bridge, return false; } - bool b_ = false; - for (const auto &real_t : real_terminals) + if (bridge->addTerminals(real_terminals)) { - if (real_t && - !bridge->real_terminals.contains(real_t)) - { - bridge->real_terminals.append(real_t); - b_ = true; - } - } - - if (b_) { emit bridgeChanged(); return true; } @@ -551,10 +532,7 @@ void TerminalStrip::unBridge(const QVector> &real_t if (canUnBridge(real_terminals)) { auto bridge_ = isBridged(real_terminals.first()); - for (const auto &real_t : qAsConst(real_terminals)) { - bridge_->real_terminals.removeOne(real_t); - } - + bridge_->removeTerminals(real_terminals); emit bridgeChanged(); } } @@ -564,7 +542,7 @@ void TerminalStrip::unBridge(const QVector> &real_t * @param m_real_terminals * @return True if all terminals of @a real_terminals can be unbridged. * For this method return True, all terminals must be bridged together, - * be consecutive and in an one or the both extremities of the bridge. + * be consecutive and in one or the both extremities of the bridge. */ bool TerminalStrip::canUnBridge(const QVector > &real_terminals) const { @@ -634,7 +612,7 @@ QSharedPointer TerminalStrip::isBridged(const QSharedPointe if (real_terminal) { for (const auto &bridge_ : qAsConst(m_bridge)) { - if (bridge_->real_terminals.contains(real_terminal)) + if (bridge_->realTerminals().contains(real_terminal)) return bridge_; } } diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 3171149f2..5837ac7f4 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -32,13 +32,7 @@ class PhysicalTerminal; class TerminalStripIndex; class TerminalElement; class TerminalStrip; - -struct TerminalStripBridge -{ - QVector> real_terminals; - QColor color_ = Qt::darkGray; - QUuid uuid_ = QUuid::createUuid(); -}; +class TerminalStripBridge; /** * @brief The TerminalStrip class @@ -55,13 +49,11 @@ class TerminalStrip : public QObject Q_OBJECT public: - static QVector bridgeColor() {return QVector{Qt::red, Qt::blue, Qt::white, Qt::darkGray, Qt::black};} - signals: void orderChanged(); //Emitted when the order of the physical terminal is changed void bridgeChanged(); - public: + TerminalStrip(QETProject *project); TerminalStrip(const QString &installation, diff --git a/sources/TerminalStrip/terminalstripbridge.cpp b/sources/TerminalStrip/terminalstripbridge.cpp new file mode 100644 index 000000000..c6c87a029 --- /dev/null +++ b/sources/TerminalStrip/terminalstripbridge.cpp @@ -0,0 +1,84 @@ +/* + 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 "terminalstripbridge.h" +#include "realterminal.h" +#include "terminalstrip.h" + +TerminalStripBridge::TerminalStripBridge(TerminalStrip *parent_strip) : + m_strip(parent_strip) +{} + +/** + * @brief TerminalStripBridge::color + * @return The color of this bridge + */ +QColor TerminalStripBridge::color() const { + return m_color; +} + +/** + * @brief TerminalStripBridge::realTerminals + * @return the real terminals who are bridged by this bridge + */ +QVector > TerminalStripBridge::realTerminals() const { + return m_real_terminals; +} + +/** + * @brief TerminalStripBridge::addTerminals + * @param real_terminals + * @return Add terminals of @a real_terminals to this bridge. + * If a terminal is already bridged by this bridge, the terminal is ignored. + * If at least one terminal doesn't belong to the same strip of this bridge + * this function do nothing and return false. + */ +bool TerminalStripBridge::addTerminals(const QVector > &real_terminals) +{ + QVector> to_append; + for (const auto &real_t : real_terminals) + { + if (!real_t.isNull()) + { + if (real_t->parentStrip() != m_strip) { + return false; + } + if (!m_real_terminals.contains(real_t)) { + to_append.append(real_t); + } + } else { + return false; + } + } + + m_real_terminals.append(to_append); + return true; +} + +/** + * @brief TerminalStripBridge::removeTerminals + * @param real_terminal + * Remove all real terminal of @real_terminals from this bridge. + * This function doesn't make any check, they just remove if exist. + * @sa TerminalStrip::canUnBridge + */ +void TerminalStripBridge::removeTerminals(const QVector> &real_terminals) +{ + for (const auto &real_t : real_terminals) { + m_real_terminals.removeOne(real_t); + } +} diff --git a/sources/TerminalStrip/terminalstripbridge.h b/sources/TerminalStrip/terminalstripbridge.h new file mode 100644 index 000000000..715bd7755 --- /dev/null +++ b/sources/TerminalStrip/terminalstripbridge.h @@ -0,0 +1,51 @@ +/* + 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 TERMINALSTRIPBRIDGE_H +#define TERMINALSTRIPBRIDGE_H + +#include +#include +#include +#include + +class RealTerminal; +class TerminalStrip; + +class TerminalStripBridge +{ + public: + static QVector bridgeColor() {return QVector{Qt::red, Qt::blue, Qt::white, Qt::darkGray, Qt::black};} + + TerminalStripBridge(TerminalStrip *parent_strip = nullptr); + + QColor color() const; + QVector> realTerminals() const; + bool addTerminals(const QVector> &real_terminals); + void removeTerminals(const QVector> &real_terminals); + + + private: + QPointer m_strip; + QVector> m_real_terminals; + QColor m_color = Qt::darkGray; + QUuid m_uuid = QUuid::createUuid(); +}; + + + +#endif // TERMINALSTRIPBRIDGE_H diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index d9e941b6e..c8afaf339 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -37,6 +37,7 @@ #include "../../utils/qetutils.h" #include "../physicalterminal.h" #include "../realterminal.h" +#include "../terminalstripbridge.h" #include @@ -63,7 +64,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : #endif //Setup the bridge color - ui->m_bridge_color_cb->setColors(TerminalStrip::bridgeColor().toList()); + ui->m_bridge_color_cb->setColors(TerminalStripBridge::bridgeColor().toList()); setUpUndoConnections(); diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 007129e86..83a97718c 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -20,6 +20,7 @@ #include "../../qetgraphicsitem/element.h" #include "../physicalterminal.h" #include "../realterminal.h" +#include "../terminalstripbridge.h" #include #include #include @@ -416,7 +417,7 @@ modelRealTerminalData TerminalStripModel::modelRealTerminalDataForIndex(const QM void TerminalStripModel::buildBridgePixmap(const QSize &pixmap_size) { m_bridges_pixmaps.clear(); - for (auto color_ : TerminalStrip::bridgeColor()) + for (auto color_ : TerminalStripBridge::bridgeColor()) { QPen pen; pen.setColor(color_); @@ -656,7 +657,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const if (next_t) next_bridge = next_t->bridge(); - auto color_ = bridge_->color_; + auto color_ = bridge_->color(); auto pixmap_ = m_bridges_pixmaps.value(color_); //Current real terminal between two bridged terminal @@ -749,7 +750,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const if (previous_bridge == m_terminal_strip->isBridged(next_data.real_terminal)) { if (previous_bridge) { - return m_bridges_pixmaps.value(previous_bridge->color_).none_; + return m_bridges_pixmaps.value(previous_bridge->color()).none_; } } From 09694ddec909f2f4991e7fa8a14693615064c0a6 Mon Sep 17 00:00:00 2001 From: joshua Date: Sun, 26 Dec 2021 18:48:11 +0100 Subject: [PATCH 13/14] Use QSharedPointer instead of QWeakPointer + remove unused include --- .../UndoCommand/groupterminalscommand.cpp | 1 - .../UndoCommand/sortterminalstripcommand.cpp | 1 - sources/TerminalStrip/ui/terminalstripeditor.cpp | 14 ++++++-------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp index 3ddb621e4..e3a74d0bf 100644 --- a/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp +++ b/sources/TerminalStrip/UndoCommand/groupterminalscommand.cpp @@ -16,7 +16,6 @@ along with QElectroTech. If not, see . */ #include "groupterminalscommand.h" -#include "../../utils/qetutils.h" #include "../physicalterminal.h" /** diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp index 6ce4a0862..50eb4ff45 100644 --- a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp @@ -19,7 +19,6 @@ #include "../terminalstrip.h" #include "../physicalterminal.h" #include "../realterminal.h" -#include "../../utils/qetutils.h" SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) : QUndoCommand(parent), diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index c8afaf339..7de7b958f 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -34,7 +34,6 @@ #include "../UndoCommand/groupterminalscommand.h" #include "../UndoCommand/changeterminallevel.h" #include "../UndoCommand/bridgeterminalscommand.h" -#include "../../utils/qetutils.h" #include "../physicalterminal.h" #include "../realterminal.h" #include "../terminalstripbridge.h" @@ -683,13 +682,13 @@ void TerminalStripEditor::on_m_group_terminals_pb_clicked() { auto receiver_ = m_current_strip->physicalTerminal(mrtd_vector.takeFirst().real_terminal); - QVector> vector_; + QVector> vector_; for (const auto & mrtd : mrtd_vector) { - vector_.append(mrtd.real_terminal); + vector_.append(mrtd.real_terminal.toStrongRef()); } m_project->undoStack()->push(new GroupTerminalsCommand(m_current_strip, receiver_, - QETUtils::weakVectorToShared(vector_))); + vector_)); } } } @@ -703,12 +702,11 @@ void TerminalStripEditor::on_m_ungroup_pb_clicked() { const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); - QVector> vector_; + QVector> vector_; for (const auto &mrtd : mrtd_vector) { - vector_.append(mrtd.real_terminal); + vector_.append(mrtd.real_terminal.toStrongRef()); } - m_project->undoStack()->push(new UnGroupTerminalsCommand(m_current_strip, - QETUtils::weakVectorToShared(vector_))); + m_project->undoStack()->push(new UnGroupTerminalsCommand(m_current_strip,vector_)); } } From 683095173ef21a031f1457be398e3b07b45f3ca3 Mon Sep 17 00:00:00 2001 From: joshua Date: Tue, 18 Jan 2022 11:24:07 +0100 Subject: [PATCH 14/14] TerminalStripBridge color can be edited. --- .../UndoCommand/changeterminalstripcolor.cpp | 51 +++++++++++++++++ .../UndoCommand/changeterminalstripcolor.h | 45 +++++++++++++++ sources/TerminalStrip/terminalstrip.cpp | 3 +- sources/TerminalStrip/terminalstrip.h | 1 + sources/TerminalStrip/terminalstripbridge.cpp | 31 ++++++++++ sources/TerminalStrip/terminalstripbridge.h | 8 +++ .../TerminalStrip/ui/terminalstripeditor.cpp | 57 ++++++++++++++++--- .../TerminalStrip/ui/terminalstripeditor.h | 1 + .../TerminalStrip/ui/terminalstripmodel.cpp | 5 ++ 9 files changed, 193 insertions(+), 9 deletions(-) create mode 100644 sources/TerminalStrip/UndoCommand/changeterminalstripcolor.cpp create mode 100644 sources/TerminalStrip/UndoCommand/changeterminalstripcolor.h diff --git a/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.cpp b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.cpp new file mode 100644 index 000000000..17507f159 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.cpp @@ -0,0 +1,51 @@ +/* + 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 "changeterminalstripcolor.h" +#include "../terminalstripbridge.h" + +/** + * @brief ChangeTerminalStripColor::ChangeTerminalStripColor + * @param bridge + * @param color + * @param parent + */ +ChangeTerminalStripColor::ChangeTerminalStripColor(QSharedPointer bridge, + const QColor &color, + QUndoCommand *parent): + QUndoCommand(parent), + m_bridge(bridge), + m_new_color(color) +{ + if (m_bridge) { + m_old_color = m_bridge->color(); + } +} + +void ChangeTerminalStripColor::redo() +{ + if (m_bridge) { + m_bridge->setColor(m_new_color); + } +} + +void ChangeTerminalStripColor::undo() +{ + if (m_bridge) { + m_bridge->setColor(m_old_color); + } +} diff --git a/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.h b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.h new file mode 100644 index 000000000..bb7b372a5 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.h @@ -0,0 +1,45 @@ +/* + 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 CHANGETERMINALSTRIPCOLOR_H +#define CHANGETERMINALSTRIPCOLOR_H + +#include +#include +#include + +class TerminalStripBridge; + +/** + * @brief The ChangeTerminalStripColor class + */ +class ChangeTerminalStripColor : public QUndoCommand +{ + public: + ChangeTerminalStripColor(QSharedPointer bridge, + const QColor &color, + QUndoCommand *parent = nullptr); + + void redo() override; + void undo() override; + + private: + QSharedPointer m_bridge; + QColor m_old_color, m_new_color; +}; + +#endif // CHANGETERMINALSTRIPCOLOR_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index e913dfb7a..04898f92f 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -484,7 +484,8 @@ bool TerminalStrip::setBridge(const QVector> &real_ auto bridge = bridgeFor(real_terminals); if (bridge.isNull()) { - bridge = QSharedPointer(new TerminalStripBridge(this)); + auto br_ = new TerminalStripBridge(this); + bridge = br_->sharedRef(); m_bridge.append(bridge); } diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 5837ac7f4..eae42117f 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -52,6 +52,7 @@ class TerminalStrip : public QObject signals: void orderChanged(); //Emitted when the order of the physical terminal is changed void bridgeChanged(); + void bridgeColorChanged(QSharedPointer bridge); public: TerminalStrip(QETProject *project); diff --git a/sources/TerminalStrip/terminalstripbridge.cpp b/sources/TerminalStrip/terminalstripbridge.cpp index c6c87a029..d810ad85d 100644 --- a/sources/TerminalStrip/terminalstripbridge.cpp +++ b/sources/TerminalStrip/terminalstripbridge.cpp @@ -23,6 +23,30 @@ TerminalStripBridge::TerminalStripBridge(TerminalStrip *parent_strip) : m_strip(parent_strip) {} +/** + * @brief TerminalStripBridge::sharedRef + * @return a QSharedPointer of this + */ +QSharedPointer TerminalStripBridge::sharedRef() +{ + QSharedPointer this_shared(this->weakRef()); + if (this_shared.isNull()) + { + this_shared = QSharedPointer(this); + m_this_weak = this_shared.toWeakRef(); + } + + return this_shared; +} + +/** + * @brief TerminalStripBridge::weakRef + * @return a QWeakPointer of this, weak pointer can be null + */ +QWeakPointer TerminalStripBridge::weakRef() { + return m_this_weak; +} + /** * @brief TerminalStripBridge::color * @return The color of this bridge @@ -31,6 +55,13 @@ QColor TerminalStripBridge::color() const { return m_color; } +void TerminalStripBridge::setColor(const QColor &color) { + m_color = color; + if (m_strip) { + m_strip->bridgeColorChanged(sharedRef()); + } +} + /** * @brief TerminalStripBridge::realTerminals * @return the real terminals who are bridged by this bridge diff --git a/sources/TerminalStrip/terminalstripbridge.h b/sources/TerminalStrip/terminalstripbridge.h index 715bd7755..082e49fb0 100644 --- a/sources/TerminalStrip/terminalstripbridge.h +++ b/sources/TerminalStrip/terminalstripbridge.h @@ -28,13 +28,20 @@ class TerminalStrip; class TerminalStripBridge { + friend class TerminalStrip; + public: static QVector bridgeColor() {return QVector{Qt::red, Qt::blue, Qt::white, Qt::darkGray, Qt::black};} TerminalStripBridge(TerminalStrip *parent_strip = nullptr); + QSharedPointer sharedRef(); + QWeakPointer weakRef(); QColor color() const; + void setColor(const QColor &color); QVector> realTerminals() const; + + private: bool addTerminals(const QVector> &real_terminals); void removeTerminals(const QVector> &real_terminals); @@ -44,6 +51,7 @@ class TerminalStripBridge QVector> m_real_terminals; QColor m_color = Qt::darkGray; QUuid m_uuid = QUuid::createUuid(); + QWeakPointer m_this_weak; }; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 7de7b958f..1b226c410 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -34,6 +34,7 @@ #include "../UndoCommand/groupterminalscommand.h" #include "../UndoCommand/changeterminallevel.h" #include "../UndoCommand/bridgeterminalscommand.h" +#include "../UndoCommand/changeterminalstripcolor.h" #include "../physicalterminal.h" #include "../realterminal.h" #include "../terminalstripbridge.h" @@ -429,20 +430,21 @@ void TerminalStripEditor::selectionChanged() //Enable/disable bridge and unbridge bool enable_bridge = false; bool enable_unbridge = false; + bool enable_bridge_color = false; //One column must be selected and the column must be a level column int level_ = TerminalStripModel::levelForColumn(isSingleColumnSelected()); if (level_ >= 0 && m_current_strip) { //Select only terminals of corresponding level cell selection - QVector model_real_terminal_level_vector; QVector> real_terminal_in_level_vector; for (const auto &mrtd : model_real_terminal_vector) { - if (mrtd.level_ == level_) - { - model_real_terminal_level_vector.append(mrtd); + if (mrtd.level_ == level_) { real_terminal_in_level_vector.append(mrtd.real_terminal.toStrongRef()); + if (!enable_bridge_color && mrtd.bridged_) { + enable_bridge_color = true; + } } } enable_bridge = m_current_strip->isBridgeable(real_terminal_in_level_vector); @@ -450,6 +452,7 @@ void TerminalStripEditor::selectionChanged() } ui->m_bridge_terminals_pb->setEnabled(enable_bridge); ui->m_unbridge_terminals_pb->setEnabled(enable_unbridge); + ui->m_bridge_color_cb->setEnabled(enable_bridge_color); } QSize TerminalStripEditor::setUpBridgeCellWidth() @@ -476,7 +479,7 @@ QSize TerminalStripEditor::setUpBridgeCellWidth() * If all current QModelIndex are in the same column * return the column type * @sa TerminalStripModel::Column - * @return + * @return the column or TerminalStripModel::Invalid if several column are selected */ TerminalStripModel::Column TerminalStripEditor::isSingleColumnSelected() const { @@ -501,6 +504,29 @@ TerminalStripModel::Column TerminalStripEditor::isSingleColumnSelected() const return TerminalStripModel::Invalid; } +/** + * @brief TerminalStripEditor::singleColumnData + * @return a QPair with for first value the column and for second value the data + * of selected cell of the table widget, only if the selected cells are + * in the same column. If selected cells are not in the same column the first value + * of the QPair is TerminalStripModel::Invalid. + */ +QPair > TerminalStripEditor::singleColumnData() const +{ + if (m_current_strip) + { + auto level_ = isSingleColumnSelected(); + if (level_ != TerminalStripModel::Invalid) + { + const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); + const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); + return qMakePair(level_, mrtd_vector); + } + } + + return qMakePair(TerminalStripModel::Invalid, QVector()); +} + /** * @brief TerminalStripEditor::on_m_add_terminal_strip_pb_clicked * Action when user click on add terminal strip button @@ -829,7 +855,6 @@ void TerminalStripEditor::on_m_bridge_terminals_pb_clicked() const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); - QVector > match_vector; for (const auto &mrtd : mrtd_vector) { @@ -879,6 +904,22 @@ void TerminalStripEditor::on_m_unbridge_terminals_pb_clicked() void TerminalStripEditor::on_m_bridge_color_cb_activated(const QColor &col) { - + const auto data_vector = singleColumnData(); + const auto column_ = data_vector.first; + if (column_ == TerminalStripModel::Level0 || + column_ == TerminalStripModel::Level1 || + column_ == TerminalStripModel::Level2 || + column_ == TerminalStripModel::Level3) + { + const auto level_ = TerminalStripModel::levelForColumn(column_); + for (const auto &mrtd : data_vector.second) + { + if (mrtd.level_ == level_ && mrtd.bridged_) { + auto bridge_ = mrtd.real_terminal.toStrongRef()->bridge(); + m_project->undoStack()->push(new ChangeTerminalStripColor(bridge_, col)); + break; + } + } + } } diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index 7f73e1f17..ed7a21c7c 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -55,6 +55,7 @@ class TerminalStripEditor : public QDialog void selectionChanged(); QSize setUpBridgeCellWidth(); TerminalStripModel::Column isSingleColumnSelected() const; + QPair> singleColumnData() const; private slots: void on_m_add_terminal_strip_pb_clicked(); diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 83a97718c..16085fd83 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -109,6 +109,11 @@ TerminalStripModel::TerminalStripModel(TerminalStrip *terminal_strip, QObject *p m_terminal_strip(terminal_strip) { fillPhysicalTerminalData(); + + connect(terminal_strip, &TerminalStrip::bridgeColorChanged, this, [=] { + emit dataChanged(index(0, LEVEL_0_CELL), + index(rowCount(), LEVEL_3_CELL)); + }); } int TerminalStripModel::rowCount(const QModelIndex &parent) const