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
9 * Michal Vokac, Petr Kubiznak
10 * License: GNU GPL v.2
14 #include <QGraphicsSceneMouseEvent>
15 #include <QGraphicsScene>
16 #include <QGraphicsRectItem>
23 #include "PlaygroundScene.h"
26 #include "../robofsm/corns_configs.h"
28 //margin around borders (just for better look)
32 #define BORDER_WIDTH 10
33 #define BORDER_COLOR black
36 #define PLAYGROUND_WIDTH PLAYGROUND_WIDTH_MM
37 #define PLAYGROUND_HEIGHT PLAYGROUND_HEIGHT_MM
39 PlaygroundScene::PlaygroundScene(QObject *parent)
40 : QGraphicsScene(parent)
43 QGraphicsRectItem *playgroundRect;
45 /* All scene units are milimeters */
46 setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
48 /* playground border */
49 addRect(QRect(0, 0, -BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR)); //left
50 addRect(QRect(-BORDER_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR)); //top
51 addRect(QRect(PLAYGROUND_WIDTH, 0, BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR)); //right
52 addRect(QRect(-BORDER_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR)); //bottom
55 playgroundRect = addRect(
56 QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT),
58 QPixmap(":/images/playground_eurobot2012_1024.png").scaled(PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT)
60 playgroundRect->setZValue(0);
63 obstacle = new QGraphicsEllipseItem(0, 0, SIM_OBST_SIZE_M*1000, SIM_OBST_SIZE_M*1000);
64 obstacle->translate(-SIM_OBST_SIZE_M*1000/2,-SIM_OBST_SIZE_M*1000/2);
65 obstacle->setZValue(5);
66 obstacle->setBrush(QBrush(QColor(0, 0, 255, 200)));
67 obstacle->setVisible(false);
68 obstacle->setPos(QPointF(2000, 1000));
69 this->addItem(obstacle);
72 PlaygroundScene::~PlaygroundScene()
76 void PlaygroundScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
78 QPointF pos = scene2world(mouseEvent->scenePos());
80 QGraphicsScene::mouseMoveEvent(mouseEvent);
83 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
85 QGraphicsScene::mousePressEvent(mouseEvent);
86 QPointF pos = scene2world(mouseEvent->scenePos());
87 obstacle->setPos(mouseEvent->scenePos().x(),
88 mouseEvent->scenePos().y());
89 emit obstacleChanged(pos);
93 \fn PlaygroundScene::scene2world(QPointF scenePos)
95 QPointF PlaygroundScene::scene2world(QPointF scenePos)
97 return QPointF((scenePos.x()) / 1000.0,
98 (scenePos.y()) / 1000.0);
103 \fn PlaygroundScene::world2scene(QPointF worldPos)
105 QPointF PlaygroundScene::world2scene(QPointF worldPos)
107 return QPointF(worldPos.x() * 1000,
108 worldPos.y() * 1000);
113 \fn PlaygroundScene::showObstacle(int val)
115 void PlaygroundScene::showObstacle(int val)
118 obstacle->setVisible(true);
120 obstacle->setVisible(false);