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 #include <QtCore/QMutex>
14 
15 
16 te::qt::widgets::TrajectoryItem::TrajectoryItem(const QString& title, te::qt::widgets::MapDisplay* display, const QString& file, const QSize& size)
17  : te::qt::widgets::AnimationItem(title, display),
18  m_iconFile(file),
19  m_iconSize(size),
20  m_drawTrail(true),
21  m_forwardColor(Qt::blue),
22  m_backwardColor(Qt::magenta),
23  m_lineWidth(2)
24 {
25  if(m_iconSize.isValid() == false)
26  m_iconSize = QSize(20, 20);
27 
28  if(file.isEmpty() == false)
29  {
30  QFile f(file);
31  if(f.exists())
32  {
33  QPixmap p(file);
34  QPixmap pp(m_iconSize);
35  QPainter painter(&pp);
36  painter.drawPixmap(pp.rect(), p, p.rect());
37  painter.end();
38  setPixmap(pp);
39  }
40  }
41 
42  if(pixmap().isNull())
43  {
44  QPixmap pix(m_iconSize);
45  pix.fill(Qt::transparent);
46  QPainter painter(&pix);
47  QBrush b(Qt::red);
48  painter.setBrush(b);
49  QPen p(Qt::red);
50  painter.setPen(p);
51  painter.drawEllipse(QRect(1, 1, 18, 18));
52  painter.end();
53  setPixmap(pix);
54  }
55  setMatrix();
56 }
57 
59 {
60 }
61 
63 {
65 }
66 
67 void te::qt::widgets::TrajectoryItem::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
68 {
69  m_posOld = m_pos;
70  m_pos = pos(); // icon position
71 
72  if(m_animationRoute.empty())
73  return;
74 
75  if(m_automaticPan)
76  {
77  te::gm::Envelope e = m_display->getExtent();
78  double w = e.getWidth();
79  double h = e.getHeight();
80  QRectF r(e.getLowerLeftX(), e.getLowerLeftY(), w, h);
81  double dist = 200;
82  QRectF rm(e.getLowerLeftX()+w/dist, e.getLowerLeftY()+h/dist, w-2*w/dist, h-2*h/dist);
83  if(rm.contains(m_pos) == false)
84  {
85  w -= w/20;
86  h -= h/20;
87  QPointF c = r.center();
88  QPointF dif = m_pos - c;
89  double dw = fabs(dif.x());
90  double dh = fabs(dif.y());
91  double mw = 0;
92  double mh = 0;
93  if(dw >= w/2)
94  mw = (dw - w / 2) + w * m_panFactor;
95  if(dh >= h/2)
96  mh = (dh - h / 2) + h * m_panFactor;
97 
98  QPointF cc;
99  if(dif.x() >= 0 && dif.y() >= 0)
100  cc = c + QPointF(mw, mh);
101  else if(dif.x() >= 0 && dif.y() < 0)
102  cc = c + QPointF(mw, -mh);
103  else if(dif.x() < 0 && dif.y() >= 0)
104  cc = c + QPointF(-mw, mh);
105  else if(dif.x() < 0 && dif.y() < 0)
106  cc = c + QPointF(-mw, -mh);
107  r.moveCenter(cc);
108 
109  if(r.contains(m_pos) == false)
110  r.moveCenter(m_pos);
111 
112  e.m_llx = r.left();
113  e.m_lly = r.top();
114  e.m_urx = r.right();
115  e.m_ury = r.bottom();
116  m_display->setExtent(e);
117  return;
118  }
119  }
120 
121  unsigned int curTime = m_animation->currentTime();
122  if(m_curTimeDuration == curTime)
123  return;
124 
125  if(m_drawTrail)
126  {
127  bool erase = false;
128  if(m_animation->direction() == QAbstractAnimation::Forward)
129  {
130  if(curTime < m_curTimeDuration) // erase trail
131  erase = true;
132  }
133  else
134  {
135  if(curTime > m_curTimeDuration) // erase trail
136  erase = true;
137  }
138 
139  if(erase)
140  {
141  //if(m_erasePerfectly)
142  //{
143  // m_curTimeDuration = curTime;
144  // draw();
145  //}
146  //else
147  this->erase(curTime);
148  }
149  else
150  drawForward(curTime);
151  }
152 
153  m_display->update();
154 }
155 
156 void te::qt::widgets::TrajectoryItem::drawForward(const unsigned int& curTime)
157 {
158  if(m_animationRoute.empty())
159  return;
160 
161  setMatrix();
162 
163  int indold = m_animation->getAnimationDataIndex((double)m_curTimeDuration / (double)m_duration);
164  int ind = m_animation->getAnimationDataIndex((double)curTime / (double)m_duration);
165  m_curTimeDuration = curTime;
166 
167  QVector<QPointF> vec;
168 
169  if(indold == ind)
170  {
171  vec.push_back(m_posOld);
172  vec.push_back(m_pos);
173  }
174  else if(m_animation->direction() == QAbstractAnimation::Forward)
175  {
176  while(indold <= ind)
177  vec.push_back(m_animationRoute[indold++]);
178  if(vec.isEmpty() == false && vec.last() != m_pos)
179  vec.push_back(m_pos);
180  }
181  else
182  {
183  while(indold >= ind)
184  vec.push_back(m_animationRoute[indold--]);
185  if(vec.isEmpty() == false && vec.last() != m_pos)
186  vec.push_back(m_pos);
187  }
188 
189  if(vec.count() > 1)
190  {
191  QPolygonF polf(vec);
192  QPolygon pol = m_matrix.map(polf).toPolygon();
193 
194  QPen pen(Qt::NoBrush, 2);
195  QColor trailColor;
196  if(m_animation->direction() == QAbstractAnimation::Forward)
197  trailColor = m_forwardColor;
198  else
199  trailColor = m_backwardColor;
200  pen.setColor(trailColor);
201 
202  AnimationScene* as = (AnimationScene*)scene();
203  as->m_mutex.lock();
204  QPixmap* scenePixmap = as->m_trajectoryPixmap;
205  QPainter painter(scenePixmap);
206  painter.setPen(pen);
207  painter.setBrush(Qt::NoBrush);
208  painter.drawPolyline(pol);
209  painter.end();
210  as->m_mutex.unlock();
211  }
212 }
213 
214 void te::qt::widgets::TrajectoryItem::erase(const unsigned int& curTime)
215 {
216  if(m_animationRoute.empty())
217  return;
218 
219  setMatrix();
220 
221  int indold = m_animation->getAnimationDataIndex((double)m_curTimeDuration / (double)m_duration);
222  int ind = m_animation->getAnimationDataIndex((double)curTime / (double)m_duration);
223  m_curTimeDuration = curTime;
224 
225  QVector<QPointF> vec;
226  if(indold == ind)
227  {
228  vec.push_back(m_posOld);
229  vec.push_back(m_pos);
230  }
231  else if(m_animation->direction() == QAbstractAnimation::Backward)
232  {
233  vec.push_back(m_posOld);
234  while(indold < ind)
235  vec.push_back(m_animationRoute[indold++]);
236  vec.push_back(m_pos);
237  }
238  else
239  {
240  vec.push_back(m_posOld);
241  while(indold > ind)
242  vec.push_back(m_animationRoute[indold--]);
243  vec.push_back(m_pos);
244  }
245 
246  if(vec.count() > 1)
247  {
248  QPolygonF polf(vec);
249  QPolygon pol = m_matrix.map(polf).toPolygon();
250 
251  QPen pen(Qt::NoBrush, 2);
252  QColor trailColor(Qt::white);
253  pen.setColor(trailColor);
254 
255  AnimationScene* as = (AnimationScene*)scene();
256  as->m_mutex.lock();
257  QPixmap* scenePixmap = as->m_trajectoryPixmap;
258  QPainter painter(scenePixmap);
259  painter.setPen(pen);
260  painter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
261  painter.setBrush(Qt::NoBrush);
262  painter.drawPolyline(pol);
263  painter.end();
264  as->m_mutex.unlock();
265  }
266 }
267 
269 {
270  if(m_animationRoute.empty())
271  return;
272 
273  //setMatrix();
274 
275  int count = m_animationRoute.size();
276  int ind = m_animation->getAnimationDataIndex((double)m_curTimeDuration / (double)m_duration);
277 
278  QVector<QPointF> vec;
279  if(m_animation->direction() == QAbstractAnimation::Forward)
280  {
281  if(ind > 0)
282  {
283  int i = 0;
284  while(i <= ind)
285  vec.push_back(m_animationRoute[i++]);
286  if(vec.isEmpty() == false && vec.last() != m_pos)
287  vec.push_back(m_pos);
288  }
289  }
290  else
291  {
292  int i = count - 1;
293  while(i >= ind)
294  vec.push_back(m_animationRoute[i--]);
295  if(m_curTimeDuration != m_duration)
296  {
297  if(vec.isEmpty() == false && vec.last() != m_pos)
298  vec.push_back(m_pos);
299  }
300  }
301 
302  if(vec.count() > 1)
303  {
304  QPolygonF polf(vec);
305  QPolygon pol = m_matrix.map(polf).toPolygon();
306 
307  QPen pen(Qt::NoBrush, 2);
308  QColor trailColor;
309  if(m_animation->direction() == QAbstractAnimation::Forward)
310  trailColor = m_forwardColor;
311  else
312  trailColor = m_backwardColor;
313 
314  pen.setColor(trailColor);
315 
316  AnimationScene* as = (AnimationScene*)scene();
317  as->m_mutex.lock();
318  QPixmap* scenePixmap = as->m_trajectoryPixmap;
319  QPainter painter(scenePixmap);
320  painter.setPen(pen);
321  painter.setBrush(Qt::NoBrush);
322  painter.drawPolyline(pol);
323  painter.end();
324  as->m_mutex.unlock();
325  }
326  m_display->update();
327 }
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.
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
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
A widget to control the display of a set of layers.
Definition: MapDisplay.h:66
void draw()
Draw the trajectory long trail. It draws the beginning until the current time. The beginning depends ...
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
This file defines a class for a Animation Scene.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
QMutex m_mutex
To not use the scene pixmap simultaneously.
TrajectoryItem(const QString &title, te::qt::widgets::MapDisplay *display, const QString &file, const QSize &size)
Constructor It constructs a Trajectory Icon Item.
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
This file defines a class for a Trajectory Animation.
void createAnimationDataInDisplayProjection()
Create route points making reprojection if necessary.
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
This file defines a class for a Trajectory Item.
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.
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448