Merge branch 'terminal_strip'

* terminal_strip:
  Terminal strip item can saved / loaded to .qet file
  See previous commit...
  Move terminal strip drawer class in is own file
  Fix wrong use of QStringLiteral and QLatin1String
  Double click a TerminalStripItem open the editor
  Minor change about checkable QAction of QetDiagramEditor
  Minor : corrects a minor aesthetic defect when unbridge terminals
  Revamp code
  Add and move terminal strip item are now managed by undo command
  TerminalStripItem : Draw terminal bridge
  Terminal strip item can be added to diagram
  Minor : add QGIUtility namespace
This commit is contained in:
joshua
2023-01-02 19:40:08 +01:00
45 changed files with 1628 additions and 276 deletions

View File

@@ -0,0 +1,231 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "terminalstripdrawer.h"
#include <QPainter>
#include "../physicalterminal.h"
#include "../realterminal.h"
#include "../terminalstrip.h"
#include "../terminalstripbridge.h"
/**
* @brief TerminalStripDrawer::TerminalStripDrawer
* @param strip
* @param pattern
*/
TerminalStripDrawer::TerminalStripDrawer(QPointer<TerminalStrip> strip) :
m_strip(strip)
{}
void TerminalStripDrawer::setStrip(TerminalStrip *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<QUuid, QVector<QPointF>> 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<QSharedPointer<RealTerminal>> 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<real_t_count ; ++i)
{
const auto index_ = offset_ + i;
if (index_ >= 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<QSharedPointer<RealTerminal>> 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<real_t_count ; ++i)
{
const auto index_ = offset_ + i;
if (index_ >= 4) {
break;
}
width_ += m_pattern.m_terminal_rect[index_].width();
}
}
}
return width_;
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef TERMINALSTRIPDRAWER_H
#define TERMINALSTRIPDRAWER_H
#include <QPointer>
#include "terminalstriplayoutpattern.h"
class QPainter;
class TerminalStrip;
class TerminalStripDrawer
{
public:
TerminalStripDrawer(QPointer<TerminalStrip> strip = QPointer<TerminalStrip>());
void setStrip(TerminalStrip *strip);
void paint(QPainter *painter);
QRectF boundingRect() const;
private:
int height() const;
int width() const;
private:
QPointer<TerminalStrip> m_strip;
TerminalStripLayoutPattern m_pattern;
};
#endif // TERMINALSTRIPDRAWER_H

View File

@@ -0,0 +1,107 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "terminalstripitem.h"
#include "../diagram.h"
#include "../../qetgraphicsitem/qgraphicsitemutility.h"
#include "../terminalstrip.h"
#include "../ui/terminalstripeditorwindow.h"
TerminalStripItem::TerminalStripItem(QPointer<TerminalStrip> strip, QGraphicsItem *parent) :
QetGraphicsItem{parent},
m_strip{strip},
m_drawer{strip}
{
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
setAcceptHoverEvents(true);
}
TerminalStripItem::TerminalStripItem(QGraphicsItem *parent) :
QetGraphicsItem { parent }
{
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
setAcceptHoverEvents(true);
}
void TerminalStripItem::setTerminalStrip(TerminalStrip *strip)
{
m_strip = strip;
m_drawer.setStrip(strip);
m_pending_strip_uuid = QUuid();
}
/**
* @brief TerminalStripItem::terminalStrip
* @return The strip drawed by this item
*/
QPointer<TerminalStrip> TerminalStripItem::terminalStrip() const {
return m_strip;
}
void TerminalStripItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
m_drawer.paint(painter);
if (isSelected() || isHovered())
{
QGIUtility::drawBoundingRectSelection(this, painter);
}
}
QRectF TerminalStripItem::boundingRect() const
{
auto br_ = m_drawer.boundingRect();
br_.adjust(-5,-5,5,5);
return br_;
}
/**
* @brief TerminalStripItem::name
* @return usual name of this item
*/
QString TerminalStripItem::name() const {
return tr("plan de bornes");
}
void TerminalStripItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED (event);
if (m_strip) {
TerminalStripEditorWindow::edit(m_strip);
}
}
void TerminalStripItem::refreshPending()
{
if (!m_pending_strip_uuid.isNull()
&& diagram()
&& diagram()->project())
{
for (const auto &strip_ : diagram()->project()->terminalStrip()) {
if (strip_->uuid() == m_pending_strip_uuid) {
setTerminalStrip(strip_);
break;
}
}
}
}

