forked from MadFishTheOne/qtpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextgraphicsitem.cpp
61 lines (51 loc) · 1.23 KB
/
textgraphicsitem.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Copyright (C) 2014 Leslie Zhai <[email protected]> */
#include "textgraphicsitem.h"
#include <QtGui/QFontMetrics>
#include <QtGui/QPainter>
TextGraphicsItem::TextGraphicsItem(QGraphicsItem* parent)
: QGraphicsItem(parent)
{
}
TextGraphicsItem::~TextGraphicsItem()
{
}
void TextGraphicsItem::setColor(const QColor& color)
{
m_color = color;
update(boundingRect());
}
void TextGraphicsItem::setFont(const QFont& font)
{
m_font = font;
update(boundingRect());
}
void TextGraphicsItem::setText(const QString& text)
{
m_text = text;
update(boundingRect());
}
void TextGraphicsItem::setImage(const QImage& image)
{
m_image = image;
update(boundingRect());
}
QRectF TextGraphicsItem::boundingRect() const
{
if (!m_image.isNull()) {
return m_image.rect();
}
QFontMetrics metrics(m_font);
return metrics.boundingRect(m_text);
}
void TextGraphicsItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
if (m_image.isNull()) {
painter->setFont(m_font);
painter->setPen(QPen(Qt::black));
painter->drawText(1, 1, m_text);
painter->setPen(QPen(m_color));
painter->drawText(0, 0, m_text);
} else {
painter->drawImage(0, 0, m_image);
}
}