Merge branch 'randomize'
[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
11
12
13 MainWindow::MainWindow(QWidget *parent)
14     : QMainWindow(parent)
15 {
16     paused_ = false;
17
18     setWindowIcon(QIcon(":/pix/laiva_10aave.png"));
19
20     pScene_ = new SeaScene ();
21     pView_  = new QGraphicsView ();
22
23
24
25     pView_->setScene(pScene_);
26     setCentralWidget(pView_);
27
28     QAction * pPauseAction = new QAction(tr("Pause"),this);
29     pPauseAction->setCheckable(true);
30     addAction(pPauseAction);
31     connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
32     menuBar()->addAction(pPauseAction);
33
34     QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
35     addAction(pRestartLevelAction);
36     connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
37     menuBar()->addAction(pRestartLevelAction);
38
39
40     QAction * pAboutAction = new QAction(tr("About"),this);
41     addAction(pAboutAction);
42     connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
43     menuBar()->addAction(pAboutAction);
44
45
46     //the boundaries of the scene are set to match the size of the view window, which is not
47     //available in the constructor --> timer needed
48     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
49
50
51
52
53
54
55 }
56
57 MainWindow::~MainWindow()
58 {
59
60 }
61
62 void MainWindow::initializeBoundaries()
63 {
64         //the boundaries of the scene are set to match the size of the view window, and
65         //the view is set to show exactly the whole scene area
66
67     QPoint topleft (0,0);
68     QSize windowsize = pView_->size();
69     QRectF rectangle (topleft,windowsize);
70
71
72     pScene_->setSceneRect(rectangle);
73     pView_->setSceneRect(rectangle);
74
75     qDebug() << "Initialized boundaries" << rectangle.right() << rectangle.bottom() << pView_->width() << pView_->height();
76
77     pScene_->setupMap(11,5,5);
78 }
79
80 void MainWindow::pause(bool paused)
81 {
82 //    qDebug() << "pause pressed " << paused;
83     if (paused_ == paused)
84             return;
85
86     paused_ = paused;
87
88     if (paused == false)
89     {
90  //       qDebug() << "starting to move again";
91         pTursas_->startMoving();
92     }
93
94     else
95     {
96         qDebug("about to stop movement");
97         pTursas_->stopMoving();
98     }
99
100 }
101
102 void MainWindow::restartLevel()
103 {
104     pScene_->setupMap(5,5,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                           ).arg(QApplication::applicationVersion()));
114
115
116
117 }