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 "mitkFocusManager.h" 00020 00021 mitk::FocusManager::FocusManager() 00022 { 00023 m_Loop = true;//default 00024 m_FocusList.clear(); 00025 m_FocElement = NULL; 00026 } 00027 00028 mitk::FocusManager::~FocusManager() 00029 { 00030 } 00031 00032 bool mitk::FocusManager::AddElement(FocusElement* element) 00033 { 00034 // Try find 00035 mitk::FocusManager::FocusListIterator position = std::find(m_FocusList.begin(),m_FocusList.end(),element); 00036 if (position != m_FocusList.end()) 00037 return false; 00038 00039 m_FocusList.push_back(element); 00040 if (m_FocElement.GetPointer() == NULL) 00041 m_FocElement = element; 00042 return true; 00043 } 00044 00045 bool mitk::FocusManager::RemoveElement(FocusElement* element) 00046 { 00047 // Try find 00048 mitk::FocusManager::FocusListIterator position = std::find(m_FocusList.begin(),m_FocusList.end(),element); 00049 if (position == m_FocusList.end()) 00050 return false; 00051 position = m_FocusList.erase(position); 00052 // first delete the one on the position, and store the one afterewards into position 00053 if ( m_FocusList.size() == 0 ) 00054 { 00055 // no more FocusElements available 00056 m_FocElement = NULL; 00057 } 00058 else if ( position == m_FocusList.end() ) 00059 { 00060 // deleted was the last in row, then take the one before 00061 m_FocElement = m_FocusList.back(); 00062 } 00063 else 00064 { 00065 // m_FocElement is equal to the next one in row 00066 m_FocElement = *position; 00067 } 00068 return true; 00069 } 00070 00071 mitk::FocusManager::FocusElement* mitk::FocusManager::GetFocused() const 00072 { 00073 return m_FocElement.GetPointer(); 00074 } 00075 00076 bool mitk::FocusManager::SetFocused(FocusElement* element) 00077 { 00078 if (m_FocElement == element) 00079 return true; 00080 FocusListIterator position = std::find(m_FocusList.begin(),m_FocusList.end(),element); 00081 if (position == m_FocusList.end())//not found 00082 return false; 00083 00084 m_FocElement = *position; 00085 ((const itk::Object*)this)->InvokeEvent(FocusEvent()); 00086 return true; 00087 } 00088 00089 bool mitk::FocusManager::IsLast() 00090 { 00091 return (m_FocElement == m_FocusList.back()); 00092 } 00093 00094 bool mitk::FocusManager::IsFirst() 00095 { 00096 return (m_FocElement == m_FocusList.front()); 00097 } 00098 00099 const mitk::FocusManager::FocusElement* mitk::FocusManager::GetFirst() const 00100 { 00101 return (m_FocusList.front()).GetPointer(); 00102 } 00103 00104 const mitk::FocusManager::FocusElement* mitk::FocusManager::GetLast() const 00105 { 00106 return (m_FocusList.back()).GetPointer(); 00107 } 00108 00109 bool mitk::FocusManager::GoToNext() 00110 { 00111 //find the m_FocElement 00112 FocusListIterator position = std::find(m_FocusList.begin(),m_FocusList.end(),m_FocElement); 00113 if (position == m_FocusList.end())//not found 00114 return false; 00115 else if (*position == m_FocusList.back())//last in row 00116 { 00117 if (m_Loop) 00118 { 00119 m_FocElement = *(m_FocusList.begin()); 00120 return true; 00121 } 00122 else 00123 { 00124 return false;//last in row and loop == false, so GoToNext == false 00125 } 00126 } 00127 else //not last in row 00128 { 00129 m_FocElement = *(++position);//increase position and set m_FocElement 00130 return true; 00131 } 00132 return false; 00133 } 00134 00135 //##Documentation 00136 //## returns an iterator, that points to the 00137 //## beginning of the list 00138 mitk::FocusManager::FocusListIterator mitk::FocusManager::GetIter() 00139 { 00140 return m_FocusList.begin(); 00141 } 00142 00143 void mitk::FocusManager::SetLoop(bool loop) 00144 { 00145 m_Loop = loop; 00146 }