From 1f99a40f1b16f476d2bb5391bb3956a9511e24ca Mon Sep 17 00:00:00 2001 From: joshua Date: Fri, 2 Dec 2022 19:35:17 +0100 Subject: [PATCH] Move terminal strip drawer class in is own file --- .../GraphicsItem/terminalstripdrawer.cpp | 226 ++++++++++++ .../GraphicsItem/terminalstripdrawer.h | 45 +++ .../GraphicsItem/terminalstripitem.h | 2 +- .../terminalstriplayoutpattern.cpp | 345 +++++++++--------- .../GraphicsItem/terminalstriplayoutpattern.h | 32 +- 5 files changed, 461 insertions(+), 189 deletions(-) create mode 100644 sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp create mode 100644 sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h diff --git a/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp new file mode 100644 index 000000000..b1c011c09 --- /dev/null +++ b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.cpp @@ -0,0 +1,226 @@ +/* + Copyright 2006-2022 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 "terminalstripdrawer.h" +#include + +#include "../physicalterminal.h" +#include "../realterminal.h" +#include "../terminalstrip.h" +#include "../terminalstripbridge.h" + +/** + * @brief TerminalStripDrawer::TerminalStripDrawer + * @param strip + * @param pattern + */ +TerminalStripDrawer::TerminalStripDrawer(QPointer strip) : + m_strip(strip) +{} + +/** + * @brief TerminalStripDrawer::paint + * @param painter + */ +void TerminalStripDrawer::paint(QPainter *painter) +{ + if (m_strip) + { + //To draw text, QPainter need a Qrect. Instead of create an instance + //for each text, we re-use the same instance of QRect. + QRect text_rect; + painter->save(); + + auto pen_{painter->pen()}; + pen_.setColor(Qt::black); + pen_.setWidth(1); + + auto brush_ = painter->brush(); + brush_.setColor(Qt::white); + + painter->setPen(pen_); + painter->setBrush(brush_); + + //Draw header + painter->drawRect(m_pattern.m_header_rect); + + //Draw the header text + painter->save(); + + if (m_pattern.m_header_text_orientation == Qt::Horizontal) + { + text_rect.setRect(0,m_pattern.m_header_rect.y(),m_pattern.m_header_rect.width(),m_pattern.m_header_rect.height()); + } + else + { + painter->translate(m_pattern.m_header_rect.bottomLeft()); + painter->rotate(270); + text_rect.setRect(0,0,m_pattern.m_header_rect.height(),m_pattern.m_header_rect.width()); + } + + const auto text_{m_strip->installation() + " " + m_strip->location() + " " + m_strip->name()}; + painter->drawText(text_rect, text_, m_pattern.headerTextOption()); + painter->restore(); + + //Move painter pos to next drawing + painter->translate(m_pattern.m_header_rect.width(),0); + + int x_offset{m_pattern.m_header_rect.width()}; + + //Draw spacer + painter->drawRect(m_pattern.m_spacer_rect); + //Move painter pos to next drawing + painter->translate(m_pattern.m_spacer_rect.width(),0); + x_offset += m_pattern.m_spacer_rect.width(); + + + //Draw terminals + const auto terminals_text_rect{m_pattern.m_terminals_text_rect}; + const auto terminals_text_orientation{m_pattern.m_terminals_text_orientation}; + const auto terminals_text_option{m_pattern.terminalsTextOption()}; + QRect terminal_rect; + + QHash> bridges_anchor_points; + + //Loop over physical terminals + 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 auto offset_{4 - real_t_count}; + + //Loop over real terminals + for (auto i=0 ; i= 4) { + break; + } + + terminal_rect = m_pattern.m_terminal_rect[index_]; + //Draw terminal rect + painter->drawRect(terminal_rect); + + //Draw text + painter->save(); + if (terminals_text_orientation[index_] == Qt::Horizontal) + { + text_rect = terminals_text_rect[index_]; + } + else + { + const auto rect_{terminals_text_rect[index_]}; + painter->translate(rect_.bottomLeft()); + painter->rotate(270); + text_rect.setRect(0, 0, rect_.height(), rect_.width()); + } + + const auto shared_real_terminal{real_terminal[i]}; + painter->drawText(text_rect, + shared_real_terminal ? shared_real_terminal->label() : QLatin1String(), + terminals_text_option[index_]); + painter->restore(); + + //Add bridge anchor + if (shared_real_terminal->isBridged()) + { + painter->save(); + if (const auto bridge_ = shared_real_terminal->bridge()) + { + const auto anchor_center{m_pattern.m_bridge_point_d/2}; + painter->setBrush(Qt::SolidPattern); + painter->drawEllipse(QPointF{terminal_rect.width()/2, m_pattern.m_bridge_point_y_offset[index_]}, + anchor_center, + anchor_center); + + auto anchor_points{bridges_anchor_points.value(bridge_->uuid())}; + anchor_points.append(QPointF{x_offset + terminal_rect.width()/2, + m_pattern.m_bridge_point_y_offset[index_]}); + bridges_anchor_points.insert(bridge_->uuid(), anchor_points); + } + painter->restore(); + } + + //Move painter pos to next drawing + painter->translate(terminal_rect.width(),0); + x_offset += terminal_rect.width(); + } + } + + painter->restore(); + + //Draw the bridges + for (const auto &points_ : qAsConst(bridges_anchor_points)) + { + painter->save(); + auto pen_{painter->pen()}; + pen_.setWidth(2); + painter->setPen(pen_); + painter->drawPolyline(QPolygonF{points_}); + painter->restore(); + } + } +} + +QRectF TerminalStripDrawer::boundingRect() const +{ + return QRect{0, 0, width(), height()};; +} + +int TerminalStripDrawer::height() const +{ + auto height_{m_pattern.m_header_rect.y() + m_pattern.m_header_rect.height()}; + + height_ = std::max(height_, m_pattern.m_spacer_rect.y() + m_pattern.m_spacer_rect.height()); + + for (const auto &rect : m_pattern.m_terminal_rect) { + height_ = std::max(height_, rect.y() + rect.height()); + } + + return height_; +} + +int TerminalStripDrawer::width() const +{ + int width_{m_pattern.m_header_rect.width() + m_pattern.m_spacer_rect.width()}; + + if (m_strip) + { + //Loop over physical terminals + 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 auto offset_{4 - real_t_count}; + + //Loop over real terminals + for (auto i=0 ; i= 4) { + break; + } + + width_ += m_pattern.m_terminal_rect[index_].width(); + } + } + } + + return width_; +} diff --git a/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h new file mode 100644 index 000000000..c59e5f8e4 --- /dev/null +++ b/sources/TerminalStrip/GraphicsItem/terminalstripdrawer.h @@ -0,0 +1,45 @@ +/* + Copyright 2006-2022 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 TERMINALSTRIPDRAWER_H +#define TERMINALSTRIPDRAWER_H + +#include + +#include "terminalstriplayoutpattern.h" + +class QPainter; +class TerminalStrip; + +class TerminalStripDrawer +{ + public: + TerminalStripDrawer(QPointer strip); + void paint(QPainter *painter); + + QRectF boundingRect() const; + + private: + int height() const; + int width() const; + + private: + QPointer m_strip; + TerminalStripLayoutPattern m_pattern; +}; + +#endif // TERMINALSTRIPDRAWER_H diff --git a/sources/TerminalStrip/GraphicsItem/terminalstripitem.h b/sources/TerminalStrip/GraphicsItem/terminalstripitem.h index 78343c423..4a8371592 100644 --- a/sources/TerminalStrip/GraphicsItem/terminalstripitem.h +++ b/sources/TerminalStrip/GraphicsItem/terminalstripitem.h @@ -20,7 +20,7 @@ #include -#include "terminalstriplayoutpattern.h" +#include "terminalstripdrawer.h" #include "../../qetgraphicsitem/qetgraphicsitem.h" class TerminalStrip; diff --git a/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.cpp b/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.cpp index c10c2e1e5..2df7976d5 100644 --- a/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.cpp +++ b/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.cpp @@ -16,215 +16,216 @@ along with QElectroTech. If not, see . */ #include "terminalstriplayoutpattern.h" +#include -#include "../physicalterminal.h" -#include "../realterminal.h" -#include "../terminalstrip.h" -#include "../terminalstripbridge.h" +//#include "../physicalterminal.h" +//#include "../realterminal.h" +//#include "../terminalstrip.h" +//#include "../terminalstripbridge.h" -#include +//#include -/** - * @brief TerminalStripDrawer::TerminalStripDrawer - * @param strip - * @param pattern - */ -TerminalStripDrawer::TerminalStripDrawer(QPointer strip) : - m_strip(strip) -{} +///** +// * @brief TerminalStripDrawer::TerminalStripDrawer +// * @param strip +// * @param pattern +// */ +//TerminalStripDrawer::TerminalStripDrawer(QPointer strip) : +// m_strip(strip) +//{} -/** - * @brief TerminalStripDrawer::paint - * @param painter - */ -void TerminalStripDrawer::paint(QPainter *painter) -{ - if (m_strip) - { - //To draw text, QPainter need a Qrect. Instead of create an instance - //for each text, we re-use the same instance of QRect. - QRect text_rect; - painter->save(); +///** +// * @brief TerminalStripDrawer::paint +// * @param painter +// */ +//void TerminalStripDrawer::paint(QPainter *painter) +//{ +// if (m_strip) +// { +// //To draw text, QPainter need a Qrect. Instead of create an instance +// //for each text, we re-use the same instance of QRect. +// QRect text_rect; +// painter->save(); - auto pen_{painter->pen()}; - pen_.setColor(Qt::black); - pen_.setWidth(1); +// auto pen_{painter->pen()}; +// pen_.setColor(Qt::black); +// pen_.setWidth(1); - auto brush_ = painter->brush(); - brush_.setColor(Qt::white); +// auto brush_ = painter->brush(); +// brush_.setColor(Qt::white); - painter->setPen(pen_); - painter->setBrush(brush_); +// painter->setPen(pen_); +// painter->setBrush(brush_); - //Draw header - painter->drawRect(m_pattern.m_header_rect); +// //Draw header +// painter->drawRect(m_pattern.m_header_rect); - //Draw the header text - painter->save(); +// //Draw the header text +// painter->save(); - if (m_pattern.m_header_text_orientation == Qt::Horizontal) - { - text_rect.setRect(0,m_pattern.m_header_rect.y(),m_pattern.m_header_rect.width(),m_pattern.m_header_rect.height()); - } - else - { - painter->translate(m_pattern.m_header_rect.bottomLeft()); - painter->rotate(270); - text_rect.setRect(0,0,m_pattern.m_header_rect.height(),m_pattern.m_header_rect.width()); - } +// if (m_pattern.m_header_text_orientation == Qt::Horizontal) +// { +// text_rect.setRect(0,m_pattern.m_header_rect.y(),m_pattern.m_header_rect.width(),m_pattern.m_header_rect.height()); +// } +// else +// { +// painter->translate(m_pattern.m_header_rect.bottomLeft()); +// painter->rotate(270); +// text_rect.setRect(0,0,m_pattern.m_header_rect.height(),m_pattern.m_header_rect.width()); +// } - const auto text_{m_strip->installation() + " " + m_strip->location() + " " + m_strip->name()}; - painter->drawText(text_rect, text_, m_pattern.headerTextOption()); - painter->restore(); +// const auto text_{m_strip->installation() + " " + m_strip->location() + " " + m_strip->name()}; +// painter->drawText(text_rect, text_, m_pattern.headerTextOption()); +// painter->restore(); - //Move painter pos to next drawing - painter->translate(m_pattern.m_header_rect.width(),0); +// //Move painter pos to next drawing +// painter->translate(m_pattern.m_header_rect.width(),0); - int x_offset{m_pattern.m_header_rect.width()}; +// int x_offset{m_pattern.m_header_rect.width()}; - //Draw spacer - painter->drawRect(m_pattern.m_spacer_rect); - //Move painter pos to next drawing - painter->translate(m_pattern.m_spacer_rect.width(),0); - x_offset += m_pattern.m_spacer_rect.width(); +// //Draw spacer +// painter->drawRect(m_pattern.m_spacer_rect); +// //Move painter pos to next drawing +// painter->translate(m_pattern.m_spacer_rect.width(),0); +// x_offset += m_pattern.m_spacer_rect.width(); - //Draw terminals - const auto terminals_text_rect{m_pattern.m_terminals_text_rect}; - const auto terminals_text_orientation{m_pattern.m_terminals_text_orientation}; - const auto terminals_text_option{m_pattern.terminalsTextOption()}; - QRect terminal_rect; +// //Draw terminals +// const auto terminals_text_rect{m_pattern.m_terminals_text_rect}; +// const auto terminals_text_orientation{m_pattern.m_terminals_text_orientation}; +// const auto terminals_text_option{m_pattern.terminalsTextOption()}; +// QRect terminal_rect; - QHash> bridges_anchor_points; +// QHash> bridges_anchor_points; - //Loop over physical terminals - 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 auto offset_{4 - real_t_count}; +// //Loop over physical terminals +// 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 auto offset_{4 - real_t_count}; - //Loop over real terminals - for (auto i=0 ; i= 4) { - break; - } +// //Loop over real terminals +// for (auto i=0 ; i= 4) { +// break; +// } - terminal_rect = m_pattern.m_terminal_rect[index_]; - //Draw terminal rect - painter->drawRect(terminal_rect); +// terminal_rect = m_pattern.m_terminal_rect[index_]; +// //Draw terminal rect +// painter->drawRect(terminal_rect); - //Draw text - painter->save(); - if (terminals_text_orientation[index_] == Qt::Horizontal) - { - text_rect = terminals_text_rect[index_]; - } - else - { - const auto rect_{terminals_text_rect[index_]}; - painter->translate(rect_.bottomLeft()); - painter->rotate(270); - text_rect.setRect(0, 0, rect_.height(), rect_.width()); - } +// //Draw text +// painter->save(); +// if (terminals_text_orientation[index_] == Qt::Horizontal) +// { +// text_rect = terminals_text_rect[index_]; +// } +// else +// { +// const auto rect_{terminals_text_rect[index_]}; +// painter->translate(rect_.bottomLeft()); +// painter->rotate(270); +// text_rect.setRect(0, 0, rect_.height(), rect_.width()); +// } - const auto shared_real_terminal{real_terminal[i]}; - painter->drawText(text_rect, - shared_real_terminal ? shared_real_terminal->label() : QLatin1String(), - terminals_text_option[index_]); - painter->restore(); +// const auto shared_real_terminal{real_terminal[i]}; +// painter->drawText(text_rect, +// shared_real_terminal ? shared_real_terminal->label() : QLatin1String(), +// terminals_text_option[index_]); +// painter->restore(); - //Add bridge anchor - if (shared_real_terminal->isBridged()) - { - painter->save(); - if (const auto bridge_ = shared_real_terminal->bridge()) - { - const auto anchor_center{m_pattern.m_bridge_point_d/2}; - painter->setBrush(Qt::SolidPattern); - painter->drawEllipse(QPointF{terminal_rect.width()/2, m_pattern.m_bridge_point_y_offset[index_]}, - anchor_center, - anchor_center); +// //Add bridge anchor +// if (shared_real_terminal->isBridged()) +// { +// painter->save(); +// if (const auto bridge_ = shared_real_terminal->bridge()) +// { +// const auto anchor_center{m_pattern.m_bridge_point_d/2}; +// painter->setBrush(Qt::SolidPattern); +// painter->drawEllipse(QPointF{terminal_rect.width()/2, m_pattern.m_bridge_point_y_offset[index_]}, +// anchor_center, +// anchor_center); - auto anchor_points{bridges_anchor_points.value(bridge_->uuid())}; - anchor_points.append(QPointF{x_offset + terminal_rect.width()/2, - m_pattern.m_bridge_point_y_offset[index_]}); - bridges_anchor_points.insert(bridge_->uuid(), anchor_points); - } - painter->restore(); - } +// auto anchor_points{bridges_anchor_points.value(bridge_->uuid())}; +// anchor_points.append(QPointF{x_offset + terminal_rect.width()/2, +// m_pattern.m_bridge_point_y_offset[index_]}); +// bridges_anchor_points.insert(bridge_->uuid(), anchor_points); +// } +// painter->restore(); +// } - //Move painter pos to next drawing - painter->translate(terminal_rect.width(),0); - x_offset += terminal_rect.width(); - } - } +// //Move painter pos to next drawing +// painter->translate(terminal_rect.width(),0); +// x_offset += terminal_rect.width(); +// } +// } - painter->restore(); +// painter->restore(); - //Draw the bridges - for (const auto &points_ : qAsConst(bridges_anchor_points)) - { - painter->save(); - auto pen_{painter->pen()}; - pen_.setWidth(2); - painter->setPen(pen_); - painter->drawPolyline(QPolygonF{points_}); - painter->restore(); - } - } -} +// //Draw the bridges +// for (const auto &points_ : qAsConst(bridges_anchor_points)) +// { +// painter->save(); +// auto pen_{painter->pen()}; +// pen_.setWidth(2); +// painter->setPen(pen_); +// painter->drawPolyline(QPolygonF{points_}); +// painter->restore(); +// } +// } +//} -QRectF TerminalStripDrawer::boundingRect() const -{ - return QRect{0, 0, width(), height()};; -} +//QRectF TerminalStripDrawer::boundingRect() const +//{ +// return QRect{0, 0, width(), height()};; +//} -int TerminalStripDrawer::height() const -{ - auto height_{m_pattern.m_header_rect.y() + m_pattern.m_header_rect.height()}; +//int TerminalStripDrawer::height() const +//{ +// auto height_{m_pattern.m_header_rect.y() + m_pattern.m_header_rect.height()}; - height_ = std::max(height_, m_pattern.m_spacer_rect.y() + m_pattern.m_spacer_rect.height()); +// height_ = std::max(height_, m_pattern.m_spacer_rect.y() + m_pattern.m_spacer_rect.height()); - for (const auto &rect : m_pattern.m_terminal_rect) { - height_ = std::max(height_, rect.y() + rect.height()); - } +// for (const auto &rect : m_pattern.m_terminal_rect) { +// height_ = std::max(height_, rect.y() + rect.height()); +// } - return height_; -} +// return height_; +//} -int TerminalStripDrawer::width() const -{ - int width_{m_pattern.m_header_rect.width() + m_pattern.m_spacer_rect.width()}; +//int TerminalStripDrawer::width() const +//{ +// int width_{m_pattern.m_header_rect.width() + m_pattern.m_spacer_rect.width()}; - if (m_strip) - { - //Loop over physical terminals - 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 auto offset_{4 - real_t_count}; +// if (m_strip) +// { +// //Loop over physical terminals +// 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 auto offset_{4 - real_t_count}; - //Loop over real terminals - for (auto i=0 ; i= 4) { - break; - } +// //Loop over real terminals +// for (auto i=0 ; i= 4) { +// break; +// } - width_ += m_pattern.m_terminal_rect[index_].width(); - } - } - } +// width_ += m_pattern.m_terminal_rect[index_].width(); +// } +// } +// } - return width_; -} +// return width_; +//} TerminalStripLayoutPattern::TerminalStripLayoutPattern() { diff --git a/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.h b/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.h index 65c5db3a5..451db1d6c 100644 --- a/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.h +++ b/sources/TerminalStrip/GraphicsItem/terminalstriplayoutpattern.h @@ -18,14 +18,14 @@ #ifndef TERMINALSTRIPLAYOUTPATTERN_H #define TERMINALSTRIPLAYOUTPATTERN_H -#include +//#include #include #include #include #include -class QPainter; -class TerminalStrip; +//class QPainter; +//class TerminalStrip; /** * @brief The TerminalStripLayoutPattern class @@ -110,21 +110,21 @@ class TerminalStripLayoutPattern }; }; -class TerminalStripDrawer -{ - public: - TerminalStripDrawer(QPointer strip); - void paint(QPainter *painter); +//class TerminalStripDrawer +//{ +// public: +// TerminalStripDrawer(QPointer strip); +// void paint(QPainter *painter); - QRectF boundingRect() const; +// QRectF boundingRect() const; - private: - int height() const; - int width() const; +// private: +// int height() const; +// int width() const; - private: - QPointer m_strip; - TerminalStripLayoutPattern m_pattern; -}; +// private: +// QPointer m_strip; +// TerminalStripLayoutPattern m_pattern; +//}; #endif // TERMINALSTRIPLAYOUTPATTERN_H