diff --git a/Modules/Annotation/files.cmake b/Modules/Annotation/files.cmake index f8518740e5..bfee730668 100644 --- a/Modules/Annotation/files.cmake +++ b/Modules/Annotation/files.cmake @@ -1,23 +1,24 @@ file(GLOB_RECURSE H_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include/*") set(CPP_FILES mitkManualPlacementAnnotationRenderer.cpp mitkColorBarAnnotation.cpp mitkLabelAnnotation3D.cpp mitkLogoAnnotation.cpp mitkLayoutAnnotationRenderer.cpp mitkScaleLegendAnnotation.cpp mitkTextAnnotation2D.cpp mitkTextAnnotation3D.cpp mitkVtkLogoRepresentation.cxx mitkVtkAnnotation.cpp mitkVtkAnnotation2D.cpp mitkVtkAnnotation3D.cpp mitkAnnotationFactory.cpp mitkAnnotationJsonReaderWriter.cpp - mitkStdAnnotationDataProviders.cpp mitkDynamicAnnotation.cpp mitkDataProvider.cpp - mitkProviderRegistry.cpp + mitkAnnotationSliceProvider.cpp + mitkAnnotationDataNodeProvider.cpp + mitkAnnotationTemporoSpatialPropertyProvider.cpp ) diff --git a/Modules/Annotation/include/mitkAnnotationDataNodeProvider.h b/Modules/Annotation/include/mitkAnnotationDataNodeProvider.h new file mode 100644 index 0000000000..2736346256 --- /dev/null +++ b/Modules/Annotation/include/mitkAnnotationDataNodeProvider.h @@ -0,0 +1,43 @@ +/*=================================================================== + +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 MITKANNOTATIONDATANODEPROVIDER_H +#define MITKANNOTATIONDATANODEPROVIDER_H +#include "mitkDataProvider.h" + +namespace mitk +{ + class MITKANNOTATION_EXPORT AnnotationDataNodeProvider : public DataProvider + { + public: + mitkClassMacro(AnnotationDataNodeProvider, DataProvider); + mitkNewMacro3Param(AnnotationDataNodeProvider, + Object::Pointer, + const itk::EventObject &, + std::function); + void OnEventTriggered(const itk::Object *caller, const itk::EventObject &event) override; + + private: + AnnotationDataNodeProvider( + Object::Pointer dataSource, + const itk::EventObject &event, + std::function howToGetUpdatedValue); + std::function m_RetrieveUpdatedValue; + }; +} // namespace mitk +#endif // MITKANNOTATIONDATANODEPROVIDER_H diff --git a/Modules/Annotation/include/mitkAnnotationFactory.h b/Modules/Annotation/include/mitkAnnotationFactory.h index 5223685b96..0553a82440 100644 --- a/Modules/Annotation/include/mitkAnnotationFactory.h +++ b/Modules/Annotation/include/mitkAnnotationFactory.h @@ -1,60 +1,62 @@ /*=================================================================== 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 MITKANNOTATIONFACTORY_H #define MITKANNOTATIONFACTORY_H // mitk #include "MitkAnnotationExports.h" #include "mitkAnnotation.h" #include "mitkDynamicAnnotation.h" #include "mitkLayoutAnnotationRenderer.h" // boost #include // stl +#include "mitkAnnotationDataNodeProvider.h" #include namespace mitk { class MITKANNOTATION_EXPORT AnnotationFactory : public itk::Object { public: using AnnotationConfigVector = std::vector>; using AnnotationPtrVector = std::vector; - static AnnotationPtrVector CreateAnnotationsFromConfiguration(const AnnotationConfigVector &annotations); + static AnnotationPtrVector CreateAnnotationsFromConfiguration(const AnnotationConfigVector &annotations, + AnnotationDataNodeProvider::Pointer dataNodeProvider); static void RegisterAnnotations(const AnnotationPtrVector &annotations); private: static LayoutAnnotationRenderer::Alignment GetAlignment(const std::string &alignmentName); const std::unordered_map m_Defaults = { {AnnotationConstants::NAME, "Unnamed Annotation"}, {AnnotationConstants::DEFAULT, "NA"}, {AnnotationConstants::PREFIX, ""}, {AnnotationConstants::SUFFIX, ""}, {AnnotationConstants::TYPE, "text"}, {AnnotationConstants::PROVIDER, ""}, {AnnotationConstants::EVENT, ""}, {AnnotationConstants::ACTION, ""}, {AnnotationConstants::WINDOW, "stdmulti.widget1"}, {AnnotationConstants::LAYOUT, "TopLeft"}, }; }; } // namespace mitk #endif // MITKANNOTATIONFACTORY_H diff --git a/Modules/Annotation/include/mitkAnnotationSliceProvider.h b/Modules/Annotation/include/mitkAnnotationSliceProvider.h new file mode 100644 index 0000000000..12f1ed58d0 --- /dev/null +++ b/Modules/Annotation/include/mitkAnnotationSliceProvider.h @@ -0,0 +1,35 @@ +/*=================================================================== + +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 MITKANNOTATIONSLICEPROVIDER_H +#define MITKANNOTATIONSLICEPROVIDER_H +#include "mitkDataProvider.h" +#include + +namespace mitk +{ + class AnnotationSliceProvider : public DataProvider + { + public: + mitkClassMacro(AnnotationSliceProvider, DataProvider); + mitkNewMacro1Param(AnnotationSliceProvider, const std::string &); + + private: + AnnotationSliceProvider(const std::string &rendererId); + void OnEventTriggered(const itk::Object *caller, const itk::EventObject &event) override; + }; +} // namespace mitk +#endif // MITKANNOTATIONSLICEPROVIDER_H diff --git a/Modules/Annotation/include/mitkAnnotationTemporoSpatialPropertyProvider.h b/Modules/Annotation/include/mitkAnnotationTemporoSpatialPropertyProvider.h new file mode 100644 index 0000000000..8a5549a765 --- /dev/null +++ b/Modules/Annotation/include/mitkAnnotationTemporoSpatialPropertyProvider.h @@ -0,0 +1,60 @@ +/*=================================================================== + +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 MITKANNOTATIONTEMPOROSPATIALPROPERTYPROVIDER_H +#define MITKANNOTATIONTEMPOROSPATIALPROPERTYPROVIDER_H +#include "mitkAnnotationDataNodeProvider.h" +#include "mitkAnnotationSliceProvider.h" +#include +#include "mitkTemporoSpatialStringProperty.h" + +namespace mitk +{ + class AnnotationTemporoSpatialPropertyProvider : public DataProvider + { + public: + mitkClassMacro(AnnotationTemporoSpatialPropertyProvider, DataProvider); + mitkNewMacro3Param(AnnotationTemporoSpatialPropertyProvider, + std::string, + AnnotationDataNodeProvider::Pointer, + AnnotationSliceProvider::Pointer); + ~AnnotationTemporoSpatialPropertyProvider(); + + private: + AnnotationTemporoSpatialPropertyProvider(std::string propertyName, + AnnotationDataNodeProvider::Pointer dataNodeProvider, + AnnotationSliceProvider::Pointer sliceProvider); + + + // members for tracking the property + void OnEventTriggered(const itk::Object *caller, const itk::EventObject &event) override; + std::string m_PropertyName; + + // members for tracking the slice + AnnotationSliceProvider::Pointer m_SliceProvider; + void ObserveSliceProvider(); + unsigned long m_SliceProviderId; + void OnSliceChanged(const itk::Object *caller, const itk::EventObject &event); + unsigned int m_Slice; + + // members for tracking the dataNode + AnnotationDataNodeProvider::Pointer m_DataNodeProvider; + void ObserveDataNodeProvider(); + unsigned long m_DataNodeProviderId; + void OnDataNodeChanged(const itk::Object *caller, const itk::EventObject &event); + }; +} // namespace mitk +#endif // MITKANNOTATIONTEMPOROSPATIALPROPERTYPROVIDER_H diff --git a/Modules/Annotation/include/mitkDataProvider.h b/Modules/Annotation/include/mitkDataProvider.h index 4342f09840..95db4ada63 100644 --- a/Modules/Annotation/include/mitkDataProvider.h +++ b/Modules/Annotation/include/mitkDataProvider.h @@ -1,92 +1,57 @@ /*=================================================================== 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 MITKDATAPROVIDER_H #define MITKDATAPROVIDER_H #include "MitkAnnotationExports.h" #include "mitkAnnotation.h" #include #include #include #include -#include #include namespace mitk { class MITKANNOTATION_EXPORT DataProvider : public itk::Object { using ReturnType = boost::any; - using AdditionalInfoMap = std::unordered_map; public: mitkClassMacroItkParent(DataProvider, itk::Object); - using OnEventAction = std::function; - using DependentOnEventAction = - std::function dependentValues)>; + virtual ~DataProvider(); - mitkNewMacro4Param(DataProvider, std::string, itk::Object::Pointer, const itk::EventObject *, OnEventAction); - mitkNewMacro5Param(DataProvider, - std::string, - itk::Object::Pointer, - const itk::EventObject *, - DependentOnEventAction, - std::vector); - mitkNewMacro6Param(DataProvider, - std::string, - itk::Object::Pointer, - const itk::EventObject *, - DependentOnEventAction, - std::vector, - AdditionalInfoMap); - - void SetProviderObject(itk::Object::Pointer providerObject); - std::string GetName() const; + void SetDataSource(itk::Object::Pointer dataSource); ReturnType GetValue() const; private: - DataProvider(std::string name, - itk::Object::Pointer object, - const itk::EventObject *event, - OnEventAction action, - AdditionalInfoMap info = {{}}); - DataProvider(std::string name, - Object::Pointer object, - const itk::EventObject *event, - DependentOnEventAction action, - std::vector dependencies, - AdditionalInfoMap info = {{}}); - void OnEventTriggeredConstCaller(const itk::Object *caller, const itk::EventObject &event); - void OnEventTriggered(itk::Object *caller, const itk::EventObject &event); - void OnDependencyModified(const itk::Object *caller, const itk::EventObject &event); - void FetchAndObserveDependencies(); - - std::string m_Name; - ReturnType m_Result; - itk::Object::Pointer m_Object; - const itk::EventObject *m_DataChangedEvent; - DependentOnEventAction m_DataRetrievalAction; - std::unordered_map m_DependencyResults; - std::unordered_set m_UnknownDependencies; - std::map m_DependencyTags; - unsigned int m_ProviderTag; + void HandleEvent(const itk::Object *caller, const itk::EventObject &event); + void HandleEventNonConst(itk::Object *caller, const itk::EventObject &event); + itk::Object::Pointer m_DataSource; + + protected: + virtual void OnEventTriggered(const itk::Object *caller, const itk::EventObject &event) = 0; + Object::Pointer GetDataSource(); + + ReturnType m_Value; + const itk::EventObject *m_DataChangedEvent{}; + unsigned int m_DataSourceTag{}; + unsigned int m_ConstDataSourceTag{}; }; } // namespace mitk #endif // MITKDATAPROVIDER_H diff --git a/Modules/Annotation/include/mitkDynamicAnnotation.h b/Modules/Annotation/include/mitkDynamicAnnotation.h index bc850bebfe..2a633a999c 100644 --- a/Modules/Annotation/include/mitkDynamicAnnotation.h +++ b/Modules/Annotation/include/mitkDynamicAnnotation.h @@ -1,71 +1,70 @@ /*=================================================================== 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 MITKDYNAMICANNOTATION_H #define MITKDYNAMICANNOTATION_H #include "mitkAnnotation.h" #include "mitkDataProvider.h" #include #include namespace mitk { namespace AnnotationConstants { const std::string NAME = "name"; const std::string DEFAULT = "defaultValue"; const std::string PREFIX = "prefix"; const std::string SUFFIX = "suffix"; const std::string LAYOUT = "layout"; const std::string TYPE = "type"; const std::string PROVIDER = "provider"; const std::string EVENT = "event"; const std::string ACTION = "action"; const std::string WINDOW = "window"; const std::string SLICENAVIGATIONCONTROLLER = "SliceNavigationController"; const std::string PATH = "path"; } // namespace AnnotationConstants class MITKANNOTATION_EXPORT DynamicAnnotation : public itk::Object { public: mitkClassMacroItkParent(DynamicAnnotation, itk::Object); using ConfigMap = std::unordered_map; mitkNewMacro1Param(DynamicAnnotation, ConfigMap); void SetDataProvider(DataProvider::Pointer provider); - void SetDataProvider(std::string provider); void SetManagedAnnotation(Annotation::Pointer annotation); Annotation::Pointer GetManagedAnnotation(); void SetText(const std::string &text); std::string GetText(); std::string ConfigEntry(const std::string& key); ConfigMap GetConfiguration(); private: DynamicAnnotation(ConfigMap config, Annotation::Pointer annotation = nullptr); void OnProviderModified(); mitk::Annotation::Pointer m_Ptr; ConfigMap m_Config; DataProvider::Pointer m_DataProvider; unsigned long m_ProviderTag; }; } // namespace mitk #endif // MITKDYNAMICANNOTATION_H diff --git a/Modules/Annotation/include/mitkProviderRegistry.h b/Modules/Annotation/include/mitkProviderRegistry.h deleted file mode 100644 index be974e2b81..0000000000 --- a/Modules/Annotation/include/mitkProviderRegistry.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include "mitkCommon.h" -#include "mitkDataProvider.h" -#include - -namespace mitk -{ - class MITKANNOTATION_EXPORT ProviderRegistry : public itk::Object - { - public: - mitkClassMacroItkParent(ProviderRegistry, itk::Object); - - static Pointer GetInstance(); - - void AddDataProvider(DataProvider::Pointer provider); - void AddDataProviders(std::set providers); - std::unordered_set FetchDataProviders(std::unordered_set names); - - private: - itkFactorylessNewMacro(ProviderRegistry); - std::unordered_map m_Providers; - }; -} // namespace mitk diff --git a/Modules/Annotation/include/mitkStdAnnotationDataProviders.h b/Modules/Annotation/include/mitkStdAnnotationDataProviders.h deleted file mode 100644 index 956c896cf1..0000000000 --- a/Modules/Annotation/include/mitkStdAnnotationDataProviders.h +++ /dev/null @@ -1,39 +0,0 @@ -/*=================================================================== - -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 MITKSTDANNOTATIONDATAPROVIDERS_H -#define MITKSTDANNOTATIONDATAPROVIDERS_H -#include "mitkDataProvider.h" - -namespace mitk -{ - class MITKANNOTATION_EXPORT AnnotationDataProviderFactory - { - public: - static DataProvider::OnEventAction StdSliceAction(); - static DataProvider::OnEventAction StdTimeAction(); - - static mitk::DataProvider::Pointer DefaultSliceNumberProvider(std::string providerName = "SliceNumberProviderAxial", - const std::string &rendererId = "stdmulti.widget1"); - static mitk::DataProvider::Pointer DefaultTimeStepProvider(std::string providerName = "TimeStepProviderAxial", - const std::string &rendererId = "stdmulti.widget1"); - static mitk::DataProvider::Pointer DefaultDataNodeProvider(std::string providerName, - itk::Object::Pointer selectionListener, - const itk::EventObject *event, - DataProvider::OnEventAction selectionChangedAction); - }; -} // namespace mitk -#endif // MITKSTDANNOTATIONDATAPROVIDERS_H diff --git a/Modules/Annotation/src/mitkAnnotationDataNodeProvider.cpp b/Modules/Annotation/src/mitkAnnotationDataNodeProvider.cpp new file mode 100644 index 0000000000..50fba0e578 --- /dev/null +++ b/Modules/Annotation/src/mitkAnnotationDataNodeProvider.cpp @@ -0,0 +1,32 @@ +/*=================================================================== + +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 "mitkAnnotationDataNodeProvider.h" + +mitk::AnnotationDataNodeProvider::AnnotationDataNodeProvider( + Object::Pointer dataSource, + const itk::EventObject &event, + std::function howToGetUpdatedValue) +{ + m_RetrieveUpdatedValue = howToGetUpdatedValue; + m_DataChangedEvent = event.MakeObject(); + this->SetDataSource(dataSource); +} + +void mitk::AnnotationDataNodeProvider::OnEventTriggered(const itk::Object *caller, const itk::EventObject &event) +{ + m_Value = m_RetrieveUpdatedValue(caller, event); +} diff --git a/Modules/Annotation/src/mitkAnnotationFactory.cpp b/Modules/Annotation/src/mitkAnnotationFactory.cpp index 0ed2ca1d4d..fd9c3babd8 100644 --- a/Modules/Annotation/src/mitkAnnotationFactory.cpp +++ b/Modules/Annotation/src/mitkAnnotationFactory.cpp @@ -1,187 +1,119 @@ /*=================================================================== 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 "mitkAnnotationFactory.h" #include "mitkColorBarAnnotation.h" #include "mitkLayoutAnnotationRenderer.h" #include "mitkLogoAnnotation.h" #include "mitkScaleLegendAnnotation.h" #include "mitkTemporoSpatialStringProperty.h" #include "mitkTextAnnotation2D.h" +#include "mitkAnnotationPropertyProvider.h" +#include "mitkAnnotationSliceProvider.h" +#include "mitkAnnotationTemporoSpatialPropertyProvider.h" #include mitk::LayoutAnnotationRenderer::Alignment mitk::AnnotationFactory::GetAlignment(const std::string &alignmentName) { static const auto alignmentMap = std::unordered_map{ {"TopLeft", LayoutAnnotationRenderer::TopLeft}, {"Top", LayoutAnnotationRenderer::Top}, {"TopRight", LayoutAnnotationRenderer::TopRight}, {"Left", LayoutAnnotationRenderer::Left}, {"Right", LayoutAnnotationRenderer::Right}, {"BottomLeft", LayoutAnnotationRenderer::BottomLeft}, {"Bottom", LayoutAnnotationRenderer::Bottom}, {"BottomRight", LayoutAnnotationRenderer::BottomRight}}; const auto alignment = alignmentMap.find(alignmentName); const auto &defaultFallback = "TopLeft"; return alignment != std::end(alignmentMap) ? alignment->second : alignmentMap.at(defaultFallback); } -auto PropertyChangeAction() -{ - return [](const itk::Object *caller, // needed to listen to modified events on the property itself - const itk::EventObject &event, - std::unordered_map dependencyValues) { - // stored as additional information on provider creation - auto propName = boost::any_cast(dependencyValues["PropName"]); - - // stored as part of provider dependency - if (dependencyValues["DataNodeProvider"].type() == typeid(mitk::DataNode::Pointer)) - { - auto node = boost::any_cast(dependencyValues["DataNodeProvider"]); - auto prop = node->GetProperty(propName.c_str()); - if (prop) - { - // in case the data node changed update it on "yourself" - auto provider = boost::any_cast(dependencyValues["Self"]); - provider->SetProviderObject(prop); - - return prop->GetValueAsString(); - } - } - return std::string{}; - }; -} -auto TemporoSpatialPropertyChangeAction() -{ - return [](const itk::Object *caller, // needed to listen to modified events on the property itself - const itk::EventObject &event, - std::unordered_map dependencyValues) { - // stored as additional information on provider creation - auto propName = boost::any_cast(dependencyValues["PropName"]); - - // stored as part of provider dependency - if (dependencyValues["DataNodeProvider"].type() == typeid(mitk::DataNode::Pointer)) - { - auto node = boost::any_cast(dependencyValues["DataNodeProvider"]); - auto prop = dynamic_cast(node->GetProperty(propName.c_str())); - if (prop) - { - // in case the data node changed update it on "yourself" - auto provider = boost::any_cast(dependencyValues["Self"]); - provider->SetProviderObject(prop); - - // get slice data from provider dependency - auto slice = 0; - if (dependencyValues.find("SliceNumberProviderAxial") != std::end(dependencyValues)) - { - if (dependencyValues["SliceNumberProviderAxial"].type() == typeid(unsigned int)) - { - slice = boost::any_cast(dependencyValues["SliceNumberProviderAxial"]); - } - } - - return prop->GetValueBySlice(slice); - } - return std::string{"Property " + propName + " not found"}; - } - return std::string{"No data node selected"}; - }; -} - mitk::AnnotationFactory::AnnotationPtrVector mitk::AnnotationFactory::CreateAnnotationsFromConfiguration( - const AnnotationConfigVector &configurations) + const AnnotationConfigVector &configurations, AnnotationDataNodeProvider::Pointer dataNodeProvider) { auto result = AnnotationPtrVector{}; for (auto &config : configurations) { auto annotation = DynamicAnnotation::New(config); result.push_back(annotation); const auto &type = annotation->ConfigEntry(AnnotationConstants::TYPE); if (type == "text") { annotation->SetManagedAnnotation(mitk::TextAnnotation2D::New().GetPointer()); annotation->SetText(annotation->ConfigEntry(AnnotationConstants::DEFAULT)); } else if (type == "property") { annotation->SetManagedAnnotation(mitk::TextAnnotation2D::New().GetPointer()); annotation->SetText(annotation->ConfigEntry(AnnotationConstants::DEFAULT)); - auto propertyProvider = DataProvider::New("PropertyProvider", - nullptr, - itk::ModifiedEvent().MakeObject(), - PropertyChangeAction(), - {"DataNodeProvider"}, - {{"PropName", annotation->ConfigEntry(AnnotationConstants::PROVIDER)}}); - + DataProvider::Pointer propertyProvider = + AnnotationPropertyProvider::New(annotation->ConfigEntry(AnnotationConstants::PROVIDER), dataNodeProvider); annotation->SetDataProvider(propertyProvider); } else if (type == "dicomProperty") { annotation->SetManagedAnnotation(mitk::TextAnnotation2D::New().GetPointer()); annotation->SetText(annotation->ConfigEntry(AnnotationConstants::DEFAULT)); - auto propertyProvider = DataProvider::New("PropertyProvider", - nullptr, - itk::ModifiedEvent().MakeObject(), - TemporoSpatialPropertyChangeAction(), - {"DataNodeProvider", "SliceNumberProviderAxial", "TimeStepProvider"}, - {{"PropName", annotation->ConfigEntry(AnnotationConstants::PROVIDER)}}); - + auto sliceProvider = AnnotationSliceProvider::New(annotation->ConfigEntry(AnnotationConstants::WINDOW)); + DataProvider::Pointer propertyProvider = AnnotationTemporoSpatialPropertyProvider::New( + annotation->ConfigEntry(AnnotationConstants::PROVIDER), dataNodeProvider, sliceProvider); annotation->SetDataProvider(propertyProvider); } else if (type == "colorBar") { annotation->SetManagedAnnotation(ColorBarAnnotation::New().GetPointer()); } else if (type == "scaleLegend") { annotation->SetManagedAnnotation(ScaleLegendAnnotation::New().GetPointer()); } else if (type == "logo") { auto logoAnnotation = LogoAnnotation::New(); auto path = annotation->ConfigEntry(AnnotationConstants::PATH); logoAnnotation->SetLogoImagePath(path); logoAnnotation->LoadLogoImageFromPath(); annotation->SetManagedAnnotation(logoAnnotation.GetPointer()); } else { throw std::logic_error("Unknown or missing annotation type"); } } return result; } void mitk::AnnotationFactory::RegisterAnnotations(const AnnotationPtrVector &annotations) { for (const auto &annotation : annotations) { const auto alignment = GetAlignment(annotation->ConfigEntry(AnnotationConstants::LAYOUT)); const auto renderer = BaseRenderer::GetByName(annotation->ConfigEntry(AnnotationConstants::WINDOW)); if (renderer) { LayoutAnnotationRenderer::AddAnnotation(annotation->GetManagedAnnotation(), renderer, alignment); // TODO wait so each annotation gets a unique id see T25927 std::this_thread::sleep_for(std::chrono::seconds(2)); } } } diff --git a/Modules/Annotation/src/mitkAnnotationSliceProvider.cpp b/Modules/Annotation/src/mitkAnnotationSliceProvider.cpp new file mode 100644 index 0000000000..a5408fab2d --- /dev/null +++ b/Modules/Annotation/src/mitkAnnotationSliceProvider.cpp @@ -0,0 +1,29 @@ +/*=================================================================== + +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 "mitkAnnotationSliceProvider.h" + +mitk::AnnotationSliceProvider::AnnotationSliceProvider(const std::string& rendererId) +{ + m_DataChangedEvent = SliceNavigationController::GeometrySliceEvent(nullptr, 0).MakeObject(); + SetDataSource(BaseRenderer::GetByName(rendererId)->GetSliceNavigationController()); +} + +void mitk::AnnotationSliceProvider::OnEventTriggered(const itk::Object* caller, const itk::EventObject& event) +{ + const auto& sliceEvent = dynamic_cast(event); + m_Value = sliceEvent.GetPos(); +} diff --git a/Modules/Annotation/src/mitkAnnotationTemporoSpatialPropertyProvider.cpp b/Modules/Annotation/src/mitkAnnotationTemporoSpatialPropertyProvider.cpp new file mode 100644 index 0000000000..57ab6ac457 --- /dev/null +++ b/Modules/Annotation/src/mitkAnnotationTemporoSpatialPropertyProvider.cpp @@ -0,0 +1,83 @@ +/*=================================================================== + +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 "mitkAnnotationTemporoSpatialPropertyProvider.h" +#include "mitkTemporoSpatialStringProperty.h" + +mitk::AnnotationTemporoSpatialPropertyProvider::~AnnotationTemporoSpatialPropertyProvider() +{ + m_DataNodeProvider->RemoveObserver(m_DataNodeProviderId); + m_SliceProvider->RemoveObserver(m_SliceProviderId); +} + +mitk::AnnotationTemporoSpatialPropertyProvider::AnnotationTemporoSpatialPropertyProvider( + std::string propertyName, + AnnotationDataNodeProvider::Pointer dataNodeProvider, + AnnotationSliceProvider::Pointer sliceProvider) + : m_PropertyName(std::move(propertyName)), m_SliceProvider(sliceProvider), m_DataNodeProvider(dataNodeProvider) +{ + m_DataChangedEvent = itk::ModifiedEvent().MakeObject(); + ObserveSliceProvider(); + ObserveDataNodeProvider(); +} + +void mitk::AnnotationTemporoSpatialPropertyProvider::OnEventTriggered(const itk::Object *caller, + const itk::EventObject &event) +{ + const auto property = dynamic_cast(caller); + if (property) + { + m_Value = property->GetValueBySlice(m_Slice); + } +} + +void mitk::AnnotationTemporoSpatialPropertyProvider::OnSliceChanged(const itk::Object *caller, + const itk::EventObject &event) +{ + m_Slice = boost::any_cast(m_SliceProvider->GetValue()); + OnEventTriggered(this->GetDataSource(), itk::ModifiedEvent()); + this->Modified(); +} + +void mitk::AnnotationTemporoSpatialPropertyProvider::OnDataNodeChanged(const itk::Object *caller, + const itk::EventObject &event) +{ + const auto nodeProvider = dynamic_cast(caller); + const auto node = boost::any_cast(nodeProvider->GetValue()); + const auto prop = node->GetProperty(m_PropertyName.c_str()); + this->SetDataSource(prop); + + OnEventTriggered(this->GetDataSource(), itk::ModifiedEvent()); + this->Modified(); +} + +void mitk::AnnotationTemporoSpatialPropertyProvider::ObserveSliceProvider() +{ + // define onEventCallback + auto cmd = itk::MemberCommand::New(); + cmd->SetCallbackFunction(this, &AnnotationTemporoSpatialPropertyProvider::OnSliceChanged); + + m_SliceProviderId = m_SliceProvider->AddObserver(itk::ModifiedEvent(), cmd); +} + +void mitk::AnnotationTemporoSpatialPropertyProvider::ObserveDataNodeProvider() +{ + // define onEventCallback + auto cmd = itk::MemberCommand::New(); + cmd->SetCallbackFunction(this, &AnnotationTemporoSpatialPropertyProvider::OnDataNodeChanged); + + m_DataNodeProviderId = m_DataNodeProvider->AddObserver(itk::ModifiedEvent(), cmd); +} diff --git a/Modules/Annotation/src/mitkDataProvider.cpp b/Modules/Annotation/src/mitkDataProvider.cpp index 1855c497a9..01084f71f9 100644 --- a/Modules/Annotation/src/mitkDataProvider.cpp +++ b/Modules/Annotation/src/mitkDataProvider.cpp @@ -1,127 +1,80 @@ /*=================================================================== 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 "mitkDataProvider.h" -#include "mitkProviderRegistry.h" - -mitk::DataProvider::DataProvider(std::string name, - itk::Object::Pointer object, - const itk::EventObject *event, - OnEventAction action, - AdditionalInfoMap info) - : DataProvider(name, - object, - event, - [action](const auto *caller, const auto &event, auto noDependencies) { return action(caller, event); }, - {}, - info) -{ -} -mitk::DataProvider::DataProvider(std::string name, - Object::Pointer object, - const itk::EventObject *event, - DependentOnEventAction action, - std::vector dependencies, - AdditionalInfoMap info) - : m_Name(std::move(name)), m_DataChangedEvent(event), m_DataRetrievalAction(action) + mitk::DataProvider::~DataProvider() { - m_UnknownDependencies.insert(std::begin(dependencies), std::end(dependencies)); - m_DependencyResults.insert(std::begin(info), std::end(info)); - - m_DependencyResults["Self"] = this; - - FetchAndObserveDependencies(); - - SetProviderObject(object); -} + if (m_DataSource.IsNotNull()) + { + m_DataSource->RemoveObserver(m_DataSourceTag); + m_DataSource->RemoveObserver(m_ConstDataSourceTag); + } + } -void mitk::DataProvider::SetProviderObject(itk::Object::Pointer providerObject) +void mitk::DataProvider::SetDataSource(itk::Object::Pointer dataSource) { - if (m_Object == providerObject) + // no change when objects equal + if (m_DataSource == dataSource) return; - if (m_Object.IsNotNull()) + // remove previous source + if (m_DataSource.IsNotNull()) { - m_Object->RemoveObserver(m_ProviderTag); - m_ProviderTag = -1; + m_DataSource->RemoveObserver(m_DataSourceTag); + m_DataSourceTag = -1; + m_DataSource->RemoveObserver(m_ConstDataSourceTag); + m_ConstDataSourceTag = -1; } - m_Object = providerObject; - if (m_Object.IsNull()) + // set new source + m_DataSource = dataSource; + if (m_DataSource.IsNull()) return; - auto itkAction = itk::MemberCommand::New(); - itkAction->SetCallbackFunction(this, &DataProvider::OnEventTriggered); - m_ProviderTag = m_Object->AddObserver(*m_DataChangedEvent, itkAction); - - auto itkActionConst = itk::MemberCommand::New(); - itkActionConst->SetCallbackFunction(this, &DataProvider::OnEventTriggeredConstCaller); - //TODO don't override provider tag - m_ProviderTag = m_Object->AddObserver(*m_DataChangedEvent, itkActionConst); -} + // safety check for event + if (m_DataChangedEvent) + { + auto itkAction = itk::MemberCommand::New(); + itkAction->SetCallbackFunction(this, &DataProvider::HandleEvent); + m_DataSourceTag = m_DataSource->AddObserver(*m_DataChangedEvent, itkAction); -std::string mitk::DataProvider::GetName() const -{ - return m_Name; + auto itkActionConst = itk::MemberCommand::New(); + itkActionConst->SetCallbackFunction(this, &DataProvider::HandleEventNonConst); + m_ConstDataSourceTag = m_DataSource->AddObserver(*m_DataChangedEvent, itkActionConst); + } } boost::any mitk::DataProvider::GetValue() const { - return m_Result; + return m_Value; } -void mitk::DataProvider::OnEventTriggeredConstCaller(const itk::Object *caller, const itk::EventObject &event) +void mitk::DataProvider::HandleEvent(const itk::Object* caller, const itk::EventObject& event) { - FetchAndObserveDependencies(); - - m_Result = m_DataRetrievalAction(caller, event, m_DependencyResults); - + OnEventTriggered(caller, event); this->Modified(); } -void mitk::DataProvider::OnEventTriggered(itk::Object* caller, const itk::EventObject& event) -{ - this->OnEventTriggeredConstCaller(const_cast(caller), event); -} - -void mitk::DataProvider::OnDependencyModified(const itk::Object *caller, const itk::EventObject &event) +void mitk::DataProvider::HandleEventNonConst(itk::Object* caller, const itk::EventObject& event) { - auto provider = dynamic_cast(caller); - if (provider) - { - auto providerName = provider->GetName(); - auto providerValue = provider->GetValue(); - m_DependencyResults.insert_or_assign(providerName, providerValue); - OnEventTriggered(m_Object,* m_DataChangedEvent); - } + HandleEvent(const_cast(caller), event); } -void mitk::DataProvider::FetchAndObserveDependencies() +itk::Object::Pointer mitk::DataProvider::GetDataSource() { - if (m_UnknownDependencies.empty()) - return; - - auto registry = ProviderRegistry::GetInstance(); - auto knownProviders = registry->FetchDataProviders(m_UnknownDependencies); - for (const auto &provider : knownProviders) - { - auto itkAction = itk::MemberCommand::New(); - itkAction->SetCallbackFunction(this, &DataProvider::OnDependencyModified); - m_DependencyTags[provider->GetName()] = provider->AddObserver(itk::ModifiedEvent(), itkAction); - m_UnknownDependencies.erase(provider->GetName()); - } + return m_DataSource; } diff --git a/Modules/Annotation/src/mitkDynamicAnnotation.cpp b/Modules/Annotation/src/mitkDynamicAnnotation.cpp index 8b330c3351..0500125a47 100644 --- a/Modules/Annotation/src/mitkDynamicAnnotation.cpp +++ b/Modules/Annotation/src/mitkDynamicAnnotation.cpp @@ -1,103 +1,88 @@ /*=================================================================== 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 "mitkDynamicAnnotation.h" -#include "mitkProviderRegistry.h" mitk::DynamicAnnotation::DynamicAnnotation(std::unordered_map config, Annotation::Pointer annotation) : m_Ptr(annotation), m_Config(config), m_ProviderTag(-1) { - if (m_Config[AnnotationConstants::TYPE] != "text") - { - SetDataProvider(m_Config[AnnotationConstants::PROVIDER]); - } } void mitk::DynamicAnnotation::SetDataProvider(DataProvider::Pointer provider) { if (m_DataProvider == provider) return; if (m_DataProvider.IsNotNull()) { m_DataProvider->RemoveObserver(m_ProviderTag); m_ProviderTag = -1; } m_DataProvider = provider; if (m_DataProvider.IsNull()) return; auto itkAction = itk::SimpleMemberCommand::New(); itkAction->SetCallbackFunction(this, &DynamicAnnotation::OnProviderModified); m_ProviderTag = m_DataProvider->AddObserver(itk::ModifiedEvent(), itkAction); } -void mitk::DynamicAnnotation::SetDataProvider(std::string provider) -{ - auto registry = ProviderRegistry::GetInstance(); - auto providers = registry->FetchDataProviders({provider}); - if (!providers.empty()) - { - this->SetDataProvider(*providers.begin()); - } -} - void mitk::DynamicAnnotation::SetManagedAnnotation(Annotation::Pointer annotation) { m_Ptr = annotation; } mitk::Annotation::Pointer mitk::DynamicAnnotation::GetManagedAnnotation() { return m_Ptr; } void mitk::DynamicAnnotation::SetText(const std::string &text) { const auto &prefix = this->m_Config[AnnotationConstants::PREFIX]; const auto &suffix = this->m_Config[AnnotationConstants::SUFFIX]; this->m_Ptr->SetText(prefix + text + suffix); } std::string mitk::DynamicAnnotation::GetText() { return m_Ptr.IsNotNull() ? m_Ptr->GetText() : "Is Nullptr"; } std::string mitk::DynamicAnnotation::ConfigEntry(const std::string& key) { const auto value = m_Config.find(key); return value == std::end(m_Config) ? "No value for key " + key : value->second; } mitk::DynamicAnnotation::ConfigMap mitk::DynamicAnnotation::GetConfiguration() { return m_Config; } void mitk::DynamicAnnotation::OnProviderModified() { if (m_DataProvider.IsNotNull()) { auto result = m_DataProvider->GetValue(); if (result.type() == typeid(std::string)) { this->SetText(boost::any_cast(result)); } } } diff --git a/Modules/Annotation/src/mitkProviderRegistry.cpp b/Modules/Annotation/src/mitkProviderRegistry.cpp deleted file mode 100644 index 8c68c97170..0000000000 --- a/Modules/Annotation/src/mitkProviderRegistry.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "mitkProviderRegistry.h" - -mitk::ProviderRegistry::Pointer mitk::ProviderRegistry::GetInstance() -{ - static Pointer singleton = ProviderRegistry::New(); - return singleton; -} - -void mitk::ProviderRegistry::AddDataProvider(DataProvider::Pointer provider) -{ - m_Providers[provider->GetName()] = provider; -} - -void mitk::ProviderRegistry::AddDataProviders(std::set providers) -{ - for (const auto &provider : providers) - { - AddDataProvider(provider); - } -} - -std::unordered_set mitk::ProviderRegistry::FetchDataProviders( - std::unordered_set names) -{ - auto matches = std::unordered_set{}; - for (const auto &provider : m_Providers) - { - if (names.find(provider.second->GetName()) != std::end(names)) - { - matches.insert(provider.second); - } - } - return matches; -} diff --git a/Modules/Annotation/src/mitkStdAnnotationDataProviders.cpp b/Modules/Annotation/src/mitkStdAnnotationDataProviders.cpp deleted file mode 100644 index dfbcb612c6..0000000000 --- a/Modules/Annotation/src/mitkStdAnnotationDataProviders.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/*=================================================================== - -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 "mitkStdAnnotationDataProviders.h" - -mitk::DataProvider::OnEventAction mitk::AnnotationDataProviderFactory::StdSliceAction() -{ - return [](const itk::Object *caller, const itk::EventObject &event) { - const auto sliceEvent = dynamic_cast(&event); - if (sliceEvent) - { - return sliceEvent->GetPos(); - } - return static_cast(0); - }; -} - -mitk::DataProvider::OnEventAction mitk::AnnotationDataProviderFactory::StdTimeAction() -{ - return [](const itk::Object *caller, const itk::EventObject &event) { - const auto sliceEvent = dynamic_cast(&event); - if (sliceEvent) - { - return sliceEvent->GetPos(); - } - return static_cast(0); - }; -} - -mitk::DataProvider::Pointer mitk::AnnotationDataProviderFactory::DefaultSliceNumberProvider( - std::string providerName, const std::string &rendererId) -{ - return DataProvider::New(std::move(providerName), - BaseRenderer::GetByName(rendererId)->GetSliceNavigationController(), - SliceNavigationController::GeometrySliceEvent(nullptr, 0).MakeObject(), - StdSliceAction()); -} - -mitk::DataProvider::Pointer mitk::AnnotationDataProviderFactory::DefaultTimeStepProvider(std::string providerName, - const std::string &rendererId) -{ - return DataProvider::New(std::move(providerName), - BaseRenderer::GetByName(rendererId)->GetSliceNavigationController(), - SliceNavigationController::TimeGeometryEvent(nullptr, 0).MakeObject(), - StdTimeAction()); -} - -mitk::DataProvider::Pointer mitk::AnnotationDataProviderFactory::DefaultDataNodeProvider( - std::string providerName, - itk::Object::Pointer selectionListener, - const itk::EventObject *event, - DataProvider::OnEventAction selectionChangedAction) -{ - return DataProvider::New(std::move(providerName), selectionListener, event, selectionChangedAction); -} \ No newline at end of file diff --git a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.cpp b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.cpp index 630de56540..7e4be679dd 100644 --- a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.cpp +++ b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.cpp @@ -1,48 +1,56 @@ /*=================================================================== 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 "mitkAnnotationNodeSelectionListener.h" #include "berryISelectionService.h" #include "berryPlatformUI.h" #include "mitkDataNodeSelection.h" + mitk::AnnotationNodeSelectionListener::~AnnotationNodeSelectionListener() +{ + if (m_SelectionService) + { + m_SelectionService->RemovePostSelectionListener(this); + } + } + void mitk::AnnotationNodeSelectionListener::SelectionChanged(const berry::IWorkbenchPart::Pointer &part, const berry::ISelection::ConstPointer &selection) { const auto nodeSelection = dynamic_cast(selection.GetPointer()); if (nodeSelection) { auto nodes = nodeSelection->GetSelectedDataNodes(); if (!nodes.empty()) { m_SelectedDataNode = nodes.front(); this->Modified(); } } }; mitk::DataNode::Pointer mitk::AnnotationNodeSelectionListener::GetSelectedDataNode() const { return m_SelectedDataNode.IsExpired() ? nullptr : m_SelectedDataNode.Lock(); } mitk::AnnotationNodeSelectionListener::AnnotationNodeSelectionListener() { auto windows = berry::PlatformUI::GetWorkbench()->GetWorkbenchWindows(); auto activeWindow = berry::PlatformUI::GetWorkbench()->GetActiveWorkbenchWindow(); - auto selectionService = activeWindow->GetSelectionService(); - selectionService->AddPostSelectionListener(this); + m_SelectionService = activeWindow->GetSelectionService(); + m_SelectionService->AddPostSelectionListener(this); } diff --git a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.h b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.h index e9dae6321d..32c0b642c7 100644 --- a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.h +++ b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationNodeSelectionListener.h @@ -1,40 +1,45 @@ /*=================================================================== 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 MITKANNOTATIONNODESELECTIONLISTENER_H #define MITKANNOTATIONNODESELECTIONLISTENER_H #include "berryISelectionListener.h" #include "mitkDataNode.h" +#include "berryAbstractUICTKPlugin.h" +#include "berryISelectionService.h" +#include "berryIWorkbenchWindow.h" namespace mitk { class AnnotationNodeSelectionListener : public berry::ISelectionListener, public itk::Object { public: + ~AnnotationNodeSelectionListener(); itkFactorylessNewMacro(AnnotationNodeSelectionListener); void SelectionChanged(const berry::IWorkbenchPart::Pointer &part, const berry::ISelection::ConstPointer &selection) override; DataNode::Pointer GetSelectedDataNode() const; private: AnnotationNodeSelectionListener(); WeakPointer m_SelectedDataNode; + berry::ISelectionService* m_SelectionService; }; } // namespace mitk #endif // MITKANNOTATIONNODESELECTIONLISTENER_H diff --git a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.cpp b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.cpp index 23ba836cd8..bf69e4509e 100644 --- a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.cpp +++ b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.cpp @@ -1,116 +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. ===================================================================*/ #include "mitkAnnotationPresetLoader.h" #include "mitkAnnotationFactory.h" #include "mitkAnnotationJsonReaderWriter.h" -#include "mitkProviderRegistry.h" -#include "mitkStdAnnotationDataProviders.h" #include auto 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 AnnotationPresetLoader::SetupDefaultProviders() -{ - auto providers = mitk::ProviderRegistry::GetInstance(); - if (providers->FetchDataProviders({"SliceNumberProviderAxial", "TimeStepProvider", "DataNodeProvider"}).empty()) - { - providers->AddDataProviders( - {mitk::AnnotationDataProviderFactory::DefaultSliceNumberProvider("SliceNumberProviderAxial"), - mitk::AnnotationDataProviderFactory::DefaultTimeStepProvider("TimeStepProvider"), - mitk::AnnotationDataProviderFactory::DefaultDataNodeProvider("DataNodeProvider", - mitk::AnnotationNodeSelectionListener::New(), - itk::ModifiedEvent().MakeObject(), - SelectionChangeAction())}); - } -} - void AnnotationPresetLoader::SaveCurrentConfiguration(const std::string &fileName) { try { mitk::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 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 = mitk::AnnotationJsonReaderWriter::ReadAnnotationsFromJsonFile(fileName); } catch (const std::exception &e) { MITK_ERROR << "Exception while loading json configuration"; MITK_ERROR << e.what(); } - - m_ManagedAnnotations = mitk::AnnotationFactory::CreateAnnotationsFromConfiguration(annotationConfigurations); + const auto selectionListener = mitk::AnnotationNodeSelectionListener::New(); + const auto nodeProvider = mitk::AnnotationDataNodeProvider::New(selectionListener, itk::ModifiedEvent(), SelectionChangeAction()); + m_ManagedAnnotations = mitk::AnnotationFactory::CreateAnnotationsFromConfiguration(annotationConfigurations, nodeProvider); mitk::AnnotationFactory::RegisterAnnotations(m_ManagedAnnotations); } void AnnotationPresetLoader::SetFocus() {} void AnnotationPresetLoader::CreateQtPartControl(QWidget *parent) { m_Controls.setupUi(parent); connect(m_Controls.btn_load, &QPushButton::pressed, this, &AnnotationPresetLoader::OnLoadConfigurationPressed); } void AnnotationPresetLoader::RenderWindowPartActivated(mitk::IRenderWindowPart *renderWindowPart) {} void AnnotationPresetLoader::RenderWindowPartDeactivated(mitk::IRenderWindowPart *renderWindowPart) {} void AnnotationPresetLoader::OnLoadConfigurationPressed() { QFileDialog prompt{nullptr, "Select a json annotation layout", "", "*.json"}; prompt.exec(); auto selectedFiles = prompt.selectedFiles(); if (!selectedFiles.empty()) { auto fileName = selectedFiles.front(); m_Controls.lbl_path->setText(fileName); this->LoadPreset(fileName.toStdString()); } } diff --git a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.h b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.h index bce8180061..7a96e4fa95 100644 --- a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.h +++ b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationPresetLoader.h @@ -1,52 +1,51 @@ /*=================================================================== 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 #include #include "mitkIRenderWindowPartListener.h" #include "ui_QmitkAnnotationPresetLoaderViewControls.h" class AnnotationPresetLoader : public QmitkAbstractView, public mitk::IRenderWindowPartListener { Q_OBJECT public: static auto SelectionChangeAction(); - void SetupDefaultProviders(); void SaveCurrentConfiguration(const std::string &fileName); void LoadPreset(const std::string &fileName); void SetFocus() override; protected: void CreateQtPartControl(QWidget *parent) override; public: void RenderWindowPartActivated(mitk::IRenderWindowPart *renderWindowPart) override; void RenderWindowPartDeactivated(mitk::IRenderWindowPart *renderWindowPart) override; public slots: void OnLoadConfigurationPressed(); private: std::unique_ptr m_NodeSelectionListener; std::vector m_ManagedAnnotations; Ui::AnnotationPresetLoaderControls m_Controls; }; #endif // MITKANNOTATIONPRESETLOADER_H