Moved "Restart game" place in menu
[ghostsoverboard] / mainwindow.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game for Maemo 5
3
4         Copyright (C) 2011  Heli Hyvättinen
5
6         This file is part of Ghosts Overboard
7
8         Ghosts Overboard is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 **************************************************************************/
22
23 #include "mainwindow.h"
24 #include <QPixmap>
25 #include <QTimer>
26 #include <QDebug>
27 #include <QAction>
28 #include <QMenuBar>
29 #include <QMessageBox>
30 #include <QApplication>
31 #include <QLabel>
32 #include <QPushButton>
33 #include <QVBoxLayout>
34
35
36
37 MainWindow::MainWindow(QWidget *parent)
38     : QMainWindow(parent)
39 {
40     setWindowIcon(QIcon(":/pix/laiva_3aave.png"));
41     setWindowTitle("Ghosts Overboard");
42
43     pScene_ = new SeaScene ();
44     connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
45
46     pView_  = new QGraphicsView ();
47
48     pView_->setScene(pScene_);
49     setCentralWidget(pView_);
50
51     pPauseAction_ = new QAction(tr("Pause"),this);
52     pPauseAction_->setCheckable(true);
53     addAction(pPauseAction_);
54     connect(pPauseAction_,SIGNAL(triggered(bool)),pScene_,SLOT(pause(bool)));
55     menuBar()->addAction(pPauseAction_);
56
57     QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
58     addAction(pRestartLevelAction);
59     connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
60     menuBar()->addAction(pRestartLevelAction);
61
62     QAction * pRestartGameAction = new QAction(tr("Restart game"),this);
63     addAction(pRestartGameAction);
64     connect(pRestartGameAction,SIGNAL(triggered()),this,SLOT(restartGame()));
65     menuBar()->addAction(pRestartGameAction);
66
67     QAction * pVibrateAction = new QAction(tr("Vibration effects"),this);
68     pVibrateAction->setCheckable(true);
69     addAction(pVibrateAction);
70     connect(pVibrateAction,SIGNAL(triggered(bool)),pScene_,SLOT(vibrationActivate(bool)));
71     menuBar()->addAction(pVibrateAction);
72
73
74     QAction * pAboutAction = new QAction(tr("About"),this);
75     addAction(pAboutAction);
76     connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
77     menuBar()->addAction(pAboutAction);
78
79
80
81     Level level1(5,10);
82     levelList_.append(level1);
83     Level level2(5,10,2,50);
84     levelList_.append(level2);
85     Level level3(5,15,3,50);
86     levelList_.append(level3);
87     Level level4(5,15,5,50);
88     levelList_.append(level4);
89     Level level5(5,15,5,100);
90     levelList_.append(level5);
91
92     currentLevel_ = 0;
93
94
95     //the boundaries of the scene are set to match the size of the view window, which is not
96     //available in the constructor --> timer needed
97     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
98 }
99
100 MainWindow::~MainWindow()
101 {
102
103 }
104
105 void MainWindow::initializeBoundaries()
106 {
107         //the boundaries of the scene are set to match the size of the view window, and
108         //the view is set to show exactly the whole scene area
109
110     //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
111
112 //    QPoint topleft (0,0);
113 //    QSize windowsize = pView_->size();
114 //    QRectF rectangle (topleft,windowsize);
115
116     QRectF rectangle(0,0,800,424);
117
118     pScene_->setSceneRect(rectangle);
119     pView_->setSceneRect(rectangle);
120
121     // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
122
123     restartLevel();
124 }
125
126
127 void MainWindow::restartLevel()
128 {
129     pScene_->setupMap(levelList_.at(currentLevel_));
130 }
131
132 void MainWindow::about()
133 {
134     QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
135                        tr("Version %1"
136                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
137                           "<p>License: General Public License v2"
138                           "<p>Bug Reports: https://bugs.maemo.org/ "
139                           "enter_bug.cgi?product=Ghosts%20Overboard"
140                           ).arg(QApplication::applicationVersion()));
141
142
143
144 }
145
146 void MainWindow::nextLevel()
147 {
148
149     currentLevel_++;
150
151     if (levelList_.empty())
152         pScene_->setupMap(Level());
153
154
155     if ( currentLevel_ < levelList_.size() )
156     {
157         pScene_->setupMap(levelList_.at(currentLevel_));
158     }
159
160     else //Victory!
161     {
162
163        QDialog* pVictoryDialog = new QDialog(this);
164        pVictoryDialog->setWindowTitle(tr("You won!"));
165
166
167        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
168 //       QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
169
170        QPixmap victoryIcon (":/pix/aavesaari.png");
171        QLabel* pVictoryLabel = new QLabel();
172        pVictoryLabel->setPixmap(victoryIcon);
173
174        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
175
176
177        QVBoxLayout* pMainLayout = new QVBoxLayout;
178
179        QHBoxLayout* pTopLayout = new QHBoxLayout;
180        pMainLayout->addLayout(pTopLayout);
181
182        pTopLayout->addWidget(pVictoryLabel);
183        pTopLayout->addWidget(pTextLabel);
184
185
186
187        QHBoxLayout* pButtonLayout = new QHBoxLayout();
188        pMainLayout->addLayout(pButtonLayout);
189
190  //      pButtonLayout->addWidget(pQuitButton);
191        pButtonLayout->addWidget(pPlayAgainButton);
192
193
194
195        pVictoryDialog->setLayout(pMainLayout);
196
197        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
198
199        pVictoryDialog->exec();
200
201         //Never mind if the user cancels the dialog: restart the game anyway
202
203        restartGame();
204     }
205 }
206
207 bool MainWindow::event(QEvent *event)
208 {
209
210     switch (event->type())
211     {
212         //pause if app goes to background
213         case QEvent::WindowDeactivate:
214
215             if (pScene_)
216                 pScene_->pause(true);
217             break;
218
219         //un-pause if app gomes back to foreground unless it was paused before going to background
220         case QEvent::WindowActivate:
221
222
223             if (pPauseAction_ && !pPauseAction_->isChecked())
224             {
225                 if (pScene_)
226                     pScene_->pause(false);
227             }
228             break;
229
230         //Just to keep the compiler from complaining...
231         default:
232             break;
233
234      }
235
236
237
238     //pass the event to the ancestor for handling
239     return QMainWindow::event(event);
240
241  }
242
243 void MainWindow::restartGame()
244 {
245     currentLevel_ = 0;
246     pScene_->setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level, so no need to check if list empty
247
248 }