Skip to content

Commit f2875ed

Browse files
committed
Fix node rendering with very long captions
1 parent 912a689 commit f2875ed

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/NodeGeometry.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ NodeGeometry(std::unique_ptr<NodeDataModel> const &dataModel)
2323
, _draggingPos(-1000, -1000)
2424
, _dataModel(dataModel)
2525
, _fontMetrics(QFont())
26-
{}
26+
, _boldFontMetrics(QFont())
27+
{
28+
QFont f; f.setBold(true);
29+
_boldFontMetrics = QFontMetrics(f);
30+
}
2731

2832
QRectF
2933
NodeGeometry::
@@ -70,7 +74,7 @@ recalculateSize() const
7074
_height = std::max(_height, static_cast<unsigned>(w->height()));
7175
}
7276

73-
_height += nameHeight();
77+
_height += captionHeight();
7478

7579
_inputPortWidth = portWidth(PortType::In);
7680
_outputPortWidth = portWidth(PortType::Out);
@@ -83,16 +87,25 @@ recalculateSize() const
8387
{
8488
_width += w->width();
8589
}
90+
91+
_width = std::max(_width, captionWidth());
8692
}
8793

8894

8995
void
9096
NodeGeometry::
91-
recalculateSize(QFontMetrics const & fontMetrics) const
97+
recalculateSize(QFont const & font) const
9298
{
93-
if (_fontMetrics != fontMetrics)
99+
QFontMetrics fontMetrics(font);
100+
QFont boldFont = font;
101+
boldFont.setBold(true);
102+
103+
QFontMetrics boldFontMetrics(boldFont);
104+
105+
if (_boldFontMetrics != boldFontMetrics)
94106
{
95107
_fontMetrics = fontMetrics;
108+
_boldFontMetrics = boldFontMetrics;
96109

97110
recalculateSize();
98111
}
@@ -113,7 +126,7 @@ portScenePosition(int index,
113126

114127
double totalHeight = 0.0;
115128

116-
totalHeight += nameHeight();
129+
totalHeight += captionHeight();
117130

118131
totalHeight += step * index;
119132

@@ -202,7 +215,7 @@ widgetPosition() const
202215
{
203216

204217
return QPointF(_spacing + portWidth(PortType::In),
205-
(nameHeight() + _height - w->height()) / 2.0);
218+
(captionHeight() + _height - w->height()) / 2.0);
206219
}
207220

208221
return QPointF();
@@ -211,14 +224,27 @@ widgetPosition() const
211224

212225
unsigned int
213226
NodeGeometry::
214-
nameHeight() const
227+
captionHeight() const
228+
{
229+
if (!_dataModel->captionVisible())
230+
return 0;
231+
232+
QString name = _dataModel->caption();
233+
234+
return _boldFontMetrics.boundingRect(name).height();
235+
}
236+
237+
238+
unsigned int
239+
NodeGeometry::
240+
captionWidth() const
215241
{
216242
if (!_dataModel->captionVisible())
217243
return 0;
218244

219245
QString name = _dataModel->caption();
220246

221-
return _fontMetrics.boundingRect(name).height();
247+
return _boldFontMetrics.boundingRect(name).width();
222248
}
223249

224250

src/NodeGeometry.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class NodeGeometry
8282

8383
/// Updates size if the QFontMetrics is changed
8484
void
85-
recalculateSize(QFontMetrics const &fontMetrics) const;
85+
recalculateSize(QFont const &font) const;
8686

8787
// TODO removed default QTransform()
8888
QPointF
@@ -105,7 +105,10 @@ class NodeGeometry
105105
private:
106106

107107
unsigned int
108-
nameHeight() const;
108+
captionHeight() const;
109+
110+
unsigned int
111+
captionWidth() const;
109112

110113
unsigned int
111114
portWidth(PortType portType) const;
@@ -135,4 +138,5 @@ class NodeGeometry
135138
std::unique_ptr<NodeDataModel> const &_dataModel;
136139

137140
mutable QFontMetrics _fontMetrics;
141+
mutable QFontMetrics _boldFontMetrics;
138142
};

src/NodePainter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ paint(QPainter* painter,
2323

2424
NodeGraphicsObject const & graphicsObject = node->nodeGraphicsObject();
2525

26-
geom.recalculateSize(painter->fontMetrics());
26+
geom.recalculateSize(painter->font());
2727

2828
//--------------------------------------------
2929

0 commit comments

Comments
 (0)