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