View File

@@ -0,0 +1,60 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef TERMINALSTRIPITEM_H
#define TERMINALSTRIPITEM_H
#include <QGraphicsObject>
#include <QUuid>
#include "terminalstripdrawer.h"
#include "../../qetgraphicsitem/qetgraphicsitem.h"
class TerminalStrip;
class TerminalStripItem : public QetGraphicsItem
{
friend class TerminalStripItemXml;
Q_OBJECT
public:
TerminalStripItem(QPointer<TerminalStrip> strip, QGraphicsItem *parent = nullptr);
TerminalStripItem(QGraphicsItem *parent = nullptr);
void setTerminalStrip(TerminalStrip *strip);
QPointer<TerminalStrip> terminalStrip() const;
enum {Type = UserType + 1011};
int type() const override {return Type;}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
QRectF boundingRect() const override;
QString name() const override;
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
void refreshPending();
private:
QPointer<TerminalStrip> m_strip;
TerminalStripDrawer m_drawer;
QUuid m_pending_strip_uuid;
};
#endif // TERMINALSTRIPITEM_H

View File

@@ -0,0 +1,78 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "terminalstriplayoutpattern.h"
#include <QDebug>
TerminalStripLayoutPattern::TerminalStripLayoutPattern()
{
updateHeaderTextOption();
updateTerminalsTextOption();
}
void TerminalStripLayoutPattern::setHeaderTextAlignment(const Qt::Alignment &alignment)
{
m_header_text_alignment = alignment;
updateHeaderTextOption();
}
Qt::Alignment TerminalStripLayoutPattern::headerTextAlignment() const
{
return m_header_text_alignment;
}
QTextOption TerminalStripLayoutPattern::headerTextOption() const {
return m_header_text_option;
}
void TerminalStripLayoutPattern::setTerminalsTextAlignment(const QVector<Qt::Alignment> &alignment)
{
m_terminals_text_alignment = alignment;
updateTerminalsTextOption();
}
QVector<Qt::Alignment> TerminalStripLayoutPattern::terminalsTextAlignment() const
{
return m_terminals_text_alignment;
}
QVector<QTextOption> TerminalStripLayoutPattern::terminalsTextOption() const
{
return m_terminals_text_option;
}
void TerminalStripLayoutPattern::updateHeaderTextOption()
{
m_header_text_option.setAlignment(m_header_text_alignment);
m_header_text_option.setWrapMode(QTextOption::WordWrap);
}
void TerminalStripLayoutPattern::updateTerminalsTextOption()
{
if (m_terminals_text_option.size() ==
m_terminals_text_alignment.size())
{
for (auto i = 0 ; i<m_terminals_text_option.size() ; ++i)
{
m_terminals_text_option[i].setAlignment(m_terminals_text_alignment.at(i));
m_terminals_text_option[i].setWrapMode(QTextOption::WordWrap);
}
}
else {
qDebug() << "TerminalStripLayoutPattern::updateTerminalsTextOption() : Wrong vector size";
}
}

View File

