Skip to content

Commit ffd931d

Browse files
committed
Improve code for node size computing
1 parent 5dae8d2 commit ffd931d

File tree

9 files changed

+85
-75
lines changed

9 files changed

+85
-75
lines changed

docs/notes.rst

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ Node Geometry
66

77
.. code-block::
88
9-
vertical spacing
10-
/
11-
port width port width
12-
| | | | | |
13-
14-
0 _ _________________________________________ ___
15-
/ Caption \
16-
| ________________ | ___ caption height
9+
spacing spacing
10+
spacing / \ spacing
11+
\ port port /
12+
| | width | | | | width | |
13+
0
14+
0_|_________________________________________ ___
15+
/ \ ___ spacing
16+
| |
17+
| Caption | caption height
18+
| ________________ | ___
19+
| | | | ___ spacing
1720
| | | |
1821
O In Name | | Out Name O entry
1922
| | | | ___
20-
| | | | ___ vertical spacing
23+
| | | | ___ spacing
2124
| | | |
2225
O Another In | | Out Name O
2326
| | | |

examples/dynamic_ports/PortAddRemoveWidget.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ PortAddRemoveWidget(unsigned int nInPorts,
2121
hl->setSpacing(0);
2222

2323
_left = new QVBoxLayout();
24+
_left->setSpacing(0);
2425
_left->setContentsMargins(0, 0, 0, 0);
25-
_left->setSpacing(5);
2626
_left->addStretch();
2727

2828
_right = new QVBoxLayout();
29+
_right->setSpacing(0);
2930
_right->setContentsMargins(0, 0, 0, 0);
30-
_right->setSpacing(5);
3131
_right->addStretch();
3232

3333
hl->addLayout(_left);
@@ -64,15 +64,18 @@ addButtonGroupToLayout(QVBoxLayout * vbl,
6464
unsigned int portIndex)
6565
{
6666
auto l = new QHBoxLayout();
67+
l->setContentsMargins(0, 0, 0, 0);
6768

6869
auto button = new QPushButton("+");
70+
button->setFixedHeight(25);
6971
l->addWidget(button);
7072
connect(button,
7173
&QPushButton::clicked,
7274
this,
7375
&PortAddRemoveWidget::onPlusClicked);
7476

7577
button = new QPushButton("-");
78+
button->setFixedHeight(25);
7679
l->addWidget(button);
7780
connect(button,
7881
&QPushButton::clicked,

include/QtNodes/internal/AbstractNodeGeometry.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ class NODE_EDITOR_PUBLIC AbstractNodeGeometry
1919
virtual ~AbstractNodeGeometry() {}
2020

2121
/**
22-
* The node's rectangle plus some area around it needed to draw effects
23-
* properly, for example, a shadow or port dots.
22+
* The node's size plus some additional margin around it to account for drawing
23+
* effects (for example shadows) or node's parts outside the size rectangle
24+
* (for example port points).
25+
*
26+
* The default implementation returns QSize + 20 percent of width and heights
27+
* at each side of the rectangle.
2428
*/
25-
virtual QRectF boundingRect(NodeId const nodeId) const = 0;
29+
virtual QRectF boundingRect(NodeId const nodeId) const;
2630

2731
/// A direct rectangle defining the borders of the node's rectangle.
2832
virtual QSize size(NodeId const nodeId) const = 0;

src/AbstractNodeGeometry.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "AbstractGraphModel.hpp"
44
#include "StyleCollection.hpp"
55

6+
#include <QMargins>
7+
8+
69
namespace QtNodes
710
{
811

@@ -14,11 +17,30 @@ AbstractNodeGeometry(AbstractGraphModel & graphModel)
1417
}
1518

1619

20+
QRectF
21+
AbstractNodeGeometry::
22+
boundingRect(NodeId const nodeId) const
23+
{
24+
QSize s = size(nodeId);
25+
26+
double ratio = 0.20;
27+
28+
int widthMargin = s.width() * ratio;
29+
int heightMargin = s.height() * ratio;
30+
31+
QMargins margins(widthMargin, heightMargin, widthMargin, heightMargin);
32+
33+
QRectF r(QPointF(0, 0), s);
34+
35+
return r.marginsAdded(margins);
36+
}
37+
38+
1739
QPointF
1840
AbstractNodeGeometry::
19-
portScenePosition(NodeId const nodeId,
20-
PortType const portType,
21-
PortIndex const index,
41+
portScenePosition(NodeId const nodeId,
42+
PortType const portType,
43+
PortIndex const index,
2244
QTransform const & t) const
2345
{
2446
QPointF result = portPosition(nodeId, portType, index);
@@ -29,11 +51,11 @@ portScenePosition(NodeId const nodeId,
2951

3052
PortIndex
3153
AbstractNodeGeometry::
32-
checkPortHit(NodeId const nodeId,
54+
checkPortHit(NodeId const nodeId,
3355
PortType const portType,
34-
QPointF const nodePoint) const
56+
QPointF const nodePoint) const
3557
{
36-
auto const& nodeStyle = StyleCollection::nodeStyle();
58+
auto const & nodeStyle = StyleCollection::nodeStyle();
3759

3860
PortIndex result = InvalidPortIndex;
3961

@@ -65,4 +87,5 @@ checkPortHit(NodeId const nodeId,
6587
return result;
6688
}
6789

90+
6891
}

src/DefaultHorizontalNodeGeometry.cpp

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ DefaultHorizontalNodeGeometry::
1717
DefaultHorizontalNodeGeometry(AbstractGraphModel & graphModel)
1818
: AbstractNodeGeometry(graphModel)
1919
, _portSize(20)
20-
, _portSpasing(20)
20+
, _portSpasing(10)
2121
, _fontMetrics(QFont())
2222
, _boldFontMetrics(QFont())
2323
{
@@ -29,22 +29,6 @@ DefaultHorizontalNodeGeometry(AbstractGraphModel & graphModel)
2929
}
3030

3131

32-
QRectF
33-
DefaultHorizontalNodeGeometry::
34-
boundingRect(NodeId const nodeId) const
35-
{
36-
QRect r(QPoint(0, 0), size(nodeId));
37-
38-
auto const& nodeStyle = StyleCollection::nodeStyle();
39-
double addon = 4 * nodeStyle.ConnectionPointDiameter;
40-
41-
return r.adjusted(-addon,
42-
-addon,
43-
+addon,
44-
+addon);
45-
}
46-
47-
4832
QSize
4933
DefaultHorizontalNodeGeometry::
5034
size(NodeId const nodeId) const
@@ -68,10 +52,13 @@ recomputeSize(NodeId const nodeId) const
6852

6953
height += capRect.height();
7054

55+
height += _portSpasing; // space above caption
56+
height += _portSpasing; // space below caption
57+
7158
unsigned int inPortWidth = maxPortsTextAdvance(nodeId, PortType::In);
7259
unsigned int outPortWidth = maxPortsTextAdvance(nodeId, PortType::Out);
7360

74-
unsigned int width = inPortWidth + outPortWidth + 2 * _portSpasing;
61+
unsigned int width = inPortWidth + outPortWidth + 4 * _portSpasing;
7562

7663
if (auto w = _graphModel.nodeData<QWidget*>(nodeId, NodeRole::Widget))
7764
{
@@ -101,6 +88,7 @@ portPosition(NodeId const nodeId,
10188
double totalHeight = 0.0;
10289

10390
totalHeight += captionRect(nodeId).height();
91+
totalHeight += _portSpasing;
10492

10593
totalHeight += step * portIndex;
10694
totalHeight += step / 2.0;
@@ -111,15 +99,17 @@ portPosition(NodeId const nodeId,
11199
{
112100
case PortType::In:
113101
{
114-
double x = 0.0 - nodeStyle.ConnectionPointDiameter;
102+
//double x = 0.0 - nodeStyle.ConnectionPointDiameter;
103+
double x = 0.0;
115104

116105
result = QPointF(x, totalHeight);
117106
break;
118107
}
119108

120109
case PortType::Out:
121110
{
122-
double x = size.width() + nodeStyle.ConnectionPointDiameter;
111+
//double x = size.width() + nodeStyle.ConnectionPointDiameter;
112+
double x = size.width();
123113

124114
result = QPointF(x, totalHeight);
125115
break;
@@ -151,11 +141,13 @@ portTextPosition(NodeId const nodeId,
151141
switch (portType)
152142
{
153143
case PortType::In:
154-
p.setX(5.0);
144+
p.setX(_portSpasing);
145+
//p.setX(5.0);
155146
break;
156147

157148
case PortType::Out:
158-
p.setX(size.width() - 5.0 - rect.width());
149+
//p.setX(size.width() - 5.0 - rect.width());
150+
p.setX(size.width() - _portSpasing - rect.width());
159151
break;
160152

161153
default:
@@ -187,7 +179,7 @@ captionPosition(NodeId const nodeId) const
187179
{
188180
QSize size = _graphModel.nodeData<QSize>(nodeId, NodeRole::Size);
189181
return QPointF(0.5 * (size.width() - captionRect(nodeId).width()),
190-
0.5 * _portSpasing);
182+
0.5 * _portSpasing + captionRect(nodeId).height());
191183
}
192184

193185

@@ -222,13 +214,13 @@ QRect
222214
DefaultHorizontalNodeGeometry::
223215
resizeHandleRect(NodeId const nodeId) const
224216
{
225-
QSize size = _graphModel.nodeData<QSize>(nodeId,
217+
QSize size = _graphModel.nodeData<QSize>(nodeId,
226218
NodeRole::Size);
227219

228220
unsigned int rectSize = 7;
229221

230-
return QRect(size.width() - rectSize,
231-
size.height() - rectSize,
222+
return QRect(size.width() - _portSpasing,
223+
size.height() - _portSpasing,
232224
rectSize,
233225
rectSize);
234226
}

src/DefaultHorizontalNodeGeometry.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class NODE_EDITOR_PUBLIC DefaultHorizontalNodeGeometry : public AbstractNodeGeom
1818
DefaultHorizontalNodeGeometry(AbstractGraphModel & graphModel);
1919

2020
public:
21-
QRectF boundingRect(NodeId const nodeId) const override;
22-
2321
QSize size(NodeId const nodeId) const override;
2422

2523
void recomputeSize(NodeId const nodeId) const override;

src/DefaultNodePainter.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,13 @@ drawNodeRect(QPainter * painter,
7878
QPointF(2.0, size.height()));
7979

8080
gradient.setColorAt(0.0, nodeStyle.GradientColor0);
81-
gradient.setColorAt(0.03, nodeStyle.GradientColor1);
82-
gradient.setColorAt(0.97, nodeStyle.GradientColor2);
81+
gradient.setColorAt(0.10, nodeStyle.GradientColor1);
82+
gradient.setColorAt(0.90, nodeStyle.GradientColor2);
8383
gradient.setColorAt(1.0, nodeStyle.GradientColor3);
8484

8585
painter->setBrush(gradient);
8686

87-
float diam = nodeStyle.ConnectionPointDiameter;
88-
89-
QRectF boundary(-diam, -diam,
90-
2.0 * diam + size.width(),
91-
2.0 * diam + size.height());
87+
QRectF boundary(0, 0, size.width(), size.height());
9288

9389
double const radius = 3.0;
9490

src/DefaultVerticalNodeGeometry.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ DefaultVerticalNodeGeometry::
1717
DefaultVerticalNodeGeometry(AbstractGraphModel & graphModel)
1818
: AbstractNodeGeometry(graphModel)
1919
, _portSize(20)
20-
, _portSpasing(20)
20+
, _portSpasing(10)
2121
, _fontMetrics(QFont())
2222
, _boldFontMetrics(QFont())
2323
{
@@ -28,21 +28,6 @@ DefaultVerticalNodeGeometry(AbstractGraphModel & graphModel)
2828
_portSize = _fontMetrics.height();
2929
}
3030

31-
QRectF
32-
DefaultVerticalNodeGeometry::
33-
boundingRect(NodeId const nodeId) const
34-
{
35-
QRect r(QPoint(0, 0), size(nodeId));
36-
37-
auto const& nodeStyle = StyleCollection::nodeStyle();
38-
double addon = 4 * nodeStyle.ConnectionPointDiameter;
39-
40-
return r.adjusted(-addon,
41-
-addon,
42-
+addon,
43-
+addon);
44-
}
45-
4631

4732
QSize
4833
DefaultVerticalNodeGeometry::
@@ -67,6 +52,8 @@ recomputeSize(NodeId const nodeId) const
6752

6853
height += capRect.height();
6954

55+
height += _portSpasing;
56+
height += _portSpasing;
7057

7158
PortCount nInPorts = _graphModel.nodeData<PortCount>(nodeId, NodeRole::InPortCount);
7259
PortCount nOutPorts = _graphModel.nodeData<PortCount>(nodeId, NodeRole::OutPortCount);
@@ -89,6 +76,9 @@ recomputeSize(NodeId const nodeId) const
8976

9077
width = std::max(width, static_cast<unsigned int>(capRect.width()));
9178

79+
width += _portSpasing;
80+
width += _portSpasing;
81+
9282
QSize size(width, height);
9383

9484
_graphModel.setNodeData(nodeId, NodeRole::Size, size);
@@ -121,7 +111,7 @@ portPosition(NodeId const nodeId,
121111

122112
double x = (size.width() - (nInPorts - 1) * inPortWidth) / 2.0 + portIndex * inPortWidth;
123113

124-
double y = 0.0 - nodeStyle.ConnectionPointDiameter;
114+
double y = 0.0;
125115

126116
result = QPointF(x, y);
127117

@@ -137,7 +127,7 @@ portPosition(NodeId const nodeId,
137127

138128
double x = (size.width() - (nOutPorts - 1) * outPortWidth) / 2.0 + portIndex * outPortWidth;
139129

140-
double y = size.height() + nodeStyle.ConnectionPointDiameter;
130+
double y = size.height();
141131

142132
result = QPointF(x, y);
143133

@@ -393,6 +383,9 @@ portCaptionsHeight(NodeId const nodeId,
393383
}
394384
}
395385
}
386+
387+
default:
388+
break;
396389
}
397390

398391
return h;

src/DefaultVerticalNodeGeometry.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class NODE_EDITOR_PUBLIC DefaultVerticalNodeGeometry : public AbstractNodeGeomet
1818
DefaultVerticalNodeGeometry(AbstractGraphModel & graphModel);
1919

2020
public:
21-
QRectF boundingRect(NodeId const nodeId) const override;
22-
2321
QSize size(NodeId const nodeId) const override;
2422

2523
void recomputeSize(NodeId const nodeId) const override;

0 commit comments

Comments
 (0)