-
Notifications
You must be signed in to change notification settings - Fork 947
Expand file tree
/
Copy pathNodeGeometry.cpp
More file actions
348 lines (265 loc) · 7.32 KB
/
NodeGeometry.cpp
File metadata and controls
348 lines (265 loc) · 7.32 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
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
#include "NodeGeometry.hpp"
#include <iostream>
#include <cmath>
#include "PortType.hpp"
#include "NodeState.hpp"
#include "NodeDataModel.hpp"
#include "Node.hpp"
#include "NodeGraphicsObject.hpp"
#include "StyleCollection.hpp"
using QtNodes::NodeGeometry;
using QtNodes::NodeDataModel;
using QtNodes::PortIndex;
using QtNodes::PortType;
using QtNodes::Node;
NodeGeometry::
NodeGeometry(std::unique_ptr<NodeDataModel> const &dataModel)
: _width(100)
, _height(150)
, _inputPortWidth(70)
, _outputPortWidth(70)
, _entryHeight(20)
, _spacing(20)
, _hovered(false)
, _nSources(dataModel->nPorts(PortType::Out))
, _nSinks(dataModel->nPorts(PortType::In))
, _draggingPos(-1000, -1000)
, _dataModel(dataModel)
, _fontMetrics(QFont())
, _boldFontMetrics(QFont())
{
QFont f; f.setBold(true);
_boldFontMetrics = QFontMetrics(f);
}
unsigned int
NodeGeometry::nSources() const
{
return _dataModel->nPorts(PortType::Out);
}
unsigned int
NodeGeometry::nSinks() const
{
return _dataModel->nPorts(PortType::In);
}
QRectF
NodeGeometry::
entryBoundingRect() const
{
double const addon = 0.0;
return QRectF(0 - addon,
0 - addon,
_entryWidth + 2 * addon,
_entryHeight + 2 * addon);
}
QRectF
NodeGeometry::
boundingRect() const
{
auto const &nodeStyle = StyleCollection::nodeStyle();
double addon = 4 * nodeStyle.ConnectionPointDiameter;
return QRectF(0 - addon,
0 - addon,
_width + 2 * addon,
_height + 2 * addon);
}
void
NodeGeometry::
recalculateSize() const
{
_entryHeight = _fontMetrics.height();
{
unsigned int maxNumOfEntries = std::max(_nSinks, _nSources);
unsigned int step = _entryHeight + _spacing;
_height = step * maxNumOfEntries;
}
if(_dataModel->wembed())
if (auto w = _dataModel->embeddedWidget())
{
_height = std::max(_height, static_cast<unsigned>(w->height()));
}
_height += captionHeight();
_inputPortWidth = portWidth(PortType::In);
_outputPortWidth = portWidth(PortType::Out);
_width = _inputPortWidth +
_outputPortWidth +
2 * _spacing;
if(_dataModel->wembed())
if (auto w = _dataModel->embeddedWidget())
{
_width += w->width();
}
_width = std::max(_width, captionWidth());
if (_dataModel->validationState() != NodeValidationState::Valid)
{
_width = std::max(_width, validationWidth());
_height += validationHeight() + _spacing;
}
}
void
NodeGeometry::
recalculateSize(QFont const & font) const
{
QFontMetrics fontMetrics(font);
QFont boldFont = font;
boldFont.setBold(true);
QFontMetrics boldFontMetrics(boldFont);
if (_boldFontMetrics != boldFontMetrics)
{
_fontMetrics = fontMetrics;
_boldFontMetrics = boldFontMetrics;
recalculateSize();
}
}
QPointF
NodeGeometry::
portScenePosition(PortIndex index,
PortType portType,
QTransform const & t) const
{
auto const &nodeStyle = StyleCollection::nodeStyle();
unsigned int step = _entryHeight + _spacing;
QPointF result;
double totalHeight = 0.0;
totalHeight += captionHeight();
totalHeight += step * index;
// TODO: why?
totalHeight += step / 2.0;
switch (portType)
{
case PortType::Out:
{
double x = _width + nodeStyle.ConnectionPointDiameter;
result = QPointF(x, totalHeight);
break;
}
case PortType::In:
{
double x = 0.0 - nodeStyle.ConnectionPointDiameter;
result = QPointF(x, totalHeight);
break;
}
default:
break;
}
return t.map(result);
}
PortIndex
NodeGeometry::
checkHitScenePoint(PortType portType,
QPointF const scenePoint,
QTransform const & sceneTransform) const
{
auto const &nodeStyle = StyleCollection::nodeStyle();
PortIndex result = INVALID;
if (portType == PortType::None)
return result;
double const tolerance = 2.0 * nodeStyle.ConnectionPointDiameter;
unsigned int const nItems = _dataModel->nPorts(portType);
for (unsigned int i = 0; i < nItems; ++i)
{
auto pp = portScenePosition(i, portType, sceneTransform);
QPointF p = pp - scenePoint;
auto distance = std::sqrt(QPointF::dotProduct(p, p));
if (distance < tolerance)
{
result = PortIndex(i);
break;
}
}
return result;
}
QRect
NodeGeometry::
resizeRect() const
{
unsigned int rectSize = 7;
return QRect(_width - rectSize,
_height - rectSize,
rectSize,
rectSize);
}
QPointF
NodeGeometry::
widgetPosition() const
{
if (auto w = _dataModel->embeddedWidget())
{
if (_dataModel->validationState() != NodeValidationState::Valid)
{
return QPointF(_spacing + portWidth(PortType::In),
(captionHeight() + _height - validationHeight() - _spacing - w->height()) / 2.0);
}
return QPointF(_spacing + portWidth(PortType::In),
(captionHeight() + _height - w->height()) / 2.0);
}
return QPointF();
}
unsigned int
NodeGeometry::
captionHeight() const
{
if (!_dataModel->captionVisible())
return 0;
QString name = _dataModel->caption();
return _boldFontMetrics.boundingRect(name).height();
}
unsigned int
NodeGeometry::
captionWidth() const
{
if (!_dataModel->captionVisible())
return 0;
QString name = _dataModel->caption();
return _boldFontMetrics.boundingRect(name).width();
}
unsigned int
NodeGeometry::
validationHeight() const
{
QString msg = _dataModel->validationMessage();
return _boldFontMetrics.boundingRect(msg).height();
}
unsigned int
NodeGeometry::
validationWidth() const
{
QString msg = _dataModel->validationMessage();
return _boldFontMetrics.boundingRect(msg).width();
}
QPointF
NodeGeometry::
calculateNodePositionBetweenNodePorts(PortIndex targetPortIndex, PortType targetPort, Node* targetNode,
PortIndex sourcePortIndex, PortType sourcePort, Node* sourceNode,
Node& newNode)
{
//Calculating the nodes position in the scene. It'll be positioned half way between the two ports that it "connects".
//The first line calculates the halfway point between the ports (node position + port position on the node for both nodes averaged).
//The second line offsets this coordinate with the size of the new node, so that the new nodes center falls on the originally
//calculated coordinate, instead of it's upper left corner.
auto converterNodePos = (sourceNode->nodeGraphicsObject().pos() + sourceNode->nodeGeometry().portScenePosition(sourcePortIndex, sourcePort) +
targetNode->nodeGraphicsObject().pos() + targetNode->nodeGeometry().portScenePosition(targetPortIndex, targetPort)) / 2.0f;
converterNodePos.setX(converterNodePos.x() - newNode.nodeGeometry().width() / 2.0f);
converterNodePos.setY(converterNodePos.y() - newNode.nodeGeometry().height() / 2.0f);
return converterNodePos;
}
unsigned int
NodeGeometry::
portWidth(PortType portType) const
{
unsigned width = 0;
for (auto i = 0ul; i < _dataModel->nPorts(portType); ++i)
{
QString name;
if (_dataModel->portCaptionVisible(portType, i))
{
name = _dataModel->portCaption(portType, i);
}
else
{
name = _dataModel->dataType(portType, i).name;
}
width = std::max(unsigned(_fontMetrics.width(name)),
width);
}
return width;
}