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"
27 //margin around borders (just for better look)
31 #define BORDER_WIDTH 10
32 #define BORDER_COLOR black
35 #define PLAYGROUND_WIDTH PLAYGROUND_WIDTH_MM
36 #define PLAYGROUND_HEIGHT PLAYGROUND_HEIGHT_MM
38 PlaygroundScene::PlaygroundScene(QObject *parent)
39 : QGraphicsScene(parent)
42 QGraphicsRectItem *playgroundRect;
44 /* All scene units are milimeters */
45 setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
47 /* playground border */
48 addRect(QRect(0, 0, -BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR)); //left
49 addRect(QRect(-BORDER_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR)); //top
50 addRect(QRect(PLAYGROUND_WIDTH, 0, BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR)); //right
51 addRect(QRect(-BORDER_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR)); //bottom
54 playgroundRect = addRect(
55 QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT),
57 QPixmap(":/images/playground_eurobot2012_1024.png").scaled(PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT)
59 playgroundRect->setZValue(0);
62 obstacle = new QGraphicsEllipseItem(0, 0, SIM_OBST_SIZE_M*1000, SIM_OBST_SIZE_M*1000);
63 obstacle->translate(-SIM_OBST_SIZE_M*1000/2,-SIM_OBST_SIZE_M*1000/2);
64 obstacle->setZValue(5);
65 obstacle->setBrush(QBrush(QColor(0, 0, 255, 200)));
66 obstacle->setVisible(false);
67 obstacle->setPos(QPointF(2000, 1000));
68 this->addItem(obstacle);
71 PlaygroundScene::~PlaygroundScene()
75 void PlaygroundScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
77 QPointF pos = scene2world(mouseEvent->scenePos());
79 QGraphicsScene::mouseMoveEvent(mouseEvent);
82 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
84 QGraphicsScene::mousePressEvent(mouseEvent);
85 QPointF pos = scene2world(mouseEvent->scenePos());
86 obstacle->setPos(mouseEvent->scenePos().x(),
87 mouseEvent->scenePos().y());
88 emit obstacleChanged(pos);
92 \fn PlaygroundScene::scene2world(QPointF scenePos)
94 QPointF PlaygroundScene::scene2world(QPointF scenePos)
96 return QPointF((scenePos.x()) / 1000.0,
97 (scenePos.y()) / 1000.0);
102 \fn PlaygroundScene::world2scene(QPointF worldPos)
104 QPointF PlaygroundScene::world2scene(QPointF worldPos)
106 return QPointF(worldPos.x() * 1000,
107 worldPos.y() * 1000);
112 \fn PlaygroundScene::showObstacle(int val)
114 void PlaygroundScene::showObstacle(int val)
117 obstacle->setVisible(true);
119 obstacle->setVisible(false);