CustusX  22.04-rc5
An IGT application
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) 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 "cxAcquisitionData.h"
13 
14 //#include "cxAcquisitionManager.h"
15 #include <vector>
16 #include <QDomNode>
17 #include <QDateTime>
18 #include <QStringList>
19 #include "cxTime.h"
20 #include "cxLogger.h"
21 #include "cxTrackingService.h"
22 #include "cxSettings.h"
24 #include "cxReporter.h"
25 #include "cxVisServices.h"
26 
27 
28 namespace cx
29 {
31 {}
32 
34  mServices(services),
35  mReconstructer(reconstructer)
36 {
37 
38 }
39 
41 {
42 }
43 
45 {
46  mRecordSessions.clear();
48 }
49 
51 {
52  mRecordSessions.push_back(session);
54 }
55 
57 {
58  for(int i = 0; i < mRecordSessions.size(); ++i)
59  {
60  if(mRecordSessions[i]->getUid() == session->getUid())
61  mRecordSessions.erase(mRecordSessions.begin() + i);
62  }
64 }
65 
66 std::vector<RecordSessionPtr> AcquisitionData::getRecordSessions()
67 {
68  return mRecordSessions;
69 }
70 
72 {
73  RecordSessionPtr retval;
74  std::vector<RecordSessionPtr>::iterator it = mRecordSessions.begin();
75  for(; it != mRecordSessions.end(); ++it)
76  {
77  if((*it)->getUid() == uid)
78  retval = (*it);
79  }
80  return retval;
81 }
82 
83 void AcquisitionData::addXml(QDomNode& parentNode)
84 {
85  QDomDocument doc = parentNode.ownerDocument();
86  QDomElement base = doc.createElement("stateManager");
87  parentNode.appendChild(base);
88 
89  QDomElement sessionsNode = doc.createElement("recordSessions");
90  std::vector<RecordSessionPtr>::iterator it = mRecordSessions.begin();
91  for(; it != mRecordSessions.end(); ++it)
92  {
93  QDomElement sessionNode = doc.createElement("recordSession");
94  (*it)->addXml(sessionNode);
95  sessionsNode.appendChild(sessionNode);
96  }
97  base.appendChild(sessionsNode);
98 }
99 
100 void AcquisitionData::parseXml(QDomNode& dataNode)
101 {
102  QDomNode recordsessionsNode = dataNode.namedItem("recordSessions");
103  QDomElement recodesessionNode = recordsessionsNode.firstChildElement("recordSession");
104  for (; !recodesessionNode.isNull(); recodesessionNode = recodesessionNode.nextSiblingElement("recordSession"))
105  {
106 // RecordSessionPtr session(new RecordSession("", 0,0,""));
107  RecordSessionPtr session(new RecordSession());
108  session->parseXml(recodesessionNode);
109  this->addRecordSession(session);
110  }
111 }
112 
117 {
118  QString retval;
119  int max = 0;
120  std::vector<RecordSessionPtr> recordsessions = this->getRecordSessions();
121  std::vector<RecordSessionPtr>::iterator iter;
122  for (iter = recordsessions.begin(); iter != recordsessions.end(); ++iter)
123  {
124  QString index = (*iter)->getUid().split("_").front();
125  max = std::max(max, index.toInt());
126  }
127 
128  return max+1;
129 }
130 
134 
135 Acquisition::Acquisition(AcquisitionDataPtr pluginData, QObject* parent) :
136  QObject(parent), mPluginData(pluginData), mCurrentState(AcquisitionService::sNOT_RUNNING),
137  mReady(true),
138  mInfoText("")
139 {
140  connect(this->getServices()->tracking().get(), &TrackingService::stateChanged, this, &Acquisition::checkIfReadySlot);
141  connect(this->getServices()->tracking().get(), &TrackingService::activeToolChanged, this, &Acquisition::checkIfReadySlot);
142  this->checkIfReadySlot();
143 }
144 
146 {
147 }
148 
150 {
151  return true;
152  if (!context.testFlag(AcquisitionService::tTRACKING))
153  return true;
154  return mReady;
155 }
156 
158 {
159  if (!context.testFlag(AcquisitionService::tTRACKING))
160  return "";
161  return mInfoText;
162 }
163 
164 void Acquisition::checkIfReadySlot()
165 {
166  bool tracking = this->getServices()->tracking()->getState()>=Tool::tsTRACKING;
167  ToolPtr tool = this->getServices()->tracking()->getActiveTool();
168  ToolPtr reference_frame = this->getServices()->tracking()->getReferenceTool();
169  if(tracking && reference_frame)
170  {
171  connect(reference_frame.get(), &Tool::toolVisible, this, &Acquisition::checkIfReadySlot, Qt::UniqueConnection);
172  }
173  QString mWhatsMissing;
174  mWhatsMissing.clear();
175 
176  if(tracking)
177  {
178  mWhatsMissing = "<font color=green>Ready to record!</font><br>";
179  if (!tool || !tool->getVisible())
180  {
181  mWhatsMissing += "<font color=orange>Tool not visible</font><br>";
182  }
183  if(!reference_frame || !reference_frame->getVisible())
184  {
185  mWhatsMissing += "<font color=orange>Reference frame not visible.</font><br>";
186  }
187  }
188  else
189  {
190  mWhatsMissing.append("<font color=red>Need to start tracking.</font><br>");
191  }
192 
193  // do not require tracking to be present in order to perform an acquisition.
194  this->setReady(tracking, mWhatsMissing);
195 }
196 
197 void Acquisition::setReady(bool val, QString text)
198 {
199  mReady = val;
200  mInfoText = text;
201 
202  emit readinessChanged();
203 }
204 
206 {
208  {
209  report("Already recording a session, stop before trying to start a new record session.");
210  return;
211  }
212 
213  mCurrentContext = context;
214 
215  if (session)
216  mLatestSession = session;
217  else
218  mLatestSession.reset(new cx::RecordSession(mPluginData->getNewSessionId(), category));
219 
220  mLatestSession->startNewInterval();
221 
222  reporter()->playStartSound();
223  this->setState(AcquisitionService::sRUNNING);
224  emit started();
225 }
226 
228 {
230  {
231  return;
232  }
233 
234  mLatestSession->stopLastInterval();
235  if (!mPluginData->getRecordSession(mLatestSession->getUid()))
236  mPluginData->addRecordSession(mLatestSession);
237  reporter()->playStopSound();
238  this->setState(AcquisitionService::sNOT_RUNNING);
239  emit acquisitionStopped();
240 }
241 
243 {
245  {
246  return;
247  }
248  reporter()->playCancelSound();
249  mLatestSession->cancelLastInterval();
250  mLatestSession.reset();
251  mCurrentContext = AcquisitionService::TYPES();
252  this->setState(AcquisitionService::sNOT_RUNNING);
253  emit cancelled();
254 }
255 
257 {
258  this->setState(AcquisitionService::sPOST_PROCESSING);
259 }
260 
262 {
263  this->setState(AcquisitionService::sNOT_RUNNING);
264 }
265 
266 void Acquisition::setState(AcquisitionService::STATE newState)
267 {
268  AcquisitionService::STATE lastState = mCurrentState;
269  mCurrentState = newState;
270 
271 // // emit some helper signals
272 // if (lastState!=AcquisitionService::sRUNNING && newState==AcquisitionService::sRUNNING)
273 // emit started();
274 // else if (lastState==AcquisitionService::sRUNNING && newState!=AcquisitionService::sRUNNING && mLatestSession)
275 // emit acquisitionStopped();
276 // else if (lastState==AcquisitionService::sRUNNING && newState!=AcquisitionService::sRUNNING && !mLatestSession)
277 // emit cancelled();
278 
279  emit stateChanged();
280 }
281 
282 VisServicesPtr Acquisition::getServices()
283 {
284  return this->getPluginData()->getServices();
285 }
286 
287 }
Acqusition services abstract interface.
boost::shared_ptr< class UsReconstructionService > UsReconstructionServicePtr
ReporterPtr reporter()
Definition: cxReporter.cpp:36
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
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:69
RecordSessionPtr getRecordSession(QString uid)
Acquisition(AcquisitionDataPtr pluginData, QObject *parent=0)
emitting tracking data
Definition: cxTool.h:77
bool isReady(AcquisitionService::TYPES) const
void acquisitionStopped()
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr