|
| 1 | +// LayerDM includes |
| 2 | +#include "vtkMRMLLayerDMObjectEventObserver.h" |
| 3 | +#include "vtkMRMLLayerDMSelectionObserver.h" |
| 4 | + |
| 5 | +// Slicer includes |
| 6 | +#include <vtkMRMLScene.h> |
| 7 | +#include <vtkMRMLInteractionNode.h> |
| 8 | +#include <vtkMRMLSelectionNode.h> |
| 9 | +#include <vtkMRMLApplicationLogic.h> |
| 10 | + |
| 11 | +// VTK includes |
| 12 | +#include <vtkObjectFactory.h> |
| 13 | + |
| 14 | +vtkStandardNewMacro(vtkMRMLLayerDMSelectionObserver); |
| 15 | + |
| 16 | +vtkMRMLLayerDMSelectionObserver::vtkMRMLLayerDMSelectionObserver() |
| 17 | + : m_obs{ vtkSmartPointer<vtkMRMLLayerDMObjectEventObserver>::New() } |
| 18 | +{ |
| 19 | + m_obs->SetUpdateCallback( |
| 20 | + [this](vtkObject* obj) |
| 21 | + { |
| 22 | + if (obj == this->m_interactionNode || obj == this->m_selectionNode) |
| 23 | + { |
| 24 | + this->Modified(); |
| 25 | + } |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +vtkMRMLLayerDMSelectionObserver::~vtkMRMLLayerDMSelectionObserver() = default; |
| 30 | + |
| 31 | +void vtkMRMLLayerDMSelectionObserver::SetScene(vtkMRMLScene* scene) |
| 32 | +{ |
| 33 | + this->UpdateNodesFromScene(scene); |
| 34 | +} |
| 35 | + |
| 36 | +void vtkMRMLLayerDMSelectionObserver::UpdateNodesFromScene(vtkMRMLScene* scene) |
| 37 | +{ |
| 38 | + bool didModify{}; |
| 39 | + didModify |= this->SetInteractionNode(vtkMRMLInteractionNode::SafeDownCast(scene ? scene->GetNodeByID("vtkMRMLInteractionNodeSingleton") : nullptr)); |
| 40 | + didModify |= this->SetSelectionNode(vtkMRMLSelectionNode::SafeDownCast(scene ? scene->GetNodeByID("vtkMRMLSelectionNodeSingleton") : nullptr)); |
| 41 | + if (didModify) |
| 42 | + { |
| 43 | + this->Modified(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void vtkMRMLLayerDMSelectionObserver::UpdateNodesFromApplicationLogic(vtkMRMLApplicationLogic* logic) |
| 48 | +{ |
| 49 | + bool didModify{}; |
| 50 | + didModify |= this->SetInteractionNode(logic ? logic->GetInteractionNode() : nullptr); |
| 51 | + didModify |= this->SetSelectionNode(logic ? logic->GetSelectionNode() : nullptr); |
| 52 | + if (didModify) |
| 53 | + { |
| 54 | + this->Modified(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +bool vtkMRMLLayerDMSelectionObserver::SetInteractionNode(vtkMRMLInteractionNode* interactionNode) |
| 59 | +{ |
| 60 | + const auto didModify = this->m_obs->UpdateObserver(m_interactionNode, interactionNode); |
| 61 | + this->m_interactionNode = interactionNode; |
| 62 | + return didModify; |
| 63 | +} |
| 64 | + |
| 65 | +vtkMRMLInteractionNode* vtkMRMLLayerDMSelectionObserver::GetInteractionNode() const |
| 66 | +{ |
| 67 | + return this->m_interactionNode; |
| 68 | +} |
| 69 | + |
| 70 | +bool vtkMRMLLayerDMSelectionObserver::SetSelectionNode(vtkMRMLSelectionNode* selectionNode) |
| 71 | +{ |
| 72 | + const auto didModify = this->m_obs->UpdateObserver(m_selectionNode, selectionNode); |
| 73 | + this->m_selectionNode = selectionNode; |
| 74 | + return didModify; |
| 75 | +} |
| 76 | + |
| 77 | +vtkMRMLSelectionNode* vtkMRMLLayerDMSelectionObserver::GetSelectionNode() const |
| 78 | +{ |
| 79 | + return this->m_selectionNode; |
| 80 | +} |
| 81 | + |
| 82 | +bool vtkMRMLLayerDMSelectionObserver::IsPlacing(vtkMRMLNode* node) const |
| 83 | +{ |
| 84 | + if (!node) |
| 85 | + { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + return this->IsPlacing() && (this->GetActivePlaceNodeID() == std::string(node->GetID())); |
| 90 | +} |
| 91 | + |
| 92 | +bool vtkMRMLLayerDMSelectionObserver::IsPlacing() const |
| 93 | +{ |
| 94 | + if (!this->m_interactionNode) |
| 95 | + { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + return this->m_interactionNode->GetCurrentInteractionMode() == vtkMRMLInteractionNode::Place; |
| 100 | +} |
| 101 | + |
| 102 | +void vtkMRMLLayerDMSelectionObserver::StartPlace(vtkMRMLNode* node, bool isPersistent) |
| 103 | +{ |
| 104 | + if (!node || !this->m_interactionNode || !this->m_selectionNode) |
| 105 | + { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + { |
| 110 | + vtkMRMLLayerDMObjectEventObserver::UpdateGuard guard(m_obs); |
| 111 | + this->m_selectionNode->SetActivePlaceNodeID(node->GetID()); |
| 112 | + this->m_interactionNode->SetCurrentInteractionMode(vtkMRMLInteractionNode::Place); |
| 113 | + this->m_interactionNode->SetPlaceModePersistence(isPersistent); |
| 114 | + } |
| 115 | + this->Modified(); |
| 116 | +} |
| 117 | + |
| 118 | +void vtkMRMLLayerDMSelectionObserver::StopPlace() const |
| 119 | +{ |
| 120 | + this->SetInteractionMode(vtkMRMLInteractionNode::ViewTransform); |
| 121 | +} |
| 122 | + |
| 123 | +std::string vtkMRMLLayerDMSelectionObserver::GetActivePlaceNodeID() const |
| 124 | +{ |
| 125 | + if (!this->m_selectionNode) |
| 126 | + { |
| 127 | + return ""; |
| 128 | + } |
| 129 | + return this->m_selectionNode->GetActivePlaceNodeID(); |
| 130 | +} |
| 131 | + |
| 132 | +void vtkMRMLLayerDMSelectionObserver::SetInteractionMode(int interactionMode) const |
| 133 | +{ |
| 134 | + if (!m_interactionNode) |
| 135 | + { |
| 136 | + return; |
| 137 | + } |
| 138 | + m_interactionNode->SetCurrentInteractionMode(interactionMode); |
| 139 | +} |
| 140 | + |
| 141 | +int vtkMRMLLayerDMSelectionObserver::GetCurrentInteractionMode() const |
| 142 | +{ |
| 143 | + if (!this->m_interactionNode) |
| 144 | + { |
| 145 | + return 0; |
| 146 | + } |
| 147 | + return this->m_interactionNode->GetCurrentInteractionMode(); |
| 148 | +} |
| 149 | + |
| 150 | +bool vtkMRMLLayerDMSelectionObserver::GetPlaceModePersistence() const |
| 151 | +{ |
| 152 | + if (!m_interactionNode) |
| 153 | + { |
| 154 | + return false; |
| 155 | + } |
| 156 | + return m_interactionNode->GetPlaceModePersistence(); |
| 157 | +} |
0 commit comments