Fraxinus  18.10
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxPatientData.h"
13 
14 #include <QDomDocument>
15 #include <QFile>
16 #include <QDir>
17 #include <QTimer>
18 #include <QTextStream>
19 #include <QApplication>
20 
21 #include "cxTime.h"
22 #include "cxLogger.h"
23 #include "cxUtilHelpers.h"
24 #include "cxCustomMetaImage.h"
25 #include "cxMesh.h"
26 
27 #include "cxSettings.h"
28 
29 #include <vtkPolyData.h>
30 #include <vtkPointData.h>
31 
32 #include "cxDataManager.h"
33 #include "cxImage.h"
34 #include "cxTypeConversions.h"
35 #include "cxConfig.h"
37 #include "cxXMLNodeWrapper.h"
38 #include "cxDataFactory.h"
39 
40 namespace cx
41 {
42 
43 
45  mDataManager(dataManager),
46  mSession(session)
47 {
49  connect(mSession.get(), &SessionStorageService::cleared, this, &PatientData::onCleared);
50 // connect(mSession.get(), &SessionStorageService::cleared, this, &PatientData::cleared);
51  connect(mSession.get(), &SessionStorageService::isLoading, this, &PatientData::onSessionLoad);
52  connect(mSession.get(), &SessionStorageService::isSaving, this, &PatientData::onSessionSave);
53 }
54 
56 {}
57 
59 {
60  return mSession->getRootFolder();
61 }
62 
64 {
65  return mSession->isValid();
66 }
67 
68 void PatientData::onCleared()
69 {
70  mDataManager->clear();
71 }
72 
73 void PatientData::onSessionLoad(QDomElement &node)
74 {
75  XMLNodeParser root(node);
76  QDomElement dataManagerNode = root.descend("managers/datamanager").node().toElement();
77 
78  if (!dataManagerNode.isNull())
79  mDataManager->parseXml(dataManagerNode, mSession->getRootFolder());
80 }
81 
82 void PatientData::onSessionSave(QDomElement &node)
83 {
84  XMLNodeAdder root(node);
85  QDomElement managerNode = root.descend("managers").node().toElement();
86 
87  mDataManager->addXml(managerNode);
88 
89  // save position transforms into the mhd files.
90  // This hack ensures data files can be used in external programs without an explicit export.
91  DataManager::ImagesMap images = mDataManager->getImages();
92  for (DataManager::ImagesMap::iterator iter = images.begin(); iter != images.end(); ++iter)
93  {
94  if(!iter->second->getFilename().isEmpty())
95  {
96  CustomMetaImagePtr customReader = CustomMetaImage::create(mSession->getRootFolder() + "/" + iter->second->getFilename());
97  customReader->setTransform(iter->second->get_rMd());
98  }
99  }
100 
101 }
102 
104 {
105  if (settings()->value("Automation/autoSave").toBool())
106  mSession->save();
107 }
108 
109 void PatientData::exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
110 {
111  QString targetFolder = mSession->getRootFolder() + "/Export/"
112  + QDateTime::currentDateTime().toString(timestampSecondsFormat());
113 
114  DataManager::ImagesMap images = mDataManager->getImages();
115  for (DataManager::ImagesMap::iterator iter = images.begin(); iter != images.end(); ++iter)
116  {
117  iter->second->save(targetFolder);
118  }
119 
120  DataManager::MeshMap meshes = mDataManager->getMeshes();
121  for (DataManager::MeshMap::iterator iter = meshes.begin(); iter != meshes.end(); ++iter)
122  {
123  MeshPtr mesh = iter->second;
124 
125  Transform3D rMd = mesh->get_rMd();
127  Transform3D sMd = sMr * rMd;
128 
129  vtkPolyDataPtr poly = mesh->getTransformedPolyDataCopy(sMd);
130  // create a copy with the SAME UID as the original. Do not load this one into the datamanager!
131  mesh = mDataManager->getDataFactory()->createSpecific<Mesh>(mesh->getUid(), mesh->getName());
132  mesh->setVtkPolyData(poly);
133  mesh->setFilename("Images");
134  mesh->save(targetFolder);
135  }
136 
137  report("Exported patient data to " + targetFolder + ".");
138 }
139 
140 DataPtr PatientData::importData(QString fileName, QString &infoText)
141 {
142  if (fileName.isEmpty())
143  {
144  QString text = "Import canceled";
145  report(text);
146  infoText = "<font color=red>" + text + "</font>";
147  return DataPtr();
148  }
149 
150  QFileInfo fileInfo(fileName);
151  QString strippedFilename = changeExtension(fileInfo.fileName(), "");
152  QString uid = strippedFilename + "_" + QDateTime::currentDateTime().toString(timestampSecondsFormat());
153 
154  if (mDataManager->getData(uid))
155  {
156  QString text = "Data with uid " + uid + " already exists. Import canceled.";
157  reportWarning(text);
158  infoText = "<font color=red>" + text + "</font>";
159  return DataPtr();
160  }
161 
162  // Read files before copy
163  DataPtr data = mDataManager->loadData(uid, fileName);
164  if (!data)
165  {
166  QString text = "Error with data file: " + fileName + " Import canceled.";
167  reportWarning(text);
168  infoText = "<font color=red>" + text + "</font>";
169  return DataPtr();
170  }
171  data->setAcquisitionTime(QDateTime::currentDateTime());
172 
173  data->save(mSession->getRootFolder());
174 
175  // remove redundant line breaks
176  infoText = infoText.split("<br>", QString::SkipEmptyParts).join("<br>");
177 
178  return data;
179 }
180 
181 void PatientData::removeData(QString uid)
182 {
183  mDataManager->removeData(uid, this->getActivePatientFolder());
184 }
185 
186 } // namespace cx
187 
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:45
XMLNodeAdder descend(QString path)
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:54
QString timestampSecondsFormat()
Definition: cxTime.cpp:18
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:70
void setVtkPolyData(const vtkPolyDataPtr &polyData)
Definition: cxMesh.cpp:91
std::map< QString, ImagePtr > ImagesMap
Definition: cxDataManager.h:53
XMLNodeParser descend(QString path)
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
void patientChanged()
void cleared()
emitted when session is cleared, before isLoading is called
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
QString changeExtension(QString name, QString ext)
void report(QString msg)
Definition: cxLogger.cpp:69
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)
Namespace for all CustusX production code.