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 to run them using associated hot keys, names or ids.

Definition at line 42 of file action_manager.h.

Member Typedef Documentation

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 ( 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.

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

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( new TOOL_ACTION( *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
ACTION_MANAGER::~ACTION_MANAGER ( )

Destructor.

Unregisters every registered action.

Definition at line 51 of file action_manager.cpp.

References m_actionNameIndex, and UnregisterAction().

52 {
53  while( !m_actionNameIndex.empty() )
54  {
55  TOOL_ACTION* action = m_actionNameIndex.begin()->second;
56  UnregisterAction( action );
57  delete action;
58  }
59 }
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

Member Function Documentation

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

References m_actionNameIndex.

Referenced by TOOL_MANAGER::RunAction().

102 {
103  std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
104 
105  if( it != m_actionNameIndex.end() )
106  return it->second;
107 
108  return NULL;
109 }
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their names
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.

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

113  {
114  static std::list<TOOL_ACTION*> actionList;
115 
116  return actionList;
117  }
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.

References TOOL_ACTION::GetId(), and m_hotkeys.

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

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:85
int ACTION_MANAGER::MakeActionId ( const std::string &  aActionName)
static

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

Definition at line 93 of file action_manager.cpp.

Referenced by ACTION_MANAGER().

94 {
95  static int currentActionId = 1;
96 
97  return currentActionId++;
98 }
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 229 of file action_manager.cpp.

References TOOL_ACTION::getDefaultHotKey(), TOOL_MANAGER::GetEditFrame(), EDA_DRAW_FRAME::GetHotKeyDescription(), 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().

230 {
231  int hotkey = aAction->getDefaultHotKey();
232 
233  if( ( hotkey & TOOL_ACTION::LEGACY_HK ) )
234  {
235  hotkey = hotkey & ~TOOL_ACTION::LEGACY_HK; // it leaves only HK_xxx identifier
236  EDA_DRAW_FRAME* frame = static_cast<EDA_DRAW_FRAME*>( m_toolMgr->GetEditFrame() );
237  EDA_HOTKEY* hk_desc = frame->GetHotKeyDescription( hotkey );
238 
239  if( hk_desc )
240  {
241  hotkey = hk_desc->m_KeyCode;
242 
243  // Convert modifiers to the ones used by the Tool Framework
244  if( hotkey & GR_KB_CTRL )
245  {
246  hotkey &= ~GR_KB_CTRL;
247  hotkey |= MD_CTRL;
248  }
249 
250  if( hotkey & GR_KB_ALT )
251  {
252  hotkey &= ~GR_KB_ALT;
253  hotkey |= MD_ALT;
254  }
255 
256  if( hotkey & GR_KB_SHIFT )
257  {
258  hotkey &= ~GR_KB_SHIFT;
259  hotkey |= MD_SHIFT;
260  }
261  }
262  else
263  {
264  hotkey = 0;
265  }
266  }
267 
268  return hotkey;
269 }
wxWindow * GetEditFrame() const
Definition: tool_manager.h:258
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:53
#define GR_KB_ALT
Definition: common.h:65
virtual EDA_HOTKEY * GetHotKeyDescription(int aCommand) const =0
Function GetHotKeyDescription Searches lists of hot key identifiers (HK_xxx) used in the frame to fin...
#define GR_KB_SHIFT
Definition: common.h:66
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:220
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:57
Class TOOL_ACTION.
Definition: tool_action.h:46
int getDefaultHotKey()
Returns the hot key assigned in the object definition.
Definition: tool_action.h:186
#define GR_KB_CTRL
Definition: common.h:64
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 62 of file action_manager.cpp.

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

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

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

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().

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

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

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

76 {
77  m_actionNameIndex.erase( aAction->m_name );
78  int hotkey = GetHotKey( *aAction );
79 
80  if( hotkey )
81  {
82  std::list<TOOL_ACTION*>& actions = m_actionHotKeys[hotkey];
83  std::list<TOOL_ACTION*>::iterator action = std::find( actions.begin(), actions.end(), aAction );
84 
85  if( action != actions.end() )
86  actions.erase( action );
87  else
88  assert( false );
89  }
90 }
std::string m_name
Name of the action (convention is: app.[tool.]action.name)
Definition: tool_action.h:192
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
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 195 of file action_manager.cpp.

References AS_GLOBAL, m_actionHotKeys, m_actionNameIndex, m_hotkeys, and processHotKey().

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

196 {
197  m_actionHotKeys.clear();
198  m_hotkeys.clear();
199 
200  for( TOOL_ACTION* action : m_actionNameIndex | boost::adaptors::map_values )
201  {
202  int hotkey = processHotKey( action );
203 
204  if( hotkey > 0 )
205  {
206  m_actionHotKeys[hotkey].push_back( action );
207  m_hotkeys[action->GetId()] = hotkey;
208  }
209  }
210 
211 #ifndef NDEBUG
212  // Check if there are two global actions assigned to the same hotkey
213  for( std::list<TOOL_ACTION*>& action_list : m_actionHotKeys | boost::adaptors::map_values )
214  {
215  int global_actions_cnt = 0;
216 
217  for( TOOL_ACTION* action : action_list )
218  {
219  if( action->GetScope() == AS_GLOBAL )
220  ++global_actions_cnt;
221  }
222 
223  assert( global_actions_cnt <= 1 );
224  }
225 #endif /* not NDEBUG */
226 }
std::map< int, int > m_hotkeys
Quick action<->hot key lookup
All active tools
Definition: tool_event.h:138
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
Class TOOL_ACTION.
Definition: tool_action.h:46

Member Data Documentation

HOTKEY_LIST ACTION_MANAGER::m_actionHotKeys
private

Definition at line 132 of file action_manager.h.

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

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().

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().

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: