]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/trail.cpp
robodim: Hokuyo returned back to original position.
[eurobot/public.git] / src / robomon / trail.cpp
1 #include "trail.h"
2 #include <QPainter>
3 #include "PlaygroundScene.h"
4
5 Trail::Trail(const QPen &pen): QGraphicsItem(), bound(-1,-1,0,0)
6 {
7         this->pen = pen;
8         reset();
9 }
10
11
12 Trail::~Trail()
13 {
14 }
15
16 QRectF Trail::boundingRect() const
17 {
18         return bound;
19 }
20
21 void Trail::findBound()
22 {
23         prepareGeometryChange();
24         bound.setCoords(9999, -1, -1, 9999);
25         for (unsigned i = 0; i<trailLen; i++) {
26                 if (points[i].x() < 0 || points[i].y() < 0)
27                         continue;
28                 if (points[i].x() < bound.left()) {
29                         bound.setLeft(points[i].x());
30                 }
31                 if (points[i].x() > bound.right()) {
32                         bound.setRight(points[i].x());
33                 }
34                 if (points[i].y() < bound.bottom()) {
35                         bound.setBottom(points[i].y());
36                 }
37                 if (points[i].y() > bound.top()) {
38                         bound.setTop(points[i].y());
39                 }
40         }
41 }
42
43 void Trail::addPoint(QPointF point)
44 {
45         prepareGeometryChange();
46         point = PlaygroundScene::world2scene(point);
47         
48         currentIndex = (currentIndex+1) % trailLen;
49         if (points[currentIndex].x() >= 0) {
50                 //overwrite old point
51                 update(QRectF( points[currentIndex],
52                                points[(currentIndex+1)%trailLen]));
53         }
54         
55         points[currentIndex] = point;
56         findBound();
57         
58         if (point.x() < bound.left()) {
59                 bound.setLeft(point.x());
60         }
61         if (point.x() > bound.right()) {
62                 bound.setRight(point.x());
63         }
64         if (point.y() < bound.bottom()) {
65                 bound.setBottom(point.y());
66         }
67         if (point.y() > bound.top()) {
68                 bound.setTop(point.y());
69         }
70
71         update(QRectF( points[(currentIndex+trailLen-1)%trailLen],
72                        points[currentIndex]));
73 }
74
75 void Trail::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
76 {
77         if (currentIndex == (unsigned)-1)
78                 return;
79                 
80         painter->setPen(pen);
81         
82         painter->drawPolyline(points, currentIndex+1);
83         if (currentIndex < trailLen - 2 && points[currentIndex+1].x() >= 0) {
84                 painter->drawPolyline(&points[currentIndex+1], trailLen-currentIndex-1);
85                 painter->drawLine(points[trailLen-1], points[0]);
86         }
87 }
88
89
90
91
92 void Trail::reset()
93 {
94         currentIndex = -1;
95         for (unsigned i=0; i<trailLen; i++)
96                 points[i] = QPointF(-1, -1);
97 }