2 Image Puzzle - A set your pieces straight game
3 Copyright (C) 2009 Timo Härkönen
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.
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.
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/>.
19 #include "introitem.h"
21 #include "mainwindow.h"
24 #include <QFontMetricsF>
26 IntroItem::IntroItem(QGraphicsItem *parent) :
32 QRectF IntroItem::boundingRect() const
34 return QRectF(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
37 void IntroItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
43 painter->setRenderHint(QPainter::Antialiasing, true);
44 painter->setPen(Qt::NoPen);
48 int hstep = boundingRect().width() / dots;
49 int vstep = boundingRect().height() / dots;
51 QColor colors[3] = {QColor(255, 255, 255), QColor(0, 0, 0), QColor(127, 127, 127)};
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)));
61 painter->setBrush(Qt::NoBrush);
62 painter->setPen(Qt::black);
64 QFont font = painter->font();
65 font.setPointSize(72);
67 painter->setFont(font);
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;
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));
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));
89 painter->drawText(boundingRect()
90 .adjusted(horizontalIntend, verticalIntend,-horizontalIntend, -verticalIntend),
93 pen.setColor(QColor(Qt::white));
95 font.setPointSize(12);
96 painter->setFont(font);
98 QString txt = QString("Version %1").arg(IMPUZZLE_VERSION);
100 QFontMetricsF fm(painter->font());
101 QRectF trect = fm.boundingRect(txt);
103 QTextOption textOption;
104 textOption.setAlignment(Qt::AlignRight);
106 painter->drawText(boundingRect().adjusted(0, verticalIntend * 2 - trect.height(), -trect.width(), 0),
112 QString IntroItem::text() const
117 void IntroItem::setText(const QString &txt)
126 void IntroItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
130 MainWindow::instance()->newGameClicked();