Piece place numbers, temporary random image fix and changelog update
[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 }
37
38 QPointF PuzzleItem::correctPlace() const
39 {
40     return correctPlace_;
41 }
42
43 QPointF PuzzleItem::currentPlace() const
44 {
45     return currentPlace_;
46 }
47
48 void PuzzleItem::setCorrectPlace(const QPointF &place)
49 {
50     correctPlace_ = place;
51 }
52
53 void PuzzleItem::setCurrentPlace(const QPointF &place)
54 {
55     currentPlace_ = place;
56 }
57
58 bool PuzzleItem::movable() const
59 {
60     return movable_;
61 }
62
63 void PuzzleItem::setMovable(bool canMove)
64 {
65     movable_ = canMove;
66 }
67
68 void PuzzleItem::moveMeTo(const QPointF &location)
69 {
70     moveAnimation_->setStartValue(currentPlace());
71     moveAnimation_->setEndValue(location);
72     moveAnimation_->start();
73 }
74
75 void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
76 {
77     if(movable_) {
78         moveMeTo(GameView::instance()->emptyPlace());
79         QPointF tmp = currentPlace();
80         setCurrentPlace(GameView::instance()->emptyPlace());
81         GameView::instance()->setEmptyPlace(tmp);
82         event->accept();
83
84         moveCount_++;
85
86         // If piece is in its place check if we won the game
87         bool won = false;
88         if(currentPlace() == correctPlace()) {
89             won = GameView::instance()->areAllPiecesOk();
90         }
91
92         // if we didn't win set pieces that can be moved
93         if(!won) {
94             GameView::instance()->setMovingPieces();
95         }
96     }
97     else {
98         event->ignore();
99     }
100 }
101
102 void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
103 {
104     event->ignore();
105 }
106
107 int PuzzleItem::moveCount()
108 {
109     return moveCount_;
110 }
111
112 void PuzzleItem::resetMoveCount()
113 {
114     moveCount_ = 0;
115 }
116
117 void PuzzleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
118 {
119     QGraphicsPixmapItem::paint(painter, option, widget);
120
121     painter->save();
122
123     QFont font = painter->font();
124     QFontMetrics metrics(font);
125     QRect numberRect(0, 0, metrics.height(), metrics.height());
126
127     painter->setPen(Qt::NoPen);
128
129     painter->setBrush(QColor(255, 255, 255, 192));
130     painter->drawRect(numberRect);
131
132     painter->setPen(Qt::black);
133
134     QTextOption textOption;
135     textOption.setAlignment(Qt::AlignCenter);
136
137     painter->drawText(numberRect, QString::number(pieceNumber_), textOption);
138
139     painter->restore();
140 }
141
142 int PuzzleItem::pieceNumber() const
143 {
144     return pieceNumber_;
145 }
146
147 void PuzzleItem::setPieceNumber(const int pieceNumber)
148 {
149     pieceNumber_ = pieceNumber;
150 }