KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 CERN
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <cstring>
27#include <string>
28
29#include <tool/tool_event.h>
30#include <tool/tool_action.h>
31#include <tool/tool_manager.h>
32#include <tool/actions.h>
33
34
35#include <wx/debug.h>
36
38{
39 int flag;
40 std::string str;
41};
42
43
44static const std::string flag2string( int aFlag, const FlagString* aExps )
45{
46 std::string rv;
47
48 for( int i = 0; aExps[i].str.length(); i++ )
49 {
50 if( aExps[i].flag & aFlag )
51 rv += aExps[i].str + " ";
52 }
53
54 return rv;
55}
56
57
59{
60 // By default only MESSAGEs and Cancels are passed to multiple recipients
62
64
65 // Cancel tool doesn't contain a position
66 if( IsCancel() )
67 m_hasPosition = false;
68
69 m_forceImmediate = false;
70 m_reactivate = false;
71}
72
73
75{
76 wxCHECK_MSG( HasPosition(), VECTOR2D(), "Attempted to get position from non-position event" );
77
78 return aPos;
79}
80
81
82bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
83{
84 return Matches( aAction->MakeEvent() );
85}
86
87
89{
90 if( m_actionGroup.has_value() )
91 return m_actionGroup.value() == aGroup;
92
93 return false;
94}
95
96
97const std::string TOOL_EVENT::Format() const
98{
99 std::string ev;
100
101 const FlagString categories[] =
102 {
103 { TC_MOUSE, "mouse" },
104 { TC_KEYBOARD, "keyboard" },
105 { TC_COMMAND, "command" },
106 { TC_MESSAGE, "message" },
107 { TC_VIEW, "view" },
108 { 0, "" }
109 };
110
111 const FlagString actions[] =
112 {
113 { TA_MOUSE_CLICK, "click" },
114 { TA_MOUSE_DBLCLICK, "double click" },
115 { TA_MOUSE_UP, "button-up" },
116 { TA_MOUSE_DOWN, "button-down" },
117 { TA_MOUSE_DRAG, "drag" },
118 { TA_MOUSE_MOTION, "motion" },
119 { TA_MOUSE_WHEEL, "wheel" },
120 { TA_KEY_PRESSED, "key-pressed" },
121 { TA_VIEW_REFRESH, "view-refresh" },
122 { TA_VIEW_ZOOM, "view-zoom" },
123 { TA_VIEW_PAN, "view-pan" },
124 { TA_VIEW_DIRTY, "view-dirty" },
125 { TA_CHANGE_LAYER, "change-layer" },
126 { TA_CANCEL_TOOL, "cancel-tool" },
127 { TA_CHOICE_MENU_UPDATE, "choice-menu-update" },
128 { TA_CHOICE_MENU_CHOICE, "choice-menu-choice" },
129 { TA_UNDO_REDO_PRE, "undo-redo-pre" },
130 { TA_UNDO_REDO_POST, "undo-redo-post" },
131 { TA_ACTION, "action" },
132 { TA_ACTIVATE, "activate" },
133 { 0, "" }
134 };
135
136 const FlagString buttons[] =
137 {
138 { BUT_NONE, "none" },
139 { BUT_LEFT, "left" },
140 { BUT_RIGHT, "right" },
141 { BUT_MIDDLE, "middle" },
142 { BUT_AUX1, "aux1" },
143 { BUT_AUX2, "aux2" },
144 { 0, "" }
145 };
146
147 const FlagString modifiers[] =
148 {
149 { MD_SHIFT, "shift" },
150 { MD_CTRL, "ctrl" },
151 { MD_ALT, "alt" },
152 { 0, "" }
153 };
154
155 ev = "category: " + flag2string( m_category, categories ) + " ";
156 ev += "action: " + flag2string( m_actions, actions ) + " ";
157 ev += "action-group: ";
158
159 if( m_actionGroup.has_value() )
160 {
161 ev += m_actionGroup.value().GetName() +
162 "(" + std::to_string( m_actionGroup.value().GetGroupID() ) + ")" + " ";
163 }
164 else
165 {
166 ev += "none ";
167 }
168
169 if( m_actions & TA_MOUSE )
170 ev += "btns: " + flag2string( m_mouseButtons, buttons ) + " ";
171
172 if( m_actions & TA_KEYBOARD )
173 ev += "key: " + std::to_string( m_keyCode ) + " ";
174
175 if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
176 ev += "mods: " + flag2string( m_modifiers, modifiers ) + " ";
177
178 if( m_commandId )
179 ev += "cmd-id: " + std::to_string( *m_commandId ) + " ";
180
181 ev += "cmd-str: " + m_commandStr;
182
183 return ev;
184}
185
186
187const std::string TOOL_EVENT_LIST::Format() const
188{
189 std::string s;
190
191 for( const TOOL_EVENT& e : m_events )
192 s += e.Format() + " ";
193
194 return s;
195}
196
197
198const std::string TOOL_EVENT_LIST::Names() const
199{
200 std::string s;
201
202 for( const TOOL_EVENT& e : m_events )
203 s += e.m_commandStr + " ";
204
205 return s;
206}
207
208
209bool TOOL_EVENT::IsClick( int aButtonMask ) const
210{
211 return ( m_actions & TA_MOUSE_CLICK ) && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
212}
213
214
215bool TOOL_EVENT::IsDblClick( int aButtonMask ) const
216{
217 return m_actions == TA_MOUSE_DBLCLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
218}
219
220
222{
223 return ( ( m_commandStr == ACTIONS::cancelInteractive.GetName() )
225 || ( m_actions == TA_CANCEL_TOOL ) );
226}
227
228
230{
235}
236
237
239{
240 return ( ( m_commandStr.find( "PointEditor" ) != getCommandStr().npos )
242}
243
244
246{
247 return ( m_commandStr.find( "InteractiveMove" ) != getCommandStr().npos );
248}
249
250
252{
253 return ( m_commandStr.find( "InteractiveEdit" ) != getCommandStr().npos );
254}
255
256
258{
259 return ( m_commandStr.find( "Simulation" ) != getCommandStr().npos );
260}
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION activatePointEditor
Definition: actions.h:205
static const TOOL_EVENT ClearedEvent
Definition: actions.h:262
static const TOOL_EVENT SelectedEvent
Definition: actions.h:260
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:259
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:261
Define a group that can be used to group actions (and their events) of similar operations.
Definition: tool_action.h:63
Represent a single user action.
Definition: tool_action.h:269
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:187
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:757
const std::string Names() const
Returns a string containing the names of all the events in this list.
Definition: tool_event.cpp:198
Generic, UI-independent tool event.
Definition: tool_event.h:167
bool HasPosition() const
Definition: tool_event.h:256
bool IsCancelInteractive() const
Indicate the event should restart/end an ongoing interactive tool's event loop (eg esc key,...
Definition: tool_event.cpp:221
bool m_hasPosition
Definition: tool_event.h:585
std::optional< int > m_commandId
Definition: tool_event.h:625
int m_keyCode
State of key modifiers (Ctrl/Alt/etc.)
Definition: tool_event.h:609
int m_modifiers
Definition: tool_event.h:612
bool Matches(const TOOL_EVENT &aEvent) const
Test whether two events match in terms of category & action or command.
Definition: tool_event.h:384
bool IsActivate() const
Definition: tool_event.h:337
std::string m_commandStr
Definition: tool_event.h:626
bool IsSimulator() const
Indicate if the event is from the simulator.
Definition: tool_event.cpp:257
bool IsSelectionEvent() const
Indicate an selection-changed notification event.
Definition: tool_event.cpp:229
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:209
bool m_passEvent
Definition: tool_event.h:584
void init()
Definition: tool_event.cpp:58
bool IsMoveTool() const
Indicate if the event is from one of the move tools.
Definition: tool_event.cpp:245
TOOL_ACTIONS m_actions
Definition: tool_event.h:582
std::optional< TOOL_ACTION_GROUP > m_actionGroup
True when the tool is being re-activated from the stack.
Definition: tool_event.h:590
bool m_forceImmediate
Optional group that the parent action for the event belongs to.
Definition: tool_event.h:586
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:82
bool IsActionInGroup(const TOOL_ACTION_GROUP &aGroup) const
Definition: tool_event.cpp:88
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:74
bool IsEditorTool() const
Indicate if the event is asking for an editor tool.
Definition: tool_event.cpp:251
bool m_reactivate
Difference between mouse cursor position and the point where dragging event has started.
Definition: tool_event.h:593
bool IsCancel() const
Definition: tool_event.h:332
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:215
const std::string & getCommandStr() const
Definition: tool_event.h:545
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:581
bool IsPointEditor() const
Indicate if the event is from one of the point editors.
Definition: tool_event.cpp:238
int m_mouseButtons
Stores code of pressed/released key.
Definition: tool_event.h:606
const std::string Format() const
Return information about event in form of a human-readable string.
Definition: tool_event.cpp:97
std::string str
Definition: tool_event.cpp:40
static const std::string flag2string(int aFlag, const FlagString *aExps)
Definition: tool_event.cpp:44
@ TA_CHOICE_MENU_CHOICE
Definition: tool_event.h:97
@ TA_UNDO_REDO_PRE
Definition: tool_event.h:105
@ TA_MOUSE_CLICK
Definition: tool_event.h:66
@ TA_CHOICE_MENU_UPDATE
Definition: tool_event.h:93
@ TA_MOUSE
Definition: tool_event.h:73
@ TA_ACTIVATE
Definition: tool_event.h:114
@ TA_MOUSE_MOTION
Definition: tool_event.h:71
@ TA_MOUSE_UP
Definition: tool_event.h:68
@ TA_KEYBOARD
Definition: tool_event.h:76
@ TA_VIEW_REFRESH
Definition: tool_event.h:79
@ TA_MOUSE_DRAG
Definition: tool_event.h:70
@ TA_CHANGE_LAYER
Definition: tool_event.h:85
@ TA_MOUSE_DOWN
Definition: tool_event.h:69
@ TA_UNDO_REDO_POST
Definition: tool_event.h:108
@ TA_KEY_PRESSED
Definition: tool_event.h:75
@ TA_MOUSE_DBLCLICK
Definition: tool_event.h:67
@ TA_MOUSE_WHEEL
Definition: tool_event.h:72
@ TA_ACTION
Definition: tool_event.h:111
@ TA_VIEW_PAN
Definition: tool_event.h:81
@ TA_CANCEL_TOOL
Definition: tool_event.h:89
@ TA_VIEW_DIRTY
Definition: tool_event.h:82
@ TA_VIEW_ZOOM
Definition: tool_event.h:80
@ TC_COMMAND
Definition: tool_event.h:56
@ TC_MOUSE
Definition: tool_event.h:54
@ TC_MESSAGE
Definition: tool_event.h:57
@ TC_KEYBOARD
Definition: tool_event.h:55
@ TC_VIEW
Definition: tool_event.h:58
@ MD_ALT
Definition: tool_event.h:144
@ MD_CTRL
Definition: tool_event.h:143
@ MD_SHIFT
Definition: tool_event.h:142
@ BUT_AUX1
Definition: tool_event.h:134
@ BUT_MIDDLE
Definition: tool_event.h:133
@ BUT_LEFT
Definition: tool_event.h:131
@ BUT_RIGHT
Definition: tool_event.h:132
@ BUT_AUX2
Definition: tool_event.h:135
@ BUT_NONE
Definition: tool_event.h:130
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587