Bug fixes
[impuzzle] / src / puzzleitem.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 "puzzleitem.h"
20 #include "gameview.h"
21
22 #include <QGraphicsSceneMouseEvent>
23 #include <QPropertyAnimation>
24 #include <QPainter>
25 #include <QFont>
26 #include <QFontMetrics>
27
28 int PuzzleItem::moveCount_ = 0;
29
30 PuzzleItem::PuzzleItem(QGraphicsItem *parent) :
31         QGraphicsPixmapItem(parent)
32 {
33     movable_ = true;
34     moveAnimation_ = new QPropertyAnimation(this, "pos", this);
35     pieceNumber_ = 0;
36     drawNumber_ = true;
37 }
38
39 QPointF PuzzleItem::correctPlace() const
40 {
41     return correctPlace_;
42 }
43
44 QPointF PuzzleItem::currentPlace() const
45 {
46     return currentPlace_;
47 }
48
49 void PuzzleItem::setCorrectPlace(const QPointF &place)
50 {
51     correctPlace_ = place;
52 }
53
54 void PuzzleItem::setCurrentPlace(const QPointF &place)
55 {
56     currentPlace_ = place;
57 }
58
59 bool PuzzleItem::movable() const
60 {
61     return movable_;
62 }
63
64 void PuzzleItem::setMovable(bool canMove)
65 {
66     movable_ = canMove;
67 }
68
69 void PuzzleItem::moveMeTo(const QPointF &location)
70 {
71     moveAnimation_->setStartValue(currentPlace());
72     moveAnimation_->setEndValue(location);
73     moveAnimation_->start();
74 }
75
76 void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
77 {
78     if(movable_) {
79         moveMeTo(GameView::instance()->emptyPlace());
80         QPointF tmp = currentPlace();
81         setCurrentPlace(GameView::instance()->emptyPlace());
82         GameView::instance()->setEmptyPlace(tmp);
83         event->accept();
84
85         moveCount_++;
86
87         // If piece is in its place check if we won the game
88         bool won = false;
89         if(currentPlace() == correctPlace()) {
90             won = GameView::instance()->areAllPiecesOk();
91         }
92
93         // if we didn't win set pieces that can be moved
94         if(!won) {
95             GameView::instance()->setMovingPieces();
96         }
97     }
98     else {
99         event->ignore();
100     }
101 }
102
103 void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
104 {
105     event->ignore();
106 }
107
108 int PuzzleItem::moveCount()
109 {
110     return moveCount_;
111 }
112
113 void PuzzleItem::resetMoveCount()
114 {
115     moveCount_ = 0;
116 }
117
118 void PuzzleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
119 {
120     QGraphicsPixmapItem::paint(painter, option, widget);
121
122     if(drawNumber_) {
123         painter->save();
124
125         QFont font = painter->font();
126         QFontMetrics metrics(font);
127         QRect numberRect(0, 0, metrics.height(), metrics.height());
128
129         painter->setPen(Qt::NoPen);
130
131         painter->setBrush(QColor(255, 255, 255, 192));
132         painter->drawRect(numberRect);
133
134         painter->setPen(Qt::black);
135
136         QTextOption textOption;
137         textOption.setAlignment(Qt::AlignCenter);
138
139         painter->drawText(numberRect, QString::number(pieceNumber_), textOption);
140
141         painter->restore();
142     }
143 }
144
145 int PuzzleItem::pieceNumber() const
146 {
147     return pieceNumber_;
148 }
149
150 void PuzzleItem::setPieceNumber(const int pieceNumber)
151 {
152     pieceNumber_ = pieceNumber;
153 }
154
155 void PuzzleItem::setDrawNumber(bool value)
156 {
157     if(value != drawNumber_) {
158         drawNumber_ = value;
159         update();
160     }
161 }
162
163 bool PuzzleItem::drawNumber() const
164 {
165     return drawNumber_;
166 }
167
168 void PuzzleItem::setMoveCount(int count)
169 {
170     moveCount_ = count;
171 }