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  * @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 #ifndef ACTION_MANAGER_H_
26 #define ACTION_MANAGER_H_
27 
28 #include <list>
29 #include <map>
30 #include <string>
31 #include <set>
32 
34 
35 class TOOL_BASE;
36 class TOOL_MANAGER;
37 class TOOL_ACTION;
38 
47 {
51 
53  {
54  checkCondition = SELECTION_CONDITIONS::ShowNever; // Never check by default
55  enableCondition = SELECTION_CONDITIONS::ShowAlways; // Always enable by default
56  showCondition = SELECTION_CONDITIONS::ShowAlways; // Always show by default
57  }
58 
60  {
61  checkCondition = aCondition;
62  return *this;
63  }
64 
66  {
67  enableCondition = aCondition;
68  return *this;
69  }
70 
72  {
73  showCondition = aCondition;
74  return *this;
75  }
76 };
77 
85 {
86 public:
90  ACTION_MANAGER( TOOL_MANAGER* aToolManager );
91 
96 
103  void RegisterAction( TOOL_ACTION* aAction );
104 
108  static int MakeActionId( const std::string& aActionName );
109 
113  const std::map<std::string, TOOL_ACTION*>& GetActions();
114 
121  TOOL_ACTION* FindAction( const std::string& aActionName ) const;
122 
129  bool RunHotKey( int aHotKey ) const;
130 
136  int GetHotKey( const TOOL_ACTION& aAction ) const;
137 
141  void UpdateHotKeys( bool aFullUpdate );
142 
148  static std::list<TOOL_ACTION*>& GetActionList()
149  {
150  static std::list<TOOL_ACTION*> actionList;
151 
152  return actionList;
153  }
154 
162  void SetConditions( const TOOL_ACTION& aAction, const ACTION_CONDITIONS& aConditions );
163 
170  const ACTION_CONDITIONS* GetCondition( const TOOL_ACTION& aAction ) const;
171 
172 private:
173  // Resolves a hotkey by applying legacy and current settings over the action's
174  // default hotkey.
175  int processHotKey( TOOL_ACTION* aAction, std::map<std::string, int> aLegacyMap,
176  std::map<std::string, int> aHotKeyMap );
177 
180 
182  std::map<std::string, TOOL_ACTION*> m_actionNameIndex;
183 
185  typedef std::map<int, std::list<TOOL_ACTION*> > HOTKEY_LIST;
187 
189  std::map<int, int> m_hotkeys;
190 
193  std::map<int, ACTION_CONDITIONS> m_uiConditions;
194 };
195 
196 #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
Quick action<->hot key lookup
static int MakeActionId(const std::string &aActionName)
Generates 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 reads the hotkey config files and then rebuilds the internal hotkey maps.
std::map< int, std::list< TOOL_ACTION * > > HOTKEY_LIST
Map for indexing actions by their hotkeys
~ACTION_MANAGER()
Unregisters every registered action.
ACTION_MANAGER.
static std::list< TOOL_ACTION * > & GetActionList()
Returns list of TOOL_ACTIONs.
const std::map< std::string, TOOL_ACTION * > & GetActions()
Get a list of currently-registered actions mapped by their name.
TOOL_ACTION * FindAction(const std::string &aActionName) const
Finds an action with a given name (if there is one available).
ACTION_CONDITIONS & Enable(const SELECTION_CONDITION &aCondition)
bool RunHotKey(int aHotKey) const
Runs an action associated with a hotkey (if there is one available).
TOOL_MANAGER.
Definition: tool_manager.h:51
int GetHotKey(const TOOL_ACTION &aAction) const
Returns 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 names
HOTKEY_LIST m_actionHotKeys
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
Tool manager needed to run actions
void RegisterAction(TOOL_ACTION *aAction)
Adds 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)
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
TOOL_BASE.
Definition: tool_base.h:67
Represents a single user action.
Definition: tool_action.h:44
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)