Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxPatientData.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 "cxPatientData.h"
34 
35 #include <QDomDocument>
36 #include <QFile>
37 #include <QDir>
38 #include <QTimer>
39 #include <QTextStream>
40 #include <QApplication>
41 
42 #include "cxTime.h"
43 #include "cxLogger.h"
44 #include "cxUtilHelpers.h"
45 #include "cxCustomMetaImage.h"
46 #include "cxMesh.h"
47 
48 #include "cxSettings.h"
49 
50 #include <vtkPolyData.h>
51 #include <vtkPointData.h>
52 
53 #include "cxDataManager.h"
54 #include "cxImage.h"
55 #include "cxTypeConversions.h"
56 #include "cxConfig.h"
58 #include "cxXMLNodeWrapper.h"
59 #include "cxDataFactory.h"
60 
61 namespace cx
62 {
63 
64 
66  mDataManager(dataManager),
67  mSession(session)
68 {
70  connect(mSession.get(), &SessionStorageService::cleared, this, &PatientData::onCleared);
71 // connect(mSession.get(), &SessionStorageService::cleared, this, &PatientData::cleared);
72  connect(mSession.get(), &SessionStorageService::isLoading, this, &PatientData::onSessionLoad);
73  connect(mSession.get(), &SessionStorageService::isSaving, this, &PatientData::onSessionSave);
74 }
75 
77 {}
78 
80 {
81  return mSession->getRootFolder();
82 }
83 
85 {
86  return mSession->isValid();
87 }
88 
89 void PatientData::onCleared()
90 {
91  mDataManager->clear();
92 }
93 
94 void PatientData::onSessionLoad(QDomElement &node)
95 {
96  XMLNodeParser root(node);
97  QDomElement dataManagerNode = root.descend("managers/datamanager").node().toElement();
98 
99  if (!dataManagerNode.isNull())
100  mDataManager->parseXml(dataManagerNode, mSession->getRootFolder());
101 }
102 
103 void PatientData::onSessionSave(QDomElement &node)
104 {
105  XMLNodeAdder root(node);
106  QDomElement managerNode = root.descend("managers").node().toElement();
107 
108  mDataManager->addXml(managerNode);
109 
110  // save position transforms into the mhd files.
111  // This hack ensures data files can be used in external programs without an explicit export.
112  DataManager::ImagesMap images = mDataManager->getImages();
113  for (DataManager::ImagesMap::iterator iter = images.begin(); iter != images.end(); ++iter)
114  {
115  if(!iter->second->getFilename().isEmpty())
116  {
117  CustomMetaImagePtr customReader = CustomMetaImage::create(mSession->getRootFolder() + "/" + iter->second->getFilename());
118  customReader->setTransform(iter->second->get_rMd());
119  }
120  }
121 
122 }
123 
125 {
126  if (settings()->value("Automation/autoSave").toBool())
127  mSession->save();
128 }
129 
130 void PatientData::exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
131 {
132  QString targetFolder = mSession->getRootFolder() + "/Export/"
133  + QDateTime::currentDateTime().toString(timestampSecondsFormat());
134 
135  DataManager::ImagesMap images = mDataManager->getImages();
136  for (DataManager::ImagesMap::iterator iter = images.begin(); iter != images.end(); ++iter)
137  {
138  iter->second->save(targetFolder);
139  }
140 
141  DataManager::MeshMap meshes = mDataManager->getMeshes();
142  for (DataManager::MeshMap::iterator iter = meshes.begin(); iter != meshes.end(); ++iter)
143  {
144  MeshPtr mesh = iter->second;
145 
146  Transform3D rMd = mesh->get_rMd();
148  Transform3D sMd = sMr * rMd;
149 
150  vtkPolyDataPtr poly = mesh->getTransformedPolyData(sMd);
151  // create a copy with the SAME UID as the original. Do not load this one into the datamanager!
152  mesh = mDataManager->getDataFactory()->createSpecific<Mesh>(mesh->getUid(), mesh->getName());
153  mesh->setVtkPolyData(poly);
154  mesh->setFilename("Images");
155  mesh->save(targetFolder);
156  }
157 
158  report("Exported patient data to " + targetFolder + ".");
159 }
160 
161 DataPtr PatientData::importData(QString fileName, QString &infoText)
162 {
163  if (fileName.isEmpty())
164  {
165  QString text = "Import canceled";
166  report(text);
167  infoText = "<font color=red>" + text + "</font>";
168  return DataPtr();
169  }
170 
171  QFileInfo fileInfo(fileName);
172  QString strippedFilename = changeExtension(fileInfo.fileName(), "");
173  QString uid = strippedFilename + "_" + QDateTime::currentDateTime().toString(timestampSecondsFormat());
174 
175  if (mDataManager->getData(uid))
176  {
177  QString text = "Data with uid " + uid + " already exists. Import canceled.";
178  reportWarning(text);
179  infoText = "<font color=red>" + text + "</font>";
180  return DataPtr();
181  }
182 
183  // Read files before copy
184  DataPtr data = mDataManager->loadData(uid, fileName);
185  if (!data)
186  {
187  QString text = "Error with data file: " + fileName + " Import canceled.";
188  reportWarning(text);
189  infoText = "<font color=red>" + text + "</font>";
190  return DataPtr();
191  }
192  data->setAcquisitionTime(QDateTime::currentDateTime());
193 
194  data->save(mSession->getRootFolder());
195 
196  // remove redundant line breaks
197  infoText = infoText.split("<br>", QString::SkipEmptyParts).join("<br>");
198 
199  return data;
200 }
201 
202 void PatientData::removeData(QString uid)
203 {
204  mDataManager->removeData(uid, this->getActivePatientFolder());
205 }
206 
207 } // namespace cx
208 
QString getActivePatientFolder() const
void isLoading(QDomElement &root)
emitted while loading a session. Xml storage is available, getRootFolder() is set to loaded value...
A mesh data set.
Definition: cxMesh.h:61
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
cxResource_EXPORT Transform3D createTransformFromReferenceToExternal(PATIENT_COORDINATE_SYSTEM external)
void exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
std::map< QString, MeshPtr > MeshMap
Definition: cxDataManager.h:74
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
DataPtr importData(QString fileName, QString &infoText)
Import data into CustusX.
boost::shared_ptr< class DataManager > DataServicePtr
void sessionChanged()
emitted after change to a new session (new or loaded or cleared)
boost::shared_ptr< class Data > DataPtr
boost::shared_ptr< class CustomMetaImage > CustomMetaImagePtr
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
void setVtkPolyData(const vtkPolyDataPtr &polyData)
Definition: cxMesh.cpp:100
std::map< QString, ImagePtr > ImagesMap
Definition: cxDataManager.h:73
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
void patientChanged()
void cleared()
emitted when session is cleared, before isLoading is called
QString changeExtension(QString name, QString ext)
void report(QString msg)
Definition: cxLogger.cpp:90
void removeData(QString uid)
PatientData(DataServicePtr dataManager, SessionStorageServicePtr session)
bool isPatientValid() const
boost::shared_ptr< class Mesh > MeshPtr
void isSaving(QDomElement &root)
xml storage is available
virtual ~PatientData()
boost::shared_ptr< class SessionStorageService > SessionStorageServicePtr
static CustomMetaImagePtr create(QString filename)