Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxAcquisitionData.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 "cxAcquisitionData.h"
34 
35 //#include "cxAcquisitionManager.h"
36 #include <vector>
37 #include <QDomNode>
38 #include <QDateTime>
39 #include <QStringList>
40 #include "cxTime.h"
41 #include "cxLogger.h"
42 #include "cxTrackingService.h"
43 #include "cxSettings.h"
45 #include "cxReporter.h"
46 #include "cxVisServices.h"
47 
48 
49 namespace cx
50 {
52 {}
53 
55  mServices(services),
56  mReconstructer(reconstructer)
57 {
58 
59 }
60 
62 {
63 }
64 
66 {
67  mRecordSessions.clear();
69 }
70 
72 {
73  mRecordSessions.push_back(session);
75 }
76 
78 {
79  for(int i = 0; i < mRecordSessions.size(); ++i)
80  {
81  if(mRecordSessions[i]->getUid() == session->getUid())
82  mRecordSessions.erase(mRecordSessions.begin() + i);
83  }
85 }
86 
87 std::vector<RecordSessionPtr> AcquisitionData::getRecordSessions()
88 {
89  return mRecordSessions;
90 }
91 
93 {
94  RecordSessionPtr retval;
95  std::vector<RecordSessionPtr>::iterator it = mRecordSessions.begin();
96  for(; it != mRecordSessions.end(); ++it)
97  {
98  if((*it)->getUid() == uid)
99  retval = (*it);
100  }
101  return retval;
102 }
103 
104 void AcquisitionData::addXml(QDomNode& parentNode)
105 {
106  QDomDocument doc = parentNode.ownerDocument();
107  QDomElement base = doc.createElement("stateManager");
108  parentNode.appendChild(base);
109 
110  QDomElement sessionsNode = doc.createElement("recordSessions");
111  std::vector<RecordSessionPtr>::iterator it = mRecordSessions.begin();
112  for(; it != mRecordSessions.end(); ++it)
113  {
114  QDomElement sessionNode = doc.createElement("recordSession");
115  (*it)->addXml(sessionNode);
116  sessionsNode.appendChild(sessionNode);
117  }
118  base.appendChild(sessionsNode);
119 }
120 
121 void AcquisitionData::parseXml(QDomNode& dataNode)
122 {
123  QDomNode recordsessionsNode = dataNode.namedItem("recordSessions");
124  QDomElement recodesessionNode = recordsessionsNode.firstChildElement("recordSession");
125  for (; !recodesessionNode.isNull(); recodesessionNode = recodesessionNode.nextSiblingElement("recordSession"))
126  {
127 // RecordSessionPtr session(new RecordSession("", 0,0,""));
128  RecordSessionPtr session(new RecordSession());
129  session->parseXml(recodesessionNode);
130  this->addRecordSession(session);
131  }
132 }
133 
138 {
139  QString retval;
140  int max = 0;
141  std::vector<RecordSessionPtr> recordsessions = this->getRecordSessions();
142  std::vector<RecordSessionPtr>::iterator iter;
143  for (iter = recordsessions.begin(); iter != recordsessions.end(); ++iter)
144  {
145  QString index = (*iter)->getUid().split("_").front();
146  max = std::max(max, index.toInt());
147  }
148 
149  return max+1;
150 }
151 
155 
156 Acquisition::Acquisition(AcquisitionDataPtr pluginData, QObject* parent) :
157  QObject(parent), mPluginData(pluginData), mCurrentState(AcquisitionService::sNOT_RUNNING),
158  mReady(true),
159  mInfoText("")
160 {
161  connect(this->getServices()->tracking().get(), &TrackingService::stateChanged, this, &Acquisition::checkIfReadySlot);
162  connect(this->getServices()->tracking().get(), &TrackingService::activeToolChanged, this, &Acquisition::checkIfReadySlot);
163  this->checkIfReadySlot();
164 }
165 
167 {
168 }
169 
171 {
172  return true;
173  if (!context.testFlag(AcquisitionService::tTRACKING))
174  return true;
175  return mReady;
176 }
177 
179 {
180  if (!context.testFlag(AcquisitionService::tTRACKING))
181  return "";
182  return mInfoText;
183 }
184 
185 void Acquisition::checkIfReadySlot()
186 {
187  bool tracking = this->getServices()->tracking()->getState()>=Tool::tsTRACKING;
188  ToolPtr tool = this->getServices()->tracking()->getActiveTool();
189  ToolPtr reference_frame = this->getServices()->tracking()->getReferenceTool();
190  if(tracking && reference_frame)
191  {
192  connect(reference_frame.get(), &Tool::toolVisible, this, &Acquisition::checkIfReadySlot, Qt::UniqueConnection);
193  }
194  QString mWhatsMissing;
195  mWhatsMissing.clear();
196 
197  if(tracking)
198  {
199  mWhatsMissing = "<font color=green>Ready to record!</font><br>";
200  if (!tool || !tool->getVisible())
201  {
202  mWhatsMissing += "<font color=orange>Tool not visible</font><br>";
203  }
204  if(!reference_frame || !reference_frame->getVisible())
205  {
206  mWhatsMissing += "<font color=orange>Reference frame not visible.</font><br>";
207  }
208  }
209  else
210  {
211  mWhatsMissing.append("<font color=red>Need to start tracking.</font><br>");
212  }
213 
214  // do not require tracking to be present in order to perform an acquisition.
215  this->setReady(tracking, mWhatsMissing);
216 }
217 
218 void Acquisition::setReady(bool val, QString text)
219 {
220  mReady = val;
221  mInfoText = text;
222 
223  emit readinessChanged();
224 }
225 
227 {
229  {
230  report("Already recording a session, stop before trying to start a new record session.");
231  return;
232  }
233 
234  mCurrentContext = context;
235 
236  if (session)
237  mLatestSession = session;
238  else
239  mLatestSession.reset(new cx::RecordSession(mPluginData->getNewSessionId(), category));
240 
241  mLatestSession->startNewInterval();
242 
243  reporter()->playStartSound();
244  this->setState(AcquisitionService::sRUNNING);
245  emit started();
246 }
247 
249 {
251  {
252  return;
253  }
254 
255  mLatestSession->stopLastInterval();
256  if (!mPluginData->getRecordSession(mLatestSession->getUid()))
257  mPluginData->addRecordSession(mLatestSession);
258  reporter()->playStopSound();
259  this->setState(AcquisitionService::sNOT_RUNNING);
260  emit acquisitionStopped();
261 }
262 
264 {
266  {
267  return;
268  }
269  reporter()->playCancelSound();
270  mLatestSession->cancelLastInterval();
271  mLatestSession.reset();
272  mCurrentContext = AcquisitionService::TYPES();
273  this->setState(AcquisitionService::sNOT_RUNNING);
274  emit cancelled();
275 }
276 
278 {
279  this->setState(AcquisitionService::sPOST_PROCESSING);
280 }
281 
283 {
284  this->setState(AcquisitionService::sNOT_RUNNING);
285 }
286 
287 void Acquisition::setState(AcquisitionService::STATE newState)
288 {
289  AcquisitionService::STATE lastState = mCurrentState;
290  mCurrentState = newState;
291 
292 // // emit some helper signals
293 // if (lastState!=AcquisitionService::sRUNNING && newState==AcquisitionService::sRUNNING)
294 // emit started();
295 // else if (lastState==AcquisitionService::sRUNNING && newState!=AcquisitionService::sRUNNING && mLatestSession)
296 // emit acquisitionStopped();
297 // else if (lastState==AcquisitionService::sRUNNING && newState!=AcquisitionService::sRUNNING && !mLatestSession)
298 // emit cancelled();
299 
300  emit stateChanged();
301 }
302 
303 VisServicesPtr Acquisition::getServices()
304 {
305  return this->getPluginData()->getServices();
306 }
307 
308 }
Acqusition services abstract interface.
boost::shared_ptr< class UsReconstructionService > UsReconstructionServicePtr
ReporterPtr reporter()
Definition: cxReporter.cpp:59
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
void readinessChanged()
std::vector< RecordSessionPtr > getRecordSessions()
void removeRecordSession(RecordSessionPtr session)
void addXml(QDomNode &dataNode)
boost::shared_ptr< class AcquisitionData > AcquisitionDataPtr
void activeToolChanged(const QString &uId)
boost::shared_ptr< class RecordSession > RecordSessionPtr
void toolVisible(bool visible)
AcquisitionService::STATE getState() const
void recordedSessionsChanged()
void addRecordSession(RecordSessionPtr session)
void startRecord(AcquisitionService::TYPES context, QString category, RecordSessionPtr session)
QString getInfoText(AcquisitionService::TYPES) const
AcquisitionDataPtr getPluginData()
void parseXml(QDomNode &dataNode)
void report(QString msg)
Definition: cxLogger.cpp:90
RecordSessionPtr getRecordSession(QString uid)
Acquisition(AcquisitionDataPtr pluginData, QObject *parent=0)
emitting tracking data
Definition: cxTool.h:98
bool isReady(AcquisitionService::TYPES) const
void acquisitionStopped()
boost::shared_ptr< class Tool > ToolPtr