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