Adding icon
[impuzzle] / src / introitem.cpp
1 /*
2   Image Puzzle - A set your pieces straight game
3   Copyright (C) 2009  Timo Härkönen
4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "introitem.h"
20 #include "defines.h"
21 #include "mainwindow.h"
22
23 #include <QPainter>
24 #include <QFontMetricsF>
25
26 IntroItem::IntroItem(QGraphicsItem *parent) :
27         QGraphicsItem(parent)
28 {
29     text_ = "";
30 }
31
32 QRectF IntroItem::boundingRect() const
33 {
34     return QRectF(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
35 }
36
37 void IntroItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
38 {
39     Q_UNUSED(option)
40     Q_UNUSED(widget)
41
42     painter->save();
43     painter->setRenderHint(QPainter::Antialiasing, true);
44     painter->setPen(Qt::NoPen);
45
46     const int dots = 96;
47
48     int hstep = boundingRect().width() / dots;
49     int vstep = boundingRect().height() / dots;
50
51     QColor colors[3] = {QColor(255, 255, 255), QColor(0, 0, 0), QColor(127, 127, 127)};
52
53     for(int i = 0; i < dots; ++i) {
54         for(int j = 0; j < dots; ++j) {
55             painter->setBrush(QBrush(colors[qrand() % 2]));
56             painter->drawRect(QRect(QPoint(i * hstep, j * vstep),
57                                     QPoint((i+1) * hstep, (j+1) * vstep)));
58         }
59     }
60
61     painter->setBrush(Qt::NoBrush);
62     painter->setPen(Qt::black);
63
64     QFont font = painter->font();
65     font.setPointSize(72);
66     font.setBold(true);
67     painter->setFont(font);
68
69     // Get font metrics
70     QFontMetricsF fontMetricsF(painter->font());
71     QRectF textRectF = fontMetricsF.boundingRect(text_);
72     int horizontalIntend = (boundingRect().width() - textRectF.width()) / 2 - 10;
73     int verticalIntend = (boundingRect().height() - textRectF.height()) / 2 - 10;
74
75     // Draw rect behind the text
76     painter->setBrush(QBrush(QColor(0,0,0,192)));
77     painter->setPen(Qt::NoPen);
78     painter->drawRect(boundingRect()
79                       .adjusted(0, verticalIntend, 5, -verticalIntend));
80
81     painter->setPen(Qt::black);
82     painter->setBrush(Qt::NoBrush);
83     // Draw text aligned to the center of boundingRect
84     QPen pen = painter->pen();
85     pen.setCosmetic(true);
86     pen.setColor(QColor(255,20,0,192));
87
88     painter->setPen(pen);
89     painter->drawText(boundingRect()
90                       .adjusted(horizontalIntend, verticalIntend,-horizontalIntend, -verticalIntend),
91                       text_);
92
93     pen.setColor(QColor(Qt::white));
94     painter->setPen(pen);
95     font.setPointSize(12);
96     painter->setFont(font);
97
98     QString txt = QString("Version %1").arg(IMPUZZLE_VERSION);
99
100     QFontMetricsF fm(painter->font());
101     QRectF trect = fm.boundingRect(txt);
102
103     QTextOption textOption;
104     textOption.setAlignment(Qt::AlignRight);
105
106     painter->drawText(boundingRect().adjusted(0, verticalIntend * 2 - trect.height(), -trect.width(), 0),
107                       txt, textOption);
108
109     painter->restore();
110 }
111
112 QString IntroItem::text() const
113 {
114     return text_;
115 }
116
117 void IntroItem::setText(const QString &txt)
118 {
119     text_ = txt;
120
121     if(isVisible()) {
122         update();
123     }
124 }
125
126 void IntroItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
127 {
128     Q_UNUSED(event);
129
130     MainWindow::instance()->newGameClicked();
131 }