1 /**************************************************************************
2 Ghosts Overboard - a game for Maemo 5
4 Copyright (C) 2011 Heli Hyvättinen
6 This file is part of Ghosts Overboard
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.
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.
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/>.
21 **************************************************************************/
23 #include "mainwindow.h"
29 #include <QMessageBox>
30 #include <QApplication>
32 #include <QPushButton>
33 #include <QVBoxLayout>
37 MainWindow::MainWindow(QWidget *parent)
40 setWindowIcon(QIcon(":/pix/laiva_3aave.png"));
41 setWindowTitle("Ghosts Overboard");
43 pScene_ = new SeaScene ();
44 connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
46 pView_ = new SeaView ();
49 pView_->setScene(pScene_);
50 setCentralWidget(pView_);
52 pPauseAction_ = new QAction(tr("Pause"),this);
53 pPauseAction_->setCheckable(true);
54 addAction(pPauseAction_);
55 connect(pPauseAction_,SIGNAL(triggered(bool)),pScene_,SLOT(pause(bool)));
56 menuBar()->addAction(pPauseAction_);
57 connect(pView_,SIGNAL(pauseChanged()),pPauseAction_,SLOT(toggle()));
59 QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
60 addAction(pRestartLevelAction);
61 connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
62 menuBar()->addAction(pRestartLevelAction);
64 QAction * pRestartGameAction = new QAction(tr("Restart game"),this);
65 addAction(pRestartGameAction);
66 connect(pRestartGameAction,SIGNAL(triggered()),this,SLOT(restartGame()));
67 menuBar()->addAction(pRestartGameAction);
69 pVibrateAction_ = new QAction(tr("Vibration effects"),this);
70 pVibrateAction_->setCheckable(true);
71 addAction(pVibrateAction_);
72 connect(pVibrateAction_,SIGNAL(triggered(bool)),pScene_,SLOT(vibrationActivate(bool)));
73 menuBar()->addAction(pVibrateAction_);
76 QAction * pAboutAction = new QAction(tr("About"),this);
77 addAction(pAboutAction);
78 connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
79 menuBar()->addAction(pAboutAction);
84 levelList_.append(level1);
85 Level level2(5,10,2,50);
86 levelList_.append(level2);
87 Level level3(5,15,2,50);
88 levelList_.append(level3);
89 Level level4(5,15,4,50);
90 levelList_.append(level4);
91 Level level5(5,15,5,100);
92 levelList_.append(level5);
97 //the boundaries of the scene are set to match the size of the view window, which is not
98 //available in the constructor --> timer needed
99 QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
102 MainWindow::~MainWindow()
107 void MainWindow::initializeBoundaries()
109 //the boundaries of the scene are set to match the size of the view window, and
110 //the view is set to show exactly the whole scene area
112 //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
114 // QPoint topleft (0,0);
115 // QSize windowsize = pView_->size();
116 // QRectF rectangle (topleft,windowsize);
118 QRectF rectangle(0,0,800,424);
120 pScene_->setSceneRect(rectangle);
121 pView_->setSceneRect(rectangle);
123 // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
129 void MainWindow::restartLevel()
131 pScene_->setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level if index is invalid, so no risk of crash
132 pScene_->vibrationActivate(pVibrateAction_->isChecked()); //Vibration effects are lost without this
133 // qDebug() << pVibrateAction_->isChecked();
136 void MainWindow::about()
138 QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
140 "<p>Copyright 2011 Heli Hyvättinen"
141 "<p>License: General Public License v2"
142 "<p>Bug Reports: https://bugs.maemo.org/ "
143 "enter_bug.cgi?product=Ghosts%20Overboard"
144 ).arg(QApplication::applicationVersion()));
150 void MainWindow::nextLevel()
155 if (levelList_.empty())
156 pScene_->setupMap(Level());
159 if ( currentLevel_ < levelList_.size() )
167 QDialog* pVictoryDialog = new QDialog(this);
168 pVictoryDialog->setWindowTitle(tr("You won!"));
171 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
172 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
174 QPixmap victoryIcon (":/pix/aavesaari.png");
175 QLabel* pVictoryLabel = new QLabel();
176 pVictoryLabel->setPixmap(victoryIcon);
178 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
181 QVBoxLayout* pMainLayout = new QVBoxLayout;
183 QHBoxLayout* pTopLayout = new QHBoxLayout;
184 pMainLayout->addLayout(pTopLayout);
186 pTopLayout->addWidget(pVictoryLabel);
187 pTopLayout->addWidget(pTextLabel);
191 QHBoxLayout* pButtonLayout = new QHBoxLayout();
192 pMainLayout->addLayout(pButtonLayout);
194 // pButtonLayout->addWidget(pQuitButton);
195 pButtonLayout->addWidget(pPlayAgainButton);
199 pVictoryDialog->setLayout(pMainLayout);
201 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
203 pVictoryDialog->exec();
205 //Never mind if the user cancels the dialog: restart the game anyway
211 bool MainWindow::event(QEvent *event)
214 switch (event->type())
216 //pause if app goes to background
217 case QEvent::WindowDeactivate:
220 pScene_->pause(true);
223 //un-pause if app gomes back to foreground unless it was paused before going to background
224 case QEvent::WindowActivate:
227 if (pPauseAction_ && !pPauseAction_->isChecked())
230 pScene_->pause(false);
234 //Just to keep the compiler from complaining...
242 //pass the event to the ancestor for handling
243 return QMainWindow::event(event);
247 void MainWindow::restartGame()