1be8e9e182f17ac29c3120cb600bc5984829e4b4
[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
25 PuzzleItem::PuzzleItem(QGraphicsItem *parent) :
26         QGraphicsPixmapItem(parent)
27 {
28     movable_ = true;
29     moveAnimation_ = new QPropertyAnimation(this, "pos", this);
30 }
31
32 QPointF PuzzleItem::correctPlace() const
33 {
34     return correctPlace_;
35 }
36
37 QPointF PuzzleItem::currentPlace() const
38 {
39     return currentPlace_;
40 }
41
42 void PuzzleItem::setCorrectPlace(const QPointF &place)
43 {
44     correctPlace_ = place;
45 }
46
47 void PuzzleItem::setCurrentPlace(const QPointF &place)
48 {
49     currentPlace_ = place;
50 }
51
52 bool PuzzleItem::movable() const
53 {
54     return movable_;
55 }
56
57 void PuzzleItem::setMovable(bool canMove)
58 {
59     movable_ = canMove;
60 }
61
62 void PuzzleItem::moveMeTo(const QPointF &location)
63 {
64     moveAnimation_->setStartValue(currentPlace());
65     moveAnimation_->setEndValue(location);
66     moveAnimation_->start();
67 }
68
69 void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
70 {
71     if(movable_) {
72         moveMeTo(GameView::instance()->emptyPlace());
73         QPointF tmp = currentPlace();
74         setCurrentPlace(GameView::instance()->emptyPlace());
75         GameView::instance()->setEmptyPlace(tmp);
76         event->accept();
77
78         // If piece is in its place check if we won the game
79         if(currentPlace() == correctPlace()) {
80             GameView::instance()->areAllPiecesOk();
81         }
82     }
83     else {
84         event->ignore();
85     }
86 }
87
88 void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
89 {
90     event->ignore();
91 }