Fraxinus  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
QString qstring_cast(const T &val)
std::vector< DataPtr > read(const QString &filename)
vtkPolyDataPtr loadVtkPolyData(QString filename)
std::vector< FileReaderWriterServicePtr > getExportersForDataType(QString dataType)
bool canLoad(const QString &type, const QString &filename)
QString getFileReaderName(const QString &filename)
bool readInto(DataPtr data, QString path)
boost::shared_ptr< class Data > DataPtr
void addFileReaderWriter(FileReaderWriterService *service)
boost::shared_ptr< class FileReaderWriterService > FileReaderWriterServicePtr
#define CX_LOG_ERROR
Definition: cxLogger.h:99
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
void removeFileReaderWriter(FileReaderWriterService *service)
#define CX_LOG_DEBUG
Definition: cxLogger.h:95
QString changeExtension(QString name, QString ext)
#define CX_LOG_WARNING
Definition: cxLogger.h:98
vtkImageDataPtr loadVtkImageData(QString filename)
virtual QString getName() const =0
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
std::vector< FileReaderWriterServicePtr > getImportersForDataType(QString dataType)
void save(DataPtr data, const QString &filename)
QString findDataTypeFromFile(QString filename)
DataPtr load(const QString &uid, const QString &filename)
Namespace for all CustusX production code.