QElectroTech  0.70
partterminal.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "partterminal.h"
19 #include "terminal.h"
20 
27 PartTerminal::PartTerminal(QETElementEditor *editor, QGraphicsItem *parent) :
28  CustomElementGraphicPart(editor, parent),
29  m_orientation(Qet::North)
30 {
32  setZValue(100000);
33 }
34 
37 }
38 
43 void PartTerminal::fromXml(const QDomElement &xml_elmt) {
44  // lit la position de la borne
45  qreal term_x = 0.0, term_y = 0.0;
46  QET::attributeIsAReal(xml_elmt, "x", &term_x);
47  QET::attributeIsAReal(xml_elmt, "y", &term_y);
48  setPos(QPointF(term_x, term_y));
49 
50  // lit l'orientation de la borne
51  m_orientation = Qet::orientationFromString(xml_elmt.attribute("orientation"));
52 
54 }
55 
61 const QDomElement PartTerminal::toXml(QDomDocument &xml_document) const {
62  QDomElement xml_element = xml_document.createElement("terminal");
63 
64  // ecrit la position de la borne
65  xml_element.setAttribute("x", QString("%1").arg(scenePos().x()));
66  xml_element.setAttribute("y", QString("%1").arg(scenePos().y()));
67 
68 
69  // ecrit l'orientation de la borne
70  xml_element.setAttribute("orientation", Qet::orientationToString(m_orientation));
71  // Write name and number to XML
72 
73  return(xml_element);
74 }
75 
82 void PartTerminal::paint(QPainter *p, const QStyleOptionGraphicsItem *options, QWidget *widget) {
83  Q_UNUSED(widget);
84  p -> save();
85 
86  // annulation des renderhints
87  p -> setRenderHint(QPainter::Antialiasing, false);
88  p -> setRenderHint(QPainter::TextAntialiasing, false);
89  p -> setRenderHint(QPainter::SmoothPixmapTransform, false);
90 
91  QPen t;
92  t.setWidthF(1.0);
93  t.setCosmetic(options && options -> levelOfDetail < 1.0);
94 
95  // dessin de la borne en rouge
96  t.setColor(isSelected() ? Terminal::neutralColor : Qt::red);
97  p -> setPen(t);
98  p -> drawLine(QPointF(0.0, 0.0), second_point);
99 
100  // dessin du point d'amarrage au conducteur en bleu
101  t.setColor(isSelected() ? Qt::red : Terminal::neutralColor);
102  p -> setPen(t);
103  p -> setBrush(Terminal::neutralColor);
104  p -> drawPoint(QPointF(0.0, 0.0));
105  p -> restore();
106 
107  if (m_hovered)
108  drawShadowShape(p);
109 }
110 
115 QPainterPath PartTerminal::shape() const
116 {
117  QPainterPath shape;
118  shape.lineTo(second_point);
119 
120  QPainterPathStroker pps;
121  pps.setWidth(1);
122 
123  return (pps.createStroke(shape));
124 }
125 
131 {
132  QRectF br(QPointF(0, 0), second_point);
133  br = br.normalized();
134 
135  qreal adjust = (SHADOWS_HEIGHT + 1) / 2;
136  br.adjust(-adjust, -adjust, adjust, adjust);
137  return(br);
138 }
139 
145 {
146  if (m_orientation == ori) return;
147  prepareGeometryChange();
148  m_orientation = ori;
150  emit orientationChanged();
151 }
152 
158  qreal ts = 4.0; // terminal size
159  switch(m_orientation) {
160  case Qet::North: second_point = QPointF(0.0, ts); break;
161  case Qet::East : second_point = QPointF(-ts, 0.0); break;
162  case Qet::South: second_point = QPointF(0.0, -ts); break;
163  case Qet::West : second_point = QPointF(ts, 0.0); break;
164  }
165 }
166 
174  return(false);
175 }
176 
184  return(sceneBoundingRect());
185 }
186 
191 void PartTerminal::startUserTransformation(const QRectF &initial_selection_rect) {
192  Q_UNUSED(initial_selection_rect)
193  saved_position_ = scenePos();
194 }
195 
199 void PartTerminal::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect) {
200  QPointF mapped_point = mapPoints(initial_selection_rect, new_selection_rect, QList<QPointF>() << saved_position_).first();
201  setPos(mapped_point);
202 }
void fromXml(const QDomElement &) override
QIcon br
Definition: qeticons.cpp:185
PartTerminal(QETElementEditor *editor, QGraphicsItem *parent=nullptr)
Definition: qet.h:207
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
Qet::Orientation m_orientation
Definition: partterminal.h:48
QList< QPointF > mapPoints(const QRectF &, const QRectF &, const QList< QPointF > &)
static Qet::Orientation orientationFromString(const QString &)
Definition: qet.cpp:35
The CustomElementGraphicPart class This class is the base for all home-made primitive like line...
QRectF boundingRect() const override
PartTerminal::boundingRect.
QIcon North
Definition: qeticons.cpp:122
void startUserTransformation(const QRectF &) override
void setOrientation(Qet::Orientation ori)
~PartTerminal() override
Destructeur.
void handleUserTransformation(const QRectF &, const QRectF &) override
void updateSecondPoint()
bool isUseless() const override
QPointF second_point
Definition: partterminal.h:49
static QString orientationToString(Qet::Orientation)
Definition: qet.cpp:47
Definition: qet.h:205
static QColor neutralColor
default color
Definition: terminal.h:100
bool attributeIsAReal(const QDomElement &, const QString &, qreal *=nullptr)
Definition: qet.cpp:219
QRectF sceneGeometricRect() const override
QPainterPath shape() const override
PartTerminal::shape.
QPointF saved_position_
Definition: partterminal.h:81
const QDomElement toXml(QDomDocument &) const override
#define SHADOWS_HEIGHT
Definition: qet.h:185
void orientationChanged()
Orientation
Orientation (used for electrical elements and their terminals)
Definition: qet.h:204
void drawShadowShape(QPainter *painter)
CustomElementGraphicPart::drawShadowShape Draw a transparent blue shadow arround the shape of this it...