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_;