@@ -0,0 +1,109 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef TERMINALSTRIPLAYOUTPATTERN_H
#define TERMINALSTRIPLAYOUTPATTERN_H
#include <QSize>
#include <QTextOption>
#include <QVector>
#include <QRect>
/**
* @brief The TerminalStripLayoutPattern class
* A class with all values used to define how a terminal strip must be drawn.
* Most of the value are public, some values are private and have getter / setter
* because when these values change we need to compute the change.
*
* The values with name '_y_offset' mean offset is relating to the top
* of the QGraphicsItem used to display the terminal strip.
*
* The terminal strip can display up to 4 terminal level,
* the value used for multilevel terminal are stored in several QVector (m_terminal_y_offset, m_terminal_height, m_bridge_point_y_offset).
* The order of the values are from the most back terminal to the front terminal.
*/
class TerminalStripLayoutPattern
{
public:
TerminalStripLayoutPattern();
//Header of terminal strip
QRect m_header_rect{0,30,50,130};
Qt::Orientation m_header_text_orientation{Qt::Horizontal};
void setHeaderTextAlignment(const Qt::Alignment &alignment);
Qt::Alignment headerTextAlignment() const;
QTextOption headerTextOption() const;
//Spacer between the header and the terminals
QRect m_spacer_rect{0, 50, 10, 90};
//Terminals
QVector<QRect> m_terminal_rect
{
QRect{0, 0, 20, 190},
QRect{0, 10, 20, 170},
QRect{0, 20, 20, 150},
QRect{0, 30, 20, 130}
};
void setTerminalsTextAlignment(const QVector<Qt::Alignment> &alignment);
QVector<Qt::Alignment> terminalsTextAlignment() const;
QVector<QTextOption> terminalsTextOption() const;
QVector<QRect> m_terminals_text_rect
{
QRect{0,35,20,50},
QRect{0,35,20,50},
QRect{0,35,20,50},
QRect{0,35,20,50}
};
QVector<Qt::Orientation> m_terminals_text_orientation
{
Qt::Vertical,
Qt::Vertical,
Qt::Vertical,
Qt::Vertical
};
int m_bridge_point_d{5};
QVector<int> m_bridge_point_y_offset{50,70,90,110};
private:
void updateHeaderTextOption();
void updateTerminalsTextOption();
private:
Qt::Alignment m_header_text_alignment{Qt::AlignCenter};
QTextOption m_header_text_option;
QVector<Qt::Alignment> m_terminals_text_alignment
{
Qt::AlignRight | Qt::AlignVCenter,
Qt::AlignRight | Qt::AlignVCenter,
Qt::AlignRight | Qt::AlignVCenter,
Qt::AlignRight | Qt::AlignVCenter
};
QVector<QTextOption> m_terminals_text_option
{
QTextOption(),
QTextOption(),
QTextOption(),
QTextOption()
};
};
#endif // TERMINALSTRIPLAYOUTPATTERN_H

View File

