KiCad PCB EDA Suite
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 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <tool/action_manager.h>
26 #include <tool/tool_manager.h>
27 #include <tool/tool_action.h>
28 #include <draw_frame.h>
29 
30 #include <hotkeys_basic.h>
31 #include <cctype>
32 
34  m_toolMgr( aToolManager )
35 {
36  // Register known actions
37  std::list<TOOL_ACTION*>& actionList = GetActionList();
38 
39  for( TOOL_ACTION* action : actionList )
40  {
41  if( action->m_id == -1 )
42  action->m_id = MakeActionId( action->m_name );
43 
44  RegisterAction( new TOOL_ACTION( *action ) );
45  }
46 }
47 
48 
50 {
51  while( !m_actionNameIndex.empty() )
52  {
53  TOOL_ACTION* action = m_actionNameIndex.begin()->second;
54  UnregisterAction( action );
55  delete action;
56  }
57 }
58 
59 
61 {
62  // TOOL_ACTIONs are supposed to be named [appName.]toolName.actionName (with dots between)
63  // action name without specifying at least toolName is not valid
64  wxASSERT( aAction->GetName().find( '.', 0 ) != std::string::npos );
65 
66  // TOOL_ACTIONs must have unique names & ids
67  wxASSERT( m_actionNameIndex.find( aAction->m_name ) == m_actionNameIndex.end() );
68 
69  m_actionNameIndex[aAction->m_name] = aAction;
70 }
71 
72 
74 {
75  m_actionNameIndex.erase( aAction->m_name );
76  int hotkey = GetHotKey( *aAction );
77 
78  if( hotkey )
79  {
80  std::list<TOOL_ACTION*>& actions = m_actionHotKeys[hotkey];
81  std::list<TOOL_ACTION*>::iterator action = std::find( actions.begin(), actions.end(), aAction );
82 
83  if( action != actions.end() )
84  actions.erase( action );
85  else
86  wxASSERT( false );
87  }
88 }
89 
90 
91 int ACTION_MANAGER::MakeActionId( const std::string& aActionName )
92 {
93  static int currentActionId = 1;
94 
95  return currentActionId++;
96 }
97 
98 
99 TOOL_ACTION* ACTION_MANAGER::FindAction( const std::string& aActionName ) const
100 {
101  std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
102 
103  if( it != m_actionNameIndex.end() )
104  return it->second;
105 
106  return NULL;
107 }
108 
109 
110 bool ACTION_MANAGER::RunHotKey( int aHotKey ) const
111 {
112  int key = aHotKey & ~MD_MODIFIER_MASK;
113  int mod = aHotKey & MD_MODIFIER_MASK;
114 
115  if( key >= 'a' && key <= 'z' )
116  key = std::toupper( key );
117 
118  HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
119 
120  // If no luck, try without Shift, to handle keys that require it
121  // e.g. to get ? you need to press Shift+/ without US keyboard layout
122  // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
123  // different combination
124  if( it == m_actionHotKeys.end() )
125  {
126  it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
127 
128  if( it == m_actionHotKeys.end() )
129  return false; // no appropriate action found for the hotkey
130  }
131 
132  const std::list<TOOL_ACTION*>& actions = it->second;
133 
134  // Choose the action that has the highest priority on the active tools stack
135  // If there is none, run the global action associated with the hot key
136  int highestPriority = -1, priority = -1;
137  const TOOL_ACTION* context = NULL; // pointer to context action of the highest priority tool
138  const TOOL_ACTION* global = NULL; // pointer to global action, if there is no context action
139 
140  for( const TOOL_ACTION* action : actions )
141  {
142  if( action->GetScope() == AS_GLOBAL )
143  {
144  // Store the global action for the hot key in case there was no possible
145  // context actions to run
146  wxASSERT( global == NULL ); // there should be only one global action per hot key
147  global = action;
148  continue;
149  }
150 
151  TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
152 
153  if( tool )
154  {
155  // Choose the action that goes to the tool with highest priority
156  // (i.e. is on the top of active tools stack)
157  priority = m_toolMgr->GetPriority( tool->GetId() );
158 
159  if( priority >= 0 && priority > highestPriority )
160  {
161  highestPriority = priority;
162  context = action;
163  }
164  }
165  }
166 
167  if( context )
168  {
169  m_toolMgr->RunAction( *context, true );
170  return true;
171  }
172  else if( global )
173  {
174  m_toolMgr->RunAction( *global, true );
175  return true;
176  }
177 
178  return false;
179 }
180 
181 
182 int ACTION_MANAGER::GetHotKey( const TOOL_ACTION& aAction ) const
183 {
184  std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );
185 
186  if( it == m_hotkeys.end() )
187  return 0;
188 
189  return it->second;
190 }
191 
192 
194 {
195  m_actionHotKeys.clear();
196  m_hotkeys.clear();
197 
198  for( const auto& actionName : m_actionNameIndex )
199  {
200  TOOL_ACTION* action = actionName.second;
201  int hotkey = processHotKey( action );
202 
203  if( hotkey > 0 )
204  {
205  m_actionHotKeys[hotkey].push_back( action );
206  m_hotkeys[action->GetId()] = hotkey;
207  }
208  }
209 
210 #ifdef DEBUG
211  // Check if there are two global actions assigned to the same hotkey
212  for( const auto& action_list : m_actionHotKeys )
213  {
214  int global_actions_cnt = 0;
215 
216  for( const TOOL_ACTION* action : action_list.second )
217  {
218  if( action->GetScope() == AS_GLOBAL )
219  ++global_actions_cnt;
220  }
221 
222  wxASSERT( global_actions_cnt <= 1 );
223  }
224 #endif /* not NDEBUG */
225 }
226 
227 
229 {
230  int hotkey = aAction->getDefaultHotKey();
231 
232  if( ( hotkey & TOOL_ACTION::LEGACY_HK ) )
233  {
234  hotkey = hotkey & ~TOOL_ACTION::LEGACY_HK; // it leaves only HK_xxx identifier
235 
236  auto frame = dynamic_cast<EDA_DRAW_FRAME*>( m_toolMgr->GetEditFrame() );
237  EDA_HOTKEY* hk_desc = nullptr;
238 
239  if( frame )
240  hk_desc = frame->GetHotKeyDescription( hotkey );
241 
242  if( hk_desc )
243  {
244  hotkey = hk_desc->m_KeyCode;
245 
246  // Convert modifiers to the ones used by the Tool Framework
247  if( hotkey & GR_KB_CTRL )
248  {
249  hotkey &= ~GR_KB_CTRL;
250  hotkey |= MD_CTRL;
251  }
252 
253  if( hotkey & GR_KB_ALT )
254  {
255  hotkey &= ~GR_KB_ALT;
256  hotkey |= MD_ALT;
257  }
258 
259  if( hotkey & GR_KB_SHIFT )
260  {
261  hotkey &= ~GR_KB_SHIFT;
262  hotkey |= MD_SHIFT;
263  }
264  }
265  else
266  {
267  hotkey = 0;
268  }
269  }
270 
271  return hotkey;
272 }
std::map< int, int > m_hotkeys
Quick action<->hot key lookup
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
static int MakeActionId(const std::string &aActionName)
Generates an unique ID from for an action with given name.
void UpdateHotKeys()
Function UpdateHotKeys() Updates TOOL_ACTIONs hot key assignment according to the current frame&#39;s Hot...
wxWindow * GetEditFrame() const
Definition: tool_manager.h:267
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
~ACTION_MANAGER()
Destructor.
TOOL_ACTION * FindAction(const std::string &aActionName) const
Function FindAction() Finds an action with a given name (if there is one available).
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:63
static std::list< TOOL_ACTION * > & GetActionList()
Function GetActionList() Returns list of TOOL_ACTIONs.
std::string m_name
Name of the action (convention is: app.[tool.]action.name)
Definition: tool_action.h:192
#define GR_KB_ALT
Definition: common.h:65
Class TOOL_MANAGER.
Definition: tool_manager.h:49
TOOL_ACTION_SCOPE GetScope() const
Definition: tool_action.h:134
#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
int GetHotKey(const TOOL_ACTION &aAction) const
Function GetHotKey() Returns the hot key associated with a given action or 0 if there is none...
bool RunHotKey(int aHotKey) const
Function RunHotKey() Runs an action associated with a hotkey (if there is one available).
ACTION_MANAGER(TOOL_MANAGER *aToolManager)
Constructor.
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
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
int processHotKey(TOOL_ACTION *aAction)
Resolves a reference to legacy hot key settings to a particular hot key.
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:73
HOTKEY_LIST m_actionHotKeys
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 EDA_HOTKEY is a class to handle hot key commands.
Definition: hotkeys_basic.h:59
Class TOOL_BASE.
Definition: tool_base.h:68
Class TOOL_ACTION.
Definition: tool_action.h:46
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
Some functions to handle hotkeys in KiCad.
int getDefaultHotKey()
Returns the hot key assigned in the object definition.
Definition: tool_action.h:186
#define GR_KB_CTRL
Definition: common.h:64
#define mod(a, n)
Definition: greymap.cpp:24