Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkBindDispatcherInteractor.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include "mitkMessage.h"
15 #include <cstring>
16 
17 // us
18 #include "usGetModuleContext.h"
19 #include "usModule.h"
20 #include "usModuleRegistry.h"
21 
23 {
24  us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext();
25  if (context == nullptr)
26  {
27  MITK_ERROR << "BindDispatcherInteractor() - Context could not be obtained.";
28  return;
29  }
30 
31  m_Dispatcher = Dispatcher::New(rendererName);
32 }
33 
35 {
36  // Set/Change Datastorage. This registers BDI to listen for events of DataStorage, to be informed when
37  // a DataNode with a Interactor is added/modified/removed.
38 
39  if (dataStorage != m_DataStorage)
40  {
41  // clean up events from previous datastorage
42  UnRegisterDataStorageEvents();
43  m_DataStorage = dataStorage;
44  RegisterDataStorageEvents();
45  }
46 }
47 
49 {
50  if (m_DataStorage.IsNotNull())
51  {
52  UnRegisterDataStorageEvents();
53  }
54 }
55 
56 void mitk::BindDispatcherInteractor::RegisterInteractor(const mitk::DataNode *dataNode)
57 {
58  if (m_Dispatcher.IsNotNull())
59  {
60  m_Dispatcher->AddDataInteractor(dataNode);
61  }
62 }
63 
64 void mitk::BindDispatcherInteractor::RegisterDataStorageEvents()
65 {
66  if (m_DataStorage.IsNotNull())
67  {
68  m_DataStorage->AddNodeEvent.AddListener(MessageDelegate1<BindDispatcherInteractor, const DataNode *>(
69  this, &BindDispatcherInteractor::RegisterInteractor));
70 
71  m_DataStorage->RemoveNodeEvent.AddListener(MessageDelegate1<BindDispatcherInteractor, const DataNode *>(
72  this, &BindDispatcherInteractor::UnRegisterInteractor));
73 
74  m_DataStorage->InteractorChangedNodeEvent.AddListener(MessageDelegate1<BindDispatcherInteractor, const DataNode *>(
75  this, &BindDispatcherInteractor::RegisterInteractor));
76  }
77 }
78 
79 void mitk::BindDispatcherInteractor::UnRegisterInteractor(const DataNode *dataNode)
80 {
81  if (m_Dispatcher.IsNotNull())
82  {
83  m_Dispatcher->RemoveDataInteractor(dataNode);
84  }
85 }
86 
88 {
89  return m_Dispatcher;
90 }
91 
93 {
94  m_Dispatcher = dispatcher;
95 }
96 
97 void mitk::BindDispatcherInteractor::UnRegisterDataStorageEvents()
98 {
99  if (m_DataStorage.IsNotNull())
100  {
101  m_DataStorage->AddNodeEvent.RemoveListener(MessageDelegate1<BindDispatcherInteractor, const DataNode *>(
102  this, &BindDispatcherInteractor::RegisterInteractor));
103  m_DataStorage->RemoveNodeEvent.RemoveListener(MessageDelegate1<BindDispatcherInteractor, const DataNode *>(
104  this, &BindDispatcherInteractor::UnRegisterInteractor));
105  m_DataStorage->InteractorChangedNodeEvent.RemoveListener(
107  &BindDispatcherInteractor::RegisterInteractor));
108  }
109 }
#define MITK_ERROR
Definition: mitkLogMacros.h:20
static Module * GetModule(long id)
void SetDataStorage(DataStorage::Pointer dataStorage)
mitk::DataStorage::Pointer m_DataStorage
ModuleContext * GetModuleContext() const
Definition: usModule.cpp:206
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57
static Pointer New(const std::string &_arg)
void SetDispatcher(Dispatcher::Pointer dispatcher)