HAL
arrow_separated_net.cpp
Go to the documentation of this file.
2 
4 
5 #include <QPainter>
6 #include <QPen>
7 #include <QStyleOptionGraphicsItem>
8 
9 namespace hal
10 {
11  qreal ArrowSeparatedNet::sWireLength;
12  qreal ArrowSeparatedNet::sInputArrowOffset;
13  qreal ArrowSeparatedNet::sOutputArrowOffset;
14  qreal ArrowSeparatedNet::sArrowLeftXShift;
15  qreal ArrowSeparatedNet::sArrowRightXShift;
16  qreal ArrowSeparatedNet::sArrowSideLength;
17  qreal ArrowSeparatedNet::sArrowWidth;
18  qreal ArrowSeparatedNet::sArrowHeight;
19 
20  qreal ArrowSeparatedNet::sInputWidth;
21  qreal ArrowSeparatedNet::sOutputWidth;
22 
23  QPainterPath ArrowSeparatedNet::sArrow;
24 
26  {
27  sWireLength = 26;
28 
29  sInputArrowOffset = 3;
30  sOutputArrowOffset = 3;
31 
32  sArrowLeftXShift = 0;
33  sArrowRightXShift = 3;
34  sArrowSideLength = 12;
35 
36  sArrowHeight = 6;
37  sArrowWidth = sArrowLeftXShift + sArrowSideLength + sArrowRightXShift;
38 
39  sInputWidth = sWireLength + sInputArrowOffset + sArrowWidth + sShapeWidth;
40  sOutputWidth = sWireLength + sOutputArrowOffset + sArrowWidth + sShapeWidth;
41 
42  QPointF point(sArrowLeftXShift, -sArrowHeight / 2);
43 
44  #if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
45  // only available in Qt >= 5.13.0
46  sArrow.clear();
47  #else
48  // low-performance fallback for older Qt
49  sArrow = QPainterPath();
50  #endif
51  sArrow.lineTo(point);
52  point.setX(point.x() + sArrowSideLength);
53  sArrow.lineTo(point);
54  point.setX(point.x() + sArrowRightXShift);
55  point.setY(0);
56  sArrow.lineTo(point);
57  point.setX(point.x() - sArrowRightXShift);
58  point.setY(sArrowHeight / 2);
59  sArrow.lineTo(point);
60  point.setX(point.x() - sArrowSideLength);
61  sArrow.lineTo(point);
62  sArrow.closeSubpath();
63  }
64 
66  {
67  }
68 
70  {
71  Q_UNUSED(widget);
72 
73  if (sLod < graph_widget_constants::sEparatedNetMinLod)
74  return;
75 
76  QColor color = penColor(option->state);
77  color.setAlphaF(sAlpha);
78 
79  sPen.setColor(color);
80  painter->setPen(sPen);
81 
82  if (mFillIcon)
83  {
84  sBrush.setColor(color);
86  painter->setBrush(sBrush);
87  }
88 
89  const Qt::PenStyle original_pen_style = sPen.style();
90 
91  for (const QPointF& position : mInputPositions)
92  {
93  QPointF to(position.x() - sWireLength, position.y());
94  painter->drawLine(position, to);
95  painter->save();
96  painter->setRenderHint(QPainter::Antialiasing, true);
97  sPen.setStyle(Qt::PenStyle::SolidLine);
98  painter->setPen(sPen);
99  painter->translate(QPointF(position.x() - sWireLength - sInputArrowOffset - sArrowWidth, position.y()));
100  painter->drawPath(sArrow);
101  sPen.setStyle(original_pen_style);
102  painter->restore();
103  }
104 
105  for (const QPointF& position : mOutputPositions)
106  {
107  QPointF to(position.x() + sWireLength, position.y());
108  painter->drawLine(position, to);
109  painter->save();
110  painter->setRenderHint(QPainter::Antialiasing, true);
111  sPen.setStyle(Qt::PenStyle::SolidLine);
112  painter->setPen(sPen);
113  painter->translate(QPointF(position.x() + sWireLength + sOutputArrowOffset, position.y()));
114  painter->drawPath(sArrow);
115  sPen.setStyle(original_pen_style);
116  painter->restore();
117  }
118 
120  painter->setBrush(sBrush);
121 
122  #ifdef HAL_DEBUG_GUI_GRAPH_WIDGET
123  bool original_cosmetic = sPen.isCosmetic();
124  sPen.setCosmetic(true);
126  painter->setPen(sPen);
127  painter->drawPath(mShape);
128  sPen.setCosmetic(original_cosmetic);
129  #endif
130  }
131 
132  void ArrowSeparatedNet::addInput(const QPointF& scene_position)
133  {
134  QPointF mapped_position = mapFromScene(scene_position);
135  mInputPositions.append(mapped_position);
136 
137  const qreal half_of_shape_width = sShapeWidth / 2;
138 
139  QPointF point(mapped_position.x() - sWireLength - half_of_shape_width, mapped_position.y() - half_of_shape_width);
140 
141  mShape.moveTo(point);
142  point.setX(point.x() + sWireLength + sShapeWidth);
143  mShape.lineTo(point);
144  point.setY(point.y() + sShapeWidth);
145  mShape.lineTo(point);
146  point.setX(point.x() - sWireLength - sShapeWidth);
147  mShape.lineTo(point);
149 
150  point.setX(mapped_position.x() - sWireLength - sInputArrowOffset - sArrowWidth - half_of_shape_width);
151  point.setY(mapped_position.y() - sArrowHeight / 2 - half_of_shape_width);
152 
153  mShape.moveTo(point);
154  point.setX(point.x() + sArrowWidth + sShapeWidth);
155  mShape.lineTo(point);
156  point.setY(point.y() + sArrowHeight + sShapeWidth);
157  mShape.lineTo(point);
158  point.setX(point.x() - sArrowWidth - sShapeWidth);
159  mShape.lineTo(point);
161  }
162 
163  void ArrowSeparatedNet::addOutput(const QPointF& scene_position)
164  {
165  QPointF mapped_position = mapFromScene(scene_position);
166  mOutputPositions.append(mapped_position);
167 
168  const qreal half_of_shape_width = sShapeWidth / 2;
169 
170  QPointF point(mapped_position.x() - half_of_shape_width, mapped_position.y() - half_of_shape_width);
171 
172  mShape.moveTo(point);
173  point.setX(point.x() + sWireLength + sShapeWidth);
174  mShape.lineTo(point);
175  point.setY(point.y() + sShapeWidth);
176  mShape.lineTo(point);
177  point.setX(point.x() - sWireLength - sShapeWidth);
178  mShape.lineTo(point);
180 
181  point.setX(mapped_position.x() + sWireLength + sOutputArrowOffset - half_of_shape_width);
182  point.setY(mapped_position.y() - sArrowHeight / 2 - half_of_shape_width);
183 
184  mShape.moveTo(point);
185  point.setX(point.x() + sArrowWidth + sShapeWidth);
186  mShape.lineTo(point);
187  point.setY(point.y() + sArrowHeight + sShapeWidth);
188  mShape.lineTo(point);
189  point.setX(point.x() - sArrowWidth - sShapeWidth);
190  mShape.lineTo(point);
192  }
193 
195  {
196  return sInputWidth;
197  }
198 
200  {
201  return sOutputWidth;
202  }
203 }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
void addOutput(const QPointF &scene_position) override
qreal outputWidth() const override
qreal inputWidth() const override
void addInput(const QPointF &scene_position) override
QColor penColor(QStyle::State state, const QColor &colorHint=QColor()) const
static qreal sLod
Definition: graphics_item.h:93
QPainterPath mShape
Definition: graphics_net.h:101
static QPen sPen
Definition: graphics_net.h:97
static QBrush sBrush
Definition: graphics_net.h:98
static qreal sShapeWidth
Definition: graphics_net.h:95
Qt::BrushStyle mBrushStyle
Definition: graphics_net.h:107
Definition: net.h:58
Abstract base class for separated nets (e.g. ArrowSeparatedNet)
QVector< QPointF > mInputPositions
QVector< QPointF > mOutputPositions
S to(const T &str)
n
Definition: test.py:6
option(PL_GUI "PL_GUI" ON) if(PL_GUI OR BUILD_ALL_PLUGINS) cmake_minimum_required(VERSION 3.1.0) if(APPLE AND CMAKE_HOST_APPLE AND NOT Qt5_DIR) set(Qt5_DIR "/usr/local/opt/qt@5/lib/cmake") endif(APPLE AND CMAKE_HOST_APPLE AND NOT Qt5_DIR) find_package(Qt5 COMPONENTS Core REQUIRED) find_package(Qt5 COMPONENTS Widgets REQUIRED) if(Qt5Widgets_FOUND) message(VERBOSE "Qt5Widgets_INCLUDE_DIRS
Definition: CMakeLists.txt:1
void setColor(const QColor &color)
void setStyle(Qt::BrushStyle style)
void setAlphaF(qreal alpha)
QPointF mapFromScene(const QPointF &point) const const
void drawLine(const QLineF &line)
void drawPath(const QPainterPath &path)
void restore()
void save()
void setBrush(const QBrush &brush)
void setPen(const QColor &color)
void setRenderHint(QPainter::RenderHint hint, bool on)
void translate(const QPointF &offset)
void closeSubpath()
void lineTo(const QPointF &endPoint)
void moveTo(const QPointF &point)
bool isCosmetic() const const
void setColor(const QColor &color)
void setCosmetic(bool cosmetic)
void setStyle(Qt::PenStyle style)
Qt::PenStyle style() const const
void setX(qreal x)
void setY(qreal y)
qreal x() const const
qreal y() const const
PenStyle
void append(const T &value)