Merge branch 'levelstructure'
[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 * pVibrateAction = new QAction(tr("Vibration effects"),this);
63     pVibrateAction->setCheckable(true);
64     addAction(pVibrateAction);
65     connect(pVibrateAction,SIGNAL(triggered(bool)),pScene_,SLOT(vibrationActivate(bool)));
66     menuBar()->addAction(pVibrateAction);
67
68
69     QAction * pAboutAction = new QAction(tr("About"),this);
70     addAction(pAboutAction);
71     connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
72     menuBar()->addAction(pAboutAction);
73
74     QAction * pRestartGameAction = new QAction(tr("Restart game"),this);
75     addAction(pRestartGameAction);
76     connect(pRestartGameAction,SIGNAL(triggered()),this,SLOT(restartGame()));
77     menuBar()->addAction(pRestartGameAction);
78
79     Level level1(5,10);
80     levelList_.append(level1);
81     Level level2(5,10,2,50);
82     levelList_.append(level2);
83     Level level3(5,15,3,50);
84     levelList_.append(level3);
85     Level level4(5,15,5,50);
86     levelList_.append(level4);
87     Level level5(5,15,5,100);
88     levelList_.append(level5);
89
90     currentLevel_ = 0;
91
92
93     //the boundaries of the scene are set to match the size of the view window, which is not
94     //available in the constructor --> timer needed
95     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
96 }
97
98 MainWindow::~MainWindow()
99 {
100
101 }
102
103 void MainWindow::initializeBoundaries()
104 {
105         //the boundaries of the scene are set to match the size of the view window, and
106         //the view is set to show exactly the whole scene area
107
108     //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
109
110 //    QPoint topleft (0,0);
111 //    QSize windowsize = pView_->size();
112 //    QRectF rectangle (topleft,windowsize);
113
114     QRectF rectangle(0,0,800,424);
115
116     pScene_->setSceneRect(rectangle);
117     pView_->setSceneRect(rectangle);
118
119     // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
120
121     restartLevel();
122 }
123
124
125 void MainWindow::restartLevel()
126 {
127     pScene_->setupMap(levelList_.at(currentLevel_));
128 }
129
130 void MainWindow::about()
131 {
132     QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
133                        tr("Version %1"
134                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
135                           "<p>License: General Public License v2"
136                           "<p>Bug Reports: https://bugs.maemo.org/ "
137                           "enter_bug.cgi?product=Ghosts%20Overboard"
138                           ).arg(QApplication::applicationVersion()));
139
140
141
142 }
143
144 void MainWindow::nextLevel()
145 {
146
147     currentLevel_++;
148
149     if (levelList_.empty())
150         pScene_->setupMap(Level());
151
152
153     if ( currentLevel_ < levelList_.size() )
154     {
155         pScene_->setupMap(levelList_.at(currentLevel_));
156     }
157
158     else //Victory!
159     {
160
161        QDialog* pVictoryDialog = new QDialog(this);
162        pVictoryDialog->setWindowTitle(tr("You won!"));
163
164
165        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
166 //       QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
167
168        QPixmap victoryIcon (":/pix/aavesaari.png");
169        QLabel* pVictoryLabel = new QLabel();
170        pVictoryLabel->setPixmap(victoryIcon);
171
172        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
173
174
175        QVBoxLayout* pMainLayout = new QVBoxLayout;
176
177        QHBoxLayout* pTopLayout = new QHBoxLayout;
178        pMainLayout->addLayout(pTopLayout);
179
180        pTopLayout->addWidget(pVictoryLabel);
181        pTopLayout->addWidget(pTextLabel);
182
183
184
185        QHBoxLayout* pButtonLayout = new QHBoxLayout();
186        pMainLayout->addLayout(pButtonLayout);
187
188  //      pButtonLayout->addWidget(pQuitButton);
189        pButtonLayout->addWidget(pPlayAgainButton);
190
191
192
193        pVictoryDialog->setLayout(pMainLayout);
194
195        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
196
197        pVictoryDialog->exec();
198
199         //Never mind if the user cancels the dialog: restart the game anyway
200
201        restartGame();
202     }
203 }
204
205 bool MainWindow::event(QEvent *event)
206 {
207
208     switch (event->type())
209     {
210         //pause if app goes to background
211         case QEvent::WindowDeactivate:
212
213             if (pScene_)
214                 pScene_->pause(true);
215             break;
216
217         //un-pause if app gomes back to foreground unless it was paused before going to background
218         case QEvent::WindowActivate:
219
220
221             if (pPauseAction_ && !pPauseAction_->isChecked())
222             {
223                 if (pScene_)
224                     pScene_->pause(false);
225             }
226             break;
227
228         //Just to keep the compiler from complaining...
229         default:
230             break;
231
232      }
233
234
235
236     //pass the event to the ancestor for handling
237     return QMainWindow::event(event);
238
239  }
240
241 void MainWindow::restartGame()
242 {
243     currentLevel_ = 0;
244     pScene_->setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level, so no need to check if list empty
245
246 }