diff --git a/Modules/Multilabel/files.cmake b/Modules/Multilabel/files.cmake index cc5f29c1e5..f05c0cb0d0 100644 --- a/Modules/Multilabel/files.cmake +++ b/Modules/Multilabel/files.cmake @@ -1,23 +1,24 @@ set(CPP_FILES mitkLabel.cpp + mitkLabelHighlightGuard.cpp mitkLabelSetImage.cpp mitkLabelSetImageConverter.cpp mitkLabelSetImageSource.cpp mitkLabelSetImageHelper.cpp mitkLabelSetImageSurfaceStampFilter.cpp mitkLabelSetImageToSurfaceFilter.cpp mitkLabelSetImageToSurfaceThreadedFilter.cpp mitkLabelSetImageVtkMapper2D.cpp mitkMultilabelObjectFactory.cpp mitkMultiLabelIOHelper.cpp mitkMultiLabelEvents.cpp mitkMultiLabelPredicateHelper.cpp mitkDICOMSegmentationPropertyHelper.cpp mitkDICOMSegmentationConstants.cpp mitkSegmentationTaskList.cpp mitkMultiLabelSegmentationVtkMapper3D.cpp ) set(RESOURCE_FILES ) diff --git a/Modules/Multilabel/mitkLabelHighlightGuard.cpp b/Modules/Multilabel/mitkLabelHighlightGuard.cpp new file mode 100644 index 0000000000..7c0f0aef7e --- /dev/null +++ b/Modules/Multilabel/mitkLabelHighlightGuard.cpp @@ -0,0 +1,95 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file. + +============================================================================*/ + +#include + +#include +#include +#include +#include + + +void mitk::LabelHighlightGuard::SetSegmentationNode(DataNode* node) +{ + auto ownNode = m_Node.Lock(); + if (node != ownNode) + { + UpdateNode(ownNode, {}, false); + m_Node = node; + m_Labels.clear(); + UpdateNode(node, m_Labels, m_HighlightInvisible); + mitk::RenderingManager::GetInstance()->RequestUpdateAll(); + } +}; + +mitk::DataNode::Pointer mitk::LabelHighlightGuard::GetSegmentationNode() const +{ + return m_Node.Lock(); +} + +void mitk::LabelHighlightGuard::SetHighlightedLabels(LabelSetImage::LabelValueVectorType labels) +{ + auto ownNode = m_Node.Lock(); + if (ownNode.IsNotNull() && labels != m_Labels) + { + m_Labels = labels; + UpdateNode(ownNode, m_Labels, m_HighlightInvisible); + mitk::RenderingManager::GetInstance()->RequestUpdateAll(); + } +} + +void mitk::LabelHighlightGuard::SetHighlightInvisibleLabels(bool highlightInvisible) +{ + auto ownNode = m_Node.Lock(); + if (highlightInvisible == m_HighlightInvisible) + return; + + m_HighlightInvisible = highlightInvisible; + + if (ownNode.IsNotNull() && !m_Labels.empty()) + { + UpdateNode(ownNode, m_Labels, m_HighlightInvisible); + mitk::RenderingManager::GetInstance()->RequestUpdateAll(); + } +} + +mitk::LabelHighlightGuard::~LabelHighlightGuard() +{ + this->SetSegmentationNode(nullptr); +} + +void mitk::LabelHighlightGuard::UpdateNode(DataNode* node, LabelSetImage::LabelValueVectorType labels, bool highlightInvisible) +{ + if (nullptr == node) + return; + + mitk::IntVectorProperty::Pointer labelProperty = dynamic_cast(node->GetNonConstProperty(PROPERTY_NAME_LABELS_HIGHLIGHTED())); + if (nullptr == labelProperty) + { + labelProperty = mitk::IntVectorProperty::New(); + node->SetProperty(PROPERTY_NAME_LABELS_HIGHLIGHTED(), labelProperty); + } + + mitk::IntVectorProperty::VectorType intValues(labels.begin(), labels.end()); + labelProperty->SetValue(intValues); + labelProperty->Modified(); //see T30386; needed because VectorProperty::SetValue does currently trigger no modified + + mitk::BoolProperty::Pointer invisibleProperty = dynamic_cast(node->GetNonConstProperty(PROPERTY_NAME_HIGHLIGHT_INVISIBLE())); + if (nullptr == invisibleProperty) + { + invisibleProperty = mitk::BoolProperty::New(); + node->SetProperty(PROPERTY_NAME_HIGHLIGHT_INVISIBLE(), invisibleProperty); + } + + invisibleProperty->SetValue(highlightInvisible); + invisibleProperty->Modified(); //see T30386; needed because VectorProperty::SetValue does currently trigger no modified +} diff --git a/Modules/Multilabel/mitkLabelHighlightGuard.h b/Modules/Multilabel/mitkLabelHighlightGuard.h new file mode 100644 index 0000000000..d8db916500 --- /dev/null +++ b/Modules/Multilabel/mitkLabelHighlightGuard.h @@ -0,0 +1,58 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file. + +============================================================================*/ + +#ifndef mitkLabelHighlightGuard_h +#define mitkLabelHighlightGuard_h + +#include + +#include +#include + +namespace mitk +{ + /** Helper class that allows to manage the node properties for label highlighting. + * Using the class ensures that the highlighting will always be removed if needed: + * (a) the destructor of the guard is called, or (b) the node changes (highlight + * of former node will be removed). The guard also manages the triggering the + * RenderWindowManager, if need, to refresh the render windows. + */ + class MITKMULTILABEL_EXPORT LabelHighlightGuard + { + public: + void SetSegmentationNode(DataNode* node); + DataNode::Pointer GetSegmentationNode() const; + + void SetHighlightedLabels(LabelSetImage::LabelValueVectorType labels); + void SetHighlightInvisibleLabels(bool highlightInvisible); + + ~LabelHighlightGuard(); + + constexpr static const char* PROPERTY_NAME_LABELS_HIGHLIGHTED() + { + return "org.mitk.multilabel.labels.highlighted"; + }; + + constexpr static const char* PROPERTY_NAME_HIGHLIGHT_INVISIBLE() + { + return "org.mitk.multilabel.highlight_invisible"; + }; + + protected: + static void UpdateNode(DataNode* node, LabelSetImage::LabelValueVectorType labels, bool highlightInvisible); + LabelSetImage::LabelValueVectorType m_Labels = {}; + bool m_HighlightInvisible = false; + WeakPointer m_Node; + }; +} + +#endif