forked from paceholder/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadless_main.cpp
More file actions
140 lines (119 loc) · 4.33 KB
/
headless_main.cpp
File metadata and controls
140 lines (119 loc) · 4.33 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
#include "AdditionModel.hpp"
#include "DivisionModel.hpp"
#include "LongProcessingRandomNumber.hpp"
#include "MultiplicationModel.hpp"
#include "NumberDisplayDataModel.hpp"
#include "NumberSourceDataModel.hpp"
#include "SubtractionModel.hpp"
#include <QtNodes/DataFlowGraphModel>
#include <QtNodes/NodeDelegateModelRegistry>
using QtNodes::DataFlowGraphModel;
using QtNodes::NodeDelegateModelRegistry;
using QtNodes::NodeId;
static std::shared_ptr<NodeDelegateModelRegistry> registerDataModels()
{
auto ret = std::make_shared<NodeDelegateModelRegistry>();
ret->registerModel<NumberSourceDataModel>("Sources");
ret->registerModel<NumberDisplayDataModel>("Displays");
ret->registerModel<AdditionModel>("Operators");
ret->registerModel<SubtractionModel>("Operators");
ret->registerModel<MultiplicationModel>("Operators");
ret->registerModel<DivisionModel>("Operators");
ret->registerModel<LongProcessingRandomNumber>("Operators");
return ret;
}
/**
* This scene JSON was saved by the normal `calculator` example.
* It has one source number node connected to both inputs of an addition node,
* the result is rendered in a displayer node.
*
* ____________
* / O[ ]
* _____________ / [ addition ] ______________
* [ source node ]O [ node ]O- - - -O[ display node ]
* ------------- \ [ ] --------------
* \ O[____________]
*/
static QString addingNumbersScene(
R"(
{
"nodes": [
{
"id": 0,
"internal-data": {
"model-name": "NumberSource",
"number": "3"
},
"position": {
"x": -338,
"y": -160
}
},
{
"id": 1,
"internal-data": {
"model-name": "Addition"
},
"position": {
"x": -31,
"y": -264
}
},
{
"id": 2,
"internal-data": {
"model-name": "Result"
},
"position": {
"x": 201,
"y": -129
}
}
],
"connections": [
{
"inPortIndex": 0,
"intNodeId": 2,
"outNodeId": 1,
"outPortIndex": 0
},
{
"inPortIndex": 1,
"intNodeId": 1,
"outNodeId": 0,
"outPortIndex": 0
},
{
"inPortIndex": 0,
"intNodeId": 1,
"outNodeId": 0,
"outPortIndex": 0
}
]
}
)");
int main(int argc, char *argv[])
{
std::shared_ptr<NodeDelegateModelRegistry> registry = registerDataModels();
// Here we create a graph model without attaching to any view or scene.
DataFlowGraphModel dataFlowGraphModel(registry);
// Alternatively you can create the graph by yourself with the functions
// `DataFlowGraphModel::addNode` and `DataFlowGraphModel::addConnection` and
// use the obtained `NodeId` to fetch the `NodeDelegateModel`s
QJsonDocument sceneJson = QJsonDocument::fromJson(addingNumbersScene.toUtf8());
dataFlowGraphModel.load(sceneJson.object());
qInfo() << "Data Flow graph was created from a json-serialized graph";
NodeId const nodeSource = 0;
NodeId const nodeResult = 2;
qInfo() << "========================================";
qInfo() << "Entering the number " << 33.3 << "to the input node";
dataFlowGraphModel.delegateModel<NumberSourceDataModel>(nodeSource)->setNumber(33.3);
qInfo() << "Result of the addiion operation: "
<< dataFlowGraphModel.delegateModel<NumberDisplayDataModel>(nodeResult)->number();
qInfo() << "========================================";
qInfo() << "Entering the number " << -5. << "to the input node";
dataFlowGraphModel.delegateModel<NumberSourceDataModel>(nodeSource)->setNumber(-5);
qInfo() << "Result of the addiion operation: "
<< dataFlowGraphModel.delegateModel<NumberDisplayDataModel>(nodeResult)->number();
return 0;
}