00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision$ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 00019 #include <mitkPropertyObserver.h> 00020 #include <mitkBaseProperty.h> 00021 #include <itkCommand.h> 00022 #include <stdexcept> 00023 00024 namespace mitk { 00025 00026 //---- PropertyObserver ----------------------------------------------------------- 00027 PropertyObserver::PropertyObserver() 00028 :m_SelfCall(false) 00029 { 00030 } 00031 00032 PropertyObserver::~PropertyObserver() 00033 { 00034 } 00035 00036 void PropertyObserver::BeginModifyProperty() 00037 { 00038 m_SelfCall = true; 00039 } 00040 00041 void PropertyObserver::EndModifyProperty() 00042 { 00043 m_SelfCall = false; 00044 } 00045 00046 //---- PropertyView --------------------------------------------------------------- 00047 00048 PropertyView::PropertyView( const mitk::BaseProperty* property ) 00049 :m_Property(property) 00050 { 00051 if ( !property ) throw std::invalid_argument("NULL pointer makes no sense in PropertyView()"); // no NULL property allowed 00052 00053 { 00054 itk::ReceptorMemberCommand<PropertyView>::Pointer command = itk::ReceptorMemberCommand<PropertyView>::New(); 00055 command->SetCallbackFunction(this, &PropertyView::OnModified); 00056 m_ModifiedTag = property->AddObserver( itk::ModifiedEvent(), command ); 00057 } 00058 { 00059 itk::ReceptorMemberCommand<PropertyView>::Pointer command = itk::ReceptorMemberCommand<PropertyView>::New(); 00060 command->SetCallbackFunction(this, &PropertyView::OnDelete); 00061 m_DeleteTag = property->AddObserver( itk::DeleteEvent(), command ); 00062 } 00063 } 00064 00065 00066 PropertyView::~PropertyView() 00067 { 00068 if (m_Property) 00069 { 00070 mitk::BaseProperty* prop = const_cast<mitk::BaseProperty*>(m_Property); 00071 prop->RemoveObserver(m_ModifiedTag); 00072 prop->RemoveObserver(m_DeleteTag); 00073 m_Property = NULL; 00074 } 00075 } 00076 00077 00078 void PropertyView::OnModified(const itk::EventObject& /*e*/) 00079 { 00080 if (m_SelfCall) return; 00081 00082 PropertyChanged(); 00083 } 00084 00085 00086 void PropertyView::OnDelete(const itk::EventObject& /*e*/) 00087 { 00088 if (m_SelfCall) return; 00089 00090 PropertyRemoved(); 00091 if (m_Property) 00092 { 00093 mitk::BaseProperty* prop = const_cast<mitk::BaseProperty*>(m_Property); 00094 prop->RemoveObserver(m_ModifiedTag); 00095 prop->RemoveObserver(m_DeleteTag); 00096 m_Property = NULL; 00097 } 00098 } 00099 00100 //---- PropertyEditor ------------------------------------------------------------- 00101 00102 PropertyEditor::PropertyEditor( mitk::BaseProperty* property ) 00103 :m_Property(property) 00104 { 00105 if ( !property ) throw std::invalid_argument("NULL pointer makes no sense in PropertyEditor()"); // no NULL property allowed 00106 00107 { 00108 itk::ReceptorMemberCommand<PropertyEditor>::Pointer command = itk::ReceptorMemberCommand<PropertyEditor>::New(); 00109 command->SetCallbackFunction(this, &PropertyEditor::OnModified); 00110 m_ModifiedTag = property->AddObserver( itk::ModifiedEvent(), command ); 00111 } 00112 { 00113 itk::ReceptorMemberCommand<PropertyEditor>::Pointer command = itk::ReceptorMemberCommand<PropertyEditor>::New(); 00114 command->SetCallbackFunction(this, &PropertyEditor::OnDelete); 00115 m_DeleteTag = property->AddObserver( itk::DeleteEvent(), command ); 00116 } 00117 } 00118 00119 00120 PropertyEditor::~PropertyEditor() 00121 { 00122 if (m_Property) 00123 { 00124 m_Property->RemoveObserver(m_ModifiedTag); 00125 m_Property->RemoveObserver(m_DeleteTag); 00126 m_Property = NULL; 00127 } 00128 } 00129 00130 00131 void PropertyEditor::OnModified(const itk::EventObject& /*e*/) 00132 { 00133 if (m_SelfCall) return; 00134 00135 PropertyChanged(); 00136 } 00137 00138 00139 void PropertyEditor::OnDelete(const itk::EventObject& /*e*/) 00140 { 00141 if (m_SelfCall) return; // does this make any sense? 00142 00143 PropertyRemoved(); 00144 if (m_Property) 00145 { 00146 m_Property->RemoveObserver(m_ModifiedTag); 00147 m_Property->RemoveObserver(m_DeleteTag); 00148 m_Property = NULL; 00149 } 00150 } 00151 00152 } // namespace 00153