KiCad PCB EDA Suite
ACTION_MANAGER Class Reference

Manage TOOL_ACTION objects. More...

#include <action_manager.h>

Public Member Functions

 ACTION_MANAGER (TOOL_MANAGER *aToolManager)
 
 ~ACTION_MANAGER ()
 Unregister every registered action. More...
 
void RegisterAction (TOOL_ACTION *aAction)
 Add a tool action to the manager and sets it up. More...
 
const std::map< std::string, TOOL_ACTION * > & GetActions () const
 Get a list of currently-registered actions mapped by their name. More...
 
TOOL_ACTIONFindAction (const std::string &aActionName) const
 Find an action with a given name (if there is one available). More...
 
bool RunHotKey (int aHotKey) const
 Run an action associated with a hotkey (if there is one available). More...
 
int GetHotKey (const TOOL_ACTION &aAction) const
 Return the hot key associated with a given action or 0 if there is none. More...
 
void UpdateHotKeys (bool aFullUpdate)
 Optionally read the hotkey config files and then rebuilds the internal hotkey maps. More...
 
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 the current UI state (e.g. More...
 
const ACTION_CONDITIONSGetCondition (const TOOL_ACTION &aAction) const
 Get the conditions to use for a specific tool action. More...
 

Static Public Member Functions

static int MakeActionId (const std::string &aActionName)
 Generate an unique ID from for an action with given name. More...
 
static std::list< TOOL_ACTION * > & GetActionList ()
 Return list of TOOL_ACTIONs. More...
 

Private Types

typedef std::map< int, std::list< TOOL_ACTION * > > HOTKEY_LIST
 

Private Member Functions

int processHotKey (TOOL_ACTION *aAction, std::map< std::string, int > aLegacyMap, std::map< std::string, int > aHotKeyMap)
 Tool manager needed to run actions. More...
 

Private Attributes

TOOL_MANAGERm_toolMgr
 Map for indexing actions by their names. More...
 
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
 Map for indexing actions by their hotkeys. More...
 
HOTKEY_LIST m_actionHotKeys
 Quick action<->hot key lookup. More...
 
std::map< int, int > m_hotkeys
 
std::map< int, ACTION_CONDITIONSm_uiConditions
 Map the command ID that wx uses for the action to the UI conditions for the menu/toolbar items. More...
 

Detailed Description

Manage TOOL_ACTION objects.

Registering them and allows one to run them using associated hot keys, names or ids.

Definition at line 85 of file action_manager.h.

Member Typedef Documentation

◆ HOTKEY_LIST

typedef std::map<int, std::list<TOOL_ACTION*> > ACTION_MANAGER::HOTKEY_LIST
private

Definition at line 188 of file action_manager.h.

Constructor & Destructor Documentation

◆ ACTION_MANAGER()

ACTION_MANAGER::ACTION_MANAGER ( TOOL_MANAGER aToolManager)
Parameters
aToolManageris a tool manager instance that is used to pass events to tools.

Definition at line 35 of file action_manager.cpp.

35  :
36  m_toolMgr( aToolManager )
37 {
38  // Register known actions
39  std::list<TOOL_ACTION*>& actionList = GetActionList();
40 
41  for( TOOL_ACTION* action : actionList )
42  {
43  if( action->m_id == -1 )
44  action->m_id = MakeActionId( action->m_name );
45 
46  RegisterAction( action );
47  }
48 }
static int MakeActionId(const std::string &aActionName)
Generate an unique ID from for an action with given name.
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
TOOL_MANAGER * m_toolMgr
Map for indexing actions by their names.
void RegisterAction(TOOL_ACTION *aAction)
Add a tool action to the manager and sets it up.
Represent a single user action.
Definition: tool_action.h:49

References GetActionList(), MakeActionId(), and RegisterAction().

◆ ~ACTION_MANAGER()

ACTION_MANAGER::~ACTION_MANAGER ( )

Unregister every registered action.

Definition at line 51 of file action_manager.cpp.

52 {
53 }

Member Function Documentation

◆ FindAction()

TOOL_ACTION * ACTION_MANAGER::FindAction ( const std::string &  aActionName) const

Find an action with a given name (if there is one available).

Parameters
aActionNameis the searched action.
Returns
Pointer to a TOOL_ACTION object or NULL if there is no such action.

Definition at line 107 of file action_manager.cpp.

108 {
109  std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
110 
111  if( it != m_actionNameIndex.end() )
112  return it->second;
113 
114  return NULL;
115 }
#define NULL
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their hotkeys.

References m_actionNameIndex, and NULL.

Referenced by TOOLS_HOLDER::PopTool(), TOOLS_HOLDER::PushTool(), and TOOL_MANAGER::RunAction().

◆ GetActionList()

static std::list<TOOL_ACTION*>& ACTION_MANAGER::GetActionList ( )
inlinestatic

Return list of TOOL_ACTIONs.

#TOOL_ACTIONs add themselves to the list upon their creation.

Returns
List of TOOL_ACTIONs.

Definition at line 151 of file action_manager.h.

152  {
153  static std::list<TOOL_ACTION*> actionList;
154 
155  return actionList;
156  }

Referenced by ACTION_MANAGER(), TOOL_ACTION::TOOL_ACTION(), and TOOL_ACTION::~TOOL_ACTION().

◆ GetActions()

const std::map< std::string, TOOL_ACTION * > & ACTION_MANAGER::GetActions ( ) const

Get a list of currently-registered actions mapped by their name.

Definition at line 228 of file action_manager.cpp.

229 {
230  return m_actionNameIndex;
231 }
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their hotkeys.

References m_actionNameIndex.

Referenced by TOOL_MANAGER::GetActions().

◆ GetCondition()

const ACTION_CONDITIONS * ACTION_MANAGER::GetCondition ( const TOOL_ACTION aAction) const

Get the conditions to use for a specific tool action.

Parameters
aActionis the tool action.
Returns
the action conditions, returns nullptr if no conditions are registered.

Definition at line 87 of file action_manager.cpp.

88 {
89  const auto it = m_uiConditions.find( aAction.GetId() );
90 
91  // If the action doesn't have something registered, then return null
92  if( it == m_uiConditions.end() )
93  return nullptr;
94  else
95  return &it->second;
96 }
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:103
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.

References TOOL_ACTION::GetId(), and m_uiConditions.

Referenced by ACTION_TOOLBAR::doSelectAction(), and RunHotKey().

◆ GetHotKey()

int ACTION_MANAGER::GetHotKey ( const TOOL_ACTION aAction) const

Return the hot key associated with a given action or 0 if there is none.

Parameters
aActionis the queried action.

Definition at line 234 of file action_manager.cpp.

235 {
236  std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );
237 
238  if( it == m_hotkeys.end() )
239  return 0;
240 
241  return it->second;
242 }
std::map< int, int > m_hotkeys
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:103

