KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 CERN
5 * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
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 <any>
32#include <cassert>
33#include <optional>
34#include <string>
35#include <string_view>
36
37#include <wx/string.h>
38
39class TOOL_EVENT;
40
41enum class BITMAPS : unsigned int;
42
45{
49};
50
53{
56 AF_NOTIFY = 2
57};
58
63{
64public:
65 TOOL_ACTION_GROUP( std::string aName ) :
66 m_name( aName )
67 {
68 // Assign a unique group ID to each group
69 static int groupIDs = 0;
70 m_groupID = ++groupIDs;
71 };
72
74 {
75 // Ensure a copy of a group is exactly the same as this one to get
76 // proper comparisons
77 m_name = aOther.GetName();
78 m_groupID = aOther.GetGroupID();
79 }
80
81 int GetGroupID() const { return m_groupID; }
82 const std::string& GetName() const { return m_name; }
83
84 bool operator==( const TOOL_ACTION_GROUP& aOther ) const
85 {
86 return m_groupID == aOther.m_groupID;
87 }
88
89private:
91 std::string m_name;
92};
93
102{
103public:
104 TOOL_ACTION_ARGS() = default;
105
111 TOOL_ACTION_ARGS& Name( const std::string_view& aName )
112 {
113 m_name = aName;
114 return *this;
115 }
116
117 TOOL_ACTION_ARGS& FriendlyName( const std::string_view& aName )
118 {
119 m_friendlyName = aName;
120 return *this;
121 }
122
127 {
128 m_scope = aScope;
129 return *this;
130 }
131
135 TOOL_ACTION_ARGS& DefaultHotkey( int aDefaultHotkey )
136 {
137 m_defaultHotKey = aDefaultHotkey;
138 return *this;
139 }
140
144 TOOL_ACTION_ARGS& DefaultHotkeyAlt( int aDefaultHotkeyAlt )
145 {
146 m_defaultHotKeyAlt = aDefaultHotkeyAlt;
147 return *this;
148 }
149
155 TOOL_ACTION_ARGS& LegacyHotkeyName( const std::string_view& aLegacyName )
156 {
157 m_legacyName = aLegacyName;
158 return *this;
159 }
160
164 TOOL_ACTION_ARGS& MenuText( const std::string_view& aMenuText )
165 {
166 m_menuText = aMenuText;
167 return *this;
168 }
169
173 TOOL_ACTION_ARGS& Tooltip( const std::string_view& aTooltip )
174 {
175 m_tooltip = aTooltip;
176 return *this;
177 }
178
182 TOOL_ACTION_ARGS& Description( const std::string_view& aDescription )
183 {
184 m_description = aDescription;
185 return *this;
186 }
187
192 {
193 m_icon = aIcon;
194 return *this;
195 }
196
201 {
202 m_flags = aFlags;
203 return *this;
204 }
205
209 template<typename T>
211 {
212 m_param = aParam;
213 return *this;
214 }
215
219 TOOL_ACTION_ARGS& UIId( int aUIId )
220 {
221 m_uiid = aUIId;
222 return *this;
223 }
224
226 {
227 m_group = aGroup;
228 return *this;
229 }
230
231protected:
232 // Let the TOOL_ACTION constructor have direct access to the members here
233 friend class TOOL_ACTION;
234
235 std::optional<std::string_view> m_name;
236 std::optional<std::string_view> m_friendlyName;
237 std::optional<TOOL_ACTION_SCOPE> m_scope;
238 std::optional<TOOL_ACTION_FLAGS> m_flags;
239
240 std::optional<int> m_uiid;
241
242 std::optional<int> m_defaultHotKey;
243 std::optional<int> m_defaultHotKeyAlt;
244 std::optional<std::string_view> m_legacyName;
245
246 std::optional<std::string_view> m_menuText;
247 std::optional<std::string_view> m_tooltip;
248 std::optional<std::string_view> m_description;
249
250 std::optional<BITMAPS> m_icon;
251
252 std::optional<TOOL_ACTION_GROUP> m_group;
253
254 std::any m_param;
255};
256
269{
270public:
271 TOOL_ACTION( const TOOL_ACTION_ARGS& aArgs );
272 TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT,
273 int aDefaultHotKey = 0, const std::string& aLegacyHotKeyName = "",
274 const wxString& aMenuText = wxEmptyString,
275 const wxString& aTooltip = wxEmptyString,
276 BITMAPS aIcon = static_cast<BITMAPS>( 0 ), TOOL_ACTION_FLAGS aFlags = AF_NONE);
277
278 ~TOOL_ACTION();
279
280 // TOOL_ACTIONS are singletons; don't be copying them around....
281 TOOL_ACTION( const TOOL_ACTION& ) = delete;
282 TOOL_ACTION& operator= ( const TOOL_ACTION& ) = delete;
283
284 bool operator==( const TOOL_ACTION& aRhs ) const
285 {
286 return m_id == aRhs.m_id;
287 }
288
289 bool operator!=( const TOOL_ACTION& aRhs ) const
290 {
291 return m_id != aRhs.m_id;
292 }
293
302 const std::string& GetName() const { return m_name; }
303
307 int GetDefaultHotKey() const { return m_defaultHotKey; }
309
313 int GetHotKey() const { return m_hotKey; }
314 int GetHotKeyAlt() const { return m_hotKeyAlt; }
315 void SetHotKey( int aKeycode, int aKeycodeAlt = 0 );
316
324 int GetId() const { return m_id; }
325
329 bool HasCustomUIId() const { return m_uiid.has_value(); }
330
331 /*
332 * Get the unique ID for this action in the user interface system.
333 *
334 * This can be either set to a specific ID during creation or computed
335 * by offsetting the action ID by @c ACTION_BASE_UI_ID.
336 *
337 * @return The unique ID number for use in the user interface system.
338 */
339 int GetUIId() const { return m_uiid.value_or( m_id + ACTION_BASE_UI_ID ); }
340
341 /*
342 * Get the base value used to offset the user interface IDs for the actions.
343 */
344 static int GetBaseUIId() { return ACTION_BASE_UI_ID; }
345
350 TOOL_EVENT MakeEvent() const;
351
355 wxString GetMenuLabel() const;
356 wxString GetMenuItem() const;
357 wxString GetTooltip( bool aIncludeHotkey = true ) const;
358 wxString GetButtonTooltip() const;
359 wxString GetDescription() const;
360
364 wxString GetFriendlyName() const;
365
367
371 template<typename T>
372 T GetParam() const
373 {
374 wxASSERT_MSG( m_param.has_value(), "Attempted to get a parameter from an action with no parameter." );
375
376 T param;
377
378 try
379 {
380 param = std::any_cast<T>( m_param );
381 }
382 catch( const std::bad_any_cast& e )
383 {
384 wxASSERT_MSG( false,
385 wxString::Format( "Requested parameter type %s from action with parameter type %s.",
386 typeid(T).name(), m_param.type().name() ) );
387 }
388
389 return param;
390 }
391
392 const std::optional<TOOL_ACTION_GROUP> GetActionGroup() const { return m_group; }
393
399 std::string GetToolName() const;
400
404 bool IsActivation() const
405 {
406 return m_flags & AF_ACTIVATE;
407 }
408
412 bool IsNotification() const
413 {
414 return m_flags & AF_NOTIFY;
415 }
416
423 {
424 return m_icon;
425 }
426
427protected:
428 TOOL_ACTION();
429
430 friend class ACTION_MANAGER;
431
433 static constexpr int ACTION_BASE_UI_ID = 20000;
434
436 std::string m_name;
438
439 std::optional<TOOL_ACTION_GROUP> m_group; // Optional group for the action to belong to
440
441 const int m_defaultHotKey; // Default hot key
442 const int m_defaultHotKeyAlt; // Default hot key alternate
443 int m_hotKey; // The current hotkey (post-user-settings-application)
444 int m_hotKeyAlt; // The alternate hotkey (post-user-settings-application)
445 const std::string m_legacyName; // Name for reading legacy hotkey settings
446
447 wxString m_friendlyName; // User-friendly name
448 std::optional<wxString> m_menuLabel; // Menu label
449 wxString m_tooltip; // User-facing tooltip help text
450 std::optional<wxString> m_description; // Description of the action
451
452 BITMAPS m_icon; // Icon for the menu entry
453
454 int m_id; // Unique ID for maps. Assigned by ACTION_MANAGER.
455 std::optional<int> m_uiid; // ID to use when interacting with the UI (if empty, generate one)
456
458 std::any m_param; // Generic parameter
459};
460
461#endif
const char * name
Definition: DXF_plotter.cpp:57
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
Manage TOOL_ACTION objects.
Build up the properties of a TOOL_ACTION in an incremental manner that is static-construction safe.
Definition: tool_action.h:102
TOOL_ACTION_ARGS & Parameter(T aParam)
Custom parameter to pass information to the tool.
Definition: tool_action.h:210
TOOL_ACTION_ARGS & DefaultHotkeyAlt(int aDefaultHotkeyAlt)
The default alternate hotkey to assign to the action.
Definition: tool_action.h:144
std::optional< std::string_view > m_description
Definition: tool_action.h:248
TOOL_ACTION_ARGS & UIId(int aUIId)
The ID number to use for the action when interacting with any UI elements.
Definition: tool_action.h:219
std::optional< int > m_defaultHotKey
Definition: tool_action.h:242
TOOL_ACTION_ARGS & Flags(TOOL_ACTION_FLAGS aFlags)
Flags describing the type of the action.
Definition: tool_action.h:200
std::optional< std::string_view > m_friendlyName
Definition: tool_action.h:236
TOOL_ACTION_ARGS & Scope(TOOL_ACTION_SCOPE aScope)
The scope of the action.
Definition: tool_action.h:126
std::optional< int > m_uiid
Definition: tool_action.h:240
TOOL_ACTION_ARGS & FriendlyName(const std::string_view &aName)
Definition: tool_action.h:117
TOOL_ACTION_ARGS & Icon(BITMAPS aIcon)
The bitmap to use as the icon for the action in toolbars and menus.
Definition: tool_action.h:191
std::optional< TOOL_ACTION_GROUP > m_group
Definition: tool_action.h:252
std::optional< TOOL_ACTION_SCOPE > m_scope
Definition: tool_action.h:237
TOOL_ACTION_ARGS & Name(const std::string_view &aName)
The name of the action, the convention is "app.tool.actionName".
Definition: tool_action.h:111
std::optional< std::string_view > m_tooltip
Definition: tool_action.h:247
TOOL_ACTION_ARGS & Group(const TOOL_ACTION_GROUP &aGroup)
Definition: tool_action.h:225
TOOL_ACTION_ARGS & DefaultHotkey(int aDefaultHotkey)
The default hotkey to assign to the action.
Definition: tool_action.h:135
TOOL_ACTION_ARGS & MenuText(const std::string_view &aMenuText)
The string to use when displaying the action in a menu.
Definition: tool_action.h:164
std::optional< std::string_view > m_name
Definition: tool_action.h:235
std::optional< TOOL_ACTION_FLAGS > m_flags
Definition: tool_action.h:238
TOOL_ACTION_ARGS & Description(const std::string_view &aDescription)
The description of the action.
Definition: tool_action.h:182
std::optional< std::string_view > m_legacyName
Definition: tool_action.h:244
TOOL_ACTION_ARGS & LegacyHotkeyName(const std::string_view &aLegacyName)
The legacy hotkey name from the old system.
Definition: tool_action.h:155
std::optional< BITMAPS > m_icon
Definition: tool_action.h:250
TOOL_ACTION_ARGS()=default
std::any m_param
Definition: tool_action.h:254
TOOL_ACTION_ARGS & Tooltip(const std::string_view &aTooltip)
The string to use as a tooltip for the action in menus and toolbars.
Definition: tool_action.h:173
std::optional< int > m_defaultHotKeyAlt
Definition: tool_action.h:243
std::optional< std::string_view > m_menuText
Definition: tool_action.h:246
Define a group that can be used to group actions (and their events) of similar operations.
Definition: tool_action.h:63
bool operator==(const TOOL_ACTION_GROUP &aOther) const
Definition: tool_action.h:84
TOOL_ACTION_GROUP(std::string aName)
Definition: tool_action.h:65
const std::string & GetName() const
Definition: tool_action.h:82
std::string m_name
Definition: tool_action.h:91
TOOL_ACTION_GROUP(const TOOL_ACTION_GROUP &aOther)
Definition: tool_action.h:73
int GetGroupID() const
Definition: tool_action.h:81
Represent a single user action.
Definition: tool_action.h:269
bool HasCustomUIId() const
Return true if this action has a custom UI ID set.
Definition: tool_action.h:329
std::optional< wxString > m_description
Definition: tool_action.h:450
TOOL_ACTION & operator=(const TOOL_ACTION &)=delete
static int GetBaseUIId()
Definition: tool_action.h:344
wxString GetMenuLabel() const
Return the translated label for the action.
wxString GetTooltip(bool aIncludeHotkey=true) const
std::optional< wxString > m_menuLabel
Definition: tool_action.h:448
BITMAPS GetIcon() const
Return an icon associated with the action.
Definition: tool_action.h:422
TOOL_ACTION_SCOPE m_scope
Definition: tool_action.h:437
bool operator!=(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:289
bool IsActivation() const
Return true if the action is intended to activate a tool.
Definition: tool_action.h:404
bool operator==(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:284
TOOL_ACTION_FLAGS m_flags
Definition: tool_action.h:457
void SetHotKey(int aKeycode, int aKeycodeAlt=0)
BITMAPS m_icon
Definition: tool_action.h:452
int GetDefaultHotKeyAlt() const
Definition: tool_action.h:308
TOOL_ACTION_SCOPE GetScope() const
Definition: tool_action.h:366
std::string GetToolName() const
Return name of the tool associated with the action.
std::optional< int > m_uiid
Definition: tool_action.h:455
wxString m_friendlyName
Definition: tool_action.h:447
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:324
bool IsNotification() const
Return true if the action is a notification.
Definition: tool_action.h:412
const std::optional< TOOL_ACTION_GROUP > GetActionGroup() const
Definition: tool_action.h:392
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:302
wxString m_tooltip
Definition: tool_action.h:449
TOOL_ACTION(const TOOL_ACTION &)=delete
wxString GetDescription() const
int GetHotKeyAlt() const
Definition: tool_action.h:314
wxString GetMenuItem() const
T GetParam() const
Return a non-standard parameter assigned to the action.
Definition: tool_action.h:372
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:313
const int m_defaultHotKey
Definition: tool_action.h:441
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
static constexpr int ACTION_BASE_UI_ID
Name of the action (convention is "app.tool.actionName")
Definition: tool_action.h:433
std::any m_param
Definition: tool_action.h:458
int GetDefaultHotKey() const
Return the default hotkey (if any) for the action.
Definition: tool_action.h:307
const std::string m_legacyName
Definition: tool_action.h:445
std::string m_name
Definition: tool_action.h:436
wxString GetFriendlyName() const
Return the translated user-friendly name of the action.
wxString GetButtonTooltip() const
std::optional< TOOL_ACTION_GROUP > m_group
Definition: tool_action.h:439
int GetUIId() const
Definition: tool_action.h:339
const int m_defaultHotKeyAlt
Definition: tool_action.h:442
Generic, UI-independent tool event.
Definition: tool_event.h:167
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_action.h:45
@ AS_GLOBAL
Global action (toolbar/main menu event, global shortcut)
Definition: tool_action.h:48
@ AS_ACTIVE
All active tools.
Definition: tool_action.h:47
@ AS_CONTEXT
Action belongs to a particular tool (i.e. a part of a pop-up menu)
Definition: tool_action.h:46
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_action.h:53
@ AF_ACTIVATE
Action activates a tool.
Definition: tool_action.h:55
@ AF_NOTIFY
Action is a notification (it is by default passed to all tools)
Definition: tool_action.h:56
@ AF_NONE
Definition: tool_action.h:54