All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TrajectoryItem.cpp
Go to the documentation of this file.
1 // TerraLib
2 #include "TrajectoryItem.h"
3 #include "AnimationScene.h"
4 #include "Animation.h"
5 #include "../canvas/MapDisplay.h"
6 #include "../canvas/Canvas.h"
7 
8 // Qt
9 #include <QtCore/QPropertyAnimation>
10 #include <QtGui/QPainter>
11 #include <QtCore/QVector>
12 #include <QtCore/QFile>
13 
14 
15 te::qt::widgets::TrajectoryItem::TrajectoryItem(const QString& title, const QString& file, te::qt::widgets::MapDisplay* display)
16  : te::qt::widgets::AnimationItem(title, display),
17  m_forwardColor(Qt::blue),
18  m_backwardColor(Qt::magenta),
19  m_lineWidth(2)
20 {
21  QFile f(file);
22  if(f.exists())
23  setPixmap(QPixmap(file));
24  else
25  {
26  QPixmap pix(20, 20);
27  pix.fill(Qt::transparent);
28  QPainter painter(&pix);
29  QBrush b(Qt::red);
30  painter.setBrush(b);
31  QPen p(Qt::red);
32  painter.setPen(p);
33  painter.drawEllipse(QRect(1, 1, 18, 18));
34  painter.end();
35  setPixmap(pix);
36  }
37  setMatrix();
38 }
39 
41 {
42 }
43 
45 {
47 }
48 
49 void te::qt::widgets::TrajectoryItem::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
50 {
51  m_posOld = m_pos;
52  m_pos = pos(); // icon position
53 
54  if(m_animationRoute.empty())
55  return;
56 
57  if(m_automaticPan)
58  {
59  te::gm::Envelope e = m_display->getExtent();
60  QRectF r(QPointF(e.getLowerLeftX(), e.getLowerLeftY()), QPointF(e.getUpperRightX(), e.getUpperRightY()));
61  if(r.contains(m_pos) == false)
62  {
63  r.moveCenter(m_pos);
64  e.m_llx = r.left();
65  e.m_lly = r.top();
66  e.m_urx = r.right();
67  e.m_ury = r.bottom();
68  m_display->setExtent(e);
69  return;
70  }
71  }
72 
73  unsigned int curTime = m_animation->currentTime();
74  if(m_curTimeDuration == curTime)
75  return;
76 
77  bool erase = false;
78  if(m_animation->direction() == QAbstractAnimation::Forward)
79  {
80  if(curTime < m_curTimeDuration) // erase trail
81  erase = true;
82  }
83  else
84  {
85  if(curTime > m_curTimeDuration) // erase trail
86  erase = true;
87  }
88 
89  if(erase)
90  {
91  if(m_erasePerfectly)
92  {
93  m_curTimeDuration = curTime;
94  draw();
95  }
96  else
97  this->erase(curTime);
98  }
99  else
100  drawForward(curTime);
101 
102  m_display->update();
103 }
104 
105 void te::qt::widgets::TrajectoryItem::drawForward(const unsigned int& curTime)
106 {
107  if(m_animationRoute.empty())
108  return;
109 
110  setMatrix();
111  AnimationScene* as = (AnimationScene*)scene();
112  QPixmap* scenePixmap = as->m_trajectoryPixmap;
113 
114  QPainter painter(scenePixmap);
115  QPen pen(Qt::NoBrush, 2);
116  QColor trailColor;
117  if(m_animation->direction() == QAbstractAnimation::Forward)
118  trailColor = m_forwardColor;
119  else
120  trailColor = m_backwardColor;
121 
122  pen.setColor(trailColor);
123  painter.setPen(pen);
124  painter.setBrush(Qt::NoBrush);
125 
126  int indold = m_animation->getAnimationDataIndex((double)m_curTimeDuration / (double)m_duration);
127  int ind = m_animation->getAnimationDataIndex((double)curTime / (double)m_duration);
128  m_curTimeDuration = curTime;
129 
130  QVector<QPointF> vec;
131 
132  if(indold == ind)
133  {
134  vec.push_back(m_posOld);
135  vec.push_back(m_pos);
136  QPolygonF polf(vec);
137  QPolygon pol = m_matrix.map(polf).toPolygon();
138  painter.drawPolyline(pol);
139  }
140  else if(m_animation->direction() == QAbstractAnimation::Forward)
141  {
142  while(indold <= ind)
143  vec.push_back(m_animationRoute[indold++]);
144  if(vec.isEmpty() == false && vec.last() != m_pos)
145  vec.push_back(m_pos);
146  QPolygonF polf(vec);
147  QPolygon pol = m_matrix.map(polf).toPolygon();
148  painter.drawPolyline(pol);
149  }
150  else
151  {
152  while(indold >= ind)
153  vec.push_back(m_animationRoute[indold--]);
154  if(vec.isEmpty() == false && vec.last() != m_pos)
155  vec.push_back(m_pos);
156  QPolygonF polf(vec);
157  QPolygon pol = m_matrix.map(polf).toPolygon();
158  painter.drawPolyline(pol);
159  }
160  painter.end();
161 }
162 
163 
164 void te::qt::widgets::TrajectoryItem::erase(const unsigned int& curTime)
165 {
166  if(m_animationRoute.empty())
167  return;
168 
169  setMatrix();
170  AnimationScene* as = (AnimationScene*)scene();
171  QPixmap* scenePixmap = as->m_trajectoryPixmap;
172 
173  QPainter painter(scenePixmap);
174  painter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
175  QPen pen(Qt::NoBrush, 2);
176  QColor trailColor(Qt::white);
177  pen.setColor(trailColor);
178  painter.setPen(pen);
179  painter.setBrush(Qt::NoBrush);
180 
181  int indold = m_animation->getAnimationDataIndex((double)m_curTimeDuration / (double)m_duration);
182  int ind = m_animation->getAnimationDataIndex((double)curTime / (double)m_duration);
183  m_curTimeDuration = curTime;
184 
185  QVector<QPointF> vec;
186  if(indold == ind)
187  {
188  vec.push_back(m_posOld);
189  vec.push_back(m_pos);
190  QPolygonF polf(vec);
191  QPolygon pol = m_matrix.map(polf).toPolygon();
192  painter.drawPolyline(pol);
193  }
194  else if(m_animation->direction() == QAbstractAnimation::Backward)
195  {
196  vec.push_back(m_posOld);
197  while(indold < ind)
198  vec.push_back(m_animationRoute[indold++]);
199  vec.push_back(m_pos);
200  QPolygonF polf(vec);
201  QPolygon pol = m_matrix.map(polf).toPolygon();
202  painter.drawPolyline(pol);
203  }
204  else
205  {
206  vec.push_back(m_posOld);
207  while(indold > ind)
208  vec.push_back(m_animationRoute[indold--]);
209  vec.push_back(m_pos);
210  QPolygonF polf(vec);
211  QPolygon pol = m_matrix.map(polf).toPolygon();
212  painter.drawPolyline(pol);
213  }
214  painter.end();
215 }
216 
218 {
219  if(m_animationRoute.empty())
220  return;
221 
222  //setMatrix();
223  AnimationScene* as = (AnimationScene*)scene();
224  QPixmap* scenePixmap = as->m_trajectoryPixmap;
225 
226  QPainter painter(scenePixmap);
227  QPen pen(Qt::NoBrush, 2);
228  QColor trailColor;
229  if(m_animation->direction() == QAbstractAnimation::Forward)
230  trailColor = m_forwardColor;
231  else
232  trailColor = m_backwardColor;
233 
234  pen.setColor(trailColor);
235  painter.setPen(pen);
236  painter.setBrush(Qt::NoBrush);
237 
238  int count = m_animationRoute.size();
239  int ind = m_animation->getAnimationDataIndex((double)m_curTimeDuration / (double)m_duration);
240 
241  QVector<QPointF> vec;
242  if(m_animation->direction() == QAbstractAnimation::Forward)
243  {
244  if(ind > 0)
245  {
246  int i = 0;
247  while(i <= ind)
248  vec.push_back(m_animationRoute[i++]);
249  if(vec.isEmpty() == false && vec.last() != m_pos)
250  vec.push_back(m_pos);
251  QPolygonF polf(vec);
252  QPolygon pol = m_matrix.map(polf).toPolygon();
253  painter.drawPolyline(pol);
254  }
255  }
256  else
257  {
258  int i = count - 1;
259  while(i >= ind)
260  vec.push_back(m_animationRoute[i--]);
261  if(m_curTimeDuration != m_duration)
262  {
263  if(vec.isEmpty() == false && vec.last() != m_pos)
264  vec.push_back(m_pos);
265  }
266  QPolygonF polf(vec);
267  QPolygon pol = m_matrix.map(polf).toPolygon();
268  painter.drawPolyline(pol);
269  }
270  painter.end();
271  m_display->update();
272 }
virtual void createAnimationDataInDisplayProjection()
Create Animation Item making reprojection if necessary.
void drawForward(const unsigned int &curTime)
Draw a piece of tracktrajectory trail. It draws from the previous time to the current time...
QPixmap * m_trajectoryPixmap
QPixmap where all the trajectory item are drawn.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
A widget to control the display of a set of layers.
Definition: MapDisplay.h:65
void draw()
Draw the trajectory long trail. It draws the beginning until the current time. The beginning depends ...
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
void setMatrix()
It sets the internal matrix.
void erase(const unsigned int &curTime)
erase a piece of tracktrajectory trail.
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
TrajectoryItem(const QString &title, const QString &file, te::qt::widgets::MapDisplay *display)
Constructor It constructs a Trajectory Icon Item.
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
void createAnimationDataInDisplayProjection()
Create route points making reprojection if necessary.
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
Paint a piece of trajectory trail.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
This class is a dialog for the Pixmap Item.
Definition: AnimationItem.h:57
virtual ~TrajectoryItem()
Destructor It destructs a Trajectory Icon Item.
This class is a dialog for the Animation.