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