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/>.
*/
#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<QWeakPointer<RealTerminal>> &to_group,
const QSharedPointer<PhysicalTerminal> &receiver_,
const QVector<QSharedPointer<RealTerminal>> &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<QWeakPointer<RealTerminal>> &to_ungroup,
const QVector<QSharedPointer<RealTerminal>> &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<QWeakPointer<RealTerminal>> &to_ungroup)
void UnGroupTerminalsCommand::setUp(const QVector<QSharedPointer<RealTerminal>> &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 (ptd_.realTerminalCount() <= 1) {
if (phy_t.toStrongRef()->realTerminalCount() <= 1) {
continue;
}
auto vector_ = m_physical_real_H.value(ptd_);
auto vector_ = m_physical_real_H.value(phy_t);
vector_.append(rt_);
m_physical_real_H.insert(ptd_, vector_);
m_physical_real_H.insert(phy_t, vector_);
}
}
}

View File

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

View File

@ -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<PhysicalTerminal> arg1, QSharedPointer<PhysicalTerminal> 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()) {

View File

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

View File

@ -22,6 +22,7 @@
#include "../elementprovider.h"
#include "../qetxml.h"
#include "../autoNum/assignvariables.h"
#include "../../utils/qetutils.h"
using shared_real_terminal = QSharedPointer<RealTerminal>;
using shared_physical_terminal = QSharedPointer<PhysicalTerminal>;
@ -61,7 +62,7 @@ QSharedPointer<RealTerminal> RealTerminal::sharedRef()
/**
* @brief RealTerminal::weakRef
* @return a QWeakPointer of this
* @return a QWeakPointer of this, weak pointer can be bull
*/
QWeakPointer<RealTerminal> 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,9 +335,6 @@ QString RealTerminal::RealTerminal::xmlTagName() {
* @sa PhysicalTerminalData
*
*/
class PhysicalTerminal
{
public:
/**
* @brief PhysicalTerminal
* @param parent_strip : Parent terminal strip
@ -344,12 +342,51 @@ class PhysicalTerminal
* who compose this physical terminal.
* \p terminals must have at least one terminal
*/
PhysicalTerminal(TerminalStrip *parent_strip,
QVector<shared_real_terminal> terminals) :
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()
{
QSharedPointer<PhysicalTerminal> this_shared(this->weakRef());
if (this_shared.isNull())
{
this_shared = QSharedPointer<PhysicalTerminal>(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> 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.
@ -357,7 +394,7 @@ class PhysicalTerminal
* represent the level of these in this physical terminal.
* @param terminals
*/
void setTerminals(QVector<shared_real_terminal> terminals) {
void PhysicalTerminal::setTerminals(const QVector<QSharedPointer<RealTerminal>> &terminals) {
m_real_terminal = terminals;
}
@ -367,7 +404,7 @@ class PhysicalTerminal
* to this physical terminal.
* @param terminal
*/
void addTerminal(shared_real_terminal terminal) {
void PhysicalTerminal::addTerminal(const QSharedPointer<RealTerminal> &terminal) {
m_real_terminal.append(terminal);
}
@ -377,7 +414,7 @@ class PhysicalTerminal
* @param terminal
* @return true if sucessfully removed
*/
bool removeTerminal(shared_real_terminal terminal) {
bool PhysicalTerminal::removeTerminal(const QSharedPointer<RealTerminal> &terminal) {
return m_real_terminal.removeOne(terminal);
}
@ -385,7 +422,7 @@ class PhysicalTerminal
* @brief levelCount
* @return the number of level of this terminal
*/
int levelCount() const {
int PhysicalTerminal::levelCount() const {
return m_real_terminal.size();
}
@ -395,8 +432,8 @@ class PhysicalTerminal
* @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);
int PhysicalTerminal::levelOf(const QWeakPointer<RealTerminal> &terminal) const {
return m_real_terminal.indexOf(terminal.toStrongRef());
}
/**
@ -405,7 +442,7 @@ class PhysicalTerminal
* @param terminal
* @param level
*/
bool setLevelOf(shared_real_terminal terminal, int level)
bool PhysicalTerminal::setLevelOf(const QSharedPointer<RealTerminal> &terminal, int level)
{
const int i = m_real_terminal.indexOf(terminal);
if (i >= 0)
@ -425,43 +462,40 @@ class PhysicalTerminal
* @brief terminals
* @return A vector of RealTerminal who compose this PhysicalTerminal
*/
QVector<shared_real_terminal> terminals() const {
return m_real_terminal;
QVector<QWeakPointer<RealTerminal>> PhysicalTerminal::realTerminals() const
{
QVector<QWeakPointer<RealTerminal>> 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 {
QUuid PhysicalTerminal::uuid() const {
return m_uuid;
}
static QString xmlTagName() {
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 toXml
* @param parent_document
* @return this physical terminal to xml
*/
QDomElement toXml(QDomDocument &parent_document) const
{
auto root_elmt = parent_document.createElement(this->xmlTagName());
for (auto &real_t : m_real_terminal) {
root_elmt.appendChild(real_t->toXml(parent_document));
}
return root_elmt;
}
private:
QPointer<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);
//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<shared_real_terminal>{real_terminal}));
m_physical_terminals.append(physical_terminal);
auto raw_phy_ptr = new PhysicalTerminal(this, QVector<QSharedPointer<RealTerminal>>{real_terminal});
m_physical_terminals.append(raw_phy_ptr->sharedRef());
static_cast<TerminalElement *>(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<PhysicalTerminal> 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<PhysicalTerminal>();
}
}
/**
* @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<RealTerminal> &real_terminal) const
QWeakPointer<PhysicalTerminal> TerminalStrip::physicalTerminal (const QWeakPointer<RealTerminal> &real_terminal) const
{
const auto real_t = real_terminal.toStrongRef();
if (real_t.isNull()) {
return PhysicalTerminalData();
return QWeakPointer<PhysicalTerminal>();
}
const auto phy_t = physicalTerminal(real_t);
if (phy_t) {
return PhysicalTerminalData(this, phy_t);
return phy_t.toWeakRef();
} else {
return QWeakPointer<PhysicalTerminal>();
}
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<PhysicalTerminalData> TerminalStrip::physicalTerminalData() const
{
QVector<PhysicalTerminalData> v_;
for (auto i = 0 ; i<physicalTerminalCount() ; ++i) {
v_.append(physicalTerminalData(i));
}
return v_;
QVector<QWeakPointer<PhysicalTerminal>> TerminalStrip::physicalTerminal() const {
return QETUtils::sharedVectorToWeak(m_physical_terminals);
}
/**
@ -685,23 +710,23 @@ QVector<PhysicalTerminalData> 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<PhysicalTerminalData> &sorted_vector)
bool TerminalStrip::setOrderTo(const QVector<QWeakPointer<PhysicalTerminal>> &sorted_vector)
{
if (sorted_vector.size() != m_physical_terminals.size()) {
return false;
}
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()) {
continue;
}
@ -730,9 +755,9 @@ bool TerminalStrip::setOrderTo(const QVector<PhysicalTerminalData> &sorted_vecto
* @param receiver_terminal
* @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()) {
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<QWeakPointer<RealTerminal>> &
{
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<shared_real_terminal>{real_terminal}));
m_physical_terminals.append(new_physical_terminal);
auto raw_ptr = new PhysicalTerminal(this, QVector<QSharedPointer<RealTerminal>>{real_terminal});
m_physical_terminals.append(raw_ptr->sharedRef());
ungrouped = true;
}
}
@ -814,7 +837,7 @@ bool TerminalStrip::setLevel(const QWeakPointer<RealTerminal> &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<RealTerminal> 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<RealTerminal> 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<PhysicalTerminal> 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<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;
};
/**
* @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<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:
PhysicalTerminalData(){}
PhysicalTerminal(){}
bool isNull() const;
int pos() const;
QUuid uuid() const;
int realTerminalCount() const;
int levelCount() const;
int levelOf(const QWeakPointer<RealTerminal> &terminal) const;
QVector<QWeakPointer<RealTerminal>> realTerminals() const;
QWeakPointer<PhysicalTerminal> physicalTerminal() const;
QUuid uuid() const;
int pos() const;
int realTerminalCount() const;
static QString xmlTagName();
private:
QPointer<const TerminalStrip> m_strip;
QWeakPointer<PhysicalTerminal> m_physical_terminal;
QPointer<TerminalStrip> m_parent_terminal_strip;
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
* This class hold all the datas and configurations
@ -180,12 +180,12 @@ class TerminalStrip : public QObject
int pos(const QWeakPointer<PhysicalTerminal> &terminal) const;
int physicalTerminalCount() const;
PhysicalTerminalData physicalTerminalData(int index) const;
PhysicalTerminalData physicalTerminalData (const QWeakPointer<RealTerminal> &real_terminal) const;
QVector<PhysicalTerminalData> physicalTerminalData() const;
QWeakPointer<PhysicalTerminal> physicalTerminal(int index) const;
QWeakPointer<PhysicalTerminal> physicalTerminal (const QWeakPointer<RealTerminal> &real_terminal) const;
QVector<QWeakPointer<PhysicalTerminal>> physicalTerminal() const;
bool setOrderTo(const QVector<PhysicalTerminalData> &sorted_vector);
bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector<QWeakPointer<RealTerminal>> &added_terminals);
bool setOrderTo(const QVector<QWeakPointer<PhysicalTerminal>> &sorted_vector);
bool groupTerminals(const QWeakPointer<PhysicalTerminal> &receiver_terminal, const QVector<QWeakPointer<RealTerminal>> &added_terminals);
void unGroupTerminals(const QVector<QWeakPointer<RealTerminal>> &terminals_to_ungroup);
bool setLevel(const QWeakPointer<RealTerminal> &real_terminal, int level);

View File

@ -34,6 +34,7 @@
#include "../UndoCommand/groupterminalscommand.h"
#include "../UndoCommand/changeterminallevel.h"
#include "../UndoCommand/bridgeterminalscommand.h"
#include "../../utils/qetutils.h"
#include <QTreeWidgetItem>
@ -231,10 +232,10 @@ QTreeWidgetItem* TerminalStripEditor::addTerminalStrip(TerminalStrip *terminal_s
//Add child terminal of the strip
for (auto i=0 ; i<terminal_strip->physicalTerminalCount() ; ++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<QWeakPointer<RealTerminal>> 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_)));
}
}

View File

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

View File

@ -1,4 +1,4 @@
/*
/*
Copyright 2006-2021 The QElectroTech Team
This file is part of QElectroTech.
@ -19,7 +19,9 @@
#define QETUTILS_H
#include <QMargins>
#include <QWeakPointer>
class RealTerminal;
/**
Provide some small utils function
*/
@ -27,6 +29,27 @@ namespace QETUtils
{
QString marginsToString(const QMargins &margins);
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