NorMIT-nav  22.09
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxPlaybackTime.h"
13 #include <iostream>
14 #include "cxTime.h"
15 #include "cxTypeConversions.h"
16 
17 #include "cxVector3D.h"
18 
19 namespace cx
20 {
21 
23 {
24  mStartTime = QDateTime::currentDateTime();
25  mOffset = 0;
26  mLength = 10000;
27  mSpeed = 1.0;
28 
29  mTimer = new QTimer;
30  connect(mTimer, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
31  mTimer->setInterval(40);
32 }
33 
34 void PlaybackTime::initialize(QDateTime start, qint64 length)
35 {
36  this->stop();
37  mStartTime = start;
38  mLength = length;
39 
40 // report(QString("Initialized PlaybackTime with start time [%1] and end time [%2]")
41 // .arg(mStartTime.toString(timestampMilliSecondsFormatNice()))
42 // .arg(mStartTime.addMSecs(mLength).toString(timestampMilliSecondsFormatNice())));
43 }
44 
46 {
47  delete mTimer;
48 }
49 
51 {
52  return mTimer->isActive();
53 }
54 
56 {
57  mPlayStart = QDateTime::currentDateTime();
58  mLastPlayOffset = mOffset;
59 // mStartTime = QDateTime::currentDateTime();
60  mTimer->start();
61  this->timeoutSlot();
62 }
63 
65 {
66  mTimer->stop();
67  mOffset = 0;
68  this->timeoutSlot();
69 }
70 
72 {
73  mOffset = this->getOffset();
74  mTimer->stop();
75  this->timeoutSlot();
76 }
77 
79 {
80  if (mTimer->isActive())
81  {
82  // find the offset from the last start to now.
83  qint64 offset = mPlayStart.msecsTo(QDateTime::currentDateTime());
84  return mLastPlayOffset + mSpeed * offset;
85  }
86  else
87  {
88  return mOffset;
89  }
90 }
91 
92 void PlaybackTime::timeoutSlot()
93 {
94  // find the offset from the last start to now. Use to update mOffset.
95 // int offset = mPlayStart.msecsTo(QDateTime::currentDateTime());
96 // mOffset = mLastPlayOffset + offset;
97 // mOffset = this->getOffset(); // do we need this???
98 
99 // int secs = mOffset; // SmStartTime.secsTo(QDateTime::currentDateTime());
100 
101 // QString text = QString("<font size=%1 color=%2><b>%3 ms</b></font>").arg(mFontSize).arg(color).arg(secs);
102 // std::cout << mOffset << std::endl;
103 
104  if (this->getOffset() > mLength)
105  {
106  // similar to pause(), except dont call timeout recusively
107  mOffset = mLength;
108  mTimer->stop();
109  }
110  else
111  emit changed();
112 }
113 
114 
115 void PlaybackTime::forward(qint64 msecs)
116 {
117  this->moveOffset(msecs);
118 }
119 
120 void PlaybackTime::rewind(qint64 msecs)
121 {
122  this->moveOffset(-msecs);
123 }
124 
125 void PlaybackTime::moveOffset(qint64 delta)
126 {
127  this->setOffset(this->getOffset()+delta);
128 }
129 
130 void PlaybackTime::setTime(QDateTime time)
131 {
132  this->setOffset(this->getStartTime().msecsTo(time));
133 }
134 
135 QDateTime PlaybackTime::getTime() const
136 {
137 // std::cout << "gettime " << this->getStartTime().addMSecs(this->getOffset()).toString(timestampMilliSecondsFormatNice()) << std::endl;
138 
139  return this->getStartTime().addMSecs(this->getOffset());
140 }
141 
146 {
148  {
149  mPlaying = mBase->isPlaying();
150  if (mPlaying)
151  mBase->pause();
152  }
154  {
155  if (mPlaying)
156  mBase->start();
157  }
158  bool mPlaying;
160 };
161 
162 void PlaybackTime::setOffset(qint64 val)
163 {
164  if (val==mOffset)
165  return;
166 
167  TemporaryPausePlay sentry(this);
168 
169  mOffset = std::max<quint64>(0, val);
170  this->timeoutSlot();
171 }
172 
174 {
175  return mLength;
176 }
177 
178 QDateTime PlaybackTime::getStartTime() const
179 {
180  return mStartTime;
181 }
182 
183 void PlaybackTime::setSpeed(double val)
184 {
185  TemporaryPausePlay sentry(this);
186  mSpeed = val;
187 }
188 
190 {
191  return mSpeed;
192 }
193 
195 {
196  mTimer->setInterval(val);
197 }
198 
200 {
201  return mTimer->interval();
202 }
203 
204 //---------------------------------------------------------
205 //---------------------------------------------------------
206 //---------------------------------------------------------
207 
208 //---------------------------------------------------------
209 //---------------------------------------------------------
210 //---------------------------------------------------------
211 
212 
216 bool TimelineEvent::isInside(double time, double tol_ms) const
217 {
218  double w = mEndTime - mStartTime;
219  double m = mStartTime + w/2;
220  return fabs(time - m) < std::max(w, tol_ms)/2;
221 }
224 {
225  double w0 = mEndTime - mStartTime;
226  double m0 = mStartTime + w0/2;
227  double w1 = rhs.mEndTime - rhs.mStartTime;
228  double m1 = rhs.mStartTime + w1/2;
229  return fabs(m1-m0) < (w1+w0)/2;
230 }
232 {
233  return mStartTime < rhs.mStartTime;
234 }
235 
236 
237 } /* namespace cx */
cx::TemporaryPausePlay::mBase
PlaybackTime * mBase
Definition: cxPlaybackTime.cpp:159
cx::PlaybackTime::getResolution
double getResolution()
Definition: cxPlaybackTime.cpp:199
cx::PlaybackTime::initialize
void initialize(QDateTime start, qint64 length)
Definition: cxPlaybackTime.cpp:34
cx::PlaybackTime::PlaybackTime
PlaybackTime()
Definition: cxPlaybackTime.cpp:22
cxPlaybackTime.h
cx::TemporaryPausePlay::TemporaryPausePlay
TemporaryPausePlay(PlaybackTime *base)
Definition: cxPlaybackTime.cpp:147
cx::PlaybackTime::setSpeed
void setSpeed(double val)
set speed as a ratio of real time. 1 is real time, less is slower, more is faster.
Definition: cxPlaybackTime.cpp:183
cx::PlaybackTime::forward
void forward(qint64 msecs)
jump forward in ms
Definition: cxPlaybackTime.cpp:115
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::PlaybackTime::start
void start()
start playing.
Definition: cxPlaybackTime.cpp:55
cx::TimelineEvent::isOverlap
bool isOverlap(const TimelineEvent &rhs) const
Definition: cxPlaybackTime.cpp:223
cx::PlaybackTime::getLength
qint64 getLength() const
length of recording in ms
Definition: cxPlaybackTime.cpp:173
cx::TimelineEvent::operator<
bool operator<(const TimelineEvent &rhs) const
Definition: cxPlaybackTime.cpp:231
cx::TimelineEvent::mEndTime
double mEndTime
Definition: cxPlaybackTime.h:101
cx::TimelineEvent
Description of one event in time.
Definition: cxPlaybackTime.h:92
cx::PlaybackTime::getOffset
qint64 getOffset() const
Definition: cxPlaybackTime.cpp:78
cx::TimelineEvent::isSingular
bool isSingular() const
Definition: cxPlaybackTime.cpp:222
cxVector3D.h
cx::TemporaryPausePlay
Definition: cxPlaybackTime.cpp:145
length
RealScalar length() const
Definition: cxMatrixBaseEigenAddons.h:35
cx::PlaybackTime::pause
void pause()
pause playing
Definition: cxPlaybackTime.cpp:71
cx::PlaybackTime::moveOffset
void moveOffset(qint64 delta)
change the offset with an amount
Definition: cxPlaybackTime.cpp:125
cx::PlaybackTime::setTime
void setTime(QDateTime time)
Definition: cxPlaybackTime.cpp:130
cx::PlaybackTime
Controller for historic time, playback etc.
Definition: cxPlaybackTime.h:38
cx::PlaybackTime::changed
void changed()
cx::PlaybackTime::setResolution
void setResolution(qint64 val)
set resolution in ms (signals are emitted with this spacing)
Definition: cxPlaybackTime.cpp:194
cx::PlaybackTime::getStartTime
QDateTime getStartTime() const
Definition: cxPlaybackTime.cpp:178
cxTypeConversions.h
cx::PlaybackTime::rewind
void rewind(qint64 msecs)
jump backward in ms
Definition: cxPlaybackTime.cpp:120
cx::PlaybackTime::setOffset
void setOffset(qint64 val)
set time as an offset from start
Definition: cxPlaybackTime.cpp:162
cxTime.h
cx::TemporaryPausePlay::~TemporaryPausePlay
~TemporaryPausePlay()
Definition: cxPlaybackTime.cpp:153
cx::PlaybackTime::isPlaying
bool isPlaying() const
Definition: cxPlaybackTime.cpp:50
cx::TemporaryPausePlay::mPlaying
bool mPlaying
Definition: cxPlaybackTime.cpp:158
cx::TimelineEvent::mStartTime
double mStartTime
Definition: cxPlaybackTime.h:100
cx::PlaybackTime::~PlaybackTime
virtual ~PlaybackTime()
Definition: cxPlaybackTime.cpp:45
cx::PlaybackTime::getSpeed
double getSpeed() const
Definition: cxPlaybackTime.cpp:189
cx::PlaybackTime::getTime
QDateTime getTime() const
Definition: cxPlaybackTime.cpp:135
cx::similar
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
Definition: cxCameraStyleForView.cpp:506
cx::TimelineEvent::isInside
bool isInside(double time, double tol_ms=0) const
Definition: cxPlaybackTime.cpp:216
cx::PlaybackTime::stop
void stop()
stop playing and reset to start
Definition: cxPlaybackTime.cpp:64