diff --git a/Core/Code/IO/mitkAbstractFileReader.cpp b/Core/Code/IO/mitkAbstractFileReader.cpp index 0969db04f2..9def67a04c 100644 --- a/Core/Code/IO/mitkAbstractFileReader.cpp +++ b/Core/Code/IO/mitkAbstractFileReader.cpp @@ -1,155 +1,161 @@ /*=================================================================== 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 - 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::list< 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); } //////////// µ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) + 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 (std::list::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { - result[*it] = std::string("true"); + for (std::list< mitk::FileServiceOption >::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 /////////////////////// -std::list< std::string > mitk::AbstractFileReader::GetSupportedOptions() const +std::list< mitk::FileServiceOption > mitk::AbstractFileReader::GetOptions() const { return m_Options; } +void mitk::AbstractFileReader::SetOptions(std::list< mitk::FileServiceOption > 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 { // 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 8c63c172ec..94bce0a9c0 100644 --- a/Core/Code/IO/mitkAbstractFileReader.h +++ b/Core/Code/IO/mitkAbstractFileReader.h @@ -1,93 +1,97 @@ /*=================================================================== 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 -{ + //##Documentation + //## @brief Interface class of readers that read from files + //## @ingroup Process + class MITK_CORE_EXPORT AbstractFileReader : public mitk::IFileReader + { public: virtual std::list< itk::SmartPointer > Read(const std::string& path, mitk::DataStorage *ds = 0 ); virtual std::list< 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 std::list< std::string > GetSupportedOptions() const; + virtual std::list< mitk::FileServiceOption > GetOptions() const; + + virtual void SetOptions(std::list< mitk::FileServiceOption > options); virtual bool CanRead(const std::string& path) const; virtual float GetProgress() const; us::ServiceRegistration RegisterService(us::ModuleContext* context = us::GetModuleContext()); -protected: + 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; - std::list< std::string > m_Options; // Options supported by this reader. Can be left emtpy if no special options are required + + /** + * \brief Options supported by this reader. Set sensible default values! + * + * Can be left emtpy if no special options are required. + */ + std::list< mitk::FileServiceOption > m_Options; virtual us::ServiceProperties GetServiceProperties(); -private: + 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/IO/mitkFileReaderManager.cpp b/Core/Code/IO/mitkFileReaderManager.cpp index 099ae0002f..fede2363c2 100644 --- a/Core/Code/IO/mitkFileReaderManager.cpp +++ b/Core/Code/IO/mitkFileReaderManager.cpp @@ -1,226 +1,222 @@ /*=================================================================== 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 "mitkFileReaderManager.h" // Microservices #include #include #include #include // Legacy Support #include //const std::string mitk::IFileWriter::PROP_EXTENSION = "org.mitk.services.FileWriter.Extension"; mitk::FileReaderManager::FileReaderManager() { } mitk::FileReaderManager::~FileReaderManager() { for (std::map >::iterator iter = m_ServiceObjects.begin(), - end = m_ServiceObjects.end(); iter != end; ++iter) + end = m_ServiceObjects.end(); iter != end; ++iter) { iter->second.UngetService(iter->first); } } //////////////////// READING DIRECTLY //////////////////// std::list< mitk::BaseData::Pointer > mitk::FileReaderManager::Read(const std::string& path, us::ModuleContext* context) { // Find extension std::string extension = path; extension.erase(0, path.find_last_of('.')); // Get best Reader mitk::IFileReader* reader = GetReader(extension, context); // Throw exception if no compatible reader was found if (reader == NULL) mitkThrow() << "Tried to directly read a file of type '" + extension + "' via FileReaderManager, but no reader supporting this filetype was found."; return reader->Read(path); } std::list< mitk::BaseData::Pointer > mitk::FileReaderManager::ReadAll(const std::list paths, std::list* unreadableFiles, us::ModuleContext* context) { std::list< mitk::BaseData::Pointer > result; for (std::list::const_iterator iterator = paths.begin(), end = paths.end(); iterator != end; ++iterator) { try { std::list baseDataList = Read( *iterator, context ); result.merge(baseDataList); } catch (...) { if (unreadableFiles) unreadableFiles->push_back( *iterator ); } } return result; } - //////////////////// GETTING READERS //////////////////// mitk::IFileReader* mitk::FileReaderManager::GetReader(const std::string& extension, us::ModuleContext* context ) { std::vector results = GetReaders(extension, context); if (results.empty()) return NULL; return results.front(); } std::vector mitk::FileReaderManager::GetReaders(const std::string& extension, us::ModuleContext* context ) { std::vector result; const std::vector > refs = GetReaderList(extension, context); result.reserve(refs.size()); // Translate List of ServiceRefs to List of Pointers for (std::vector >::const_iterator iter = refs.begin(), end = refs.end(); - iter != end; ++iter) + iter != end; ++iter) { us::ServiceObjects serviceObjects = context->GetServiceObjects(*iter); mitk::IFileReader* reader = serviceObjects.GetService(); m_ServiceObjects.insert(std::make_pair(reader, serviceObjects)); result.push_back(reader); } return result; } mitk::IFileReader* mitk::FileReaderManager::GetReader(const std::string& extension, const std::list& options, us::ModuleContext* context ) { std::vector matching = mitk::FileReaderManager::GetReaders(extension, options, context); if (matching.empty()) return NULL; return matching.front(); } std::vector mitk::FileReaderManager::GetReaders(const std::string& extension, const std::list& options, us::ModuleContext* context ) { const std::vector allReaders = mitk::FileReaderManager::GetReaders(extension, context); std::vector result; result.reserve(allReaders.size()); // the list is already sorted by priority. Now find reader that supports all options for (std::vector ::const_iterator iter = allReaders.begin(), end = allReaders.end(); - iter != end; ++iter) + iter != end; ++iter) { mitk::IFileReader * currentReader = *iter; // Now see if this reader supports all options. If yes, push to results if ( mitk::FileReaderManager::ReaderSupportsOptions(currentReader, options) ) { result.push_back(currentReader); } } return result; } void mitk::FileReaderManager::UngetReader(mitk::IFileReader* reader) { std::map >::iterator readerIter = - m_ServiceObjects.find(reader); + m_ServiceObjects.find(reader); if (readerIter != m_ServiceObjects.end()) { readerIter->second.UngetService(reader); m_ServiceObjects.erase(readerIter); } } void mitk::FileReaderManager::UngetReaders(const std::vector& readers) { for (std::vector::const_iterator iter = readers.begin(), end = readers.end(); - iter != end; ++iter) + iter != end; ++iter) { this->UngetReader(*iter); } } //////////////////// GENERIC INFORMATION //////////////////// std::string mitk::FileReaderManager::GetSupportedExtensions(const std::string& extension) { us::ModuleContext* context = us::GetModuleContext(); const std::vector > refs = GetReaderList(extension, context); std::vector entries; // Will contain Description + Extension (Human readable) entries.reserve(refs.size()); std::string knownExtensions; // Will contain plain list of all known extensions (for the QFileDialog entry "All Known Extensions") for (std::vector >::const_iterator iterator = refs.begin(), end = refs.end(); iterator != end; ++iterator) { // Generate List of Extensions if (iterator == refs.begin()) // First entry without semicolon knownExtensions += "*" + iterator->GetProperty(mitk::IFileReader::PROP_EXTENSION).ToString(); else // Ad semicolon for each following entry knownExtensions += "; *" + iterator->GetProperty(mitk::IFileReader::PROP_EXTENSION).ToString(); // Generate List of human readable entries composed of Description + Extension std::string entry = iterator->GetProperty(mitk::IFileReader::PROP_DESCRIPTION).ToString() + "(*" + iterator->GetProperty(mitk::IFileReader::PROP_EXTENSION).ToString() + ");;"; entries.push_back(entry); } std::sort(entries.begin(), entries.end()); std::string result = "Known Extensions (" + knownExtensions + ");;All (*)"; for (std::vector::const_iterator iterator = entries.begin(), end = entries.end(); iterator != end; ++iterator) { result += ";;" + *iterator; } return result; } //////////////////// INTERNAL CODE //////////////////// bool mitk::FileReaderManager::ReaderSupportsOptions(mitk::IFileReader* reader, const std::list& options ) { - const std::list readerOptions = reader->GetSupportedOptions(); + const std::list< mitk::FileServiceOption > readerOptions = reader->GetOptions(); if (options.empty()) return true; // if no options were requested, return true unconditionally if (readerOptions.empty()) return false; // if options were requested and reader supports no options, return false // For each of the strings in requested options, check if option is available in reader - for(std::list::const_iterator options_i = options.begin(), i_end = options.end(); options_i != i_end; ++options_i) + for(std::list< std::string >::const_iterator options_i = options.begin(), i_end = options.end(); options_i != i_end; ++options_i) { { bool optionFound = false; // Iterate over each available option from reader to check if one of them matches the current option - for(std::list::const_iterator options_j = readerOptions.begin(), j_end = readerOptions.end(); - options_j != j_end; ++options_j) + for(std::list::const_iterator options_j = readerOptions.begin(), j_end = readerOptions.end(); + options_j != j_end; ++options_j) { - if ( *options_i == *options_j ) optionFound = true; + if ( *options_i == options_j->first ) optionFound = true; } if (optionFound == false) return false; // If one option was not found, leave method and return false } } return true; // if all options have been found, return true } - - //////////////////// uS-INTERACTION //////////////////// std::vector< us::ServiceReference > mitk::FileReaderManager::GetReaderList(const std::string& extension, us::ModuleContext* context ) { // filter for class and extension std::string filter; if (!extension.empty()) { filter = us::LDAPProp(mitk::IFileReader::PROP_EXTENSION) == extension; } std::vector > result = context->GetServiceReferences(filter); std::sort(result.begin(), result.end()); std::reverse(result.begin(), result.end()); return result; } diff --git a/Core/Code/Interfaces/mitkIFileReader.h b/Core/Code/Interfaces/mitkIFileReader.h index 60f068bbe6..71c5aca4cd 100644 --- a/Core/Code/Interfaces/mitkIFileReader.h +++ b/Core/Code/Interfaces/mitkIFileReader.h @@ -1,134 +1,139 @@ /*=================================================================== 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 // 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 -{ - + typedef std::pair FileServiceOption; + + /** + * \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(); /** * \brief Reads the specified file and returns its contents. */ virtual std::list< itk::SmartPointer > Read(const std::string& path, mitk::DataStorage* ds = 0) = 0; /** * \brief Reads the specified input stream and returns its contents. */ virtual std::list< 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 * - * Options are strings that are treated as flags when passed to the read method. + * 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 std::list< mitk::FileServiceOption > 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 std::list< std::string > GetSupportedOptions() const = 0; + virtual void SetOptions(std::list< mitk::FileServiceOption > 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; // 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: - -}; - + 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/Testing/mitkFileReaderManagerTest.cpp b/Core/Code/Testing/mitkFileReaderManagerTest.cpp index da79a142fd..35344f57f3 100644 --- a/Core/Code/Testing/mitkFileReaderManagerTest.cpp +++ b/Core/Code/Testing/mitkFileReaderManagerTest.cpp @@ -1,234 +1,230 @@ /*=================================================================== 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 "mitkTestingMacros.h" #include "mitkAbstractFileReader.h" #include "mitkIFileReader.h" #include "mitkFileReaderManager.h" #include #include #include #include #include #include #include - class DummyReader : public mitk::AbstractFileReader { - public: DummyReader(const DummyReader& other) : mitk::AbstractFileReader(other) { } DummyReader(const std::string& extension, int priority) : mitk::AbstractFileReader(extension, "This is a dummy description") { m_Priority = priority; m_ServiceReg = this->RegisterService(); } ~DummyReader() { if (m_ServiceReg) m_ServiceReg.Unregister(); } using mitk::AbstractFileReader::Read; virtual std::list< itk::SmartPointer > Read(const std::istream& /*stream*/, mitk::DataStorage* /*ds*/ = 0) { std::list result; return result; } - virtual void SetOptions(const std::list< std::string >& options ) + virtual void SetOptions(const std::list< mitk::FileServiceOption >& options ) { m_Options = options; //m_Registration.SetProperties(ConstructServiceProperties()); } private: DummyReader* Clone() const { return new DummyReader(*this); } us::ServiceRegistration m_ServiceReg; - }; // End of internal dummy reader class DummyReader2 : public mitk::AbstractFileReader { - public: DummyReader2(const DummyReader2& other) : mitk::AbstractFileReader(other) { } DummyReader2(const std::string& extension, int priority) : mitk::AbstractFileReader(extension, "This is a second dummy description") { m_Priority = priority; m_ServiceReg = this->RegisterService(); } ~DummyReader2() { if (m_ServiceReg) m_ServiceReg.Unregister(); } using mitk::AbstractFileReader::Read; virtual std::list< itk::SmartPointer > Read(const std::istream& /*stream*/, mitk::DataStorage* /*ds*/ = 0) { std::list result; return result; } - virtual void SetOptions(const std::list< std::string >& options ) + virtual void SetOptions(const std::list< mitk::FileServiceOption >& options ) { m_Options = options; //m_Registration.SetProperties(ConstructServiceProperties()); } private: DummyReader2* Clone() const { return new DummyReader2(*this); } us::ServiceRegistration m_ServiceReg; - }; // End of internal dummy reader 2 /** - * TODO - */ +* TODO +*/ int mitkFileReaderManagerTest(int /*argc*/ , char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("FileReaderManager"); - // mitk::FileReaderManager::Pointer frm = mitk::FileReaderManager::New(); - // MITK_TEST_CONDITION_REQUIRED(argc == 2,"Testing FileReaderManager instantiation"); + // mitk::FileReaderManager::Pointer frm = mitk::FileReaderManager::New(); + // MITK_TEST_CONDITION_REQUIRED(argc == 2,"Testing FileReaderManager instantiation"); DummyReader testDR("test",1); DummyReader otherDR("other",1); MITK_TEST_CONDITION_REQUIRED(testDR.CanRead("/this/is/a/folder/file.test"),"Positive test of default CanRead() implementation"); MITK_TEST_CONDITION_REQUIRED(!testDR.CanRead("/this/is/a/folder/file.tes"),"Negative test of default CanRead() implementation"); mitk::FileReaderManager* readerManager = new mitk::FileReaderManager; mitk::IFileReader* returned = readerManager->GetReader("test"); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(testDR) != returned,"Testing correct retrieval of FileReader 1/2"); returned = readerManager->GetReader("other"); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(otherDR) != returned,"Testing correct retrieval of FileReader 2/2"); DummyReader mediocreTestDR("test", 20); DummyReader prettyFlyTestDR("test", 50); DummyReader2 awesomeTestDR("test", 100); returned = readerManager->GetReader("test"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returned), "Testing correct priorized retrieval of FileReader: Best reader"); // Now to give those readers some options, then we will try again - std::list options; - options.push_front("isANiceGuy"); + std::list< mitk::FileServiceOption > options; + options.push_front(std::make_pair("isANiceGuy", true)); mediocreTestDR.SetOptions(options); options.clear(); - options.push_front("canFly"); + options.push_front(std::make_pair("canFly", true)); prettyFlyTestDR.SetOptions(options); - options.push_front("isAwesome"); + options.push_front(std::make_pair("isAwesome", true)); awesomeTestDR.SetOptions(options); //note: awesomeReader canFly and isAwesome // Reset Options, use to define what we want the reader to do options.clear(); - options.push_front("canFly"); - returned = readerManager->GetReader("test", options); + std::list optionsFilter; + optionsFilter.push_front("canFly"); + returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(awesomeTestDR) != returned, "Testing correct retrieval of FileReader with Options: Best reader with options"); - options.push_front("isAwesome"); - returned = readerManager->GetReader("test", options); + optionsFilter.push_front("isAwesome"); + returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(awesomeTestDR) != returned, "Testing correct retrieval of FileReader with multiple Options: Best reader with options"); - options.clear(); - options.push_front("isANiceGuy"); - returned = readerManager->GetReader("test", options); + optionsFilter.clear(); + optionsFilter.push_front("isANiceGuy"); + returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(mediocreTestDR) != returned, "Testing correct retrieval of specific FileReader with Options: Low priority reader with specific option"); - options.push_front("canFly"); - returned = readerManager->GetReader("test", options); + optionsFilter.push_front("canFly"); + returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned == NULL, "Testing correct return of 0 value when no matching reader was found"); // Onward to test the retrieval of multiple readers std::vector< mitk::IFileReader* > returnedList; - returnedList = readerManager->GetReaders("test", options); + returnedList = readerManager->GetReaders("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returnedList.empty(), "Testing correct return of zero readers when no matching reader was found, asking for all compatibles"); - options.clear(); - options.push_back("canFly"); - returnedList = readerManager->GetReaders("test", options); + optionsFilter.clear(); + optionsFilter.push_back("canFly"); + returnedList = readerManager->GetReaders("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 2, "Testing correct return of two readers when two matching reader was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returnedList.front()), "Testing correct priorization of returned Readers with options 1/2"); - options.clear(); - options.push_back("isAwesome"); - returnedList = readerManager->GetReaders("test", options); + optionsFilter.clear(); + optionsFilter.push_back("isAwesome"); + returnedList = readerManager->GetReaders("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 1, "Testing correct return of one readers when one matching reader was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returnedList.front()), "Testing correctness of result from former query"); // And now to verify a working read chain for a mps file: //mitk::PointSetReader::Pointer psr = mitk::PointSetReader::New(); //std::list basedata; //basedata = mitk::FileReaderManager::Read("F://Build//MITK-Data//pointSet.mps"); //MITK_TEST_CONDITION_REQUIRED(basedata.size() > 0, "Testing correct read of PointSet"); // Need to instanciate the CoreObjectFactory, so legacy Readers are available mitk::CoreObjectFactory::GetInstance(); // Testing templated call to ReaderManager //mitk::PointSet::Pointer pointset = mitk::FileReaderManager::Read< mitk::PointSet >("F://Build//MITK-Data//pointSet.mps"); //MITK_TEST_CONDITION_REQUIRED(pointset.IsNotNull(), "Testing templated call of Read()"); // And now for something completely different... (Debug) - // mitk::LegacyFileReaderService::Pointer lfr = mitk::LegacyFileReaderService::New(".nrrd", "Nearly Raw Raster Data"); + // mitk::LegacyFileReaderService::Pointer lfr = mitk::LegacyFileReaderService::New(".nrrd", "Nearly Raw Raster Data"); //returned = mitk::FileReaderManager::GetReader(".nrrd"); //MITK_TEST_CONDITION_REQUIRED(lfr == returned, "Testing correct retrieval of specific FileReader with Options: Low priority reader with specific option"); //std::list image = mitk::FileReaderManager::Read("F://Build//MITK-Data//Pic2DplusT.nrrd"); //MITK_TEST_CONDITION_REQUIRED(image.size() > 0, "Testing whether image was returned or not"); //mitk::Image::Pointer image2 = dynamic_cast (image.front().GetPointer()); //MITK_TEST_CONDITION_REQUIRED(image2.IsNotNull(), "Testing if BaseData is an image"); // Delete this here because it will call the PrototypeServiceFactory::Unget() method // of the dummy readers. delete readerManager; // always end with this! MITK_TEST_END(); } diff --git a/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp b/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp index 5a1cfad956..f7dc4550cc 100644 --- a/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp +++ b/Plugins/org.mitk.gui.qt.application/src/QmitkFileOpenAction.cpp @@ -1,120 +1,130 @@ /*=================================================================== 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 "QmitkFileOpenAction.h" #include "internal/org_mitk_gui_qt_application_Activator.h" #include #include #include #include +#include + #include #include #include #include #include #include #include class QmitkFileOpenActionPrivate { public: QmitkFileOpenActionPrivate() : m_PrefServiceTracker(mitk::PluginActivator::GetContext()) {} void init ( berry::IWorkbenchWindow::Pointer window, QmitkFileOpenAction* action ) { m_PrefServiceTracker.open(); m_Window = window; action->setParent(static_cast(m_Window.Lock()->GetShell()->GetControl())); action->setText("&Open..."); action->setToolTip("Open data files (images, surfaces,...)"); QObject::connect(action, SIGNAL(triggered(bool)), action, SLOT(Run())); } berry::IPreferences::Pointer GetPreferences() const { berry::IPreferencesService* prefService = m_PrefServiceTracker.getService(); if (prefService) { return prefService->GetSystemPreferences()->Node("/General"); } return berry::IPreferences::Pointer(0); } QString getLastFileOpenPath() const { berry::IPreferences::Pointer prefs = GetPreferences(); if(prefs.IsNotNull()) { return QString::fromStdString(prefs->Get("LastFileOpenPath", "")); } return QString(); } void setLastFileOpenPath(const QString& path) const { berry::IPreferences::Pointer prefs = GetPreferences(); if(prefs.IsNotNull()) { prefs->Put("LastFileOpenPath", path.toStdString()); prefs->Flush(); } } berry::IWorkbenchWindow::WeakPtr m_Window; ctkServiceTracker m_PrefServiceTracker; }; QmitkFileOpenAction::QmitkFileOpenAction(berry::IWorkbenchWindow::Pointer window) : QAction(0), d(new QmitkFileOpenActionPrivate) { d->init(window, this); } QmitkFileOpenAction::QmitkFileOpenAction(const QIcon & icon, berry::IWorkbenchWindow::Pointer window) : QAction(0), d(new QmitkFileOpenActionPrivate) { d->init(window, this); this->setIcon(icon); } QmitkFileOpenAction::~QmitkFileOpenAction() { } void QmitkFileOpenAction::Run() { - // Ask the user for a list of files to open - QStringList fileNames = QFileDialog::getOpenFileNames(NULL, "Open", - d->getLastFileOpenPath(), - mitk::CoreObjectFactory::GetInstance()->GetFileExtensions()); - if (fileNames.empty()) - return; + QmitkFileDialog dialog; + //QmitkFileDialog dialog; + dialog.setFilter(mitk::CoreObjectFactory::GetInstance()->GetFileExtensions()); + //dialog.show(); + dialog.exec(); + + // QStringList fileNames = dialog.getOpenFileName(); + + //QStringList fileNames = QmitkFileDialog::getOpenFileNames(NULL, "Open", + //d->getLastFileOpenPath(), + // mitk::CoreObjectFactory::GetInstance()->GetFileExtensions()); + + //if (fileNames.empty()) + return; - d->setLastFileOpenPath(fileNames.front()); - mitk::WorkbenchUtil::LoadFiles(fileNames, d->m_Window.Lock()); + //d->setLastFileOpenPath(fileNames.front()); + //mitk::WorkbenchUtil::LoadFiles(fileNames, d->m_Window.Lock()); }