forked from l3enQ/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPainter.cpp
More file actions
192 lines (135 loc) · 4.9 KB
/
Copy pathConnectionPainter.cpp
File metadata and controls
192 lines (135 loc) · 4.9 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
#include "ConnectionPainter.hpp"
#include <QtGui/QIcon>
#include "AbstractGraphModel.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "ConnectionState.hpp"
#include "Definitions.hpp"
#include "NodeData.hpp"
#include "StyleCollection.hpp"
namespace QtNodes {
static QPainterPath cubicPath(ConnectionGraphicsObject const &connection)
{
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;
}
QPainterPath DefaultConnectionPainter::getPainterStroke(ConnectionGraphicsObject const &connection) const
{
auto cubic = cubicPath(connection);
QPointF const &out = connection.endPoint(PortType::Out);
QPainterPath result(out);
unsigned 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
static void debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo)
{
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(cubicPath(cgo));
}
{
painter->setPen(Qt::yellow);
painter->drawRect(cgo.boundingRect());
}
}
#endif
static void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo)
{
ConnectionState const &state = cgo.connectionState();
if (state.requiresPort()) {
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();
QPen pen;
pen.setWidth(connectionStyle.constructionLineWidth());
pen.setColor(connectionStyle.constructionColor());
pen.setStyle(Qt::DashLine);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
auto cubic = cubicPath(cgo);
// cubic spline
painter->drawPath(cubic);
}
}
static void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo)
{
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(2 * lineWidth);
pen.setColor(selected ? connectionStyle.selectedHaloColor()
: connectionStyle.hoveredColor());
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
// cubic spline
auto const cubic = cubicPath(cgo);
painter->drawPath(cubic);
}
}
static void drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo)
{
ConnectionState const &state = cgo.connectionState();
if (state.requiresPort())
return;
// colors
auto const &connectionStyle = cgo.connectionStyle();
QColor normalColor = connectionStyle.normalColor();
QColor selectedColor = connectionStyle.selectedColor();
// geometry
double const lineWidth = connectionStyle.lineWidth();
// draw normal line
QPen p;
p.setWidth(lineWidth);
bool const selected = cgo.isSelected();
auto cubic = cubicPath(cgo);
// Gradient coloring disabled for performance - use single color
p.setColor(normalColor);
if (selected) {
p.setColor(selectedColor);
}
painter->setPen(p);
painter->setBrush(Qt::NoBrush);
painter->drawPath(cubic);
}
void DefaultConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const
{
drawHoveredOrSelected(painter, cgo);
drawSketchLine(painter, cgo);
drawNormalLine(painter, cgo);
#ifdef NODE_DEBUG_DRAWING
debugDrawing(painter, cgo);
#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;
painter->drawEllipse(cgo.out(), pointRadius, pointRadius);
painter->drawEllipse(cgo.in(), pointRadius, pointRadius);
}
} // namespace QtNodes