Animation.cpp
Go to the documentation of this file.
1 #include "Animation.h"
2 #include "AnimationItem.h"
3 
4 te::qt::widgets::Animation::Animation(QObject* target, const QByteArray& propertyName, QObject* parent)
5  : QPropertyAnimation(target, propertyName, parent)
6 {
8  ai->m_animation = this;
9 }
10 
12 {
13 }
14 
16 {
17  if(m_temporalAnimationExtent != period)
18  m_temporalAnimationExtent = te::dt::TimePeriod(period.getInitialTimeInstant(), period.getFinalTimeInstant());
19 
20  AnimationItem* ai = (AnimationItem*)targetObject();
21 
22  // create route points in display projection
24 
25  setDataKeyValues();
26 }
27 
29 {
31 
32  // total time duration
33  boost::posix_time::ptime iTime = m_temporalAnimationExtent.getInitialTimeInstant().getTimeInstant();
34  boost::posix_time::ptime fTime = m_temporalAnimationExtent.getFinalTimeInstant().getTimeInstant();
35  boost::posix_time::time_duration diff = fTime - iTime;
36  double totalSeconds = diff.total_seconds();
37 
38  size_t size = ai->m_animationTime.size();
39 
40  // set first animation point
41  setStartValue(ai->m_animationRoute[0]);
42 
43  boost::posix_time::ptime itime = ai->m_animationTime[0].getTimeInstant();
44  boost::posix_time::ptime ftime = ai->m_animationTime[(int)size-1].getTimeInstant();
45 
46  // if the initial time is greater than the total initial time,
47  // be stopped until the time be equal to the initial time this animation.
48  // To this add a key value.
49  ai->m_norInitialTime = 0;
50  if(itime > iTime)
51  {
52  // be stopped until the beginning of the animation time
53  diff = itime - iTime;
54  double seconds = diff.total_seconds();
55 
56  // normalizing the time
57  double t = seconds / totalSeconds;
58  ai->m_norInitialTime = t;
59  t *= .999999;
60  setKeyValueAt(t, ai->m_animationRoute[0]); // add a new key value
61  }
62 
63  for(size_t i = 1; i < size-1; ++i)
64  {
65  te::dt::TimeInstant tinstant = ai->m_animationTime[(int)i]; // animation time instant
66  boost::posix_time::ptime time = tinstant.getTimeInstant();
67  diff = time - iTime;
68  double seconds = diff.total_seconds();
69 
70  // normalizing the time
71  double t = seconds / totalSeconds;
72  setKeyValueAt(t, ai->m_animationRoute[(int)i]);
73  }
74 
75  // if the final time is shorter than the total final time,
76  // stop when reach the end of this animation
77  // To this add a key value.
78  ai->m_norFinalTime = 1;
79  if(ftime < fTime)
80  {
81  // be stopped when it reaches the end of its animation
82  diff = ftime - iTime;
83  double seconds = diff.total_seconds();
84 
85  // normalizing the time
86  double t = seconds / totalSeconds;
87  ai->m_norFinalTime = t;
88  setKeyValueAt(t, ai->m_animationRoute[(int)size-1]); // add a new key value
89  }
90 
91  // set last animation point
92  setEndValue(ai->m_animationRoute[(int)size-1]);
93 }
94 
96 {
97  AnimationItem* ai = (AnimationItem*)targetObject();
98  size_t count = ai->m_animationTime.count();
99 
100  // animation time duration
101  boost::posix_time::ptime aiTime = m_temporalExtent.getInitialTimeInstant().getTimeInstant();
102  boost::posix_time::ptime afTime = m_temporalExtent.getFinalTimeInstant().getTimeInstant();
103 
104  // total time duration
105  boost::posix_time::ptime iTime = m_temporalAnimationExtent.getInitialTimeInstant().getTimeInstant();
106  boost::posix_time::ptime fTime = m_temporalAnimationExtent.getFinalTimeInstant().getTimeInstant();
107  boost::posix_time::time_duration diff = fTime - iTime;
108  double totalSeconds = diff.total_seconds();
109 
110  diff = aiTime - iTime;
111  double aiSeconds = diff.total_seconds();
112  double itrel = aiSeconds / totalSeconds;
113  if (trel < itrel) // time is before start of this animation
114  return -1;
115 
116  diff = afTime - iTime;
117  double afSeconds = diff.total_seconds();
118  double ftrel = afSeconds / totalSeconds;
119  if (trel > ftrel)// time is after the end of this animation
120  return -1;
121 
122  int i;
123  if (direction() == QAbstractAnimation::Forward)
124  {
125  for (i = 0; i < count; ++i)
126  {
127  te::dt::TimeInstant tinstant = ai->m_animationTime[i]; // animation time instant
128  boost::posix_time::ptime time = tinstant.getTimeInstant();
129  diff = time - iTime;
130  double seconds = diff.total_seconds();
131 
132  // normalizing the time
133  double t = seconds / totalSeconds;
134 
135  if (t >= trel)
136  return i;
137  }
138  }
139  else
140  {
141  for (i = (int)count-1; i >= 0; --i)
142  {
143  te::dt::TimeInstant tinstant = ai->m_animationTime[i]; // animation time instant
144  boost::posix_time::ptime time = tinstant.getTimeInstant();
145  diff = time - iTime;
146  double seconds = diff.total_seconds();
147 
148  // normalizing the time
149  double t = seconds / totalSeconds;
150 
151  if (t <= trel)
152  return i;
153  }
154  }
155  return -1;
156 }
double m_norInitialTime
Normalized initial time (between 0 and 1).
int getAnimationDataIndex(const double &trel)
It It gets the animation data index. It is relative to temporal animation extent. ...
Definition: Animation.cpp:95
const boost::posix_time::ptime & getTimeInstant() const
It returns the boost time instant type.
Definition: TimeInstant.h:92
A class to represent time instant.
Definition: TimeInstant.h:55
Animation * m_animation
The animation this item.
double m_norFinalTime
Normalized final time (between 0 and 1).
This file defines a class for a Animation Item.
virtual void adjustDataToAnimationTemporalExtent()
Adjust data for a given time animation period.
TimeInstant getFinalTimeInstant() const
It gets the final time instant.
Definition: TimePeriod.cpp:50
TimeInstant getInitialTimeInstant() const
It gets the initial time instant.
Definition: TimePeriod.cpp:45
void setDataKeyValues()
It It sets the data key values.
Definition: Animation.cpp:28
virtual ~Animation()
Destructor It destructs a Animation.
Definition: Animation.cpp:11
A class to represent time period.
Definition: TimePeriod.h:54
This file defines a class for a Trajectory Animation.
An abstract class for Animation Item.
Definition: AnimationItem.h:62
QVector< te::dt::TimeInstant > m_animationTime
The animation time. It contains only the portions to be animated.
Animation(QObject *target, const QByteArray &propertyName, QObject *parent=0)
Constructor It constructs a Animation.
Definition: Animation.cpp:4
QVector< QPointF > m_animationRoute
It contains only the portions to be animated.
void adjustDataToAnimationTemporalExtent(const te::dt::TimePeriod &period)
It adjust animation data for a given period; /param period The temporal period.
Definition: Animation.cpp:15