CustusX  22.04-rc3
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 {
147  TemporaryPausePlay(PlaybackTime* base) : mBase(base)
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 }
222 bool TimelineEvent::isSingular() const { return similar(mEndTime,mStartTime); }
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 */
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)
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)
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
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
Namespace for all CustusX production code.