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);
63 QAction * pAboutAction = new QAction(tr("About"),this);
64 addAction(pAboutAction);
65 connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
66 menuBar()->addAction(pAboutAction);
69 QAction * 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 //the boundaries of the scene are set to match the size of the view window, which is not
77 //available in the constructor --> timer needed
78 QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
81 MainWindow::~MainWindow()
86 void MainWindow::initializeBoundaries()
88 //the boundaries of the scene are set to match the size of the view window, and
89 //the view is set to show exactly the whole scene area
91 //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
93 // QPoint topleft (0,0);
94 // QSize windowsize = pView_->size();
95 // QRectF rectangle (topleft,windowsize);
97 QRectF rectangle(0,0,800,424);
99 pScene_->setSceneRect(rectangle);
100 pView_->setSceneRect(rectangle);
102 // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
108 void MainWindow::restartLevel()
110 pScene_->setupMap(5,10,5);
113 void MainWindow::about()
115 QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
117 "<p>Copyright 2011 Heli Hyvättinen"
118 "<p>License: General Public License v2"
119 "<p>Bug Reports: https://bugs.maemo.org/ "
120 "enter_bug.cgi?product=Ghosts%20Overboard"
121 ).arg(QApplication::applicationVersion()));
127 void MainWindow::nextLevel()
130 //for now, just the handling of last level is implemented, and there is just one level
134 QDialog* pVictoryDialog = new QDialog(this);
135 pVictoryDialog->setWindowTitle(tr("You won!"));
138 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
139 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
141 QPixmap victoryIcon (":/pix/aavesaari.png");
142 QLabel* pVictoryLabel = new QLabel();
143 pVictoryLabel->setPixmap(victoryIcon);
145 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
148 QVBoxLayout* pMainLayout = new QVBoxLayout;
150 QHBoxLayout* pTopLayout = new QHBoxLayout;
151 pMainLayout->addLayout(pTopLayout);
153 pTopLayout->addWidget(pVictoryLabel);
154 pTopLayout->addWidget(pTextLabel);
158 QHBoxLayout* pButtonLayout = new QHBoxLayout();
159 pMainLayout->addLayout(pButtonLayout);
161 // pButtonLayout->addWidget(pQuitButton);
162 pButtonLayout->addWidget(pPlayAgainButton);
166 pVictoryDialog->setLayout(pMainLayout);
168 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
170 pVictoryDialog->exec();
172 //Never mind if the user cancels the dialog: restart the game anyway
178 bool MainWindow::event(QEvent *event)
181 switch (event->type())
183 //pause if app goes to background
184 case QEvent::WindowDeactivate:
187 pScene_->pause(true);
190 //un-pause if app gomes back to foreground unless it was paused before going to background
191 case QEvent::WindowActivate:
194 if (pPauseAction_ && !pPauseAction_->isChecked())
197 pScene_->pause(false);
201 //Just to keep the compiler from complaining...
209 //pass the event to the ancestor for handling
210 return QMainWindow::event(event);