diff --git a/Plugins/org.mitk.annotations/src/mitkAnnotationNodeSelectionListener.cpp b/Plugins/org.mitk.annotations/src/mitkAnnotationNodeSelectionListener.cpp new file mode 100644 index 0000000000..630de56540 --- /dev/null +++ b/Plugins/org.mitk.annotations/src/mitkAnnotationNodeSelectionListener.cpp @@ -0,0 +1,48 @@ +/*=================================================================== + +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" + +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); +} diff --git a/Plugins/org.mitk.annotations/src/mitkAnnotationNodeSelectionListener.h b/Plugins/org.mitk.annotations/src/mitkAnnotationNodeSelectionListener.h new file mode 100644 index 0000000000..e9dae6321d --- /dev/null +++ b/Plugins/org.mitk.annotations/src/mitkAnnotationNodeSelectionListener.h @@ -0,0 +1,40 @@ +/*=================================================================== + +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" + +namespace mitk +{ + class AnnotationNodeSelectionListener : public berry::ISelectionListener, public itk::Object + { + public: + itkFactorylessNewMacro(AnnotationNodeSelectionListener); + void SelectionChanged(const berry::IWorkbenchPart::Pointer &part, + const berry::ISelection::ConstPointer &selection) override; + + DataNode::Pointer GetSelectedDataNode() const; + + private: + AnnotationNodeSelectionListener(); + + WeakPointer m_SelectedDataNode; + }; +} // namespace mitk + +#endif // MITKANNOTATIONNODESELECTIONLISTENER_H