New shuffle implementation
[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 "puzzleitem.h"
24
25 #include <QAction>
26 #include <QMenu>
27 #include <QMenuBar>
28
29 #include <QDebug>
30
31 #include "imageimporter.h"
32
33 MainWindow::MainWindow(QWidget *parent) :
34         QMainWindow(parent)
35 {
36     createActions();
37     createMenu();
38
39     setCentralWidget(GameView::instance());
40     settingsDialog_ = new SettingsDialog(this);
41
42     setWindowTitle(tr("ImPuzzle"));
43
44     connect(GameView::instance(), SIGNAL(gameWon()), this, SLOT(gameEnded()));
45     connect(GameView::instance(), SIGNAL(gameRestored()), this, SLOT(enableSaving()));
46 }
47
48 void MainWindow::createMenu()
49 {
50     menu_ = menuBar()->addMenu("");
51     menu_->addAction(newGameAction_);
52     menu_->addAction(saveAction_);
53     menu_->addAction(importAction_);
54 }
55
56 void MainWindow::createActions()
57 {
58     newGameAction_ = new QAction(tr("New game"), this);
59     connect(newGameAction_, SIGNAL(triggered()), this, SLOT(newGameClicked()));
60
61     importAction_ = new QAction(tr("Import image"), this);
62     connect(importAction_, SIGNAL(triggered()), this, SLOT(importClicked()));
63     importAction_->setDisabled(true);
64
65     settingsAction_ = new QAction(tr("Settings"), this);
66     connect(settingsAction_, SIGNAL(triggered()), this, SLOT(settingsClicked()));
67
68     saveAction_ = new QAction(tr("Save and quit"), this);
69     connect(saveAction_, SIGNAL(triggered()), GameView::instance(), SLOT(saveGame()));
70     saveAction_->setDisabled(true);
71 }
72
73 void MainWindow::importClicked()
74 {
75
76 }
77
78 void MainWindow::newGameClicked()
79 {
80     settingsDialog_->exec();
81
82     GameView::instance()->setPieces(ImageImporter::instance()->newPieces(Settings::instance()->image(), Settings::instance()->pieceCount()));
83     enableSaving();
84 }
85
86 void MainWindow::settingsClicked()
87 {
88
89 }
90
91 void MainWindow::gameEnded()
92 {
93     if(saveAction_->isEnabled()) {
94         saveAction_->setDisabled(true);
95         PuzzleItem::resetMoveCount();
96     }
97 }
98
99 void MainWindow::enableSaving()
100 {
101     if(!saveAction_->isEnabled()) {
102         saveAction_->setEnabled(true);
103     }
104 }