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
191 lines (138 loc) · 4.63 KB
/
NodeConnectionInteraction.cpp
File metadata and controls
191 lines (138 loc) · 4.63 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
#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::NodeDataModel;
using QtNodes::TypeConverter;
NodeConnectionInteraction::
NodeConnectionInteraction(NodeIndex const& node,
ConnectionGraphicsObject& connection)
: _node(node)
, _connection(&connection)
{}
bool
NodeConnectionInteraction::
canConnect(PortIndex &portIndex, bool& converted) const
{
// 1) Connection requires a port
PortType requiredPort = connectionRequiredPort();
if (requiredPort == PortType::None)
{
return false;
}
// 1.5) Forbid connecting the node to itself
NodeIndex node = _connection->node(oppositePort(requiredPort));
if (node == _node)
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(oppositePort(requiredPort));
auto const modelTarget = _node.model();
NodeDataType candidateNodeDataType = modelTarget->nodePortDataType(_node, portIndex, requiredPort);
// if the types don't match, try a conversion
if (connectionDataType.id != candidateNodeDataType.id)
{
converted = true;
if (requiredPort == PortType::In)
{
return modelTarget->getTypeConvertable({connectionDataType, candidateNodeDataType});
}
else if (requiredPort == PortType::Out)
{
return modelTarget->getTypeConvertable({candidateNodeDataType, connectionDataType});
}
Q_UNREACHABLE();
}
converted = false;
return true;
}
bool
NodeConnectionInteraction::
tryConnect() const
{
// 1) Check conditions from 'canConnect'
PortIndex portIndex = INVALID;
bool converted;
if (!canConnect(portIndex, converted))
{
return false;
}
auto requiredPort = connectionRequiredPort();
auto connectedPort = oppositePort(requiredPort);
auto outNodePortIndex = _connection->portIndex(connectedPort);
auto outNode = _connection->node(connectedPort);
auto model = _connection->flowScene().model();
if (requiredPort == PortType::In)
{
return model->addConnection(outNode, outNodePortIndex, _node, portIndex);
}
return model->addConnection(_node, portIndex, outNode, outNodePortIndex);
}
// ------------------ util functions below
PortType
NodeConnectionInteraction::
connectionRequiredPort() const
{
auto const &state = _connection->state();
return state.requiredPort();
}
QPointF
NodeConnectionInteraction::
connectionEndScenePosition(PortType portType) const
{
ConnectionGeometry& geometry = _connection->geometry();
QPointF endPoint = geometry.getEndPoint(portType);
return _connection->mapToScene(endPoint);
}
QPointF
NodeConnectionInteraction::
nodePortScenePosition(PortType portType, PortIndex portIndex) const
{
NodeGraphicsObject const& ngo = *_connection->flowScene().nodeGraphicsObject(_node);
NodeGeometry const &geom = ngo.geometry();
QPointF p = geom.portScenePosition(portIndex, portType);
return ngo.sceneTransform().map(p);
}
PortIndex
NodeConnectionInteraction::
nodePortIndexUnderScenePoint(PortType portType,
QPointF const & scenePoint) const
{
NodeGraphicsObject const& ngo = *_connection->flowScene().nodeGraphicsObject(_node);
NodeGeometry const &nodeGeom = ngo.geometry();
QTransform sceneTransform =
ngo.sceneTransform();
PortIndex portIndex = nodeGeom.checkHitScenePoint(portType,
scenePoint,
sceneTransform);
return portIndex;
}
bool
NodeConnectionInteraction::
nodePortIsEmpty(PortType portType, PortIndex portIndex) const
{
NodeState const & nodeState = _connection->flowScene().nodeGraphicsObject(_node)->nodeState();
auto const & entries = nodeState.getEntries(portType);
if (entries[portIndex].empty())
return true;
const auto outPolicy = _node.model()->nodePortConnectionPolicy(_node, portIndex, portType);
return outPolicy == ConnectionPolicy::Many;
}