diff --git a/Plugins/org.mitk.annotations/src/mitkAnnotationPresetLoader.cpp b/Plugins/org.mitk.annotations/src/mitkAnnotationPresetLoader.cpp new file mode 100644 index 0000000000..3a057bf437 --- /dev/null +++ b/Plugins/org.mitk.annotations/src/mitkAnnotationPresetLoader.cpp @@ -0,0 +1,87 @@ +/*=================================================================== + +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 "mitkAnnotationPresetLoader.h" +#include "mitkAnnotationFactory.h" +#include "mitkAnnotationJsonReaderWriter.h" +#include "mitkProviderRegistry.h" +#include "mitkStdAnnotationDataProviders.h" + +auto mitk::AnnotationPresetLoader::SelectionChangeAction() +{ + return [](const itk::Object *caller, const itk::EventObject &event) { + const auto dataNodeProvider = dynamic_cast(caller); + if (dataNodeProvider) + { + return dataNodeProvider->GetSelectedDataNode(); + } + return itk::SmartPointer(nullptr); + }; +} + +void mitk::AnnotationPresetLoader::SetupDefaultProviders() +{ + auto providers = ProviderRegistry::GetInstance(); + providers->AddDataProviders( + {AnnotationDataProviderFactory::DefaultSliceNumberProvider(), + AnnotationDataProviderFactory::DefaultTimeStepProvider(), + AnnotationDataProviderFactory::DefaultDataNodeProvider("DataNodeProvider", + AnnotationNodeSelectionListener::New(), + itk::ModifiedEvent().MakeObject(), + SelectionChangeAction())}); +} + +void mitk::AnnotationPresetLoader::SaveCurrentConfiguration(const std::string &fileName) +{ + try + { + AnnotationJsonReaderWriter::WriteAnnotationsToJsonFile(fileName, m_ManagedAnnotations); + } + catch (const std::exception &e) + { + MITK_ERROR << "Exception while saving json configuration. The save file probably wasn't written or updated"; + MITK_ERROR << e.what(); + } +} + +void mitk::AnnotationPresetLoader::LoadPreset(const std::string &fileName) +{ + SetupDefaultProviders(); + + // TODO move propertyProviders to StdAnnotationDataProvidersClass + // TODO move this logic to a seperate class + // TODO move data node change listener to seperate class + // TODO fix issue with viewless plugin + // TODO add cpp file to reader writer + // TODO move all default action and event functions to a seperate class + // TODO make the tests great again + // TODO fix workbench closing crash (hint: destructing DataNodeProvider twice?) + + auto annotationConfigurations = std::vector{}; + try + { + annotationConfigurations = AnnotationJsonReaderWriter::ReadAnnotationsFromJsonFile(fileName); + } + catch (const std::exception &e) + { + MITK_ERROR << "Exception while loading json configuration"; + MITK_ERROR << e.what(); + } + + m_ManagedAnnotations = AnnotationFactory::CreateAnnotationsFromConfiguration(annotationConfigurations); + + AnnotationFactory::RegisterAnnotations(m_ManagedAnnotations); +} diff --git a/Plugins/org.mitk.annotations/src/mitkAnnotationPresetLoader.h b/Plugins/org.mitk.annotations/src/mitkAnnotationPresetLoader.h new file mode 100644 index 0000000000..9233c360db --- /dev/null +++ b/Plugins/org.mitk.annotations/src/mitkAnnotationPresetLoader.h @@ -0,0 +1,37 @@ +/*=================================================================== + +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 MITKANNOTATIONPRESETLOADER_H +#define MITKANNOTATIONPRESETLOADER_H +#include "mitkAnnotationNodeSelectionListener.h" +#include "mitkDynamicAnnotation.h" +#include + +namespace mitk +{ + class AnnotationPresetLoader + { + public: + static auto SelectionChangeAction(); + void SetupDefaultProviders(); + void SaveCurrentConfiguration(const std::string &fileName); + void LoadPreset(const std::string &fileName); + + private: + std::unique_ptr m_NodeSelectionListener; + std::vector m_ManagedAnnotations; + }; +} // namespace mitk +#endif // MITKANNOTATIONPRESETLOADER_H