CustusX  22.04-rc5
An IGT application
cxFileReaderWriterService.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 
14 #include "cxNullDeleter.h"
15 #include <QFileInfo>
16 #include <QUuid>
18 #include "cxTime.h"
19 #include "cxUtilHelpers.h"
20 
21 namespace cx
22 {
23 
25 {
26  static FileReaderWriterServicePtr mNull;
27  if (!mNull)
28  mNull.reset(new FileReaderWriterServiceNull, null_deleter());
29  return mNull;
30 }
31 
32 //----------------------------
33 
34 bool sameSuffix(QString suffix1, QString suffix2)
35 {
36  return (suffix1.compare(suffix2, Qt::CaseInsensitive) == 0 || suffix1.compare(suffix2, Qt::CaseInsensitive) == 0);
37 }
38 
39 FileReaderWriterImplService::FileReaderWriterImplService(QString name, QString canReadDataType, QString canWriteDataType, QString fileSuffix, PatientModelServicePtr patientModelService) :
40  mName(name),
41  mCanReadDataType(canReadDataType),
42  mCanWriteDataType(canWriteDataType),
43  mFileSuffix(fileSuffix),
44  mPatientModelService(patientModelService)
45 {
46  this->setObjectName(mName);
47 }
48 
50 {
51  return mName;
52 }
53 
55 {
56  return mFileSuffix;
57 }
58 
59 bool FileReaderWriterImplService::canReadInternal(const QString &type, const QString &filename) const
60 {
61  QString fileType = QFileInfo(filename).suffix();
62  bool rightFileType = sameSuffix(fileType, mFileSuffix);
63  bool rightDataType = (type == mCanReadDataType);
64  return rightFileType;
65 }
66 
67 bool FileReaderWriterImplService::canWriteInternal(const QString &type, const QString &filename) const
68 {
69  QString fileType = QFileInfo(filename).suffix();
70  bool rightFileType = sameSuffix(fileType, mFileSuffix);
71  bool rightDataType = (type == mCanWriteDataType);
72  return rightFileType;
73 }
74 
75 DataPtr FileReaderWriterImplService::createData(QString type, QString filename, QString name) const
76 {
77  QFileInfo fileInfo(filename);
78  QString strippedFilename = changeExtension(fileInfo.fileName(), "");
79  QString unique = QUuid::createUuid().toString();
80  //std::cout << "unique: " << unique.toStdString().c_str() << std::endl;
81  QString uid = strippedFilename + "_" + unique + "_" +QDateTime::currentDateTime().toString(timestampSecondsFormat());
82  if(name.isEmpty())
83  name = strippedFilename;
84 
85  DataPtr data = mPatientModelService->createData(type, uid, name);
86 
87  return data;
88 }
89 
90 
91 }
virtual QString canWriteDataType() const =0
static FileReaderWriterServicePtr getNullObject()
DataPtr createData(QString type, QString filename, QString name="") const
bool sameSuffix(QString suffix1, QString suffix2)
FileReaderWriterImplService(QString name, QString canReadDataType, QString canWriteDataType, QString fileSuffix, PatientModelServicePtr patientModelService)
bool canReadInternal(const QString &type, const QString &filename) const
QString timestampSecondsFormat()
Definition: cxTime.cpp:18
boost::shared_ptr< class Data > DataPtr
boost::shared_ptr< class FileReaderWriterService > FileReaderWriterServicePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
QString changeExtension(QString name, QString ext)
virtual QString canReadDataType() const =0
bool canWriteInternal(const QString &type, const QString &filename) const
Namespace for all CustusX production code.