NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxPlaybackTime.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxPlaybackTime.h"
34 #include <iostream>
35 #include "cxTime.h"
36 #include "cxTypeConversions.h"
37 
38 #include "cxVector3D.h"
39 
40 namespace cx
41 {
42 
44 {
45  mStartTime = QDateTime::currentDateTime();
46  mOffset = 0;
47  mLength = 10000;
48  mSpeed = 1.0;
49 
50  mTimer = new QTimer;
51  connect(mTimer, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
52  mTimer->setInterval(40);
53 }
54 
55 void PlaybackTime::initialize(QDateTime start, qint64 length)
56 {
57  this->stop();
58  mStartTime = start;
59  mLength = length;
60 
61 // report(QString("Initialized PlaybackTime with start time [%1] and end time [%2]")
62 // .arg(mStartTime.toString(timestampMilliSecondsFormatNice()))
63 // .arg(mStartTime.addMSecs(mLength).toString(timestampMilliSecondsFormatNice())));
64 }
65 
67 {
68  delete mTimer;
69 }
70 
72 {
73  return mTimer->isActive();
74 }
75 
77 {
78  mPlayStart = QDateTime::currentDateTime();
79  mLastPlayOffset = mOffset;
80 // mStartTime = QDateTime::currentDateTime();
81  mTimer->start();
82  this->timeoutSlot();
83 }
84 
86 {
87  mTimer->stop();
88  mOffset = 0;
89  this->timeoutSlot();
90 }
91 
93 {
94  mOffset = this->getOffset();
95  mTimer->stop();
96  this->timeoutSlot();
97 }
98 
100 {
101  if (mTimer->isActive())
102  {
103  // find the offset from the last start to now.
104  qint64 offset = mPlayStart.msecsTo(QDateTime::currentDateTime());
105  return mLastPlayOffset + mSpeed * offset;
106  }
107  else
108  {
109  return mOffset;
110  }
111 }
112 
113 void PlaybackTime::timeoutSlot()
114 {
115  // find the offset from the last start to now. Use to update mOffset.
116 // int offset = mPlayStart.msecsTo(QDateTime::currentDateTime());
117 // mOffset = mLastPlayOffset + offset;
118 // mOffset = this->getOffset(); // do we need this???
119 
120 // int secs = mOffset; // SmStartTime.secsTo(QDateTime::currentDateTime());
121 
122 // QString text = QString("<font size=%1 color=%2><b>%3 ms</b></font>").arg(mFontSize).arg(color).arg(secs);
123 // std::cout << mOffset << std::endl;
124 
125  if (this->getOffset() > mLength)
126  {
127  // similar to pause(), except dont call timeout recusively
128  mOffset = mLength;
129  mTimer->stop();
130  }
131  else
132  emit changed();
133 }
134 
135 
136 void PlaybackTime::forward(qint64 msecs)
137 {
138  this->moveOffset(msecs);
139 }
140 
141 void PlaybackTime::rewind(qint64 msecs)
142 {
143  this->moveOffset(-msecs);
144 }
145 
146 void PlaybackTime::moveOffset(qint64 delta)
147 {
148  this->setOffset(this->getOffset()+delta);
149 }
150 
151 void PlaybackTime::setTime(QDateTime time)
152 {
153  this->setOffset(this->getStartTime().msecsTo(time));
154 }
155 
156 QDateTime PlaybackTime::getTime() const
157 {
158 // std::cout << "gettime " << this->getStartTime().addMSecs(this->getOffset()).toString(timestampMilliSecondsFormatNice()) << std::endl;
159 
160  return this->getStartTime().addMSecs(this->getOffset());
161 }
162 
167 {
169  {
170  mPlaying = mBase->isPlaying();
171  if (mPlaying)
172  mBase->pause();
173  }
175  {
176  if (mPlaying)
177  mBase->start();
178  }
179  bool mPlaying;
181 };
182 
183 void PlaybackTime::setOffset(qint64 val)
184 {
185  if (val==mOffset)
186  return;
187 
188  TemporaryPausePlay sentry(this);
189 
190  mOffset = std::max<quint64>(0, val);
191  this->timeoutSlot();
192 }
193 
195 {
196  return mLength;
197 }
198 
199 QDateTime PlaybackTime::getStartTime() const
200 {
201  return mStartTime;
202 }
203 
204 void PlaybackTime::setSpeed(double val)
205 {
206  TemporaryPausePlay sentry(this);
207  mSpeed = val;
208 }
209 
211 {
212  return mSpeed;
213 }
214 
216 {
217  mTimer->setInterval(val);
218 }
219 
221 {
222  return mTimer->interval();
223 }
224 
225 //---------------------------------------------------------
226 //---------------------------------------------------------
227 //---------------------------------------------------------
228 
229 //---------------------------------------------------------
230 //---------------------------------------------------------
231 //---------------------------------------------------------
232 
233 
237 bool TimelineEvent::isInside(double time, double tol_ms) const
238 {
239  double w = mEndTime - mStartTime;
240  double m = mStartTime + w/2;
241  return fabs(time - m) < std::max(w, tol_ms)/2;
242 }
245 {
246  double w0 = mEndTime - mStartTime;
247  double m0 = mStartTime + w0/2;
248  double w1 = rhs.mEndTime - rhs.mStartTime;
249  double m1 = rhs.mStartTime + w1/2;
250  return fabs(m1-m0) < (w1+w0)/2;
251 }
253 {
254  return mStartTime < rhs.mStartTime;
255 }
256 
257 
258 } /* namespace cx */
bool isInside(double time, double tol_ms=0) const
qint64 getOffset() const
void stop()
stop playing and reset to start
bool operator<(const TimelineEvent &rhs) const
virtual ~PlaybackTime()
bool isOverlap(const TimelineEvent &rhs) const
TemporaryPausePlay(PlaybackTime *base)
void pause()
pause playing
Description of one event in time.
void setSpeed(double val)
set speed as a ratio of real time. 1 is real time, less is slower, more is faster.
void forward(qint64 msecs)
jump forward in ms
QDateTime getTime() const
void setOffset(qint64 val)
set time as an offset from start
void setResolution(qint64 val)
set resolution in ms (signals are emitted with this spacing)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
void rewind(qint64 msecs)
jump backward in ms
QDateTime getStartTime() const
bool isSingular() const
void moveOffset(qint64 delta)
change the offset with an amount
void setTime(QDateTime time)
qint64 getLength() const
length of recording in ms
RealScalar length() const
void start()
start playing.
Controller for historic time, playback etc.
bool isPlaying() const
void initialize(QDateTime start, qint64 length)
double getSpeed() const