-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextLayoutItem.cpp
45 lines (36 loc) · 981 Bytes
/
TextLayoutItem.cpp
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
#include "TextLayoutItem.h"
#include <QPainter>
#include <QPen>
TextLayoutItem::TextLayoutItem(const QString& text, QGraphicsItem* parent)
: SimpleLayoutItem(parent), m_text(text) {
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
updateTextSize();
}
void TextLayoutItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->drawText(boundingRect(), Qt::AlignCenter, m_text);
}
void TextLayoutItem::expandTo(const QSizeF& size)
{
m_sizeHint = m_sizeHint.expandedTo(size);
updateGeometry();
}
void TextLayoutItem::growBy(const QMarginsF& margins)
{
m_sizeHint = m_sizeHint.grownBy(margins);
updateGeometry();
}
void TextLayoutItem::setFont(const QFont& font)
{
m_font = font;
updateTextSize();
}
QSizeF TextLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
{
return m_sizeHint;
}
void TextLayoutItem::updateTextSize()
{
QFontMetrics metrics(m_font);
expandTo(metrics.size(0, m_text));
}