FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
nodeeditor/src/DataFlowGraphModel.cpp at bt3 · BehaviorTree/nodeeditor · GitHub
BehaviorTree
/
nodeeditor
Public
forked from
l3enQ/nodeeditor
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
Code
Pull requests
1
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
/
DataFlowGraphModel.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
532 lines (399 loc) · 14.6 KB
Breadcrumbs
nodeeditor
/
src
/
DataFlowGraphModel.cpp
Copy path
File metadata and controls
532 lines (399 loc) · 14.6 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#
include
"
DataFlowGraphModel.hpp
"
#
include
"
ConnectionIdHash.hpp
"
#
include
<
QJsonArray
>
#
include
<
stdexcept
>
namespace
QtNodes
{
DataFlowGraphModel::DataFlowGraphModel
(std::shared_ptr<NodeDelegateModelRegistry> registry)
: _registry(std::move(registry))
, _nextNodeId{
0
}
{}
std::unordered_set<NodeId>
DataFlowGraphModel::allNodeIds
()
const
{
std::unordered_set<NodeId> nodeIds;
for_each
(_models.
begin
(), _models.
end
(), [&nodeIds](
auto
const
&p) { nodeIds.
insert
(p.
first
); });
return
nodeIds;
}
std::unordered_set<ConnectionId>
DataFlowGraphModel::allConnectionIds
(NodeId
const
nodeId)
const
{
std::unordered_set<ConnectionId> result;
std::copy_if
(_connectivity.
begin
(),
_connectivity.
end
(),
std::inserter
(result,
std::end
(result)),
[&nodeId](ConnectionId
const
&cid) {
return
cid.
inNodeId
== nodeId || cid.
outNodeId
== nodeId;
});
return
result;
}
std::unordered_set<ConnectionId>
DataFlowGraphModel::connections
(NodeId nodeId,
PortType portType,
PortIndex portIndex)
const
{
std::unordered_set<ConnectionId> result;
std::copy_if
(_connectivity.
begin
(),
_connectivity.
end
(),
std::inserter
(result,
std::end
(result)),
[&portType, &portIndex, &nodeId](ConnectionId
const
&cid) {
return
(
getNodeId
(portType, cid) == nodeId
&&
getPortIndex
(portType, cid) == portIndex);
});
return
result;
}
bool
DataFlowGraphModel::connectionExists
(ConnectionId
const
connectionId)
const
{
return
(_connectivity.
find
(connectionId) != _connectivity.
end
());
}
NodeId
DataFlowGraphModel::addNode
(QString
const
nodeType)
{
std::unique_ptr<NodeDelegateModel> model = _registry->
create
(nodeType);
if
(model) {
NodeId newId =
newNodeId
();
connect
(model.
get
(),
&NodeDelegateModel::dataUpdated,
[newId,
this
](PortIndex
const
portIndex) {
onOutPortDataUpdated
(newId, portIndex);
});
connect
(model.
get
(),
&NodeDelegateModel::portsAboutToBeDeleted,
this
,
[newId,
this
](PortType
const
portType, PortIndex
const
first, PortIndex
const
last) {
portsAboutToBeDeleted
(newId, portType, first, last);
});
connect
(model.
get
(),
&NodeDelegateModel::portsDeleted,
this
,
&DataFlowGraphModel::portsDeleted);
connect
(model.
get
(),
&NodeDelegateModel::portsAboutToBeInserted,
this
,
[newId,
this
](PortType
const
portType, PortIndex
const
first, PortIndex
const
last) {
portsAboutToBeInserted
(newId, portType, first, last);
});
connect
(model.
get
(),
&NodeDelegateModel::portsInserted,
this
,
&DataFlowGraphModel::portsInserted);
_models[newId] =
std::move
(model);
Q_EMIT
nodeCreated
(newId);
return
newId;
}
return
InvalidNodeId;
}
bool
DataFlowGraphModel::connectionPossible
(ConnectionId
const
connectionId)
const
{
auto
getDataType = [&](PortType
const
portType) {
return
portData
(
getNodeId
(portType, connectionId),
portType,
getPortIndex
(portType, connectionId),
PortRole::DataType)
.
value
<NodeDataType>();
};
auto
portVacant = [&](PortType
const
portType) {
NodeId
const
nodeId =
getNodeId
(portType, connectionId);
PortIndex
const
portIndex =
getPortIndex
(portType, connectionId);
auto
const
connected =
connections
(nodeId, portType, portIndex);
auto
policy =
portData
(nodeId, portType, portIndex, PortRole::ConnectionPolicyRole)
.
value
<ConnectionPolicy>();
return
connected.
empty
() || (policy == ConnectionPolicy::Many);
};
return
getDataType
(PortType::Out).
id
==
getDataType
(PortType::In).
id
&&
portVacant
(PortType::Out) &&
portVacant
(PortType::In);
}
void
DataFlowGraphModel::addConnection
(ConnectionId
const
connectionId)
{
_connectivity.
insert
(connectionId);
sendConnectionCreation
(connectionId);
QVariant
const
portDataToPropagate =
portData
(connectionId.
outNodeId
,
PortType::Out,
connectionId.
outPortIndex
,
PortRole::Data);
setPortData
(connectionId.
inNodeId
,
PortType::In,
connectionId.
inPortIndex
,
portDataToPropagate,
PortRole::Data);
}
void
DataFlowGraphModel::sendConnectionCreation
(ConnectionId
const
connectionId)
{
Q_EMIT
connectionCreated
(connectionId);
auto
iti = _models.
find
(connectionId.
inNodeId
);
auto
ito = _models.
find
(connectionId.
outNodeId
);
if
(iti != _models.
end
() && ito != _models.
end
()) {
auto
&modeli = iti->
second
;
auto
&modelo = ito->
second
;
modeli->
inputConnectionCreated
(connectionId);
modelo->
outputConnectionCreated
(connectionId);
}
}
void
DataFlowGraphModel::sendConnectionDeletion
(ConnectionId
const
connectionId)
{
Q_EMIT
connectionDeleted
(connectionId);
auto
iti = _models.
find
(connectionId.
inNodeId
);
auto
ito = _models.
find
(connectionId.
outNodeId
);
if
(iti != _models.
end
() && ito != _models.
end
()) {
auto
&modeli = iti->
second
;
auto
&modelo = ito->
second
;
modeli->
inputConnectionDeleted
(connectionId);
modelo->
outputConnectionDeleted
(connectionId);
}
}
bool
DataFlowGraphModel::nodeExists
(NodeId
const
nodeId)
const
{
return
(_models.
find
(nodeId) != _models.
end
());
}
QVariant
DataFlowGraphModel::nodeData
(NodeId nodeId, NodeRole role)
const
{
QVariant result;
auto
it = _models.
find
(nodeId);
if
(it == _models.
end
())
return
result;
auto
&model = it->
second
;
switch
(role) {
case
NodeRole::Type:
result = model->
name
();
break
;
case
NodeRole::Position:
result = _nodeGeometryData[nodeId].
pos
;
break
;
case
NodeRole::Size:
result = _nodeGeometryData[nodeId].
size
;
break
;
case
NodeRole::CaptionVisible:
result = model->
captionVisible
();
break
;
case
NodeRole::Caption:
result = model->
caption
();
break
;
case
NodeRole::Style: {
auto
style =
StyleCollection::nodeStyle
();
result = style.
toJson
().
toVariantMap
();
}
break
;
case
NodeRole::InternalData: {
QJsonObject nodeJson;
nodeJson[
"
internal-data
"
] = _models.
at
(nodeId)->
save
();
result = nodeJson.
toVariantMap
();
break
;
}
case
NodeRole::InPortCount:
result = model->
nPorts
(PortType::In);
break
;
case
NodeRole::OutPortCount:
result = model->
nPorts
(PortType::Out);
break
;
case
NodeRole::Widget: {
auto
w = model->
embeddedWidget
();
result =
QVariant::fromValue
(w);
}
break
;
}
return
result;
}
NodeFlags
DataFlowGraphModel::nodeFlags
(NodeId nodeId)
const
{
auto
it = _models.
find
(nodeId);
if
(it != _models.
end
() && it->
second
->
resizable
())
return
NodeFlag::Resizable;
return
NodeFlag::NoFlags;
}
bool
DataFlowGraphModel::setNodeData
(NodeId nodeId, NodeRole role, QVariant value)
{
Q_UNUSED
(nodeId);
Q_UNUSED
(role);
Q_UNUSED
(value);
bool
result =
false
;
switch
(role) {
case
NodeRole::Type:
break
;
case
NodeRole::Position: {
_nodeGeometryData[nodeId].
pos
= value.
value
<QPointF>();
Q_EMIT
nodePositionUpdated
(nodeId);
result =
true
;
}
break
;
case
NodeRole::Size: {
_nodeGeometryData[nodeId].
size
= value.
value
<QSize>();
result =
true
;
}
break
;
case
NodeRole::CaptionVisible:
break
;
case
NodeRole::Caption:
break
;
case
NodeRole::Style:
break
;
case
NodeRole::InternalData:
break
;
case
NodeRole::InPortCount:
break
;
case
NodeRole::OutPortCount:
break
;
case
NodeRole::Widget:
break
;
}
return
result;
}
QVariant
DataFlowGraphModel::portData
(NodeId nodeId,
PortType portType,
PortIndex portIndex,
PortRole role)
const
{
QVariant result;
auto
it = _models.
find
(nodeId);
if
(it == _models.
end
())
return
result;
auto
&model = it->
second
;
switch
(role) {
case
PortRole::Data:
if
(portType == PortType::Out)
result =
QVariant::fromValue
(model->
outData
(portIndex));
break
;
case
PortRole::DataType:
result =
QVariant::fromValue
(model->
dataType
(portType, portIndex));
break
;
case
PortRole::ConnectionPolicyRole:
result =
QVariant::fromValue
(model->
portConnectionPolicy
(portType, portIndex));
break
;
case
PortRole::CaptionVisible:
result = model->
portCaptionVisible
(portType, portIndex);
break
;
case
PortRole::Caption:
result = model->
portCaption
(portType, portIndex);
break
;
}
return
result;
}
bool
DataFlowGraphModel::setPortData
(
NodeId nodeId, PortType portType, PortIndex portIndex, QVariant
const
&value, PortRole role)
{
Q_UNUSED
(nodeId);
QVariant result;
auto
it = _models.
find
(nodeId);
if
(it == _models.
end
())
return
false
;
auto
&model = it->
second
;
switch
(role) {
case
PortRole::Data:
if
(portType == PortType::In) {
model->
setInData
(value.
value
<std::shared_ptr<NodeData>>(), portIndex);
//
Triggers repainting on the scene.
Q_EMIT
inPortDataWasSet
(nodeId, portType, portIndex);
}
break
;
default
:
break
;
}
return
false
;
}
bool
DataFlowGraphModel::deleteConnection
(ConnectionId
const
connectionId)
{
bool
disconnected =
false
;
auto
it = _connectivity.
find
(connectionId);
if
(it != _connectivity.
end
()) {
disconnected =
true
;
_connectivity.
erase
(it);
}
if
(disconnected) {
sendConnectionDeletion
(connectionId);
propagateEmptyDataTo
(
getNodeId
(PortType::In, connectionId),
getPortIndex
(PortType::In, connectionId));
}
return
disconnected;
}
bool
DataFlowGraphModel::deleteNode
(NodeId
const
nodeId)
{
//
Delete connections to this node first.
auto
connectionIds =
allConnectionIds
(nodeId);
for
(
auto
&cId : connectionIds) {
deleteConnection
(cId);
}
_nodeGeometryData.
erase
(nodeId);
_models.
erase
(nodeId);
Q_EMIT
nodeDeleted
(nodeId);
return
true
;
}
QJsonObject
DataFlowGraphModel::saveNode
(NodeId
const
nodeId)
const
{
QJsonObject nodeJson;
nodeJson[
"
id
"
] =
static_cast
<qint64>(nodeId);
nodeJson[
"
internal-data
"
] = _models.
at
(nodeId)->
save
();
{
QPointF
const
pos =
nodeData
(nodeId, NodeRole::Position).
value
<QPointF>();
QJsonObject posJson;
posJson[
"
x
"
] = pos.
x
();
posJson[
"
y
"
] = pos.
y
();
nodeJson[
"
position
"
] = posJson;
}
return
nodeJson;
}
QJsonObject
DataFlowGraphModel::save
()
const
{
QJsonObject sceneJson;
QJsonArray nodesJsonArray;
for
(
auto
const
nodeId :
allNodeIds
()) {
nodesJsonArray.
append
(
saveNode
(nodeId));
}
sceneJson[
"
nodes
"
] = nodesJsonArray;
QJsonArray connJsonArray;
for
(
auto
const
&cid : _connectivity) {
connJsonArray.
append
(
toJson
(cid));
}
sceneJson[
"
connections
"
] = connJsonArray;
return
sceneJson;
}
void
DataFlowGraphModel::loadNode
(QJsonObject
const
&nodeJson)
{
//
Possibility of the id clash when reading it from json and not generating a
//
new value.
//
1. When restoring a scene from a file.
//
Conflict is not possible because the scene must be cleared by the time of
//
loading.
//
2. When undoing the deletion command. Conflict is not possible
//
because all the new ids were created past the removed nodes.
NodeId restoredNodeId = nodeJson[
"
id
"
].
toInt
();
_nextNodeId =
std::max
(_nextNodeId, restoredNodeId +
1
);
QJsonObject
const
internalDataJson = nodeJson[
"
internal-data
"
].
toObject
();
QString delegateModelName = internalDataJson[
"
model-name
"
].
toString
();
std::unique_ptr<NodeDelegateModel> model = _registry->
create
(delegateModelName);
if
(model) {
connect
(model.
get
(),
&NodeDelegateModel::dataUpdated,
[restoredNodeId,
this
](PortIndex
const
portIndex) {
onOutPortDataUpdated
(restoredNodeId, portIndex);
});
_models[restoredNodeId] =
std::move
(model);
Q_EMIT
nodeCreated
(restoredNodeId);
QJsonObject posJson = nodeJson[
"
position
"
].
toObject
();
QPointF
const
pos
(posJson[
"
x
"
].
toDouble
(), posJson[
"
y
"
].
toDouble
());
setNodeData
(restoredNodeId, NodeRole::Position, pos);
_models[restoredNodeId]->
load
(internalDataJson);
}
else
{
throw
std::logic_error
(
std::string
(
"
No registered model with name
"
)
+ delegateModelName.
toLocal8Bit
().
data
());
}
}
void
DataFlowGraphModel::load
(QJsonObject
const
&jsonDocument)
{
QJsonArray nodesJsonArray = jsonDocument[
"
nodes
"
].
toArray
();
for
(QJsonValueRef nodeJson : nodesJsonArray) {
loadNode
(nodeJson.
toObject
());
}
QJsonArray connectionJsonArray = jsonDocument[
"
connections
"
].
toArray
();
for
(QJsonValueRef connection : connectionJsonArray) {
QJsonObject connJson = connection.
toObject
();
ConnectionId connId =
fromJson
(connJson);
//
Restore the connection
addConnection
(connId);
}
}
void
DataFlowGraphModel::onOutPortDataUpdated
(NodeId
const
nodeId, PortIndex
const
portIndex)
{
std::unordered_set<ConnectionId>
const
&connected =
connections
(nodeId,
PortType::Out,
portIndex);
QVariant
const
portDataToPropagate =
portData
(nodeId, PortType::Out, portIndex, PortRole::Data);
for
(
auto
const
&cn : connected) {
setPortData
(cn.
inNodeId
, PortType::In, cn.
inPortIndex
, portDataToPropagate, PortRole::Data);
}
}
void
DataFlowGraphModel::propagateEmptyDataTo
(NodeId
const
nodeId, PortIndex
const
portIndex)
{
QVariant emptyData{};
setPortData
(nodeId, PortType::In, portIndex, emptyData, PortRole::Data);
}
}
//
namespace QtNodes
Back
|
FazBrowse Home
|
New Git URL