KiCad PCB EDA Suite
tool_action.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-2015 CERN
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  * @author Maciej Suminski <maciej.suminski@cern.ch>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #ifndef __TOOL_ACTION_H
29 #define __TOOL_ACTION_H
30 
31 #include <string>
32 #include <cassert>
33 
34 #include <wx/string.h>
35 
36 class TOOL_EVENT;
37 
38 enum class BITMAPS : unsigned int;
39 
42 {
43  AS_CONTEXT = 1,
44  AS_ACTIVE,
45  AS_GLOBAL
46 };
47 
50 {
51  AF_NONE = 0,
53  AF_NOTIFY = 2
54 };
55 
68 {
69 public:
70  TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT,
71  int aDefaultHotKey = 0, const std::string& aLegacyHotKeyName = "",
72  const wxString& aMenuText = wxEmptyString,
73  const wxString& aTooltip = wxEmptyString,
74  BITMAPS aIcon = static_cast<BITMAPS>( 0 ), TOOL_ACTION_FLAGS aFlags = AF_NONE,
75  void* aParam = nullptr );
76 
77  ~TOOL_ACTION();
78 
79  // TOOL_ACTIONS are singletons; don't be copying them around....
80  TOOL_ACTION( const TOOL_ACTION& ) = delete;
81  TOOL_ACTION& operator= ( const TOOL_ACTION& ) = delete;
82 
83  bool operator==( const TOOL_ACTION& aRhs ) const
84  {
85  return m_id == aRhs.m_id;
86  }
87 
88  bool operator!=( const TOOL_ACTION& aRhs ) const
89  {
90  return m_id != aRhs.m_id;
91  }
92 
101  const std::string& GetName() const { return m_name; }
102 
106  int GetDefaultHotKey() const { return m_defaultHotKey; }
107 
111  int GetHotKey() const { return m_hotKey; }
112  void SetHotKey( int aKeycode );
113 
121  int GetId() const { return m_id; }
122 
123  /*
124  * Get the unique ID for this action in the user interface system.
125  *
126  * This is simply the action ID offset by @c ACTION_BASE_UI_ID.
127  *
128  * @return The unique ID number for use in the user interface system.
129  */
130  int GetUIId() const { return m_id + ACTION_BASE_UI_ID; }
131 
132  /*
133  * Get the base value used to offset the user interface IDs for the actions.
134  */
135  static int GetBaseUIId() { return ACTION_BASE_UI_ID; }
136 
141  TOOL_EVENT MakeEvent() const;
142 
143  wxString GetLabel() const;
144  wxString GetMenuItem() const;
145  wxString GetDescription( bool aIncludeHotkey = true ) const;
146 
147  TOOL_ACTION_SCOPE GetScope() const { return m_scope; }
148 
149  void* GetParam() const { return m_param; }
150 
156  std::string GetToolName() const;
157 
161  bool IsActivation() const
162  {
163  return m_flags & AF_ACTIVATE;
164  }
165 
169  bool IsNotification() const
170  {
171  return m_flags & AF_NOTIFY;
172  }
173 
179  const BITMAPS GetIcon() const
180  {
181  return m_icon;
182  }
183 
184 protected:
185  TOOL_ACTION();
186 
187  friend class ACTION_MANAGER;
188 
190  static constexpr int ACTION_BASE_UI_ID = 20000;
191 
193  std::string m_name;
195 
196  const int m_defaultHotKey; // Default hot key
197  int m_hotKey; // The current hotkey (post-user-settings-application)
198  const std::string m_legacyName; // Name for reading legacy hotkey settings
199 
200  wxString m_label;
201  wxString m_tooltip;
202  BITMAPS m_icon; // Icon for the menu entry
203 
204  int m_id; // Unique ID for maps. Assigned by ACTION_MANAGER.
205 
207  void* m_param; // Generic parameter
208 };
209 
210 #endif
wxString m_label
Definition: tool_action.h:200
int GetUIId() const
Definition: tool_action.h:130
bool operator==(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:83
bool IsActivation() const
Return true if the action is intended to activate a tool.
Definition: tool_action.h:161
Manage TOOL_ACTION objects.
std::string m_name
Definition: tool_action.h:193
static int GetBaseUIId()
Definition: tool_action.h:135
int GetDefaultHotKey() const
Return the default hotkey (if any) for the action.
Definition: tool_action.h:106
TOOL_ACTION & operator=(const TOOL_ACTION &)=delete
bool operator!=(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:88
const std::string m_legacyName
Definition: tool_action.h:198
TOOL_ACTION_SCOPE GetScope() const
Definition: tool_action.h:147
TOOL_ACTION_SCOPE m_scope
Definition: tool_action.h:194
Action activates a tool.
Definition: tool_action.h:52
Generic, UI-independent tool event.
Definition: tool_event.h:152
Action belongs to a particular tool (i.e. a part of a pop-up menu)
const BITMAPS GetIcon() const
Return an icon associated with the action.
Definition: tool_action.h:179
bool IsNotification() const
Return true if the action is a notification.
Definition: tool_action.h:169
void SetHotKey(int aKeycode)
BITMAPS m_icon
Definition: tool_action.h:202
void * GetParam() const
Definition: tool_action.h:149
wxString m_tooltip
Definition: tool_action.h:201
static constexpr int ACTION_BASE_UI_ID
Name of the action (convention is "app.tool.actionName")
Definition: tool_action.h:190
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:97
std::string GetToolName() const
Return name of the tool associated with the action.
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_action.h:49
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:121
wxString GetLabel() const
Definition: tool_action.cpp:83
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
TOOL_ACTION_FLAGS m_flags
Definition: tool_action.h:206
Global action (toolbar/main menu event, global shortcut)
Represent a single user action.
Definition: tool_action.h:67
void * m_param
Definition: tool_action.h:207
Action is a notification (it is by default passed to all tools)
Definition: tool_action.h:53
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
wxString GetMenuItem() const
Definition: tool_action.cpp:89
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:111
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_action.h:41
const int m_defaultHotKey
Definition: tool_action.h:196
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:101