From 48f9ef763202abe0a07330bf0249d2383ebea1c8 Mon Sep 17 00:00:00 2001 From: joshua Date: Mon, 15 May 2023 22:22:12 +0200 Subject: [PATCH] Add AbstractTerminalStrip class to be used by TerminalStripDrawer class --- .../GraphicsItem/terminalstripdrawer.cpp | 22 +-- .../GraphicsItem/terminalstripdrawer.h | 78 +++++++--- .../GraphicsItem/terminalstripitem.h | 4 +- .../GraphicsItem/trueterminalstrip.cpp | 133 ++++++++++++++++++ .../GraphicsItem/trueterminalstrip.h | 78 ++++++++++ 5 files changed, 286 insertions(+), 29 deletions(-) create mode 100644 sources/TerminalStrip/GraphicsItem/trueterminalstrip.cpp create mode 100644 sources/TerminalStrip/GraphicsItem/trueterminalstrip.h diff --git a/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp index 731c5160c..8448cecfa 100644 --- a/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp +++ b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp @@ -22,6 +22,9 @@ #include "../realterminal.h" #include "../terminalstrip.h" #include "../terminalstripbridge.h" +#include "trueterminalstrip.h" + +namespace TerminalStripDrawer { /** * @brief TerminalStripDrawer::TerminalStripDrawer @@ -30,13 +33,13 @@ */ TerminalStripDrawer::TerminalStripDrawer(QPointer strip, QSharedPointer layout) : - m_strip(strip), + m_strip{new TrueTerminalStrip{strip.data()}}, m_pattern(layout) {} void TerminalStripDrawer::setStrip(TerminalStrip *strip) { - m_strip = strip; + m_strip.reset(new TrueTerminalStrip{strip}); } /** @@ -115,8 +118,8 @@ void TerminalStripDrawer::paint(QPainter *painter) for (const auto &physical_t : m_strip->physicalTerminal()) { //Get the good offset according to how many level have the current physical terminal - const QVector> real_terminal{physical_t->realTerminals()}; - const auto real_t_count{real_terminal.size()}; + const QVector> real_terminal_vector{physical_t->realTerminals()}; + const auto real_t_count{real_terminal_vector.size()}; const auto offset_{4 - real_t_count}; //Loop over real terminals @@ -162,7 +165,7 @@ void TerminalStripDrawer::paint(QPainter *painter) text_rect.setRect(0, 0, rect_.height(), rect_.width()); } - const auto shared_real_terminal{real_terminal[i]}; + const auto shared_real_terminal{real_terminal_vector[i]}; painter->drawText(text_rect, shared_real_terminal ? shared_real_terminal->label() : QLatin1String(), terminals_text_option[index_]); @@ -179,7 +182,8 @@ void TerminalStripDrawer::paint(QPainter *painter) if (shared_real_terminal->isBridged()) { painter->save(); - if (const auto bridge_ = shared_real_terminal->bridge()) + if (QScopedPointer bridge_ { + shared_real_terminal->bridge() }) { const auto x_anchor{terminal_rect.width()/2}; const auto y_anchor {m_pattern->m_bridge_point_y_offset[index_]}; @@ -261,8 +265,8 @@ int TerminalStripDrawer::width() const for (const auto &physical_t : m_strip->physicalTerminal()) { //Get the good offset according to how many level have the current physical terminal - const QVector> real_terminal{physical_t->realTerminals()}; - const auto real_t_count{real_terminal.size()}; + const QVector> real_terminal_vector{physical_t->realTerminals()}; + const auto real_t_count{real_terminal_vector.size()}; const auto offset_{4 - real_t_count}; //Loop over real terminals @@ -283,3 +287,5 @@ int TerminalStripDrawer::width() const return 0; } + +} //End namespace TerminalStripDrawer diff --git a/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h index 681a5b086..d32f434e8 100644 --- a/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h +++ b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h @@ -1,5 +1,5 @@ /* - Copyright 2006-2022 The QElectroTech Team + Copyright 2006-2023 The QElectroTech Team This file is part of QElectroTech. QElectroTech is free software: you can redistribute it and/or modify @@ -19,32 +19,74 @@ #define TERMINALSTRIPDRAWER_H #include + #include "properties/terminalstriplayoutpattern.h" class QPainter; class TerminalStrip; -class TerminalStripDrawer +namespace TerminalStripDrawer { - public: - TerminalStripDrawer(QPointer strip = QPointer(), - QSharedPointer layout = QSharedPointer()); + class AbstractBridgeInterface + { + public: + AbstractBridgeInterface() {} + virtual ~AbstractBridgeInterface() {} + virtual QUuid uuid() const = 0; + }; - void setStrip(TerminalStrip *strip); - void paint(QPainter *painter); - QRectF boundingRect() const; + class AbstractRealTerminalInterface + { + public: + AbstractRealTerminalInterface() {} + virtual ~AbstractRealTerminalInterface() {} + virtual QString label() const = 0; + virtual bool isBridged() const = 0; + virtual AbstractBridgeInterface* bridge() const = 0; + }; - void setLayout(QSharedPointer layout); - bool haveLayout() const; + class AbstractPhysicalTerminalInterface + { + public: + AbstractPhysicalTerminalInterface() {} + virtual ~AbstractPhysicalTerminalInterface() {} + virtual QVector> realTerminals() const = 0; + }; - private: - int height() const; - int width() const; + class AbstractTerminalStripInterface + { + public: + AbstractTerminalStripInterface() {} + virtual ~AbstractTerminalStripInterface() {} + virtual QString installation() const = 0; + virtual QString location() const = 0; + virtual QString name() const = 0; + virtual QVector> physicalTerminal() const = 0; + virtual bool operator()() = 0; + }; - private: - QPointer m_strip; - QSharedPointer m_pattern; - bool m_debug_draw { false }; -}; + class TerminalStripDrawer + { + public: + TerminalStripDrawer(QPointer strip = QPointer(), + QSharedPointer layout = QSharedPointer()); + + void setStrip(TerminalStrip *strip); + void paint(QPainter *painter); + QRectF boundingRect() const; + + void setLayout(QSharedPointer layout); + bool haveLayout() const; + + private: + int height() const; + int width() const; + + private: + QScopedPointer m_strip; + QSharedPointer m_pattern; + bool m_debug_draw { false }; + }; +} #endif // TERMINALSTRIPDRAWER_H diff --git a/sources/TerminalStrip/GraphicsItem/terminalstripitem.h b/sources/TerminalStrip/GraphicsItem/terminalstripitem.h index d60e5ad35..b26a6dd8b 100644 --- a/sources/TerminalStrip/GraphicsItem/terminalstripitem.h +++ b/sources/TerminalStrip/GraphicsItem/terminalstripitem.h @@ -47,9 +47,7 @@ class TerminalStripItem : public QetGraphicsItem QString name() const override; void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override; - void refreshPending(); - void setLayout(QSharedPointer layout); private: @@ -57,7 +55,7 @@ class TerminalStripItem : public QetGraphicsItem private: QPointer m_strip; - TerminalStripDrawer m_drawer; + TerminalStripDrawer::TerminalStripDrawer m_drawer; QUuid m_pending_strip_uuid; }; diff --git a/sources/TerminalStrip/GraphicsItem/trueterminalstrip.cpp b/sources/TerminalStrip/GraphicsItem/trueterminalstrip.cpp new file mode 100644 index 000000000..612f01425 --- /dev/null +++ b/sources/TerminalStrip/GraphicsItem/trueterminalstrip.cpp @@ -0,0 +1,133 @@ +/* + Copyright 2006-2023 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "trueterminalstrip.h" +#include "../physicalterminal.h" +#include "../realterminal.h" +#include "../terminalstrip.h" +#include "../terminalstripbridge.h" + +#include "terminalstripdrawer.h" + +namespace TerminalStripDrawer +{ + TrueTerminalStrip::TrueTerminalStrip(TerminalStrip *strip) : + m_strip { strip } + {} + + + QString TrueTerminalStrip::installation() const + { + if (m_strip) { + return m_strip->installation(); + } else { + return QString(); + } + } + + QString TrueTerminalStrip::location() const + { + if (m_strip) { + return m_strip->location(); + } else { + return QString(); + } + } + + QString TrueTerminalStrip::name() const + { + if (m_strip) { + return m_strip->name(); + } else { + return QString(); + } + } + + QVector> TrueTerminalStrip::physicalTerminal() const + { + QVector> vector_; + if (m_strip) { + for (const auto &phy : m_strip->physicalTerminal()) { + vector_.append(QSharedPointer{ new TruePhysicalTerminal(phy) }); + } + } + + return vector_; + } + + bool TrueTerminalStrip::operator()() + { + return m_strip; + } + + TruePhysicalTerminal::TruePhysicalTerminal(QSharedPointer physical) : + m_physical { physical } + {} + + QVector> TruePhysicalTerminal::realTerminals() const + { + QVector> vector_; + if (m_physical) { + for (const auto &real_ : m_physical->realTerminals()) { + vector_.append(QSharedPointer { new TrueRealTerminal{ real_ }}); + } + } + + return vector_; + } + + TrueRealTerminal::TrueRealTerminal(QSharedPointer real) : + m_real { real } + {} + + QString TrueRealTerminal::label() const + { + if (m_real) { + return m_real->label(); + } else { + return QString(); + } + } + + bool TrueRealTerminal::isBridged() const + { + if (m_real) { + return m_real->isBridged(); + } else { + return false; + } + } + + //Return a raw pointer, the pointer is not managed by this class + AbstractBridgeInterface* TrueRealTerminal::bridge() const + { + return new TrueBridge(m_real->bridge()); + } + + TrueBridge::TrueBridge(QSharedPointer bridge) : + m_bridge { bridge } + {} + + QUuid TrueBridge::uuid() const + { + if (m_bridge) { + return m_bridge->uuid(); + } else { + return QUuid(); + } + } +} diff --git a/sources/TerminalStrip/GraphicsItem/trueterminalstrip.h b/sources/TerminalStrip/GraphicsItem/trueterminalstrip.h new file mode 100644 index 000000000..9853304f7 --- /dev/null +++ b/sources/TerminalStrip/GraphicsItem/trueterminalstrip.h @@ -0,0 +1,78 @@ +/* + Copyright 2006-2023 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef TRUETERMINALSTRIP_H +#define TRUETERMINALSTRIP_H + +#include "terminalstripdrawer.h" + +class TerminalStrip; +class PhysicalTerminal; +class RealTerminal; +class TerminalStripBridge; + +namespace TerminalStripDrawer +{ + class TrueTerminalStrip : public AbstractTerminalStripInterface + { + public: + TrueTerminalStrip(TerminalStrip *strip); + + QString installation() const override; + QString location() const override; + QString name() const override; + QVector> physicalTerminal() const override; + bool operator()() override; + + private: + QPointer m_strip; + }; + + class TruePhysicalTerminal : public AbstractPhysicalTerminalInterface + { + public: + TruePhysicalTerminal(QSharedPointer physical); + QVector> realTerminals() const override; + + private: + QSharedPointer m_physical; + }; + + class TrueRealTerminal : public AbstractRealTerminalInterface + { + public: + TrueRealTerminal(QSharedPointer real); + QString label() const override; + bool isBridged() const override; + AbstractBridgeInterface* bridge() const override; + + private: + QSharedPointer m_real; + }; + + class TrueBridge : public AbstractBridgeInterface + { + public: + TrueBridge(QSharedPointer bridge); + QUuid uuid() const override; + + private: + QSharedPointer m_bridge; + }; +} + +#endif // TRUETERMINALSTRIP_H