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"
29 #include <QMessageBox>
30 #include <QCloseEvent>
34 #include "imageimporter.h"
36 MainWindow *MainWindow::instance_ = 0;
38 MainWindow::MainWindow(QWidget *parent) :
44 setCentralWidget(GameView::instance());
45 settingsDialog_ = new SettingsDialog(this);
47 setWindowTitle(tr("ImPuzzle"));
49 connect(GameView::instance(), SIGNAL(gameWon()), this, SLOT(gameEnded()));
50 connect(GameView::instance(), SIGNAL(gameRestored()), this, SLOT(enableSaving()));
53 MainWindow *MainWindow::instance()
56 instance_ = new MainWindow;
62 void MainWindow::createMenu()
64 menu_ = menuBar()->addMenu("&Game");
65 menu_->addAction(newGameAction_);
66 menu_->addAction(saveAction_);
67 menu_->addAction(importAction_);
69 helpMenu_ = menuBar()->addMenu("&Help");
70 helpMenu_->addAction(aboutAction_);
73 void MainWindow::createActions()
75 newGameAction_ = new QAction(tr("New game"), this);
76 connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
78 importAction_ = new QAction(tr("Import image"), this);
79 connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
80 importAction_->setDisabled(true);
82 aboutAction_ = new QAction(tr("About ImPuzzle"), this);
83 connect(aboutAction_, SIGNAL(triggered()), this, SLOT(aboutClicked()));
85 saveAction_ = new QAction(tr("Save and quit"), this);
86 connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame()));
87 saveAction_->setDisabled(true);
90 void MainWindow::importClicked()
95 void MainWindow::newGameClicked()
97 int r = settingsDialog_->exec();
100 GameView::instance()->setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
102 PuzzleItem::setMoveCount(0);
106 void MainWindow::aboutClicked()
108 AboutDialog *dialog = new AboutDialog(this);
110 dialog->deleteLater();
113 void MainWindow::gameEnded()
115 if(saveAction_->isEnabled()) {
116 saveAction_->setDisabled(true);
117 PuzzleItem::resetMoveCount();
121 void MainWindow::enableSaving()
123 if(!saveAction_->isEnabled()) {
124 saveAction_->setEnabled(true);
128 void MainWindow::closeEvent(QCloseEvent *event)
130 if(saveAction_->isEnabled()) {
131 int answer = QMessageBox::question(this, tr("Save game status?"),
132 tr("Saved status will be automatically loaded when you start the application next time"),
133 QMessageBox::Yes, QMessageBox::No);
135 if(answer == QMessageBox::Yes) {
136 GameView::instance()->saveGame();