Basic implementation
authortimoph <timop.harkonen@gmail.com>
Fri, 1 Jan 2010 09:04:41 +0000 (09:04 +0000)
committertimoph <timop.harkonen@gmail.com>
Fri, 1 Jan 2010 09:04:41 +0000 (09:04 +0000)
git-svn-id: file:///svnroot/impuzzle/trunk@1 e6bec12f-0854-4cc4-ad26-6875f1509f77

16 files changed:
impuzzle.pro [new file with mode: 0644]
src/defines.h [new file with mode: 0644]
src/gameview.cpp [new file with mode: 0644]
src/gameview.h [new file with mode: 0644]
src/imageimporter.cpp [new file with mode: 0644]
src/imageimporter.h [new file with mode: 0644]
src/images/default.jpg [new file with mode: 0644]
src/main.cpp [new file with mode: 0644]
src/mainwindow.cpp [new file with mode: 0644]
src/mainwindow.h [new file with mode: 0644]
src/newgamedialog.cpp [new file with mode: 0644]
src/newgamedialog.h [new file with mode: 0644]
src/puzzleitem.cpp [new file with mode: 0644]
src/puzzleitem.h [new file with mode: 0644]
src/resources.qrc [new file with mode: 0644]
src/src.pro [new file with mode: 0644]

