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  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <tool/action_manager.h>
27 #include <tool/tool_manager.h>
28 #include <tool/tool_action.h>
29 #include <draw_frame.h>
30 
31 #include <hotkeys_basic.h>
32 #include <cctype>
33 
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 }
48 
49 
51 {
52  while( !m_actionNameIndex.empty() )
53  {
54  TOOL_ACTION* action = m_actionNameIndex.begin()->second;
55  UnregisterAction( action );
56  delete action;
57  }
58 }
59 
60 
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 }
72 
73 
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  std::list<TOOL_ACTION*>::iterator action = std::find( actions.begin(), actions.end(), aAction );
83 
84  if( action != actions.end() )
85  actions.erase( action );
86  else
87  wxASSERT( false );
88  }
89 }
90 
91 
92 int ACTION_MANAGER::MakeActionId( const std::string& aActionName )
93 {
94  static int currentActionId = 1;
95 
96  return currentActionId++;
97 }
98 
99 
100 TOOL_ACTION* ACTION_MANAGER::FindAction( const std::string& aActionName ) const
101 {
102  std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
103 
104  if( it != m_actionNameIndex.end() )
105  return it->second;
106 
107  return NULL;
108 }
109 
110 
111 bool ACTION_MANAGER::RunHotKey( int aHotKey ) const
112 {
113  int key = aHotKey & ~MD_MODIFIER_MASK;
114  int mod = aHotKey & MD_MODIFIER_MASK;
115 
116  if( key >= 'a' && key <= 'z' )
117  key = std::toupper( key );
118 
119  HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
120 
121  // If no luck, try without Shift, to handle keys that require it
122  // e.g. to get ? you need to press Shift+/ without US keyboard layout
123  // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
124  // different combination
125  if( it == m_actionHotKeys.end() )
126  {
127  it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
128 
129  if( it == m_actionHotKeys.end() )
130  return false; // no appropriate action found for the hotkey
131  }
132 
133  const std::list<TOOL_ACTION*>& actions = it->second;
134 
135  // Choose the action that has the highest priority on the active tools stack
136  // If there is none, run the global action associated with the hot key
137  int highestPriority = -1, priority = -1;
138  const TOOL_ACTION* context = NULL; // pointer to context action of the highest priority tool
139  const TOOL_ACTION* global = NULL; // pointer to global action, if there is no context action
140 
141  for( const TOOL_ACTION* action : actions )
142  {
143  if( action->GetScope() == AS_GLOBAL )
144  {
145  // Store the global action for the hot key in case there was no possible
146  // context actions to run
147  wxASSERT( global == NULL ); // there should be only one global action per hot key
148  global = action;
149  continue;
150  }
151 
152  TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
153 
154  if( tool )
155  {
156  // Choose the action that goes to the tool with highest priority
157  // (i.e. is on the top of active tools stack)
158  priority = m_toolMgr->GetPriority( tool->GetId() );
159 
160  if( priority >= 0 && priority > highestPriority )
161  {
162  highestPriority = priority;
163  context = action;
164  }
165  }
166  }
167 
168  if( context )
169  {
170  m_toolMgr->RunAction( *context, true );
171  return true;
172  }
173  else if( global )
174  {
175  m_toolMgr->RunAction( *global, true );
176  return true;
177  }
178 
179  return false;
180 }
181 
182 
183 int ACTION_MANAGER::GetHotKey( const TOOL_ACTION& aAction ) const
184 {
185  std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );
186 
187  if( it == m_hotkeys.end() )
188  return 0;
189 
190  return it->second;
191 }
192 
193 
195 {
196  m_actionHotKeys.clear();
197  m_hotkeys.clear();
198 
199  for( const auto& actionName : m_actionNameIndex )
200  {
201  TOOL_ACTION* action = actionName.second;
202  int hotkey = processHotKey( action );
203 
204  if( hotkey <= 0 )
205  continue;
206 
207  // Second hotkey takes priority as defaults are loaded first and updates
208  // are loaded after
209  if( action->GetScope() == AS_GLOBAL && m_actionHotKeys.count( hotkey ) )
210  {
211  for( auto it = m_actionHotKeys[hotkey].begin();
212  it != m_actionHotKeys[hotkey].end(); )
213  {
214  if( (*it)->GetScope() == AS_GLOBAL )
215  it = m_actionHotKeys[hotkey].erase( it );
216  else
217  it++;
218  }
219  }
220 
221  m_actionHotKeys[hotkey].push_back( action );
222  m_hotkeys[action->GetId()] = hotkey;
223 
224  }
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 }
#define GR_KB_ALT
std::map< int, int > m_hotkeys
Quick action<->hot key lookup
int GetPriority(int aToolId) const
Returns priority of a given tool.
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's Hot...
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
#define GR_KB_CTRL
~ACTION_MANAGER()
Destructor.
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
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
TOOL_ACTION * FindAction(const std::string &aActionName) const
Function FindAction() Finds an action with a given name (if there is one available).
bool RunHotKey(int aHotKey) const
Function RunHotKey() Runs an action associated with a hotkey (if there is one available).
Class TOOL_MANAGER.
Definition: tool_manager.h:49
TOOL_ACTION_SCOPE GetScope() const
Definition: tool_action.h:134
int GetHotKey(const TOOL_ACTION &aAction) const
Function GetHotKey() Returns the hot key associated with a given action or 0 if there is none.
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
ACTION_MANAGER(TOOL_MANAGER *aToolManager)
Constructor.
wxWindow * GetEditFrame() const
Definition: tool_manager.h:267
#define GR_KB_SHIFT
All active tools
Definition: tool_event.h:144
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.
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.
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
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
Some functions to handle hotkeys in KiCad.
int getDefaultHotKey()
Returns the hot key assigned in the object definition.
Definition: tool_action.h:186
#define mod(a, n)
Definition: greymap.cpp:24
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:73