References TOOL_ACTION::GetId(), and m_hotkeys.

Referenced by TOOL_MANAGER::GetHotKey().

◆ MakeActionId()

int ACTION_MANAGER::MakeActionId ( const std::string &  aActionName)
static

Generate an unique ID from for an action with given name.

Definition at line 99 of file action_manager.cpp.

100 {
101  static int currentActionId = 1;
102 
103  return currentActionId++;
104 }

Referenced by ACTION_GROUP::ACTION_GROUP(), and ACTION_MANAGER().

◆ processHotKey()

int ACTION_MANAGER::processHotKey ( TOOL_ACTION aAction,
std::map< std::string, int >  aLegacyMap,
std::map< std::string, int >  aHotKeyMap 
)
private

Tool manager needed to run actions.

Definition at line 277 of file action_manager.cpp.

279 {
280  aAction->m_hotKey = aAction->m_defaultHotKey;
281 
282  if( !aAction->m_legacyName.empty() && aLegacyMap.count( aAction->m_legacyName ) )
283  aAction->SetHotKey( aLegacyMap[ aAction->m_legacyName ] );
284 
285  if( aHotKeyMap.count( aAction->m_name ) )
286  aAction->SetHotKey( aHotKeyMap[ aAction->m_name ] );
287 
288  return aAction->m_hotKey;
289 }
std::string m_name
Definition: tool_action.h:183
const std::string m_legacyName
Definition: tool_action.h:188
void SetHotKey(int aKeycode)
Definition: tool_action.cpp:94
const int m_defaultHotKey
Definition: tool_action.h:186

