4 * Draw and move a robot on the playground.
6 * Copyright: (c) 2007 CTU Dragons
7 * CTU FEE - Department of Control Engineering
8 * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
15 #include "PlaygroundScene.h"
18 #include <actuators.h>
21 Robot::Robot(const QString &text, const QPen &pen, const QBrush &brush) :
22 QObject(), QGraphicsItem()
28 setJaws(JAW_LEFT_CLOSE);
29 moveRobot(ROBOT_START_X_M, ROBOT_START_Y_M, 0);
36 QRectF Robot::boundingRect() const
39 ROBOT_WIDTH_MM, ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM);
42 void Robot::paint(QPainter *painter,
43 const QStyleOptionGraphicsItem *option, QWidget *widget)
50 painter->setBrush(brush);
51 painter->drawRect(QRectF(0, 0, ROBOT_WIDTH_MM,
52 ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM));
53 const float xa = ROBOT_WIDTH_MM/2.0;
54 const float ya = ROBOT_AXIS_TO_BACK_MM;
55 const float yb = ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM*0.8;
56 const float yc = ROBOT_AXIS_TO_BACK_MM + ROBOT_AXIS_TO_FRONT_MM*0.4;
57 const float xd = ROBOT_WIDTH_MM/4.0;
59 QLineF(xa, ya, xa, yb),
60 QLineF(xa, yb, xa-xd, yc),
61 QLineF(xa, yb, xa+xd, yc)};
62 painter->drawLines(arrow, 3);
64 QLineF jawsLines[2]; /* field witch jaws lines */
65 const int offset = 40; /* offset of point of rotation from robot sides [mm] */
66 const int hold = 40; /* offset from open position [mm] */
67 const int close = 110; /* offset from open positon [mm] */
68 const double y_hold = sqrt(pow(ROBOT_JAWS_LENGHT_MM, 2) - pow(hold, 2));
69 const double y_close = sqrt(pow(ROBOT_JAWS_LENGHT_MM, 2) - pow(close, 2));
71 switch (jawsPosition) {
73 jawsLines[0] = QLineF(offset, ROBOT_HEIGHT_MM, offset, ROBOT_HEIGHT_MM + ROBOT_JAWS_LENGHT_MM);
74 jawsLines[1] = QLineF(ROBOT_WIDTH_MM - offset , ROBOT_HEIGHT_MM, ROBOT_WIDTH_MM - offset, ROBOT_HEIGHT_MM + ROBOT_JAWS_LENGHT_MM);
78 jawsLines[0] = QLineF(offset, ROBOT_HEIGHT_MM, offset + hold, ROBOT_HEIGHT_MM + ROBOT_JAWS_LENGHT_MM);
79 jawsLines[1] = QLineF(ROBOT_WIDTH_MM - offset , ROBOT_HEIGHT_MM, ROBOT_WIDTH_MM - offset - hold, ROBOT_HEIGHT_MM + y_hold);
83 jawsLines[0] = QLineF(offset, ROBOT_HEIGHT_MM, offset + close, ROBOT_HEIGHT_MM + y_close);
84 jawsLines[1] = QLineF(ROBOT_WIDTH_MM - offset , ROBOT_HEIGHT_MM, ROBOT_WIDTH_MM - offset - close, ROBOT_HEIGHT_MM + y_close);
88 QPen penThick(Qt::black, 20, Qt::SolidLine); /* new pen with 20px thickness for jaws drawing */
89 painter->setPen(penThick);
90 painter->drawLines(jawsLines, 2); /* draw jaws */
91 painter->setPen(pen); /* set pen to previous slim pen */
93 painter->setFont(QFont("Arial", 40, 0, 0));
96 painter->setTransform(mirror, true);
97 painter->drawText(QRect(0, 0, ROBOT_WIDTH_MM, -ROBOT_AXIS_TO_BACK_MM),
98 Qt::AlignCenter, text);
101 void Robot::moveRobot(double x, double y, double phi)
105 pos = PlaygroundScene::world2scene(pos);
108 setTransform(QTransform().rotateRadians(phi-M_PI/2.0).translate(-ROBOT_WIDTH_MM/2.0, -ROBOT_AXIS_TO_BACK_MM));
111 void Robot::mySetVisible(bool show)
116 void Robot::setJaws(int value)
118 QRectF r = boundingRect();
120 jawsPosition = value;