NorMIT-nav  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 
cx::DataManager::MeshMap
std::map< QString, MeshPtr > MeshMap
Definition: cxDataManager.h:54
cx::PatientData::importData
DataPtr importData(QString fileName, QString &infoText)
Import data into CustusX.
Definition: cxPatientData.cpp:141
cxLogger.h
cx::Mesh::setVtkPolyData
void setVtkPolyData(const vtkPolyDataPtr &polyData)
Definition: cxMesh.cpp:92
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::PatientData::isPatientValid
bool isPatientValid() const
Definition: cxPatientData.cpp:64
cxImage.h
cxSessionStorageService.h
cx::CustomMetaImagePtr
boost::shared_ptr< class CustomMetaImage > CustomMetaImagePtr
Definition: cxCustomMetaImage.h:26
cx::report
void report(QString msg)
Definition: cxLogger.cpp:69
cx::PatientData::PatientData
PatientData(DataServicePtr dataManager, SessionStorageServicePtr session, FileManagerServicePtr fileManager)
Definition: cxPatientData.cpp:44
cxPatientData.h
cxUtilHelpers.h
cxDataFactory.h
cx::FileManagerServicePtr
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
Definition: cxLogicManager.h:31
cx::PatientData::getActivePatientFolder
QString getActivePatientFolder() const
Definition: cxPatientData.cpp:59
cx::PatientData::exportPatient
void exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
Definition: cxPatientData.cpp:110
cx::MeshPtr
boost::shared_ptr< class Mesh > MeshPtr
Definition: cxForwardDeclarations.h:48
cx::SessionStorageServicePtr
boost::shared_ptr< class SessionStorageService > SessionStorageServicePtr
Definition: cxLogicManager.h:30
cx::Transform3D
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Definition: cxLandmarkPatientRegistrationWidget.h:33
cx::PatientData::patientChanged
void patientChanged()
cxXMLNodeWrapper.h
cxCustomMetaImage.h
cxTypeConversions.h
cx::DataPtr
boost::shared_ptr< class Data > DataPtr
Definition: cxRegistrationApplicator.h:22
cx::changeExtension
QString changeExtension(QString name, QString ext)
Definition: cxUtilHelpers.cpp:53
cx::timestampSecondsFormat
QString timestampSecondsFormat()
Definition: cxTime.cpp:18
cxDataManager.h
cx::SessionStorageService::isSaving
void isSaving(QDomElement &root)
xml storage is available
cxSettings.h
cx::vtkPolyDataPtr
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Definition: cxCenterlineRegistration.h:42
cx::PatientData::~PatientData
virtual ~PatientData()
Definition: cxPatientData.cpp:56
cxTime.h
cx::Mesh
A mesh data set.
Definition: cxMesh.h:45
cx::DataServicePtr
boost::shared_ptr< class DataManager > DataServicePtr
Definition: cxDataManagerImpl.h:36
cx::SessionStorageService::isLoading
void isLoading(QDomElement &root)
emitted while loading a session. Xml storage is available, getRootFolder() is set to loaded value.
cx::DataManager::ImagesMap
std::map< QString, ImagePtr > ImagesMap
Definition: cxDataManager.h:53
cx::CustomMetaImage::create
static CustomMetaImagePtr create(QString filename)
Definition: cxCustomMetaImage.h:38
cxMesh.h
cx::SessionStorageService::sessionChanged
void sessionChanged()
emitted after change to a new session (new or loaded or cleared)
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
cx::SessionStorageService::cleared
void cleared()
emitted when session is cleared, before isLoading is called
cx::settings
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
cx::PatientData::autoSave
void autoSave()
Definition: cxPatientData.cpp:104
cx::createTransformFromReferenceToExternal
cxResource_EXPORT Transform3D createTransformFromReferenceToExternal(PATIENT_COORDINATE_SYSTEM external)
Definition: cxTransform3D.cpp:271
cx::PatientData::removeData
void removeData(QString uid)
Definition: cxPatientData.cpp:181