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#ifdef WX_COMPATIBILITY
34#include <wx/debug.h>
35#else
36#include <cassert>
37#endif
38
40{
41 int flag;
42 std::string str;
43};
44
45
46static 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
88bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
89{
90 return Matches( aAction->MakeEvent() );
91}
92
93
94const 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 { BUT_AUX1, "aux1" },
140 { BUT_AUX2, "aux2" },
141 { 0, "" }
142 };
143
144 const FlagString modifiers[] =
145 {
146 { MD_SHIFT, "shift" },
147 { MD_CTRL, "ctrl" },
148 { MD_ALT, "alt" },
149 { 0, "" }
150 };
151
152 ev = "category: ";
153 ev += flag2string( m_category, categories );
154 ev += " action: ";
155 ev += flag2string( m_actions, actions );
156
157 if( m_actions & TA_MOUSE )
158 {
159 ev += " btns: ";
160 ev += flag2string( m_mouseButtons, buttons );
161 }
162
163 if( m_actions & TA_KEYBOARD )
164 {
165 char tmp[128];
166 sprintf( tmp, "key: %d", m_keyCode );
167 ev += tmp;
168 }
169
170 if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
171 {
172 ev += " mods: ";
173 ev += flag2string( m_modifiers, modifiers );
174 }
175
176 if( m_commandId )
177 {
178 char tmp[128];
179 sprintf( tmp, "cmd-id: %d", *m_commandId );
180 ev += tmp;
181 }
182
183 ev += "cmd-str: " + m_commandStr;
184
185 return ev;
186}
187
188
189const std::string TOOL_EVENT_LIST::Format() const
190{
191 std::string s;
192
193 for( const TOOL_EVENT& e : m_events )
194 s += e.Format() + " ";
195
196 return s;
197}
198
199
200bool TOOL_EVENT::IsClick( int aButtonMask ) const
201{
202 return ( m_actions & TA_MOUSE_CLICK ) && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
203}
204
205
206bool TOOL_EVENT::IsDblClick( int aButtonMask ) const
207{
208 return m_actions == TA_MOUSE_DBLCLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
209}
210
211
213{
214 return ( ( m_commandStr == ACTIONS::cancelInteractive.GetName() )
216 || ( m_actions == TA_CANCEL_TOOL ) );
217}
218
219
221{
226}
227
228
230{
231 return ( ( m_commandStr.find( "PointEditor" ) != getCommandStr().npos )
233}
234
235
237{
238 return ( m_commandStr.find( "InteractiveMove" ) != getCommandStr().npos );
239}
240
241
243{
244 return ( m_commandStr.find( "InteractiveEdit" ) != getCommandStr().npos );
245}
246
247
249{
250 return ( m_commandStr.find( "Simulation" ) != getCommandStr().npos );
251}
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION activatePointEditor
Definition: actions.h:172
static const TOOL_EVENT ClearedEvent
Definition: actions.h:208
static const TOOL_EVENT SelectedEvent
Definition: actions.h:206
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:205
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:207
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:189
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:212
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:248
bool IsSelectionEvent() const
Indicate an selection-changed notification event.
Definition: tool_event.cpp:220
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:200
bool m_passEvent
Definition: tool_event.h:521
void init()
Definition: tool_event.cpp:60
bool IsMoveTool() const
Indicate if the event is from one of the move tools.
Definition: tool_event.cpp:236
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: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:76
bool IsEditorTool() const
Indicate if the event is asking for an editor tool.
Definition: tool_event.cpp:242
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:206
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:229
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:94
std::string str
Definition: tool_event.cpp:42
static const std::string flag2string(int aFlag, const FlagString *aExps)
Definition: tool_event.cpp:46
@ 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:617