FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
nodeeditor/src/NodeGraphicsObject.cpp at exmix-master · DigitalPulseSoftware/nodeeditor · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DigitalPulseSoftware
/
nodeeditor
Public
forked from
paceholder/nodeeditor
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
nodeeditor
/
src
/
NodeGraphicsObject.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
409 lines (307 loc) · 8.28 KB
Breadcrumbs
nodeeditor
/
src
/
NodeGraphicsObject.cpp
Copy path
File metadata and controls
409 lines (307 loc) · 8.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#
include
"
NodeGraphicsObject.hpp
"
#
include
<
iostream
>
#
include
<
cstdlib
>
#
include
<
QtWidgets/QtWidgets
>
#
include
<
QtWidgets/QGraphicsEffect
>
#
include
"
ConnectionGraphicsObject.hpp
"
#
include
"
ConnectionState.hpp
"
#
include
"
FlowScene.hpp
"
#
include
"
NodePainter.hpp
"
#
include
"
Node.hpp
"
#
include
"
NodeDataModel.hpp
"
#
include
"
NodeConnectionInteraction.hpp
"
#
include
"
StyleCollection.hpp
"
using
QtNodes::NodeGraphicsObject;
using
QtNodes::Node;
using
QtNodes::FlowScene;
NodeGraphicsObject::
NodeGraphicsObject
(FlowScene &scene,
Node& node)
: _scene(scene)
, _node(node)
, _locked(
false
)
, _proxyWidget(
nullptr
)
{
_scene.
addItem
(
this
);
setFlag
(QGraphicsItem::ItemDoesntPropagateOpacityToChildren,
true
);
setFlag
(QGraphicsItem::ItemIsMovable,
true
);
setFlag
(QGraphicsItem::ItemIsFocusable,
true
);
setFlag
(QGraphicsItem::ItemIsSelectable,
true
);
setFlag
(QGraphicsItem::ItemSendsScenePositionChanges,
true
);
setCacheMode
( QGraphicsItem::DeviceCoordinateCache );
auto
const
&nodeStyle = node.
nodeDataModel
()->
nodeStyle
();
{
auto
effect =
new
QGraphicsDropShadowEffect;
effect->
setOffset
(
4
,
4
);
effect->
setBlurRadius
(
20
);
effect->
setColor
(nodeStyle.
ShadowColor
);
setGraphicsEffect
(effect);
}
setOpacity
(nodeStyle.
Opacity
);
setAcceptHoverEvents
(
true
);
setZValue
(
0
);
embedQWidget
();
//
connect to the move signals to emit the move signals in FlowScene
auto
onMoveSlot = [
this
] {
_scene.
nodeMoved
(_node,
pos
());
};
connect
(
this
, &QGraphicsObject::xChanged,
this
, onMoveSlot);
connect
(
this
, &QGraphicsObject::yChanged,
this
, onMoveSlot);
}
NodeGraphicsObject::
~NodeGraphicsObject
()
{
_scene.
removeItem
(
this
);
}
Node&
NodeGraphicsObject::
node
()
{
return
_node;
}
Node
const
&
NodeGraphicsObject::
node
()
const
{
return
_node;
}
void
NodeGraphicsObject::
embedQWidget
()
{
NodeGeometry & geom = _node.
nodeGeometry
();
if
(
auto
w = _node.
nodeDataModel
()->
embeddedWidget
())
{
_proxyWidget =
new
QGraphicsProxyWidget
(
this
);
_proxyWidget->
setWidget
(w);
_proxyWidget->
setPreferredWidth
(
5
);
geom.
recalculateSize
();
_proxyWidget->
setPos
(geom.
widgetPosition
());
update
();
_proxyWidget->
setOpacity
(
1.0
);
_proxyWidget->
setFlag
(QGraphicsItem::ItemIgnoresParentOpacity);
}
}
QRectF
NodeGraphicsObject::
boundingRect
()
const
{
return
_node.
nodeGeometry
().
boundingRect
();
}
void
NodeGraphicsObject::
setGeometryChanged
()
{
prepareGeometryChange
();
}
void
NodeGraphicsObject::
moveConnections
()
const
{
NodeState
const
& nodeState = _node.
nodeState
();
for
(PortType portType: {PortType::In, PortType::Out})
{
auto
const
& connectionEntries =
nodeState.
getEntries
(portType);
for
(
auto
const
& connections : connectionEntries)
{
for
(
auto
& con : connections)
con.
second
->
getConnectionGraphicsObject
().
move
();
}
}
}
void
NodeGraphicsObject::
lock
(
bool
locked)
{
_locked = locked;
setFlag
(QGraphicsItem::ItemIsMovable, !locked);
setFlag
(QGraphicsItem::ItemIsFocusable, !locked);
setFlag
(QGraphicsItem::ItemIsSelectable, !locked);
}
void
NodeGraphicsObject::
paint
(QPainter * painter,
QStyleOptionGraphicsItem
const
* option,
QWidget* )
{
painter->
setClipRect
(option->
exposedRect
);
NodePainter::paint
(painter, _node, _scene);
}
QVariant
NodeGraphicsObject::
itemChange
(GraphicsItemChange change,
const
QVariant &value)
{
if
(change == ItemPositionChange &&
scene
())
{
moveConnections
();
}
return
QGraphicsItem::itemChange
(change, value);
}
void
NodeGraphicsObject::
mousePressEvent
(QGraphicsSceneMouseEvent * event)
{
if
(_locked)
return
;
//
deselect all other items after this one is selected
if
(!
isSelected
() &&
!(event->
modifiers
() & Qt::ControlModifier))
{
_scene.
clearSelection
();
}
for
(PortType portToCheck: {PortType::In, PortType::Out})
{
NodeGeometry
const
& nodeGeometry = _node.
nodeGeometry
();
//
TODO do not pass sceneTransform
int
const
portIndex = nodeGeometry.
checkHitScenePoint
(portToCheck,
event->
scenePos
(),
sceneTransform
());
if
(portIndex !=
INVALID
)
{
NodeState
const
& nodeState = _node.
nodeState
();
std::unordered_map<QUuid, Connection*> connections =
nodeState.
connections
(portToCheck, portIndex);
//
start dragging existing connection
if
(!connections.
empty
() && portToCheck == PortType::In)
{
auto
con = connections.
begin
()->
second
;
NodeConnectionInteraction
interaction
(_node, *con, _scene);
interaction.
disconnect
(portToCheck);
}
else
//
initialize new Connection
{
if
(portToCheck == PortType::Out)
{
auto
const
outPolicy = _node.
nodeDataModel
()->
portOutConnectionPolicy
(portIndex);
if
(!connections.
empty
() &&
outPolicy == NodeDataModel::ConnectionPolicy::One)
{
_scene.
deleteConnection
( *connections.
begin
()->
second
);
}
}
//
todo add to FlowScene
auto
connection = _scene.
createConnection
(portToCheck,
_node,
portIndex);
_node.
nodeState
().
setConnection
(portToCheck,
portIndex,
*connection);
connection->
getConnectionGraphicsObject
().
grabMouse
();
}
}
}
auto
pos = event->
pos
();
auto
& geom = _node.
nodeGeometry
();
auto
& state = _node.
nodeState
();
if
(_node.
nodeDataModel
()->
resizable
() &&
geom.
resizeRect
().
contains
(
QPoint
(pos.
x
(),
pos.
y
())))
{
state.
setResizing
(
true
);
}
}
void
NodeGraphicsObject::
mouseMoveEvent
(QGraphicsSceneMouseEvent * event)
{
auto
& geom = _node.
nodeGeometry
();
auto
& state = _node.
nodeState
();
if
(state.
resizing
())
{
auto
diff = event->
pos
() - event->
lastPos
();
if
(
auto
w = _node.
nodeDataModel
()->
embeddedWidget
())
{
prepareGeometryChange
();
auto
oldSize = w->
size
();
oldSize +=
QSize
(diff.
x
(), diff.
y
());
w->
setFixedSize
(oldSize);
_proxyWidget->
setMinimumSize
(oldSize);
_proxyWidget->
setMaximumSize
(oldSize);
_proxyWidget->
setPos
(geom.
widgetPosition
());
geom.
recalculateSize
();
update
();
moveConnections
();
event->
accept
();
}
}
else
{
QGraphicsObject::mouseMoveEvent
(event);
if
(event->
lastPos
() != event->
pos
())
moveConnections
();
event->
ignore
();
}
QRectF r =
scene
()->
sceneRect
();
r = r.
united
(
mapToScene
(
boundingRect
()).
boundingRect
());
scene
()->
setSceneRect
(r);
}
void
NodeGraphicsObject::
mouseReleaseEvent
(QGraphicsSceneMouseEvent* event)
{
auto
& state = _node.
nodeState
();
state.
setResizing
(
false
);
QGraphicsObject::mouseReleaseEvent
(event);
//
position connections precisely after fast node move
moveConnections
();
}
void
NodeGraphicsObject::
hoverEnterEvent
(QGraphicsSceneHoverEvent * event)
{
//
bring all the colliding nodes to background
QList<QGraphicsItem *> overlapItems =
collidingItems
();
for
(QGraphicsItem *item : overlapItems)
{
if
(item->
zValue
() >
0.0
)
{
item->
setZValue
(
0.0
);
}
}
//
bring this node forward
setZValue
(
1.0
);
_node.
nodeGeometry
().
setHovered
(
true
);
update
();
_scene.
nodeHovered
(
node
(), event->
screenPos
());
event->
accept
();
}
void
NodeGraphicsObject::
hoverLeaveEvent
(QGraphicsSceneHoverEvent * event)
{
_node.
nodeGeometry
().
setHovered
(
false
);
update
();
_scene.
nodeHoverLeft
(
node
());
event->
accept
();
}
void
NodeGraphicsObject::
hoverMoveEvent
(QGraphicsSceneHoverEvent * event)
{
auto
pos = event->
pos
();
auto
& geom = _node.
nodeGeometry
();
if
(_node.
nodeDataModel
()->
resizable
() &&
geom.
resizeRect
().
contains
(
QPoint
(pos.
x
(), pos.
y
())))
{
setCursor
(
QCursor
(Qt::SizeFDiagCursor));
}
else
{
setCursor
(
QCursor
());
}
event->
accept
();
}
void
NodeGraphicsObject::
mouseDoubleClickEvent
(QGraphicsSceneMouseEvent* event)
{
QGraphicsItem::mouseDoubleClickEvent
(event);
_scene.
nodeDoubleClicked
(
node
());
}
void
NodeGraphicsObject::
contextMenuEvent
(QGraphicsSceneContextMenuEvent* event)
{
_scene.
nodeContextMenu
(
node
(),
mapToScene
(event->
pos
()));
}
Back
|
FazBrowse Home
|
New Git URL