SeaScene with randomized ghost positions
[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
9
10
11 MainWindow::MainWindow(QWidget *parent)
12     : QMainWindow(parent)
13 {
14     paused_ = false;
15
16     pScene_ = new SeaScene ();
17     pView_  = new QGraphicsView ();
18
19     QPixmap waves (":/pix/meri.png");
20     pScene_->setBackgroundBrush(QBrush(waves));
21
22     pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/laiva.png"));
23     pScene_->addItem(pTursas_);
24
25     pView_->setScene(pScene_);
26     setCentralWidget(pView_);
27
28
29     //the boundaries of the scene are set to match the size of the view window, which is not
30     //available in the constructor --> timer needed
31     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
32
33
34     QAction * pPauseAction = new QAction(tr("Pause"),this);
35     pPauseAction->setCheckable(true);
36     addAction(pPauseAction);
37     connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
38     menuBar()->addAction(pPauseAction);
39
40 //    QGraphicsPixmapItem * pGhost = pScene_->addPixmap(QPixmap(":/pix/aave.png"));
41 //    pGhost->setData(0,"ghost");
42     QGraphicsPixmapItem * pRock =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
43         QGraphicsPixmapItem * pRock2 =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
44     pRock->moveBy(40,40);
45     pRock->setData(0,"rock");
46     pRock2->moveBy(80,80);
47     pRock2->setData(0,"rock");
48
49     TimerControlledTursas * pMustekala = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
50     pScene_->addItem(pMustekala);
51     pMustekala->moveBy(100,100);
52     pMustekala->startMoving();
53
54
55
56
57 }
58
59 MainWindow::~MainWindow()
60 {
61
62 }
63
64 void MainWindow::initializeBoundaries()
65 {
66         //the boundaries of the scene are set to match the size of the view window, and
67         //the view is set to show exactly the whole scene area
68
69     QPoint topleft (0,0);
70     QSize windowsize = pView_->size();
71     QRectF rectangle (topleft,windowsize);
72
73
74     pScene_->setSceneRect(rectangle);
75     pView_->setSceneRect(rectangle);
76     pTursas_->setBoundaries(rectangle);
77     pTursas_->startMoving();
78
79     qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width();
80
81     pScene_->setupMap(5,5,5);
82 }
83
84 void MainWindow::pause(bool paused)
85 {
86 //    qDebug() << "pause pressed " << paused;
87     if (paused_ == paused)
88             return;
89
90     paused_ = paused;
91
92     if (paused == false)
93     {
94  //       qDebug() << "starting to move again";
95         pTursas_->startMoving();
96     }
97
98     else
99     {
100         qDebug("about to stop movement");
101         pTursas_->stopMoving();
102     }
103 }