CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 "cxActiveData.h"
34 #include "cxImage.h"
35 #include "cxTrackedStream.h"
36 #include "cxPatientStorage.h"
37 #include "cxReporter.h"
38 #include "cxPatientModelService.h"
39 
40 namespace cx
41 {
42 
43 // --------------------------------------------------------
44 ActiveDataPtr ActiveData::mNull;
45 // --------------------------------------------------------
46 
50 {
51 public:
54  virtual QList<DataPtr> getActiveDataHistory() const
55  {
56  printWarning("getActiveDataHistory");
57  return QList<DataPtr>();
58  }
59  virtual DataPtr getActive() const
60  {
61  printWarning("getActive");
62  return DataPtr();
63  }
64  virtual DataPtr getActiveUsingRegexp(QString typeRegexp) const
65  {
66  printWarning("getActiveUsingRegexp(QString)");
67  return DataPtr();
68  }
70  {
71  printWarning("getDerivedActiveImage");
72  return ImagePtr();
73  }
74  virtual void setActive(DataPtr activeData)
75  {
76  printWarning("setActive");
77  }
78  virtual QString getActiveImageUid()
79  {
80  printWarning("getActiveImageUid");
81  return QString();
82  }
83  virtual void remove(DataPtr dataToBeRemoved)
84  {
85  printWarning("remove");
86  }
87  void printWarning(QString warning = "") const
88  {
89 // reportWarning("Trying to use ActiveDataNull. Function: " + warning);
90  }
92  virtual bool isNull() const
93  {
94  return true;
95  }
96 };
97 
98 //---------------------------------------------------------
99 //------- ActiveData ------------------------------------
100 //---------------------------------------------------------
101 
102 
104  mStorage(new PatientStorage(sessionStorageService, "ActiveData", true)),
105  mPatientModelService(patientModelService)
106 {
107  mStorage->storeVariable("activeUids",
108  boost::bind(&ActiveData::getStringToSave, this),
109  boost::bind(&ActiveData::loadFromString, this, _1));
110 }
111 
112 QString ActiveData::getStringToSave() const
113 {
114  return this->getActiveDataStringList().join(" ");
115 }
116 
117 QStringList ActiveData::getActiveDataStringList() const
118 {
119  QStringList retval;
120  if(!mActiveData.isEmpty())
121  for(int i = 0; i < mActiveData.size(); ++i)
122  retval << mActiveData.at(i)->getUid();
123  return retval;
124 }
125 
126 void ActiveData::loadFromString(const QString activeDatas)
127 {
128  mActiveData.clear();
129  QStringList activeDataList = activeDatas.split(" ");
130  for(int i = 0; i < activeDataList.size(); ++i)
131  {
132  DataPtr data = mPatientModelService->getData(activeDataList.at(i));
133  this->setActive(data);
134  }
135 }
136 
137 QList<DataPtr> ActiveData::getActiveDataHistory() const
138 {
139  return mActiveData;
140 }
141 
143 {
144  if(mActiveData.isEmpty())
145  return DataPtr();
146  return mActiveData.last();
147 }
148 
149 DataPtr ActiveData::getActiveUsingRegexp(QString typeRegexp) const
150 {
151  QList<DataPtr> activeDatas = getActiveDataHistory(typeRegexp);
152 
153  DataPtr activeData;
154  if(!activeDatas.isEmpty())
155  activeData = activeDatas.last();
156 
157  return activeData;
158 }
159 
161 {
162  DataPtr activeData = this->getActiveUsingRegexp("image|trackedStream");
163  ImagePtr retval;
164  TrackedStreamPtr stream = boost::dynamic_pointer_cast<TrackedStream>(activeData);
165  if(stream)
166  retval = stream->getChangingImage();
167  else
168  retval = boost::dynamic_pointer_cast<Image>(activeData);
169  return retval;
170 }
171 
172 QList<DataPtr> ActiveData::getActiveDataHistory(QString typeRegexp) const
173 {
174  QRegExp reg(typeRegexp);
175  QList<DataPtr> active = this->getActiveDataHistory();
176  QList<DataPtr> retval;
177 
178  for(int i = 0; i < active.size(); ++i)
179  {
180  DataPtr current = active[i];
181  if(current->getType().contains(reg))
182  retval.push_back(current);
183  }
184 
185  return retval;
186 }
187 
189 {
190  if (!activeData)
191  return;
192  if (!mActiveData.empty() && mActiveData.last() == activeData)
193  return;
194 
195  mActiveData.removeAll(activeData);
196  mActiveData.append(activeData);
197 
198  this->emitSignals(activeData);
199 }
200 
201 void ActiveData::setActive(QString uid)
202 {
203  DataPtr dataToSet = mPatientModelService->getData(uid);
204  this->setActive(dataToSet);
205 }
206 
208 {
209  ImagePtr image = this->getActive<Image>();
210  if (image)
211  return image->getUid();
212  else
213  return "";
214 }
215 
216 void ActiveData::emitSignals(DataPtr activeData)
217 {
218  this->emitActiveDataChanged();
219  if(activeData && activeData->getType() == "image")
220  this->emitActiveImageChanged();
221 }
222 
223 void ActiveData::emitActiveImageChanged()
224 {
225  DataPtr activeData = ActiveData::getActive<Image>();
226  QString uid = getChangedUid(activeData);
227  emit activeImageChanged(uid);
228 }
229 
230 void ActiveData::emitActiveDataChanged()
231 {
232  DataPtr activeData = this->getActive();
233  QString uid = getChangedUid(activeData);
234  emit activeDataChanged(uid);
235 }
236 
237 QString ActiveData::getChangedUid(DataPtr activeData) const
238 {
239  QString uid = "";
240  if(activeData)
241  uid = activeData->getUid();
242  return uid;
243 }
244 
245 void ActiveData::remove(DataPtr dataToBeRemoved)
246 {
247  if(!dataToBeRemoved)
248  reportWarning("ActiveData::remove: No data");
249  if(!mActiveData.contains(dataToBeRemoved))
250  return;
251 
252  bool resendActiveImage = false;
253  bool resendActiveData = false;
254  if (this->getActive<Image>() == dataToBeRemoved)
255  resendActiveImage = true;
256  if(this->getActive() == dataToBeRemoved)
257  resendActiveData = true;
258 
259  mActiveData.removeAll(dataToBeRemoved);
260 
261  if(resendActiveImage)
262  emitActiveImageChanged();
263  if(resendActiveData)
264  emitActiveDataChanged();
265 }
266 
268 {
269  if (!mNull)
270  mNull.reset(new ActiveDataNull);
271  return mNull;
272 }
273 
274 }//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:56
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:42
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:91
void activeDataChanged(const QString &uId)
A volumetric data set.
Definition: cxImage.h:66
cxLogicManager_EXPORT SessionStorageServicePtr sessionStorageService()
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
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)