diff --git a/Core/Code/IO/mitkAbstractFileReader.cpp b/Core/Code/IO/mitkAbstractFileReader.cpp index 502ec53e5b..671ed33a8a 100644 --- a/Core/Code/IO/mitkAbstractFileReader.cpp +++ b/Core/Code/IO/mitkAbstractFileReader.cpp @@ -1,177 +1,179 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include mitk::AbstractFileReader::AbstractFileReader() : m_Priority (0) , m_PrototypeFactory(NULL) { } mitk::AbstractFileReader::~AbstractFileReader() { delete m_PrototypeFactory; } mitk::AbstractFileReader::AbstractFileReader(const mitk::AbstractFileReader& other) : m_Extension(other.m_Extension) , m_Description(other.m_Description) , m_Priority(other.m_Priority) , m_Options(other.m_Options) , m_PrototypeFactory(NULL) { } mitk::AbstractFileReader::AbstractFileReader(const std::string& extension, const std::string& description) : m_Extension (extension) , m_Description (description) , m_Priority (0) , m_PrototypeFactory(NULL) { } ////////////////////// Reading ///////////////////////// std::vector< itk::SmartPointer > mitk::AbstractFileReader::Read(const std::string& path, mitk::DataStorage* /*ds*/) { if (! itksys::SystemTools::FileExists(path.c_str())) mitkThrow() << "File '" + path + "' not found."; std::ifstream stream; stream.open(path.c_str()); return this->Read(stream); } std::vector< itk::SmartPointer > mitk::AbstractFileReader::Read(const std::istream& stream, mitk::DataStorage* ds) { // Create a temporary file and copy the data to it std::ofstream tmpOutputStream; std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile(tmpOutputStream); tmpOutputStream << stream.rdbuf(); tmpOutputStream.close(); // Now read from the temporary file std::vector< itk::SmartPointer > result = this->Read(tmpFilePath, ds); std::remove(tmpFilePath.c_str()); return result; } //////////// µS Registration & Properties ////////////// us::ServiceRegistration mitk::AbstractFileReader::RegisterService(us::ModuleContext* context) { if (m_PrototypeFactory) return us::ServiceRegistration(); struct PrototypeFactory : public us::PrototypeServiceFactory { mitk::AbstractFileReader* const m_Prototype; PrototypeFactory(mitk::AbstractFileReader* prototype) : m_Prototype(prototype) {} us::InterfaceMap GetService(us::Module* /*module*/, const us::ServiceRegistrationBase& /*registration*/) { return us::MakeInterfaceMap(m_Prototype->Clone()); } void UngetService(us::Module* /*module*/, const us::ServiceRegistrationBase& /*registration*/, const us::InterfaceMap& service) { delete us::ExtractInterface(service); } }; m_PrototypeFactory = new PrototypeFactory(this); us::ServiceProperties props = this->GetServiceProperties(); return context->RegisterService(m_PrototypeFactory, props); } us::ServiceProperties mitk::AbstractFileReader::GetServiceProperties() { if ( m_Extension.empty() ) MITK_WARN << "Registered a Reader with no extension defined (m_Extension is empty). Reader will not be found by calls from ReaderManager.)"; if ( m_Description.empty() ) MITK_WARN << "Registered a Reader with no description defined (m_Description is empty). Reader will have no human readable extension information in FileDialogs.)"; std::transform(m_Extension.begin(), m_Extension.end(), m_Extension.begin(), ::tolower); us::ServiceProperties result; result[mitk::IFileReader::PROP_EXTENSION] = m_Extension; result[mitk::IFileReader::PROP_DESCRIPTION] = m_Description; result[us::ServiceConstants::SERVICE_RANKING()] = m_Priority; for (mitk::IFileReader::OptionList::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { if (it->second) result[it->first] = std::string("true"); else result[it->first] = std::string("false"); } return result; } //////////////////////// Options /////////////////////// mitk::IFileReader::OptionList mitk::AbstractFileReader::GetOptions() const { return m_Options; } void mitk::AbstractFileReader::SetOptions(const mitk::IFileReader::OptionList& options) { if (options.size() != m_Options.size()) MITK_WARN << "Number of set Options differs from Number of available Options, which is a sign of false usage. Please consult documentation"; m_Options = options; } ////////////////// MISC ////////////////// bool mitk::AbstractFileReader::CanRead(const std::string& path) const { // Default implementation only checks if extension is correct std::string pathEnd = path.substr( path.length() - m_Extension.length(), m_Extension.length() ); return (m_Extension == pathEnd); } -float mitk::AbstractFileReader::GetProgress() const +void mitk::AbstractFileReader::AddProgressCallback(const mitk::MessageAbstractDelegate& callback) +{ +} + +void mitk::AbstractFileReader::RemoveProgressCallback(const mitk::MessageAbstractDelegate& callback) { - // Default implementation always returns 1 (finished) - return 1; } ////////////////// µS related Getters ////////////////// int mitk::AbstractFileReader::GetPriority() const { return m_Priority; } std::string mitk::AbstractFileReader::GetExtension() const { return m_Extension; } std::string mitk::AbstractFileReader::GetDescription() const { return m_Description; } diff --git a/Core/Code/IO/mitkAbstractFileReader.h b/Core/Code/IO/mitkAbstractFileReader.h index 0e90d9a33e..92c6623307 100644 --- a/Core/Code/IO/mitkAbstractFileReader.h +++ b/Core/Code/IO/mitkAbstractFileReader.h @@ -1,97 +1,99 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 #define AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 // Macro #include // MITK #include #include // Microservices #include #include #include namespace us { struct PrototypeServiceFactory; } namespace mitk { //##Documentation //## @brief Interface class of readers that read from files //## @ingroup Process class MITK_CORE_EXPORT AbstractFileReader : public mitk::IFileReader { public: virtual std::vector< itk::SmartPointer > Read(const std::string& path, mitk::DataStorage *ds = 0 ); virtual std::vector< itk::SmartPointer > Read(const std::istream& stream, mitk::DataStorage *ds = 0 ) = 0; virtual int GetPriority() const; virtual std::string GetExtension() const; virtual std::string GetDescription() const; virtual mitk::IFileReader::OptionList GetOptions() const; virtual void SetOptions(const mitk::IFileReader::OptionList& options); virtual bool CanRead(const std::string& path) const; - virtual float GetProgress() const; + virtual void AddProgressCallback(const mitk::MessageAbstractDelegate& callback); + + virtual void RemoveProgressCallback(const mitk::MessageAbstractDelegate& callback); us::ServiceRegistration RegisterService(us::ModuleContext* context = us::GetModuleContext()); protected: AbstractFileReader(); ~AbstractFileReader(); AbstractFileReader(const AbstractFileReader& other); AbstractFileReader(const std::string& extension, const std::string& description); // Minimal Service Properties: ALWAYS SET THESE IN CONSTRUCTOR OF DERIVED CLASSES! std::string m_Extension; std::string m_Description; int m_Priority; /** * \brief Options supported by this reader. Set sensible default values! * * Can be left emtpy if no special options are required. */ mitk::IFileReader::OptionList m_Options; virtual us::ServiceProperties GetServiceProperties(); private: us::PrototypeServiceFactory* m_PrototypeFactory; virtual mitk::IFileReader* Clone() const = 0; }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::AbstractFileReader, "org.mitk.services.FileReader") #endif /* AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/Interfaces/mitkIFileReader.h b/Core/Code/Interfaces/mitkIFileReader.h index 5a90214181..0630591272 100644 --- a/Core/Code/Interfaces/mitkIFileReader.h +++ b/Core/Code/Interfaces/mitkIFileReader.h @@ -1,141 +1,142 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef IFileReader_H_HEADER_INCLUDED_C1E7E521 #define IFileReader_H_HEADER_INCLUDED_C1E7E521 // Macro #include #include // Microservices #include +// MITK +#include + // STL #include namespace mitk { class BaseData; class DataStorage; } namespace itk { template class SmartPointer; } namespace mitk { /** * \brief The common interface of all FileReader. * * This interface defines the Methods necessary for the FileReaderManager * to interact with its FileReaders. To implement a new Filereader, it is * recommended to derive from FileReaderAbstract instead of from the Interface, * as the abstract class already implements most of the functions and also makes sure * that your reader will be managed by the FileReaderManager. */ struct MITK_CORE_EXPORT IFileReader { virtual ~IFileReader(); typedef std::pair FileServiceOption; typedef std::vector OptionNames; typedef std::vector OptionList; /** * \brief Reads the specified file and returns its contents. */ virtual std::vector< itk::SmartPointer > Read(const std::string& path, mitk::DataStorage* ds = 0) = 0; /** * \brief Reads the specified input stream and returns its contents. */ virtual std::vector< itk::SmartPointer > Read(const std::istream& stream, mitk::DataStorage* ds = 0) = 0; /** * \brief Returns the priority which defined how 'good' the FileReader can handle it's file format. * * Default is zero and should only be chosen differently for a reason. * The priority is intended to be used by the MicroserviceFramework to determine * which reader to use if several equivalent readers have been found. * It may be used to replace a default reader from MITK in your own project. * E.g. if you want to use your own reader for *.nrrd files instead of the default, * implement it and give it a higher priority than zero. */ virtual int GetPriority() const = 0 ; /** * \brief returns the file extension that this FileReader is able to handle. * * Please enter only the characters after the fullstop, e.g "nrrd" is correct * while "*.nrrd" and ".nrrd" are incorrect. */ virtual std::string GetExtension() const = 0; /** * \brief Returns a human readable description of the file format. * * This will be used in FileDialogs for example. */ virtual std::string GetDescription() const = 0; /** * \brief returns a list of the supported Options * * Inititally, the reader contains a set of standard options. These can be retrieved, * manipulated and set again. To activate or deactivate an option, just set it's bool * value accordingly. All supported options are in the default set, it does not * make sense to add strings artificially after retrieving a reader - the specific * implementation decides which options it wants to support. * If no options are supported, an empty list is returned. */ virtual OptionList GetOptions() const = 0; /** * \brief Sets the options for this reader * * The best way to use this method is to retireve the options via GetOptions, manipulate the bool values, * and then set the options again. */ virtual void SetOptions(const OptionList& options) = 0; /** * \brief Returns true if this writer can confirm that it can read this file and false otherwise. */ virtual bool CanRead(const std::string& path) const = 0; - /** - * \brief Returns a value between 0 and 1 depending on the progress of the read. - * This method need not necessarily be implemented meaningfully, always returning zero is accepted. - */ - virtual float GetProgress() const = 0; + virtual void AddProgressCallback(const mitk::MessageAbstractDelegate& callback) = 0; + + virtual void RemoveProgressCallback(const mitk::MessageAbstractDelegate& callback) = 0; // Microservice properties static const std::string PROP_EXTENSION; static const std::string PROP_DESCRIPTION; static const std::string PROP_IS_LEGACY; // Microservice names for defined properties static const std::string OPTION_READ_AS_BINARY; static const std::string OPTION_READ_MULTIPLE_FILES; protected: }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::IFileReader, "org.mitk.IFileReader") #endif /* IFileReader_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/Interfaces/mitkIFileWriter.h b/Core/Code/Interfaces/mitkIFileWriter.h index 7e96b18f05..004fc9bbef 100644 --- a/Core/Code/Interfaces/mitkIFileWriter.h +++ b/Core/Code/Interfaces/mitkIFileWriter.h @@ -1,102 +1,105 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef IFileWriter_H_HEADER_INCLUDED_C1E7E521 #define IFileWriter_H_HEADER_INCLUDED_C1E7E521 // Macro #include #include // Microservices #include +// MITK +#include + // STL #include namespace mitk { class BaseData; } namespace mitk { /** * \brief The common interface of all FileWriters. * * This interface defines the methods necessary for the FileWriterManager * to interact with its FileWriters. To implement a new FileWriter, it is * recommended to derive from FileWriterAbstract instead of directly from this Interface, * as the abstract class already implements most of the methods and also makes sure that your writer * will be managed by the FileWriterManager. */ struct MITK_CORE_EXPORT IFileWriter { virtual ~IFileWriter(); typedef std::pair FileServiceOption; typedef std::vector OptionList; typedef std::vector OptionNames; virtual void Write(const BaseData* data, const std::string& path ) = 0; virtual void Write(const BaseData* data, std::ostream& stream ) = 0; /** * \brief returns the file extension that this FileWriter is able to handle. * * Please return only the characters after the fullstop, e.g "nrrd" is correct * while "*.nrrd" and ".nrrd" are incorrect. */ virtual std::string GetExtension() const = 0; /** * \brief returns the itk classname that this FileWriter is able to handle. */ virtual std::string GetSupportedBasedataType() const = 0; /** * \brief returns a list of the supported Options * * Options are strings that are treated as flags when passed to the write method. */ virtual OptionList GetOptions() const = 0; virtual void SetOptions(const OptionList& options) = 0; /** * \brief Returns true if this reader can confirm that it can write \c data and false otherwise. */ virtual bool CanWrite(const BaseData* data) const = 0; /** * \brief Returns a value between 0 and 1 depending on the progress of the writing process. * This method need not necessarily be implemented meaningfully, always returning zero is accepted. */ virtual float GetProgress() const = 0; // Microservice properties static const std::string PROP_EXTENSION; static const std::string PROP_BASEDATA_TYPE; static const std::string PROP_DESCRIPTION; static const std::string PROP_IS_LEGACY; protected: }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::IFileWriter, "org.mitk.IFileWriter") #endif /* IFileWriter_H_HEADER_INCLUDED_C1E7E521 */