Add AbstractTerminalStrip class to be used by TerminalStripDrawer class

This commit is contained in:
joshua
2023-05-15 22:22:12 +02:00
parent 752fe032e7
commit 48f9ef7632
5 changed files with 286 additions and 29 deletions

View File

@@ -22,6 +22,9 @@
#include "../realterminal.h" #include "../realterminal.h"
#include "../terminalstrip.h" #include "../terminalstrip.h"
#include "../terminalstripbridge.h" #include "../terminalstripbridge.h"
#include "trueterminalstrip.h"
namespace TerminalStripDrawer {
/** /**
* @brief TerminalStripDrawer::TerminalStripDrawer * @brief TerminalStripDrawer::TerminalStripDrawer
@@ -30,13 +33,13 @@
*/ */
TerminalStripDrawer::TerminalStripDrawer(QPointer<TerminalStrip> strip, TerminalStripDrawer::TerminalStripDrawer(QPointer<TerminalStrip> strip,
QSharedPointer<TerminalStripLayoutPattern> layout) : QSharedPointer<TerminalStripLayoutPattern> layout) :
m_strip(strip), m_strip{new TrueTerminalStrip{strip.data()}},
m_pattern(layout) m_pattern(layout)
{} {}
void TerminalStripDrawer::setStrip(TerminalStrip *strip) 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()) for (const auto &physical_t : m_strip->physicalTerminal())
{ {
//Get the good offset according to how many level have the current physical terminal //Get the good offset according to how many level have the current physical terminal
const QVector<QSharedPointer<RealTerminal>> real_terminal{physical_t->realTerminals()}; const QVector<QSharedPointer<AbstractRealTerminalInterface>> real_terminal_vector{physical_t->realTerminals()};
const auto real_t_count{real_terminal.size()}; const auto real_t_count{real_terminal_vector.size()};
const auto offset_{4 - real_t_count}; const auto offset_{4 - real_t_count};
//Loop over real terminals //Loop over real terminals
@@ -162,7 +165,7 @@ void TerminalStripDrawer::paint(QPainter *painter)
text_rect.setRect(0, 0, rect_.height(), rect_.width()); 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, painter->drawText(text_rect,
shared_real_terminal ? shared_real_terminal->label() : QLatin1String(), shared_real_terminal ? shared_real_terminal->label() : QLatin1String(),
terminals_text_option[index_]); terminals_text_option[index_]);
@@ -179,7 +182,8 @@ void TerminalStripDrawer::paint(QPainter *painter)
if (shared_real_terminal->isBridged()) if (shared_real_terminal->isBridged())
{ {
painter->save(); painter->save();
if (const auto bridge_ = shared_real_terminal->bridge()) if (QScopedPointer<AbstractBridgeInterface> bridge_ {
shared_real_terminal->bridge() })
{ {
const auto x_anchor{terminal_rect.width()/2}; const auto x_anchor{terminal_rect.width()/2};
const auto y_anchor {m_pattern->m_bridge_point_y_offset[index_]}; 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()) for (const auto &physical_t : m_strip->physicalTerminal())
{ {
//Get the good offset according to how many level have the current physical terminal //Get the good offset according to how many level have the current physical terminal
const QVector<QSharedPointer<RealTerminal>> real_terminal{physical_t->realTerminals()}; const QVector<QSharedPointer<AbstractRealTerminalInterface>> real_terminal_vector{physical_t->realTerminals()};
const auto real_t_count{real_terminal.size()}; const auto real_t_count{real_terminal_vector.size()};
const auto offset_{4 - real_t_count}; const auto offset_{4 - real_t_count};
//Loop over real terminals //Loop over real terminals
@@ -283,3 +287,5 @@ int TerminalStripDrawer::width() const
return 0; return 0;
} }
} //End namespace TerminalStripDrawer

View File

@@ -1,5 +1,5 @@
/* /*
Copyright 2006-2022 The QElectroTech Team Copyright 2006-2023 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
@@ -19,32 +19,74 @@
#define TERMINALSTRIPDRAWER_H #define TERMINALSTRIPDRAWER_H
#include <QPointer> #include <QPointer>
#include "properties/terminalstriplayoutpattern.h" #include "properties/terminalstriplayoutpattern.h"
class QPainter; class QPainter;
class TerminalStrip; class TerminalStrip;
class TerminalStripDrawer namespace TerminalStripDrawer
{ {
public: class AbstractBridgeInterface
TerminalStripDrawer(QPointer<TerminalStrip> strip = QPointer<TerminalStrip>(), {
QSharedPointer<TerminalStripLayoutPattern> layout = QSharedPointer<TerminalStripLayoutPattern>()); public:
AbstractBridgeInterface() {}
virtual ~AbstractBridgeInterface() {}
virtual QUuid uuid() const = 0;
};
void setStrip(TerminalStrip *strip); class AbstractRealTerminalInterface
void paint(QPainter *painter); {
QRectF boundingRect() const; public:
AbstractRealTerminalInterface() {}
virtual ~AbstractRealTerminalInterface() {}
virtual QString label() const = 0;
virtual bool isBridged() const = 0;
virtual AbstractBridgeInterface* bridge() const = 0;
};
void setLayout(QSharedPointer<TerminalStripLayoutPattern> layout); class AbstractPhysicalTerminalInterface
bool haveLayout() const; {
public:
AbstractPhysicalTerminalInterface() {}
virtual ~AbstractPhysicalTerminalInterface() {}
virtual QVector<QSharedPointer<AbstractRealTerminalInterface>> realTerminals() const = 0;
};
private: class AbstractTerminalStripInterface
int height() const; {
int width() const; public:
AbstractTerminalStripInterface() {}
virtual ~AbstractTerminalStripInterface() {}
virtual QString installation() const = 0;
virtual QString location() const = 0;
virtual QString name() const = 0;
virtual QVector<QSharedPointer<AbstractPhysicalTerminalInterface>> physicalTerminal() const = 0;
virtual bool operator()() = 0;
};
private: class TerminalStripDrawer
QPointer<TerminalStrip> m_strip; {
QSharedPointer<TerminalStripLayoutPattern> m_pattern; public:
bool m_debug_draw { false }; TerminalStripDrawer(QPointer<TerminalStrip> strip = QPointer<TerminalStrip>(),
}; QSharedPointer<TerminalStripLayoutPattern> layout = QSharedPointer<TerminalStripLayoutPattern>());
void setStrip(TerminalStrip *strip);
void paint(QPainter *painter);
QRectF boundingRect() const;
void setLayout(QSharedPointer<TerminalStripLayoutPattern> layout);
bool haveLayout() const;
private:
int height() const;
int width() const;
private:
QScopedPointer <AbstractTerminalStripInterface> m_strip;
QSharedPointer<TerminalStripLayoutPattern> m_pattern;
bool m_debug_draw { false };
};
}
#endif // TERMINALSTRIPDRAWER_H #endif // TERMINALSTRIPDRAWER_H

View File

@@ -47,9 +47,7 @@ class TerminalStripItem : public QetGraphicsItem
QString name() const override; QString name() const override;
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override; void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
void refreshPending(); void refreshPending();
void setLayout(QSharedPointer<TerminalStripLayoutPattern> layout); void setLayout(QSharedPointer<TerminalStripLayoutPattern> layout);
private: private:
@@ -57,7 +55,7 @@ class TerminalStripItem : public QetGraphicsItem
private: private:
QPointer<TerminalStrip> m_strip; QPointer<TerminalStrip> m_strip;
TerminalStripDrawer m_drawer; TerminalStripDrawer::TerminalStripDrawer m_drawer;
QUuid m_pending_strip_uuid; QUuid m_pending_strip_uuid;
}; };

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<QSharedPointer<AbstractPhysicalTerminalInterface>> TrueTerminalStrip::physicalTerminal() const
{
QVector<QSharedPointer<AbstractPhysicalTerminalInterface>> vector_;
if (m_strip) {
for (const auto &phy : m_strip->physicalTerminal()) {
vector_.append(QSharedPointer<AbstractPhysicalTerminalInterface>{ new TruePhysicalTerminal(phy) });
}
}
return vector_;
}
bool TrueTerminalStrip::operator()()
{
return m_strip;
}
TruePhysicalTerminal::TruePhysicalTerminal(QSharedPointer<PhysicalTerminal> physical) :
m_physical { physical }
{}
QVector<QSharedPointer<AbstractRealTerminalInterface>> TruePhysicalTerminal::realTerminals() const
{
QVector<QSharedPointer<AbstractRealTerminalInterface>> vector_;
if (m_physical) {
for (const auto &real_ : m_physical->realTerminals()) {
vector_.append(QSharedPointer<AbstractRealTerminalInterface> { new TrueRealTerminal{ real_ }});
}
}
return vector_;
}
TrueRealTerminal::TrueRealTerminal(QSharedPointer<RealTerminal> 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<TerminalStripBridge> bridge) :
m_bridge { bridge }
{}
QUuid TrueBridge::uuid() const
{
if (m_bridge) {
return m_bridge->uuid();
} else {
return QUuid();
}
}
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<QSharedPointer<AbstractPhysicalTerminalInterface>> physicalTerminal() const override;
bool operator()() override;
private:
QPointer<TerminalStrip> m_strip;
};
class TruePhysicalTerminal : public AbstractPhysicalTerminalInterface
{
public:
TruePhysicalTerminal(QSharedPointer<PhysicalTerminal> physical);
QVector<QSharedPointer<AbstractRealTerminalInterface>> realTerminals() const override;
private:
QSharedPointer<PhysicalTerminal> m_physical;
};
class TrueRealTerminal : public AbstractRealTerminalInterface
{
public:
TrueRealTerminal(QSharedPointer<RealTerminal> real);
QString label() const override;
bool isBridged() const override;
AbstractBridgeInterface* bridge() const override;
private:
QSharedPointer<RealTerminal> m_real;
};
class TrueBridge : public AbstractBridgeInterface
{
public:
TrueBridge(QSharedPointer<TerminalStripBridge> bridge);
QUuid uuid() const override;
private:
QSharedPointer<TerminalStripBridge> m_bridge;
};
}
#endif // TRUETERMINALSTRIP_H