@@ -16,6 +16,7 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bridgeterminalscommand.h"
#include "../terminalstripbridge.h"
BridgeTerminalsCommand::BridgeTerminalsCommand(TerminalStrip *strip,
QVector<QSharedPointer<RealTerminal>> real_terminal,
@@ -57,8 +58,18 @@ UnBridgeTerminalsCommand::UnBridgeTerminalsCommand(TerminalStrip *strip,
if (strip->canUnBridge(real_terminal))
{
m_terminals = real_terminal;
m_bridge = strip->isBridged(real_terminal.first());
m_bridge = strip->isBridged(real_terminal.first());
//If bridge have one more terminals than @real_terminal
//that mean every terminals of the bridge must be unbridged by this undo command,
//else the single terminal who's not umbridged by this undo command
//continue to have a bridge (to nothing) and this nowhere bridge is visible
//in the terminal strip graphic item and terminal strip editor dialog.
if (m_bridge->realTerminals().size() == real_terminal.size() + 1) {
m_terminals = m_bridge->realTerminals();
} else {
m_terminals = real_terminal;
}
}
}

View File

@@ -975,8 +975,9 @@ QDomElement TerminalStrip::toXml(QDomDocument &parent_document)
root_elmt.appendChild(m_data.toXml(parent_document));
//Undrawn terminals
auto xml_layout = parent_document.createElement("layout");
//Undrawed terminals
auto xml_layout = parent_document.createElement(QStringLiteral("layout"));
for (auto &phy_t : m_physical_terminals) {
xml_layout.appendChild(phy_t->toXml(parent_document));
}

View File

@@ -120,6 +120,14 @@ void TerminalStripBridge::fromXml(const QDomElement &dom_element)
}
}
/**
* @brief TerminalStripBridge::uuid
* @return The uuid of this terminal
*/
QUuid TerminalStripBridge::uuid() const noexcept {
return m_uuid;
}
/**
* @brief TerminalStripBridge::addTerminals
* @param real_terminals

View File

@@ -46,6 +46,7 @@ class TerminalStripBridge
static QString xmlTagName() {return QStringLiteral("terminal_strip_bridge");}
QDomElement toXml(QDomDocument &parent_document) const;
void fromXml(const QDomElement &dom_element);
QUuid uuid() const noexcept;
private:
bool addTerminals(const QVector<QSharedPointer<RealTerminal>> &real_terminals);

View File

@@ -34,7 +34,7 @@ QDomElement TerminalStripData::toXml(QDomDocument &xml_document) const
root_elmt.setAttribute(QStringLiteral("uuid"), m_uuid.toString());
auto info_elmt = xml_document.createElement("informations");
auto info_elmt = xml_document.createElement(QStringLiteral("informations"));
root_elmt.appendChild(info_elmt);
if (!m_installation.isEmpty()) {
@@ -65,23 +65,23 @@ bool TerminalStripData::fromXml(const QDomElement &xml_element)
return false;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
m_uuid = QUuid::fromString(xml_element.attribute(QLatin1String("uuid")));
m_uuid = QUuid::fromString(xml_element.attribute(QStringLiteral("uuid")));
#else
m_uuid = QUuid(xml_element.attribute(QStringLiteral("uuid")));
#endif
for (auto &xml_info :
QETXML::findInDomElement(xml_element.firstChildElement("informations"),
QStringLiteral("information")))
QETXML::findInDomElement(xml_element.firstChildElement(QStringLiteral("informations")),
QStringLiteral("information")))
{
auto name = xml_info.attribute("name");
auto name = xml_info.attribute(QStringLiteral("name"));
auto value = xml_info.text();
if (name == QStringLiteral("installation")) { m_installation = value;}
else if (name == QStringLiteral("location")) {m_location = value;}
else if (name == QStringLiteral("name")) {m_name = value;}
else if (name == QStringLiteral("comment")) {m_comment = value;}
else if (name == QStringLiteral("description")) {m_description = value;}
if (name == QLatin1String("installation")) { m_installation = value;}
else if (name == QLatin1String("location")) {m_location = value;}
else if (name == QLatin1String("name")) {m_name = value;}
else if (name == QLatin1String("comment")) {m_comment = value;}
else if (name == QLatin1String("description")) {m_description = value;}
}
return true;
@@ -101,7 +101,7 @@ TerminalStripData &TerminalStripData::operator=(const TerminalStripData &other)
QDomElement TerminalStripData::infoToXml(QDomDocument &xml_doc, const QString &name, const QString &value)
{
auto xml_elmt = xml_doc.createElement("information");
auto xml_elmt = xml_doc.createElement(QStringLiteral("information"));
xml_elmt.setAttribute(QStringLiteral("name"), name);
xml_elmt.appendChild(xml_doc.createTextNode(value));

View File

@@ -0,0 +1,88 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include "addterminalstripitemdialog.h"
#include "ui_addterminalstripitemdialog.h"
#include "../../undocommand/addgraphicsobjectcommand.h"
#include "../terminalstrip.h"
#include "../GraphicsItem/terminalstripitem.h"
#include "../../diagram.h"
void AddTerminalStripItemDialog::openDialog(Diagram *diagram, QWidget *parent)
{
AddTerminalStripItemDialog d(diagram->project(), parent);
if (d.exec())
{
const auto strip_{d.selectedTerminalStrip()};
if (strip_)
{
auto item_ = new TerminalStripItem(strip_);
diagram->addItem(item_);
item_->setPos(50, 50);
diagram->project()->undoStack()->push(new AddGraphicsObjectCommand(item_, diagram, QPointF{50, 50}));
}
}
}
AddTerminalStripItemDialog::AddTerminalStripItemDialog(QETProject *project, QWidget *parent) :
QDialog{parent},
m_project{project},
ui{new Ui::AddTerminalStripItemDialog}
{
ui->setupUi(this);
fillComboBox();
}
AddTerminalStripItemDialog::~AddTerminalStripItemDialog()
{
delete ui;
}
/**
* @brief AddTerminalStripItemDialog::selectedTerminalStrip
* @return The selected terminal strip or nullptr if no one is selected
* or error encounted.
*/
TerminalStrip *AddTerminalStripItemDialog::selectedTerminalStrip() const
{
if (m_project)
{
const QUuid uuid_{ui->m_terminal_strip_cb->currentData().toUuid()};
for (auto &&strip_ : m_project->terminalStrip())
{
if (strip_->uuid() == uuid_) {
return strip_;
}
}
}
return nullptr;
}
void AddTerminalStripItemDialog::fillComboBox()
{
if (m_project)
{
for (auto &&strip_ : m_project->terminalStrip())
{
const auto text{strip_->installation() + " " + strip_->location() + " " + strip_->name()};
ui->m_terminal_strip_cb->addItem(text, strip_->uuid());
}
}
}

