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 <core/optional.h>
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 
310  bool IsMenu() const
311  {
312  return m_actions & TA_CONTEXT_MENU;
313  }
314 
316  int Modifier( int aMask = MD_MODIFIER_MASK ) const
317  {
318  return m_modifiers & aMask;
319  }
320 
321  int KeyCode() const
322  {
323  return m_keyCode;
324  }
325 
326  bool IsKeyPressed() const
327  {
328  return m_actions == TA_KEY_PRESSED;
329  }
330 
338  bool Matches( const TOOL_EVENT& aEvent ) const
339  {
340  if( !( m_category & aEvent.m_category ) )
341  return false;
342 
344  {
345  if( (bool) m_commandStr && (bool) aEvent.m_commandStr )
346  return *m_commandStr == *aEvent.m_commandStr;
347 
348  if( (bool) m_commandId && (bool) aEvent.m_commandId )
349  return *m_commandId == *aEvent.m_commandId;
350  }
351 
352  // BUGFIX: TA_ANY should match EVERYTHING, even TA_NONE (for TC_MESSAGE)
353  if( m_actions == TA_ANY && aEvent.m_actions == TA_NONE && aEvent.m_category == TC_MESSAGE )
354  return true;
355 
356  // BUGFIX: This check must happen after the TC_COMMAND check because otherwise events of
357  // the form { TC_COMMAND, TA_NONE } will be incorrectly skipped
358  if( !( m_actions & aEvent.m_actions ) )
359  return false;
360 
361  return true;
362  }
363 
370  bool IsAction( const TOOL_ACTION* aAction ) const;
371 
377  template<typename T>
378  inline T Parameter() const
379  {
380  return reinterpret_cast<T>( m_param );
381  }
382 
389  template<typename T>
390  void SetParameter(T aParam)
391  {
392  m_param = (void*) aParam;
393  }
394 
396  {
397  return m_commandId;
398  }
399 
401  {
402  return m_commandStr;
403  }
404 
405  void SetMousePosition( const VECTOR2D& aP )
406  {
407  m_mousePos = aP;
408  }
409 
410 private:
411  friend class TOOL_DISPATCHER;
412 
413  void setMouseDragOrigin( const VECTOR2D& aP )
414  {
415  m_mouseDragOrigin = aP;
416  }
417 
418  void setMouseDelta( const VECTOR2D& aP )
419  {
420  m_mouseDelta = aP;
421  }
422 
423  void setMouseButtons( int aButtons )
424  {
425  assert( ( aButtons & ~BUT_BUTTON_MASK ) == 0 );
426  m_mouseButtons = aButtons;
427  }
428 
429  void setModifiers( int aMods )
430  {
431  assert( ( aMods & ~MD_MODIFIER_MASK ) == 0 );
432  m_modifiers = aMods;
433  }
434 
438 
442 
445 
448 
451 
454 
457 
459  void* m_param;
460 
463 };
464 
466 
474 {
475 public:
477  typedef std::deque<TOOL_EVENT>::iterator iterator;
478  typedef std::deque<TOOL_EVENT>::const_iterator const_iterator;
479 
482  {}
483 
485  TOOL_EVENT_LIST( const TOOL_EVENT& aSingleEvent )
486  {
487  m_events.push_back( aSingleEvent );
488  }
489 
496  const std::string Format() const;
497 
499  {
500  for( const_iterator i = m_events.begin(); i != m_events.end(); ++i )
501  if( i->Matches( aEvent ) )
502  return *i;
503 
504  return OPT<const TOOL_EVENT&>();
505  }
506 
512  void Add( const TOOL_EVENT& aEvent )
513  {
514  m_events.push_back( aEvent );
515  }
516 
517  iterator begin()
518  {
519  return m_events.begin();
520  }
521 
522  iterator end()
523  {
524  return m_events.end();
525  }
526 
527  const_iterator cbegin() const
528  {
529  return m_events.begin();
530  }
531 
532  const_iterator cend() const
533  {
534  return m_events.end();
535  }
536 
537  int size() const
538  {
539  return m_events.size();
540  }
541 
542  void clear()
543  {
544  m_events.clear();
545  }
546 
548  {
549  m_events.clear();
550 
551  for( std::deque<TOOL_EVENT>::const_iterator i = aEventList.m_events.begin();
552  i != aEventList.m_events.end(); ++i )
553  {
554  m_events.push_back( *i );
555  }
556 
557  return *this;
558  }
559 
561  {
562  m_events.clear();
563  m_events.push_back( aEvent );
564  return *this;
565  }
566 
568  {
569  Add( aEvent );
570  return *this;
571  }
572 
574  {
575  std::copy( aEvent.m_events.begin(), aEvent.m_events.end(), std::back_inserter( m_events ) );
576  return *this;
577  }
578 
579 private:
580  std::deque<TOOL_EVENT> m_events;
581 };
582 
583 inline const TOOL_EVENT_LIST operator||( const TOOL_EVENT& aEventA, const TOOL_EVENT& aEventB )
584 {
585  TOOL_EVENT_LIST l;
586 
587  l.Add( aEventA );
588  l.Add( aEventB );
589 
590  return l;
591 }
592 
593 inline const TOOL_EVENT_LIST operator||( const TOOL_EVENT& aEvent,
594  const TOOL_EVENT_LIST& aEventList )
595 {
596  TOOL_EVENT_LIST l( aEventList );
597 
598  l.Add( aEvent );
599  return l;
600 }
601 
602 #endif
void setMouseButtons(int aButtons)
Definition: tool_event.h:423
void setModifiers(int aMods)
Definition: tool_event.h:429
void setMouseDelta(const VECTOR2D &aP)
Definition: tool_event.h:418
TOOL_EVENT value_type
Definition: tool_event.h:476
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:517
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
int m_modifiers
State of key modifierts (Ctrl/Alt/etc.)
Definition: tool_event.h:456
TOOL_ACTIONS m_actions
Definition: tool_event.h:436
TOOL_ACTIONS
Definition: tool_event.h:55
int m_mouseButtons
State of mouse buttons
Definition: tool_event.h:450
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
OPT< std::string > m_commandStr
Definition: tool_event.h:462
VECTOR2D m_mouseDelta
Difference between mouse cursor position and the point where dragging event has started ...
Definition: tool_event.h:441
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:316
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:453
TOOL_EVENT_LIST & operator=(const TOOL_EVENT &aEvent)
Definition: tool_event.h:560
bool IsKeyPressed() const
Definition: tool_event.h:326
int KeyCode() const
Definition: tool_event.h:321
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:274
VECTOR2D m_mouseDragOrigin
Point where dragging has started
Definition: tool_event.h:447
TOOL_EVENT_LIST & operator||(const TOOL_EVENT_LIST &aEvent)
Definition: tool_event.h:573
OPT< int > m_commandId
Definition: tool_event.h:461
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_event.h:142
void SetMousePosition(const VECTOR2D &aP)
Definition: tool_event.h:405
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:580
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:481
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:390
Class TOOL_EVENT_LIST.
Definition: tool_event.h:473
Action belongs to a particular tool (i.e. a part of a pop-up menu)
Definition: tool_event.h:137
OPT< int > GetCommandId() const
Definition: tool_event.h:395
TOOL_ACTION_SCOPE m_scope
Definition: tool_event.h:437
const_iterator cend() const
Definition: tool_event.h:532
TOOL_EVENT_LIST & operator||(const TOOL_EVENT &aEvent)
Definition: tool_event.h:567
CONTEXT_MENU_TRIGGER
Defines when a context menu is opened.
Definition: tool_event.h:150
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:485
bool IsActivate() const
Definition: tool_event.h:300
bool IsCancel() const
Definition: tool_event.h:295
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:400
std::deque< TOOL_EVENT >::const_iterator const_iterator
Definition: tool_event.h:478
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
OPT< const TOOL_EVENT & > Matches(const TOOL_EVENT &aEvent) const
Definition: tool_event.h:498
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:537
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:338
bool IsMenu() const
Definition: tool_event.h:310
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:378
void Add(const TOOL_EVENT &aEvent)
Function Add() Adds a tool event to the list.
Definition: tool_event.h:512
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
size_t i
Definition: json11.cpp:597
TOOL_EVENT_LIST & operator=(const TOOL_EVENT_LIST &aEventList)
Definition: tool_event.h:547
void * m_param
Generic parameter used for passing non-standard data.
Definition: tool_event.h:459
boost::optional< T > OPT
Definition: optional.h:7
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:477
void setMouseDragOrigin(const VECTOR2D &aP)
Definition: tool_event.h:413
iterator end()
Definition: tool_event.h:522
VECTOR2D m_mousePos
Current mouse cursor position
Definition: tool_event.h:444
const TOOL_EVENT_LIST operator||(const TOOL_EVENT &aEventA, const TOOL_EVENT &aEventB)
Definition: tool_event.h:583
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:268
const_iterator cbegin() const
Definition: tool_event.h:527
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:435
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:465