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 if( it == m_actionHotKeys.end() )
157 {
158 wxLogTrace( kicadTraceToolStack,
159 wxS( "ACTION_MANAGER::RunHotKey No actions found, searching with key: %s" ),
160 KeyNameFromKeyCode( key | ( mod & ~MD_SHIFT ) ) );
161
162 it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
163
164 if( it == m_actionHotKeys.end() )
165 return false; // no appropriate action found for the hotkey
166 }
167
168 const std::list<TOOL_ACTION*>& actions = it->second;
169
170 // Choose the action that has the highest priority on the active tools stack
171 // If there is none, run the global action associated with the hot key
172 int highestPriority = -1, priority = -1;
173 const TOOL_ACTION* context = nullptr; // pointer to context action of the highest priority tool
174 std::vector<const TOOL_ACTION*> global; // pointers to global actions
175 // if there is no context action
176
177 for( const TOOL_ACTION* action : actions )
178 {
179 if( action->GetScope() == AS_GLOBAL )
180 {
181 // Store the global action in case there are no context actions to run
182 global.emplace_back( action );
183 continue;
184 }
185
186 TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
187
188 if( tool )
189 {
190 // Choose the action that goes to the tool with highest priority
191 // (i.e. is on the top of active tools stack)
192 priority = m_toolMgr->GetPriority( tool->GetId() );
193
194 if( priority >= 0 && priority > highestPriority )
195 {
196 highestPriority = priority;
197 context = action;
198 }
199 }
200 }
201
202 // Get the selection to use to test if the action is enabled
204
205 if( context )
206 {
207 bool runAction = true;
208
209 if( const ACTION_CONDITIONS* aCond = GetCondition( *context ) )
210 runAction = aCond->enableCondition( sel );
211
212 wxLogTrace( kicadTraceToolStack,
213 wxS( "ACTION_MANAGER::RunHotKey %s context action: %s for hotkey %s" ),
214 runAction ? wxS( "Running" ) : wxS( "Not running" ),
215 context->GetName(),
216 KeyNameFromKeyCode( aHotKey ) );
217
218 if( runAction )
219 return m_toolMgr->RunAction( *context );
220 }
221 else if( !global.empty() )
222 {
223 for( const TOOL_ACTION* act : global )
224 {
225 bool runAction = true;
226
227 if( const ACTION_CONDITIONS* aCond = GetCondition( *act ) )
228 runAction = aCond->enableCondition( sel );
229
230 wxLogTrace( kicadTraceToolStack,
231 wxS( "ACTION_MANAGER::RunHotKey %s global action: %s for hotkey %s" ),
232 runAction ? wxS( "Running" ) : wxS( "Not running" ),
233 act->GetName(),
234 KeyNameFromKeyCode( aHotKey ) );
235
236 if( runAction && m_toolMgr->RunAction( *act ) )
237 return true;
238 }
239 }
240
241 wxLogTrace( kicadTraceToolStack,
242 wxS( "ACTION_MANAGER::RunHotKey No action found for key %s" ),
243 KeyNameFromKeyCode( aHotKey ) );
244
245 return false;
246}
247
248
249bool ACTION_MANAGER::IsActionUIId( int aId ) const
250{
251 // Automatically assigned IDs are always in this range
252 if( aId >= TOOL_ACTION::GetBaseUIId() )
253 return true;
254
255 // Search the custom assigned UI IDs
256 auto it = m_customUIIdIndex.find( aId );
257
258 return ( it != m_customUIIdIndex.end() );
259}
260
261
262const std::map<std::string, TOOL_ACTION*>& ACTION_MANAGER::GetActions() const
263{
264 return m_actionNameIndex;
265}
266
267
268int ACTION_MANAGER::GetHotKey( const TOOL_ACTION& aAction ) const
269{
270 std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );
271
272 if( it == m_hotkeys.end() )
273 return 0;
274
275 return it->second;
276}
277
278
279void ACTION_MANAGER::UpdateHotKeys( bool aFullUpdate )
280{
281 static std::map<std::string, int> legacyHotKeyMap;
282 static std::map<std::string, std::pair<int, int>> userHotKeyMap;
283 static bool mapsInitialized = false;
284
285 m_actionHotKeys.clear();
286 m_hotkeys.clear();
287
288 if( aFullUpdate && !mapsInitialized && m_toolMgr->GetToolHolder() )
289 {
291 ReadHotKeyConfig( wxEmptyString, userHotKeyMap );
292 mapsInitialized = true;
293 }
294
295 for( const auto& ii : m_actionNameIndex )
296 {
297 TOOL_ACTION* action = ii.second;
298 int hotkey = 0;
299 int alt = 0;
300
301 if( aFullUpdate )
302 processHotKey( action, legacyHotKeyMap, userHotKeyMap );
303
304 hotkey = action->GetHotKey();
305 alt = action->GetHotKeyAlt();
306
307 if( hotkey > 0 )
308 m_actionHotKeys[hotkey].push_back( action );
309
310 if( alt > 0 )
311 m_actionHotKeys[alt].push_back( action );
312
313 m_hotkeys[action->GetId()] = hotkey;
314 }
315}
316
317
319 const std::map<std::string, int>& aLegacyMap,
320 const std::map<std::string, std::pair<int, int>>& aHotKeyMap )
321{
322 aAction->m_hotKey = aAction->m_defaultHotKey;
323
324 if( !aAction->m_legacyName.empty() && aLegacyMap.count( aAction->m_legacyName ) )
325 aAction->SetHotKey( aLegacyMap.at( aAction->m_legacyName ) );
326
327 if( aHotKeyMap.count( aAction->m_name ) )
328 {
329 std::pair<int, int> keys = aHotKeyMap.at( aAction->m_name );
330 aAction->SetHotKey( keys.first, keys.second );
331 }
332}
~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:122
Master controller class:
Definition: tool_manager.h:57
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:145
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:389
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.