View File

@@ -0,0 +1,51 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef ADDTERMINALSTRIPITEMDIALOG_H
#define ADDTERMINALSTRIPITEMDIALOG_H
#include <QDialog>
#include <QPointer>
class Diagram;
class QETDiagramEditor;
class QETProject;
class TerminalStrip;
namespace Ui {
class AddTerminalStripItemDialog;
}
class AddTerminalStripItemDialog : public QDialog
{
Q_OBJECT
public:
static void openDialog(Diagram *diagram, QWidget *parent = nullptr);
private:
explicit AddTerminalStripItemDialog(QETProject *project, QWidget *parent = nullptr);
~AddTerminalStripItemDialog();
TerminalStrip *selectedTerminalStrip() const;
void fillComboBox();
private:
QPointer<QETProject> m_project;
Ui::AddTerminalStripItemDialog *ui;
};
#endif // ADDTERMINALSTRIPITEMDIALOG_H

View File

@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AddTerminalStripItemDialog</class>
<widget class="QDialog" name="AddTerminalStripItemDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>326</width>
<height>100</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Ajouter le plan de bornes suivant :</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="m_terminal_strip_cb"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AddTerminalStripItemDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AddTerminalStripItemDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -333,7 +333,7 @@ QWidget *FreeTerminalModelDelegate::createEditor(QWidget *parent, const QStyleOp
{
if (index.column() == TYPE_CELL) {
auto qcb = new QComboBox(parent);
qcb->setObjectName(QLatin1String("terminal_type"));
qcb->setObjectName(QStringLiteral("terminal_type"));
qcb->addItem(ElementData::translatedTerminalType(ElementData::TTGeneric), ElementData::TTGeneric);
qcb->addItem(ElementData::translatedTerminalType(ElementData::TTFuse), ElementData::TTFuse);
qcb->addItem(ElementData::translatedTerminalType(ElementData::TTSectional), ElementData::TTSectional);
@@ -344,7 +344,7 @@ QWidget *FreeTerminalModelDelegate::createEditor(QWidget *parent, const QStyleOp
}
if (index.column() == FUNCTION_CELL) {
auto qcb = new QComboBox(parent);
qcb->setObjectName(QLatin1String("terminal_function"));
qcb->setObjectName(QStringLiteral("terminal_function"));
qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFGeneric), ElementData::TFGeneric);
qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFPhase), ElementData::TFPhase);
qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFNeutral), ElementData::TFNeutral);

View File

