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"
24 #include "timercontrolledtursas.h"
30 #include <QMessageBox>
31 #include <QApplication>
33 #include <QPushButton>
34 #include <QVBoxLayout>
38 MainWindow::MainWindow(QWidget *parent)
41 setWindowIcon(QIcon(":/pix/laiva_3aave.png"));
42 setWindowTitle("Ghosts Overboard");
44 pScene_ = new SeaScene ();
45 connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
47 pView_ = new QGraphicsView ();
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_);
58 QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
59 addAction(pRestartLevelAction);
60 connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
61 menuBar()->addAction(pRestartLevelAction);
64 QAction * pAboutAction = new QAction(tr("About"),this);
65 addAction(pAboutAction);
66 connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
67 menuBar()->addAction(pAboutAction);
70 //the boundaries of the scene are set to match the size of the view window, which is not
71 //available in the constructor --> timer needed
72 QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
75 MainWindow::~MainWindow()
80 void MainWindow::initializeBoundaries()
82 //the boundaries of the scene are set to match the size of the view window, and
83 //the view is set to show exactly the whole scene area
85 //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
87 // QPoint topleft (0,0);
88 // QSize windowsize = pView_->size();
89 // QRectF rectangle (topleft,windowsize);
91 QRectF rectangle(0,0,800,424);
93 pScene_->setSceneRect(rectangle);
94 pView_->setSceneRect(rectangle);
96 // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
102 void MainWindow::restartLevel()
104 pScene_->setupMap(5,10,5);
107 void MainWindow::about()
109 QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
111 "<p>Copyright 2011 Heli Hyvättinen"
112 "<p>License: General Public License v2"
113 "<p>Bug Reports: https://bugs.maemo.org/ "
114 "enter_bug.cgi?product=Ghosts%20Overboard"
115 ).arg(QApplication::applicationVersion()));
121 void MainWindow::nextLevel()
124 //for now, just the handling of last level is implemented, and there is just one level
128 QDialog* pVictoryDialog = new QDialog(this);
129 pVictoryDialog->setWindowTitle(tr("You won!"));
132 QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
133 // QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
135 QPixmap victoryIcon (":/pix/aavesaari.png");
136 QLabel* pVictoryLabel = new QLabel();
137 pVictoryLabel->setPixmap(victoryIcon);
139 QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
142 QVBoxLayout* pMainLayout = new QVBoxLayout;
144 QHBoxLayout* pTopLayout = new QHBoxLayout;
145 pMainLayout->addLayout(pTopLayout);
147 pTopLayout->addWidget(pVictoryLabel);
148 pTopLayout->addWidget(pTextLabel);
152 QHBoxLayout* pButtonLayout = new QHBoxLayout();
153 pMainLayout->addLayout(pButtonLayout);
155 // pButtonLayout->addWidget(pQuitButton);
156 pButtonLayout->addWidget(pPlayAgainButton);
160 pVictoryDialog->setLayout(pMainLayout);
162 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
164 pVictoryDialog->exec();
166 //Never mind if the user cancels the dialog: restart the game anyway
172 bool MainWindow::event(QEvent *event)
175 switch (event->type())
177 //pause if app goes to background
178 case QEvent::WindowDeactivate:
181 pScene_->pause(true);
184 //un-pause if app gomes back to foreground unless it was paused before going to background
185 case QEvent::WindowActivate:
188 if (pPauseAction_ && !pPauseAction_->isChecked())
191 pScene_->pause(false);
195 //Just to keep the compiler from complaining...
203 //pass the event to the ancestor for handling
204 return QMainWindow::event(event);