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 QGraphicsView ();
48 pView_->setScene(pScene_);
49 setCentralWidget(pView_);
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_);
57 QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
58 addAction(pRestartLevelAction);
59 connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
60 menuBar()->addAction(pRestartLevelAction);
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);
69 QAction * pAboutAction = new QAction(tr("About"),this);
70 addAction(pAboutAction);
71 connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
72 menuBar()->addAction(pAboutAction);
74 QAction * pRestartGameAction = new QAction(tr("Restart game"),this);
75 addAction(pRestartGameAction);
76 connect(pRestartGameAction,SIGNAL(triggered()),this,SLOT(restartGame()));
77 menuBar()->addAction(pRestartGameAction);
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);
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()));
98 MainWindow::~MainWindow()
103 void MainWindow::initializeBoundaries()
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
108 //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
110 // QPoint topleft (0,0);
111 // QSize windowsize = pView_->size();
112 // QRectF rectangle (topleft,windowsize);
114 QRectF rectangle(0,0,800,424);
116 pScene_->setSceneRect(rectangle);
117 pView_->setSceneRect(rectangle);
119 // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
125 void MainWindow::restartLevel()
127 pScene_->setupMap(levelList_.at(currentLevel_));
130 void MainWindow::about()
132 QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
134 "<p>Copyright 2011 Heli Hyvä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()));
144 void MainWindow::nextLevel()
149 if (levelList_.empty())
150 pScene_->setupMap(Level());
153 if ( currentLevel_ < levelList_.size() )
155 pScene_->setupMap(levelList_.at(currentLevel_));
161 QDialog* pVictoryDialog = new QDialog(this);
162 pVictoryDialog->setWindowTitle(tr("You won!"));
165 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
166 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
168 QPixmap victoryIcon (":/pix/aavesaari.png");
169 QLabel* pVictoryLabel = new QLabel();
170 pVictoryLabel->setPixmap(victoryIcon);
172 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
175 QVBoxLayout* pMainLayout = new QVBoxLayout;
177 QHBoxLayout* pTopLayout = new QHBoxLayout;
178 pMainLayout->addLayout(pTopLayout);
180 pTopLayout->addWidget(pVictoryLabel);
181 pTopLayout->addWidget(pTextLabel);
185 QHBoxLayout* pButtonLayout = new QHBoxLayout();
186 pMainLayout->addLayout(pButtonLayout);
188 // pButtonLayout->addWidget(pQuitButton);
189 pButtonLayout->addWidget(pPlayAgainButton);
193 pVictoryDialog->setLayout(pMainLayout);
195 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
197 pVictoryDialog->exec();
199 //Never mind if the user cancels the dialog: restart the game anyway
205 bool MainWindow::event(QEvent *event)
208 switch (event->type())
210 //pause if app goes to background
211 case QEvent::WindowDeactivate:
214 pScene_->pause(true);
217 //un-pause if app gomes back to foreground unless it was paused before going to background
218 case QEvent::WindowActivate:
221 if (pPauseAction_ && !pPauseAction_->isChecked())
224 pScene_->pause(false);
228 //Just to keep the compiler from complaining...
236 //pass the event to the ancestor for handling
237 return QMainWindow::event(event);
241 void MainWindow::restartGame()
244 pScene_->setupMap(levelList_.value(currentLevel_)); //value() returns default constructor Level, so no need to check if list empty