Fraxinus  16.5.0-fx-rc7
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  CustomMetaImagePtr customReader = CustomMetaImage::create(mSession->getRootFolder() + "/" + iter->second->getFilename());
116  customReader->setTransform(iter->second->get_rMd());
117  }
118 
119 }
120 
122 {
123  if (settings()->value("Automation/autoSave").toBool())
124  mSession->save();
125 }
126 
127 void PatientData::exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
128 {
129  QString targetFolder = mSession->getRootFolder() + "/Export/"
130  + QDateTime::currentDateTime().toString(timestampSecondsFormat());
131 
132  DataManager::ImagesMap images = mDataManager->getImages();
133  for (DataManager::ImagesMap::iterator iter = images.begin(); iter != images.end(); ++iter)
134  {
135  iter->second->save(targetFolder);
136  }
137 
138  DataManager::MeshMap meshes = mDataManager->getMeshes();
139  for (DataManager::MeshMap::iterator iter = meshes.begin(); iter != meshes.end(); ++iter)
140  {
141  MeshPtr mesh = iter->second;
142 
143  Transform3D rMd = mesh->get_rMd();
145  Transform3D sMd = sMr * rMd;
146 
147  vtkPolyDataPtr poly = mesh->getTransformedPolyData(sMd);
148  // create a copy with the SAME UID as the original. Do not load this one into the datamanager!
149  mesh = mDataManager->getDataFactory()->createSpecific<Mesh>(mesh->getUid(), mesh->getName());
150  mesh->setVtkPolyData(poly);
151  mesh->setFilename("Images");
152  mesh->save(targetFolder);
153  }
154 
155  report("Exported patient data to " + targetFolder + ".");
156 }
157 
158 DataPtr PatientData::importData(QString fileName, QString &infoText)
159 {
160  if (fileName.isEmpty())
161  {
162  QString text = "Import canceled";
163  report(text);
164  infoText = "<font color=red>" + text + "</font>";
165  return DataPtr();
166  }
167 
168  QFileInfo fileInfo(fileName);
169  QString strippedFilename = changeExtension(fileInfo.fileName(), "");
170  QString uid = strippedFilename + "_" + QDateTime::currentDateTime().toString(timestampSecondsFormat());
171 
172  if (mDataManager->getData(uid))
173  {
174  QString text = "Data with uid " + uid + " already exists. Import canceled.";
175  reportWarning(text);
176  infoText = "<font color=red>" + text + "</font>";
177  return DataPtr();
178  }
179 
180  // Read files before copy
181  DataPtr data = mDataManager->loadData(uid, fileName);
182  if (!data)
183  {
184  QString text = "Error with data file: " + fileName + " Import canceled.";
185  reportWarning(text);
186  infoText = "<font color=red>" + text + "</font>";
187  return DataPtr();
188  }
189  data->setAcquisitionTime(QDateTime::currentDateTime());
190 
191  data->save(mSession->getRootFolder());
192 
193  // remove redundant line breaks
194  infoText = infoText.split("<br>", QString::SkipEmptyParts).join("<br>");
195 
196  return data;
197 }
198 
199 void PatientData::removeData(QString uid)
200 {
201  mDataManager->removeData(uid, this->getActivePatientFolder());
202 }
203 
204 } // namespace cx
205 
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)