Pause now works
[ghostsoverboard] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <QPixmap>
3 #include <QTimer>
4 #include <QDebug>
5 #include <QAction>
6 #include <QMenuBar>
7
8
9
10 MainWindow::MainWindow(QWidget *parent)
11     : QMainWindow(parent)
12 {
13     paused_ = false;
14
15     pScene_ = new QGraphicsScene ();
16     pView_  = new QGraphicsView ();
17
18     QPixmap waves (":/pix/meri.png");
19     pScene_->setBackgroundBrush(QBrush(waves));
20
21     pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/tursas.png"));
22     pScene_->addItem(pTursas_);
23
24     pView_->setScene(pScene_);
25     setCentralWidget(pView_);
26
27
28     //the boundaries of the scene are set to match the size of the view window, which is not
29     //available in the constructor --> timer needed
30     QTimer::singleShot(60,this,SLOT(initializeBoundaries()));
31
32
33     QAction * pPauseAction = new QAction(tr("Pause"),this);
34     pPauseAction->setCheckable(true);
35     addAction(pPauseAction);
36     connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
37     menuBar()->addAction(pPauseAction);
38
39 }
40
41 MainWindow::~MainWindow()
42 {
43
44 }
45
46 void MainWindow::initializeBoundaries()
47 {
48         //the boundaries of the scene are set to match the size of the view window, and
49         //the view is set to show exactly the whole scene area
50
51     QPoint topleft (0,0);
52     QSize windowsize = pView_->size();
53     QRectF rectangle (topleft,windowsize);
54
55
56     pScene_->setSceneRect(rectangle);
57     pView_->setSceneRect(rectangle);
58     pTursas_->setBoundaries(rectangle);
59     pTursas_->startMoving();
60
61     qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width();
62 }
63
64 void MainWindow::pause(bool paused)
65 {
66 //    qDebug() << "pause pressed " << paused;
67     if (paused_ == paused)
68             return;
69
70     paused_ = paused;
71
72     if (paused == false)
73     {
74  //       qDebug() << "starting to move again";
75         pTursas_->startMoving();
76     }
77
78     else
79     {
80         qDebug("about to stop movement");
81         pTursas_->stopMoving();
82     }
83 }