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...
 
void UnregisterAction (TOOL_ACTION *aAction)
 Function UnregisterAction() Removes a tool action from the manager and makes it unavailable for further usage. 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 ()
 Function UpdateHotKeys() Updates TOOL_ACTIONs hot key assignment according to the current frame's Hot Key Editor settings. 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)
 

Resolves a reference to legacy hot key settings to a particular hot key.

More...
 

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 131 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 34 of file action_manager.cpp.

34  :
35  m_toolMgr( aToolManager )
36 {
37  // Register known actions
38  std::list<TOOL_ACTION*>& actionList = GetActionList();
39 
40  for( TOOL_ACTION* action : actionList )
41  {
42  if( action->m_id == -1 )
43  action->m_id = MakeActionId( action->m_name );
44 
45  RegisterAction( new TOOL_ACTION( *action ) );
46  }
47 }
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 50 of file action_manager.cpp.

51 {
52  while( !m_actionNameIndex.empty() )
53  {
54  TOOL_ACTION* action = m_actionNameIndex.begin()->second;
55  UnregisterAction( action );
56  delete action;
57  }
58 }
void UnregisterAction(TOOL_ACTION *aAction)
Function UnregisterAction() Removes a tool action from the manager and makes it unavailable for furth...
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names
Class TOOL_ACTION.
Definition: tool_action.h:46

References m_actionNameIndex, and UnregisterAction().

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 98 of file action_manager.cpp.

99 {
100  std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
101 
102  if( it != m_actionNameIndex.end() )
103  return it->second;
104 
105  return NULL;
106 }
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names

References m_actionNameIndex.

Referenced by 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 112 of file action_manager.h.

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

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

◆ 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 181 of file action_manager.cpp.

182 {
183  std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );
184 
185  if( it == m_hotkeys.end() )
186  return 0;
187 
188  return it->second;
189 }
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:85

References TOOL_ACTION::GetId(), and m_hotkeys.

Referenced by TOOL_MANAGER::GetHotKey(), and UnregisterAction().

◆ MakeActionId()

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

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

Definition at line 90 of file action_manager.cpp.

91 {
92  static int currentActionId = 1;
93 
94  return currentActionId++;
95 }

Referenced by ACTION_MANAGER().

◆ processHotKey()

int ACTION_MANAGER::processHotKey ( TOOL_ACTION aAction)
private

Resolves a reference to legacy hot key settings to a particular hot key.

Parameters
aActionis the action to be resolved.

Definition at line 236 of file action_manager.cpp.

237 {
238  int hotkey = aAction->getDefaultHotKey();
239 
240  if( ( hotkey & TOOL_ACTION::LEGACY_HK ) )
241  {
242  hotkey = hotkey & ~TOOL_ACTION::LEGACY_HK; // it leaves only HK_xxx identifier
243 
244  auto frame = dynamic_cast<EDA_DRAW_FRAME*>( m_toolMgr->GetEditFrame() );
245  EDA_HOTKEY* hk_desc = nullptr;
246 
247  if( frame )
248  hk_desc = frame->GetHotKeyDescription( hotkey );
249 
250  if( hk_desc )
251  {
252  hotkey = hk_desc->m_KeyCode;
253 
254  // Convert modifiers to the ones used by the Tool Framework
255  if( hotkey & GR_KB_CTRL )
256  {
257  hotkey &= ~GR_KB_CTRL;
258  hotkey |= MD_CTRL;
259  }
260 
261  if( hotkey & GR_KB_ALT )
262  {
263  hotkey &= ~GR_KB_ALT;
264  hotkey |= MD_ALT;
265  }
266 
267  if( hotkey & GR_KB_SHIFT )
268  {
269  hotkey &= ~GR_KB_SHIFT;
270  hotkey |= MD_SHIFT;
271  }
272  }
273  else
274  {
275  hotkey = 0;
276  }
277  }
278 
279  return hotkey;
280 }
#define GR_KB_ALT
EDA_DRAW_FRAME * GetEditFrame() const
Definition: tool_manager.h:267
#define GR_KB_CTRL
static const int LEGACY_HK
Flag to determine the hot key settings is not a particular key, but a reference to legacy hot key set...
Definition: tool_action.h:211
#define GR_KB_SHIFT
TOOL_MANAGER * m_toolMgr
Tool manager needed to run actions
class EDA_HOTKEY is a class to handle hot key commands.
Definition: hotkeys_basic.h:59
Class TOOL_ACTION.
Definition: tool_action.h:46
int getDefaultHotKey()
Returns the hot key assigned in the object definition.
Definition: tool_action.h:177

