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