KiCad PCB EDA Suite
tool_event.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef __TOOL_EVENT_H
26 #define __TOOL_EVENT_H
27 
28 #include <cstdio>
29 #include <deque>
30 #include <iterator>
31 
32 #include <math/vector2d.h>
33 #include <cassert>
34 
35 #include <boost/optional.hpp>
36 
37 class TOOL_ACTION;
38 class TOOL_MANAGER;
39 
45 {
46  TC_NONE = 0x00,
47  TC_MOUSE = 0x01,
48  TC_KEYBOARD = 0x02,
49  TC_COMMAND = 0x04,
50  TC_MESSAGE = 0x08,
51  TC_VIEW = 0x10,
52  TC_ANY = 0xffffffff
53 };
54 
56 {
57  // UI input events
58  TA_NONE = 0x0000,
59  TA_MOUSE_CLICK = 0x0001,
61  TA_MOUSE_UP = 0x0004,
62  TA_MOUSE_DOWN = 0x0008,
63  TA_MOUSE_DRAG = 0x0010,
64  TA_MOUSE_MOTION = 0x0020,
65  TA_MOUSE_WHEEL = 0x0040,
66  TA_MOUSE = 0x007f,
67 
68  TA_KEY_PRESSED = 0x0080,
70 
71  // View related events
72  TA_VIEW_REFRESH = 0x0100,
73  TA_VIEW_ZOOM = 0x0200,
74  TA_VIEW_PAN = 0x0400,
75  TA_VIEW_DIRTY = 0x0800,
76  TA_VIEW = 0x0f00,
77 
78  TA_CHANGE_LAYER = 0x1000,
79 
80  // Tool cancel event. Issued automagically when the user hits escape or selects End Tool from
81  // the context menu.
82  TA_CANCEL_TOOL = 0x2000,
83 
84  // Context menu update. Issued whenever context menu is open and the user hovers the mouse
85  // over one of choices. Used in dynamic highligting in disambiguation menu
87 
88  // Context menu choice. Sent if the user picked something from the context menu or
89  // closed it without selecting anything.
91 
92  // Context menu is closed, no matter whether anything has been chosen or not.
94 
96 
97  // This event is sent *before* undo/redo command is performed.
98  TA_UNDO_REDO_PRE = 0x20000,
99 
100  // This event is sent *after* undo/redo command is performed.
101  TA_UNDO_REDO_POST = 0x40000,
102 
103  // Tool action (allows to control tools).
104  TA_ACTION = 0x80000,
105 
106  // Tool activation event.
107  TA_ACTIVATE = 0x100000,
108 
109  // Model has changed (partial update).
110  TA_MODEL_CHANGE = 0x200000,
111 
112  TA_ANY = 0xffffffff
113 };
114 
116 {
117  BUT_NONE = 0x0,
118  BUT_LEFT = 0x1,
119  BUT_RIGHT = 0x2,
120  BUT_MIDDLE = 0x4,
122  BUT_ANY = 0xffffffff
123 };
124 
126 {
127  MD_SHIFT = 0x1000,
128  MD_CTRL = 0x2000,
129  MD_ALT = 0x4000,
131 };
132 
135 {
139 };
140 
143 {
144  AF_NONE = 0,
147 };
148 
151 {
152  CMENU_BUTTON = 0, // On the right button
153  CMENU_NOW, // Right now (after TOOL_INTERACTIVE::SetContextMenu)
154  CMENU_OFF // Never
155 };
156 
163 {
164 public:
171  const std::string Format() const;
172 
174  TOOL_ACTION_SCOPE aScope = AS_GLOBAL, void* aParameter = NULL ) :
175  m_category( aCategory ),
176  m_actions( aAction ),
177  m_scope( aScope ),
178  m_mouseButtons( 0 ),
179  m_keyCode( 0 ),
180  m_modifiers( 0 ),
181  m_param( aParameter ) {}
182 
183  TOOL_EVENT( TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, int aExtraParam,
184  TOOL_ACTION_SCOPE aScope = AS_GLOBAL, void* aParameter = NULL ) :
185  m_category( aCategory ),
186  m_actions( aAction ),
187  m_scope( aScope ),
188  m_mouseButtons( 0 ),
189  m_keyCode( 0 ),
190  m_modifiers( 0 ),
191  m_param( aParameter )
192  {
193  if( aCategory == TC_MOUSE )
194  {
195  setMouseButtons( aExtraParam & BUT_BUTTON_MASK );
196  }
197  else if( aCategory == TC_KEYBOARD )
198  {
199  m_keyCode = aExtraParam & ~MD_MODIFIER_MASK; // Filter out modifiers
200  }
201  else if( aCategory == TC_COMMAND )
202  {
203  m_commandId = aExtraParam;
204  }
205 
206  if( aCategory & ( TC_MOUSE | TC_KEYBOARD ) )
207  {
208  m_modifiers = aExtraParam & MD_MODIFIER_MASK;
209  }
210  }
211 
213  const std::string& aExtraParam, TOOL_ACTION_SCOPE aScope = AS_GLOBAL,
214  void* aParameter = NULL ) :
215  m_category( aCategory ),
216  m_actions( aAction ),
217  m_scope( aScope ),
218  m_mouseButtons( 0 ),
219  m_keyCode( 0 ),
220  m_modifiers( 0 ),
221  m_param( aParameter )
222  {
223  if( aCategory == TC_COMMAND || aCategory == TC_MESSAGE )
224  m_commandStr = aExtraParam;
225  }
226 
229  {
230  return m_category;
231  }
232 
235  {
236  return m_actions;
237  }
238 
241  const VECTOR2D& Delta() const
242  {
243  assert( m_category == TC_MOUSE ); // this should be used only with mouse events
244  return m_mouseDelta;
245  }
246 
248  const VECTOR2D& Position() const
249  {
250  assert( m_category == TC_MOUSE ); // this should be used only with mouse events
251  return m_mousePos;
252  }
253 
255  const VECTOR2D& DragOrigin() const
256  {
257  assert( m_category == TC_MOUSE ); // this should be used only with mouse events
258  return m_mouseDragOrigin;
259  }
260 
262  int Buttons() const
263  {
264  assert( m_category == TC_MOUSE ); // this should be used only with mouse events
265  return m_mouseButtons;
266  }
267 
268  bool IsClick( int aButtonMask = BUT_ANY ) const
269  {
270  return ( m_actions == TA_MOUSE_CLICK )
271  && ( ( m_mouseButtons & aButtonMask ) == aButtonMask );
272  }
273 
274  bool IsDblClick( int aButtonMask = BUT_ANY ) const
275  {
276  return ( m_actions == TA_MOUSE_DBLCLICK )
277  && ( ( m_mouseButtons & aButtonMask ) == aButtonMask );
278  }
279 
280  bool IsDrag( int aButtonMask = BUT_ANY ) const
281  {
282  return ( m_actions == TA_MOUSE_DRAG ) && ( ( m_mouseButtons & aButtonMask ) == aButtonMask );
283  }
284 
285  bool IsMouseUp( int aButtonMask = BUT_ANY ) const
286  {
287  return ( m_actions == TA_MOUSE_UP ) && ( ( m_mouseButtons & aButtonMask ) == aButtonMask );
288  }
289 
290  bool IsMotion() const
291  {
292  return m_actions == TA_MOUSE_MOTION;
293  }
294 
295  bool IsCancel() const
296  {
297  return m_actions == TA_CANCEL_TOOL;
298  }
299 
300  bool IsActivate() const
301  {
302  return m_actions == TA_ACTIVATE;
303  }
304 
305  bool IsUndoRedo() const
306  {
308  }
309 
311  int Modifier( int aMask = MD_MODIFIER_MASK ) const
312  {
313  return m_modifiers & aMask;
314  }
315 
316  int KeyCode() const
317  {
318  return m_keyCode;
319  }
320 
321  bool IsKeyPressed() const
322  {
323  return m_actions == TA_KEY_PRESSED;
324  }
325 
333  bool Matches( const TOOL_EVENT& aEvent ) const
334  {
335  if( !( m_category & aEvent.m_category ) )
336  return false;
337 
339  {
340  if( (bool) m_commandStr && (bool) aEvent.m_commandStr )
341  return *m_commandStr == *aEvent.m_commandStr;
342 
343  if( (bool) m_commandId && (bool) aEvent.m_commandId )
344  return *m_commandId == *aEvent.m_commandId;
345  }
346 
347  // BUGFIX: TA_ANY should match EVERYTHING, even TA_NONE (for TC_MESSAGE)
348  if( m_actions == TA_ANY && aEvent.m_actions == TA_NONE && aEvent.m_category == TC_MESSAGE )
349  return true;
350 
351  // BUGFIX: This check must happen after the TC_COMMAND check because otherwise events of
352  // the form { TC_COMMAND, TA_NONE } will be incorrectly skipped
353  if( !( m_actions & aEvent.m_actions ) )
354  return false;
355 
356  return true;
357  }
358 
365  bool IsAction( const TOOL_ACTION* aAction ) const;
366 
372  template<typename T>
373  inline T Parameter() const
374  {
375  return reinterpret_cast<T>( m_param );
376  }
377 
384  template<typename T>
385  void SetParameter(T aParam)
386  {
387  m_param = (void*) aParam;
388  }
389 
391  {
392  return m_commandId;
393  }
394 
396  {
397  return m_commandStr;
398  }
399 
400  void SetMousePosition( const VECTOR2D& aP )
401  {
402  m_mousePos = aP;
403  }
404 
405 private:
406  friend class TOOL_DISPATCHER;
407 
408  void setMouseDragOrigin( const VECTOR2D& aP )
409  {
410  m_mouseDragOrigin = aP;
411  }
412 
413  void setMouseDelta( const VECTOR2D& aP )
414  {
415  m_mouseDelta = aP;
416  }
417 
418  void setMouseButtons( int aButtons )
419  {
420  assert( ( aButtons & ~BUT_BUTTON_MASK ) == 0 );
421  m_mouseButtons = aButtons;
422  }
423 
424  void setModifiers( int aMods )
425  {
426  assert( ( aMods & ~MD_MODIFIER_MASK ) == 0 );
427  m_modifiers = aMods;
428  }
429 
433 
437 
440 
443 
446 
449 
452 
454  void* m_param;
455 
458 };
459 
461 
469 {
470 public:
472  typedef std::deque<TOOL_EVENT>::iterator iterator;
473  typedef std::deque<TOOL_EVENT>::const_iterator const_iterator;
474 
477  {}
478 
480  TOOL_EVENT_LIST( const TOOL_EVENT& aSingleEvent )
481  {
482  m_events.push_back( aSingleEvent );
483  }
484 
491  const std::string Format() const;
492 
494  {
495  for( const_iterator i = m_events.begin(); i != m_events.end(); ++i )
496  if( i->Matches( aEvent ) )
497  return *i;
498 
500  }
501 
507  void Add( const TOOL_EVENT& aEvent )
508  {
509  m_events.push_back( aEvent );
510  }
511 
512  iterator begin()
513  {
514  return m_events.begin();
515  }
516 
517  iterator end()
518  {
519  return m_events.end();
520  }
521 
522  const_iterator cbegin() const
523  {
524  return m_events.begin();
525  }
526 
527  const_iterator cend() const
528  {
529  return m_events.end();
530  }
531 
532  int size() const
533  {
534  return m_events.size();
535  }
536 
537  void clear()
538  {
539  m_events.clear();
540  }
541 
543  {
544  m_events.clear();
545 
546  for( std::deque<TOOL_EVENT>::const_iterator i = aEventList.m_events.begin();
547  i != aEventList.m_events.end(); ++i )
548  {
549  m_events.push_back( *i );
550  }
551 
552  return *this;
553  }
554 
556  {
557  m_events.clear();
558  m_events.push_back( aEvent );
559  return *this;
560  }
561 
563  {
564  Add( aEvent );
565  return *this;
566  }
567 
569  {
570  std::copy( aEvent.m_events.begin(), aEvent.m_events.end(), std::back_inserter( m_events ) );
571  return *this;
572  }
573 
574 private:
575  std::deque<TOOL_EVENT> m_events;
576 };
577 
578 inline const TOOL_EVENT_LIST operator||( const TOOL_EVENT& aEventA, const TOOL_EVENT& aEventB )
579 {
580  TOOL_EVENT_LIST l;
581 
582  l.Add( aEventA );
583  l.Add( aEventB );
584 
585  return l;
586 }
587 
588 inline const TOOL_EVENT_LIST operator||( const TOOL_EVENT& aEvent,
589  const TOOL_EVENT_LIST& aEventList )
590 {
591  TOOL_EVENT_LIST l( aEventList );
592 
593  l.Add( aEvent );
594  return l;
595 }
596 
597 #endif
void setMouseButtons(int aButtons)
Definition: tool_event.h:418
void setModifiers(int aMods)
Definition: tool_event.h:424
void setMouseDelta(const VECTOR2D &aP)
Definition: tool_event.h:413
TOOL_EVENT value_type
Definition: tool_event.h:471
bool IsDrag(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:280
bool IsUndoRedo() const
Definition: tool_event.h:305
iterator begin()
Definition: tool_event.h:512
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:60
const VECTOR2D & DragOrigin() const
Returns the point where dragging has started.
Definition: tool_event.h:255
bool IsMotion() const
Definition: tool_event.h:290
T
enum T contains all this lexer's tokens.
int m_modifiers
State of key modifierts (Ctrl/Alt/etc.)
Definition: tool_event.h:451
TOOL_ACTIONS m_actions
Definition: tool_event.h:431
TOOL_ACTIONS
Definition: tool_event.h:55
boost::optional< int > GetCommandId() const
Definition: tool_event.h:390
int m_mouseButtons
State of mouse buttons
Definition: tool_event.h:445
Action activates a tool
Definition: tool_event.h:146
TOOL_EVENT(TOOL_EVENT_CATEGORY aCategory=TC_NONE, TOOL_ACTIONS aAction=TA_NONE, TOOL_ACTION_SCOPE aScope=AS_GLOBAL, void *aParameter=NULL)
Definition: tool_event.h:173
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:54
VECTOR2D m_mouseDelta
Difference between mouse cursor position and the point where dragging event has started ...
Definition: tool_event.h:436
TOOL_EVENT_CATEGORY
Internal (GUI-independent) event definitions.
Definition: tool_event.h:44
TOOL_EVENT(TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, const std::string &aExtraParam, TOOL_ACTION_SCOPE aScope=AS_GLOBAL, void *aParameter=NULL)
Definition: tool_event.h:212
int Modifier(int aMask=MD_MODIFIER_MASK) const
Returns information about key modifiers state (Ctrl, Alt, etc.)
Definition: tool_event.h:311
const VECTOR2D & Position() const
Returns mouse cursor position in world coordinates.
Definition: tool_event.h:248
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_event.h:134
int m_keyCode
Stores code of pressed/released key
Definition: tool_event.h:448
TOOL_EVENT_LIST & operator=(const TOOL_EVENT &aEvent)
Definition: tool_event.h:555
bool IsKeyPressed() const
Definition: tool_event.h:321
int KeyCode() const
Definition: tool_event.h:316
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:274
VECTOR2D m_mouseDragOrigin
Point where dragging has started
Definition: tool_event.h:442
TOOL_EVENT_LIST & operator||(const TOOL_EVENT_LIST &aEvent)
Definition: tool_event.h:568
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_event.h:142
void SetMousePosition(const VECTOR2D &aP)
Definition: tool_event.h:400
boost::optional< int > m_commandId
Definition: tool_event.h:456
boost::optional< const TOOL_EVENT & > Matches(const TOOL_EVENT &aEvent) const
Definition: tool_event.h:493
boost::optional< std::string > GetCommandStr() const
Definition: tool_event.h:395
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:575
boost::optional< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:460
TOOL_EVENT(TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, int aExtraParam, TOOL_ACTION_SCOPE aScope=AS_GLOBAL, void *aParameter=NULL)
Definition: tool_event.h:183
TOOL_EVENT_LIST()
Default constructor. Creates an empty list.
Definition: tool_event.h:476
Class TOOL_MANAGER.
Definition: tool_manager.h:49
void SetParameter(T aParam)
Function SetParameter() Sets a non-standard parameter assigned to the event.
Definition: tool_event.h:385
Class TOOL_EVENT_LIST.
Definition: tool_event.h:468
Action belongs to a particular tool (i.e. a part of a pop-up menu)
Definition: tool_event.h:137
TOOL_ACTION_SCOPE m_scope
Definition: tool_event.h:432
const_iterator cend() const
Definition: tool_event.h:527
TOOL_EVENT_LIST & operator||(const TOOL_EVENT &aEvent)
Definition: tool_event.h:562
CONTEXT_MENU_TRIGGER
Defines when a context menu is opened.
Definition: tool_event.h:150
boost::optional< std::string > m_commandStr
Definition: tool_event.h:457
Class TOOL_EVENT.
Definition: tool_event.h:162
TOOL_EVENT_LIST(const TOOL_EVENT &aSingleEvent)
Constructor for a list containing only one TOOL_EVENT.
Definition: tool_event.h:480
bool IsActivate() const
Definition: tool_event.h:300
bool IsCancel() const
Definition: tool_event.h:295
std::deque< TOOL_EVENT >::const_iterator const_iterator
Definition: tool_event.h:473
bool IsMouseUp(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:285
All active tools
Definition: tool_event.h:138
TOOL_ACTIONS Action() const
Returns more specific information about the type of an event.
Definition: tool_event.h:234
TOOL_EVENT_CATEGORY Category() const
Returns the category (eg. mouse/keyboard/action) of an event..
Definition: tool_event.h:228
Class TOOL_DISPATCHER.
int size() const
Definition: tool_event.h:532
TOOL_MOUSE_BUTTONS
Definition: tool_event.h:115
bool Matches(const TOOL_EVENT &aEvent) const
Function Matches() Tests whether two events match in terms of category & action or command...
Definition: tool_event.h:333
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:373
void Add(const TOOL_EVENT &aEvent)
Function Add() Adds a tool event to the list.
Definition: tool_event.h:507
const VECTOR2D & Delta() const
Returns information about difference between current mouse cursor position and the place where draggi...
Definition: tool_event.h:241
Class TOOL_ACTION.
Definition: tool_action.h:46
TOOL_EVENT_LIST & operator=(const TOOL_EVENT_LIST &aEventList)
Definition: tool_event.h:542
void * m_param
Generic parameter used for passing non-standard data.
Definition: tool_event.h:454
TOOL_MODIFIERS
Definition: tool_event.h:125
int Buttons() const
Returns information about mouse buttons state.
Definition: tool_event.h:262
std::deque< TOOL_EVENT >::iterator iterator
Definition: tool_event.h:472
void setMouseDragOrigin(const VECTOR2D &aP)
Definition: tool_event.h:408
iterator end()
Definition: tool_event.h:517
VECTOR2D m_mousePos
Current mouse cursor position
Definition: tool_event.h:439
const TOOL_EVENT_LIST operator||(const TOOL_EVENT &aEventA, const TOOL_EVENT &aEventB)
Definition: tool_event.h:578
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:268
const_iterator cbegin() const
Definition: tool_event.h:522
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:430
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:154