2 * PlaygroundScene.cpp 07/10/31
4 * Draw playground and likewise stuffs.
6 * Copyright: (c) 2007 CTU Dragons
7 * CTU FEE - Department of Control Engineering
8 * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
13 #include <QGraphicsSceneMouseEvent>
14 #include <QGraphicsScene>
15 #include <QGraphicsRectItem>
17 #include "PlaygroundScene.h"
19 PlaygroundScene::PlaygroundScene(QObject *parent)
20 : QGraphicsScene(parent)
23 QGraphicsRectItem *tempRect;
25 /* All scene units are milimeters */
26 setSceneRect(QRectF(QPointF(-40, -40), QPointF(3040, 2140)));
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));
34 tempRect = addRect(QRect(0, 0, 3000, 2100), QPen(), QBrush(QColor(0, 148, 180)));
35 tempRect->setZValue(1);
37 /* green starting area */
38 tempRect = addRect(QRect(0, 1600, 500, 500), QPen(NoPen), QBrush(green));
39 tempRect->setZValue(3);
41 /* red starting area */
42 tempRect = addRect(QRect(2500, 1600, 500, 500), QPen(NoPen), QBrush(red));
43 tempRect->setZValue(3);
45 QBrush brownBrush = QBrush(QColor(202, 98, 9));
47 /* circular building area */
48 addEllipse(1500-150, 1050-150, 300, 300, QPen(), brownBrush)->setZValue(3);
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);
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);
66 PlaygroundScene::~PlaygroundScene()
70 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
72 QGraphicsScene::mousePressEvent(mouseEvent);
73 QPointF pos = scene2world(mouseEvent->scenePos());
74 obstacle->setPos(mouseEvent->scenePos().x(),
75 mouseEvent->scenePos().y());
80 \fn PlaygroundScene::scene2world(QPointF scenePos)
82 QPointF PlaygroundScene::scene2world(QPointF scenePos)
84 return QPointF((scenePos.x()) / 1000.0,
85 (scenePos.y()) / 1000.0);
90 \fn PlaygroundScene::world2scene(QPointF worldPos)
92 QPointF PlaygroundScene::world2scene(QPointF worldPos)
94 return QPointF(worldPos.x() * 1000,
100 \fn PlaygroundScene::showObstacle(int val)
102 void PlaygroundScene::showObstacle(int val)
105 obstacle->setVisible(true);
107 obstacle->setVisible(false);
110 void PlaygroundScene::initMap()
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]);
123 map->setVisible(false);
128 void PlaygroundScene::showMap(int show)
130 map->setVisible(show);
133 void PlaygroundScene::setMapColor(int x, int y, QColor color)
136 rects[x][y]->setBrush(QBrush(color));