Fraxinus  22.09
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  mFileManagerService(fileManager)
48 {
50  connect(mSession.get(), &SessionStorageService::cleared, this, &PatientData::onCleared);
51 // connect(mSession.get(), &SessionStorageService::cleared, this, &PatientData::cleared);
52  connect(mSession.get(), &SessionStorageService::isLoading, this, &PatientData::onSessionLoad);
53  connect(mSession.get(), &SessionStorageService::isSaving, this, &PatientData::onSessionSave);
54 }
55 
57 {}
58 
60 {
61  return mSession->getRootFolder();
62 }
63 
65 {
66  return mSession->isValid();
67 }
68 
69 void PatientData::onCleared()
70 {
71  mDataManager->clear();
72 }
73 
74 void PatientData::onSessionLoad(QDomElement &node)
75 {
76  XMLNodeParser root(node);
77  QDomElement dataManagerNode = root.descend("managers/datamanager").node().toElement();
78 
79  if (!dataManagerNode.isNull())
80  mDataManager->parseXml(dataManagerNode, mSession->getRootFolder());
81 }
82 
83 void PatientData::onSessionSave(QDomElement &node)
84 {
85  XMLNodeAdder root(node);
86  QDomElement managerNode = root.descend("managers").node().toElement();
87 
88  mDataManager->addXml(managerNode);
89 
90  // save position transforms into the mhd files.
91  // This hack ensures data files can be used in external programs without an explicit export.
92  DataManager::ImagesMap images = mDataManager->getImages();
93  for (DataManager::ImagesMap::iterator iter = images.begin(); iter != images.end(); ++iter)
94  {
95  if(!iter->second->getFilename().isEmpty())
96  {
97  CustomMetaImagePtr customReader = CustomMetaImage::create(mSession->getRootFolder() + "/" + iter->second->getFilename());
98  customReader->setTransform(iter->second->get_rMd());
99  }
100  }
101 
102 }
103 
105 {
106  if (settings()->value("Automation/autoSave").toBool())
107  mSession->save();
108 }
109 
110 void PatientData::exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
111 {
112  QString targetFolder = mSession->getRootFolder() + "/Export/"
113  + QDateTime::currentDateTime().toString(timestampSecondsFormat());
114 
115  DataManager::ImagesMap images = mDataManager->getImages();
116  for (DataManager::ImagesMap::iterator iter = images.begin(); iter != images.end(); ++iter)
117  {
118  iter->second->save(targetFolder, mFileManagerService);
119  }
120 
121  DataManager::MeshMap meshes = mDataManager->getMeshes();
122  for (DataManager::MeshMap::iterator iter = meshes.begin(); iter != meshes.end(); ++iter)
123  {
124  MeshPtr mesh = iter->second;
125 
126  Transform3D rMd = mesh->get_rMd();
128  Transform3D sMd = sMr * rMd;
129 
130  vtkPolyDataPtr poly = mesh->getTransformedPolyDataCopy(sMd);
131  // create a copy with the SAME UID as the original. Do not load this one into the datamanager!
132  mesh = mDataManager->getDataFactory()->createSpecific<Mesh>(mesh->getUid(), mesh->getName());
133  mesh->setVtkPolyData(poly);
134  mesh->setFilename("Images");
135  mesh->save(targetFolder, mFileManagerService);
136  }
137 
138  report("Exported patient data to " + targetFolder + ".");
139 }
140 
141 DataPtr PatientData::importData(QString fileName, QString &infoText)
142 {
143  if (fileName.isEmpty())
144  {
145  QString text = "Import canceled";
146  report(text);
147  infoText = "<font color=red>" + text + "</font>";
148  return DataPtr();
149  }
150 
151  QFileInfo fileInfo(fileName);
152  QString strippedFilename = changeExtension(fileInfo.fileName(), "");
153  QString uid = strippedFilename + "_" + QDateTime::currentDateTime().toString(timestampSecondsFormat());
154 
155  if (mDataManager->getData(uid))
156  {
157  QString text = "Data with uid " + uid + " already exists. Import canceled.";
158  reportWarning(text);
159  infoText = "<font color=red>" + text + "</font>";
160  return DataPtr();
161  }
162 
163  // Read files before copy
164  DataPtr data = mDataManager->loadData(uid, fileName);
165  if (!data)
166  {
167  QString text = "Error with data file: " + fileName + " Import canceled.";
168  reportWarning(text);
169  infoText = "<font color=red>" + text + "</font>";
170  return DataPtr();
171  }
172  data->setAcquisitionTime(QDateTime::currentDateTime());
173  data->save(mSession->getRootFolder(), mFileManagerService);
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 
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
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
PatientData(DataServicePtr dataManager, SessionStorageServicePtr session, FileManagerServicePtr fileManager)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
void setVtkPolyData(const vtkPolyDataPtr &polyData)
Definition: cxMesh.cpp:92
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)
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.