PeriDyno 1.2.1
Loading...
Searching...
No Matches
WtConnectionGraphicsObject.cpp
Go to the documentation of this file.
2
3#include "WtFlowScene.h"
4
5static Wt::WPainterPath cubicPath(WtConnectionGeometry const& geom)
6{
7 Wt::WPointF const& source = geom.source();
8
9 Wt::WPointF const& sink = geom.sink();
10
11 auto c1c2 = geom.pointsC1C2();
12
13 //cubic spline
14 Wt::WPainterPath cubic(source);
15
16 cubic.cubicTo(c1c2.first, c1c2.second, sink);
17
18 return cubic;
19}
20
21// No PainterPathStroker
22//Wt::WPainterPath WtConnectionPainter::getPainterStroke(WtConnectionGeometry const& geom)
23//{
24// auto cubic = cubicPath(geom);
25//
26// Wt::WPointF const& source = geom.source();
27//
28// Wt::WPainterPath result(source);
29//
30// unsigned segments = 20;
31//
32// for (auto i = 0ul; i < segments; ++i)
33// {
34// double ratio = double(i + 1) / segments;
35// //result.lineTo(cubic.pointAtPercent(ratio));
36// }
37//
38// //QPainterPathStroker stroker; stroker.setWidth(10.0);
39//
40// //return stroker.createStroke(result);
41//}
42
43static void drawSketchLine(Wt::WPainter* painter, WtConnection const& connection)
44{
45 WtConnectionState const& state = connection.connectionState();
46
47 if (state.requiresPort())
48 {
49 auto const& connectionStyle = WtStyleCollection::connectionStyle();
50
51 Wt::WPen p;
52 p.setWidth(connectionStyle.constructionLineWidth());
53 p.setColor(connectionStyle.constructionColor());
54 p.setStyle(Wt::PenStyle::DashLine);
55
56 painter->setPen(p);
57 painter->setBrush(Wt::BrushStyle::None);
58
59 WtConnectionGeometry const& geom = connection.connectionGeometry();
60 auto cubic = cubicPath(geom);
61
62 painter->drawPath(cubic);
63 }
64}
65
66static void drawHoveredOrSelected(Wt::WPainter* painter, WtConnection const& connection)
67{
68 WtConnectionGeometry const& geom = connection.connectionGeometry();
69
70 bool const hovered = geom.hovered();
71
72 auto const& graphicsObject = connection.getConnectionGraphicsObject();
73
74 bool const selected = graphicsObject.isSelected();
75
76 // drawn as a fat background
77 if (hovered || selected)
78 {
79 Wt::WPen p;
80
81 auto const& connectionStyle = WtStyleCollection::connectionStyle();
82
83 double const lineWidth = connectionStyle.lineWidth();
84
85 p.setWidth(2 * lineWidth);
86 p.setColor(Wt::WColor(Wt::StandardColor::Red));
87 p.setColor(selected ? connectionStyle.selectedHaloColor() : connectionStyle.hoveredColor());
88
89 painter->setPen(p);
90 painter->setBrush(Wt::BrushStyle::None);
91
92 auto cubic = cubicPath(geom);
93 painter->drawPath(cubic);
94 }
95}
96
97static void drawNormalLine(Wt::WPainter* painter, WtConnection const& connection)
98{
99 WtConnectionState const& state = connection.connectionState();
100
101 if (state.requiresPort())
102 {
103 return;
104 }
105
106 // color
107 auto const& connectionStyle = WtStyleCollection::connectionStyle();
108
109 Wt::WColor normalColorOut = connectionStyle.normalColor();
110 Wt::WColor normalColorIn = connectionStyle.normalColor();
111 Wt::WColor selectedColor = connectionStyle.selectedColor();
112
113 bool gradientColor = false;
114
115 if (connectionStyle.useDataDefinedColors())
116 {
117 auto dataTypeOut = connection.dataType(PortType::Out);
118 auto dataTypeIn = connection.dataType(PortType::In);
119
120 gradientColor = (dataTypeOut.id != dataTypeIn.id);
121 normalColorOut = connectionStyle.normalColor(dataTypeOut.id);
122 normalColorIn = connectionStyle.normalColor(dataTypeIn.id);
123 // no darker()
124 //selectedColor = normalColorOut.darker(200);
125 }
126
127 //geometry
128 WtConnectionGeometry const& geom = connection.connectionGeometry();
129
130 double const lineWidth = connectionStyle.lineWidth();
131
132 Wt::WPen p;
133 p.setWidth(lineWidth);
134
135 auto const& graphicsObject = connection.getConnectionGraphicsObject();
136 bool const selected = graphicsObject.isSelected();
137
138 // bug
139 auto cubic = cubicPath(geom);
140
141 if (gradientColor)
142 {
143 painter->setBrush(Wt::BrushStyle::None);
144
145 Wt::WColor c = normalColorOut;
146 //if (selected)
147 // no darker()
148 //c = c.darker(200);
149 p.setColor(c);
150 painter->setPen(p);
151
152 unsigned int const segments = 60;
153
154 for (unsigned int i = 0ul; i < segments; ++i)
155 {
156 double ratioPrev = double(i) / segments;
157 double ratio = double(i + 1) / segments;
158
159 if (i == segments / 2)
160 {
161 Wt::WColor c = normalColorIn;
162 /*if (selected)
163 c = c.darker(200);*/
164 p.setColor(Wt::WColor(Wt::StandardColor::Red));
165 painter->setPen(p);
166 }
167 //painter->drawLine(cubic.pointAtPercent(ratioPrev), cubic.pointAtPercent(ratio));
168 //painter->drawLine(Wt::WPointF(10, 10), Wt::WPointF(100, 100));
169 //painter->drawPath(cubic);
170 }
171
172 {
173 //QIcon icon(":convert.png");
174
175 //QPixmap pixmap = icon.pixmap(QSize(22, 22));
176 //painter->drawPixmap(cubic.pointAtPercent(0.50) - QPoint(pixmap.width() / 2,
177 // pixmap.height() / 2),
178 // pixmap);
179 }
180 }
181 else
182 {
183 p.setColor(normalColorOut);
184
185 if (selected)
186 {
187 //p.setColor(selectedColor);
188 p.setColor(Wt::WColor(Wt::StandardColor::Yellow));
189 }
190
191
192
193 painter->setPen(p);
194 painter->setBrush(Wt::BrushStyle::None);
195 painter->drawPath(cubic);
196 }
197}
198
200 Wt::WPainter* painter,
201 WtConnection const& connection)
202{
203 drawHoveredOrSelected(painter, connection);
204
205 drawSketchLine(painter, connection);
206
207 drawNormalLine(painter, connection);
208
209 // draw end points
210 WtConnectionGeometry const& geom = connection.connectionGeometry();
211
212 Wt::WPointF const& source = geom.source();
213 Wt::WPointF const& sink = geom.sink();
214
215 auto const& connectionStyle = WtStyleCollection::connectionStyle();
216
217 double const pointDiameter = connectionStyle.pointDiameter();
218
219 painter->setPen(connectionStyle.constructionColor());
220 painter->setBrush(connectionStyle.constructionColor());
221 double const pointRadius = pointDiameter / 4.0;
222 //painter->drawEllipse(source, pointRadius, pointRadius);
223 //painter->drawEllipse(sink, pointRadius, pointRadius);
224}
225
227 : _scene(scene)
229 , _painter(painter)
230{
231 //_scene.addItem(this);
232 //setFlag(QGraphicsItem::ItemIsMovable, true);
233 //setFlag(QGraphicsItem::ItemIsFocusable, true);
234 //setFlag(QGraphicsItem::ItemIsSelectable, true);
235
236 //setAcceptHoverEvents(true);
237
238 // addGraphicsEffect();
239
240 //setZValue(-1.0);
241
242 //paint(_painter);
243}
244
246{
247 //_scene.removeItem(this);
248}
249
254
256{
257 return _connection.connectionGeometry().boundingRect();
258}
259
260// No getPainterStroke()
261//Wt::WPainterPath WtConnectionGraphicsObject::shape() const
262//{
263// auto const& geom = _connection.connectionGeometry();
264//
265// return WtConnectionPainter::getPainterStroke(geom);
266//}
267
269{
270 // QtItem
271 //prepareGeometryChange();
272}
273
275{
276 for (PortType portType : { PortType::In, PortType::Out })
277 {
278 if (auto node = _connection.getNode(portType))
279 {
280 auto const& nodeGraphics = node->nodeGraphicsObject();
281
282 auto const& nodeGeom = node->nodeGeometry();
283
284 Wt::WPointF origin = node->nodeGraphicsObject().getPos();
285
286 Wt::WPointF scenePos = nodeGeom.portScenePosition(
287 _connection.getPortIndex(portType),
288 portType,
289 nodeGraphics.sceneTransform());
290
291 //Wt::WTransform sceneTransform = this->sceneTransform();
292 Wt::WTransform sceneTransform(1, 0, 0, 1, 0, 0);
293
294 Wt::WPointF connectionPos = sceneTransform.inverted().map(scenePos);
295
296 Wt::WPointF result = Wt::WPointF(connectionPos.x() - origin.x(), connectionPos.y() - origin.y());
297
298 _connection.connectionGeometry().setEndPoint(portType, result);
299
300 //_connection.getConnectionGraphicsObject().setGeometryChanged();
301 //_connection.getConnectionGraphicsObject().update();
302
303 }
304 }
306}
307
309{
310 /*setFlag(QGraphicsItem::ItemIsMovable, !locked);
311 setFlag(QGraphicsItem::ItemIsFocusable, !locked);
312 setFlag(QGraphicsItem::ItemIsSelectable, !locked);*/
313}
314
315void WtConnectionGraphicsObject::paint(Wt::WPainter* painter)
316{
317 //painter->setClipRect(option->exposedRect);
318
320}
321
323{
324 //auto effect = new QGraphicsBlurEffect;
325
326 //effect->setBlurRadius(5);
327 //setGraphicsEffect(effect);
328
329 //auto effect = new QGraphicsDropShadowEffect;
330 //auto effect = new ConnectionBlurEffect(this);
331 //effect->setOffset(4, 4);
332 //effect->setColor(QColor(Qt::gray).darker(800));
333}
static void drawHoveredOrSelected(Wt::WPainter *painter, WtConnection const &connection)
static Wt::WPainterPath cubicPath(WtConnectionGeometry const &geom)
static void drawSketchLine(Wt::WPainter *painter, WtConnection const &connection)
static void drawNormalLine(Wt::WPainter *painter, WtConnection const &connection)
PortType
Wt::WPointF sink() const
Wt::WPointF source() const
double lineWidth() const
bool hovered() const
std::pair< Wt::WPointF, Wt::WPointF > pointsC1C2() const
WtConnectionGraphicsObject(WtFlowScene &scene, WtConnection &connection, Wt::WPainter *painter)
void move()
Updates the position of both ends.
NodeDataType dataType(PortType portType) const
WtConnectionGraphicsObject & getConnectionGraphicsObject() const
WtConnectionState const & connectionState() const
WtConnectionGeometry & connectionGeometry()
static void paint(Wt::WPainter *painter, WtConnection const &connection)
bool requiresPort() const
static WtConnectionStyle const & connectionStyle()