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;
30 PuzzleItem::PuzzleItem(QGraphicsItem *parent) :
31 QGraphicsPixmapItem(parent)
34 moveAnimation_ = new QPropertyAnimation(this, "pos", this);
38 QPointF PuzzleItem::correctPlace() const
43 QPointF PuzzleItem::currentPlace() const
48 void PuzzleItem::setCorrectPlace(const QPointF &place)
50 correctPlace_ = place;
53 void PuzzleItem::setCurrentPlace(const QPointF &place)
55 currentPlace_ = place;
58 bool PuzzleItem::movable() const
63 void PuzzleItem::setMovable(bool canMove)
68 void PuzzleItem::moveMeTo(const QPointF &location)
70 moveAnimation_->setStartValue(currentPlace());
71 moveAnimation_->setEndValue(location);
72 moveAnimation_->start();
75 void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
78 moveMeTo(GameView::instance()->emptyPlace());
79 QPointF tmp = currentPlace();
80 setCurrentPlace(GameView::instance()->emptyPlace());
81 GameView::instance()->setEmptyPlace(tmp);
86 // If piece is in its place check if we won the game
88 if(currentPlace() == correctPlace()) {
89 won = GameView::instance()->areAllPiecesOk();
92 // if we didn't win set pieces that can be moved
94 GameView::instance()->setMovingPieces();
102 void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
107 int PuzzleItem::moveCount()
112 void PuzzleItem::resetMoveCount()
117 void PuzzleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
119 QGraphicsPixmapItem::paint(painter, option, widget);
123 QFont font = painter->font();
124 QFontMetrics metrics(font);
125 QRect numberRect(0, 0, metrics.height(), metrics.height());
127 painter->setPen(Qt::NoPen);
129 painter->setBrush(QColor(255, 255, 255, 192));
130 painter->drawRect(numberRect);
132 painter->setPen(Qt::black);
134 QTextOption textOption;
135 textOption.setAlignment(Qt::AlignCenter);
137 painter->drawText(numberRect, QString::number(pieceNumber_), textOption);
142 int PuzzleItem::pieceNumber() const
147 void PuzzleItem::setPieceNumber(const int pieceNumber)
149 pieceNumber_ = pieceNumber;