Adding icon
[impuzzle] / src / introitem.cpp
1 #include "introitem.h"
2 #include "defines.h"
3
4 #include <QPainter>
5 #include <QFontMetricsF>
6
7 IntroItem::IntroItem(QGraphicsItem *parent) :
8         QGraphicsItem(parent)
9 {
10     text_ = "";
11 }
12
13 QRectF IntroItem::boundingRect() const
14 {
15     return QRectF(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
16 }
17
18 void IntroItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
19 {
20     Q_UNUSED(option)
21     Q_UNUSED(widget)
22
23     painter->save();
24
25     painter->setBrush(Qt::NoBrush);
26     painter->setPen(Qt::black);
27
28     // Get font metrics
29     QFontMetricsF fontMetricsF(painter->font());
30     QRectF textRectF = fontMetricsF.boundingRect(text_);
31     int horizontalIntend = (IMAGE_WIDTH - textRectF.width()) / 2;
32     int verticalIntend = (IMAGE_HEIGHT - textRectF.height()) / 2;
33
34     // Draw text aligned to the center of boundingRect
35     painter->drawText(boundingRect()
36                       .adjusted(horizontalIntend, verticalIntend,-horizontalIntend, -verticalIntend),
37                       text_);
38
39     painter->restore();
40 }
41
42 QString IntroItem::text() const
43 {
44     return text_;
45 }
46
47 void IntroItem::setText(const QString &txt)
48 {
49     text_ = txt;
50
51     if(isVisible()) {
52         update();
53     }
54 }