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 #ifndef GLOBALINTERACTION_H_HEADER_INCLUDED_C152938A 00020 #define GLOBALINTERACTION_H_HEADER_INCLUDED_C152938A 00021 00022 #include "mitkFocusManager.h" 00023 #include "mitkCommon.h" 00024 #include "mitkStateMachineFactory.h" 00025 #include "mitkEventMapper.h" 00026 #include "mitkInteractor.h" 00027 00028 namespace mitk { 00029 00030 class PositionEvent; 00031 00032 //##Documentation 00033 //## @brief handles all global Events 00034 //## 00035 //## superior statemachine, that spreads the events to all other interactors 00036 //## 00037 //## Initialization 00038 //## Attention: GlobalInteraction <strong>must</strong> be initialized by the Initialize() method 00039 //## before usage by giving it an XML scheme. Possibilities are giving it an empty string (default), 00040 //## the filename of an XML file or the actual XML content as std::string. If an empty string is given, 00041 //## the content is tried to be loaded from the default file location. 00042 //## 00043 //## Concept of sending events: 00044 //## In this concept of interaction, the statemachines can be divided into two main statemachines: 00045 //## Listeners and interactors. 00046 //## Listeners only receive the event to process it, but don't change any data. They want to listen to all events. 00047 //## Interactors do change data according to the received event. They do not need to receive all events, only 00048 //## those they are interested in. 00049 //## 00050 //## To divide these two types of statemachine this class holds three lists and one map: 00051 //## m_ListenerList, m_InteractorList, m_SelectedList and m_JurisdictionMap 00052 //## The list m_ListenerList holds all listeners. 00053 //## m_InteractorList holds all interactors, and the List m_SelectedList holds all machines, that were set to SELECTED or SUBSELECTED. 00054 //## m_JurisdictionMap maps values returned from CanHandleEvent to the asked Interactors. 00055 //## Through this map stepping through interactors, that were not selected and could handle that event, can be done. 00056 //## 00057 //## First the listeners are informed with the event. 00058 //## Then the selected or subselected interactors are asked if they can handle that event. 00059 //## They can handle it, if the mode of the interactor after HandleEvent(..) is still in SMSELECTED or SMSUBSELECTED. 00060 //## They can't handle it, if the mode changed to SMDESELECTED. Then the interactor is removed from the selected-list. 00061 //## In that case, all interactors are asked to calculate and return their area of jurisdiction. 00062 //## An iterator is held on one interactor in the map. With the iterator, the map can be looped through so 00063 //## so that several geometric objects, that lie on top of each other, can be selected. 00064 //## @ingroup Interaction 00065 class MITK_CORE_EXPORT GlobalInteraction : public StateMachine 00066 { 00067 public: 00068 mitkClassMacro(GlobalInteraction, StateMachine); 00069 itkNewMacro(Self); 00070 00071 typedef std::vector<StateMachine::Pointer> StateMachineList; 00072 typedef std::vector<StateMachine*> StateMachineCPointerList; 00073 typedef StateMachineList::iterator StateMachineListIter; 00074 typedef StateMachineCPointerList::iterator StateMachineCPointerListIter; 00075 typedef std::vector<Interactor::Pointer> InteractorList; 00076 typedef InteractorList::iterator InteractorListIter; 00077 typedef std::multimap<float, Interactor::Pointer, std::greater<double> > InteractorMap; 00078 typedef InteractorMap::iterator InteractorMapIter; 00079 00080 //##Documentation 00081 //## @brief add an Interactor to the list of all interactors that are asked for handling an event 00082 //## 00083 //## returns true in case of success 00084 void AddInteractor(Interactor* interactor); 00085 00086 //##Documentation 00087 //## @brief remove a certain Interactor from the set of interactors that are asked for handling an event 00088 //## 00089 //## returns true in case of success 00090 bool RemoveInteractor(Interactor* interactor); 00091 00092 //##Documentation 00093 //## @brief returns true, if the given interactor is already added to the Interactor-List 00094 bool InteractorRegistered (Interactor* interactor); 00095 00096 //##Documentation 00097 //## @brief add a Listener to the list of all Listeners that are informed of an event 00098 //## 00099 //## returns true in case of success 00100 void AddListener(StateMachine* listener); 00101 00102 //##Documentation 00103 //## @brief remove a certain Listener from the set of Listeners that are informed of an event 00104 //## 00105 //## returns true in case of success 00106 bool RemoveListener(StateMachine* listener); 00107 00108 //##Documentation 00109 //## @brief returns true, if the given interactor is already added to the Listener-List 00110 bool ListenerRegistered (Interactor* interactor); 00111 00112 //##Documentation 00113 //## @brief adds an element in the list in FocusManager 00114 //## 00115 //## true if success, false if the element is already in list 00116 bool AddFocusElement(FocusManager::FocusElement* element); 00117 00118 //##Documentation 00119 //## @brief Removes an element in FocusManager 00120 //## 00121 //## true if success, false if the element was not in the list 00122 bool RemoveFocusElement(FocusManager::FocusElement* element); 00123 00124 //##Documentation 00125 //## @brief Returns the focused Element in FocusManager 00126 FocusManager::FocusElement* GetFocus(); 00127 00128 //##Documentation 00129 //## @brief Sets the given Element to focused 00130 //## 00131 //## returns true if the given element was found and focused 00132 bool SetFocus(FocusManager::FocusElement* element); 00133 00134 //##Documentation 00135 //## @brief Returns the pointer to the FocusManager 00136 //## 00137 //## to add the observer for an event 00138 FocusManager* GetFocusManager(); 00139 00140 //##Documentation 00141 //## @brief Returns the pointer to the EventMapper 00142 //## 00143 //## to add an addon 00144 EventMapper* GetEventMapper(); 00145 00149 StateMachineFactory* GetStateMachineFactory(); 00150 00157 State* GetStartState(const char* type); 00158 00159 //##Documentation 00160 //## @brief Returns the global (singleton) instance of 00161 //## GlobalInteraction. Create it, if it does not exist. 00162 static GlobalInteraction* GetInstance(); 00163 00164 //##Documentation 00165 //## @brief Initializes the global (singleton) instance of 00166 //## GlobalInteraction via an XML string. Must! be done before usage. Can be done only once. 00167 //## Can be used with an empty string (default), a file name with path, or the actual XML content as string. 00168 bool Initialize(const char* globalInteractionName, const std::string XMLBehaviorInput = ""); 00169 00170 //##Documentation 00171 //## @brief Check if GlobalInteraction has already been initialized. Init must! be done before usage. 00172 bool IsInitialized() {return m_IsInitialized;}; 00173 00174 00175 //so that the interactors can call AddToSelectedInteractors() and RemoveFromSelectedInteractors() 00176 friend class Interactor; 00177 00178 protected: 00184 GlobalInteraction(); 00185 00189 ~GlobalInteraction(); 00190 00191 virtual bool ExecuteAction(Action* action, mitk::StateEvent const* stateEvent); 00192 00193 /* 00194 *@brief adds the given interactor to the list of selected interactors. 00195 * This list is asked first to handle an event. 00196 */ 00197 virtual bool AddToSelectedInteractors(Interactor* interactor); 00198 00199 /* 00200 *@brief removes the given interactor from the list of selected interactors 00201 * This list is asked first to handle an event. 00202 */ 00203 virtual bool RemoveFromSelectedInteractors(Interactor* interactor); 00204 00205 private: 00206 00207 //##Documentation 00208 //##@brief informing all statemachines that are held in the list m_ListenerList 00209 void InformListeners(mitk::StateEvent const* stateEvent); 00210 00211 //##Documentation 00212 //##@brief asking the selected Interactor if an event can be handled 00213 //## 00214 //## returns false if no Interactor could handle the event 00215 bool AskSelected(mitk::StateEvent const* stateEvent); 00216 00217 //##Documentation 00218 //##@brief asking next interactor of m_JurisdictionMap 00219 bool AskCurrentInteractor(mitk::StateEvent const* stateEvent); 00220 00221 //##Documentation 00222 //##@brief filling m_JurisdictionMap 00223 //## 00224 //## @ params swell: if the calculated jurisdiction value is above swell, then add it to the map 00225 void FillJurisdictionMap(mitk::StateEvent const* stateEvent, float threshold); 00226 00227 void RemoveFlaggedListeners(); 00228 00229 StateMachineCPointerList m_ListenersFlaggedForRemoval; 00230 00231 //##Documentation 00232 //## @brief list of all listening statemachines, that want to receive all events 00233 StateMachineList m_ListenerList; 00234 00235 //##Documentation 00236 //## @brief list of all interactors (statemachine, that change data) 00237 InteractorList m_InteractorList; 00238 00239 //##Documentation 00240 //## @brief list of all interactors, that are in Mode SELECTED or SUBSELECTED 00241 InteractorList m_SelectedList; 00242 00243 //##Documentation 00244 //## @brief map for sorting all interactors by the value returned from CanHandleEvent(..). 00245 //## 00246 //## With that list certain interactors can be looped through like diving through layers 00247 InteractorMap m_JurisdictionMap; 00248 00249 //##Documentation 00250 //## @brief iterator on an entry in m_JurisdictionMap for stepping through interactors 00251 InteractorMapIter m_CurrentInteractorIter; 00252 00253 //##Documentation 00254 //## @brief holds a list of BaseRenderer and one focused 00255 FocusManager::Pointer m_FocusManager; 00256 00260 StateMachineFactory* m_StateMachineFactory; 00261 00265 EventMapper* m_EventMapper; 00266 00267 bool m_CurrentlyInInformListenersLoop; 00268 bool m_CurrentlyInInformInteractorsLoop; 00269 bool m_IsInitialized; 00270 }; 00271 } // namespace mitk 00272 00273 #endif /* GLOBALINTERACTION_H_HEADER_INCLUDED_C152938A */ 00274 00275