fea49c2bf951f59f0b02a3f2e88231c0c7696e65
[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("");
63     menu_->addAction(newGameAction_);
64     menu_->addAction(saveAction_);
65     menu_->addAction(aboutAction_);
66     menu_->addAction(importAction_);
67 }
68
69 void MainWindow::createActions()
70 {
71     newGameAction_ = new QAction(tr("New game"), this);
72     connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
73
74     importAction_ = new QAction(tr("Import image"), this);
75     connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
76     importAction_->setDisabled(true);
77
78     aboutAction_ = new QAction(tr("About ImPuzzle"), this);
79     connect(aboutAction_, SIGNAL(triggered()), this, SLOT(aboutClicked()));
80
81     saveAction_ = new QAction(tr("Save and quit"), this);
82     connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame()));
83     saveAction_->setDisabled(true);
84 }
85
86 void MainWindow::importClicked()
87 {
88
89 }
90
91 void MainWindow::newGameClicked()
92 {
93     settingsDialog_->exec();
94
95     GameView::instance()->setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
96     enableSaving();
97 }
98
99 void MainWindow::aboutClicked()
100 {
101     AboutDialog *dialog = new AboutDialog(this);
102     dialog->exec();
103     dialog->deleteLater();
104 }
105
106 void MainWindow::gameEnded()
107 {
108     if(saveAction_->isEnabled()) {
109         saveAction_->setDisabled(true);
110         PuzzleItem::resetMoveCount();
111     }
112 }
113
114 void MainWindow::enableSaving()
115 {
116     if(!saveAction_->isEnabled()) {
117         saveAction_->setEnabled(true);
118     }
119 }