Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxActiveData.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 "cxActiveData.h"
13 #include "cxImage.h"
14 #include "cxTrackedStream.h"
15 #include "cxPatientStorage.h"
16 #include "cxReporter.h"
17 #include "cxPatientModelService.h"
18 
19 namespace cx
20 {
21 
22 // --------------------------------------------------------
23 ActiveDataPtr ActiveData::mNull;
24 // --------------------------------------------------------
25 
29 {
30 public:
33  virtual QList<DataPtr> getActiveDataHistory() const
34  {
35  printWarning("getActiveDataHistory");
36  return QList<DataPtr>();
37  }
38  virtual DataPtr getActive() const
39  {
40  printWarning("getActive");
41  return DataPtr();
42  }
43  virtual DataPtr getActiveUsingRegexp(QString typeRegexp) const
44  {
45  printWarning("getActiveUsingRegexp(QString)");
46  return DataPtr();
47  }
49  {
50  printWarning("getDerivedActiveImage");
51  return ImagePtr();
52  }
53  virtual void setActive(DataPtr activeData)
54  {
55  printWarning("setActive");
56  }
57  virtual QString getActiveImageUid()
58  {
59  printWarning("getActiveImageUid");
60  return QString();
61  }
62  virtual void remove(DataPtr dataToBeRemoved)
63  {
64  printWarning("remove");
65  }
66  void printWarning(QString warning = "") const
67  {
68 // reportWarning("Trying to use ActiveDataNull. Function: " + warning);
69  }
71  virtual bool isNull() const
72  {
73  return true;
74  }
75 };
76 
77 //---------------------------------------------------------
78 //------- ActiveData ------------------------------------
79 //---------------------------------------------------------
80 
81 
82 ActiveData::ActiveData(PatientModelServicePtr patientModelService, SessionStorageServicePtr sessionStorageService) :
83  mStorage(new PatientStorage(sessionStorageService, "ActiveData", true)),
84  mPatientModelService(patientModelService)
85 {
86  mStorage->storeVariable("activeUids",
87  boost::bind(&ActiveData::getStringToSave, this),
88  boost::bind(&ActiveData::loadFromString, this, _1));
89 }
90 
91 QString ActiveData::getStringToSave() const
92 {
93  return this->getActiveDataStringList().join(" ");
94 }
95 
96 QStringList ActiveData::getActiveDataStringList() const
97 {
98  QStringList retval;
99  if(!mActiveData.isEmpty())
100  for(int i = 0; i < mActiveData.size(); ++i)
101  retval << mActiveData.at(i)->getUid();
102  return retval;
103 }
104 
105 void ActiveData::loadFromString(const QString activeDatas)
106 {
107  mActiveData.clear();
108  QStringList activeDataList = activeDatas.split(" ");
109  for(int i = 0; i < activeDataList.size(); ++i)
110  {
111  DataPtr data = mPatientModelService->getData(activeDataList.at(i));
112  this->setActive(data);
113  }
114 }
115 
116 QList<DataPtr> ActiveData::getActiveDataHistory() const
117 {
118  return mActiveData;
119 }
120 
122 {
123  if(mActiveData.isEmpty())
124  return DataPtr();
125  return mActiveData.last();
126 }
127 
128 DataPtr ActiveData::getActiveUsingRegexp(QString typeRegexp) const
129 {
130  QList<DataPtr> activeDatas = getActiveDataHistory(typeRegexp);
131 
132  DataPtr activeData;
133  if(!activeDatas.isEmpty())
134  activeData = activeDatas.last();
135 
136  return activeData;
137 }
138 
140 {
141  DataPtr activeData = this->getActiveUsingRegexp("image|trackedStream");
142  ImagePtr retval;
143  TrackedStreamPtr stream = boost::dynamic_pointer_cast<TrackedStream>(activeData);
144  if(stream)
145  retval = stream->getChangingImage();
146  else
147  retval = boost::dynamic_pointer_cast<Image>(activeData);
148  return retval;
149 }
150 
151 QList<DataPtr> ActiveData::getActiveDataHistory(QString typeRegexp) const
152 {
153  QRegExp reg(typeRegexp);
154  QList<DataPtr> active = this->getActiveDataHistory();
155  QList<DataPtr> retval;
156 
157  for(int i = 0; i < active.size(); ++i)
158  {
159  DataPtr current = active[i];
160  if(current->getType().contains(reg))
161  retval.push_back(current);
162  }
163 
164  return retval;
165 }
166 
168 {
169  if (!activeData)
170  return;
171  if (!mActiveData.empty() && mActiveData.last() == activeData)
172  return;
173 
174  mActiveData.removeAll(activeData);
175  mActiveData.append(activeData);
176 
177  this->emitSignals(activeData);
178 }
179 
180 void ActiveData::setActive(QString uid)
181 {
182  DataPtr dataToSet = mPatientModelService->getData(uid);
183  this->setActive(dataToSet);
184 }
185 
187 {
188  ImagePtr image = this->getActive<Image>();
189  if (image)
190  return image->getUid();
191  else
192  return "";
193 }
194 
195 void ActiveData::emitSignals(DataPtr activeData)
196 {
197  this->emitActiveDataChanged();
198  if(activeData && activeData->getType() == Image::getTypeName())
199  this->emitActiveImageChanged();
200 }
201 
202 void ActiveData::emitActiveImageChanged()
203 {
204  DataPtr activeData = ActiveData::getActive<Image>();
205  QString uid = getChangedUid(activeData);
206  emit activeImageChanged(uid);
207 }
208 
209 void ActiveData::emitActiveDataChanged()
210 {
211  DataPtr activeData = this->getActive();
212  QString uid = getChangedUid(activeData);
213  emit activeDataChanged(uid);
214 }
215 
216 QString ActiveData::getChangedUid(DataPtr activeData) const
217 {
218  QString uid = "";
219  if(activeData)
220  uid = activeData->getUid();
221  return uid;
222 }
223 
224 void ActiveData::remove(DataPtr dataToBeRemoved)
225 {
226  if(!dataToBeRemoved)
227  reportWarning("ActiveData::remove: No data");
228  if(!mActiveData.contains(dataToBeRemoved))
229  return;
230 
231  bool resendActiveImage = false;
232  bool resendActiveData = false;
233  if (this->getActive<Image>() == dataToBeRemoved)
234  resendActiveImage = true;
235  if(this->getActive() == dataToBeRemoved)
236  resendActiveData = true;
237 
238  mActiveData.removeAll(dataToBeRemoved);
239 
240  if(resendActiveImage)
241  emitActiveImageChanged();
242  if(resendActiveData)
243  emitActiveDataChanged();
244 }
245 
247 {
248  if (!mNull)
249  mNull.reset(new ActiveDataNull);
250  return mNull;
251 }
252 
253 }//cx
virtual ImagePtr getDerivedActiveImage() const
In addition to returning Image this also provides derived (changing) images from TrackedStream.
virtual QList< DataPtr > getActiveDataHistory() const
The virtual patient.
virtual ImagePtr getDerivedActiveImage() const
In addition to returning Image this also provides derived (changing) images from TrackedStream.
virtual DataPtr getActiveUsingRegexp(QString typeRegexp) const
boost::shared_ptr< class TrackedStream > TrackedStreamPtr
Helper class for storing variables in the patient file.
Provides the last active data of warious types.
Definition: cxActiveData.h:35
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:21
A data set for video streams (2D/3D).
static ActiveDataPtr getNullObject()
virtual void remove(DataPtr dataToBeRemoved)
ActiveData(PatientModelServicePtr patientModelService, SessionStorageServicePtr sessionStorageService)
boost::shared_ptr< class Data > DataPtr
virtual bool isNull() const
virtual DataPtr getActive() const
virtual void setActive(DataPtr activeData)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
void activeDataChanged(const QString &uId)
A volumetric data set.
Definition: cxImage.h:45
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
static QString getTypeName()
Definition: cxImage.h:126
ImagePtr getChangingImage()
virtual DataPtr getActiveUsingRegexp(QString typeRegexp) const
virtual QString getActiveImageUid()
virtual QList< DataPtr > getActiveDataHistory() const
void printWarning(QString warning="") const
virtual DataPtr getActive() const
static ActiveDataPtr getNullObject()
virtual QString getActiveImageUid()
virtual void setActive(DataPtr activeData)
boost::shared_ptr< class SessionStorageService > SessionStorageServicePtr
void activeImageChanged(const QString &uId)
Namespace for all CustusX production code.