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