Added author, copyright and license information to source files.
[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 "timercontrolledtursas.h"
25 #include <QPixmap>
26 #include <QTimer>
27 #include <QDebug>
28 #include <QAction>
29 #include <QMenuBar>
30 #include <QMessageBox>
31 #include <QApplication>
32 #include <QLabel>
33 #include <QPushButton>
34 #include <QVBoxLayout>
35
36
37
38 MainWindow::MainWindow(QWidget *parent)
39     : QMainWindow(parent)
40 {
41     setWindowIcon(QIcon(":/pix/laiva_3aave.png"));
42     setWindowTitle("Ghosts Overboard");
43
44     pScene_ = new SeaScene ();
45     connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
46
47     pView_  = new QGraphicsView ();
48
49     pView_->setScene(pScene_);
50     setCentralWidget(pView_);
51
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
58     QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
59     addAction(pRestartLevelAction);
60     connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
61     menuBar()->addAction(pRestartLevelAction);
62
63
64     QAction * pAboutAction = new QAction(tr("About"),this);
65     addAction(pAboutAction);
66     connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
67     menuBar()->addAction(pAboutAction);
68
69
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()));
73 }
74
75 MainWindow::~MainWindow()
76 {
77
78 }
79
80 void MainWindow::initializeBoundaries()
81 {
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
84
85     //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
86
87 //    QPoint topleft (0,0);
88 //    QSize windowsize = pView_->size();
89 //    QRectF rectangle (topleft,windowsize);
90
91     QRectF rectangle(0,0,800,424);
92
93     pScene_->setSceneRect(rectangle);
94     pView_->setSceneRect(rectangle);
95
96     // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
97
98     restartLevel();
99 }
100
101
102 void MainWindow::restartLevel()
103 {
104     pScene_->setupMap(5,10,5);
105 }
106
107 void MainWindow::about()
108 {
109     QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
110                        tr("Version %1"
111                           "<p>Copyright 2011 Heli Hyv&auml;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()));
116
117
118
119 }
120
121 void MainWindow::nextLevel()
122 {
123
124     //for now, just the handling of last level is implemented, and there is just one level
125
126
127
128        QDialog* pVictoryDialog = new QDialog(this);
129        pVictoryDialog->setWindowTitle(tr("You won!"));
130
131
132        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
133 //       QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
134
135        QPixmap victoryIcon (":/pix/aavesaari.png");
136        QLabel* pVictoryLabel = new QLabel();
137        pVictoryLabel->setPixmap(victoryIcon);
138
139        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
140
141
142        QVBoxLayout* pMainLayout = new QVBoxLayout;
143
144        QHBoxLayout* pTopLayout = new QHBoxLayout;
145        pMainLayout->addLayout(pTopLayout);
146
147        pTopLayout->addWidget(pVictoryLabel);
148        pTopLayout->addWidget(pTextLabel);
149
150
151
152        QHBoxLayout* pButtonLayout = new QHBoxLayout();
153        pMainLayout->addLayout(pButtonLayout);
154
155  //      pButtonLayout->addWidget(pQuitButton);
156        pButtonLayout->addWidget(pPlayAgainButton);
157
158
159
160        pVictoryDialog->setLayout(pMainLayout);
161
162        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
163
164        pVictoryDialog->exec();
165
166         //Never mind if the user cancels the dialog: restart the game anyway
167
168        restartLevel();
169
170 }
171
172 bool MainWindow::event(QEvent *event)
173 {
174
175     switch (event->type())
176     {
177         //pause if app goes to background
178         case QEvent::WindowDeactivate:
179
180             if (pScene_)
181                 pScene_->pause(true);
182             break;
183
184         //un-pause if app gomes back to foreground unless it was paused before going to background
185         case QEvent::WindowActivate:
186
187
188             if (pPauseAction_ && !pPauseAction_->isChecked())
189             {
190                 if (pScene_)
191                     pScene_->pause(false);
192             }
193             break;
194
195         //Just to keep the compiler from complaining...
196         default:
197             break;
198
199      }
200
201
202
203     //pass the event to the ancestor for handling
204     return QMainWindow::event(event);
205
206  }