diff --git a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.cpp b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.cpp index 9202319eae..46ab461420 100644 --- a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.cpp +++ b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.cpp @@ -1,150 +1,30 @@ /*=================================================================== 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 "mitkAnnotationsActivator.h" -#include "mitkAnnotationFactory.h" -#include "mitkAnnotationJsonReaderWriter.h" -#include "mitkDataNodeSelection.h" -#include "mitkProviderRegistry.h" - -#include "berryISelectionService.h" -#include "berryPlatformUI.h" - -#include - -mitk::NodeSelectionListener::NodeSelectionListener(std::function callback) - : m_Callback(std::move(callback)){}; - -void mitk::NodeSelectionListener::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_Callback(nodes.front()); - } - } -}; - -auto mitk::AnnotationsActivator::OnSelectionChangedCallback() -{ - return [this](DataNode::Pointer node) { - if (node.IsNotNull()) - { - m_SelectedDataNode = node; - this->Modified(); - } - }; -} - -auto mitk::AnnotationsActivator::SliceNumberAction() const -{ - return [](const itk::Object *caller, const itk::EventObject &event) { - const auto sliceEvent = dynamic_cast(&event); - if (sliceEvent) - { - return sliceEvent->GetPos(); - } - return static_cast(0); - }; -} - -auto mitk::AnnotationsActivator::SelectionChangeAction() const -{ - return [](const itk::Object *caller, const itk::EventObject &event) { - const auto dataNodeProvider = dynamic_cast(caller); - if (dataNodeProvider) - { - return dataNodeProvider->GetSelectedDataNode(); - } - return itk::SmartPointer(nullptr); - }; -} +#include "mitkAnnotationPresetLoader.h" void mitk::AnnotationsActivator::start(ctkPluginContext *context) { - m_NodeSelectionListener = std::make_unique(OnSelectionChangedCallback()); - ObserveSelectionChanges(); - - auto sliceNumberProvider = - DataProvider::New("SliceNumberProviderAxial", - BaseRenderer::GetByName("stdmulti.widget1")->GetSliceNavigationController(), - SliceNavigationController::GeometrySliceEvent(nullptr, 0).MakeObject(), - SliceNumberAction()); - auto timeStepProvider = DataProvider::New("TimeStepProvider", - BaseRenderer::GetByName("stdmulti.widget1")->GetSliceNavigationController(), - SliceNavigationController::TimeGeometryEvent(nullptr, 0).MakeObject(), - SliceNumberAction()); - auto dataNodeProvider = DataProvider::New("DataNodeProvider", this, SelectionChangeEvent(), SelectionChangeAction()); - - auto providers = ProviderRegistry::GetInstance(); - providers->AddDataProviders({sliceNumberProvider, timeStepProvider, dataNodeProvider}); - // TODO remove hardcoded string auto fileName = std::string{R"(D:\Arbeit\Programming\mitk_m\Plugins\org.mitk.annotations\resources\annotationsReduced.json)"}; - try - { - m_Annotations = AnnotationJsonReaderWriter::ReadAnnotationsFromJsonFile(fileName); - } - catch (const std::exception &e) - { - MITK_ERROR << "Exception while loading json configuration"; - MITK_ERROR << e.what(); - } - - AnnotationFactory::CreateAnnotationsFromConfiguration(m_Annotations); - - AnnotationFactory::RegisterAnnotations(m_Annotations); - - try - { - AnnotationJsonReaderWriter::WriteAnnotationsToJsonFile(fileName + "Saved", m_Annotations); - } - 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(); - } + m_AnnotationPresetLoader = std::make_unique(); + m_AnnotationPresetLoader->LoadPreset(fileName); } void mitk::AnnotationsActivator::stop(ctkPluginContext * /*context*/) {} - -mitk::DataNode::Pointer mitk::AnnotationsActivator::GetSelectedDataNode() const -{ - return m_SelectedDataNode.IsExpired() ? nullptr : m_SelectedDataNode.Lock(); -} - -void mitk::AnnotationsActivator::ObserveSelectionChanges() -{ - auto windows = berry::PlatformUI::GetWorkbench()->GetWorkbenchWindows(); - auto activeWindow = berry::PlatformUI::GetWorkbench()->GetActiveWorkbenchWindow(); - auto selectionService = activeWindow->GetSelectionService(); - - if (m_NodeSelectionListener) - { - selectionService->AddPostSelectionListener(m_NodeSelectionListener.get()); - } -} - -const itk::EventObject *mitk::AnnotationsActivator::SelectionChangeEvent() -{ - return itk::ModifiedEvent().MakeObject(); -} diff --git a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.h b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.h index dead29fcc8..715e0b0a4d 100644 --- a/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.h +++ b/Plugins/org.mitk.annotations/src/internal/mitkAnnotationsActivator.h @@ -1,81 +1,53 @@ /*=================================================================== 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 MITKANNOTATIONSACTIVATOR_H_ #define MITKANNOTATIONSACTIVATOR_H_ #include "mitkAnnotationFactory.h" #include "mitkIRenderWindowPart.h" #include "berryISelection.h" #include "berryISelectionListener.h" #include "berryIWorkbenchPart.h" #include +#include "mitkAnnotationPresetLoader.h" #include namespace mitk { - class NodeSelectionListener : public berry::ISelectionListener - { - public: - NodeSelectionListener(std::function callback); - void SelectionChanged(const berry::IWorkbenchPart::Pointer &part, - const berry::ISelection::ConstPointer &selection) override; - - private: - std::function m_Callback; - }; - /** * \ingroup org_mitk_annotations_internal * * \brief The plug-in activator for mitk annotations */ - class AnnotationsActivator : public QObject, public ctkPluginActivator, public itk::Object + class AnnotationsActivator : public QObject, public ctkPluginActivator { Q_OBJECT Q_PLUGIN_METADATA(IID "org.mitk.annotations") Q_INTERFACES(ctkPluginActivator) public: void start(ctkPluginContext *context) override; void stop(ctkPluginContext *context) override; - DataNode::Pointer GetSelectedDataNode() const; - private: - // observers and callbacks - void ObserveSelectionChanges(); - auto OnSelectionChangedCallback(); - - // events as defined in the json file - const itk::EventObject *SelectionChangeEvent(); - - // action functions as defined in the json file - auto SliceNumberAction() const; - auto SelectionChangeAction() const; - auto PropertyChangeAction() const; - - WeakPointer m_AnnotationFactory; - std::future m_AnnotationFuture; - std::unique_ptr m_NodeSelectionListener; - WeakPointer m_SelectedDataNode; - std::vector m_Annotations; + std::unique_ptr m_AnnotationPresetLoader; }; } // namespace mitk #endif /* MITKANNOTATIONSACTIVATOR_H_ */