3 #include "PlaygroundScene.h"
5 Trail::Trail(const QPen &pen): QGraphicsItem(), bound(-1,-1,0,0)
16 QRectF Trail::boundingRect() const
21 void Trail::findBound()
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)
28 if (points[i].x() < bound.left()) {
29 bound.setLeft(points[i].x());
31 if (points[i].x() > bound.right()) {
32 bound.setRight(points[i].x());
34 if (points[i].y() < bound.bottom()) {
35 bound.setBottom(points[i].y());
37 if (points[i].y() > bound.top()) {
38 bound.setTop(points[i].y());
43 void Trail::addPoint(QPointF point)
45 prepareGeometryChange();
46 point = PlaygroundScene::world2scene(point);
48 currentIndex = (currentIndex+1) % trailLen;
49 if (points[currentIndex].x() >= 0) {
51 update(QRectF( points[currentIndex],
52 points[(currentIndex+1)%trailLen]));
55 points[currentIndex] = point;
58 if (point.x() < bound.left()) {
59 bound.setLeft(point.x());
61 if (point.x() > bound.right()) {
62 bound.setRight(point.x());
64 if (point.y() < bound.bottom()) {
65 bound.setBottom(point.y());
67 if (point.y() > bound.top()) {
68 bound.setTop(point.y());
71 update(QRectF( points[(currentIndex+trailLen-1)%trailLen],
72 points[currentIndex]));
75 void Trail::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
77 if (currentIndex == (unsigned)-1)
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]);
95 for (unsigned i=0; i<trailLen; i++)
96 points[i] = QPointF(-1, -1);