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 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 ev += "key: " + std::to_string( m_keyCode );
159 }
160
161 if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
162 {
163 ev += " mods: ";
164 ev += flag2string( m_modifiers, modifiers );
165 }
166
167 if( m_commandId )
168 {
169 ev += "cmd-id: " + std::to_string( *m_commandId );
170 }
171
172 ev += "cmd-str: " + m_commandStr;
173
174 return ev;
175}
176
177
178const std::string TOOL_EVENT_LIST::Format() const
179{
180 std::string s;
181
182 for( const TOOL_EVENT& e : m_events )
183 s += e.Format() + " ";
184
185 return s;
186}
187
188
189bool TOOL_EVENT::IsClick( int aButtonMask ) const
190{
191 return ( m_actions & TA_MOUSE_CLICK ) && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
192}
193
194
195bool TOOL_EVENT::IsDblClick( int aButtonMask ) const
196{
197 return m_actions == TA_MOUSE_DBLCLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
198}
199
200
202{
203 return ( ( m_commandStr == ACTIONS::cancelInteractive.GetName() )
205 || ( m_actions == TA_CANCEL_TOOL ) );
206}
207
208
210{
215}
216
217
219{
220 return ( ( m_commandStr.find( "PointEditor" ) != getCommandStr().npos )
222}
223
224
226{
227 return ( m_commandStr.find( "InteractiveMove" ) != getCommandStr().npos );
228}
229
230
232{
233 return ( m_commandStr.find( "InteractiveEdit" ) != getCommandStr().npos );
234}
235
236
238{
239 return ( m_commandStr.find( "Simulation" ) != getCommandStr().npos );
240}
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION activatePointEditor
Definition: actions.h:181
static const TOOL_EVENT ClearedEvent
Definition: actions.h:234
static const TOOL_EVENT SelectedEvent
Definition: actions.h:232
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:231
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:233
Represent a single user action.
Definition: tool_action.h:219
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:178
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:736
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:201
bool m_hasPosition
Definition: tool_event.h:575
std::optional< int > m_commandId
Definition: tool_event.h:611
int m_keyCode
State of key modifiers (Ctrl/Alt/etc.)
Definition: tool_event.h:595
int m_modifiers
Definition: tool_event.h:598
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:612
bool IsSimulator() const
Indicate if the event is from the simulator.
Definition: tool_event.cpp:237
bool IsSelectionEvent() const
Indicate an selection-changed notification event.
Definition: tool_event.cpp:209
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:189
bool m_passEvent
Definition: tool_event.h:574
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:225
TOOL_ACTIONS m_actions
Definition: tool_event.h:572
bool m_forceImmediate
True when the tool is being re-activated from the stack.
Definition: tool_event.h:576
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:231
bool m_reactivate
Difference between mouse cursor position and the point where dragging event has started.
Definition: tool_event.h:579
bool IsCancel() const
Definition: tool_event.h:332
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:195
const std::string & getCommandStr() const
Definition: tool_event.h:535
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:571
bool IsPointEditor() const
Indicate if the event is from one of the point editors.
Definition: tool_event.cpp:218
int m_mouseButtons
Stores code of pressed/released key.
Definition: tool_event.h:592
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: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