KiCad PCB EDA Suite
Loading...
Searching...
No Matches
action_manager.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) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[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 <eda_draw_frame.h>
27#include <tool/action_manager.h>
28#include <tool/tool_action.h>
29#include <tool/tool_manager.h>
30#include <trace_helpers.h>
31#include <wx/log.h>
32
33#include <hotkeys_basic.h>
34#include <cctype>
35
37 m_toolMgr( aToolManager )
38{
39 // Register known actions
40 std::list<TOOL_ACTION*>& actionList = GetActionList();
41
42 for( TOOL_ACTION* action : actionList )
43 {
44 if( action->m_id == -1 )
45 action->m_id = MakeActionId( action->m_name );
46
47 int groupID = 0;
48 std::string groupName = "none";
49
50 std::optional<TOOL_ACTION_GROUP> group = action->GetActionGroup();
51
52 if( group.has_value() )
53 {
54 groupID = group.value().GetGroupID();
55 groupName = group.value().GetName();
56 }
57
58 wxLogTrace( kicadTraceToolStack,
59 "ACTION_MANAGER::ACTION_MANAGER: Registering action %s with ID %d, UI ID %d, and group %s(%d)",
60 action->m_name, action->m_id, action->GetUIId(), groupName, groupID );
61
62 RegisterAction( action );
63 }
64}
65
66
68{
69}
70
71
73{
74 // TOOL_ACTIONs are supposed to be named [appName.]toolName.actionName (with dots between)
75 // action name without specifying at least toolName is not valid
76 wxASSERT( aAction->GetName().find( '.', 0 ) != std::string::npos );
77
78 // TOOL_ACTIONs must have unique names & ids
79 wxASSERT( m_actionNameIndex.find( aAction->m_name ) == m_actionNameIndex.end() );
80
81 m_actionNameIndex[aAction->m_name] = aAction;
82
83 if( aAction->HasCustomUIId() )
84 m_customUIIdIndex[aAction->GetUIId()] = aAction;
85}
86
87
89 const ACTION_CONDITIONS& aConditions )
90{
91 // Remove any existing handlers with the old conditions to ensure the UI layer doesn't have
92 // stale data.
93 if( m_toolMgr )
95
96 m_uiConditions[aAction.GetId()] = aConditions;
97
98 wxLogTrace( kicadTraceToolStack,
99 wxS( "ACTION_MANAGER::SetConditions: Registering conditions for ID %d - %s" ),
100 aAction.GetId(), aAction.GetName() );
101
102 // Register a new handler with the new conditions
103 if( m_toolMgr )
104 m_toolMgr->GetToolHolder()->RegisterUIUpdateHandler( aAction, aConditions );
105}
106
107
109{
110 const auto it = m_uiConditions.find( aAction.GetId() );
111
112 // If the action doesn't have something registered, then return null
113 if( it == m_uiConditions.end() )
114 return nullptr;
115 else
116 return &it->second;
117}
118
119
120int ACTION_MANAGER::MakeActionId( const std::string& aActionName )
121{
122 static int currentActionId = 1;
123
124 return currentActionId++;
125}
126
127
128TOOL_ACTION* ACTION_MANAGER::FindAction( const std::string& aActionName ) const
129{
130 std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
131
132 if( it != m_actionNameIndex.end() )
133 return it->second;
134
135 return nullptr;
136}
137
138
139bool ACTION_MANAGER::RunHotKey( int aHotKey ) const
140{
141 int key = aHotKey & ~MD_MODIFIER_MASK;
142 int mod = aHotKey & MD_MODIFIER_MASK;
143
144 if( key >= 'a' && key <= 'z' )
145 key = std::toupper( key );
146
147 wxLogTrace( kicadTraceToolStack, wxS( "ACTION_MANAGER::RunHotKey Key: %s" ),
148 KeyNameFromKeyCode( aHotKey ) );
149
150 HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
151
152 // If no luck, try without Shift, to handle keys that require it
153 // e.g. to get ? you need to press Shift+/ without US keyboard layout
154 // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
155 // different combination.
156 // This doesn't apply for letters, as we already handled case normalisation.
157 if( it == m_actionHotKeys.end() && !std::isalpha( key ) )
158 {
159 wxLogTrace( kicadTraceToolStack,
160 wxS( "ACTION_MANAGER::RunHotKey No actions found, searching with key: %s" ),
161 KeyNameFromKeyCode( key | ( mod & ~MD_SHIFT ) ) );
162
163 it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
164 }
165
166 // Still no luck, we're done without a match
167 if( it == m_actionHotKeys.end() )
168 return false; // no appropriate action found for the hotkey
169
170 const std::list<TOOL_ACTION*>& actions = it->second;
171
172 // Choose the action that has the highest priority on the active tools stack
173 // If there is none, run the global action associated with the hot key
174 int highestPriority = -1, priority = -1;
175 const TOOL_ACTION* context = nullptr; // pointer to context action of the highest priority tool
176 std::vector<const TOOL_ACTION*> global; // pointers to global actions
177 // if there is no context action
178
179 for( const TOOL_ACTION* action : actions )
180 {
181 if( action->GetScope() == AS_GLOBAL )
182 {
183 // Store the global action in case there are no context actions to run
184 global.emplace_back( action );
185 continue;
186 }
187
188 TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
189
190 if( tool )
191 {
192 // Choose the action that goes to the tool with highest priority
193 // (i.e. is on the top of active tools stack)
194 priority = m_toolMgr->GetPriority( tool->GetId() );
195
196 if( priority >= 0 && priority > highestPriority )
197 {
198 highestPriority = priority;
199 context = action;
200 }
201 }
202 }
203
204 // Get the selection to use to test if the action is enabled
206
207 if( context )
208 {
209 bool runAction = true;
210
211 if( const ACTION_CONDITIONS* aCond = GetCondition( *context ) )
212 runAction = aCond->enableCondition( sel );
213
214 wxLogTrace( kicadTraceToolStack,
215 wxS( "ACTION_MANAGER::RunHotKey %s context action: %s for hotkey %s" ),
216 runAction ? wxS( "Running" ) : wxS( "Not running" ),
217 context->GetName(),
218 KeyNameFromKeyCode( aHotKey ) );
219
220 if( runAction )
221 return m_toolMgr->RunAction( *context );
222 }
223 else if( !global.empty() )
224 {
225 for( const TOOL_ACTION* act : global )
226 {
227 bool runAction = true;
228
229 if( const ACTION_CONDITIONS* aCond = GetCondition( *act ) )
230 runAction = aCond->enableCondition( sel );
231
232 wxLogTrace( kicadTraceToolStack,
233 wxS( "ACTION_MANAGER::RunHotKey %s global action: %s for hotkey %s" ),
234 runAction ? wxS( "Running" ) : wxS( "Not running" ),
235 act->GetName(),
236 KeyNameFromKeyCode( aHotKey ) );
237
238 if( runAction && m_toolMgr->RunAction( *act ) )
239 return true;
240 }
241 }
242
243 wxLogTrace( kicadTraceToolStack,
244 wxS( "ACTION_MANAGER::RunHotKey No action found for key %s" ),
245 KeyNameFromKeyCode( aHotKey ) );
246
247 return false;
248}
249
250
251bool ACTION_MANAGER::IsActionUIId( int aId ) const
252{
253 // Automatically assigned IDs are always in this range
254 if( aId >= TOOL_ACTION::GetBaseUIId() )
255 return true;
256
257 // Search the custom assigned UI IDs
258 auto it = m_customUIIdIndex.find( aId );
259
260 return ( it != m_customUIIdIndex.end() );
261}
262
263
264const std::map<std::string, TOOL_ACTION*>& ACTION_MANAGER::GetActions() const
265{
266 return m_actionNameIndex;
267}
268
269
270int ACTION_MANAGER::GetHotKey( const TOOL_ACTION& aAction ) const
271{
272 std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );
273
274 if( it == m_hotkeys.end() )
275 return 0;
276
277 return it->second;
278}
279
280
281void ACTION_MANAGER::UpdateHotKeys( bool aFullUpdate )
282{
283 static std::map<std::string, int> legacyHotKeyMap;
284 static std::map<std::string, std::pair<int, int>> userHotKeyMap;
285 static bool mapsInitialized = false;
286
287 m_actionHotKeys.clear();
288 m_hotkeys.clear();
289
290 if( aFullUpdate && !mapsInitialized && m_toolMgr->GetToolHolder() )
291 {
293 ReadHotKeyConfig( wxEmptyString, userHotKeyMap );
294 mapsInitialized = true;
295 }
296
297 for( const auto& ii : m_actionNameIndex )
298 {
299 TOOL_ACTION* action = ii.second;
300 int hotkey = 0;
301 int alt = 0;
302
303 if( aFullUpdate )
304 processHotKey( action, legacyHotKeyMap, userHotKeyMap );
305
306 hotkey = action->GetHotKey();
307 alt = action->GetHotKeyAlt();
308
309 if( hotkey > 0 )
310 m_actionHotKeys[hotkey].push_back( action );
311
312 if( alt > 0 )
313 m_actionHotKeys[alt].push_back( action );
314
315 m_hotkeys[action->GetId()] = hotkey;
316 }
317}
318
319
321 const std::map<std::string, int>& aLegacyMap,
322 const std::map<std::string, std::pair<int, int>>& aHotKeyMap )
323{
324 aAction->m_hotKey = aAction->m_defaultHotKey;
325
326 if( !aAction->m_legacyName.empty() && aLegacyMap.count( aAction->m_legacyName ) )
327 aAction->SetHotKey( aLegacyMap.at( aAction->m_legacyName ) );
328
329 if( aHotKeyMap.count( aAction->m_name ) )
330 {
331 std::pair<int, int> keys = aHotKeyMap.at( aAction->m_name );
332 aAction->SetHotKey( keys.first, keys.second );
333 }
334}
~ACTION_MANAGER()
Unregister every registered action.
HOTKEY_LIST m_actionHotKeys
Quick action<->hot key lookup.
const ACTION_CONDITIONS * GetCondition(const TOOL_ACTION &aAction) const
Get the conditions to use for a specific tool action.
std::map< int, TOOL_ACTION * > m_customUIIdIndex
Map for indexing actions by their hotkeys.
TOOL_MANAGER * m_toolMgr
Map for indexing actions by their names.
void processHotKey(TOOL_ACTION *aAction, const std::map< std::string, int > &aLegacyMap, const std::map< std::string, std::pair< int, int > > &aHotKeyMap)
Tool manager needed to run actions.
const std::map< std::string, TOOL_ACTION * > & GetActions() const
Get a list of currently-registered actions mapped by their name.
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for recording actions that have custom UI IDs.
std::map< int, int > m_hotkeys
bool RunHotKey(int aHotKey) const
Run an action associated with a hotkey (if there is one available).
void SetConditions(const TOOL_ACTION &aAction, const ACTION_CONDITIONS &aConditions)
Set the conditions the UI elements for activating a specific tool action should use for determining t...
void RegisterAction(TOOL_ACTION *aAction)
Add a tool action to the manager and sets it up.
TOOL_ACTION * FindAction(const std::string &aActionName) const
Find an action with a given name (if there is one available).
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
bool IsActionUIId(int aId) const
Test if a UI ID corresponds to an action ID in our system.
int GetHotKey(const TOOL_ACTION &aAction) const
Return the hot key associated with a given action or 0 if there is none.
static int MakeActionId(const std::string &aActionName)
Generate an unique ID from for an action with given name.
std::map< int, ACTION_CONDITIONS > m_uiConditions
Map the command ID that wx uses for the action to the UI conditions for the menu/toolbar items.
void UpdateHotKeys(bool aFullUpdate)
Optionally read the hotkey config files and then rebuilds the internal hotkey maps.
ACTION_MANAGER(TOOL_MANAGER *aToolManager)
virtual wxString ConfigBaseName()
Definition: tools_holder.h:164
virtual void RegisterUIUpdateHandler(const TOOL_ACTION &aAction, const ACTION_CONDITIONS &aConditions)
Register an action's update conditions with the UI layer to allow the UI to appropriately display the...
virtual SELECTION & GetCurrentSelection()
Get the current selection from the canvas area.
Definition: tools_holder.h:98
virtual void UnregisterUIUpdateHandler(const TOOL_ACTION &aAction)
Unregister a UI handler for an action that was registered using RegisterUIUpdateHandler.
Represent a single user action.
Definition: tool_action.h:269
bool HasCustomUIId() const
Return true if this action has a custom UI ID set.
Definition: tool_action.h:329
static int GetBaseUIId()
Definition: tool_action.h:344
void SetHotKey(int aKeycode, int aKeycodeAlt=0)
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:324
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:302
int GetHotKeyAlt() const
Definition: tool_action.h:314
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:313
const int m_defaultHotKey
Definition: tool_action.h:441
const std::string m_legacyName
Definition: tool_action.h:445
std::string m_name
Definition: tool_action.h:436
int GetUIId() const
Definition: tool_action.h:339
Base abstract interface for all kinds of tools.
Definition: tool_base.h:66
TOOL_ID GetId() const
Return the unique identifier of the tool.
Definition: tool_base.h:123
Master controller class:
Definition: tool_manager.h:62
int GetPriority(int aToolId) const
Return priority of a given tool.
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:402
TOOL_BASE * FindTool(int aId) const
Search for a tool with given ID.
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
void ReadHotKeyConfig(const wxString &aFileName, std::map< std::string, std::pair< int, int > > &aHotKeys)
Reads a hotkey config file into a map.
int ReadLegacyHotkeyConfig(const wxString &aAppname, std::map< std::string, int > &aMap)
Read configuration data and fill the current hotkey list with hotkeys.
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
@ AS_GLOBAL
Global action (toolbar/main menu event, global shortcut)
Definition: tool_action.h:48
@ MD_MODIFIER_MASK
Definition: tool_event.h:145
@ MD_SHIFT
Definition: tool_event.h:142
wxLogTrace helper definitions.