KiCad PCB EDA Suite
ACTION_MANAGER Class Reference

Class ACTION_MANAGER. More...

#include <action_manager.h>

Public Member Functions

 ACTION_MANAGER (TOOL_MANAGER *aToolManager)
 Constructor. More...
 
 ~ACTION_MANAGER ()
 Destructor. More...
 
void RegisterAction (TOOL_ACTION *aAction)
 Function RegisterAction() Adds a tool action to the manager and sets it up. More...
 
const std::map< std::string, TOOL_ACTION * > & GetActions ()
 Get a list of currently-registered actions mapped by their name. More...
 
TOOL_ACTIONFindAction (const std::string &aActionName) const
 Function FindAction() Finds an action with a given name (if there is one available). More...
 
bool RunHotKey (int aHotKey) const
 Function RunHotKey() Runs an action associated with a hotkey (if there is one available). More...
 
int GetHotKey (const TOOL_ACTION &aAction) const
 Function GetHotKey() Returns the hot key associated with a given action or 0 if there is none. More...
 
void UpdateHotKeys (bool aFullUpdate)
 Function UpdateHotKeys() Optionally reads the hotkey config files and then rebuilds the internal hotkey maps. More...
 

Static Public Member Functions

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

Private Types

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

Map for indexing actions by their hotkeys

More...
 

Private Member Functions

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

Private Attributes

TOOL_MANAGERm_toolMgr
 

Tool manager needed to run actions

More...
 
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
 

Map for indexing actions by their names

More...
 
HOTKEY_LIST m_actionHotKeys
 
std::map< int, int > m_hotkeys
 

Quick action<->hot key lookup

More...
 

Detailed Description

Class ACTION_MANAGER.

Takes care of TOOL_ACTION objects. Registers them and allows one to run them using associated hot keys, names or ids.

Definition at line 42 of file action_manager.h.

Member Typedef Documentation

◆ HOTKEY_LIST

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

Map for indexing actions by their hotkeys

Definition at line 130 of file action_manager.h.

Constructor & Destructor Documentation

◆ ACTION_MANAGER()

ACTION_MANAGER::ACTION_MANAGER ( TOOL_MANAGER aToolManager)

Constructor.

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)
Generates an unique ID from for an action with given name.
static std::list< TOOL_ACTION * > & GetActionList()
Function GetActionList() Returns list of TOOL_ACTIONs.
TOOL_MANAGER * m_toolMgr
Tool manager needed to run actions
void RegisterAction(TOOL_ACTION *aAction)
Function RegisterAction() Adds a tool action to the manager and sets it up.
Class TOOL_ACTION.
Definition: tool_action.h:46

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

◆ ~ACTION_MANAGER()

ACTION_MANAGER::~ACTION_MANAGER ( )

Destructor.

Unregisters 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

Function FindAction() Finds 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 77 of file action_manager.cpp.

78 {
79  std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
80 
81  if( it != m_actionNameIndex.end() )
82  return it->second;
83 
84  return NULL;
85 }
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names

References m_actionNameIndex.

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

◆ GetActionList()

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

Function GetActionList() Returns list of TOOL_ACTIONs.

TOOL_ACTIONs add themselves to the list upon their creation.

Returns
List of TOOL_ACTIONs.

Definition at line 110 of file action_manager.h.

111  {
112  static std::list<TOOL_ACTION*> actionList;
113 
114  return actionList;
115  }

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

◆ GetActions()

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

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

Definition at line 178 of file action_manager.cpp.

179 {
180  return m_actionNameIndex;
181 }
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names

References m_actionNameIndex.

Referenced by TOOL_MANAGER::GetActions().

◆ GetHotKey()

int ACTION_MANAGER::GetHotKey ( const TOOL_ACTION aAction) const

Function GetHotKey() Returns the hot key associated with a given action or 0 if there is none.

Parameters
aActionis the queried action.

Definition at line 184 of file action_manager.cpp.

185 {
186  std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );
187 
188  if( it == m_hotkeys.end() )
189  return 0;
190 
191  return it->second;
192 }
std::map< int, int > m_hotkeys
Quick action<->hot key lookup
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:100

References TOOL_ACTION::GetId(), and m_hotkeys.

Referenced by TOOL_MANAGER::GetHotKey().

