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