Victory dialog shown and game restarted when all ghosts found
[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     paused_ = false;
20
21     setWindowIcon(QIcon(":/pix/laiva_10aave.png"));
22
23     pScene_ = new SeaScene ();
24     connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
25
26     pView_  = new QGraphicsView ();
27
28     pView_->setScene(pScene_);
29     setCentralWidget(pView_);
30
31     QAction * pPauseAction = new QAction(tr("Pause"),this);
32     pPauseAction->setCheckable(true);
33     addAction(pPauseAction);
34     connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
35     menuBar()->addAction(pPauseAction);
36
37     QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
38     addAction(pRestartLevelAction);
39     connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
40     menuBar()->addAction(pRestartLevelAction);
41
42
43     QAction * pAboutAction = new QAction(tr("About"),this);
44     addAction(pAboutAction);
45     connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
46     menuBar()->addAction(pAboutAction);
47
48
49     //the boundaries of the scene are set to match the size of the view window, which is not
50     //available in the constructor --> timer needed
51     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
52
53
54
55
56
57
58 }
59
60 MainWindow::~MainWindow()
61 {
62
63 }
64
65 void MainWindow::initializeBoundaries()
66 {
67         //the boundaries of the scene are set to match the size of the view window, and
68         //the view is set to show exactly the whole scene area
69
70     QPoint topleft (0,0);
71     QSize windowsize = pView_->size();
72     QRectF rectangle (topleft,windowsize);
73
74
75     pScene_->setSceneRect(rectangle);
76     pView_->setSceneRect(rectangle);
77
78     qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
79
80     restartLevel();
81 }
82
83 void MainWindow::pause(bool paused)
84 {
85 //    qDebug() << "pause pressed " << paused;
86     if (paused_ == paused)
87             return;
88
89     paused_ = paused;
90
91     if (paused == false)
92     {
93  //       qDebug() << "starting to move again";
94         pTursas_->startMoving();
95     }
96
97     else
98     {
99         qDebug("about to stop movement");
100         pTursas_->stopMoving();
101     }
102
103 }
104
105 void MainWindow::restartLevel()
106 {
107     pScene_->setupMap(5,5,5);
108 }
109
110 void MainWindow::about()
111 {
112     QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
113                        tr("Version %1"
114                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
115                           "<p>License: General Public License v2"
116                           ).arg(QApplication::applicationVersion()));
117
118
119
120 }
121
122 void MainWindow::nextLevel()
123 {
124
125     //for now, just the handling of last level is implemented, and there is just one level
126
127     qDebug() << "starting game over";
128        QPixmap victoryIcon (":/pix/aavesaari.png");
129 //    QMessageBox victoryBox(QMessageBox::Information, tr("You won1"), tr("Congratulations! You have saved all the ghosts."));
130
131 //    victoryBox.setIconPixmap(victoryIcon);
132 //    victoryBox.addButton("Start a new game",QMessageBox::YesRole);
133 //    victoryBox.addButton("Quit",QMessageBox::NoRole);
134
135 //    victoryBox.exec();
136
137
138        QDialog* pVictoryDialog = new QDialog(this);
139        pVictoryDialog->setWindowTitle(tr("You won!"));
140
141
142        QPushButton* pPlayAgainButton = new QPushButton(tr("Play again"));
143        QPushButton* pQuitButton = new QPushButton(tr("Quit game"));
144        QLabel* pVictoryLabel = new QLabel();
145        pVictoryLabel->setPixmap(victoryIcon);
146        QLabel* pTextLabel = new QLabel(tr("Congratulations! <p>You have saved all the ghosts."));
147
148
149        QVBoxLayout* pMainLayout = new QVBoxLayout;
150
151        QHBoxLayout* pTopLayout = new QHBoxLayout;
152        pMainLayout->addLayout(pTopLayout);
153
154        pTopLayout->addWidget(pVictoryLabel);
155        pTopLayout->addWidget(pTextLabel);
156
157
158
159        QHBoxLayout* pButtonLayout = new QHBoxLayout();
160        pMainLayout->addLayout(pButtonLayout);
161
162
163  //      pButtonLayout->addWidget(pQuitButton);
164        pButtonLayout->addWidget(pPlayAgainButton);
165
166
167
168        pVictoryDialog->setLayout(pMainLayout);
169
170        connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
171
172        pVictoryDialog->exec();
173
174        restartLevel();
175
176
177
178 }