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