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);
39 QPointF PuzzleItem::correctPlace() const
44 QPointF PuzzleItem::currentPlace() const
49 void PuzzleItem::setCorrectPlace(const QPointF &place)
51 correctPlace_ = place;
54 void PuzzleItem::setCurrentPlace(const QPointF &place)
56 currentPlace_ = place;
59 bool PuzzleItem::movable() const
64 void PuzzleItem::setMovable(bool canMove)
69 void PuzzleItem::moveMeTo(const QPointF &location)
71 moveAnimation_->setStartValue(currentPlace());
72 moveAnimation_->setEndValue(location);
73 moveAnimation_->start();
76 void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
79 moveMeTo(GameView::instance()->emptyPlace());
80 QPointF tmp = currentPlace();
81 setCurrentPlace(GameView::instance()->emptyPlace());
82 GameView::instance()->setEmptyPlace(tmp);
87 // If piece is in its place check if we won the game
89 if(currentPlace() == correctPlace()) {
90 won = GameView::instance()->areAllPiecesOk();
93 // if we didn't win set pieces that can be moved
95 GameView::instance()->setMovingPieces();
103 void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
108 int PuzzleItem::moveCount()
113 void PuzzleItem::resetMoveCount()
118 void PuzzleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
120 QGraphicsPixmapItem::paint(painter, option, widget);
125 QFont font = painter->font();
126 QFontMetrics metrics(font);
127 QRect numberRect(0, 0, metrics.height(), metrics.height());
129 painter->setPen(Qt::NoPen);
131 painter->setBrush(QColor(255, 255, 255, 192));
132 painter->drawRect(numberRect);
134 painter->setPen(Qt::black);
136 QTextOption textOption;
137 textOption.setAlignment(Qt::AlignCenter);
139 painter->drawText(numberRect, QString::number(pieceNumber_), textOption);
145 int PuzzleItem::pieceNumber() const
150 void PuzzleItem::setPieceNumber(const int pieceNumber)
152 pieceNumber_ = pieceNumber;
155 void PuzzleItem::setDrawNumber(bool value)
157 if(value != drawNumber_) {
163 bool PuzzleItem::drawNumber() const