]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/PlaygroundScene.cpp
268fd72ef93e292d07957c9db78abe54fd111abf
[eurobot/public.git] / src / robomon / PlaygroundScene.cpp
1 /*
2  * PlaygroundScene.cpp                  07/10/31
3  *
4  * Draw playground and likewise stuffs.
5  *
6  * Copyright: (c) 2007 CTU Dragons
7  *            CTU FEE - Department of Control Engineering
8  * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
9  * License: GNU GPL v.2
10  */
11
12 #include <QPointF>
13 #include <QGraphicsSceneMouseEvent>
14 #include <QGraphicsScene>
15 #include <QGraphicsRectItem>
16
17 #include "PlaygroundScene.h"
18
19 PlaygroundScene::PlaygroundScene(QObject *parent)
20         : QGraphicsScene(parent)
21 {
22         using namespace Qt;
23         QGraphicsRectItem *tempRect;
24         
25         /* All scene units are milimeters */
26         setSceneRect(QRectF(QPointF(-40, -40), QPointF(3040, 2140)));
27         
28         /* playground border */
29         addRect(QRect(0, 0, -22, 2100), QPen(), QBrush(white));
30         addRect(QRect(-22, 2100, 3000+2*22, 22), QPen(), QBrush(white));
31         addRect(QRect(3000, 0, 22, 2100), QPen(), QBrush(white));
32         
33         /* playground */
34         tempRect = addRect(QRect(0, 0, 3000, 2100), QPen(), QBrush(QColor(0, 148, 180)));
35         tempRect->setZValue(1);
36         
37         /* green starting area */
38         tempRect = addRect(QRect(0, 1600, 500, 500), QPen(NoPen), QBrush(green));
39         tempRect->setZValue(3);
40         
41         /* red starting area */
42         tempRect = addRect(QRect(2500, 1600, 500, 500), QPen(NoPen), QBrush(red));
43         tempRect->setZValue(3);
44         
45         QBrush brownBrush = QBrush(QColor(202, 98, 9));
46         
47         /* circular building area */
48         addEllipse(1500-150, 1050-150, 300, 300, QPen(), brownBrush)->setZValue(3);
49         
50         /* side building areas */
51         addRect(QRect(600, 0, 600, 100), QPen(),  brownBrush)->setZValue(3);
52         addRect(QRect(1200, 0, 600, 100), QPen(),  brownBrush)->setZValue(3);
53         addRect(QRect(1800, 0, 600, 100), QPen(),  brownBrush)->setZValue(3);
54         
55         obstacle = new QGraphicsEllipseItem(0, 0, 300, 300);
56         obstacle->translate(-150,-150);
57         obstacle->setZValue(5);
58         obstacle->setBrush(QBrush(QColor(0, 0, 255, 200)));
59         obstacle->setVisible(false);
60         obstacle->setPos(QPointF(2000, 1000));
61         this->addItem(obstacle);
62         
63         initMap();
64 }
65
66 PlaygroundScene::~PlaygroundScene()
67 {
68 }
69
70 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
71 {
72         QGraphicsScene::mousePressEvent(mouseEvent);
73         QPointF pos = scene2world(mouseEvent->scenePos());
74         obstacle->setPos(mouseEvent->scenePos().x(),
75                          mouseEvent->scenePos().y());
76         obstacleChanged(pos);
77 }
78
79 /*!
80     \fn PlaygroundScene::scene2world(QPointF scenePos)
81  */
82 QPointF PlaygroundScene::scene2world(QPointF scenePos)
83 {
84         return QPointF((scenePos.x()) / 1000.0,
85                         (scenePos.y()) / 1000.0);
86 }
87
88
89 /*!
90     \fn PlaygroundScene::world2scene(QPointF worldPos)
91  */
92 QPointF PlaygroundScene::world2scene(QPointF worldPos)
93 {
94         return QPointF(worldPos.x() * 1000,
95                        worldPos.y() * 1000);
96 }
97
98
99 /*!
100     \fn PlaygroundScene::showObstacle(int val)
101  */
102 void PlaygroundScene::showObstacle(int val)
103 {
104         if (val)
105                 obstacle->setVisible(true);
106         else
107                 obstacle->setVisible(false);
108 }
109
110 void PlaygroundScene::initMap()
111 {
112         this->map = new QGraphicsItemGroup();
113         for(int i=0; i < MAP_WIDTH; i++) {
114                 for(int j=0; j<MAP_HEIGHT; j++) {
115                         rects[i][j] = new QGraphicsRectItem(
116                                         QRectF(i*MAP_CELL_SIZE_MM, (MAP_HEIGHT-1-j)*MAP_CELL_SIZE_MM,
117                                                MAP_CELL_SIZE_MM,MAP_CELL_SIZE_MM));
118                         rects[i][j]->setPen(QPen());
119                         rects[i][j]->setBrush(QBrush(Qt::lightGray));
120                         map->addToGroup(rects[i][j]);
121                 }
122         }
123         map->setVisible(false);
124         map->setZValue(4);
125         addItem(map);
126 }
127
128 void PlaygroundScene::showMap(int show)
129 {
130         map->setVisible(show);
131 }
132
133 void PlaygroundScene::setMapColor(int x, int y, QColor color)
134 {
135         color.setAlpha(200);
136         rects[x][y]->setBrush(QBrush(color));
137 }