HAL
standard_graphics_net.cpp
Go to the documentation of this file.
2 
4 
5 #include <QPainter>
6 #include <QPen>
7 #include <QPointF>
8 #include <QStyleOptionGraphicsItem>
9 #include <assert.h>
10 #include <limits>
11 #include <QThread>
12 #include <QDebug>
13 #include <QGraphicsScene>
14 
15 namespace hal
16 {
17  qreal StandardGraphicsNet::sAlpha;
18 
19  qreal StandardGraphicsNet::sWireLength;
20  qreal StandardGraphicsNet::sLeftArrowOffset;
21  qreal StandardGraphicsNet::sRightArrowOffset;
22  qreal StandardGraphicsNet::sArrowLeftXShift;
23  qreal StandardGraphicsNet::sArrowRightXShift;
24  qreal StandardGraphicsNet::sArrowSideLength;
25  qreal StandardGraphicsNet::sArrowWidth;
26  qreal StandardGraphicsNet::sArrowHeight;
27 
28  QPainterPath StandardGraphicsNet::sArrow;
29 
30  qreal StandardGraphicsNet::sSplitRadius;
31 
33  {
34  sWireLength = 26;
35 
36  sLeftArrowOffset = 3;
37  sRightArrowOffset = 3;
38 
39  sArrowLeftXShift = 0;
40  sArrowRightXShift = 3;
41  sArrowSideLength = 12;
42 
43  sArrowHeight = 6;
44  sArrowWidth = sArrowLeftXShift + sArrowSideLength + sArrowRightXShift;
45 
46  QPointF point(sArrowLeftXShift, -sArrowHeight / 2);
47 
48 #if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
49  // only available in Qt >= 5.13.0
50  sArrow.clear();
51 #else
52  // low-performance fallback for older Qt
53  sArrow = QPainterPath();
54 #endif
55  sArrow.lineTo(point);
56  point.setX(point.x() + sArrowSideLength);
57  sArrow.lineTo(point);
58  point.setX(point.x() + sArrowRightXShift);
59  point.setY(0);
60  sArrow.lineTo(point);
61  point.setX(point.x() - sArrowRightXShift);
62  point.setY(sArrowHeight / 2);
63  sArrow.lineTo(point);
64  point.setX(point.x() - sArrowSideLength);
65  sArrow.lineTo(point);
66  sArrow.closeSubpath();
67 
68  sSplitRadius = 3;
69  }
70 
72  {
73  if (sLod >= graph_widget_constants::sNetFadeInLod && sLod <= graph_widget_constants::sNetFadeOutLod)
74  sAlpha = (sLod - graph_widget_constants::sNetFadeInLod) / (graph_widget_constants::sNetFadeOutLod - graph_widget_constants::sNetFadeInLod);
75  else
76  sAlpha = 1;
77  }
78 
80  {
81  for (const QPointF& point : knots)
82  {
83  mSplits.append(point);
84  mShape.addEllipse(point, sSplitRadius, sSplitRadius);
85  }
86 
87  qreal smallest_x = std::numeric_limits<qreal>::max();
88  qreal biggest_x = std::numeric_limits<qreal>::min();
89 
90  qreal smallest_y = std::numeric_limits<qreal>::max();
91  qreal biggest_y = std::numeric_limits<qreal>::min();
92 
93  for (const HLine& h : l.mHLines)
94  {
95  if (h.mSmallX < smallest_x)
96  smallest_x = h.mSmallX;
97 
98  if (h.mBigX > biggest_x)
99  biggest_x = h.mBigX;
100 
101  if (h.y < smallest_y)
102  smallest_y = h.y;
103  if (h.y > biggest_y)
104  biggest_y = h.y;
105 
106  QLineF line(h.mSmallX, h.y, h.mBigX, h.y);
107  mLines.append(line);
108  QRectF rect(h.mSmallX - sShapeWidth / 2, h.y - sShapeWidth / 2, h.mBigX - h.mSmallX + sShapeWidth, sShapeWidth);
109  mShape.addRect(rect);
110  }
111 
112  for (const VLine& v : l.mVLines)
113  {
114  if (v.x < smallest_x)
115  smallest_x = v.x;
116  else if (v.x > biggest_x)
117  biggest_x = v.x;
118 
119  if (v.mSmallY < smallest_y)
120  smallest_y = v.mSmallY;
121 
122  if (v.mBigY > biggest_y)
123  biggest_y = v.mBigY;
124 
125  QLineF line(v.x, v.mSmallY, v.x, v.mBigY);
126  mLines.append(line);
127  QRectF rect(v.x - sShapeWidth / 2, v.mSmallY - sShapeWidth / 2, sShapeWidth, v.mBigY - v.mSmallY + sShapeWidth);
128  mShape.addRect(rect);
129  }
130 
131  const qreal padding = sSplitRadius + sShapeWidth;
132 
133  mRect = QRectF(smallest_x - padding, smallest_y - padding, biggest_x - smallest_x + padding, biggest_y - smallest_y + padding);
134  }
135 
137  {
138  Q_UNUSED(widget);
139 
140  QColor color = penColor(option->state);
141  sPen.setColor(color);
143 
144  painter->setPen(sPen);
145  painter->drawLines(mLines);
146 
147  if (sLod > graph_widget_constants::sNetFadeInLod)
148  {
149  color.setAlphaF(sAlpha);
150 
151  sPen.setColor(color);
153  painter->setPen(sPen);
154 
155  sBrush.setColor(color);
157  painter->setBrush(sBrush);
158 
159  const bool original_antialiasing = painter->renderHints().testFlag(QPainter::Antialiasing);
160  painter->setRenderHint(QPainter::Antialiasing, true);
161 
162  for (const QPointF& point : mSplits)
163  painter->drawEllipse(point, sSplitRadius, sSplitRadius);
164 
165  painter->setRenderHint(QPainter::Antialiasing, original_antialiasing);
166  }
167 
169  painter->setBrush(sBrush);
170 
171 #ifdef HAL_DEBUG_GUI_GRAPH_WIDGET
173  const bool original_cosmetic = sPen.isCosmetic();
174  sPen.setCosmetic(true);
175  painter->setPen(sPen);
176  painter->drawPath(mShape);
177  sPen.setCosmetic(original_cosmetic);
178 #endif
179  }
180 
181  void StandardGraphicsNet::Lines::appendHLine(const qreal mSmallX, const qreal mBigX, const qreal y)
182  {
183  assert(mSmallX < mBigX);
184 
185  mHLines.append(HLine{mSmallX, mBigX, y});
186  }
187 
188  void StandardGraphicsNet::Lines::appendVLine(const qreal x, const qreal mSmallY, const qreal mBigY)
189  {
190  assert(mSmallY < mBigY);
191 
192  mVLines.append(VLine{x, mSmallY, mBigY});
193  }
194 } // namespace hal
QColor penColor(QStyle::State state, const QColor &colorHint=QColor()) const
static qreal sLod
Definition: graphics_item.h:93
The basic net class all other nets inherit from.
Definition: graphics_net.h:43
QPainterPath mShape
Definition: graphics_net.h:101
Qt::PenStyle mPenStyle
Definition: graphics_net.h:103
static QPen sPen
Definition: graphics_net.h:97
static QBrush sBrush
Definition: graphics_net.h:98
static qreal sShapeWidth
Definition: graphics_net.h:95
Definition: net.h:58
StandardGraphicsNet(Net *n, const Lines &l, const QList< QPointF > &knots=QList< QPointF >())
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
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)
qreal x() const const
qreal y() const const
void drawEllipse(const QRectF &rectangle)
void drawLines(const QLineF *lines, int lineCount)
void drawPath(const QPainterPath &path)
QPainter::RenderHints renderHints() const const
void setBrush(const QBrush &brush)
void setPen(const QColor &color)
void setRenderHint(QPainter::RenderHint hint, bool on)
void addEllipse(const QRectF &boundingRectangle)
void addRect(const QRectF &rectangle)
void closeSubpath()
void lineTo(const QPointF &endPoint)
bool isCosmetic() const const
void setColor(const QColor &color)
void setCosmetic(bool cosmetic)
void setStyle(Qt::PenStyle style)
void setX(qreal x)
void setY(qreal y)
qreal x() const const
SolidPattern
SolidLine
void append(const T &value)
void appendHLine(const qreal mSmallX, const qreal mBigX, const qreal y)
void appendVLine(const qreal x, const qreal mSmallY, const qreal mBigY)