References TOOL_ACTION::getDefaultHotKey(), TOOL_MANAGER::GetEditFrame(), GR_KB_ALT, GR_KB_CTRL, GR_KB_SHIFT, TOOL_ACTION::LEGACY_HK, EDA_HOTKEY::m_KeyCode, m_toolMgr, MD_ALT, MD_CTRL, and MD_SHIFT.

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 61 of file action_manager.cpp.

62 {
63  // TOOL_ACTIONs are supposed to be named [appName.]toolName.actionName (with dots between)
64  // action name without specifying at least toolName is not valid
65  wxASSERT( aAction->GetName().find( '.', 0 ) != std::string::npos );
66 
67  // TOOL_ACTIONs must have unique names & ids
68  wxASSERT( m_actionNameIndex.find( aAction->m_name ) == m_actionNameIndex.end() );
69 
70  m_actionNameIndex[aAction->m_name] = aAction;
71 }
std::string m_name
Name of the action (convention is: app.[tool.]action.name)
Definition: tool_action.h:183
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:73

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

Referenced by ACTION_MANAGER(), and TOOL_MANAGER::RegisterAction().

◆ 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 109 of file action_manager.cpp.

110 {
111  int key = aHotKey & ~MD_MODIFIER_MASK;
112  int mod = aHotKey & MD_MODIFIER_MASK;
113 
114  if( key >= 'a' && key <= 'z' )
115  key = std::toupper( key );
116 
117  HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
118 
119  // If no luck, try without Shift, to handle keys that require it
120  // e.g. to get ? you need to press Shift+/ without US keyboard layout
121  // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
122  // different combination
123  if( it == m_actionHotKeys.end() )
124  {
125  it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
126 
127  if( it == m_actionHotKeys.end() )
128  return false; // no appropriate action found for the hotkey
129  }
130 
131  const std::list<TOOL_ACTION*>& actions = it->second;
132 
133  // Choose the action that has the highest priority on the active tools stack
134  // If there is none, run the global action associated with the hot key
135  int highestPriority = -1, priority = -1;
136  const TOOL_ACTION* context = NULL; // pointer to context action of the highest priority tool
137  const TOOL_ACTION* global = NULL; // pointer to global action, if there is no context action
138 
139  for( const TOOL_ACTION* action : actions )
140  {
141  if( action->GetScope() == AS_GLOBAL )
142  {
143  // Store the global action for the hot key in case there was no possible
144  // context actions to run
145  wxASSERT( global == NULL ); // there should be only one global action per hot key
146  global = action;
147  continue;
148  }
149 
150  TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
151 
152  if( tool )
153  {
154  // Choose the action that goes to the tool with highest priority
155  // (i.e. is on the top of active tools stack)
156  priority = m_toolMgr->GetPriority( tool->GetId() );
157 
158  if( priority >= 0 && priority > highestPriority )
159  {
160  highestPriority = priority;
161  context = action;
162  }
163  }
164  }
165 
166  if( context )
167  {
168  m_toolMgr->RunAction( *context, true );
169  return true;
170  }
171  else if( global )
172  {
173  m_toolMgr->RunAction( *global, true );
174  return true;
175  }
176 
177  return false;
178 }
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:122
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
All active tools
Definition: tool_event.h:143
HOTKEY_LIST m_actionHotKeys
TOOL_MANAGER * m_toolMgr
Tool manager needed to run actions
Class TOOL_BASE.
Definition: tool_base.h:68
Class TOOL_ACTION.
Definition: tool_action.h:46
#define mod(a, n)
Definition: greymap.cpp:24

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

Referenced by TOOL_MANAGER::dispatchStandardEvents().

◆ UnregisterAction()

void ACTION_MANAGER::UnregisterAction ( TOOL_ACTION aAction)

