OpenWalnut  1.4.0
WCustomWidgetEventHandler.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WCUSTOMWIDGETEVENTHANDLER_H
26 #define WCUSTOMWIDGETEVENTHANDLER_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/signals2/signal.hpp>
30 #endif
31 
32 #include <osgGA/GUIEventAdapter>
33 #include <osgGA/GUIEventHandler>
34 
35 #include "../common/math/linearAlgebra/WVectorFixed.h"
36 #include "../common/WLogger.h"
37 #include "WCustomWidget.h"
38 
39 /**
40  * An event handler for a custom widget which eases interaction with GUIEvents within your module. Without you need to write your
41  * own event handler and register it. However if you still need your own event handler you might consider subclassing from this
42  * one. Basically there are two ways of getting notified of GUIEvents. First you can connect a member function to events or you
43  * can overwrite member functions of this class to handle specific events.
44  *
45  * Use boost::bind and the subscribeXY methods to connect a member function of your module to a specific GUIEvent. Please note
46  * that this is now called in context of the GUI thread and that you need to take care of threadsafety by yourself.
47  *
48  * Use the corresponding handleXY() member functions if you still need a custom event handler. But take care that you may need to
49  * set the WCustomWidgetEventHandler::m_preselection event-mask accordingly within the constructor then.
50  *
51  * In case you might not know what the specific parameters of the handle function represent you might have luck looking into the
52  * OpenSceneGraph documentation http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs within the GUIEventAdapter
53  * class.
54  */
55 class WCustomWidgetEventHandler : public osgGA::GUIEventHandler
56 {
57 public:
58  /**
59  * Constructor.
60  *
61  * \param widget The custom widget for which events should be handled.
62  */
64 
65  /**
66  * The OSG calls this function whenever a new event has occured.
67  *
68  * \param ea Event class for storing GUI events such as mouse or keyboard interation etc.
69  *
70  * \return true if the event was handled.
71  */
72  bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& /* aa */ );
73 
74  /**
75  * Short hand type for signal signature of PUSH, RELEASE and DOUBLECLICK event.
76  */
77  typedef boost::signals2::signal< void ( WVector2f, int ) > ButtonSignalType;
78 
79  /**
80  * Short hand type for signal signature of DRAG event.
81  */
82  typedef boost::signals2::signal< void ( WVector2f, unsigned int ) > DragSignalType;
83 
84  /**
85  * Short hand type for signal signature of MOVE event.
86  */
87  typedef boost::signals2::signal< void ( WVector2f ) > MoveSignalType;
88 
89  /**
90  * Short hand type for signal signature of SCROLL event.
91  */
92  typedef boost::signals2::signal< void ( GUIEvents::ScrollingMotion, float, float ) > ScrollSignalType;
93 
94  /**
95  * Short hand type for signal signature of FRAME, PEN_PROXIMITY_ENTER, -LEAVE, CLOSE_WINDOW, QUIT_APPLICATION and USER event.
96  */
97  typedef boost::signals2::signal< void ( void ) > TriggerSignalType;
98 
99  /**
100  * Short hand type for signal signature of KEYDOWN and KEYUP event.
101  */
102  typedef boost::signals2::signal< void ( int, unsigned int ) > KeySignalType;
103 
104  /**
105  * Short hand type for signal signature of RESIZE event.
106  */
107  typedef boost::signals2::signal< void ( int, int, int, int ) > ResizeSignalType;
108 
109  /**
110  * Short hand type for signal signature of PEN_PRESSURE event.
111  */
112  typedef boost::signals2::signal< void ( float ) > PenPressureSignalType;
113 
114  /**
115  * Short hand type for signal signature of PEN_ORIENTATION event.
116  */
117  typedef boost::signals2::signal< void ( const osg::Matrix ) > PenOrientationSignalType;
118 
119  /**
120  * Registers a function slot to PUSH events. Whenever the event occurs, the slot is called with current parameters.
121  *
122  * \param slot Function object having the appropriate signature according to the used SignalType.
123  */
124  virtual void subscribePush( ButtonSignalType::slot_type slot );
125 
126  /**
127  * Registers a function slot to RELEASE events. Whenever the event occurs, the slot is called with current parameters.
128  * \copydetails subscribePush()
129  */
130  virtual void subscribeRelease( ButtonSignalType::slot_type slot );
131 
132  /**
133  * Registers a function slot to DOUBLECLICK events.
134  * \copydetails subscribePush()
135  */
136  virtual void subscribeDoubleclick( ButtonSignalType::slot_type slot );
137 
138  /**
139  * Registers a function slot to DRAG events.
140  * \copydetails subscribePush()
141  */
142  virtual void subscribeDrag( DragSignalType::slot_type slot );
143 
144  /**
145  * Registers a function slot to MOVE events.
146  *
147  * \copydetails subscribePush()
148  */
149  virtual void subscribeMove( MoveSignalType::slot_type slot );
150 
151  /**
152  * Registers a function slot to KEYDOWN events.
153  *
154  * \copydetails subscribePush()
155  */
156  virtual void subscribeKeydown( KeySignalType::slot_type slot );
157 
158  /**
159  * Registers a function slot to KEYUP events.
160  *
161  * \copydetails subscribePush()
162  */
163  virtual void subscribeKeyup( KeySignalType::slot_type slot );
164 
165  /**
166  * Registers a function slot to FRAME events.
167  *
168  * \copydetails subscribePush()
169  */
170  virtual void subscribeFrame( TriggerSignalType::slot_type );
171 
172  /**
173  * Registers a function slot to RESIZE events.
174  *
175  * \copydetails subscribePush()
176  */
177  virtual void subscribeResize( ResizeSignalType::slot_type slot );
178 
179  /**
180  * Registers a function slot to SCROLL events.
181  *
182  * \copydetails subscribePush()
183  */
184  virtual void subscribeScroll( ScrollSignalType::slot_type slot );
185 
186  /**
187  * Registers a function slot to PEN_PRESSURE events.
188  *
189  * \copydetails subscribePush()
190  */
191  virtual void subscribePenPressure( PenPressureSignalType::slot_type slot );
192 
193  /**
194  * Registers a function slot to PEN_ORIENTATION events.
195  *
196  * \copydetails subscribePush()
197  */
198  virtual void subscribePenOrientation( PenOrientationSignalType::slot_type slot );
199 
200  /**
201  * Registers a function slot to PEN_PROXIMITY_ENTER events.
202  *
203  * \copydetails subscribePush()
204  */
205  virtual void subscribePenProximityEnter( TriggerSignalType::slot_type slot );
206 
207  /**
208  * Registers a function slot to PEN_PROXIMITY_LEAVE events.
209  *
210  * \copydetails subscribePush()
211  */
212  virtual void subscribePenProximityLeave( TriggerSignalType::slot_type slot );
213 
214  /**
215  * Registers a function slot to CLOSE_WINDOW events.
216  *
217  * \copydetails subscribePush()
218  */
219  virtual void subscribeCloseWindow( TriggerSignalType::slot_type slot );
220 
221  /**
222  * Registers a function slot to QUIT_APPLICATION events.
223  * \copydetails subscribePush()
224  */
225  virtual void subscribeQuitApplication( TriggerSignalType::slot_type slot );
226 
227  /**
228  * Registers a function slot to USER events.
229  * \copydetails subscribePush()
230  */
231  virtual void subscribeUser( TriggerSignalType::slot_type slot );
232 
233  /**
234  * Called whenever the PUSH event occurs.
235  *
236  * \param mousePos Current mouse position in X and Y.
237  * \param button The involved mouse button. See http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs
238  * GUIEventAdapter class for values.
239  */
240  virtual void handlePush( WVector2f mousePos, int button );
241 
242  /**
243  * Called whenever the RELEASE event occurs.
244  * \copydetails handlePush()
245  */
246  virtual void handleRelease( WVector2f mousePos, int button );
247 
248  /**
249  * Called whenever the DOUBLECLICK event occurs.
250  * \copydetails handlePush()
251  */
252  virtual void handleDoubleclick( WVector2f mousePos, int button );
253 
254  /**
255  * Called whenever the DRAG event occurs.
256  *
257  * \param mousePos Current mouse position in X and Y.
258  * \param buttonMask The pushed mouse buttons as a mask. See http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs
259  * GUIEventAdapter class for values.
260  */
261  virtual void handleDrag( WVector2f mousePos, int buttonMask );
262 
263  /**
264  * Called whenever the MOVE event occurs.
265  *
266  * \param mousePos Current mouse position in X and Y.
267  */
268  virtual void handleMove( WVector2f mousePos );
269 
270  /**
271  * Called whenever the KEYDOWN event occurs.
272  *
273  * \param keyID The pressed key. See http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs GUIEventAdapter
274  * class for values.
275  * \param modKeyMask Additional function keys pressed.
276  */
277  virtual void handleKeydown( int keyID, unsigned int modKeyMask );
278 
279  /**
280  * Called whenever the KEYUP event occurs.
281  *
282  * \param keyID The released key. See http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs GUIEventAdapter
283  * class for values.
284  * \param modKeyMask Additional function keys pressed.
285  */
286  virtual void handleKeyup( int keyID, unsigned int modKeyMask );
287 
288  /**
289  * Called whenever the FRAME event occurs. This is every new frame.
290  */
291  virtual void handleFrame();
292 
293  /**
294  * Called whenever the widget has resized.
295  *
296  * \param xPos Position in X.
297  * \param yPos Position in Y.
298  * \param width Width of the widget.
299  * \param height Height of the widget.
300  */
301  virtual void handleResize( int xPos, int yPos, int width, int height );
302 
303  /**
304  * Called whenever the SCROLL event occurs.
305  *
306  * \param motion Scrolling motion. See http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs GUIEventAdapter class for values.
307  * \param deltaX Delta in X.
308  * \param deltaY Delta in Y.
309  */
310  virtual void handleScroll( GUIEvents::ScrollingMotion motion, float deltaX, float deltaY );
311 
312  /**
313  * Called whenever the PEN_PRESSURE event occurs.
314  *
315  * \param pressure pressure of the pen.
316  */
317  virtual void handlePenPressure( float pressure );
318 
319  /**
320  * Called whenever the PEN_ORIENTATION event occurs.
321  *
322  * \param orientation the orientation of the pen.
323  */
324  virtual void handlePenOrientation( const osg::Matrix orientation );
325 
326  /**
327  * Called whenever the PEN_PROXIMITY_ENTER event occurs.
328  */
329  virtual void handlePenProximityEnter();
330 
331  /**
332  * Called whenever the PEN_PROXIMITY_LEAVE event occurs.
333  */
334  virtual void handlePenProximityLeave();
335 
336  /**
337  * Called whenever the CLOSE_WINDOW event occurs.
338  */
339  virtual void handleCloseWindow();
340 
341  /**
342  * Called whenever the QUIT_APPLICATION event occurs.
343  */
344  virtual void handleQuitApplication();
345 
346  /**
347  * Called whenever the USER event occurs.
348  */
349  virtual void handleUser();
350 
351 protected:
352  /**
353  * Signal used for notification of the PUSH event.
354  */
355  ButtonSignalType m_sigPush;
356 
357  /**
358  * Signal used for notification of the RELEASE event.
359  */
360  ButtonSignalType m_sigRelease;
361 
362  /**
363  * Signal used for notification of the DOUBLECLICK event.
364  */
365  ButtonSignalType m_sigDoubleclick;
366 
367  /**
368  * Signal used for notification of the DRAG event.
369  */
370  DragSignalType m_sigDrag;
371 
372  /**
373  * Signal used for notification of the MOVE event.
374  */
375  MoveSignalType m_sigMove;
376 
377  /**
378  * Signal used for notification of the KEYDOWN event.
379  */
380  KeySignalType m_sigKeydown;
381 
382  /**
383  * Signal used for notification of the KEYUP event.
384  */
385  KeySignalType m_sigKeyup;
386 
387  /**
388  * Signal used for notification of the FRAME event.
389  */
390  TriggerSignalType m_sigFrame;
391 
392  /**
393  * Signal used for notification of the RESIZE event.
394  */
395  ResizeSignalType m_sigResize;
396 
397  /**
398  * Signal used for notification of the SCROLL event.
399  */
400  ScrollSignalType m_sigScroll;
401 
402  /**
403  * Signal used for notification of the PEN_PRESSURE event.
404  */
405  PenPressureSignalType m_sigPenPressure;
406 
407  /**
408  * Signal used for notification of the PEN_ORIENTATION event.
409  */
410  PenOrientationSignalType m_sigPenOrientation;
411 
412  /**
413  * Signal used for notification of the PEN_PROXIMITY_ENTER event.
414  */
415  TriggerSignalType m_sigPenProximityEnter;
416 
417  /**
418  * Signal used for notification of the PEN_PROXIMITY_LEAVE event.
419  */
420  TriggerSignalType m_sigPenProximityLeave;
421 
422  /**
423  * Signal used for notification of the CLOSE_WINDOW event.
424  */
425  TriggerSignalType m_sigCloseWindow;
426 
427  /**
428  * Signal used for notification of the QUIT_APPLICATION event.
429  */
430  TriggerSignalType m_sigQuitApplication;
431 
432  /**
433  * Signal used for notification of the USER event.
434  */
435  TriggerSignalType m_sigUser;
436 
437  /**
438  * Reference to the WCustomWidget for which event handling should performed.
439  */
441 
442  /**
443  * Binary mask describing which events should be used for notification or subscription.
444  */
445  unsigned int m_preselection;
446 
447  /**
448  * Logger instance for comfortable error logging. Simply use errorLog() << "my error".
449  *
450  * \return the logger stream.
451  */
453 };
454 
455 #endif // WCUSTOMWIDGETEVENTHANDLER_H
virtual void handleKeydown(int keyID, unsigned int modKeyMask)
Called whenever the KEYDOWN event occurs.
PenPressureSignalType m_sigPenPressure
Signal used for notification of the PEN_PRESSURE event.
virtual void handleMove(WVector2f mousePos)
Called whenever the MOVE event occurs.
wlog::WStreamedLogger errorLog() const
Logger instance for comfortable error logging.
ScrollSignalType m_sigScroll
Signal used for notification of the SCROLL event.
TriggerSignalType m_sigUser
Signal used for notification of the USER event.
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
The OSG calls this function whenever a new event has occured.
virtual void handlePenProximityLeave()
Called whenever the PEN_PROXIMITY_LEAVE event occurs.
virtual void subscribeResize(ResizeSignalType::slot_type slot)
Registers a function slot to RESIZE events.
virtual void subscribeCloseWindow(TriggerSignalType::slot_type slot)
Registers a function slot to CLOSE_WINDOW events.
virtual void subscribeMove(MoveSignalType::slot_type slot)
Registers a function slot to MOVE events.
virtual void handleScroll(GUIEvents::ScrollingMotion motion, float deltaX, float deltaY)
Called whenever the SCROLL event occurs.
MoveSignalType m_sigMove
Signal used for notification of the MOVE event.
boost::signals2::signal< void(const osg::Matrix) > PenOrientationSignalType
Short hand type for signal signature of PEN_ORIENTATION event.
ResizeSignalType m_sigResize
Signal used for notification of the RESIZE event.
virtual void handleFrame()
Called whenever the FRAME event occurs.
virtual void handlePenPressure(float pressure)
Called whenever the PEN_PRESSURE event occurs.
virtual void subscribeScroll(ScrollSignalType::slot_type slot)
Registers a function slot to SCROLL events.
boost::signals2::signal< void(void) > TriggerSignalType
Short hand type for signal signature of FRAME, PEN_PROXIMITY_ENTER, -LEAVE, CLOSE_WINDOW, QUIT_APPLICATION and USER event.
virtual void handlePenProximityEnter()
Called whenever the PEN_PROXIMITY_ENTER event occurs.
virtual void handlePush(WVector2f mousePos, int button)
Called whenever the PUSH event occurs.
virtual void subscribePenOrientation(PenOrientationSignalType::slot_type slot)
Registers a function slot to PEN_ORIENTATION events.
virtual void subscribePenProximityEnter(TriggerSignalType::slot_type slot)
Registers a function slot to PEN_PROXIMITY_ENTER events.
TriggerSignalType m_sigPenProximityLeave
Signal used for notification of the PEN_PROXIMITY_LEAVE event.
boost::signals2::signal< void(int, unsigned int) > KeySignalType
Short hand type for signal signature of KEYDOWN and KEYUP event.
virtual void handleDoubleclick(WVector2f mousePos, int button)
Called whenever the DOUBLECLICK event occurs.
boost::signals2::signal< void(WVector2f, int) > ButtonSignalType
Short hand type for signal signature of PUSH, RELEASE and DOUBLECLICK event.
virtual void handleCloseWindow()
Called whenever the CLOSE_WINDOW event occurs.
PenOrientationSignalType m_sigPenOrientation
Signal used for notification of the PEN_ORIENTATION event.
virtual void subscribeUser(TriggerSignalType::slot_type slot)
Registers a function slot to USER events.
virtual void subscribePush(ButtonSignalType::slot_type slot)
Registers a function slot to PUSH events.
virtual void subscribeDrag(DragSignalType::slot_type slot)
Registers a function slot to DRAG events.
virtual void handleKeyup(int keyID, unsigned int modKeyMask)
Called whenever the KEYUP event occurs.
ButtonSignalType m_sigPush
Signal used for notification of the PUSH event.
virtual void subscribePenProximityLeave(TriggerSignalType::slot_type slot)
Registers a function slot to PEN_PROXIMITY_LEAVE events.
Resource class for streamed logging.
Definition: WLogger.h:183
virtual void handleRelease(WVector2f mousePos, int button)
Called whenever the RELEASE event occurs.
boost::signals2::signal< void(GUIEvents::ScrollingMotion, float, float) > ScrollSignalType
Short hand type for signal signature of SCROLL event.
virtual void handleResize(int xPos, int yPos, int width, int height)
Called whenever the widget has resized.
A fixed size matrix class.
Definition: WMatrixFixed.h:153
ButtonSignalType m_sigRelease
Signal used for notification of the RELEASE event.
TriggerSignalType m_sigPenProximityEnter
Signal used for notification of the PEN_PROXIMITY_ENTER event.
unsigned int m_preselection
Binary mask describing which events should be used for notification or subscription.
virtual void subscribePenPressure(PenPressureSignalType::slot_type slot)
Registers a function slot to PEN_PRESSURE events.
boost::signals2::signal< void(int, int, int, int) > ResizeSignalType
Short hand type for signal signature of RESIZE event.
virtual void handleQuitApplication()
Called whenever the QUIT_APPLICATION event occurs.
virtual void subscribeQuitApplication(TriggerSignalType::slot_type slot)
Registers a function slot to QUIT_APPLICATION events.
virtual void subscribeKeyup(KeySignalType::slot_type slot)
Registers a function slot to KEYUP events.
virtual void handlePenOrientation(const osg::Matrix orientation)
Called whenever the PEN_ORIENTATION event occurs.
TriggerSignalType m_sigQuitApplication
Signal used for notification of the QUIT_APPLICATION event.
virtual void subscribeRelease(ButtonSignalType::slot_type slot)
Registers a function slot to RELEASE events.
virtual void subscribeKeydown(KeySignalType::slot_type slot)
Registers a function slot to KEYDOWN events.
virtual void subscribeDoubleclick(ButtonSignalType::slot_type slot)
Registers a function slot to DOUBLECLICK events.
KeySignalType m_sigKeydown
Signal used for notification of the KEYDOWN event.
An event handler for a custom widget which eases interaction with GUIEvents within your module...
TriggerSignalType m_sigCloseWindow
Signal used for notification of the CLOSE_WINDOW event.
WCustomWidget::SPtr m_widget
Reference to the WCustomWidget for which event handling should performed.
DragSignalType m_sigDrag
Signal used for notification of the DRAG event.
boost::signals2::signal< void(WVector2f, unsigned int) > DragSignalType
Short hand type for signal signature of DRAG event.
WCustomWidgetEventHandler(WCustomWidget::SPtr widget)
Constructor.
virtual void subscribeFrame(TriggerSignalType::slot_type)
Registers a function slot to FRAME events.
boost::signals2::signal< void(float) > PenPressureSignalType
Short hand type for signal signature of PEN_PRESSURE event.
TriggerSignalType m_sigFrame
Signal used for notification of the FRAME event.
virtual void handleUser()
Called whenever the USER event occurs.
boost::shared_ptr< WCustomWidget > SPtr
Abbreviation for a shared pointer on a instance of this class.
Definition: WCustomWidget.h:72
boost::signals2::signal< void(WVector2f) > MoveSignalType
Short hand type for signal signature of MOVE event.
KeySignalType m_sigKeyup
Signal used for notification of the KEYUP event.
virtual void handleDrag(WVector2f mousePos, int buttonMask)
Called whenever the DRAG event occurs.
ButtonSignalType m_sigDoubleclick
Signal used for notification of the DOUBLECLICK event.