forked from paceholder/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeConnectionInteraction.cpp
More file actions
257 lines (187 loc) · 7.18 KB
/
NodeConnectionInteraction.cpp
File metadata and controls
257 lines (187 loc) · 7.18 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
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
#include "NodeConnectionInteraction.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "NodeGraphicsObject.hpp"
#include "NodeDataModel.hpp"
#include "DataModelRegistry.hpp"
#include "FlowScene.hpp"
using QtNodes::NodeConnectionInteraction;
using QtNodes::PortType;
using QtNodes::PortIndex;
using QtNodes::FlowScene;
using QtNodes::Node;
using QtNodes::Connection;
using QtNodes::NodeDataModel;
NodeConnectionInteraction::
NodeConnectionInteraction(Node& node, Connection& connection, FlowScene& scene)
: _node(&node)
, _connection(&connection)
, _scene(&scene)
{}
bool
NodeConnectionInteraction::
canConnect(PortIndex &portIndex, bool& typeConversionNeeded, std::unique_ptr<NodeDataModel> & converterModel) const
{
typeConversionNeeded = false;
// 1) Connection requires a port
PortType requiredPort = connectionRequiredPort();
if (requiredPort == PortType::None)
{
return false;
}
// 2) connection point is on top of the node port
QPointF connectionPoint = connectionEndScenePosition(requiredPort);
portIndex = nodePortIndexUnderScenePoint(requiredPort,
connectionPoint);
if (portIndex == INVALID)
{
return false;
}
// 3) Node port is vacant
// port should be empty
if (!nodePortIsEmpty(requiredPort, portIndex))
return false;
// 4) Connection type equals node port type, or there is a registered type conversion that can translate between the two
auto connectionDataType = _connection->dataType();
auto const &modelTarget = _node->nodeDataModel();
NodeDataType candidateNodeDataType = modelTarget->dataType(requiredPort, portIndex);
if (connectionDataType.id != candidateNodeDataType.id)
{
if (requiredPort == PortType::In)
{
return typeConversionNeeded = (converterModel = _scene->registry().getTypeConverter(connectionDataType.id, candidateNodeDataType.id)) != nullptr;
}
return typeConversionNeeded = (converterModel = _scene->registry().getTypeConverter(candidateNodeDataType.id, connectionDataType.id)) != nullptr;
}
return true;
}
bool
NodeConnectionInteraction::
tryConnect() const
{
// 1) Check conditions from 'canConnect'
PortIndex portIndex = INVALID;
bool typeConversionNeeded = false;
std::unique_ptr<NodeDataModel> typeConverterModel;
if (!canConnect(portIndex, typeConversionNeeded, typeConverterModel))
{
return false;
}
/// 1.5) If the connection is possible but a type conversion is needed, add a converter node to the scene, and connect it properly
if (typeConversionNeeded)
{
//Determining port types
PortType requiredPort = connectionRequiredPort();
PortType connectedPort = requiredPort == PortType::Out ? PortType::In : PortType::Out;
//Get the node and port from where the connection starts
auto outNode = _connection->getNode(connectedPort);
auto outNodePortIndex = _connection->getPortIndex(connectedPort);
//Creating the converter node
Node& converterNode = _scene->createNode(std::move(typeConverterModel));
//Calculate and set the converter node's position
auto converterNodePos = NodeGeometry::calculateNodePositionBetweenNodePorts(portIndex, requiredPort, _node, outNodePortIndex, connectedPort, outNode, converterNode);
converterNode.nodeGraphicsObject().setPos(converterNodePos);
//Connecting the converter node to the two nodes trhat originally supposed to be connected.
//The connection order is different based on if the users connection was started from an input port, or an output port.
if (requiredPort == PortType::In)
{
_scene->createConnection(converterNode, 0, *outNode, outNodePortIndex);
_scene->createConnection(*_node, portIndex, converterNode, 0);
}
else
{
_scene->createConnection(converterNode, 0, *_node, portIndex);
_scene->createConnection(*outNode, outNodePortIndex, converterNode, 0);
}
//Delete the users connection, we already replaced it.
_scene->deleteConnection(*_connection);
return true;
}
// 2) Assign node to required port in Connection
PortType requiredPort = connectionRequiredPort();
_node->nodeState().setConnection(requiredPort,
portIndex,
*_connection);
// 3) Assign Connection to empty port in NodeState
// The port is not longer required after this function
_connection->setNodeToPort(*_node, requiredPort, portIndex);
// 4) Adjust Connection geometry
_node->nodeGraphicsObject().moveConnections();
// 5) Poke model to intiate data transfer
auto outNode = _connection->getNode(PortType::Out);
if (outNode)
{
PortIndex outPortIndex = _connection->getPortIndex(PortType::Out);
outNode->onDataUpdated(outPortIndex);
}
return true;
}
/// 1) Node and Connection should be already connected
/// 2) If so, clear Connection entry in the NodeState
/// 3) Set Connection end to 'requiring a port'
bool
NodeConnectionInteraction::
disconnect(PortType portToDisconnect) const
{
PortIndex portIndex =
_connection->getPortIndex(portToDisconnect);
NodeState &state = _node->nodeState();
// clear pointer to Connection in the NodeState
state.getEntries(portToDisconnect)[portIndex].clear();
// 4) Propagate invalid data to IN node
_connection->propagateEmptyData();
// clear Connection side
_connection->clearNode(portToDisconnect);
_connection->setRequiredPort(portToDisconnect);
_connection->getConnectionGraphicsObject().grabMouse();
return true;
}
// ------------------ util functions below
PortType
NodeConnectionInteraction::
connectionRequiredPort() const
{
auto const &state = _connection->connectionState();
return state.requiredPort();
}
QPointF
NodeConnectionInteraction::
connectionEndScenePosition(PortType portType) const
{
auto &go =
_connection->getConnectionGraphicsObject();
ConnectionGeometry& geometry = _connection->connectionGeometry();
QPointF endPoint = geometry.getEndPoint(portType);
return go.mapToScene(endPoint);
}
QPointF
NodeConnectionInteraction::
nodePortScenePosition(PortType portType, PortIndex portIndex) const
{
NodeGeometry const &geom = _node->nodeGeometry();
QPointF p = geom.portScenePosition(portIndex, portType);
NodeGraphicsObject& ngo = _node->nodeGraphicsObject();
return ngo.sceneTransform().map(p);
}
PortIndex
NodeConnectionInteraction::
nodePortIndexUnderScenePoint(PortType portType,
QPointF const & scenePoint) const
{
NodeGeometry const &nodeGeom = _node->nodeGeometry();
QTransform sceneTransform =
_node->nodeGraphicsObject().sceneTransform();
PortIndex portIndex = nodeGeom.checkHitScenePoint(portType,
scenePoint,
sceneTransform);
return portIndex;
}
bool
NodeConnectionInteraction::
nodePortIsEmpty(PortType portType, PortIndex portIndex) const
{
NodeState const & nodeState = _node->nodeState();
auto const & entries = nodeState.getEntries(portType);
if (entries[portIndex].empty()) return true;
const auto outPolicy = _node->nodeDataModel()->portOutConnectionPolicy(portIndex);
return ( portType == PortType::Out && outPolicy == NodeDataModel::ConnectionPolicy::Many);
}