Keeps screen lit and pauses when app is backgrounded
[ghostsoverboard] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "timercontrolledtursas.h"
3 #include <QPixmap>
4 #include <QTimer>
5 #include <QDebug>
6 #include <QAction>
7 #include <QMenuBar>
8 #include <QMessageBox>
9 #include <QApplication>
10 #include <QLabel>
11 #include <QPushButton>
12 #include <QVBoxLayout>
13
14
15
16 MainWindow::MainWindow(QWidget *parent)
17     : QMainWindow(parent)
18 {
19     setWindowIcon(QIcon(":/pix/laiva_10aave.png"));
20     setWindowTitle("Ghosts Overboard");
21
22     pScene_ = new SeaScene ();
23     connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
24
25     pView_  = new QGraphicsView ();
26
27     pView_->setScene(pScene_);
28     setCentralWidget(pView_);
29
30     pPauseAction_ = new QAction(tr("Pause"),this);
31     pPauseAction_->setCheckable(true);
32     addAction(pPauseAction_);
33     connect(pPauseAction_,SIGNAL(triggered(bool)),pScene_,SLOT(pause(bool)));
34     menuBar()->addAction(pPauseAction_);
35
36     QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
37     addAction(pRestartLevelAction);
38     connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
39     menuBar()->addAction(pRestartLevelAction);
40
41
42     QAction * pAboutAction = new QAction(tr("About"),this);
43     addAction(pAboutAction);
44     connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
45     menuBar()->addAction(pAboutAction);
46
47
48     //the boundaries of the scene are set to match the size of the view window, which is not
49     //available in the constructor --> timer needed
50     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
51 }
52
53 MainWindow::~MainWindow()
54 {
55
56 }
57
58 void MainWindow::initializeBoundaries()
59 {
60         //the boundaries of the scene are set to match the size of the view window, and
61         //the view is set to show exactly the whole scene area
62
63     //this occasionally gives a tiny scene, so using a fixed size fit for N900/Maemo5 until a fix is found
64
65 //    QPoint topleft (0,0);
66 //    QSize windowsize = pView_->size();
67 //    QRectF rectangle (topleft,windowsize);
68
69     QRectF rectangle(0,0,800,424);
70
71     pScene_->setSceneRect(rectangle);
72     pView_->setSceneRect(rectangle);
73
74     // qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
75
76     restartLevel();
77 }
78
79
80 void MainWindow::restartLevel()
81 {
82     pScene_->setupMap(5,10,0);
83 }
84
85 void MainWindow::about()
86 {
87     QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
88                        tr("Version %1"
89                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
90                           "<p>License: General Public License v2"
91                           "<p>Bug Reports: https://bugs.maemo.org/ "
92                           "enter_bug.cgi?product=Ghosts%20Overboard"
93                           ).arg(QApplication::applicationVersion()));
94
95
96
97 }
98
99 void MainWindow::nextLevel()
100 {
101
102     //for now, just the handling of last level is implemented, and there is just one level
103
104
105
106        QDialog* pVictoryDialog = new QDialog(this);
107        pVictoryDialog->setWindowTitle(tr("You won!"));
108
109
110        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
111 //       QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
112
113        QPixmap victoryIcon (":/pix/aavesaari.png");
114        QLabel* pVictoryLabel = new QLabel();
115        pVictoryLabel->setPixmap(victoryIcon);
116
117        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
118
119
120        QVBoxLayout* pMainLayout = new QVBoxLayout;
121
122        QHBoxLayout* pTopLayout = new QHBoxLayout;
123        pMainLayout->addLayout(pTopLayout);
124
125        pTopLayout->addWidget(pVictoryLabel);
126        pTopLayout->addWidget(pTextLabel);
127
128
129
130        QHBoxLayout* pButtonLayout = new QHBoxLayout();
131        pMainLayout->addLayout(pButtonLayout);
132
133  //      pButtonLayout->addWidget(pQuitButton);
134        pButtonLayout->addWidget(pPlayAgainButton);
135
136
137
138        pVictoryDialog->setLayout(pMainLayout);
139
140        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
141
142        pVictoryDialog->exec();
143
144         //Never mind if the user cancels the dialog: restart the game anyway
145
146        restartLevel();
147
148 }
149
150 bool MainWindow::event(QEvent *event)
151 {
152
153     switch (event->type())
154     {
155         //pause if app goes to background
156         case QEvent::WindowDeactivate:
157
158             if (pScene_)
159                 pScene_->pause(true);
160             break;
161
162         //un-pause if app gomes back to foreground unless it was paused before going to background
163         case QEvent::WindowActivate:
164
165
166             if (pPauseAction_ && !pPauseAction_->isChecked())
167             {
168                 if (pScene_)
169                     pScene_->pause(false);
170             }
171             break;
172
173         //Just to keep the compiler from complaining...
174         default:
175             break;
176
177      }
178
179
180
181     //pass the event to the ancestor for handling
182     return QMainWindow::event(event);
183
184  }