NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
cxFileManagerServiceBase.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 
13 #include <QFileInfo>
14 #include "cxTypeConversions.h"
15 #include "cxUtilHelpers.h"
16 #include "cxNullDeleter.h"
17 #include "cxLogger.h"
18 #include "boost/bind.hpp"
19 #include "cxCoreServices.h"
20 
21 namespace cx
22 {
24 {}
25 
27 {
28 }
29 
31 {
32  return false;
33 }
34 
36 {
37  std::cout << "[TODO] error FileManagerServiceBase::canLoadDataType not implemented yet." << std::endl;
38  return QString("TODO");
39 }
40 
41 bool FileManagerServiceBase::canLoad(const QString &type, const QString &filename)
42 {
43  FileReaderWriterServicePtr reader = this->findReader(filename);
44  if (reader)
45  return reader->canRead(type, filename);
46  else
47  return false;
48 }
49 
50 DataPtr FileManagerServiceBase::load(const QString &uid, const QString &filename)
51 {
52  FileReaderWriterServicePtr reader = this->findReader(filename);
53  if (reader)
54  return reader->read(uid, filename);
55  else
56  return DataPtr();
57 }
58 
59 FileReaderWriterServicePtr FileManagerServiceBase::findReader(const QString& path, const QString& type)
60 {
61  for (std::set<FileReaderWriterServicePtr>::iterator iter = mDataReaders.begin(); iter != mDataReaders.end(); ++iter)
62  {
63  if ((*iter)->canRead(type, path))
64  return *iter;
65  }
67 }
68 
69 FileReaderWriterServicePtr FileManagerServiceBase::findWriter(const QString& path, const QString& type)
70 {
71  //TODO refactor with the findreader function..
72  for (std::set<FileReaderWriterServicePtr>::iterator iter = mDataReaders.begin(); iter != mDataReaders.end(); ++iter)
73  {
74  if ((*iter)->canWrite(type, path))
75  return *iter;
76  }
78 }
79 
81 {
83  FileReaderWriterServicePtr reader = this->findReader(filename);
84  if (reader)
85  {
86  retval = reader->loadVtkImageData(filename);
87  }
88  return retval;
89 }
90 
92 {
93  FileReaderWriterServicePtr reader = this->findReader(filename);
94  if (reader)
95  return reader->loadVtkPolyData(filename);
96  return vtkPolyDataPtr();
97 }
98 
99 std::vector<FileReaderWriterServicePtr> FileManagerServiceBase::getExportersForDataType(QString dataType)
100 {
101  std::vector<FileReaderWriterServicePtr> retval;
102  for (std::set<FileReaderWriterServicePtr>::iterator iter = mDataReaders.begin(); iter != mDataReaders.end(); ++iter)
103  {
104  if (dataType.compare((*iter)->canWriteDataType()) == 0)
105  retval.push_back(*iter);
106  }
107 
108  return retval;
109 }
110 
111 std::vector<FileReaderWriterServicePtr> FileManagerServiceBase::getImportersForDataType(QString dataType)
112 {
113  std::vector<FileReaderWriterServicePtr> retval;
114  for (std::set<FileReaderWriterServicePtr>::iterator iter = mDataReaders.begin(); iter != mDataReaders.end(); ++iter)
115  {
116  if (dataType.compare((*iter)->canReadDataType()) == 0)
117  retval.push_back(*iter);
118  }
119 
120  return retval;
121 
122 }
123 
125 {
126  FileReaderWriterServicePtr reader = this->findReader(filename);
127  if (reader)
128  return reader->canReadDataType();
129  CX_LOG_WARNING() << "FileManagerServiceBase::findDataTypeFromFile: Cannot find a file reader";
130  return "";
131 }
132 
134 {
135  bool success = false;
136  FileReaderWriterServicePtr reader = this->findReader(path, data->getType());
137  if (reader)
138  success = reader->readInto(data, path);
139 
140  if(data)
141  {
142  QFileInfo fileInfo(qstring_cast(path));
143  data->setName(changeExtension(fileInfo.fileName(), ""));
144  //data->setFilename(path); // need path even when not set explicitly: nice for testing
145  }
146  return success;
147 
148 }
149 
150 std::vector<DataPtr> FileManagerServiceBase::read(const QString &filename)
151 {
152  std::vector<DataPtr> retval;
153  FileReaderWriterServicePtr reader = this->findReader(filename);
154  if (reader)
155  retval = reader->read(filename);
156  return retval;
157 }
158 
159 void FileManagerServiceBase::save(DataPtr data, const QString &filename)
160 {
161  FileReaderWriterServicePtr writer = this->findWriter(filename);
162  if (writer)
163  return writer->write(data, filename);
164  else
165  CX_LOG_ERROR() << "Could not find writer.";
166 }
167 
169 {
170  // adding a service inside a smartpointer... not so smart, think it is fixed with null_deleter
171  mDataReaders.insert(FileReaderWriterServicePtr(service, null_deleter()));
172  CX_LOG_DEBUG() << "Adding a reader/writer: " << service->objectName() << " to: " << this;
173 }
174 
176 {
177  for(std::set<FileReaderWriterServicePtr>::iterator it = mDataReaders.begin(); it != mDataReaders.end(); )
178  {
179  if (service->getName() == (*it)->getName())
180  {
181  mDataReaders.erase(it++);
182  }
183  else
184  ++it;
185  }
186 }
187 
188 QString FileManagerServiceBase::getFileReaderName(const QString &filename)
189 {
190  FileReaderWriterServicePtr reader = this->findReader(filename);
191  if (reader)
192  return reader->getName();
193  else
194  return QString();
195 }
196 
197 } // cx
cx::null_deleter
Definition: cxNullDeleter.h:8
cx::FileManagerServiceBase::save
void save(DataPtr data, const QString &filename)
Definition: cxFileManagerServiceBase.cpp:159
cx::FileManagerServiceBase::loadVtkPolyData
vtkPolyDataPtr loadVtkPolyData(QString filename)
Definition: cxFileManagerServiceBase.cpp:91
cxLogger.h
cx::FileManagerServiceBase::canLoadDataType
QString canLoadDataType() const
Definition: cxFileManagerServiceBase.cpp:35
qstring_cast
QString qstring_cast(const T &val)
Definition: cxTypeConversions.h:46
cx::FileManagerServiceBase::~FileManagerServiceBase
virtual ~FileManagerServiceBase()
Definition: cxFileManagerServiceBase.cpp:26
cx::FileManagerServiceBase::getExportersForDataType
std::vector< FileReaderWriterServicePtr > getExportersForDataType(QString dataType)
Definition: cxFileManagerServiceBase.cpp:99
cx::FileManagerServiceBase::canLoad
bool canLoad(const QString &type, const QString &filename)
Definition: cxFileManagerServiceBase.cpp:41
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::FileManagerServiceBase::read
std::vector< DataPtr > read(const QString &filename)
Definition: cxFileManagerServiceBase.cpp:150
cx::FileReaderWriterService::getName
virtual QString getName() const =0
cx::FileManagerServiceBase::readInto
bool readInto(DataPtr data, QString path)
Definition: cxFileManagerServiceBase.cpp:133
vtkImageDataPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Definition: cxVideoConnectionWidget.h:30
cxUtilHelpers.h
cx::FileManagerServiceBase::getFileReaderName
QString getFileReaderName(const QString &filename)
Definition: cxFileManagerServiceBase.cpp:188
cx::FileManagerServiceBase::isNull
virtual bool isNull()
Definition: cxFileManagerServiceBase.cpp:30
cx::FileManagerServiceBase::addFileReaderWriter
void addFileReaderWriter(FileReaderWriterService *service)
Definition: cxFileManagerServiceBase.cpp:168
cxNullDeleter.h
cxTypeConversions.h
cx::DataPtr
boost::shared_ptr< class Data > DataPtr
Definition: cxRegistrationApplicator.h:22
cx::FileManagerServiceBase::FileManagerServiceBase
FileManagerServiceBase()
Definition: cxFileManagerServiceBase.cpp:23
cx::changeExtension
QString changeExtension(QString name, QString ext)
Definition: cxUtilHelpers.cpp:53
CX_LOG_ERROR
#define CX_LOG_ERROR
Definition: cxLogger.h:99
cxFileManagerServiceBase.h
cx::vtkPolyDataPtr
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Definition: cxCenterlineRegistration.h:42
cx::FileManagerServiceBase::loadVtkImageData
vtkImageDataPtr loadVtkImageData(QString filename)
Definition: cxFileManagerServiceBase.cpp:80
cx::FileManagerServiceBase::removeFileReaderWriter
void removeFileReaderWriter(FileReaderWriterService *service)
Definition: cxFileManagerServiceBase.cpp:175
cxCoreServices.h
CX_LOG_DEBUG
#define CX_LOG_DEBUG
Definition: cxLogger.h:95
cx::FileManagerServiceBase::getImportersForDataType
std::vector< FileReaderWriterServicePtr > getImportersForDataType(QString dataType)
Definition: cxFileManagerServiceBase.cpp:111
CX_LOG_WARNING
#define CX_LOG_WARNING
Definition: cxLogger.h:98
cx::FileReaderWriterServicePtr
boost::shared_ptr< class FileReaderWriterService > FileReaderWriterServicePtr
Definition: cxForwardDeclarations.h:118
cx::FileReaderWriterService
Definition: cxFileReaderWriterService.h:30
cx::FileManagerServiceBase::load
DataPtr load(const QString &uid, const QString &filename)
Definition: cxFileManagerServiceBase.cpp:50
cx::FileManagerServiceBase::findDataTypeFromFile
QString findDataTypeFromFile(QString filename)
Definition: cxFileManagerServiceBase.cpp:124