version++
[impuzzle] / src / mainwindow.cpp
1 /*
2   Image Puzzle - A set your pieces straight game
3   Copyright (C) 2009  Timo Härkönen
4
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.
9
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.
14
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/>.
17  */
18
19 #include "mainwindow.h"
20 #include "gameview.h"
21 #include "settings.h"
22 #include "settingsdialog.h"
23 #include "aboutdialog.h"
24 #include "puzzleitem.h"
25 #include "statistics.h"
26 #include "statisticsdialog.h"
27
28 #include <QAction>
29 #include <QMenu>
30 #include <QMenuBar>
31 #include <QMessageBox>
32 #include <QCloseEvent>
33 #include <QApplication>
34
35 #include <QDebug>
36
37 #include "imageimporter.h"
38
39 MainWindow *MainWindow::instance_ = 0;
40
41 MainWindow::MainWindow(QWidget *parent) :
42         QMainWindow(parent)
43 {
44     createActions();
45     createMenu();
46
47     setCentralWidget(GameView::instance());
48     settingsDialog_ = new SettingsDialog(this);
49
50     setWindowTitle(tr("ImPuzzle"));
51
52     connect(GameView::instance(), SIGNAL(gameWon()), this, SLOT(gameEnded()));
53     connect(GameView::instance(), SIGNAL(gameRestored()), this, SLOT(enableSaving()));
54 }
55
56 MainWindow *MainWindow::instance()
57 {
58     if(!instance_) {
59         instance_ = new MainWindow;
60     }
61
62     return instance_;
63 }
64
65 void MainWindow::createMenu()
66 {
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_);
73
74     helpMenu_ = menuBar()->addMenu("&Help");
75     helpMenu_->addAction(aboutAction_);
76 }
77
78 void MainWindow::createActions()
79 {
80     newGameAction_ = new QAction(tr("New game"), this);
81     connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
82
83     importAction_ = new QAction(tr("Import image"), this);
84     connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
85     importAction_->setDisabled(true);
86
87     aboutAction_ = new QAction(tr("About ImPuzzle"), this);
88     connect(aboutAction_, SIGNAL(triggered()), this, SLOT(aboutClicked()));
89
90     saveAction_ = new QAction(tr("Save and quit"), this);
91     connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame()));
92     saveAction_->setDisabled(true);
93
94     statisticsAction_ = new QAction(tr("Statistics"), this);
95     connect(statisticsAction_, SIGNAL(triggered()), this, SLOT(showStatistics()));
96
97     quitAction_ = new QAction(tr("Quit"), this);
98     connect(quitAction_, SIGNAL(triggered()), qApp, SLOT(quit()));
99 }
100
101 void MainWindow::importClicked()
102 {
103
104 }
105
106 void MainWindow::newGameClicked()
107 {
108     int r = settingsDialog_->exec();
109
110     if(r) {
111         GameView::instance()->setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
112         enableSaving();
113         PuzzleItem::setMoveCount(0);
114     }
115 }
116
117 void MainWindow::aboutClicked()
118 {
119     AboutDialog *dialog = new AboutDialog(this);
120     dialog->exec();
121     dialog->deleteLater();
122 }
123
124 void MainWindow::gameEnded()
125 {
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();
132     }
133 }
134
135 void MainWindow::enableSaving()
136 {
137     if(!saveAction_->isEnabled()) {
138         saveAction_->setEnabled(true);
139     }
140 }
141
142 void MainWindow::closeEvent(QCloseEvent *event)
143 {
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);
148
149         if(answer == QMessageBox::Yes) {
150             GameView::instance()->saveGame();
151         }
152     }
153
154     event->accept();
155 }
156
157 void MainWindow::showStatistics()
158 {
159     StatisticsDialog dialog(this);
160     dialog.exec();
161 }