forked from paceholder/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeGeometry.cpp
More file actions
213 lines (162 loc) · 3.78 KB
/
NodeGeometry.cpp
File metadata and controls
213 lines (162 loc) · 3.78 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
#include "NodeGeometry.hpp"
#include <iostream>
#include "PortType.hpp"
#include "NodeState.hpp"
#include "NodeDataModel.hpp"
NodeGeometry::
NodeGeometry(std::unique_ptr<NodeDataModel> const &dataModel)
: _width(100)
, _height(150)
, _inputPortWidth(70)
, _outputPortWidth(70)
, _entryHeight(20)
, _spacing(20)
, _connectionPointDiameter(8)
, _hovered(false)
, _nSources(dataModel->nPorts(PortType::OUT))
, _nSinks(dataModel->nPorts(PortType::IN))
, _draggingPos(-1000, -1000)
, _opacity(0.80)
, _dataModel(dataModel)
, _fontMetrics(QFont())
{}
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
{
double addon = 4 * _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;
}
_height += nameHeight();
_inputPortWidth = portWidth(PortType::IN);
_outputPortWidth = portWidth(PortType::OUT);
_width = _inputPortWidth +
_outputPortWidth +
2 * _spacing;
if (auto w = _dataModel->embeddedWidget())
{
_width += w->width();
}
}
void
NodeGeometry::
recalculateSize(QFontMetrics const & fontMetrics) const
{
if (_fontMetrics != fontMetrics)
{
_fontMetrics = fontMetrics;
recalculateSize();
}
}
QPointF
NodeGeometry::
portScenePosition(int index,
PortType portType,
QTransform t) const
{
unsigned int step = _entryHeight + _spacing;
QPointF result;
double totalHeight = 0.0;
totalHeight += nameHeight();
totalHeight += step * index;
// TODO: why?
totalHeight += step / 2.0;
switch (portType)
{
case PortType::OUT:
{
double x = _width + _connectionPointDiameter;
result = QPointF(x, totalHeight);
break;
}
case PortType::IN:
{
double x = 0.0 - _connectionPointDiameter;
result = QPointF(x, totalHeight);
break;
}
default:
break;
}
return t.map(result);
}
PortIndex
NodeGeometry::
checkHitScenePoint(PortType portType,
QPointF const scenePoint,
QTransform sceneTransform) const
{
PortIndex result = INVALID;
if (portType == PortType::NONE)
return result;
double const tolerance = 2.0 * _connectionPointDiameter;
size_t const nItems = _dataModel->nPorts(portType);
for (size_t 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;
}
QPointF
NodeGeometry::
widgetPosition() const
{
if (auto w = _dataModel->embeddedWidget())
{
return QPointF(_spacing + portWidth(PortType::IN),
(nameHeight() + _height - w->height()) / 2.0);
}
return QPointF();
}
unsigned int
NodeGeometry::
nameHeight() const
{
QString name = _dataModel->modelName();
if (name.isEmpty())
return 0;
return _fontMetrics.boundingRect(name).height();
}
unsigned int
NodeGeometry::
portWidth(PortType portType) const
{
unsigned width = 0;
for (auto i = 0ul; i < _dataModel->nPorts(portType); ++i)
{
auto const &name = _dataModel->dataType(PortType::IN, i).name;
width = std::max(unsigned(_fontMetrics.width(name)),
width);
}
return width;
}