1 #include "mainwindow.h"
2 #include "timercontrolledtursas.h"
9 #include <QApplication>
11 #include <QPushButton>
12 #include <QVBoxLayout>
16 MainWindow::MainWindow(QWidget *parent)
21 setWindowIcon(QIcon(":/pix/laiva_10aave.png"));
23 pScene_ = new SeaScene ();
24 connect(pScene_,SIGNAL(allGhostsPicked()),this,SLOT(nextLevel()));
26 pView_ = new QGraphicsView ();
28 pView_->setScene(pScene_);
29 setCentralWidget(pView_);
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);
37 QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
38 addAction(pRestartLevelAction);
39 connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
40 menuBar()->addAction(pRestartLevelAction);
43 QAction * pAboutAction = new QAction(tr("About"),this);
44 addAction(pAboutAction);
45 connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
46 menuBar()->addAction(pAboutAction);
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()));
60 MainWindow::~MainWindow()
65 void MainWindow::initializeBoundaries()
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
71 QSize windowsize = pView_->size();
72 QRectF rectangle (topleft,windowsize);
75 pScene_->setSceneRect(rectangle);
76 pView_->setSceneRect(rectangle);
78 qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
83 void MainWindow::pause(bool paused)
85 // qDebug() << "pause pressed " << paused;
86 if (paused_ == paused)
93 // qDebug() << "starting to move again";
94 pTursas_->startMoving();
99 qDebug("about to stop movement");
100 pTursas_->stopMoving();
105 void MainWindow::restartLevel()
107 pScene_->setupMap(5,5,5);
110 void MainWindow::about()
112 QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
114 "<p>Copyright 2011 Heli Hyvättinen"
115 "<p>License: General Public License v2"
116 ).arg(QApplication::applicationVersion()));
122 void MainWindow::nextLevel()
125 //for now, just the handling of last level is implemented, and there is just one level
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."));
131 // victoryBox.setIconPixmap(victoryIcon);
132 // victoryBox.addButton("Start a new game",QMessageBox::YesRole);
133 // victoryBox.addButton("Quit",QMessageBox::NoRole);
135 // victoryBox.exec();
138 QDialog* pVictoryDialog = new QDialog(this);
139 pVictoryDialog->setWindowTitle(tr("You won!"));
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."));
149 QVBoxLayout* pMainLayout = new QVBoxLayout;
151 QHBoxLayout* pTopLayout = new QHBoxLayout;
152 pMainLayout->addLayout(pTopLayout);
154 pTopLayout->addWidget(pVictoryLabel);
155 pTopLayout->addWidget(pTextLabel);
159 QHBoxLayout* pButtonLayout = new QHBoxLayout();
160 pMainLayout->addLayout(pButtonLayout);
163 // pButtonLayout->addWidget(pQuitButton);
164 pButtonLayout->addWidget(pPlayAgainButton);
168 pVictoryDialog->setLayout(pMainLayout);
170 connect(pPlayAgainButton, SIGNAL(clicked()),pVictoryDialog,SLOT(accept()));
172 pVictoryDialog->exec();