@@ -19,6 +19,8 @@
#include "../UndoCommand/addterminalstripcommand.h"
#include "freeterminaleditor.h"
#include "../../qetapp.h"
#include "../../qetdiagrameditor.h"
#include "../../qetproject.h"
#include "../realterminal.h"
#include "../terminalstrip.h"
@@ -27,6 +29,8 @@
#include "terminalstripeditorwindow.h"
#include "terminalstriptreedockwidget.h"
QPointer<TerminalStripEditorWindow> TerminalStripEditorWindow::window_;
static const int EMPTY_PAGE = 0;
static const int FREE_TERMINAL_PAGE = 1;
static const int TERMINAL_STRIP_PAGE = 2;
@@ -35,6 +39,16 @@ static const int TERMINAL_STRIP_PAGE = 2;
* @param project
* @param parent
*/
void TerminalStripEditorWindow::edit(TerminalStrip *strip)
{
if (const auto project_ = strip->project())
{
auto editor_ = TerminalStripEditorWindow::instance(project_, QETApp::diagramEditor(project_));
editor_->setCurrentStrip(strip);
editor_->show();
}
}
TerminalStripEditorWindow::TerminalStripEditorWindow(QETProject *project, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TerminalStripEditorWindow),
@@ -59,7 +73,11 @@ TerminalStripEditorWindow::TerminalStripEditorWindow(QETProject *project, QWidge
*/
TerminalStripEditorWindow::~TerminalStripEditorWindow()
{
delete ui;
delete ui;
}
void TerminalStripEditorWindow::setCurrentStrip(TerminalStrip *strip) {
m_tree_dock->setSelectedStrip(strip);
}
/**

View File

@@ -19,6 +19,8 @@
#define TERMINALSTRIPEDITORWINDOW_H
#include <QMainWindow>
#include <QMutex>
#include <QPointer>
class QETProject;
class TerminalStripTreeDockWidget;
@@ -35,16 +37,47 @@ class TerminalStripEditorWindow : public QMainWindow
{
Q_OBJECT
public:
private:
//We need to use a QPointer instead of a raw pointer because when window_
//have got a parent widget, the parent widget can delete the window_
//instance in her destrucor and then window_ become a dangling pointer.
static QPointer<TerminalStripEditorWindow> window_;
public:
static TerminalStripEditorWindow* instance(QETProject *project, QWidget *parent = nullptr) {
static QMutex mutex_;
if (!window_) {
mutex_.lock();
if (!window_)
window_ = new TerminalStripEditorWindow{project, parent};
mutex_.unlock();
}
return window_;
}
static void dropInstance () {
static QMutex mutex;
if (window_) {
mutex.lock();
window_->deleteLater();
window_.clear();
mutex.unlock();
}
}
static void edit(TerminalStrip *strip);
public:
explicit TerminalStripEditorWindow(QETProject *project, QWidget *parent = nullptr);
~TerminalStripEditorWindow();
void setCurrentStrip(TerminalStrip *strip);
private slots:
void on_m_add_terminal_strip_triggered();
void on_m_remove_terminal_triggered();
void on_m_reload_triggered();
void on_m_button_box_clicked(QAbstractButton *button);
void on_m_stacked_widget_currentChanged(int arg1);
private:

View File

@@ -794,7 +794,7 @@ QWidget *TerminalStripModelDelegate::createEditor(QWidget *parent, const QStyleO
{
if (index.column() == TYPE_CELL) {
auto qcb = new QComboBox(parent);
qcb->setObjectName(QLatin1String("terminal_type"));
qcb->setObjectName(QStringLiteral("terminal_type"));
qcb->addItem(ElementData::translatedTerminalType(ElementData::TTGeneric), ElementData::TTGeneric);
qcb->addItem(ElementData::translatedTerminalType(ElementData::TTFuse), ElementData::TTFuse);
qcb->addItem(ElementData::translatedTerminalType(ElementData::TTSectional), ElementData::TTSectional);
@@ -805,7 +805,7 @@ QWidget *TerminalStripModelDelegate::createEditor(QWidget *parent, const QStyleO
}
if (index.column() == FUNCTION_CELL) {
auto qcb = new QComboBox(parent);
qcb->setObjectName(QLatin1String("terminal_function"));
qcb->setObjectName(QStringLiteral("terminal_function"));
qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFGeneric), ElementData::TFGeneric);
qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFPhase), ElementData::TFPhase);
qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFNeutral), ElementData::TFNeutral);