References TOOL_ACTION::m_defaultHotKey, TOOL_ACTION::m_hotKey, TOOL_ACTION::m_legacyName, TOOL_ACTION::m_name, and TOOL_ACTION::SetHotKey().

Referenced by UpdateHotKeys().

◆ RegisterAction()

void ACTION_MANAGER::RegisterAction ( TOOL_ACTION aAction)

Add a tool action to the manager and sets it up.

After that is is possible to invoke the action using hotkeys or sending a command event with its name.

Parameters
aActionaction to be added. Ownership is not transferred.

Definition at line 56 of file action_manager.cpp.

57 {
58  // TOOL_ACTIONs are supposed to be named [appName.]toolName.actionName (with dots between)
59  // action name without specifying at least toolName is not valid
60  wxASSERT( aAction->GetName().find( '.', 0 ) != std::string::npos );
61 
62  // TOOL_ACTIONs must have unique names & ids
63  wxASSERT( m_actionNameIndex.find( aAction->m_name ) == m_actionNameIndex.end() );
64 
65  m_actionNameIndex[aAction->m_name] = aAction;
66 }
std::string m_name
Definition: tool_action.h:183
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their hotkeys.
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:83

References TOOL_ACTION::GetName(), m_actionNameIndex, and TOOL_ACTION::m_name.

Referenced by ACTION_MANAGER().

◆ RunHotKey()

bool ACTION_MANAGER::RunHotKey ( int  aHotKey) const

Run an action associated with a hotkey (if there is one available).

Parameters
aHotKeyis the hotkey to be handled.
Returns
True if there was an action associated with the hotkey, false otherwise.

Definition at line 118 of file action_manager.cpp.

119 {
120  int key = aHotKey & ~MD_MODIFIER_MASK;
121  int mod = aHotKey & MD_MODIFIER_MASK;
122 
123  if( key >= 'a' && key <= 'z' )
124  key = std::toupper( key );
125 
126  wxLogTrace( kicadTraceToolStack, "ACTION_MANAGER::RunHotKey Key: %s",
127  KeyNameFromKeyCode( aHotKey ) );
128 
129  HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
130 
131  // If no luck, try without Shift, to handle keys that require it
132  // e.g. to get ? you need to press Shift+/ without US keyboard layout
133  // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
134  // different combination
135  if( it == m_actionHotKeys.end() )
136  {
137  wxLogTrace( kicadTraceToolStack,
138  "ACTION_MANAGER::RunHotKey No actions found, searching with key: %s",
139  KeyNameFromKeyCode( key | ( mod & ~MD_SHIFT ) ) );
140 
141  it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
142 
143  if( it == m_actionHotKeys.end() )
144  return false; // no appropriate action found for the hotkey
145  }
146 
147  const std::list<TOOL_ACTION*>& actions = it->second;
148 
149  // Choose the action that has the highest priority on the active tools stack
150  // If there is none, run the global action associated with the hot key
151  int highestPriority = -1, priority = -1;
152  const TOOL_ACTION* context = NULL; // pointer to context action of the highest priority tool
153  std::vector<const TOOL_ACTION*> global; // pointers to global actions
154  // if there is no context action
155 
156  for( const TOOL_ACTION* action : actions )
157  {
158  if( action->GetScope() == AS_GLOBAL )
159  {
160  // Store the global action in case there are no context actions to run
161  global.emplace_back( action );
162  continue;
163  }
164 
165  TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
166 
167  if( tool )
168  {
169  // Choose the action that goes to the tool with highest priority
170  // (i.e. is on the top of active tools stack)
171  priority = m_toolMgr->GetPriority( tool->GetId() );
172 
173  if( priority >= 0 && priority > highestPriority )
174  {
175  highestPriority = priority;
176  context = action;
177  }
178  }
179  }
180 
181  // Get the selection to use to test if the action is enabled
183 
184  if( context )
185  {
186  bool runAction = true;
187 
188  if( const ACTION_CONDITIONS* aCond = GetCondition( *context ) )
189  runAction = aCond->enableCondition( sel );
190 
191  wxLogTrace( kicadTraceToolStack,
192  "ACTION_MANAGER::RunHotKey %s context action: %s for hotkey %s",
193  runAction ? "Running" : "Not running",
194  context->GetName(),
195  KeyNameFromKeyCode( aHotKey ) );
196 
197  if( runAction )
198  return m_toolMgr->RunAction( *context, true );
199  }
200  else if( !global.empty() )
201  {
202  for( auto act : global )
203  {
204  bool runAction = true;
205 
206  if( const ACTION_CONDITIONS* aCond = GetCondition( *act ) )
207  runAction = aCond->enableCondition( sel );
208 
209  wxLogTrace( kicadTraceToolStack,
210  "ACTION_MANAGER::RunHotKey %s global action: %s for hotkey %s",
211  runAction ? "Running" : "Not running",
212  act->GetName(),
213  KeyNameFromKeyCode( aHotKey ) );
214 
215  if( runAction && m_toolMgr->RunAction( *act, true ) )
216  return true;
217  }
218  }
219 
220  wxLogTrace( kicadTraceToolStack,
221  "ACTION_MANAGER::RunHotKey No action found for key %s",
222  KeyNameFromKeyCode( aHotKey ) );
223 
224  return false;
225 }
int GetPriority(int aToolId) const
Return priority of a given tool.
TOOL_ID GetId() const
Return the unique identifier of the tool.
Definition: tool_base.h:121
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:141
TOOL_BASE * FindTool(int aId) const
Search for a tool with given ID.
#define NULL
Global action (toolbar/main menu event, global shortcut)
Definition: tool_event.h:151
HOTKEY_LIST m_actionHotKeys
Quick action<->hot key lookup.
TOOL_MANAGER * m_toolMgr
Map for indexing actions by their names.
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:300
virtual SELECTION & GetCurrentSelection()
Get the current selection from the canvas area.
Definition: tools_holder.h:102
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
Base abstract interface for all kinds of tools.
Definition: tool_base.h:66
Represent a single user action.
Definition: tool_action.h:49
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
const ACTION_CONDITIONS * GetCondition(const TOOL_ACTION &aAction) const
Get the conditions to use for a specific tool action.
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:83

