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 The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <cstring>
23#include <string>
24
25#include <tool/tool_event.h>
26#include <tool/tool_action.h>
27#include <tool/tool_manager.h>
28#include <tool/actions.h>
29
30
31#include <wx/debug.h>
32
34{
35 int flag;
36 std::string str;
37};
38
39
40static const std::string flag2string( int aFlag, const FlagString* aExps )
41{
42 std::string rv;
43
44 for( int i = 0; aExps[i].str.length(); i++ )
45 {
46 if( aExps[i].flag & aFlag )
47 rv += aExps[i].str + " ";
48 }
49
50 return rv;
51}
52
53
55{
56 // By default only MESSAGEs and Cancels are passed to multiple recipients
58
60
61 // Cancel tool doesn't contain a position
62 if( IsCancel() )
63 m_hasPosition = false;
64
65 m_forceImmediate = false;
66 m_reactivate = false;
67}
68
69
71{
72 wxCHECK_MSG( HasPosition(), VECTOR2D(), "Attempted to get position from non-position event" );
73
74 return aPos;
75}
76
77
78bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
79{
80 return Matches( aAction->MakeEvent() );
81}
82
83
85{
86 if( m_actionGroup.has_value() )
87 return m_actionGroup.value() == aGroup;
88
89 return false;
90}
91
92
93const std::string TOOL_EVENT::Format() const
94{
95 std::string ev;
96
97 const FlagString categories[] =
98 {
99 { TC_MOUSE, "mouse" },
100 { TC_KEYBOARD, "keyboard" },
101 { TC_COMMAND, "command" },
102 { TC_MESSAGE, "message" },
103 { TC_VIEW, "view" },
104 { 0, "" }
105 };
106
107 const FlagString actions[] =
108 {
109 { TA_MOUSE_CLICK, "click" },
110 { TA_MOUSE_DBLCLICK, "double click" },
111 { TA_MOUSE_UP, "button-up" },
112 { TA_MOUSE_DOWN, "button-down" },
113 { TA_MOUSE_DRAG, "drag" },
114 { TA_MOUSE_MOTION, "motion" },
115 { TA_MOUSE_WHEEL, "wheel" },
116 { TA_KEY_PRESSED, "key-pressed" },
117 { TA_VIEW_REFRESH, "view-refresh" },
118 { TA_VIEW_ZOOM, "view-zoom" },
119 { TA_VIEW_PAN, "view-pan" },
120 { TA_VIEW_DIRTY, "view-dirty" },
121 { TA_CHANGE_LAYER, "change-layer" },
122 { TA_CANCEL_TOOL, "cancel-tool" },
123 { TA_CHOICE_MENU_UPDATE, "choice-menu-update" },
124 { TA_CHOICE_MENU_CHOICE, "choice-menu-choice" },
125 { TA_UNDO_REDO_PRE, "undo-redo-pre" },
126 { TA_UNDO_REDO_POST, "undo-redo-post" },
127 { TA_ACTION, "action" },
128 { TA_ACTIVATE, "activate" },
129 { 0, "" }
130 };
131
132 const FlagString buttons[] =
133 {
134 { BUT_NONE, "none" },
135 { BUT_LEFT, "left" },
136 { BUT_RIGHT, "right" },
137 { BUT_MIDDLE, "middle" },
138 { BUT_AUX1, "aux1" },
139 { BUT_AUX2, "aux2" },
140 { 0, "" }
141 };
142
143 const FlagString modifiers[] =
144 {
145 { MD_SHIFT, "shift" },
146 { MD_CTRL, "ctrl" },
147 { MD_ALT, "alt" },
148 { MD_SUPER, "super" },
149 { MD_META, "meta" },
150 { MD_ALTGR, "altgr" },
151 { 0, "" }
152 };
153
154 ev = "category: " + flag2string( m_category, categories ) + " ";
155 ev += "action: " + flag2string( m_actions, actions ) + " ";
156 ev += "action-group: ";
157
158 if( m_actionGroup.has_value() )
159 {
160 ev += m_actionGroup.value().GetName() +
161 "(" + std::to_string( m_actionGroup.value().GetGroupID() ) + ")" + " ";
162 }
163 else
164 {
165 ev += "none ";
166 }
167
168 if( m_actions & TA_MOUSE )
169 ev += "btns: " + flag2string( m_mouseButtons, buttons ) + " ";
170
171 if( m_actions & TA_KEYBOARD )
172 ev += "key: " + std::to_string( m_keyCode ) + " ";
173
174 if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
175 ev += "mods: " + flag2string( m_modifiers, modifiers ) + " ";
176
177 if( m_commandId )
178 ev += "cmd-id: " + std::to_string( *m_commandId ) + " ";
179
180 ev += "cmd-str: " + m_commandStr;
181
182 return ev;
183}
184
185
186const std::string TOOL_EVENT_LIST::Format() const
187{
188 std::string s;
189
190 for( const TOOL_EVENT& e : m_events )
191 s += e.Format() + " ";
192
193 return s;
194}
195
196
197const std::string TOOL_EVENT_LIST::Names() const
198{
199 std::string s;
200
201 for( const TOOL_EVENT& e : m_events )
202 s += e.m_commandStr + " ";
203
204 return s;
205}
206
207
208bool TOOL_EVENT::IsClick( int aButtonMask ) const
209{
210 return ( m_actions & TA_MOUSE_CLICK ) && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
211}
212
213
214bool TOOL_EVENT::IsDblClick( int aButtonMask ) const
215{
216 return m_actions == TA_MOUSE_DBLCLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
217}
218
219
221{
222 return ( ( m_commandStr == ACTIONS::cancelInteractive.GetName() )
224 || ( m_actions == TA_CANCEL_TOOL ) );
225}
226
227
235
236
238{
239 return ( ( m_commandStr.find( "PointEditor" ) != getCommandStr().npos )
241}
242
243
245{
246 return ( m_commandStr.find( "InteractiveMove" ) != getCommandStr().npos );
247}
248
249
251{
252 return ( m_commandStr.find( "InteractiveEdit" ) != getCommandStr().npos );
253}
254
255
257{
258 return ( m_commandStr.find( "Simulation" ) != getCommandStr().npos );
259}
static TOOL_ACTION cancelInteractive
Definition actions.h:68
static TOOL_ACTION activatePointEditor
Definition actions.h:267
static const TOOL_EVENT ClearedEvent
Definition actions.h:343
static const TOOL_EVENT SelectedEvent
Definition actions.h:341
static const TOOL_EVENT PointSelectedEvent
Definition actions.h:340
static const TOOL_EVENT UnselectedEvent
Definition actions.h:342
Define a group that can be used to group actions (and their events) of similar operations.
Definition tool_action.h:75
Represent a single user action.
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
const std::string Format() const
Return information about event in form of a human-readable string.
std::deque< TOOL_EVENT > m_events
Definition tool_event.h:764
const std::string Names() const
Return a string containing the names of all the events in this list.
Generic, UI-independent tool event.
Definition tool_event.h:167
bool HasPosition() const
Returns if it this event has a valid position (true for mouse events and context-menu or hotkey-based...
Definition tool_event.h:256
bool IsCancelInteractive() const
Indicate the event should restart/end an ongoing interactive tool's event loop (eg esc key,...
bool m_hasPosition
Definition tool_event.h:594
std::optional< int > m_commandId
Definition tool_event.h:633
int m_keyCode
Stores code of pressed/released key.
Definition tool_event.h:617
int m_modifiers
State of key modifiers (Ctrl/Alt/etc.).
Definition tool_event.h:620
bool Matches(const TOOL_EVENT &aEvent) const
Test whether two events match in terms of category & action or command.
Definition tool_event.h:388
bool IsActivate() const
Definition tool_event.h:341
std::string m_commandStr
Definition tool_event.h:634
bool IsSimulator() const
Indicate if the event is from the simulator.
bool IsSelectionEvent() const
Indicate an selection-changed notification event.
bool IsClick(int aButtonMask=BUT_ANY) const
bool m_passEvent
Definition tool_event.h:593
void init()
bool IsMoveTool() const
Indicate if the event is from one of the move tools.
TOOL_ACTIONS m_actions
Definition tool_event.h:591
std::optional< TOOL_ACTION_GROUP > m_actionGroup
Optional group that the parent action for the event belongs to.
Definition tool_event.h:599
bool m_forceImmediate
Definition tool_event.h:595
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
bool IsActionInGroup(const TOOL_ACTION_GROUP &aGroup) const
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...
bool IsEditorTool() const
Indicate if the event is asking for an editor tool.
bool m_reactivate
True when the tool is being re-activated from the stack.
Definition tool_event.h:602
bool IsCancel() const
Definition tool_event.h:336
bool IsDblClick(int aButtonMask=BUT_ANY) const
const std::string & getCommandStr() const
Definition tool_event.h:554
TOOL_EVENT_CATEGORY m_category
Definition tool_event.h:590
bool IsPointEditor() const
Indicate if the event is from one of the point editors.
int m_mouseButtons
State of mouse buttons.
Definition tool_event.h:614
const std::string Format() const
Return information about event in form of a human-readable string.
std::string str
static const std::string flag2string(int aFlag, const FlagString *aExps)
@ TA_CHOICE_MENU_CHOICE
Context menu choice.
Definition tool_event.h:94
@ TA_UNDO_REDO_PRE
This event is sent before undo/redo command is performed.
Definition tool_event.h:102
@ TA_MOUSE_CLICK
Definition tool_event.h:63
@ TA_CHOICE_MENU_UPDATE
Context menu update.
Definition tool_event.h:90
@ TA_MOUSE
Definition tool_event.h:70
@ TA_ACTIVATE
Tool activation event.
Definition tool_event.h:111
@ TA_MOUSE_MOTION
Definition tool_event.h:68
@ TA_MOUSE_UP
Definition tool_event.h:65
@ TA_KEYBOARD
Definition tool_event.h:73
@ TA_VIEW_REFRESH
Definition tool_event.h:76
@ TA_MOUSE_DRAG
Definition tool_event.h:67
@ TA_CHANGE_LAYER
Definition tool_event.h:82
@ TA_MOUSE_DOWN
Definition tool_event.h:66
@ TA_UNDO_REDO_POST
This event is sent after undo/redo command is performed.
Definition tool_event.h:105
@ TA_KEY_PRESSED
Definition tool_event.h:72
@ TA_MOUSE_DBLCLICK
Definition tool_event.h:64
@ TA_MOUSE_WHEEL
Definition tool_event.h:69
@ TA_ACTION
Tool action (allows one to control tools).
Definition tool_event.h:108
@ TA_VIEW_PAN
Definition tool_event.h:78
@ TA_CANCEL_TOOL
Tool cancel event.
Definition tool_event.h:86
@ TA_VIEW_DIRTY
Definition tool_event.h:79
@ TA_VIEW_ZOOM
Definition tool_event.h:77
@ MD_META
Definition tool_event.h:143
@ MD_ALT
Definition tool_event.h:141
@ MD_CTRL
Definition tool_event.h:140
@ MD_SUPER
Definition tool_event.h:142
@ MD_ALTGR
Definition tool_event.h:144
@ MD_SHIFT
Definition tool_event.h:139
@ TC_COMMAND
Definition tool_event.h:53
@ TC_MOUSE
Definition tool_event.h:51
@ TC_MESSAGE
Definition tool_event.h:54
@ TC_KEYBOARD
Definition tool_event.h:52
@ TC_VIEW
Definition tool_event.h:55
@ BUT_AUX1
Definition tool_event.h:131
@ BUT_MIDDLE
Definition tool_event.h:130
@ BUT_LEFT
Definition tool_event.h:128
@ BUT_RIGHT
Definition tool_event.h:129
@ BUT_AUX2
Definition tool_event.h:132
@ BUT_NONE
Definition tool_event.h:127
VECTOR2< double > VECTOR2D
Definition vector2d.h:682