Collecting basic statistics from won games
[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
34 #include <QDebug>
35
36 #include "imageimporter.h"
37
38 MainWindow *MainWindow::instance_ = 0;
39
40 MainWindow::MainWindow(QWidget *parent) :
41         QMainWindow(parent)
42 {
43     createActions();
44     createMenu();
45
46     setCentralWidget(GameView::instance());
47     settingsDialog_ = new SettingsDialog(this);
48
49     setWindowTitle(tr("ImPuzzle"));
50
51     connect(GameView::instance(), SIGNAL(gameWon()), this, SLOT(gameEnded()));
52     connect(GameView::instance(), SIGNAL(gameRestored()), this, SLOT(enableSaving()));
53 }
54
55 MainWindow *MainWindow::instance()
56 {
57     if(!instance_) {
58         instance_ = new MainWindow;
59     }
60
61     return instance_;
62 }
63
64 void MainWindow::createMenu()
65 {
66     menu_ = menuBar()->addMenu("&Game");
67     menu_->addAction(newGameAction_);
68     menu_->addAction(statisticsAction_);
69     menu_->addAction(saveAction_);
70     menu_->addAction(importAction_);
71
72     helpMenu_ = menuBar()->addMenu("&Help");
73     helpMenu_->addAction(aboutAction_);
74 }
75
76 void MainWindow::createActions()
77 {
78     newGameAction_ = new QAction(tr("New game"), this);
79     connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
80
81     importAction_ = new QAction(tr("Import image"), this);
82     connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
83     importAction_->setDisabled(true);
84
85     aboutAction_ = new QAction(tr("About ImPuzzle"), this);
86     connect(aboutAction_, SIGNAL(triggered()), this, SLOT(aboutClicked()));
87
88     saveAction_ = new QAction(tr("Save and quit"), this);
89     connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame()));
90     saveAction_->setDisabled(true);
91
92     statisticsAction_ = new QAction(tr("Statistics"), this);
93     connect(statisticsAction_, SIGNAL(triggered()), this, SLOT(showStatistics()));
94 }
95
96 void MainWindow::importClicked()
97 {
98
99 }
100
101 void MainWindow::newGameClicked()
102 {
103     int r = settingsDialog_->exec();
104
105     if(r) {
106         GameView::instance()->setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
107         enableSaving();
108         PuzzleItem::setMoveCount(0);
109     }
110 }
111
112 void MainWindow::aboutClicked()
113 {
114     AboutDialog *dialog = new AboutDialog(this);
115     dialog->exec();
116     dialog->deleteLater();
117 }
118
119 void MainWindow::gameEnded()
120 {
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();
127     }
128 }
129
130 void MainWindow::enableSaving()
131 {
132     if(!saveAction_->isEnabled()) {
133         saveAction_->setEnabled(true);
134     }
135 }
136
137 void MainWindow::closeEvent(QCloseEvent *event)
138 {
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);
143
144         if(answer == QMessageBox::Yes) {
145             GameView::instance()->saveGame();
146         }
147     }
148
149     event->accept();
150 }
151
152 void MainWindow::showStatistics()
153 {
154     StatisticsDialog dialog(this);
155     dialog.exec();
156 }