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 "puzzleitem.h"
22 #include <QGraphicsSceneMouseEvent>
23 #include <QPropertyAnimation>
26 #include <QFontMetrics>
28 int PuzzleItem::moveCount_ = 0;
29 bool PuzzleItem::manuallyMovable_ = false;
31 PuzzleItem::PuzzleItem(QGraphicsItem *parent) :
32 QGraphicsPixmapItem(parent)
35 moveAnimation_ = new QPropertyAnimation(this, "pos", this);
40 QPointF PuzzleItem::correctPlace() const
45 QPointF PuzzleItem::currentPlace() const
50 void PuzzleItem::setCorrectPlace(const QPointF &place)
52 correctPlace_ = place;
55 void PuzzleItem::setCurrentPlace(const QPointF &place)
57 currentPlace_ = place;
60 bool PuzzleItem::movable() const
65 void PuzzleItem::setMovable(bool canMove)
70 void PuzzleItem::moveMeTo(const QPointF &location)
72 moveAnimation_->setStartValue(currentPlace());
73 moveAnimation_->setEndValue(location);
74 moveAnimation_->start();
77 void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
79 if(movable_ && manuallyMovable_) {
80 moveMeTo(GameView::instance()->emptyPlace());
81 QPointF tmp = currentPlace();
82 setCurrentPlace(GameView::instance()->emptyPlace());
83 GameView::instance()->setEmptyPlace(tmp);
88 // If piece is in its place check if we won the game
90 if(currentPlace() == correctPlace()) {
91 won = GameView::instance()->areAllPiecesOk();
94 // if we didn't win set pieces that can be moved
96 GameView::instance()->setMovingPieces();
104 void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
109 int PuzzleItem::moveCount()
114 void PuzzleItem::resetMoveCount()
119 void PuzzleItem::setManuallyMovable(const bool manuallyMovable)
121 manuallyMovable_ = manuallyMovable;
124 void PuzzleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
126 QGraphicsPixmapItem::paint(painter, option, widget);
131 QFont font = painter->font();
132 QFontMetrics metrics(font);
133 QRect numberRect(0, 0, metrics.height(), metrics.height());
135 painter->setPen(Qt::NoPen);
137 painter->setBrush(QColor(255, 255, 255, 192));
138 painter->drawRect(numberRect);
140 painter->setPen(Qt::black);
142 QTextOption textOption;
143 textOption.setAlignment(Qt::AlignCenter);
145 painter->drawText(numberRect, QString::number(pieceNumber_), textOption);
151 int PuzzleItem::pieceNumber() const
156 void PuzzleItem::setPieceNumber(const int pieceNumber)
158 pieceNumber_ = pieceNumber;
161 void PuzzleItem::setDrawNumber(bool value)
163 if(value != drawNumber_) {
169 bool PuzzleItem::drawNumber() const
174 void PuzzleItem::setMoveCount(int count)