KiCad PCB EDA Suite
action_manager.h
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) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef ACTION_MANAGER_H_
28 #define ACTION_MANAGER_H_
29 
30 #include <list>
31 #include <map>
32 #include <string>
33 #include <set>
34 
36 
37 class TOOL_BASE;
38 class TOOL_MANAGER;
39 class TOOL_ACTION;
40 
49 {
51  {
52  checkCondition = SELECTION_CONDITIONS::ShowNever; // Never check by default
53  enableCondition = SELECTION_CONDITIONS::ShowAlways; // Always enable by default
54  showCondition = SELECTION_CONDITIONS::ShowAlways; // Always show by default
55  }
56 
58  {
59  checkCondition = aCondition;
60  return *this;
61  }
62 
64  {
65  enableCondition = aCondition;
66  return *this;
67  }
68 
70  {
71  showCondition = aCondition;
72  return *this;
73  }
74 
78 };
79 
86 {
87 public:
91  ACTION_MANAGER( TOOL_MANAGER* aToolManager );
92 
97 
104  void RegisterAction( TOOL_ACTION* aAction );
105 
109  static int MakeActionId( const std::string& aActionName );
110 
114  const std::map<std::string, TOOL_ACTION*>& GetActions() const;
115 
122  TOOL_ACTION* FindAction( const std::string& aActionName ) const;
123 
130  bool RunHotKey( int aHotKey ) const;
131 
137  int GetHotKey( const TOOL_ACTION& aAction ) const;
138 
142  void UpdateHotKeys( bool aFullUpdate );
143 
151  static std::list<TOOL_ACTION*>& GetActionList()
152  {
153  static std::list<TOOL_ACTION*> actionList;
154 
155  return actionList;
156  }
157 
165  void SetConditions( const TOOL_ACTION& aAction, const ACTION_CONDITIONS& aConditions );
166 
173  const ACTION_CONDITIONS* GetCondition( const TOOL_ACTION& aAction ) const;
174 
175 private:
176  // Resolve a hotkey by applying legacy and current settings over the action's
177  // default hotkey.
178  int processHotKey( TOOL_ACTION* aAction, std::map<std::string, int> aLegacyMap,
179  std::map<std::string, int> aHotKeyMap );
180 
183 
185  std::map<std::string, TOOL_ACTION*> m_actionNameIndex;
186 
188  typedef std::map<int, std::list<TOOL_ACTION*> > HOTKEY_LIST;
190 
192  std::map<int, int> m_hotkeys;
193 
196  std::map<int, ACTION_CONDITIONS> m_uiConditions;
197 };
198 
199 #endif /* ACTION_MANAGER_H_ */
SELECTION_CONDITION showCondition
Returns true if the UI control should be shown.
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
std::map< int, int > m_hotkeys
const std::map< std::string, TOOL_ACTION * > & GetActions() const
Get a list of currently-registered actions mapped by their name.
static int MakeActionId(const std::string &aActionName)
Generate an unique ID from for an action with given name.
SELECTION_CONDITION enableCondition
Returns true if the UI control should be enabled.
SELECTION_CONDITION checkCondition
Returns true if the UI control should be checked.
ACTION_CONDITIONS & Show(const SELECTION_CONDITION &aCondition)
void UpdateHotKeys(bool aFullUpdate)
Optionally read the hotkey config files and then rebuilds the internal hotkey maps.
std::map< int, std::list< TOOL_ACTION * > > HOTKEY_LIST
~ACTION_MANAGER()
Unregister every registered action.
Manage TOOL_ACTION objects.
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
TOOL_ACTION * FindAction(const std::string &aActionName) const
Find an action with a given name (if there is one available).
ACTION_CONDITIONS & Enable(const SELECTION_CONDITION &aCondition)
bool RunHotKey(int aHotKey) const
Run an action associated with a hotkey (if there is one available).
Master controller class:
Definition: tool_manager.h:54
int GetHotKey(const TOOL_ACTION &aAction) const
Return the hot key associated with a given action or 0 if there is none.
ACTION_MANAGER(TOOL_MANAGER *aToolManager)
std::function< bool(const SELECTION &)> SELECTION_CONDITION
< Functor type that checks a specific condition for selected items.
std::map< std::string, TOOL_ACTION * > m_actionNameIndex
Map for indexing actions by their hotkeys.
HOTKEY_LIST m_actionHotKeys
Quick action<->hot key lookup.
void SetConditions(const TOOL_ACTION &aAction, const ACTION_CONDITIONS &aConditions)
Set the conditions the UI elements for activating a specific tool action should use for determining t...
TOOL_MANAGER * m_toolMgr
Map for indexing actions by their names.
void RegisterAction(TOOL_ACTION *aAction)
Add a tool action to the manager and sets it up.
std::map< int, ACTION_CONDITIONS > m_uiConditions
Map the command ID that wx uses for the action to the UI conditions for the menu/toolbar items.
int processHotKey(TOOL_ACTION *aAction, std::map< std::string, int > aLegacyMap, std::map< std::string, int > aHotKeyMap)
Tool manager needed to run actions.
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
Base abstract interface for all kinds of tools.
Definition: tool_base.h:65
Represent a single user action.
Definition: tool_action.h:67
static bool ShowNever(const SELECTION &aSelection)
Always returns false.
const ACTION_CONDITIONS * GetCondition(const TOOL_ACTION &aAction) const
Get the conditions to use for a specific tool action.
ACTION_CONDITIONS & Check(const SELECTION_CONDITION &aCondition)