Medical Imaging Interaction Toolkit  2018.4.99-dcb22cd7
Medical Imaging Interaction Toolkit
mitkPropertyObserver.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 
13 #include <itkCommand.h>
14 #include <mitkBaseProperty.h>
15 #include <mitkPropertyObserver.h>
16 #include <stdexcept>
17 
18 namespace mitk
19 {
20  //---- PropertyObserver -----------------------------------------------------------
21  PropertyObserver::PropertyObserver() : m_SelfCall(false) {}
25  //---- PropertyView ---------------------------------------------------------------
26 
27  PropertyView::PropertyView(const mitk::BaseProperty *property) : m_Property(property)
28  {
29  if (!property)
30  throw std::invalid_argument("nullptr pointer makes no sense in PropertyView()"); // no nullptr property allowed
31 
32  {
33  itk::ReceptorMemberCommand<PropertyView>::Pointer command = itk::ReceptorMemberCommand<PropertyView>::New();
34  command->SetCallbackFunction(this, &PropertyView::OnModified);
35  m_ModifiedTag = property->AddObserver(itk::ModifiedEvent(), command);
36  }
37  {
38  itk::ReceptorMemberCommand<PropertyView>::Pointer command = itk::ReceptorMemberCommand<PropertyView>::New();
39  command->SetCallbackFunction(this, &PropertyView::OnDelete);
40  m_DeleteTag = property->AddObserver(itk::DeleteEvent(), command);
41  }
42  }
43 
45  {
46  if (m_Property)
47  {
48  auto *prop = const_cast<mitk::BaseProperty *>(m_Property);
49  prop->RemoveObserver(m_ModifiedTag);
50  prop->RemoveObserver(m_DeleteTag);
51  m_Property = nullptr;
52  }
53  }
54 
55  void PropertyView::OnModified(const itk::EventObject & /*e*/)
56  {
57  if (m_SelfCall)
58  return;
59 
61  }
62 
63  void PropertyView::OnDelete(const itk::EventObject & /*e*/)
64  {
65  if (m_SelfCall)
66  return;
67 
69  if (m_Property)
70  {
71  auto *prop = const_cast<mitk::BaseProperty *>(m_Property);
72  prop->RemoveObserver(m_ModifiedTag);
73  prop->RemoveObserver(m_DeleteTag);
74  m_Property = nullptr;
75  }
76  }
77 
78  //---- PropertyEditor -------------------------------------------------------------
79 
81  {
82  if (!property)
83  throw std::invalid_argument("nullptr pointer makes no sense in PropertyEditor()"); // no nullptr property allowed
84 
85  {
86  itk::ReceptorMemberCommand<PropertyEditor>::Pointer command = itk::ReceptorMemberCommand<PropertyEditor>::New();
87  command->SetCallbackFunction(this, &PropertyEditor::OnModified);
88  m_ModifiedTag = property->AddObserver(itk::ModifiedEvent(), command);
89  }
90  {
91  itk::ReceptorMemberCommand<PropertyEditor>::Pointer command = itk::ReceptorMemberCommand<PropertyEditor>::New();
92  command->SetCallbackFunction(this, &PropertyEditor::OnDelete);
93  m_DeleteTag = property->AddObserver(itk::DeleteEvent(), command);
94  }
95  }
96 
98  {
99  if (m_Property)
100  {
101  m_Property->RemoveObserver(m_ModifiedTag);
102  m_Property->RemoveObserver(m_DeleteTag);
103  m_Property = nullptr;
104  }
105  }
106 
107  void PropertyEditor::OnModified(const itk::EventObject & /*e*/)
108  {
109  if (m_SelfCall)
110  return;
111 
112  PropertyChanged();
113  }
114 
115  void PropertyEditor::OnDelete(const itk::EventObject & /*e*/)
116  {
117  if (m_SelfCall)
118  return; // does this make any sense?
119 
120  PropertyRemoved();
121  if (m_Property)
122  {
123  m_Property->RemoveObserver(m_ModifiedTag);
124  m_Property->RemoveObserver(m_DeleteTag);
125  m_Property = nullptr;
126  }
127  }
128 
129 } // namespace
PropertyView(const mitk::BaseProperty *)
virtual void PropertyRemoved()=0
DataCollection - Class to facilitate loading/accessing structured data.
mitk::BaseProperty * m_Property
Abstract base class for properties.
void OnDelete(const itk::EventObject &e)
const mitk::BaseProperty * m_Property
PropertyEditor(mitk::BaseProperty *)
virtual void PropertyChanged()=0
void OnModified(const itk::EventObject &e)
void OnDelete(const itk::EventObject &e)
void OnModified(const itk::EventObject &e)