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