Revamp PhysicalTerminal class

This commit is contained in:
joshua 2021-12-23 22:17:37 +01:00
parent a2e5989f3b
commit 2ea9f8a2c6
9 changed files with 303 additions and 315 deletions

View File

@ -16,6 +16,7 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "groupterminalscommand.h" #include "groupterminalscommand.h"
#include "../../utils/qetutils.h"
/** /**
* @brief GroupTerminalsCommand::GroupTerminalsCommand * @brief GroupTerminalsCommand::GroupTerminalsCommand
@ -24,8 +25,8 @@
* @param to_group : Terminals to group * @param to_group : Terminals to group
*/ */
GroupTerminalsCommand::GroupTerminalsCommand(TerminalStrip *strip, GroupTerminalsCommand::GroupTerminalsCommand(TerminalStrip *strip,
const PhysicalTerminalData &receiver_, const QSharedPointer<PhysicalTerminal> &receiver_,
const QVector<QWeakPointer<RealTerminal>> &to_group, const QVector<QSharedPointer<RealTerminal>> &to_group,
QUndoCommand *parent): QUndoCommand *parent):
QUndoCommand(parent), QUndoCommand(parent),
m_terminal_strip(strip), m_terminal_strip(strip),
@ -37,18 +38,18 @@ GroupTerminalsCommand::GroupTerminalsCommand(TerminalStrip *strip,
void GroupTerminalsCommand::undo() { void GroupTerminalsCommand::undo() {
if (m_terminal_strip) { if (m_terminal_strip) {
m_terminal_strip->unGroupTerminals(m_to_group); m_terminal_strip->unGroupTerminals(QETUtils::sharedVectorToWeak(m_to_group));
} }
} }
void GroupTerminalsCommand::redo() { void GroupTerminalsCommand::redo() {
if (m_terminal_strip) { 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, UnGroupTerminalsCommand::UnGroupTerminalsCommand(TerminalStrip *strip,
const QVector<QWeakPointer<RealTerminal>> &to_ungroup, const QVector<QSharedPointer<RealTerminal>> &to_ungroup,
QUndoCommand *parent) : QUndoCommand *parent) :
QUndoCommand(parent), QUndoCommand(parent),
m_terminal_strip(strip) m_terminal_strip(strip)
@ -62,7 +63,7 @@ void UnGroupTerminalsCommand::undo()
if (m_terminal_strip) if (m_terminal_strip)
{ {
for (const auto &key : m_physical_real_H.keys()) { 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) if (m_terminal_strip)
{ {
for (const auto &value : qAsConst(m_physical_real_H)) { 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<QWeakPointer<RealTerminal>> &to_ungroup) void UnGroupTerminalsCommand::setUp(const QVector<QSharedPointer<RealTerminal>> &to_ungroup)
{ {
for (const auto &rt_ : 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 auto vector_ = m_physical_real_H.value(phy_t);
if (ptd_.realTerminalCount() <= 1) { vector_.append(rt_);
continue; 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_);
} }
} }

View File

@ -31,8 +31,8 @@ class GroupTerminalsCommand : public QUndoCommand
{ {
public: public:
GroupTerminalsCommand(TerminalStrip *strip, GroupTerminalsCommand(TerminalStrip *strip,
const PhysicalTerminalData &receiver_, const QSharedPointer<PhysicalTerminal> &receiver_,
const QVector<QWeakPointer<RealTerminal>> &to_group, const QVector<QSharedPointer<RealTerminal>> &to_group,
QUndoCommand *parent = nullptr); QUndoCommand *parent = nullptr);
void undo() override; void undo() override;
@ -40,8 +40,8 @@ class GroupTerminalsCommand : public QUndoCommand
private: private:
QPointer<TerminalStrip> m_terminal_strip; QPointer<TerminalStrip> m_terminal_strip;
PhysicalTerminalData m_receiver; QSharedPointer<PhysicalTerminal> m_receiver;
QVector<QWeakPointer<RealTerminal>> m_to_group; QVector<QSharedPointer<RealTerminal>> m_to_group;
}; };
/** /**
@ -52,18 +52,18 @@ class UnGroupTerminalsCommand : public QUndoCommand
{ {
public: public:
UnGroupTerminalsCommand(TerminalStrip *strip, UnGroupTerminalsCommand(TerminalStrip *strip,
const QVector<QWeakPointer<RealTerminal>> &to_ungroup, const QVector<QSharedPointer<RealTerminal>> &to_ungroup,
QUndoCommand *parent = nullptr); QUndoCommand *parent = nullptr);
void undo() override; void undo() override;
void redo() override; void redo() override;
private: private:
void setUp(const QVector<QWeakPointer<RealTerminal>> &to_ungroup); void setUp(const QVector<QSharedPointer<RealTerminal>> &to_ungroup);
private: private:
QPointer<TerminalStrip> m_terminal_strip; QPointer<TerminalStrip> m_terminal_strip;
QHash <PhysicalTerminalData, QVector<QWeakPointer<RealTerminal>>> m_physical_real_H; QHash <QSharedPointer<PhysicalTerminal>, QVector<QSharedPointer<RealTerminal>>> m_physical_real_H;
}; };
#endif // GROUPTERMINALSCOMMAND_H #endif // GROUPTERMINALSCOMMAND_H

View File

@ -17,33 +17,35 @@
*/ */
#include "sortterminalstripcommand.h" #include "sortterminalstripcommand.h"
#include "../terminalstrip.h" #include "../terminalstrip.h"
#include "../../utils/qetutils.h"
SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) : SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) :
QUndoCommand(parent), QUndoCommand(parent),
m_strip(strip) m_strip(strip)
{ {
setText(QObject::tr("Trier le bornier %1").arg(m_strip->name())); 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(); sort();
} }
void SortTerminalStripCommand::undo() void SortTerminalStripCommand::undo()
{ {
if (m_strip) { if (m_strip) {
m_strip->setOrderTo(m_old_order); m_strip->setOrderTo(QETUtils::sharedVectorToWeak(m_old_order));
} }
} }
void SortTerminalStripCommand::redo() void SortTerminalStripCommand::redo()
{ {
if (m_strip) { if (m_strip) {
m_strip->setOrderTo(m_new_order); m_strip->setOrderTo(QETUtils::sharedVectorToWeak(m_new_order));
} }
} }
void SortTerminalStripCommand::sort() 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<PhysicalTerminal> arg1, QSharedPointer<PhysicalTerminal> arg2)
{ {
const QRegularExpression rx(QStringLiteral("^\\d+")); const QRegularExpression rx(QStringLiteral("^\\d+"));
@ -52,9 +54,9 @@ void SortTerminalStripCommand::sort()
int int1 =-1; int int1 =-1;
int int2 =-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); auto match = rx.match(str1);
if (match.hasMatch()) { 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); auto match = rx.match(str2);
if (match.hasMatch()) { if (match.hasMatch()) {

View File

@ -23,7 +23,7 @@
#include <QVector> #include <QVector>
class TerminalStrip; class TerminalStrip;
class PhysicalTerminalData; class PhysicalTerminal;
/** /**
* @brief The SortTerminalStripCommand class * @brief The SortTerminalStripCommand class
@ -43,8 +43,8 @@ class SortTerminalStripCommand : public QUndoCommand
private: private:
QPointer<TerminalStrip> m_strip; QPointer<TerminalStrip> m_strip;
QVector<PhysicalTerminalData> m_old_order, QVector<QSharedPointer<PhysicalTerminal>> m_old_order,
m_new_order; m_new_order;
}; };
#endif // SORTTERMINALSTRIPCOMMAND_H #endif // SORTTERMINALSTRIPCOMMAND_H

View File

@ -22,6 +22,7 @@
#include "../elementprovider.h" #include "../elementprovider.h"
#include "../qetxml.h" #include "../qetxml.h"
#include "../autoNum/assignvariables.h" #include "../autoNum/assignvariables.h"
#include "../../utils/qetutils.h"
using shared_real_terminal = QSharedPointer<RealTerminal>; using shared_real_terminal = QSharedPointer<RealTerminal>;
using shared_physical_terminal = QSharedPointer<PhysicalTerminal>; using shared_physical_terminal = QSharedPointer<PhysicalTerminal>;
@ -61,7 +62,7 @@ QSharedPointer<RealTerminal> RealTerminal::sharedRef()
/** /**
* @brief RealTerminal::weakRef * @brief RealTerminal::weakRef
* @return a QWeakPointer of this * @return a QWeakPointer of this, weak pointer can be bull
*/ */
QWeakPointer<RealTerminal> RealTerminal::weakRef() { QWeakPointer<RealTerminal> RealTerminal::weakRef() {
return m_this_weak; return m_this_weak;
@ -128,9 +129,9 @@ TerminalStrip *RealTerminal::parentStrip() const {
int RealTerminal::level() const int RealTerminal::level() const
{ {
if (m_parent_terminal_strip) { if (m_parent_terminal_strip) {
const auto phy_t = m_parent_terminal_strip->physicalTerminalData(m_this_weak); const auto phy_t = m_parent_terminal_strip->physicalTerminal(m_this_weak);
if (!phy_t.isNull()) { if (phy_t) {
return phy_t.realTerminals().indexOf(m_this_weak); return phy_t.toStrongRef()->levelOf(m_this_weak);
} }
} }
@ -334,133 +335,166 @@ QString RealTerminal::RealTerminal::xmlTagName() {
* @sa PhysicalTerminalData * @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<QSharedPointer<RealTerminal>> terminals) :
m_parent_terminal_strip(parent_strip),
m_real_terminal(terminals)
{}
/**
* @brief PhysicalTerminal::sharedRef
* @return a QSharedPointer of this
*/
QSharedPointer<PhysicalTerminal> PhysicalTerminal::sharedRef()
{ {
public: QSharedPointer<PhysicalTerminal> this_shared(this->weakRef());
/** if (this_shared.isNull())
* @brief PhysicalTerminal {
* @param parent_strip : Parent terminal strip this_shared = QSharedPointer<PhysicalTerminal>(this);
* @param terminals : A vector of real terminals m_this_weak = this_shared.toWeakRef();
* who compose this physical terminal. }
* \p terminals must have at least one terminal
*/
PhysicalTerminal(TerminalStrip *parent_strip,
QVector<shared_real_terminal> terminals) :
m_parent_terminal_strip(parent_strip),
m_real_terminal(terminals)
{}
/** return this_shared;
* @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<shared_real_terminal> terminals) {
m_real_terminal = terminals;
}
/** /**
* @brief addTerminals * @brief PhysicalTerminal::weakRef
* Append the real terminal @a terminal * @return a QWeakPointer of this, weak pointer can be null
* to this physical terminal. */
* @param terminal QWeakPointer<PhysicalTerminal> PhysicalTerminal::weakRef() {
*/ return m_this_weak;
void addTerminal(shared_real_terminal terminal) { }
m_real_terminal.append(terminal);
}
/** /**
* @brief removeTerminal * @brief toXml
* Remove @a terminal from the list of real terminal * @param parent_document
* @param terminal * @return this physical terminal to xml
* @return true if sucessfully removed */
*/ QDomElement PhysicalTerminal::toXml(QDomDocument &parent_document) const
bool removeTerminal(shared_real_terminal terminal) { {
return m_real_terminal.removeOne(terminal); 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 levelCount }
* @return the number of level of this terminal
*/
int levelCount() const {
return m_real_terminal.size();
}
/** /**
* @brief levelOf * @brief setTerminals
* @param terminal * Set the RealTerminal who compose this physical terminal.
* @return the level of real terminal \p terminal or * The position of the RealTerminal in @a terminals
* -1 if \terminal is not a part of this physicalTerminal * represent the level of these in this physical terminal.
*/ * @param terminals
int levelOf(shared_real_terminal terminal) const { */
return m_real_terminal.indexOf(terminal); void PhysicalTerminal::setTerminals(const QVector<QSharedPointer<RealTerminal>> &terminals) {
} m_real_terminal = terminals;
}
/** /**
* @brief setLevelOf * @brief addTerminals
* Change the level of \p terminal * Append the real terminal @a terminal
* @param terminal * to this physical terminal.
* @param level * @param terminal
*/ */
bool setLevelOf(shared_real_terminal terminal, int level) void PhysicalTerminal::addTerminal(const QSharedPointer<RealTerminal> &terminal) {
{ m_real_terminal.append(terminal);
const int i = m_real_terminal.indexOf(terminal); }
if (i >= 0)
{ /**
* @brief removeTerminal
* Remove @a terminal from the list of real terminal
* @param terminal
* @return true if sucessfully removed
*/
bool PhysicalTerminal::removeTerminal(const QSharedPointer<RealTerminal> &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<RealTerminal> &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<RealTerminal> &terminal, int level)
{
const int i = m_real_terminal.indexOf(terminal);
if (i >= 0)
{
#if QT_VERSION >= QT_VERSION_CHECK(5,14,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 #else
auto j = std::min(level, m_real_terminal.size()-1); auto j = std::min(level, m_real_terminal.size()-1);
std::swap(m_real_terminal.begin()[i], m_real_terminal.begin()[j]); std::swap(m_real_terminal.begin()[i], m_real_terminal.begin()[j]);
#endif #endif
return true; return true;
} }
return false; return false;
} }
/** /**
* @brief terminals * @brief terminals
* @return A vector of RealTerminal who compose this PhysicalTerminal * @return A vector of RealTerminal who compose this PhysicalTerminal
*/ */
QVector<shared_real_terminal> terminals() const { QVector<QWeakPointer<RealTerminal>> PhysicalTerminal::realTerminals() const
return m_real_terminal; {
} QVector<QWeakPointer<RealTerminal>> vector_;
for (const auto &real_t : m_real_terminal) {
vector_.append(real_t.toWeakRef());
}
return vector_;
}
/** /**
* @brief uuid * @brief uuid
* @return the uuid of this physical terminal * @return the uuid of this physical terminal
*/ */
QUuid uuid() const { QUuid PhysicalTerminal::uuid() const {
return m_uuid; return m_uuid;
} }
static QString xmlTagName() { int PhysicalTerminal::pos() const
return QStringLiteral("physical_terminal"); {
} if (m_parent_terminal_strip) {
return m_parent_terminal_strip->pos(m_this_weak);
} else {
return -1;
}
}
/** int PhysicalTerminal::realTerminalCount() const {
* @brief toXml return m_real_terminal.size();
* @param parent_document }
* @return this physical terminal to xml
*/
QDomElement toXml(QDomDocument &parent_document) const
{
auto root_elmt = parent_document.createElement(this->xmlTagName());
for (auto &real_t : m_real_terminal) {
root_elmt.appendChild(real_t->toXml(parent_document));
}
return root_elmt; QString PhysicalTerminal::xmlTagName() {
} return QStringLiteral("physical_terminal");
}
private:
QPointer<TerminalStrip> m_parent_terminal_strip;
QVector<shared_real_terminal> m_real_terminal;
const QUuid m_uuid = QUuid::createUuid();
};
/************************************************************************************/ /************************************************************************************/
@ -554,16 +588,13 @@ bool TerminalStrip::addTerminal(Element *terminal)
m_terminal_elements_vector.append(terminal); m_terminal_elements_vector.append(terminal);
//Create the real terminal //Create the real terminal
auto raw_ptr = new RealTerminal(this, terminal); auto raw_real_ptr = new RealTerminal(this, terminal);
auto real_terminal = raw_ptr->sharedRef(); auto real_terminal = raw_real_ptr->sharedRef();
m_real_terminals.append(real_terminal); m_real_terminals.append(real_terminal);
//Create a new single level physical terminal //Create a new single level physical terminal
const shared_physical_terminal physical_terminal( auto raw_phy_ptr = new PhysicalTerminal(this, QVector<QSharedPointer<RealTerminal>>{real_terminal});
new PhysicalTerminal(this, m_physical_terminals.append(raw_phy_ptr->sharedRef());
QVector<shared_real_terminal>{real_terminal}));
m_physical_terminals.append(physical_terminal);
static_cast<TerminalElement *>(terminal)->setParentTerminalStrip(this); static_cast<TerminalElement *>(terminal)->setParentTerminalStrip(this);
@ -581,7 +612,6 @@ bool TerminalStrip::removeTerminal(Element *terminal)
if (m_terminal_elements_vector.contains(terminal)) if (m_terminal_elements_vector.contains(terminal))
{ {
m_terminal_elements_vector.removeOne(terminal); m_terminal_elements_vector.removeOne(terminal);
//Get the real and physical terminal associated to @terminal //Get the real and physical terminal associated to @terminal
if (auto real_terminal = realTerminal(terminal)) if (auto real_terminal = realTerminal(terminal))
{ {
@ -590,9 +620,9 @@ bool TerminalStrip::removeTerminal(Element *terminal)
if (physical_terminal->levelCount() == 1) { if (physical_terminal->levelCount() == 1) {
m_physical_terminals.removeOne(physical_terminal); m_physical_terminals.removeOne(physical_terminal);
} else { } else {
auto v = physical_terminal->terminals(); auto v = physical_terminal->realTerminals();
v.removeOne(real_terminal); v.removeOne(real_terminal);
physical_terminal->setTerminals(v); physical_terminal->setTerminals(QETUtils::weakVectorToShared(v));
} }
} }
m_real_terminals.removeOne(real_terminal); m_real_terminals.removeOne(real_terminal);
@ -632,50 +662,45 @@ int TerminalStrip::physicalTerminalCount() const {
* @brief TerminalStrip::physicalTerminalData * @brief TerminalStrip::physicalTerminalData
* @param index * @param index
* @return The data of the physical terminal at index \p index * @return The data of the physical terminal at index \p index
* returned QWeakPointer can be null
*/ */
PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) const QWeakPointer<PhysicalTerminal> TerminalStrip::physicalTerminal(int index) const
{ {
if (index < m_physical_terminals.size()) { if (index < m_physical_terminals.size()) {
return PhysicalTerminalData(this, m_physical_terminals.at(index)); return m_physical_terminals.at(index).toWeakRef();
} else { } else {
return PhysicalTerminalData(); return QWeakPointer<PhysicalTerminal>();
} }
} }
/** /**
* @brief TerminalStrip::physicalTerminalData * @brief TerminalStrip::physicalTerminalData
* @param real_terminal * @param real_terminal
* @return the parent PhysicalTerminalData of \p real_terminal. * @return the parent PhysicalTerminal of \p real_terminal.
* the PhysicalTerminalData can be invalid if \p real_terminal don't belong to this strip * the PhysicalTerminal can be null if \p real_terminal don't belong to this strip
*/ */
PhysicalTerminalData TerminalStrip::physicalTerminalData (const QWeakPointer<RealTerminal> &real_terminal) const QWeakPointer<PhysicalTerminal> TerminalStrip::physicalTerminal (const QWeakPointer<RealTerminal> &real_terminal) const
{ {
const auto real_t = real_terminal.toStrongRef(); const auto real_t = real_terminal.toStrongRef();
if (real_t.isNull()) { if (real_t.isNull()) {
return PhysicalTerminalData(); return QWeakPointer<PhysicalTerminal>();
} }
const auto phy_t = physicalTerminal(real_t); const auto phy_t = physicalTerminal(real_t);
if (phy_t) { if (phy_t) {
return PhysicalTerminalData(this, phy_t); return phy_t.toWeakRef();
} else {
return QWeakPointer<PhysicalTerminal>();
} }
return PhysicalTerminalData();
} }
/** /**
* @brief TerminalStrip::physicalTerminalData * @brief TerminalStrip::physicalTerminal
* @return A vector of all physical terminal data owned by this terminal strip. * @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 * The order of the vector is the same as the order of the terminal in the strip
*/ */
QVector<PhysicalTerminalData> TerminalStrip::physicalTerminalData() const QVector<QWeakPointer<PhysicalTerminal>> TerminalStrip::physicalTerminal() const {
{ return QETUtils::sharedVectorToWeak(m_physical_terminals);
QVector<PhysicalTerminalData> v_;
for (auto i = 0 ; i<physicalTerminalCount() ; ++i) {
v_.append(physicalTerminalData(i));
}
return v_;
} }
/** /**
@ -685,23 +710,23 @@ QVector<PhysicalTerminalData> TerminalStrip::physicalTerminalData() const
* \p sorted_vector must contain exaclty the same physical terminal as this strip * \p sorted_vector must contain exaclty the same physical terminal as this strip
* else this function do nothing. * 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 * 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. * the vector contain the same values, no more no less.
* *
* @param sorted_vector * @param sorted_vector
* @return true is successfully sorted. * @return true is successfully sorted.
*/ */
bool TerminalStrip::setOrderTo(const QVector<PhysicalTerminalData> &sorted_vector) bool TerminalStrip::setOrderTo(const QVector<QWeakPointer<PhysicalTerminal>> &sorted_vector)
{ {
if (sorted_vector.size() != m_physical_terminals.size()) { if (sorted_vector.size() != m_physical_terminals.size()) {
return false; return false;
} }
QVector<QSharedPointer<PhysicalTerminal>> new_order; QVector<QSharedPointer<PhysicalTerminal>> 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()) { if (physical_t.isNull()) {
continue; continue;
} }
@ -730,9 +755,9 @@ bool TerminalStrip::setOrderTo(const QVector<PhysicalTerminalData> &sorted_vecto
* @param receiver_terminal * @param receiver_terminal
* @return true if success * @return true if success
*/ */
bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector<QWeakPointer<RealTerminal>> &added_terminals) bool TerminalStrip::groupTerminals(const QWeakPointer<PhysicalTerminal> &receiver_terminal, const QVector<QWeakPointer<RealTerminal>> &added_terminals)
{ {
const auto receiver_ = receiver_terminal.physicalTerminal().toStrongRef(); const auto receiver_ = receiver_terminal.toStrongRef();
if (receiver_.isNull()) { if (receiver_.isNull()) {
qDebug() << "TerminalStrip::groupTerminal : Arguments terminals don't belong to this strip. Operation aborted."; qDebug() << "TerminalStrip::groupTerminal : Arguments terminals don't belong to this strip. Operation aborted.";
return false; return false;
@ -758,7 +783,7 @@ bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal
{ {
const auto vector_ = m_physical_terminals; const auto vector_ = m_physical_terminals;
for (const auto &phys : vector_) { for (const auto &phys : vector_) {
if (phys->terminals().isEmpty()) { if (phys->realTerminals().isEmpty()) {
m_physical_terminals.removeOne(phys); m_physical_terminals.removeOne(phys);
} }
} }
@ -782,13 +807,11 @@ void TerminalStrip::unGroupTerminals(const QVector<QWeakPointer<RealTerminal>> &
{ {
if (auto physical_terminal = physicalTerminal(real_terminal)) //Get the physical terminal 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); physical_terminal->removeTerminal(real_terminal);
const shared_physical_terminal new_physical_terminal ( auto raw_ptr = new PhysicalTerminal(this, QVector<QSharedPointer<RealTerminal>>{real_terminal});
new PhysicalTerminal(this, QVector<shared_real_terminal>{real_terminal})); m_physical_terminals.append(raw_ptr->sharedRef());
m_physical_terminals.append(new_physical_terminal);
ungrouped = true; ungrouped = true;
} }
} }
@ -814,7 +837,7 @@ bool TerminalStrip::setLevel(const QWeakPointer<RealTerminal> &real_terminal, in
auto physical_terminal = physicalTerminal(real_t); auto physical_terminal = physicalTerminal(real_t);
if (physical_terminal) if (physical_terminal)
{ {
if (physical_terminal->terminals().size() > 1 && if (physical_terminal->realTerminals().size() > 1 &&
physical_terminal->setLevelOf(real_t, level)) physical_terminal->setLevelOf(real_t, level))
{ {
emit orderChanged(); emit orderChanged();
@ -1084,7 +1107,7 @@ QWeakPointer<RealTerminal> TerminalStrip::previousTerminalInLevel(const QWeakPoi
const auto index = m_physical_terminals.indexOf(phy_t); const auto index = m_physical_terminals.indexOf(phy_t);
if (index >= 1) 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_) { if (t_vector.size() > level_) {
return t_vector.at(level_); return t_vector.at(level_);
} }
@ -1110,7 +1133,7 @@ QWeakPointer<RealTerminal> TerminalStrip::nextTerminalInLevel(const QWeakPointer
const auto index = m_physical_terminals.indexOf(phy_t); const auto index = m_physical_terminals.indexOf(phy_t);
if (index < m_physical_terminals.size()-1) 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_) { if (t_vector.size() > level_) {
return t_vector.at(level_); return t_vector.at(level_);
} }
@ -1213,8 +1236,8 @@ bool TerminalStrip::fromXml(QDomElement &xml_element)
real_t_vector.append(real_t); real_t_vector.append(real_t);
} }
const shared_physical_terminal phy_t(new PhysicalTerminal(this, real_t_vector)); auto raw_ptr = new PhysicalTerminal(this, real_t_vector);
m_physical_terminals.append(phy_t); m_physical_terminals.append(raw_ptr->sharedRef());
m_real_terminals.append(real_t_vector); m_real_terminals.append(real_t_vector);
} }
@ -1255,7 +1278,7 @@ QSharedPointer<PhysicalTerminal> TerminalStrip::physicalTerminal(QSharedPointer<
} }
for (auto &physical : qAsConst(m_physical_terminals)) { for (auto &physical : qAsConst(m_physical_terminals)) {
if (physical->terminals().contains(terminal)) { if (physical->realTerminals().contains(terminal)) {
pt = physical; pt = physical;
break; break;
} }
@ -1321,74 +1344,6 @@ void TerminalStrip::rebuildRealVector()
{ {
m_real_terminals.clear(); m_real_terminals.clear();
for (const auto &phy : qAsConst(m_physical_terminals)) { 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<PhysicalTerminal> 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<QWeakPointer<RealTerminal>> PhysicalTerminalData::realTerminals() const
{
const auto phy_t = m_physical_terminal.toStrongRef();
if (phy_t)
{
QVector<QWeakPointer<RealTerminal>> vector_;
for (const auto &real_t : phy_t->terminals()) {
vector_.append(real_t.toWeakRef());
}
return vector_;
}
else
{
return QVector<QWeakPointer<RealTerminal>>();
}
}
QWeakPointer<PhysicalTerminal> PhysicalTerminalData::physicalTerminal() const {
return m_physical_terminal;
}

View File

@ -95,42 +95,42 @@ class RealTerminal
QWeakPointer<RealTerminal> m_this_weak; QWeakPointer<RealTerminal> m_this_weak;
}; };
/** class PhysicalTerminal
* @brief The PhysicalTerminalData
* Conveniant struct to quickly get some values
* of a PhysicalTerminal
*/
class PhysicalTerminalData
{ {
friend class TerminalStrip; friend class TerminalStrip;
private: private:
PhysicalTerminalData(const TerminalStrip *strip, QSharedPointer<PhysicalTerminal> terminal); PhysicalTerminal(TerminalStrip *parent_strip, QVector<QSharedPointer<RealTerminal>> terminals);
QSharedPointer<PhysicalTerminal> sharedRef();
QWeakPointer<PhysicalTerminal> weakRef();
QDomElement toXml(QDomDocument &parent_document) const;
void setTerminals(const QVector<QSharedPointer<RealTerminal>> &terminals);
void addTerminal(const QSharedPointer<RealTerminal> &terminal);
bool removeTerminal(const QSharedPointer<RealTerminal> &terminal);
bool setLevelOf(const QSharedPointer<RealTerminal> &terminal, int level);
public: public:
PhysicalTerminalData(){} PhysicalTerminal(){}
bool isNull() const; int levelCount() const;
int pos() const; int levelOf(const QWeakPointer<RealTerminal> &terminal) const;
QUuid uuid() const;
int realTerminalCount() const;
QVector<QWeakPointer<RealTerminal>> realTerminals() const; QVector<QWeakPointer<RealTerminal>> realTerminals() const;
QWeakPointer<PhysicalTerminal> physicalTerminal() const; QUuid uuid() const;
int pos() const;
int realTerminalCount() const;
static QString xmlTagName();
private: private:
QPointer<const TerminalStrip> m_strip; QPointer<TerminalStrip> m_parent_terminal_strip;
QWeakPointer<PhysicalTerminal> m_physical_terminal; QVector<QSharedPointer<RealTerminal>> m_real_terminal;
QUuid m_uuid = QUuid::createUuid();
QWeakPointer<PhysicalTerminal> 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 * @brief The TerminalStrip class
* This class hold all the datas and configurations * This class hold all the datas and configurations
@ -180,12 +180,12 @@ class TerminalStrip : public QObject
int pos(const QWeakPointer<PhysicalTerminal> &terminal) const; int pos(const QWeakPointer<PhysicalTerminal> &terminal) const;
int physicalTerminalCount() const; int physicalTerminalCount() const;
PhysicalTerminalData physicalTerminalData(int index) const; QWeakPointer<PhysicalTerminal> physicalTerminal(int index) const;
PhysicalTerminalData physicalTerminalData (const QWeakPointer<RealTerminal> &real_terminal) const; QWeakPointer<PhysicalTerminal> physicalTerminal (const QWeakPointer<RealTerminal> &real_terminal) const;
QVector<PhysicalTerminalData> physicalTerminalData() const; QVector<QWeakPointer<PhysicalTerminal>> physicalTerminal() const;
bool setOrderTo(const QVector<PhysicalTerminalData> &sorted_vector); bool setOrderTo(const QVector<QWeakPointer<PhysicalTerminal>> &sorted_vector);
bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector<QWeakPointer<RealTerminal>> &added_terminals); bool groupTerminals(const QWeakPointer<PhysicalTerminal> &receiver_terminal, const QVector<QWeakPointer<RealTerminal>> &added_terminals);
void unGroupTerminals(const QVector<QWeakPointer<RealTerminal>> &terminals_to_ungroup); void unGroupTerminals(const QVector<QWeakPointer<RealTerminal>> &terminals_to_ungroup);
bool setLevel(const QWeakPointer<RealTerminal> &real_terminal, int level); bool setLevel(const QWeakPointer<RealTerminal> &real_terminal, int level);

View File

@ -34,6 +34,7 @@
#include "../UndoCommand/groupterminalscommand.h" #include "../UndoCommand/groupterminalscommand.h"
#include "../UndoCommand/changeterminallevel.h" #include "../UndoCommand/changeterminallevel.h"
#include "../UndoCommand/bridgeterminalscommand.h" #include "../UndoCommand/bridgeterminalscommand.h"
#include "../../utils/qetutils.h"
#include <QTreeWidgetItem> #include <QTreeWidgetItem>
@ -231,10 +232,10 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s
//Add child terminal of the strip //Add child terminal of the strip
for (auto i=0 ; i<terminal_strip->physicalTerminalCount() ; ++i) for (auto i=0 ; i<terminal_strip->physicalTerminalCount() ; ++i)
{ {
auto ptd = terminal_strip->physicalTerminalData(i); auto phy_t = terminal_strip->physicalTerminal(i).toStrongRef();
if (ptd.realTerminalCount()) 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); 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->setData(0, TerminalStripTreeWidget::UUID_USER_ROLE, real_t->elementUuid());
terminal_item->setIcon(0, QET::Icons::ElementTerminal); terminal_item->setIcon(0, QET::Icons::ElementTerminal);
@ -352,7 +353,7 @@ void TerminalStripEditor::spanMultiLevelTerminals()
auto current_row = 0; auto current_row = 0;
for (auto i = 0 ; i < m_current_strip->physicalTerminalCount() ; ++i) 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) { if (level_count > 1) {
ui->m_table_widget->setSpan(current_row, 0, 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()); auto mrtd_vector = m_model->modelRealTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes());
if (mrtd_vector.size() >= 2) 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<QWeakPointer<RealTerminal>> vector_; QVector<QWeakPointer<RealTerminal>> vector_;
for (const auto & mrtd : mrtd_vector) { for (const auto & mrtd : mrtd_vector) {
vector_.append(mrtd.real_terminal); 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) { for (const auto &mrtd : mrtd_vector) {
vector_.append(mrtd.real_terminal); 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_)));
} }
} }

View File

@ -481,13 +481,14 @@ void TerminalStripModel::fillPhysicalTerminalData()
//Get all physical terminal //Get all physical terminal
if (m_terminal_strip) 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; modelPhysicalTerminalData mptd;
mptd.pos_ = ptd.pos(); mptd.pos_ = phy_t->pos();
mptd.uuid_ = ptd.uuid(); mptd.uuid_ = phy_t->uuid();
for (const auto &real_t : ptd.realTerminals()) for (const auto &real_t : phy_t->realTerminals())
{ {
if (!real_t.isNull()) 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 if we need to draw a none bridge pixmap
//Check previous //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_real_terminal = mrtd;
auto current_phy_uuid = physical_data.uuid(); auto current_phy_uuid = phy_t->uuid();
bool already_jumped_to_previous = false; bool already_jumped_to_previous = false;
modelRealTerminalData previous_data; modelRealTerminalData previous_data;
@ -687,7 +688,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const
} }
//We are in the same physical terminal as previous loop //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_ && if (current_real_terminal.bridged_ &&
current_real_terminal.level_ == level_column) { current_real_terminal.level_ == level_column) {
@ -699,7 +700,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const
break; break;
} else { } else {
already_jumped_to_previous = true; 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_ && if (current_real_terminal.bridged_ &&
current_real_terminal.level_ == level_column) { current_real_terminal.level_ == level_column) {
previous_data = current_real_terminal; previous_data = current_real_terminal;
@ -710,7 +711,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const
//Check next //Check next
current_real_terminal = mrtd; current_real_terminal = mrtd;
current_phy_uuid = physical_data.uuid(); current_phy_uuid = phy_t->uuid();
bool already_jumped_to_next = false; bool already_jumped_to_next = false;
modelRealTerminalData next_data; modelRealTerminalData next_data;
@ -722,7 +723,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const
} }
//We are in the same physical terminal as previous loop //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_ && if (current_real_terminal.bridged_ &&
current_real_terminal.level_ == level_column) { current_real_terminal.level_ == level_column) {
@ -734,7 +735,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const
break; break;
} else { } else {
already_jumped_to_next = true; 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_ && if (current_real_terminal.bridged_ &&
current_real_terminal.level_ == level_column) { current_real_terminal.level_ == level_column) {
next_data = current_real_terminal; next_data = current_real_terminal;

View File

@ -1,4 +1,4 @@
/* /*
Copyright 2006-2021 The QElectroTech Team Copyright 2006-2021 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
@ -19,7 +19,9 @@
#define QETUTILS_H #define QETUTILS_H
#include <QMargins> #include <QMargins>
#include <QWeakPointer>
class RealTerminal;
/** /**
Provide some small utils function Provide some small utils function
*/ */
@ -27,6 +29,27 @@ namespace QETUtils
{ {
QString marginsToString(const QMargins &margins); QString marginsToString(const QMargins &margins);
QMargins marginsFromString(const QString &string); QMargins marginsFromString(const QString &string);
template <typename T>
QVector<QWeakPointer<T>> sharedVectorToWeak(const QVector<QSharedPointer<T>> &vector)
{
QVector<QWeakPointer<T>> return_vector;
for (const auto shared : vector) {
return_vector.append(shared.toWeakRef());
}
return return_vector;
}
template <typename T>
QVector<QSharedPointer<T>> weakVectorToShared(const QVector<QWeakPointer<T>> &vector)
{
QVector<QSharedPointer<T>> return_vector;
for (const auto weak : vector) {
return_vector.append(weak.toStrongRef());
}
return return_vector;
}
} }
#endif // QETUTILS_H #endif // QETUTILS_H