diff --git a/impuzzle.pro b/impuzzle.pro
new file mode 100644 (file)
index 0000000..3330bde
--- /dev/null
@@ -0,0 +1,2 @@
+TEMPLATE = subdirs
+SUBDIRS += src
diff --git a/src/defines.h b/src/defines.h
new file mode 100644 (file)
index 0000000..573bef0
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DEFINES_H
+#define DEFINES_H
+
+#define IMAGE_WIDTH 600
+
+#define IMAGE_HEIGHT 400
+
+#endif // DEFINES_H
diff --git a/src/gameview.cpp b/src/gameview.cpp
new file mode 100644 (file)
index 0000000..f660dc1
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gameview.h"
+#include "puzzleitem.h"
+#include "defines.h"
+
+#include <QGraphicsScene>
+#include <QDateTime>
+#include <QTimer>
+#include <QPropertyAnimation>
+#include <QParallelAnimationGroup>
+#include <QFont>
+
+#include <QDebug>
+
+GameView *GameView::instance_ = 0;
+
+GameView::GameView(QWidget *parent) :
+        QGraphicsView(parent)
+{
+    scene_ = new QGraphicsScene;
+    hiddenIndex_ = -1;
+    setScene(scene_);
+
+    qsrand(QDateTime::currentDateTime().toTime_t());
+}
+
+GameView *GameView::instance()
+{
+    if(!instance_) {
+        instance_ = new GameView;
+    }
+
+    return instance_;
+}
+
+QList<PuzzleItem *> GameView::pieces() const
+{
+    return pieces_;
+}
+
+void GameView::setPieces(const QList<PuzzleItem *> pieces)
+{
+    if(pieces.isEmpty()) {
+        qDebug() << "Empty list @ GameView::setPieces";
+        return;
+    }
+
+    QList<QGraphicsItem *> previousItems = scene_->items();
+    if(!previousItems.isEmpty()) {
+        foreach(QGraphicsItem *item, previousItems) {
+            scene_->removeItem(item);
+        }
+    }
+
+    pieces_ = pieces;
+
+    int horizontalCount = 0;
+
+    // Find out board size
+    if(pieces_.count() == 12) {
+        horizontalCount = 4;
+    }
+    else if(pieces_.count() == 20) {
+        horizontalCount = 5;
+    }
+    else {
+        qDebug() << "Invalid piece count @ GameView::setPieces";
+        qDebug() << QString("Count was %1").arg(pieces_.count());
+        return;
+    }
+
+    int verticalCount = pieces_.count() / horizontalCount;
+    int horizontalStep = IMAGE_WIDTH / horizontalCount + 5;
+    int verticalStep = IMAGE_HEIGHT / verticalCount + 5;
+
+    int pieceNumber = 0;
+
+    // Set pieces to their correct positions
+    for(int i = 0; i < verticalCount; ++i) {
+        for(int j = 0; j < horizontalCount; ++j) {
+            scene_->addItem(pieces_.at(pieceNumber));
+            QPointF point(j * horizontalStep, i * verticalStep);
+            pieces_.at(pieceNumber)->setPos(point);
+            pieces_.at(pieceNumber)->setCorrectPlace(point);
+            pieces_.at(pieceNumber)->setCurrentPlace(point);
+            pieceNumber++;
+        }
+    }
+
+    // Wait a second
+    QTimer::singleShot(1000, this, SLOT(shufflePieces()));
+}
+
+void GameView::shufflePieces()
+{
+    if(pieces_.isEmpty()) {
+        qDebug() << "Empty list @ GameView::shufflePieces";
+        return;
+    }
+
+    // TODO Give pieces ramdom locations
+    int rounds = 5;
+    for(int j = 0; j < rounds; ++j) {
+        for(int i = 0; i < pieces_.count(); ++i) {
+            QPointF tmp;
+            int changeIndex = 0;
+            while(changeIndex == i) {
+                changeIndex = qrand() % pieces_.count();
+            }
+            tmp = pieces_.at(changeIndex)->currentPlace();
+            pieces_.at(changeIndex)->setCurrentPlace(pieces_.at(i)->currentPlace());
+            pieces_.at(i)->setCurrentPlace(tmp);
+        }
+    }
+
+    // TODO Animate transitions to new locations
+    QParallelAnimationGroup *animationGroup = new QParallelAnimationGroup(this);
+    for(int i = 0; i < pieces_.count(); ++i) {
+        QPropertyAnimation *animation = new QPropertyAnimation(pieces_.at(i), "pos");
+        animation->setStartValue(pieces_.at(i)->correctPlace());
+        animation->setEndValue(pieces_.at(i)->currentPlace());
+        animation->setDuration(750);
+        animation->setEasingCurve(QEasingCurve::InOutCirc);
+        animationGroup->addAnimation(animation);
+    }
+    animationGroup->start();
+
+    // Hide random piece
+    int hiddenPiece = qrand() % pieces_.count();
+    emptyPlace_ = pieces_.at(hiddenPiece)->currentPlace();
+    pieces_.at(hiddenPiece)->hide();
+    hiddenIndex_ = hiddenPiece;
+}
+
+QPointF GameView::emptyPlace()
+{
+    return emptyPlace_;
+}
+
+void GameView::setEmptyPlace(const QPointF &place)
+{
+    emptyPlace_ = place;
+}
+
+bool GameView::areAllPiecesOk() const
+{
+    for(int i = 0; i < pieces_.count(); ++i) {
+        if(i == hiddenIndex_) {
+            continue;
+        }
+        else if(pieces_.at(i)->correctPlace() != pieces_.at(i)->currentPlace()) {
+            return false;
+        }
+    }
+    pieces_.at(hiddenIndex_)->show();
+    pieces_.at(hiddenIndex_)->moveMeTo(emptyPlace_);
+
+    for(int i = 0; i < pieces_.count(); ++i) {
+        pieces_.at(i)->setMovable(false);
+    }
+
+    return true;
+}
diff --git a/src/gameview.h b/src/gameview.h
new file mode 100644 (file)
index 0000000..f213fe3
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GAMEVIEW_H
+#define GAMEVIEW_H
+
+#include <QGraphicsView>
+
+class GraphicsScene;
+class PuzzleItem;
+
+class GameView : public QGraphicsView
+{
+    Q_OBJECT
+
+public:
+    static GameView *instance();
+    QList<PuzzleItem *> pieces() const;
+    QPointF emptyPlace();
+    void setEmptyPlace(const QPointF &place);
+    bool areAllPiecesOk() const;
+
+public slots:
+    void setPieces(const QList<PuzzleItem *> pieces);
+    void shufflePieces();
+
+private:
+    GameView(QWidget *parent = 0);
+
+    static GameView *instance_;
+    QGraphicsScene *scene_;
+    QList<PuzzleItem *> pieces_;
+    QPointF emptyPlace_;
+    int hiddenIndex_;
+};
+#endif
diff --git a/src/imageimporter.cpp b/src/imageimporter.cpp
new file mode 100644 (file)
index 0000000..961171f
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "imageimporter.h"
+#include "puzzleitem.h"
+#include "defines.h"
+
+#include <QPixmap>
+
+#include <QDebug>
+
+ImageImporter *ImageImporter::instance_ = 0;
+
+ImageImporter::ImageImporter(QObject *parent) :
+        QObject(parent)
+{
+
+}
+
+ImageImporter *ImageImporter::instance()
+{
+    if(!instance_) {
+        instance_ = new ImageImporter;
+    }
+
+    return instance_;
+}
+
+QList<PuzzleItem *> ImageImporter::newPieces(const QPixmap &pixmap, const int count)
+{
+    QPixmap tmp;
+
+    if(pixmap.isNull()) {
+        tmp = QPixmap(":/images/default.jpg");
+    }
+    else {
+        tmp = pixmap;
+    }
+
+    if(tmp.size().height() != IMAGE_HEIGHT || tmp.size().width() != IMAGE_WIDTH) {
+        tmp = pixmap.scaled(QSize(IMAGE_WIDTH, IMAGE_HEIGHT), Qt::KeepAspectRatioByExpanding);
+    }
+
+    QList<PuzzleItem *> list;
+
+    int horizontalCount = 0;
+
+    if(count == 12) {
+        horizontalCount = 4;
+    }
+    else if(count == 20) {
+        horizontalCount = 5;
+    }
+    else {
+        return list;
+    }
+
+    int verticalCount = count / horizontalCount;
+    int verticalStep = IMAGE_HEIGHT / verticalCount;
+    int horizontalStep = IMAGE_WIDTH / horizontalCount;
+
+    for(int i = 0; i < verticalCount; ++i) {
+        for(int j = 0; j < horizontalCount; ++j) {
+            PuzzleItem *item = new PuzzleItem;
+            item->setPixmap(tmp.copy(QRect(QPoint(j * horizontalStep, i * verticalStep),
+                                           QPoint(horizontalStep + j * horizontalStep, verticalStep + i * verticalStep))));
+            list.append(item);
+        }
+    }
+
+    return list;
+}
diff --git a/src/imageimporter.h b/src/imageimporter.h
new file mode 100644 (file)
index 0000000..9d56c62
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IMAGEIMPORTER_H
+#define IMAGEIMPORTER_H
+
+#include <QObject>
+
+class QPixmap;
+class PuzzleItem;
+
+class ImageImporter : public QObject
+{
+    Q_OBJECT
+
+public:
+    static ImageImporter *instance();
+    QList<PuzzleItem *> newPieces(const QPixmap &pixmap, const int count = 12);
+
+private:
+    ImageImporter(QObject *parent = 0);
+
+    static ImageImporter *instance_;
+};
+#endif
diff --git a/src/images/default.jpg b/src/images/default.jpg
new file mode 100644 (file)
index 0000000..0ada032
Binary files /dev/null and b/src/images/default.jpg differ
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644 (file)
index 0000000..13c7841
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <QApplication>
+
+#include "mainwindow.h"
+
+int main(int argc, char *argv[])
+{
+    QApplication app(argc, argv);
+
+    MainWindow mw;
+    mw.show();
+
+    return app.exec();
+}
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
new file mode 100644 (file)
index 0000000..e95883f
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "mainwindow.h"
+#include "gameview.h"
+
+#include <QAction>
+#include <QMenu>
+#include <QMenuBar>
+
+#include "imageimporter.h"
+
+MainWindow::MainWindow(QWidget *parent) :
+        QMainWindow(parent)
+{
+    createActions();
+    createMenu();
+
+    setCentralWidget(GameView::instance());
+}
+
+void MainWindow::createMenu()
+{
+    menu_ = menuBar()->addMenu("");
+    menu_->addAction(newGameAction_);
+    menu_->addAction(importAction_);
+}
+
+void MainWindow::createActions()
+{
+    newGameAction_ = new QAction(tr("New game"), this);
+    connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
+
+    importAction_ = new QAction(tr("Import image"), this);
+    connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
+    importAction_->setDisabled(true);
+}
+
+void MainWindow::importClicked()
+{
+
+}
+
+void MainWindow::newGameClicked()
+{
+    GameView::instance()->setPieces(ImageImporter::instance()->newPieces(0, 20));
+}
diff --git a/src/mainwindow.h b/src/mainwindow.h
new file mode 100644 (file)
index 0000000..f42c97b
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+class QAction;
+class QMenu;
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    MainWindow(QWidget *parent = 0);
+
+public slots:
+    void newGameClicked();
+    void importClicked();
+
+private:
+    void createActions();
+    void createMenu();
+
+    QAction *newGameAction_;
+    QAction *importAction_;
+
+    QMenu *menu_;
+};
+#endif
diff --git a/src/newgamedialog.cpp b/src/newgamedialog.cpp
new file mode 100644 (file)
index 0000000..2168e4c
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "newgamedialog.h"
+
+#include <QComboBox>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QPushButton>
+#include <QStringList>
+
+NewGameDialog::NewGameDialog(QWidget *parent) :
+        QDialog(parent)
+{
+    imageLabel_ = new QLabel(tr("Select image"));
+    imageCombo_ = new QComboBox;
+
+    piecesLabel_ = new QLabel(tr("Set piece count"));
+    piecesCombo_ = new QComboBox;
+
+    QStringList pieceList;
+    pieceList << "12" << "20" << "30";
+    piecesCombo_->addItems(pieceList);
+
+    comboLayout_ = new QHBoxLayout;
+    comboLayout_->addWidget(imageLabel_);
+    comboLayout_->addWidget(imageCombo_);
+    comboLayout_->addStretch();
+
+    startButton_ = new QPushButton(tr("Start"));
+
+    buttonLayout_ = new QHBoxLayout;
+    buttonLayout_->addWidget(piecesLabel_);
+    buttonLayout_->addWidget(piecesCombo_);
+    buttonLayout_->addStretch();
+    buttonLayout_->addWidget(startButton_);
+
+    mainLayout_ = new QVBoxLayout;
+    mainLayout_->addLayout(comboLayout_);
+    mainLayout_->addLayout(buttonLayout_);
+
+    setLayout(mainLayout_);
+
+    //connect(startButton_, SIGNAL(clicked()))
+}
diff --git a/src/newgamedialog.h b/src/newgamedialog.h
new file mode 100644 (file)
index 0000000..30bcf7f
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NEWGAMEDIALOG_H
+#define NEWGAMEDIALOG_H
+
+#include <QDialog>
+
+class QLabel;
+class QComboBox;
+class QPushButton;
+class QVBoxLayout;
+class QHBoxLayout;
+
+class NewGameDialog : public QDialog
+{
+    Q_OBJECT
+
+public:
+    NewGameDialog(QWidget *parent = 0);
+
+private:
+    QLabel *imageLabel_;
+    QLabel *piecesLabel_;
+
+    QComboBox *imageCombo_;
+    QComboBox *piecesCombo_;
+
+    QPushButton *startButton_;
+
+    QVBoxLayout *mainLayout_;
+    QHBoxLayout *comboLayout_;
+    QHBoxLayout *buttonLayout_;
+};
+
+#endif // NEWGAMEDIALOG_H
diff --git a/src/puzzleitem.cpp b/src/puzzleitem.cpp
new file mode 100644 (file)
index 0000000..1be8e9e
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "puzzleitem.h"
+#include "gameview.h"
+
+#include <QGraphicsSceneMouseEvent>
+#include <QPropertyAnimation>
+
+PuzzleItem::PuzzleItem(QGraphicsItem *parent) :
+        QGraphicsPixmapItem(parent)
+{
+    movable_ = true;
+    moveAnimation_ = new QPropertyAnimation(this, "pos", this);
+}
+
+QPointF PuzzleItem::correctPlace() const
+{
+    return correctPlace_;
+}
+
+QPointF PuzzleItem::currentPlace() const
+{
+    return currentPlace_;
+}
+
+void PuzzleItem::setCorrectPlace(const QPointF &place)
+{
+    correctPlace_ = place;
+}
+
+void PuzzleItem::setCurrentPlace(const QPointF &place)
+{
+    currentPlace_ = place;
+}
+
+bool PuzzleItem::movable() const
+{
+    return movable_;
+}
+
+void PuzzleItem::setMovable(bool canMove)
+{
+    movable_ = canMove;
+}
+
+void PuzzleItem::moveMeTo(const QPointF &location)
+{
+    moveAnimation_->setStartValue(currentPlace());
+    moveAnimation_->setEndValue(location);
+    moveAnimation_->start();
+}
+
+void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+    if(movable_) {
+        moveMeTo(GameView::instance()->emptyPlace());
+        QPointF tmp = currentPlace();
+        setCurrentPlace(GameView::instance()->emptyPlace());
+        GameView::instance()->setEmptyPlace(tmp);
+        event->accept();
+
+        // If piece is in its place check if we won the game
+        if(currentPlace() == correctPlace()) {
+            GameView::instance()->areAllPiecesOk();
+        }
+    }
+    else {
+        event->ignore();
+    }
+}
+
+void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
+{
+    event->ignore();
+}
diff --git a/src/puzzleitem.h b/src/puzzleitem.h
new file mode 100644 (file)
index 0000000..0fb5827
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PUZZLEITEM_H
+#define PUZZLEITEM_H
+
+#include <QGraphicsPixmapItem>
+#include <QObject>
+
+class QPropertyAnimation;
+
+class PuzzleItem : public QObject, public QGraphicsPixmapItem
+{
+    Q_OBJECT
+    Q_PROPERTY(QPointF pos READ pos WRITE setPos)
+
+public:
+    PuzzleItem(QGraphicsItem *parent = 0);
+    QPointF correctPlace() const;
+    QPointF currentPlace() const;
+    void setCorrectPlace(const QPointF &place);
+    void setCurrentPlace(const QPointF &place);
+    bool movable() const;
+    void setMovable(bool canMove);
+    void moveMeTo(const QPointF &location);
+
+protected:
+    void mousePressEvent(QGraphicsSceneMouseEvent *event);
+    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
+
+private:
+    QPointF correctPlace_;
+    QPointF currentPlace_;
+    bool movable_;
+    QPropertyAnimation *moveAnimation_;
+};
+#endif
diff --git a/src/resources.qrc b/src/resources.qrc
new file mode 100644 (file)
index 0000000..5c9eef1
--- /dev/null
@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/">
+        <file>images/default.jpg</file>
+    </qresource>
+</RCC>
diff --git a/src/src.pro b/src/src.pro
new file mode 100644 (file)
index 0000000..a8781bb
--- /dev/null
@@ -0,0 +1,20 @@
+TEMPLATE = app
+TARGET = impuzzle
+DEPENDPATH += .
+INCLUDEPATH += .
+DESTDIR = ../bin
+
+# Input
+HEADERS += gameview.h \
+    mainwindow.h \
+    imageimporter.h \
+    puzzleitem.h \
+    newgamedialog.h \
+    defines.h
+SOURCES += gameview.cpp \
+    main.cpp \
+    mainwindow.cpp \
+    imageimporter.cpp \
+    puzzleitem.cpp \
+    newgamedialog.cpp
+RESOURCES += resources.qrc