loom
294 строки · 6.6 Кб
1#include "SimpleGraphModel.hpp"
2
3SimpleGraphModel::SimpleGraphModel()
4: _nextNodeId{0}
5{}
6
7SimpleGraphModel::~SimpleGraphModel()
8{
9//
10}
11
12std::unordered_set<NodeId> SimpleGraphModel::allNodeIds() const
13{
14return _nodeIds;
15}
16
17std::unordered_set<ConnectionId> SimpleGraphModel::allConnectionIds(NodeId const nodeId) const
18{
19std::unordered_set<ConnectionId> result;
20
21std::copy_if(_connectivity.begin(),
22_connectivity.end(),
23std::inserter(result, std::end(result)),
24[&nodeId](ConnectionId const &cid) {
25return cid.inNodeId == nodeId || cid.outNodeId == nodeId;
26});
27
28return result;
29}
30
31std::unordered_set<ConnectionId> SimpleGraphModel::connections(NodeId nodeId,
32PortType portType,
33PortIndex portIndex) const
34{
35std::unordered_set<ConnectionId> result;
36
37std::copy_if(_connectivity.begin(),
38_connectivity.end(),
39std::inserter(result, std::end(result)),
40[&portType, &portIndex, &nodeId](ConnectionId const &cid) {
41return (getNodeId(portType, cid) == nodeId
42&& getPortIndex(portType, cid) == portIndex);
43});
44
45return result;
46}
47
48bool SimpleGraphModel::connectionExists(ConnectionId const connectionId) const
49{
50return (_connectivity.find(connectionId) != _connectivity.end());
51}
52
53NodeId SimpleGraphModel::addNode(QString const )
54{
55NodeId newId = newNodeId();
56// Create new node.
57_nodeIds.insert(newId);
58
59Q_EMIT nodeCreated(newId);
60
61return newId;
62}
63
64bool SimpleGraphModel::connectionPossible(ConnectionId const connectionId) const
65{
66return _connectivity.find(connectionId) == _connectivity.end();
67}
68
69void SimpleGraphModel::addConnection(ConnectionId const connectionId)
70{
71_connectivity.insert(connectionId);
72
73Q_EMIT connectionCreated(connectionId);
74}
75
76bool SimpleGraphModel::nodeExists(NodeId const nodeId) const
77{
78return (_nodeIds.find(nodeId) != _nodeIds.end());
79}
80
81QVariant SimpleGraphModel::nodeData(NodeId nodeId, NodeRole role) const
82{
83Q_UNUSED(nodeId);
84
85QVariant result;
86
87switch (role) {
88case NodeRole::Type:
89result = QString("Default Node Type");
90break;
91
92case NodeRole::Position:
93result = _nodeGeometryData[nodeId].pos;
94break;
95
96case NodeRole::Size:
97result = _nodeGeometryData[nodeId].size;
98break;
99
100case NodeRole::CaptionVisible:
101result = true;
102break;
103
104case NodeRole::Caption:
105result = QString("Node");
106break;
107
108case NodeRole::Style: {
109auto style = StyleCollection::nodeStyle();
110result = style.toJson().toVariantMap();
111} break;
112
113case NodeRole::InternalData:
114break;
115
116case NodeRole::InPortCount:
117result = 1u;
118break;
119
120case NodeRole::OutPortCount:
121result = 1u;
122break;
123
124case NodeRole::Widget:
125result = QVariant();
126break;
127}
128
129return result;
130}
131
132bool SimpleGraphModel::setNodeData(NodeId nodeId, NodeRole role, QVariant value)
133{
134bool result = false;
135
136switch (role) {
137case NodeRole::Type:
138break;
139case NodeRole::Position: {
140_nodeGeometryData[nodeId].pos = value.value<QPointF>();
141
142Q_EMIT nodePositionUpdated(nodeId);
143
144result = true;
145} break;
146
147case NodeRole::Size: {
148_nodeGeometryData[nodeId].size = value.value<QSize>();
149result = true;
150} break;
151
152case NodeRole::CaptionVisible:
153break;
154
155case NodeRole::Caption:
156break;
157
158case NodeRole::Style:
159break;
160
161case NodeRole::InternalData:
162break;
163
164case NodeRole::InPortCount:
165break;
166
167case NodeRole::OutPortCount:
168break;
169
170case NodeRole::Widget:
171break;
172}
173
174return result;
175}
176
177QVariant SimpleGraphModel::portData(NodeId ,
178PortType portType,
179PortIndex ,
180PortRole role) const
181{
182switch (role) {
183case PortRole::Data:
184return QVariant();
185break;
186
187case PortRole::DataType:
188return QVariant();
189break;
190
191case PortRole::ConnectionPolicyRole:
192return QVariant::fromValue(ConnectionPolicy::One);
193break;
194
195case PortRole::CaptionVisible:
196return true;
197break;
198
199case PortRole::Caption:
200if (portType == PortType::In)
201return QString::fromUtf8("Port In");
202else
203return QString::fromUtf8("Port Out");
204
205break;
206}
207
208return QVariant();
209}
210
211bool SimpleGraphModel::setPortData(
212NodeId nodeId, PortType portType, PortIndex portIndex, QVariant const &value, PortRole role)
213{
214Q_UNUSED(nodeId);
215Q_UNUSED(portType);
216Q_UNUSED(portIndex);
217Q_UNUSED(value);
218Q_UNUSED(role);
219
220return false;
221}
222
223bool SimpleGraphModel::deleteConnection(ConnectionId const connectionId)
224{
225bool disconnected = false;
226
227auto it = _connectivity.find(connectionId);
228
229if (it != _connectivity.end()) {
230disconnected = true;
231
232_connectivity.erase(it);
233}
234
235if (disconnected)
236Q_EMIT connectionDeleted(connectionId);
237
238return disconnected;
239}
240
241bool SimpleGraphModel::deleteNode(NodeId const nodeId)
242{
243// Delete connections to this node first.
244auto connectionIds = allConnectionIds(nodeId);
245
246for (auto &cId : connectionIds) {
247deleteConnection(cId);
248}
249
250_nodeIds.erase(nodeId);
251_nodeGeometryData.erase(nodeId);
252
253Q_EMIT nodeDeleted(nodeId);
254
255return true;
256}
257
258QJsonObject SimpleGraphModel::saveNode(NodeId const nodeId) const
259{
260QJsonObject nodeJson;
261
262nodeJson["id"] = static_cast<qint64>(nodeId);
263
264{
265QPointF const pos = nodeData(nodeId, NodeRole::Position).value<QPointF>();
266
267QJsonObject posJson;
268posJson["x"] = pos.x();
269posJson["y"] = pos.y();
270nodeJson["position"] = posJson;
271}
272
273return nodeJson;
274}
275
276void SimpleGraphModel::loadNode(QJsonObject const &nodeJson)
277{
278NodeId restoredNodeId = static_cast<NodeId>(nodeJson["id"].toInt());
279
280// Next NodeId must be larger that any id existing in the graph
281_nextNodeId = std::max(_nextNodeId, restoredNodeId + 1);
282
283// Create new node.
284_nodeIds.insert(restoredNodeId);
285
286Q_EMIT nodeCreated(restoredNodeId);
287
288{
289QJsonObject posJson = nodeJson["position"].toObject();
290QPointF const pos(posJson["x"].toDouble(), posJson["y"].toDouble());
291
292setNodeData(restoredNodeId, NodeRole::Position, pos);
293}
294}
295