]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/SmallRobot.cpp
Remove layers provided by manufacturers
[eurobot/public.git] / src / robomon / SmallRobot.cpp
1 /*
2  * SmallRobot.cpp                       07/12/04
3  *
4  * Draw a small robot on the playground.
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 <math.h>
13 #include <QGraphicsScene>
14 #include <QPainter>
15 #include <QStyleOption>
16 #include <robomath.h>
17
18 #include "SmallRobot.h"
19
20 SmallRobot::SmallRobot()
21         : color(255, 0, 0)
22 {
23 }
24
25 SmallRobot::SmallRobot(QColor color)
26 {
27         this->color = color;
28 }
29
30 QRectF SmallRobot::boundingRect() const
31 {
32         qreal adjust = 0.5;
33         return QRectF(-18 - adjust, -22 - adjust, 36 + adjust, 60 + adjust);
34 }
35
36 void SmallRobot::setParent(QWidget *parent)
37 {
38         /* used to update after the robot's replacement */      
39         this->parent = parent;
40         connect(this, SIGNAL(robotMovedSignal()), parent, SLOT(robotMoved()));
41 }
42
43 void SmallRobot::paint(QPainter *painter, 
44                         const QStyleOptionGraphicsItem *, QWidget *)
45 {
46         painter->setBrush(color);
47         painter->drawEllipse((int)(5 + -10/2.0), (int)(-10/2.0), 
48                                 (int)10, (int)10);
49         painter->drawLine(5, 0, 25, 0);
50 }
51
52 void SmallRobot::init()
53 {
54         setFlag(ItemIsMovable);
55         updateRobot();
56 }
57
58 void SmallRobot::move()
59 {
60         QPointF position = pos();
61
62         mclPart->x = (qreal)(position.x() / 
63                         widgetSize.width() * playgroundSize.width());
64         mclPart->y = (qreal)((widgetSize.height() - position.y()) / 
65                         widgetSize.height() * playgroundSize.height());
66
67         emit robotMovedSignal();
68         updateRobot();
69 }
70
71 void SmallRobot::updateRobot()
72 {
73         resetTransform();
74         rotate(RAD2DEG(-mclPart->angle));
75         setPos((qreal)(mclPart->x / playgroundSize.width() * widgetSize.width()), 
76                 (qreal)((playgroundSize.height() - mclPart->y)
77                         / playgroundSize.height() * widgetSize.height()));
78
79         //FIXME: I do not know how calculate thetas here. Michal
80         //mcl_pos2ang(&mclPart, theta, mcl.beacon_cnt, mcl.beacon_color);
81         setToolTip(QString("X=%1 Y=%2\nangle=%3\n"
82 //                              "weight=%4"
83                         // "\nth1=%5\nth2=%6\nth3=%7\nth4=%8"
84                            )
85                         .arg(mclPart->x, 0, 'f', 3)
86                         .arg(mclPart->y, 0, 'f', 3)
87                         .arg(RAD2DEG(mclPart->angle), 0, 'f', 3)
88 //                      .arg(mclPart->weight, 0, 'f', 5)
89                         // .arg(RAD2DEG(mclPart->theta[0]), 0, 'f', 3)
90 //                      .arg(RAD2DEG(mclPart->theta[1]), 0, 'f', 3)
91 //                      .arg(RAD2DEG(mclPart->theta[2]), 0, 'f', 3)
92 //                      .arg(RAD2DEG(mclPart->theta[3]), 0, 'f', 3)
93                 );
94
95 //      int col = (int)(mclPart->weight * 128 + 50);
96 //      col = (col > 255) ? 255 : col;
97 //      col = (col < 0) ? 0 : col;
98         int col = 255;
99         color = QColor(col, 40, 40);
100 }
101
102 QPainterPath SmallRobot::shape() const
103 {
104         QPainterPath path;
105         path.addEllipse((int)(5 + -10/2.0), (int)(-10/2.0), 
106                                 (int)10, (int)10);
107         return path;
108 }
109
110 void SmallRobot::mousePressEvent(QGraphicsSceneMouseEvent *event)
111 {
112         update();
113         QGraphicsItem::mousePressEvent(event);
114 }
115
116 void SmallRobot::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
117 {
118         update();
119         move();
120         QGraphicsItem::mouseReleaseEvent(event);
121 }