3d6f65bd5e88235aea309124531f634fc125e2dc
[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::black);
26     painter->setPen(Qt::NoPen);
27     painter->drawRect(boundingRect());
28
29     painter->setBrush(Qt::NoBrush);
30     painter->setPen(Qt::white);
31
32     // Get font metrics
33     QFontMetricsF fontMetricsF(painter->font());
34     QRectF textRectF = fontMetricsF.boundingRect(text_);
35     int horizontalIntend = (IMAGE_WIDTH - textRectF.width()) / 2;
36     int verticalIntend = (IMAGE_HEIGHT - textRectF.height()) / 2;
37
38     // Draw text aligned to the center of boundingRect
39     painter->drawText(boundingRect()
40                       .adjusted(horizontalIntend, verticalIntend,-horizontalIntend, -verticalIntend),
41                       text_);
42
43     painter->restore();
44 }
45
46 QString IntroItem::text() const
47 {
48     return text_;
49 }
50
51 void IntroItem::setText(const QString &txt)
52 {
53     text_ = txt;
54
55     if(isVisible()) {
56         update();
57     }
58 }