KiCad PCB EDA Suite
tool_event.cpp
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 #include <cstring>
26 #include <string>
27 
28 #include <tool/tool_event.h>
29 #include <tool/tool_action.h>
30 #include <tool/tool_manager.h>
31 #include <tool/actions.h>
32 
33 #ifdef WX_COMPATIBILITY
34 #include <wx/debug.h>
35 #else
36 #include <cassert>
37 #endif
38 
39 struct FlagString
40 {
41  int flag;
42  std::string str;
43 };
44 
45 
46 static const std::string flag2string( int aFlag, const FlagString* aExps )
47 {
48  std::string rv;
49 
50  for( int i = 0; aExps[i].str.length(); i++ )
51  {
52  if( aExps[i].flag & aFlag )
53  rv += aExps[i].str + " ";
54  }
55 
56  return rv;
57 }
58 
59 
61 {
62  // By default only MESSAGEs and Cancels are passed to multiple recipients
64 
66 
67  // Cancel tool doesn't contain a position
68  if( IsCancel() )
69  m_hasPosition = false;
70 
71  m_forceImmediate = false;
72  m_reactivate = false;
73 }
74 
75 
77 {
78 #ifdef WX_COMPATIBILITY
79  wxCHECK_MSG( HasPosition(), VECTOR2D(), "Attempted to get position from non-position event" );
80 #else
81  assert( HasPosition() );
82 #endif
83 
84  return aPos;
85 }
86 
87 
88 bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
89 {
90  return Matches( aAction->MakeEvent() );
91 }
92 
93 
94 const std::string TOOL_EVENT::Format() const
95 {
96  std::string ev;
97 
98  const FlagString categories[] =
99  {
100  { TC_MOUSE, "mouse" },
101  { TC_KEYBOARD, "keyboard" },
102  { TC_COMMAND, "command" },
103  { TC_MESSAGE, "message" },
104  { TC_VIEW, "view" },
105  { 0, "" }
106  };
107 
108  const FlagString actions[] =
109  {
110  { TA_MOUSE_CLICK, "click" },
111  { TA_MOUSE_DBLCLICK, "double click" },
112  { TA_MOUSE_UP, "button-up" },
113  { TA_MOUSE_DOWN, "button-down" },
114  { TA_MOUSE_DRAG, "drag" },
115  { TA_MOUSE_MOTION, "motion" },
116  { TA_MOUSE_WHEEL, "wheel" },
117  { TA_KEY_PRESSED, "key-pressed" },
118  { TA_VIEW_REFRESH, "view-refresh" },
119  { TA_VIEW_ZOOM, "view-zoom" },
120  { TA_VIEW_PAN, "view-pan" },
121  { TA_VIEW_DIRTY, "view-dirty" },
122  { TA_CHANGE_LAYER, "change-layer" },
123  { TA_CANCEL_TOOL, "cancel-tool" },
124  { TA_CHOICE_MENU_UPDATE, "choice-menu-update" },
125  { TA_CHOICE_MENU_CHOICE, "choice-menu-choice" },
126  { TA_UNDO_REDO_PRE, "undo-redo-pre" },
127  { TA_UNDO_REDO_POST, "undo-redo-post" },
128  { TA_ACTION, "action" },
129  { TA_ACTIVATE, "activate" },
130  { 0, "" }
131  };
132 
133  const FlagString buttons[] =
134  {
135  { BUT_NONE, "none" },
136  { BUT_LEFT, "left" },
137  { BUT_RIGHT, "right" },
138  { BUT_MIDDLE, "middle" },
139  { 0, "" }
140  };
141 
142  const FlagString modifiers[] =
143  {
144  { MD_SHIFT, "shift" },
145  { MD_CTRL, "ctrl" },
146  { MD_ALT, "alt" },
147  { 0, "" }
148  };
149 
150  ev = "category: ";
151  ev += flag2string( m_category, categories );
152  ev += " action: ";
153  ev += flag2string( m_actions, actions );
154 
155  if( m_actions & TA_MOUSE )
156  {
157  ev += " btns: ";
158  ev += flag2string( m_mouseButtons, buttons );
159  }
160 
161  if( m_actions & TA_KEYBOARD )
162  {
163  char tmp[128];
164  sprintf( tmp, "key: %d", m_keyCode );
165  ev += tmp;
166  }
167 
168  if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
169  {
170  ev += " mods: ";
171  ev += flag2string( m_modifiers, modifiers );
172  }
173 
174  if( m_commandId )
175  {
176  char tmp[128];
177  sprintf( tmp, "cmd-id: %d", *m_commandId );
178  ev += tmp;
179  }
180 
181  if( m_commandStr )
182  ev += "cmd-str: " + ( *m_commandStr );
183 
184  return ev;
185 }
186 
187 
188 const std::string TOOL_EVENT_LIST::Format() const
189 {
190  std::string s;
191 
192  for( const TOOL_EVENT& e : m_events )
193  s += e.Format() + " ";
194 
195  return s;
196 }
197 
198 
199 bool TOOL_EVENT::IsClick( int aButtonMask ) const
200 {
201  return ( m_actions & TA_MOUSE_CLICK ) && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
202 }
203 
204 
205 bool TOOL_EVENT::IsDblClick( int aButtonMask ) const
206 {
207  return m_actions == TA_MOUSE_DBLCLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
208 }
209 
210 
212 {
213  return( ( m_commandStr.is_initialized()
215  || ( m_commandId.is_initialized()
217  || ( m_actions == TA_CANCEL_TOOL ) );
218 }
219 
220 
222 {
223  return Matches( EVENTS::ClearedEvent )
226 }
227 
228 
230 {
231  return( ( m_commandStr.is_initialized()
232  && m_commandStr.get().find( "PointEditor" ) != GetCommandStr()->npos )
233  || ( m_commandId.is_initialized()
235 }
236 
237 
239 {
240  return( m_commandStr.is_initialized()
241  && m_commandStr.get().find( "InteractiveMove" ) != GetCommandStr()->npos );
242 }
243 
244 
246 {
247  return( m_commandStr.is_initialized()
248  && m_commandStr.get().find( "Simulation" ) != GetCommandStr()->npos );
249 }
bool IsCancel() const
Definition: tool_event.h:310
bool m_hasPosition
Definition: tool_event.h:513
static const TOOL_EVENT SelectedEvent
Definition: actions.h:199
bool m_forceImmediate
True when the tool is being re-activated from the stack.
Definition: tool_event.h:514
const std::string Format() const
Return information about event in form of a human-readable string.
Definition: tool_event.cpp:94
static TOOL_ACTION activatePointEditor
Definition: actions.h:168
bool IsSelectionEvent() const
Indicate an selection-changed notification event.
Definition: tool_event.cpp:221
void init()
Definition: tool_event.cpp:60
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:200
int m_modifiers
Generic parameter used for passing non-standard data.
Definition: tool_event.h:536
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:199
TOOL_ACTIONS m_actions
Definition: tool_event.h:510
static TOOL_ACTION cancelInteractive
Definition: actions.h:62
int m_mouseButtons
Stores code of pressed/released key.
Definition: tool_event.h:530
VECTOR2D returnCheckedPosition(const VECTOR2D &aPos) const
Ensure that the event is a type that has a position before returning a position, otherwise return a n...
Definition: tool_event.cpp:76
OPT< std::string > m_commandStr
Definition: tool_event.h:545
bool IsMoveTool() const
Indicate if the event is from one of the move tools.
Definition: tool_event.cpp:238
int m_keyCode
State of key modifiers (Ctrl/Alt/etc.)
Definition: tool_event.h:533
OPT< int > m_commandId
Definition: tool_event.h:544
bool IsCancelInteractive() const
Indicate the event should restart/end an ongoing interactive tool's event loop (eg esc key,...
Definition: tool_event.cpp:211
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:88
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:669
bool m_passEvent
Definition: tool_event.h:512
bool IsSimulator() const
Indicate if the event is from the simulator.
Definition: tool_event.cpp:245
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:205
Generic, UI-independent tool event.
Definition: tool_event.h:152
bool IsPointEditor() const
Indicate if the event is from one of the point editors.
Definition: tool_event.cpp:229
static const TOOL_EVENT ClearedEvent
Selected item had a property changed (except movement)
Definition: actions.h:201
bool Matches(const TOOL_EVENT &aEvent) const
Test whether two events match in terms of category & action or command.
Definition: tool_event.h:362
bool m_reactivate
Difference between mouse cursor position and the point where dragging event has started.
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:188
static const std::string flag2string(int aFlag, const FlagString *aExps)
Definition: tool_event.cpp:46
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:460
bool IsActivate() const
Definition: tool_event.h:315
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:121
std::string str
Definition: tool_event.cpp:42
Represent a single user action.
Definition: tool_action.h:67
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
bool HasPosition() const
Definition: tool_event.h:240
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:509
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:101