◆ MakeActionId()

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

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

Definition at line 69 of file action_manager.cpp.

70 {
71  static int currentActionId = 1;
72 
73  return currentActionId++;
74 }

Referenced by ACTION_MANAGER().

◆ processHotKey()

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

Definition at line 227 of file action_manager.cpp.

229 {
230  aAction->m_hotKey = aAction->m_defaultHotKey;
231 
232  if( !aAction->m_legacyName.empty() && aLegacyMap.count( aAction->m_legacyName ) )
233  aAction->SetHotKey( aLegacyMap[ aAction->m_legacyName ] );
234 
235  if( aHotKeyMap.count( aAction->m_name ) )
236  aAction->SetHotKey( aHotKeyMap[ aAction->m_name ] );
237 
238  return aAction->m_hotKey;
239 }
std::string m_name
Name of the action (convention is "app.tool.actionName")
Definition: tool_action.h:160
const std::string m_legacyName
Definition: tool_action.h:165
void SetHotKey(int aKeycode)
Definition: tool_action.cpp:94
const int m_defaultHotKey
Definition: tool_action.h:163

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)

Function RegisterAction() Adds 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
Name of the action (convention is "app.tool.actionName")
Definition: tool_action.h:160
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78

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

Referenced by ACTION_MANAGER().

◆ RunHotKey()

bool ACTION_MANAGER::RunHotKey ( int  aHotKey) const

Function RunHotKey() Runs 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 88 of file action_manager.cpp.

89 {
90  int key = aHotKey & ~MD_MODIFIER_MASK;
91  int mod = aHotKey & MD_MODIFIER_MASK;
92 
93  if( key >= 'a' && key <= 'z' )
94  key = std::toupper( key );
95 
96  wxLogTrace( kicadTraceToolStack, "ACTION_MANAGER::RunHotKey Key: %s",
97  KeyNameFromKeyCode( aHotKey ) );
98 
99  HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
100 
101  // If no luck, try without Shift, to handle keys that require it
102  // e.g. to get ? you need to press Shift+/ without US keyboard layout
103  // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
104  // different combination
105  if( it == m_actionHotKeys.end() )
106  {
107  wxLogTrace( kicadTraceToolStack,
108  "ACTION_MANAGER::RunHotKey No actions found, searching with key: %s",
109  KeyNameFromKeyCode( key | ( mod & ~MD_SHIFT ) ) );
110  it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
111 
112  if( it == m_actionHotKeys.end() )
113  return false; // no appropriate action found for the hotkey
114  }
115 
116  const std::list<TOOL_ACTION*>& actions = it->second;
117 
118  // Choose the action that has the highest priority on the active tools stack
119  // If there is none, run the global action associated with the hot key
120  int highestPriority = -1, priority = -1;
121  const TOOL_ACTION* context = NULL; // pointer to context action of the highest priority tool
122  std::vector<const TOOL_ACTION*> global; // pointers to global actions
123  // if there is no context action
124 
125  for( const TOOL_ACTION* action : actions )
126  {
127  if( action->GetScope() == AS_GLOBAL )
128  {
129  // Store the global action in case there are no context actions to run
130  global.emplace_back( action );
131  continue;
132  }
133 
134  TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
135 
136  if( tool )
137  {
138  // Choose the action that goes to the tool with highest priority
139  // (i.e. is on the top of active tools stack)
140  priority = m_toolMgr->GetPriority( tool->GetId() );
141 
142  if( priority >= 0 && priority > highestPriority )
143  {
144  highestPriority = priority;
145  context = action;
146  }
147  }
148  }
149 
150  if( context )
151  {
152  wxLogTrace( kicadTraceToolStack,
153  "ACTION_MANAGER::RunHotKey Running action %s for hotkey %s", context->GetName(),
154  KeyNameFromKeyCode( aHotKey ) );
155 
156  return m_toolMgr->RunAction( *context, true );
157  }
158  else if( !global.empty() )
159  {
160  for( auto act : global )
161  {
162  wxLogTrace( kicadTraceToolStack,
163  "ACTION_MANAGER::RunHotKey Running action: %s for hotkey %s", act->GetName(),
164  KeyNameFromKeyCode( aHotKey ) );
165 
166  if( m_toolMgr->RunAction( *act, true ) )
167  return true;
168  }
169  }
170 
171  wxLogTrace( kicadTraceToolStack, "ACTION_MANAGER::RunHotKey No action found for key %s",
172  KeyNameFromKeyCode( aHotKey ) );
173 
174  return false;
175 }
int GetPriority(int aToolId) const
Returns priority of a given tool.
TOOL_ID GetId() const
Function GetId() Returns the unique identifier of the tool.
Definition: tool_base.h:121
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
All active tools
Definition: tool_event.h:144
HOTKEY_LIST m_actionHotKeys
TOOL_MANAGER * m_toolMgr
Tool manager needed to run actions
Class TOOL_BASE.
Definition: tool_base.h:67
Class TOOL_ACTION.
Definition: tool_action.h:46
const wxChar *const kicadTraceToolStack
Flag to enable tracing of the tool handling stack.
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...
#define mod(a, n)
Definition: greymap.cpp:24
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78

