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 <tool/tool_event.h>
35 
36 enum class BITMAPS : unsigned int;
37 
50 {
51 public:
52  TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT,
53  int aDefaultHotKey = 0, const std::string& aLegacyHotKeyName = "",
54  const wxString& aMenuText = wxEmptyString,
55  const wxString& aTooltip = wxEmptyString,
56  BITMAPS aIcon = static_cast<BITMAPS>( 0 ), TOOL_ACTION_FLAGS aFlags = AF_NONE,
57  void* aParam = nullptr );
58 
59  ~TOOL_ACTION();
60 
61  // TOOL_ACTIONS are singletons; don't be copying them around....
62  TOOL_ACTION( const TOOL_ACTION& ) = delete;
63  TOOL_ACTION& operator= ( const TOOL_ACTION& ) = delete;
64 
65  bool operator==( const TOOL_ACTION& aRhs ) const
66  {
67  return m_id == aRhs.m_id;
68  }
69 
70  bool operator!=( const TOOL_ACTION& aRhs ) const
71  {
72  return m_id != aRhs.m_id;
73  }
74 
83  const std::string& GetName() const { return m_name; }
84 
88  int GetDefaultHotKey() const { return m_defaultHotKey; }
89 
93  int GetHotKey() const { return m_hotKey; }
94  void SetHotKey( int aKeycode );
95 
103  int GetId() const { return m_id; }
104 
105  /*
106  * Get the unique ID for this action in the user interface system.
107  *
108  * This is simply the action ID offset by @c ACTION_BASE_UI_ID.
109  *
110  * @return The unique ID number for use in the user interface system.
111  */
112  int GetUIId() const { return m_id + ACTION_BASE_UI_ID; }
113 
114  /*
115  * Get the base value used to offset the user interface IDs for the actions.
116  */
117  static int GetBaseUIId() { return ACTION_BASE_UI_ID; }
118 
124  {
125  if( IsActivation() )
126  return TOOL_EVENT( TC_COMMAND, TA_ACTIVATE, m_name, m_scope, m_param );
127  else if( IsNotification() )
128  return TOOL_EVENT( TC_MESSAGE, TA_NONE, m_name, m_scope, m_param );
129  else
130  return TOOL_EVENT( TC_COMMAND, TA_ACTION, m_name, m_scope, m_param );
131  }
132 
133  wxString GetLabel() const;
134  wxString GetMenuItem() const;
135  wxString GetDescription( bool aIncludeHotkey = true ) const;
136 
137  TOOL_ACTION_SCOPE GetScope() const { return m_scope; }
138 
139  void* GetParam() const { return m_param; }
140 
146  std::string GetToolName() const;
147 
151  bool IsActivation() const
152  {
153  return m_flags & AF_ACTIVATE;
154  }
155 
159  bool IsNotification() const
160  {
161  return m_flags & AF_NOTIFY;
162  }
163 
169  const BITMAPS GetIcon() const
170  {
171  return m_icon;
172  }
173 
174 protected:
175  TOOL_ACTION();
176 
177  friend class ACTION_MANAGER;
178 
180  static constexpr int ACTION_BASE_UI_ID = 20000;
181 
183  std::string m_name;
185 
186  const int m_defaultHotKey; // Default hot key
187  int m_hotKey; // The current hotkey (post-user-settings-application)
188  const std::string m_legacyName; // Name for reading legacy hotkey settings
189 
190  wxString m_label;
191  wxString m_tooltip;
192  BITMAPS m_icon; // Icon for the menu entry
193 
194  int m_id; // Unique ID for maps. Assigned by ACTION_MANAGER.
195 
197  void* m_param; // Generic parameter
198 };
199 
200 #endif
wxString m_label
Definition: tool_action.h:190
int GetUIId() const
Definition: tool_action.h:112
Action belongs to a particular tool (i.e. a part of a pop-up menu)
Definition: tool_event.h:149
bool operator==(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:65
Action is a notification (it is by default passed to all tools)
Definition: tool_event.h:159
bool IsActivation() const
Return true if the action is intended to activate a tool.
Definition: tool_action.h:151
Manage TOOL_ACTION objects.
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_event.h:147
std::string m_name
Definition: tool_action.h:183
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_event.h:155
static int GetBaseUIId()
Definition: tool_action.h:117
int GetDefaultHotKey() const
Return the default hotkey (if any) for the action.
Definition: tool_action.h:88
Action activates a tool.
Definition: tool_event.h:158
TOOL_ACTION & operator=(const TOOL_ACTION &)=delete
bool operator!=(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:70
const std::string m_legacyName
Definition: tool_action.h:188
TOOL_ACTION_SCOPE GetScope() const
Definition: tool_action.h:137
TOOL_ACTION_SCOPE m_scope
Definition: tool_action.h:184
Generic, UI-independent tool event.
Definition: tool_event.h:173
const BITMAPS GetIcon() const
Return an icon associated with the action.
Definition: tool_action.h:169
bool IsNotification() const
Return true if the action is a notification.
Definition: tool_action.h:159
void SetHotKey(int aKeycode)
Definition: tool_action.cpp:95
BITMAPS m_icon
Definition: tool_action.h:192
void * GetParam() const
Definition: tool_action.h:139
wxString m_tooltip
Definition: tool_action.h:191
static constexpr int ACTION_BASE_UI_ID
Name of the action (convention is "app.tool.actionName")
Definition: tool_action.h:180
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:84
std::string GetToolName() const
Return name of the tool associated with the action.
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:103
wxString GetLabel() const
Definition: tool_action.cpp:70
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
TOOL_ACTION_FLAGS m_flags
Definition: tool_action.h:196
Represent a single user action.
Definition: tool_action.h:49
void * m_param
Definition: tool_action.h:197
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.h:123
wxString GetMenuItem() const
Definition: tool_action.cpp:76
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:93
const int m_defaultHotKey
Definition: tool_action.h:186
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:83