-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathDefaultConnectionPainter.cpp
More file actions
283 lines (212 loc) · 8.49 KB
/
DefaultConnectionPainter.cpp
File metadata and controls
283 lines (212 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "DefaultConnectionPainter.hpp"
#include <QtGui/QIcon>
#include "AbstractGraphModel.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "ConnectionState.hpp"
#include "Definitions.hpp"
#include "NodeData.hpp"
#include "StyleCollection.hpp"
namespace QtNodes {
QPainterPath DefaultConnectionPainter::cubicPath(ConnectionGraphicsObject const &connection) const
{
QPointF const &in = connection.endPoint(PortType::In);
QPointF const &out = connection.endPoint(PortType::Out);
auto const c1c2 = connection.pointsC1C2();
// cubic spline
QPainterPath cubic(out);
cubic.cubicTo(c1c2.first, c1c2.second, in);
return cubic;
}
void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo, QPainterPath const &cubic) const
{
ConnectionState const &state = cgo.connectionState();
if (state.requiresPort()) {
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();
QPen pen;
pen.setWidth(static_cast<int>(connectionStyle.constructionLineWidth()));
pen.setColor(connectionStyle.constructionColor());
pen.setStyle(Qt::DashLine);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
// cubic spline
painter->drawPath(cubic);
}
}
void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo, QPainterPath const &cubic) const
{
bool const hovered = cgo.connectionState().hovered();
bool const selected = cgo.isSelected();
// drawn as a fat background
if (hovered || selected) {
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();
double const lineWidth = connectionStyle.lineWidth();
QPen pen;
pen.setWidth(static_cast<int>(2 * lineWidth));
pen.setColor(selected ? connectionStyle.selectedHaloColor()
: connectionStyle.hoveredColor());
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
// cubic spline
painter->drawPath(cubic);
}
}
void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo, QPainterPath const &cubic) const
{
ConnectionState const &state = cgo.connectionState();
if (state.requiresPort())
return;
// colors
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();
QColor normalColorOut = connectionStyle.normalColor();
QColor normalColorIn = connectionStyle.normalColor();
QColor selectedColor = connectionStyle.selectedColor();
bool useGradientColor = false;
AbstractGraphModel const &graphModel = cgo.graphModel();
if (connectionStyle.useDataDefinedColors()) {
using QtNodes::PortType;
auto const cId = cgo.connectionId();
auto dataTypeOut = graphModel
.portData(cId.outNodeId,
PortType::Out,
cId.outPortIndex,
PortRole::DataType)
.value<NodeDataType>();
auto dataTypeIn
= graphModel.portData(cId.inNodeId, PortType::In, cId.inPortIndex, PortRole::DataType)
.value<NodeDataType>();
useGradientColor = (dataTypeOut.id != dataTypeIn.id);
normalColorOut = connectionStyle.normalColor(dataTypeOut.id);
normalColorIn = connectionStyle.normalColor(dataTypeIn.id);
selectedColor = normalColorOut.darker(200);
}
// geometry
double const lineWidth = connectionStyle.lineWidth();
// draw normal line
QPen p;
p.setWidth(lineWidth);
bool const selected = cgo.isSelected();
if (useGradientColor) {
painter->setBrush(Qt::NoBrush);
QColor cOut = normalColorOut;
if (selected)
cOut = cOut.darker(200);
p.setColor(cOut);
painter->setPen(p);
unsigned int constexpr segments = 60;
for (unsigned int i = 0ul; i < segments; ++i) {
double ratioPrev = double(i) / segments;
double ratio = double(i + 1) / segments;
if (i == segments / 2) {
QColor cIn = normalColorIn;
if (selected)
cIn = cIn.darker(200);
p.setColor(cIn);
painter->setPen(p);
}
painter->drawLine(cubic.pointAtPercent(ratioPrev), cubic.pointAtPercent(ratio));
}
{
QIcon icon(":convert.png");
QPixmap pixmap = icon.pixmap(QSize(22, 22));
painter->drawPixmap(cubic.pointAtPercent(0.50)
- QPoint(pixmap.width() / 2, pixmap.height() / 2),
pixmap);
}
} else {
p.setColor(normalColorOut);
if (selected) {
p.setColor(selectedColor);
}
painter->setPen(p);
painter->setBrush(Qt::NoBrush);
painter->drawPath(cubic);
}
}
void DefaultConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const
{
auto cubic = cubicPath(cgo);
drawHoveredOrSelected(painter, cgo,cubic);
drawSketchLine(painter, cgo,cubic);
drawNormalLine(painter, cgo,cubic);
#ifdef NODE_DEBUG_DRAWING
debugDrawing(painter, cgo,cubic);
#endif
// draw end points
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();
double const pointDiameter = connectionStyle.pointDiameter();
painter->setPen(connectionStyle.constructionColor());
painter->setBrush(connectionStyle.constructionColor());
double const pointRadius = pointDiameter / 2.0;
if(connectionStyle.outArrow()) {
auto out = createArrowPoly(cubic,pointRadius,pointDiameter * 1.5,false);
painter->drawPolygon(out);
} else {
painter->drawEllipse(cgo.out(), pointRadius, pointRadius);
}
if(connectionStyle.inArrow()) {
auto in = createArrowPoly(cubic,pointRadius,pointDiameter * 1.5,true);
painter->drawPolygon(in);
} else {
painter->drawEllipse(cgo.in(), pointRadius, pointRadius);
}
}
QPainterPath DefaultConnectionPainter::getPainterStroke(ConnectionGraphicsObject const &connection) const
{
auto cubic = cubicPath(connection);
QPointF const &out = connection.endPoint(PortType::Out);
QPainterPath result(out);
unsigned int constexpr segments = 20;
for (auto i = 0ul; i < segments; ++i) {
double ratio = double(i + 1) / segments;
result.lineTo(cubic.pointAtPercent(ratio));
}
QPainterPathStroker stroker;
stroker.setWidth(10.0);
return stroker.createStroke(result);
}
#ifdef NODE_DEBUG_DRAWING
void DefaultConnectionPainter::debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo,QPainterPath const & cubic)
{
Q_UNUSED(painter);
{
QPointF const &in = cgo.endPoint(PortType::In);
QPointF const &out = cgo.endPoint(PortType::Out);
auto const points = cgo.pointsC1C2();
painter->setPen(Qt::red);
painter->setBrush(Qt::red);
painter->drawLine(QLineF(out, points.first));
painter->drawLine(QLineF(points.first, points.second));
painter->drawLine(QLineF(points.second, in));
painter->drawEllipse(points.first, 3, 3);
painter->drawEllipse(points.second, 3, 3);
painter->setBrush(Qt::NoBrush);
painter->drawPath(debugDrawing);
}
{
painter->setPen(Qt::yellow);
painter->drawRect(cgo.boundingRect());
}
}
#endif
QPolygonF DefaultConnectionPainter::createArrowPoly(const QPainterPath& p, double mRadius,double arrowSize,bool drawIn) {
float arrowStartPercentage;
float arrowEndPercentage;
if (drawIn) {
arrowStartPercentage = p.percentAtLength(p.length() - mRadius - arrowSize);
arrowEndPercentage = p.percentAtLength(p.length() - mRadius);
}
else {
arrowStartPercentage = p.percentAtLength(mRadius + arrowSize);
arrowEndPercentage = p.percentAtLength(mRadius);
}
QPointF headStartP = p.pointAtPercent(arrowStartPercentage);
QPointF headEndP = p.pointAtPercent(arrowEndPercentage);
QLineF arrowMiddleLine(headStartP, headEndP);
QPointF normHead(arrowMiddleLine.dy(), -arrowMiddleLine.dx());
QPointF arrowP1 = headStartP + normHead * 0.4;
QPointF arrowP2 = headStartP - normHead * 0.4;
QPolygonF arrowHeadEnd;
arrowHeadEnd << headEndP << arrowP1 << arrowP2 /*<< headEndP*/;
return arrowHeadEnd;
}
} // namespace QtNodes