References AS_GLOBAL, TOOL_MANAGER::FindTool(), GetCondition(), TOOLS_HOLDER::GetCurrentSelection(), TOOL_BASE::GetId(), TOOL_ACTION::GetName(), TOOL_MANAGER::GetPriority(), TOOL_MANAGER::GetToolHolder(), KeyNameFromKeyCode(), kicadTraceToolStack, m_actionHotKeys, m_toolMgr, MD_MODIFIER_MASK, MD_SHIFT, NULL, and TOOL_MANAGER::RunAction().

Referenced by TOOL_MANAGER::DispatchHotKey().

◆ SetConditions()

void ACTION_MANAGER::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 the current UI state (e.g.

checked, enabled, shown)

Parameters
aActionis the tool action using these conditions.
aConditionsare the conditions to use for the action.

Definition at line 69 of file action_manager.cpp.

70 {
71  // Remove any existing handlers with the old conditions to ensure the UI layer doesn't have stale data
72  if( m_toolMgr )
74 
75  m_uiConditions[aAction.GetId()] = aConditions;
76 
77  wxLogTrace( kicadTraceToolStack,
78  "ACTION_MANAGER::SetConditions: Registering conditions for ID %d - %s",
79  aAction.GetId(), aAction.GetName() );
80 
81  // Register a new handler with the new conditions
82  if( m_toolMgr )
83  m_toolMgr->GetToolHolder()->RegisterUIUpdateHandler( aAction, aConditions );
84 }
virtual void UnregisterUIUpdateHandler(const TOOL_ACTION &aAction)
Unregister a UI handler for an action that was registered using RegisterUIUpdateHandler.
Definition: tools_holder.h:84
TOOL_MANAGER * m_toolMgr
Map for indexing actions by their names.
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:300
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:103
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.
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
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...
Definition: tools_holder.h:64
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:83

References TOOL_ACTION::GetId(), TOOL_ACTION::GetName(), TOOL_MANAGER::GetToolHolder(), kicadTraceToolStack, m_toolMgr, m_uiConditions, TOOLS_HOLDER::RegisterUIUpdateHandler(), and TOOLS_HOLDER::UnregisterUIUpdateHandler().

