CustusX  15.4.0-beta
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(bool niftiFormat)
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();
145  if (niftiFormat)
146  {
147  rMd = rMd * createTransformRotateZ(M_PI); // convert back to NIFTI format
148  report("Nifti export: rotated data " + mesh->getName() + " 180* around Z-axis.");
149  }
150 
151  vtkPolyDataPtr poly = mesh->getTransformedPolyData(rMd);
152  // create a copy with the SAME UID as the original. Do not load this one into the datamanager!
153  mesh = mDataManager->getDataFactory()->createSpecific<Mesh>(mesh->getUid(), mesh->getName());
154  mesh->setVtkPolyData(poly);
155  mesh->setFilename("Images");
156  mesh->save(targetFolder);
157  }
158 
159  report("Exported patient data to " + targetFolder + ".");
160 }
161 
162 DataPtr PatientData::importData(QString fileName, QString &infoText)
163 {
164  if (fileName.isEmpty())
165  {
166  QString text = "Import canceled";
167  report(text);
168  infoText = "<font color=red>" + text + "</font>";
169  return DataPtr();
170  }
171 
172  QFileInfo fileInfo(fileName);
173  QString strippedFilename = changeExtension(fileInfo.fileName(), "");
174  QString uid = strippedFilename + "_" + QDateTime::currentDateTime().toString(timestampSecondsFormat());
175 
176  if (mDataManager->getData(uid))
177  {
178  QString text = "Data with uid " + uid + " already exists. Import canceled.";
179  reportWarning(text);
180  infoText = "<font color=red>" + text + "</font>";
181  return DataPtr();
182  }
183 
184  // Read files before copy
185  DataPtr data = mDataManager->loadData(uid, fileName);
186  if (!data)
187  {
188  QString text = "Error with data file: " + fileName + " Import canceled.";
189  reportWarning(text);
190  infoText = "<font color=red>" + text + "</font>";
191  return DataPtr();
192  }
193  data->setAcquisitionTime(QDateTime::currentDateTime());
194 
195  data->save(mSession->getRootFolder());
196 
197  // remove redundant line breaks
198  infoText = infoText.split("<br>", QString::SkipEmptyParts).join("<br>");
199 
200  return data;
201 }
202 
203 void PatientData::removeData(QString uid)
204 {
205  mDataManager->removeData(uid, this->getActivePatientFolder());
206 }
207 
208 } // namespace cx
209 
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.
std::map< QString, MeshPtr > MeshMap
Definition: cxDataManager.h:74
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
Definition: cxProbeSector.h:47
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
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
void setVtkPolyData(const vtkPolyDataPtr &polyData)
Definition: cxMesh.cpp:94
void exportPatient(bool niftiFormat)
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)
Transform3D createTransformRotateZ(const double angle)
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
#define M_PI
static CustomMetaImagePtr create(QString filename)