Merge branch 'shipimages'
[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     QPixmap waves (":/pix/meri.png");
24     pScene_->setBackgroundBrush(QBrush(waves));
25
26     pView_->setScene(pScene_);
27     setCentralWidget(pView_);
28
29     QAction * pPauseAction = new QAction(tr("Pause"),this);
30     pPauseAction->setCheckable(true);
31     addAction(pPauseAction);
32     connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
33     menuBar()->addAction(pPauseAction);
34
35     QAction * pRestartLevelAction = new QAction(tr("Restart level"),this);
36     addAction(pRestartLevelAction);
37     connect(pRestartLevelAction,SIGNAL(triggered()),this,SLOT(restartLevel()));
38     menuBar()->addAction(pRestartLevelAction);
39
40
41     QAction * pAboutAction = new QAction(tr("About"),this);
42     addAction(pAboutAction);
43     connect(pAboutAction,SIGNAL(triggered()),this,SLOT(about()));
44     menuBar()->addAction(pAboutAction);
45
46
47     //the boundaries of the scene are set to match the size of the view window, which is not
48     //available in the constructor --> timer needed
49     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
50
51
52
53
54
55
56 }
57
58 MainWindow::~MainWindow()
59 {
60
61 }
62
63 void MainWindow::initializeBoundaries()
64 {
65         //the boundaries of the scene are set to match the size of the view window, and
66         //the view is set to show exactly the whole scene area
67
68     QPoint topleft (0,0);
69     QSize windowsize = pView_->size();
70     QRectF rectangle (topleft,windowsize);
71
72
73     pScene_->setSceneRect(rectangle);
74     pView_->setSceneRect(rectangle);
75
76     qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width();
77
78     pScene_->setupMap(11,5,5);
79 }
80
81 void MainWindow::pause(bool paused)
82 {
83 //    qDebug() << "pause pressed " << paused;
84     if (paused_ == paused)
85             return;
86
87     paused_ = paused;
88
89     if (paused == false)
90     {
91  //       qDebug() << "starting to move again";
92         pTursas_->startMoving();
93     }
94
95     else
96     {
97         qDebug("about to stop movement");
98         pTursas_->stopMoving();
99     }
100
101 }
102
103 void MainWindow::restartLevel()
104 {
105     pScene_->setupMap(5,5,5);
106 }
107
108 void MainWindow::about()
109 {
110     QMessageBox::about(this, tr("About %1").arg(QApplication::applicationName()),
111                        tr("Version %1"
112                           "<p>Copyright 2011 Heli Hyv&auml;ttinen"
113                           "<p>License: General Public License v2"
114                           ).arg(QApplication::applicationVersion()));
115
116
117
118 }