Function UnregisterAction() Removes a tool action from the manager and makes it unavailable for further usage.

Parameters
aActionaction to be removed.

Definition at line 74 of file action_manager.cpp.

75 {
76  m_actionNameIndex.erase( aAction->m_name );
77  int hotkey = GetHotKey( *aAction );
78 
79  if( hotkey )
80  {
81  std::list<TOOL_ACTION*>& actions = m_actionHotKeys[hotkey];
82  auto action = std::find( actions.begin(), actions.end(), aAction );
83 
84  if( action != actions.end() )
85  actions.erase( action );
86  }
87 }
std::string m_name
Name of the action (convention is: app.[tool.]action.name)
Definition: tool_action.h:183
int GetHotKey(const TOOL_ACTION &aAction) const
Function GetHotKey() Returns the hot key associated with a given action or 0 if there is none.
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names
HOTKEY_LIST m_actionHotKeys

References GetHotKey(), m_actionHotKeys, m_actionNameIndex, and TOOL_ACTION::m_name.

Referenced by TOOL_MANAGER::UnregisterAction(), and ~ACTION_MANAGER().

◆ UpdateHotKeys()

void ACTION_MANAGER::UpdateHotKeys ( )

Function UpdateHotKeys() Updates TOOL_ACTIONs hot key assignment according to the current frame's Hot Key Editor settings.

Definition at line 192 of file action_manager.cpp.

193 {
194  m_actionHotKeys.clear();
195  m_hotkeys.clear();
196 
197  for( const auto& actionName : m_actionNameIndex )
198  {
199  TOOL_ACTION* action = actionName.second;
200  int hotkey = processHotKey( action );
201 
202  if( hotkey <= 0 )
203  continue;
204 
205  // Second hotkey takes priority as defaults are loaded first and updates
206  // are loaded after
207  if( action->GetScope() == AS_GLOBAL && m_actionHotKeys.count( hotkey ) )
208  {
209  for( auto it = m_actionHotKeys[hotkey].begin();
210  it != m_actionHotKeys[hotkey].end(); )
211  {
212  if( (*it)->GetScope() == AS_GLOBAL )
213  {
214  // Users are free to define colliding hotkeys, but we want to know if
215  // our default set has any collisions.
216  wxMessageBox( wxString::Format(
217  "Duplicate hotkey definitions for '%s': %s and %s",
218  KeyNameFromKeyCode( hotkey ),
219  actionName.first,
220  m_actionHotKeys[hotkey].front()->GetName() ) );
221 
222  it = m_actionHotKeys[hotkey].erase( it );
223  }
224  else
225  it++;
226  }
227  }
228 
229  m_actionHotKeys[hotkey].push_back( action );
230  m_hotkeys[action->GetId()] = hotkey;
231 
232  }
233 }
std::map< int, int > m_hotkeys
Quick action<->hot key lookup
TOOL_ACTION_SCOPE GetScope() const
Definition: tool_action.h:125
All active tools
Definition: tool_event.h:143
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names
int processHotKey(TOOL_ACTION *aAction)
Resolves a reference to legacy hot key settings to a particular hot key.
HOTKEY_LIST m_actionHotKeys
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
Class TOOL_ACTION.
Definition: tool_action.h:46
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...

References AS_GLOBAL, Format(), TOOL_ACTION::GetId(), TOOL_ACTION::GetScope(), KeyNameFromKeyCode(), m_actionHotKeys, m_actionNameIndex, m_hotkeys, and processHotKey().

Referenced by TOOL_MANAGER::SetEnvironment(), and TOOL_MANAGER::UpdateHotKeys().

Member Data Documentation

◆ m_actionHotKeys

HOTKEY_LIST ACTION_MANAGER::m_actionHotKeys
private

Definition at line 132 of file action_manager.h.

Referenced by RunHotKey(), UnregisterAction(), 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 128 of file action_manager.h.

Referenced by FindAction(), RegisterAction(), UnregisterAction(), UpdateHotKeys(), and ~ACTION_MANAGER().

◆ m_hotkeys

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

Quick action<->hot key lookup

Definition at line 135 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 125 of file action_manager.h.

Referenced by processHotKey(), and RunHotKey().


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