References AS_GLOBAL, TOOL_MANAGER::FindTool(), TOOL_BASE::GetId(), TOOL_ACTION::GetName(), TOOL_MANAGER::GetPriority(), KeyNameFromKeyCode(), kicadTraceToolStack, m_actionHotKeys, m_toolMgr, MD_MODIFIER_MASK, MD_SHIFT, mod, and TOOL_MANAGER::RunAction().

Referenced by TOOL_MANAGER::dispatchHotKey().

◆ UpdateHotKeys()

void ACTION_MANAGER::UpdateHotKeys ( bool  aFullUpdate)

Function UpdateHotKeys() Optionally reads the hotkey config files and then rebuilds the internal hotkey maps.

Definition at line 195 of file action_manager.cpp.

196 {
197  std::map<std::string, int> legacyHotKeyMap;
198  std::map<std::string, int> userHotKeyMap;
199 
200  m_actionHotKeys.clear();
201  m_hotkeys.clear();
202 
203  if( aFullUpdate )
204  {
206  ReadHotKeyConfig( wxEmptyString, userHotKeyMap );
207  }
208 
209  for( const auto& ii : m_actionNameIndex )
210  {
211  TOOL_ACTION* action = ii.second;
212  int hotkey = 0;
213 
214  if( aFullUpdate )
215  hotkey = processHotKey( action, legacyHotKeyMap, userHotKeyMap );
216  else
217  hotkey = action->GetHotKey();
218 
219  if( hotkey > 0 )
220  m_actionHotKeys[hotkey].push_back( action );
221 
222  m_hotkeys[action->GetId()] = hotkey;
223  }
224 }
std::map< int, int > m_hotkeys
Quick action<->hot key lookup
EDA_BASE_FRAME * GetEditFrame() const
Definition: tool_manager.h:256
wxString ConfigBaseName()
void ReadHotKeyConfig(wxString fileName, std::map< std::string, int > &aHotKeys)
Function ReadotKeyConfig Reads a hotkey config file into a map.
int ReadLegacyHotkeyConfig(const wxString &aAppname, std::map< std::string, int > &aMap)
Function ReadLegacyHotkeyConfig 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 names
HOTKEY_LIST m_actionHotKeys
TOOL_MANAGER * m_toolMgr
Tool manager needed to run actions
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:100
int processHotKey(TOOL_ACTION *aAction, std::map< std::string, int > aLegacyMap, std::map< std::string, int > aHotKeyMap)
Class TOOL_ACTION.
Definition: tool_action.h:46
int GetHotKey() const
Function GetHotKey() Returns the hotkey keycode which initiates the action.
Definition: tool_action.h:90

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

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

Member Data Documentation

◆ m_actionHotKeys

HOTKEY_LIST ACTION_MANAGER::m_actionHotKeys
private

Definition at line 131 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 names

Definition at line 127 of file action_manager.h.

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

◆ m_hotkeys

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

Quick action<->hot key lookup

Definition at line 134 of file action_manager.h.

Referenced by GetHotKey(), and UpdateHotKeys().

◆ m_toolMgr

TOOL_MANAGER* ACTION_MANAGER::m_toolMgr
private

Tool manager needed to run actions

Definition at line 124 of file action_manager.h.

Referenced by RunHotKey(), and UpdateHotKeys().


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