Referenced by PL_EDITOR_FRAME::setupUIConditions(), CVPCB_MAINFRAME::setupUIConditions(), FOOTPRINT_VIEWER_FRAME::setupUIConditions(), PCB_EDIT_FRAME::setupUIConditions(), DISPLAY_FOOTPRINTS_FRAME::setupUIConditions(), SYMBOL_VIEWER_FRAME::setupUIConditions(), KICAD_MANAGER_FRAME::setupUIConditions(), EDA_3D_VIEWER::setupUIConditions(), SCH_EDIT_FRAME::setupUIConditions(), FOOTPRINT_EDIT_FRAME::setupUIConditions(), SYMBOL_EDIT_FRAME::setupUIConditions(), and GERBVIEW_FRAME::setupUIConditions().

◆ UpdateHotKeys()

void ACTION_MANAGER::UpdateHotKeys ( bool  aFullUpdate)

Optionally read the hotkey config files and then rebuilds the internal hotkey maps.

Definition at line 245 of file action_manager.cpp.

246 {
247  std::map<std::string, int> legacyHotKeyMap;
248  std::map<std::string, int> userHotKeyMap;
249 
250  m_actionHotKeys.clear();
251  m_hotkeys.clear();
252 
253  if( aFullUpdate && m_toolMgr->GetToolHolder() )
254  {
256  ReadHotKeyConfig( wxEmptyString, userHotKeyMap );
257  }
258 
259  for( const auto& ii : m_actionNameIndex )
260  {
261  TOOL_ACTION* action = ii.second;
262  int hotkey = 0;
263 
264  if( aFullUpdate )
265  hotkey = processHotKey( action, legacyHotKeyMap, userHotKeyMap );
266  else
267  hotkey = action->GetHotKey();
268 
269  if( hotkey > 0 )
270  m_actionHotKeys[hotkey].push_back( action );
271 
272  m_hotkeys[action->GetId()] = hotkey;
273  }
274 }
std::map< int, int > m_hotkeys
virtual wxString ConfigBaseName()
Definition: tools_holder.h:154
void ReadHotKeyConfig(const wxString &aFileName, std::map< std::string, 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.
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their hotkeys.
HOTKEY_LIST m_actionHotKeys
Quick action<->hot key lookup.
TOOL_MANAGER * m_toolMgr
Map for indexing actions by their names.
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:300
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:103
int processHotKey(TOOL_ACTION *aAction, std::map< std::string, int > aLegacyMap, std::map< std::string, int > aHotKeyMap)
Tool manager needed to run actions.
Represent a single user action.
Definition: tool_action.h:49
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:93

References TOOLS_HOLDER::ConfigBaseName(), TOOL_ACTION::GetHotKey(), TOOL_ACTION::GetId(), TOOL_MANAGER::GetToolHolder(), m_actionHotKeys, m_actionNameIndex, m_hotkeys, m_toolMgr, processHotKey(), ReadHotKeyConfig(), and ReadLegacyHotkeyConfig().

Referenced by TOOLS_HOLDER::CommonSettingsChanged(), and TOOL_MANAGER::InitTools().

Member Data Documentation

◆ m_actionHotKeys

HOTKEY_LIST ACTION_MANAGER::m_actionHotKeys
private

Quick action<->hot key lookup.

Definition at line 189 of file action_manager.h.

Referenced by RunHotKey(), and UpdateHotKeys().

◆ m_actionNameIndex

std::map<std::string, TOOL_ACTION*> ACTION_MANAGER::m_actionNameIndex
private

Map for indexing actions by their hotkeys.

Definition at line 185 of file action_manager.h.

Referenced by FindAction(), GetActions(), RegisterAction(), and UpdateHotKeys().

◆ m_hotkeys

std::map<int, int> ACTION_MANAGER::m_hotkeys
private

Definition at line 192 of file action_manager.h.

Referenced by GetHotKey(), and UpdateHotKeys().

◆ m_toolMgr

TOOL_MANAGER* ACTION_MANAGER::m_toolMgr
private

Map for indexing actions by their names.

Definition at line 182 of file action_manager.h.

Referenced by RunHotKey(), SetConditions(), and UpdateHotKeys().

◆ m_uiConditions

std::map<int, ACTION_CONDITIONS> ACTION_MANAGER::m_uiConditions
private

Map the command ID that wx uses for the action to the UI conditions for the menu/toolbar items.

Definition at line 196 of file action_manager.h.

Referenced by GetCondition(), and SetConditions().


The documentation for this class was generated from the following files: