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>
33 #include <QApplication>
37 #include "imageimporter.h"
39 MainWindow *MainWindow::instance_ = 0;
41 MainWindow::MainWindow(QWidget *parent) :
47 setCentralWidget(GameView::instance());
48 settingsDialog_ = new SettingsDialog(this);
50 setWindowTitle(tr("ImPuzzle"));
52 connect(GameView::instance(), SIGNAL(gameWon()), this, SLOT(gameEnded()));
53 connect(GameView::instance(), SIGNAL(gameRestored()), this, SLOT(enableSaving()));
56 MainWindow *MainWindow::instance()
59 instance_ = new MainWindow;
65 void MainWindow::createMenu()
67 menu_ = menuBar()->addMenu("&Game");
68 menu_->addAction(newGameAction_);
69 menu_->addAction(statisticsAction_);
70 menu_->addAction(saveAction_);
71 menu_->addAction(quitAction_);
72 //menu_->addAction(importAction_);
74 helpMenu_ = menuBar()->addMenu("&Help");
75 helpMenu_->addAction(aboutAction_);
78 void MainWindow::createActions()
80 newGameAction_ = new QAction(tr("New game"), this);
81 connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
83 importAction_ = new QAction(tr("Import image"), this);
84 connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
85 importAction_->setDisabled(true);
87 aboutAction_ = new QAction(tr("About ImPuzzle"), this);
88 connect(aboutAction_, SIGNAL(triggered()), this, SLOT(aboutClicked()));
90 saveAction_ = new QAction(tr("Save and quit"), this);
91 connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame()));
92 saveAction_->setDisabled(true);
94 statisticsAction_ = new QAction(tr("Statistics"), this);
95 connect(statisticsAction_, SIGNAL(triggered()), this, SLOT(showStatistics()));
97 quitAction_ = new QAction(tr("Quit"), this);
98 connect(quitAction_, SIGNAL(triggered()), qApp, SLOT(quit()));
101 void MainWindow::importClicked()
106 void MainWindow::newGameClicked()
108 int r = settingsDialog_->exec();
111 GameView::instance()->setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
113 PuzzleItem::setMoveCount(0);
117 void MainWindow::aboutClicked()
119 AboutDialog *dialog = new AboutDialog(this);
121 dialog->deleteLater();
124 void MainWindow::gameEnded()
126 if(saveAction_->isEnabled()) {
127 saveAction_->setDisabled(true);
128 Statistics::instance()->increaseGameCount(Settings::instance()->pieceCount() == EASY_PIECE_COUNT ? Statistics::easyDifficulty : Statistics::hardDifficulty);
129 Statistics::instance()->addNewScore(PuzzleItem::moveCount(),
130 Settings::instance()->pieceCount() == EASY_PIECE_COUNT ? Statistics::easyDifficulty : Statistics::hardDifficulty);
131 PuzzleItem::resetMoveCount();
135 void MainWindow::enableSaving()
137 if(!saveAction_->isEnabled()) {
138 saveAction_->setEnabled(true);
142 void MainWindow::closeEvent(QCloseEvent *event)
144 if(saveAction_->isEnabled()) {
145 int answer = QMessageBox::question(this, tr("Save game status?"),
146 tr("Saved status will be automatically loaded when you start the application next time"),
147 QMessageBox::Yes, QMessageBox::No);
149 if(answer == QMessageBox::Yes) {
150 GameView::instance()->saveGame();
157 void MainWindow::showStatistics()
159 StatisticsDialog dialog(this);