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 "mainwindow.h"
22 #include "settingsdialog.h"
23 #include "aboutdialog.h"
24 #include "puzzleitem.h"
25 #include "statistics.h"
26 #include "statisticsdialog.h"
31 #include <QMessageBox>
32 #include <QCloseEvent>
36 #include "imageimporter.h"
38 MainWindow *MainWindow::instance_ = 0;
40 MainWindow::MainWindow(QWidget *parent) :
46 setCentralWidget(GameView::instance());
47 settingsDialog_ = new SettingsDialog(this);
49 setWindowTitle(tr("ImPuzzle"));
51 connect(GameView::instance(), SIGNAL(gameWon()), this, SLOT(gameEnded()));
52 connect(GameView::instance(), SIGNAL(gameRestored()), this, SLOT(enableSaving()));
55 MainWindow *MainWindow::instance()
58 instance_ = new MainWindow;
64 void MainWindow::createMenu()
66 menu_ = menuBar()->addMenu("&Game");
67 menu_->addAction(newGameAction_);
68 menu_->addAction(statisticsAction_);
69 menu_->addAction(saveAction_);
70 menu_->addAction(importAction_);
72 helpMenu_ = menuBar()->addMenu("&Help");
73 helpMenu_->addAction(aboutAction_);
76 void MainWindow::createActions()
78 newGameAction_ = new QAction(tr("New game"), this);
79 connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
81 importAction_ = new QAction(tr("Import image"), this);
82 connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
83 importAction_->setDisabled(true);
85 aboutAction_ = new QAction(tr("About ImPuzzle"), this);
86 connect(aboutAction_, SIGNAL(triggered()), this, SLOT(aboutClicked()));
88 saveAction_ = new QAction(tr("Save and quit"), this);
89 connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame()));
90 saveAction_->setDisabled(true);
92 statisticsAction_ = new QAction(tr("Statistics"), this);
93 connect(statisticsAction_, SIGNAL(triggered()), this, SLOT(showStatistics()));
96 void MainWindow::importClicked()
101 void MainWindow::newGameClicked()
103 int r = settingsDialog_->exec();
106 GameView::instance()->setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
108 PuzzleItem::setMoveCount(0);
112 void MainWindow::aboutClicked()
114 AboutDialog *dialog = new AboutDialog(this);
116 dialog->deleteLater();
119 void MainWindow::gameEnded()
121 if(saveAction_->isEnabled()) {
122 saveAction_->setDisabled(true);
123 Statistics::instance()->increaseGameCount(Settings::instance()->pieceCount() == EASY_PIECE_COUNT ? Statistics::easyDifficulty : Statistics::hardDifficulty);
124 Statistics::instance()->addNewScore(PuzzleItem::moveCount(),
125 Settings::instance()->pieceCount() == EASY_PIECE_COUNT ? Statistics::easyDifficulty : Statistics::hardDifficulty);
126 PuzzleItem::resetMoveCount();
130 void MainWindow::enableSaving()
132 if(!saveAction_->isEnabled()) {
133 saveAction_->setEnabled(true);
137 void MainWindow::closeEvent(QCloseEvent *event)
139 if(saveAction_->isEnabled()) {
140 int answer = QMessageBox::question(this, tr("Save game status?"),
141 tr("Saved status will be automatically loaded when you start the application next time"),
142 QMessageBox::Yes, QMessageBox::No);
144 if(answer == QMessageBox::Yes) {
145 GameView::instance()->saveGame();
152 void MainWindow::showStatistics()
154 StatisticsDialog dialog(this);