Change struct TerminalStripBridge to class

And move it in a new file
This commit is contained in:
joshua
2021-12-26 18:43:17 +01:00
parent 5709f469fc
commit 1572fafabe
7 changed files with 156 additions and 49 deletions

View File

@@ -26,7 +26,7 @@ class TerminalStrip;
class Element; class Element;
class TerminalElement; class TerminalElement;
class PhysicalTerminal; class PhysicalTerminal;
struct TerminalStripBridge; class TerminalStripBridge;
/** /**
* @brief The RealTerminal class * @brief The RealTerminal class

View File

@@ -22,9 +22,9 @@
#include "../elementprovider.h" #include "../elementprovider.h"
#include "../qetxml.h" #include "../qetxml.h"
#include "../autoNum/assignvariables.h" #include "../autoNum/assignvariables.h"
#include "../../utils/qetutils.h"
#include "physicalterminal.h" #include "physicalterminal.h"
#include "realterminal.h" #include "realterminal.h"
#include "terminalstripbridge.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>;
@@ -480,29 +480,20 @@ bool TerminalStrip::setBridge(const QVector<QSharedPointer<RealTerminal>> &real_
if (!isBridgeable(real_terminals)) { if (!isBridgeable(real_terminals)) {
return false; return false;
} }
QVector<QSharedPointer<RealTerminal>> real_terminals_vector;
for (const auto &real_terminal : real_terminals) auto bridge = bridgeFor(real_terminals);
if (bridge.isNull())
{ {
if (real_terminal) { bridge = QSharedPointer<TerminalStripBridge>(new TerminalStripBridge(this));
real_terminals_vector.append(real_terminal);
}
}
auto bridge = bridgeFor(real_terminals_vector);
if (bridge.isNull()) {
bridge = QSharedPointer<TerminalStripBridge>::create();
m_bridge.append(bridge); m_bridge.append(bridge);
} }
for (const auto &real_t : qAsConst(real_terminals_vector)) if (bridge->addTerminals(real_terminals))
{ {
if (!bridge->real_terminals.contains(real_t)) emit bridgeChanged();;
bridge->real_terminals.append(real_t);
}
emit bridgeChanged();
return true; return true;
}
return false;
} }
/** /**
@@ -520,18 +511,8 @@ bool TerminalStrip::setBridge(const QSharedPointer<TerminalStripBridge> &bridge,
return false; return false;
} }
bool b_ = false; if (bridge->addTerminals(real_terminals))
for (const auto &real_t : real_terminals)
{ {
if (real_t &&
!bridge->real_terminals.contains(real_t))
{
bridge->real_terminals.append(real_t);
b_ = true;
}
}
if (b_) {
emit bridgeChanged(); emit bridgeChanged();
return true; return true;
} }
@@ -551,10 +532,7 @@ void TerminalStrip::unBridge(const QVector<QSharedPointer<RealTerminal>> &real_t
if (canUnBridge(real_terminals)) if (canUnBridge(real_terminals))
{ {
auto bridge_ = isBridged(real_terminals.first()); auto bridge_ = isBridged(real_terminals.first());
for (const auto &real_t : qAsConst(real_terminals)) { bridge_->removeTerminals(real_terminals);
bridge_->real_terminals.removeOne(real_t);
}
emit bridgeChanged(); emit bridgeChanged();
} }
} }
@@ -564,7 +542,7 @@ void TerminalStrip::unBridge(const QVector<QSharedPointer<RealTerminal>> &real_t
* @param m_real_terminals * @param m_real_terminals
* @return True if all terminals of @a real_terminals can be unbridged. * @return True if all terminals of @a real_terminals can be unbridged.
* For this method return True, all terminals must be bridged together, * For this method return True, all terminals must be bridged together,
* be consecutive and in an one or the both extremities of the bridge. * be consecutive and in one or the both extremities of the bridge.
*/ */
bool TerminalStrip::canUnBridge(const QVector<QSharedPointer<RealTerminal> > &real_terminals) const bool TerminalStrip::canUnBridge(const QVector<QSharedPointer<RealTerminal> > &real_terminals) const
{ {
@@ -634,7 +612,7 @@ QSharedPointer<TerminalStripBridge> TerminalStrip::isBridged(const QSharedPointe
if (real_terminal) if (real_terminal)
{ {
for (const auto &bridge_ : qAsConst(m_bridge)) { for (const auto &bridge_ : qAsConst(m_bridge)) {
if (bridge_->real_terminals.contains(real_terminal)) if (bridge_->realTerminals().contains(real_terminal))
return bridge_; return bridge_;
} }
} }

View File

@@ -32,13 +32,7 @@ class PhysicalTerminal;
class TerminalStripIndex; class TerminalStripIndex;
class TerminalElement; class TerminalElement;
class TerminalStrip; class TerminalStrip;
class TerminalStripBridge;
struct TerminalStripBridge
{
QVector<QSharedPointer<RealTerminal>> real_terminals;
QColor color_ = Qt::darkGray;
QUuid uuid_ = QUuid::createUuid();
};
/** /**
* @brief The TerminalStrip class * @brief The TerminalStrip class
@@ -55,13 +49,11 @@ class TerminalStrip : public QObject
Q_OBJECT Q_OBJECT
public: public:
static QVector<QColor> bridgeColor() {return QVector<QColor>{Qt::red, Qt::blue, Qt::white, Qt::darkGray, Qt::black};}
signals: signals:
void orderChanged(); //Emitted when the order of the physical terminal is changed void orderChanged(); //Emitted when the order of the physical terminal is changed
void bridgeChanged(); void bridgeChanged();
public: public:
TerminalStrip(QETProject *project); TerminalStrip(QETProject *project);
TerminalStrip(const QString &installation, TerminalStrip(const QString &installation,

View File

@@ -0,0 +1,84 @@
/*
Copyright 2006-2021 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "terminalstripbridge.h"
#include "realterminal.h"
#include "terminalstrip.h"
TerminalStripBridge::TerminalStripBridge(TerminalStrip *parent_strip) :
m_strip(parent_strip)
{}
/**
* @brief TerminalStripBridge::color
* @return The color of this bridge
*/
QColor TerminalStripBridge::color() const {
return m_color;
}
/**
* @brief TerminalStripBridge::realTerminals
* @return the real terminals who are bridged by this bridge
*/
QVector<QSharedPointer<RealTerminal> > TerminalStripBridge::realTerminals() const {
return m_real_terminals;
}
/**
* @brief TerminalStripBridge::addTerminals
* @param real_terminals
* @return Add terminals of @a real_terminals to this bridge.
* If a terminal is already bridged by this bridge, the terminal is ignored.
* If at least one terminal doesn't belong to the same strip of this bridge
* this function do nothing and return false.
*/
bool TerminalStripBridge::addTerminals(const QVector<QSharedPointer<RealTerminal> > &real_terminals)
{
QVector<QSharedPointer<RealTerminal>> to_append;
for (const auto &real_t : real_terminals)
{
if (!real_t.isNull())
{
if (real_t->parentStrip() != m_strip) {
return false;
}
if (!m_real_terminals.contains(real_t)) {
to_append.append(real_t);
}
} else {
return false;
}
}
m_real_terminals.append(to_append);
return true;
}
/**
* @brief TerminalStripBridge::removeTerminals
* @param real_terminal
* Remove all real terminal of @real_terminals from this bridge.
* This function doesn't make any check, they just remove if exist.
* @sa TerminalStrip::canUnBridge
*/
void TerminalStripBridge::removeTerminals(const QVector<QSharedPointer<RealTerminal>> &real_terminals)
{
for (const auto &real_t : real_terminals) {
m_real_terminals.removeOne(real_t);
}
}

View File

@@ -0,0 +1,51 @@
/*
Copyright 2006-2021 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TERMINALSTRIPBRIDGE_H
#define TERMINALSTRIPBRIDGE_H
#include <QSharedPointer>
#include <QUuid>
#include <QPointer>
#include <QColor>
class RealTerminal;
class TerminalStrip;
class TerminalStripBridge
{
public:
static QVector<QColor> bridgeColor() {return QVector<QColor>{Qt::red, Qt::blue, Qt::white, Qt::darkGray, Qt::black};}
TerminalStripBridge(TerminalStrip *parent_strip = nullptr);
QColor color() const;
QVector<QSharedPointer<RealTerminal>> realTerminals() const;
bool addTerminals(const QVector<QSharedPointer<RealTerminal>> &real_terminals);
void removeTerminals(const QVector<QSharedPointer<RealTerminal>> &real_terminals);
private:
QPointer<TerminalStrip> m_strip;
QVector<QSharedPointer<RealTerminal>> m_real_terminals;
QColor m_color = Qt::darkGray;
QUuid m_uuid = QUuid::createUuid();
};
#endif // TERMINALSTRIPBRIDGE_H

View File

@@ -37,6 +37,7 @@
#include "../../utils/qetutils.h" #include "../../utils/qetutils.h"
#include "../physicalterminal.h" #include "../physicalterminal.h"
#include "../realterminal.h" #include "../realterminal.h"
#include "../terminalstripbridge.h"
#include <QTreeWidgetItem> #include <QTreeWidgetItem>
@@ -63,7 +64,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) :
#endif #endif
//Setup the bridge color //Setup the bridge color
ui->m_bridge_color_cb->setColors(TerminalStrip::bridgeColor().toList()); ui->m_bridge_color_cb->setColors(TerminalStripBridge::bridgeColor().toList());
setUpUndoConnections(); setUpUndoConnections();

View File

@@ -20,6 +20,7 @@
#include "../../qetgraphicsitem/element.h" #include "../../qetgraphicsitem/element.h"
#include "../physicalterminal.h" #include "../physicalterminal.h"
#include "../realterminal.h" #include "../realterminal.h"
#include "../terminalstripbridge.h"
#include <QDebug> #include <QDebug>
#include <QBrush> #include <QBrush>
#include <QVector> #include <QVector>
@@ -416,7 +417,7 @@ modelRealTerminalData TerminalStripModel::modelRealTerminalDataForIndex(const QM
void TerminalStripModel::buildBridgePixmap(const QSize &pixmap_size) void TerminalStripModel::buildBridgePixmap(const QSize &pixmap_size)
{ {
m_bridges_pixmaps.clear(); m_bridges_pixmaps.clear();
for (auto color_ : TerminalStrip::bridgeColor()) for (auto color_ : TerminalStripBridge::bridgeColor())
{ {
QPen pen; QPen pen;
pen.setColor(color_); pen.setColor(color_);
@@ -656,7 +657,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const
if (next_t) if (next_t)
next_bridge = next_t->bridge(); next_bridge = next_t->bridge();
auto color_ = bridge_->color_; auto color_ = bridge_->color();
auto pixmap_ = m_bridges_pixmaps.value(color_); auto pixmap_ = m_bridges_pixmaps.value(color_);
//Current real terminal between two bridged terminal //Current real terminal between two bridged terminal
@@ -749,7 +750,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const
if (previous_bridge == m_terminal_strip->isBridged(next_data.real_terminal)) if (previous_bridge == m_terminal_strip->isBridged(next_data.real_terminal))
{ {
if (previous_bridge) { if (previous_bridge) {
return m_bridges_pixmaps.value(previous_bridge->color_).none_; return m_bridges_pixmaps.value(previous_bridge->color()).none_;
} }
} }