QElectroTech  0.70
conductor.h
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 #ifndef CONDUCTOR_H
19 #define CONDUCTOR_H
20 
21 #include "conductorproperties.h"
22 #include <QGraphicsPathItem>
23 #include "assignvariables.h"
24 
25 class ConductorProfile;
27 class Diagram;
28 class Terminal;
29 class ConductorSegment;
30 class ConductorTextItem;
31 class Element;
32 class QETDiagramEditor;
35 
36 typedef QPair<QPointF, Qt::Corner> ConductorBend;
37 typedef QHash<Qt::Corner, ConductorProfile> ConductorProfilesGroup;
42 class Conductor : public QGraphicsObject
43 {
44  Q_OBJECT
45 
46  Q_PROPERTY(QPointF pos READ pos WRITE setPos)
47  Q_PROPERTY(int animPath READ fakePath WRITE updatePathAnimate)
49  Q_PROPERTY(autonum::sequentialNumbers sequenceNum READ sequenceNum WRITE setSequenceNum)
50 
51  signals:
52  void propertiesChange();
53 
54  public:
56  ~Conductor() override;
57 
58  bool isValid() const;
59 
60  private:
61  Conductor(const Conductor &);
62 
63  public:
64  enum { Type = UserType + 1001 };
65  enum Highlight { None, Normal, Alert };
66 
69 
70  public:
75  int type() const override { return Type; }
76  Diagram *diagram() const;
77  ConductorTextItem *textItem() const;
78  void updatePath(const QRectF & = QRectF());
79 
80  //This method do nothing, it's only made to be used with Q_PROPERTY
81  //It's used to anim the path when is change
82  void updatePathAnimate(const int = 1) {updatePath();}
83  int fakePath() {return 1;}
84 
85  void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override;
86  QRectF boundingRect() const override;
87  QPainterPath shape() const override;
88  virtual QPainterPath nearShape() const;
89  qreal length() const;
91  QPointF posForText(Qt::Orientations &flag);
92  void refreshText();
93  void setPath(const QPainterPath &path);
94  QPainterPath path() const;
95 
96  public:
97  static bool valideXml (QDomElement &);
98  bool fromXml (QDomElement &);
99  QDomElement toXml (QDomDocument &, QHash<Terminal *, int> &) const;
100  private:
101  bool pathFromXml(const QDomElement &);
102 
103  public:
104  QVector <QPointF> handlerPoints() const;
105  const QList<ConductorSegment *> segmentsList() const;
106 
107  void setPropertyToPotential(const ConductorProperties &property, bool only_text = false);
108  void setProperties(const ConductorProperties &property);
110 
111  void setProfile(const ConductorProfile &, Qt::Corner);
112  ConductorProfile profile(Qt::Corner) const;
113  void setProfiles(const ConductorProfilesGroup &);
116  virtual Highlight highlight() const;
117  virtual void setHighlighted(Highlight);
118  QSet<Conductor *> relatedPotentialConductors(const bool all_diagram = true, QList <Terminal *> *t_list=nullptr);
120  void editProperty ();
121 
125 
126  private:
127  void setUpConnectionForFormula(QString old_formula, QString new_formula);
129 
130  public:
131  void setFreezeLabel(bool freeze);
132 
133  public slots:
134  void displayedTextChanged();
135 
136  protected:
137  void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
138  void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
139  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
140  void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
141  void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
142  QVariant itemChange(GraphicsItemChange, const QVariant &) override;
143  bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
144 
145  private:
146  void adjusteHandlerPos();
147 
148  void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
149  void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
150  void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
151  void addHandler();
152  void removeHandler();
153 
154 
155  QVector<QetGraphicsHandlerItem *> m_handler_vector;
156  int m_vector_index = -1;
179  bool m_valid;
180  bool m_freeze_label = false;
181 
183  static QPen conductor_pen;
184  static QBrush conductor_brush;
186  QPainterPath m_path;
187 
188  private:
189  void segmentsToPath();
190  void saveProfile(bool = true);
191  void generateConductorPath(const QPointF &, Qet::Orientation, const QPointF &, Qet::Orientation);
192  void updateConductorPath(const QPointF &, Qet::Orientation, const QPointF &, Qet::Orientation);
194  QList<QPointF> segmentsToPoints() const;
195  QList<ConductorBend> bends() const;
196  QList<QPointF> junctions() const;
197  void pointsToSegments(const QList<QPointF>&);
198  Qt::Corner currentPathType() const;
199  void deleteSegments();
200  static int getCoeff(const qreal &, const qreal &);
201  static int getSign(const qreal &);
202  QHash<ConductorSegmentProfile *, qreal> shareOffsetBetweenSegments(const qreal &offset, const QList<ConductorSegmentProfile *> &, const qreal & = 0.01) const;
203  static QPointF extendTerminal(const QPointF &, Qet::Orientation, qreal = 9.0);
204  static Qt::Corner movementType(const QPointF &, const QPointF &);
205  static QPointF movePointIntoPolygon(const QPointF &, const QPainterPath &);
206 };
207 
208 Conductor * longuestConductorInPotential (Conductor *conductor, bool all_diagram = false);
209 QList <Conductor *> relatedConductors (const Conductor *conductor);
210 
211 
212 //return true if @a is between or at @b and @c.
213 template <typename T>
214 bool isBetween (const T a, const T b, const T c) {
215  return (b <= c)? (a >= b && a <= c) : (a <= b && a >= c);
216 }
217 
218 #endif
QDomElement toXml(QDomDocument &, QHash< Terminal *, int > &) const
Definition: conductor.cpp:991
void generateConductorPath(const QPointF &, Qet::Orientation, const QPointF &, Qet::Orientation)
Definition: conductor.cpp:344
QVariant itemChange(GraphicsItemChange, const QVariant &) override
Conductor::itemChange.
Definition: conductor.cpp:653
void refreshText()
Conductor::refreshText Refresh the text of this conductor. recalcule and set the text according to th...
Definition: conductor.cpp:1415
ConductorProfilesGroup conductor_profiles
Definition: conductor.h:176
bool isBetween(const T a, const T b, const T c)
Definition: conductor.h:214
void saveProfile(bool=true)
Definition: conductor.cpp:1349
QETDiagramEditor * diagramEditor() const
Conductor::diagramEditor.
Definition: conductor.cpp:1654
The QetGraphicsHandlerItem class This graphics item represents a point, destined to be used as an han...
bool modified_path
Whether the conductor was manually modified by users.
Definition: conductor.h:171
bool m_mouse_over
Definition: conductor.h:157
QPainterPath m_path
Definition: conductor.h:186
autonum::sequentialNumbers m_autoNum_seq
Definition: conductor.h:128
QList< QPointF > segmentsToPoints() const
Definition: conductor.cpp:914
void setFreezeLabel(bool freeze)
Conductor::setFreezeLabel Freeze this conductor label if true Unfreeze this conductor label if false...
Definition: conductor.cpp:1985
QSet< Conductor * > relatedPotentialConductors(const bool all_diagram=true, QList< Terminal *> *t_list=nullptr)
Conductor::relatedPotentialConductors Return all conductors at the same potential of this conductor...
Definition: conductor.cpp:1603
ConductorProfilesGroup profiles() const
Definition: conductor.cpp:1867
static bool pen_and_brush_initialized
Definition: conductor.h:185
bool fromXml(QDomElement &)
Conductor::fromXml Load the conductor and her information from xml element.
Definition: conductor.cpp:960
QList< ConductorBend > bends() const
Definition: conductor.cpp:1794
ConductorProfile profile(Qt::Corner) const
Definition: conductor.cpp:1406
bool m_moving_segment
Attributs related to mouse interaction.
Definition: conductor.h:165
ConductorSegment * segments
Segments composing the conductor.
Definition: conductor.h:163
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
Conductor::mouseDoubleClickEvent Manage the mouse double click.
Definition: conductor.cpp:597
int type() const override
Definition: conductor.h:75
QPointF posForText(Qt::Orientations &flag)
Conductor::posForText Calculate and return the better pos for text.
Definition: conductor.cpp:1182
void handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
Conductor::handlerMouseMoveEvent.
Definition: conductor.cpp:778
bool pathFromXml(const QDomElement &)
Conductor::pathFromXml Generate the path from xml file.
Definition: conductor.cpp:1038
QIcon Orientations
Definition: qeticons.cpp:126
QRectF boundingRect() const override
Conductor::boundingRect.
Definition: conductor.cpp:862
int fakePath()
Definition: conductor.h:83
QHash< ConductorSegmentProfile *, qreal > shareOffsetBetweenSegments(const qreal &offset, const QList< ConductorSegmentProfile *> &, const qreal &=0.01) const
Definition: conductor.cpp:285
void setPath(const QPainterPath &path)
Definition: conductor.cpp:1444
QList< QPointF > junctions() const
Definition: conductor.cpp:1724
void setProperties(const ConductorProperties &property)
Definition: conductor.cpp:1495
void setPropertyToPotential(const ConductorProperties &property, bool only_text=false)
Conductor::setPropertiesToPotential.
Definition: conductor.cpp:1467
Terminal * terminal2
Definition: conductor.h:68
virtual void setHighlighted(Highlight)
Definition: conductor.cpp:1552
static int getCoeff(const qreal &, const qreal &)
Definition: conductor.cpp:1371
ConductorSegmentType
Known kinds of conductor segments.
Definition: qet.h:86
QPointF before_mov_text_pos_
Definition: conductor.h:169
void addHandler()
Conductor::addHandler Add handlers for this item.
Definition: conductor.cpp:829
int m_vector_index
Definition: conductor.h:156
QHash< Qt::Corner, ConductorProfile > ConductorProfilesGroup
Definition: conductor.h:37
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
Definition: conductor.cpp:473
Diagram * diagram() const
Definition: conductor.cpp:557
Terminal * terminal1
Definition: conductor.h:67
ConductorSegment * middleSegment()
Definition: conductor.cpp:1159
autonum::sequentialNumbers & rSequenceNum()
Definition: conductor.h:123
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
Conductor::mouseReleaseEvent.
Definition: conductor.cpp:619
QPainterPath path() const
Definition: conductor.cpp:1454
ConductorTextItem * textItem() const
Definition: conductor.cpp:564
const QList< ConductorSegment * > segmentsList() const
Definition: conductor.cpp:1134
ConductorSegment * m_moved_segment
Definition: conductor.h:168
void segmentsToPath()
Conductor::segmentsToPath Generate the QPainterPath from the list of points.
Definition: conductor.cpp:175
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
Conductor::hoverLeaveEvent Manage the mouse leave event.
Definition: conductor.cpp:641
bool isValid() const
Conductor::isValid.
Definition: conductor.cpp:148
void setUpConnectionForFormula(QString old_formula, QString new_formula)
Conductor::setUpConnectionForFormula setup connection according to the variable of formula...
Definition: conductor.cpp:1684
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
Conductor::hoverEnterEvent Manage the hover enter event.
Definition: conductor.cpp:630
static bool valideXml(QDomElement &)
Definition: conductor.cpp:573
void pointsToSegments(const QList< QPointF > &)
Definition: conductor.cpp:942
void removeHandler()
Conductor::removeHandler Remove the handlers of this item.
Definition: conductor.cpp:849
QVector< QPointF > handlerPoints() const
Conductor::handlerPoints.
Definition: conductor.cpp:1116
bool m_freeze_label
Definition: conductor.h:180
qreal m_previous_z_value
Definition: conductor.h:167
ConductorTextItem * m_text_item
Text input for non simple, non-singleline conductors.
Definition: conductor.h:161
uint segmentsCount(QET::ConductorSegmentType=QET::Both) const
Definition: conductor.cpp:900
Invalid segment.
Definition: qet.h:89
ConductorProperties m_properties
Functional properties.
Definition: conductor.h:159
void setProfile(const ConductorProfile &, Qt::Corner)
Definition: conductor.cpp:1388
qreal length() const
Conductor::length.
Definition: conductor.cpp:1152
void editProperty()
Conductor::editProperty.
Definition: conductor.cpp:1668
QPointF pos
Definition: conductor.h:46
void handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
Conductor::handlerMousePressEvent.
Definition: conductor.cpp:755
QVector< QetGraphicsHandlerItem * > m_handler_vector
Definition: conductor.h:155
int moved_point
Definition: conductor.h:166
void handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
Conductor::handlerMouseReleaseEvent.
Definition: conductor.cpp:806
Highlight must_highlight_
Define whether and how the conductor should be highlighted.
Definition: conductor.h:178
void calculateTextItemPosition()
Conductor::calculateTextItemPosition Move the text at middle of conductor (if is vertical or horizont...
Definition: conductor.cpp:1253
static Qt::Corner movementType(const QPointF &, const QPointF &)
Definition: conductor.cpp:1851
static QBrush conductor_brush
Definition: conductor.h:184
static QPen conductor_pen
QPen et QBrush objects used to draw conductors.
Definition: conductor.h:183
QPainterPath shape() const override
Conductor::shape.
Definition: conductor.cpp:873
QPair< QPointF, Qt::Corner > ConductorBend
Definition: conductor.h:34
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
Conductor::mousePressEvent Manage the mouse press event.
Definition: conductor.cpp:607
void propertiesChange()
virtual QPainterPath nearShape() const
Conductor::nearShape.
Definition: conductor.cpp:888
bool m_valid
Definition: conductor.h:179
static QPointF movePointIntoPolygon(const QPointF &, const QPainterPath &)
Definition: conductor.cpp:1904
bool has_to_save_profile
Whether the current profile should be saved as soon as possible.
Definition: conductor.h:173
static int getSign(const qreal &)
Definition: conductor.cpp:1379
void updatePath(const QRectF &=QRectF())
Definition: conductor.cpp:159
autonum::sequentialNumbers sequenceNum() const
Definition: conductor.h:122
ConductorProperties properties() const
void adjusteHandlerPos()
Conductor::adjusteHandlerPos Adjust the position of the handler item.
Definition: conductor.cpp:737
void setSequenceNum(const autonum::sequentialNumbers &sn)
Definition: conductor.cpp:1672
static QPointF extendTerminal(const QPointF &, Qet::Orientation, qreal=9.0)
Definition: conductor.cpp:447
int animPath
Definition: conductor.h:47
void displayedTextChanged()
Conductor::displayedTextChanged Update the properties (text) of this conductor and other conductors a...
Definition: conductor.cpp:1562
Conductor * longuestConductorInPotential(Conductor *conductor, bool all_diagram=false)
longuestConductorInPotential
Definition: conductor.cpp:1956
Qt::Corner currentPathType() const
Definition: conductor.cpp:1862
void setProfiles(const ConductorProfilesGroup &)
Conductor::setProfiles.
Definition: conductor.cpp:1875
void deleteSegments()
Supprime les segments.
Definition: conductor.cpp:1890
QList< Conductor * > relatedConductors(const Conductor *conductor)
relatedConductors
Definition: conductor.cpp:1972
Orientation
Orientation (used for electrical elements and their terminals)
Definition: qet.h:204
virtual Highlight highlight() const
Definition: conductor.cpp:1545
void updatePathAnimate(const int=1)
Definition: conductor.h:82
void updateConductorPath(const QPointF &, Qet::Orientation, const QPointF &, Qet::Orientation)
Definition: conductor.cpp:216
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override
Conductor::sceneEventFilter.
Definition: conductor.cpp:694