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 The 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, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef __TOOL_ACTION_H
25#define __TOOL_ACTION_H
26
27#include <bitset>
28#include <cassert>
29#include <optional>
30#include <string>
31#include <string_view>
32
33#include <ki_any.h>
34#include <wx/string.h>
35
36class TOOL_EVENT;
37
38enum class BITMAPS : unsigned int;
39
47
55
57enum class TOOLBAR_STATE
58{
59 HIDDEN = 0,
60 TOGGLE = 1,
61 CANCEL = 2,
62
64};
65
66// The length of the TOOLBAR_STATE enum as a size_t (used for bitset creation)
67const size_t gToolbarStateNumber = static_cast<size_t>( TOOLBAR_STATE::ENUM_LENGTH );
68
69typedef std::bitset<gToolbarStateNumber> TOOLBAR_STATE_FLAGS;
70
75{
76public:
77 TOOL_ACTION_GROUP( std::string aName ) :
78 m_name( aName )
79 {
80 // Assign a unique group ID to each group
81 static int groupIDs = 0;
82 m_groupID = ++groupIDs;
83 };
84
86 {
87 // Ensure a copy of a group is exactly the same as this one to get
88 // proper comparisons
89 m_name = aOther.GetName();
90 m_groupID = aOther.GetGroupID();
91 }
92
93 int GetGroupID() const { return m_groupID; }
94 const std::string& GetName() const { return m_name; }
95
96 bool operator==( const TOOL_ACTION_GROUP& aOther ) const
97 {
98 return m_groupID == aOther.m_groupID;
99 }
100
101private:
103 std::string m_name;
104};
105
114{
115public:
116 TOOL_ACTION_ARGS() = default;
117
123 TOOL_ACTION_ARGS& Name( const std::string_view& aName )
124 {
125 m_name = aName;
126 return *this;
127 }
128
129 TOOL_ACTION_ARGS& FriendlyName( const std::string_view& aName )
130 {
131 m_friendlyName = aName;
132 return *this;
133 }
134
139 {
140 m_scope = aScope;
141 return *this;
142 }
143
147 TOOL_ACTION_ARGS& DefaultHotkey( int aDefaultHotkey )
148 {
149 m_defaultHotKey = aDefaultHotkey;
150 return *this;
151 }
152
156 TOOL_ACTION_ARGS& DefaultHotkeyAlt( int aDefaultHotkeyAlt )
157 {
158 m_defaultHotKeyAlt = aDefaultHotkeyAlt;
159 return *this;
160 }
161
167 TOOL_ACTION_ARGS& LegacyHotkeyName( const std::string_view& aLegacyName )
168 {
169 m_legacyName = aLegacyName;
170 return *this;
171 }
172
176 TOOL_ACTION_ARGS& MenuText( const std::string_view& aMenuText )
177 {
178 m_menuText = aMenuText;
179 return *this;
180 }
181
185 TOOL_ACTION_ARGS& Tooltip( const std::string_view& aTooltip )
186 {
187 m_tooltip = aTooltip;
188 return *this;
189 }
190
194 TOOL_ACTION_ARGS& Description( const std::string_view& aDescription )
195 {
196 m_description = aDescription;
197 return *this;
198 }
199
204 {
205 m_icon = aIcon;
206 return *this;
207 }
208
213 {
214 m_flags = aFlags;
215 return *this;
216 }
217
221 template<typename T>
223 {
224 m_param = aParam;
225 return *this;
226 }
227
231 TOOL_ACTION_ARGS& UIId( int aUIId )
232 {
233 m_uiid = aUIId;
234 return *this;
235 }
236
238 {
239 m_group = aGroup;
240 return *this;
241 }
242
244 {
246 m_toolbarState.value().set( static_cast<size_t>( aState ) );
247 return *this;
248 }
249
250 TOOL_ACTION_ARGS& ToolbarState( std::initializer_list<TOOLBAR_STATE> aState )
251 {
253
254 for( auto flag : aState )
255 m_toolbarState.value().set( static_cast<size_t>( flag ) );
256
257 return *this;
258 }
259
260protected:
261 // Let the TOOL_ACTION constructor have direct access to the members here
262 friend class TOOL_ACTION;
263
264 std::optional<std::string_view> m_name;
265 std::optional<std::string_view> m_friendlyName;
266 std::optional<TOOL_ACTION_SCOPE> m_scope;
267 std::optional<TOOL_ACTION_FLAGS> m_flags;
268
269 std::optional<int> m_uiid;
270
271 std::optional<int> m_defaultHotKey;
272 std::optional<int> m_defaultHotKeyAlt;
273 std::optional<std::string_view> m_legacyName;
274
275 std::optional<std::string_view> m_menuText;
276 std::optional<std::string_view> m_tooltip;
277 std::optional<std::string_view> m_description;
278
279 std::optional<BITMAPS> m_icon;
280
281 std::optional<TOOL_ACTION_GROUP> m_group;
282
283 std::optional<TOOLBAR_STATE_FLAGS> m_toolbarState;
284
286};
287
300{
301public:
302 TOOL_ACTION( const TOOL_ACTION_ARGS& aArgs );
303 TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT,
304 int aDefaultHotKey = 0, const std::string& aLegacyHotKeyName = "",
305 const wxString& aMenuText = wxEmptyString,
306 const wxString& aTooltip = wxEmptyString,
307 BITMAPS aIcon = static_cast<BITMAPS>( 0 ), TOOL_ACTION_FLAGS aFlags = AF_NONE );
308
309 ~TOOL_ACTION();
310
311 // TOOL_ACTIONS are singletons; don't be copying them around....
312 TOOL_ACTION( const TOOL_ACTION& ) = delete;
313 TOOL_ACTION& operator= ( const TOOL_ACTION& ) = delete;
314
315 bool operator==( const TOOL_ACTION& aRhs ) const
316 {
317 return m_id == aRhs.m_id;
318 }
319
320 bool operator!=( const TOOL_ACTION& aRhs ) const
321 {
322 return m_id != aRhs.m_id;
323 }
324
333 const std::string& GetName() const { return m_name; }
334
338 int GetDefaultHotKey() const { return m_defaultHotKey; }
340
344 int GetHotKey() const { return m_hotKey; }
345 int GetHotKeyAlt() const { return m_hotKeyAlt; }
346 void SetHotKey( int aKeycode, int aKeycodeAlt = 0 );
347
354 bool IsHotKeyUserBound( int aMatchedHotKey ) const;
355
363 int GetId() const { return m_id; }
364
368 bool HasCustomUIId() const { return m_uiid.has_value(); }
369
378 int GetUIId() const { return m_uiid.value_or( m_id + ACTION_BASE_UI_ID ); }
379
383 static int GetBaseUIId() { return ACTION_BASE_UI_ID; }
384
389 TOOL_EVENT MakeEvent() const;
390
394 wxString GetMenuLabel() const;
395 wxString GetMenuItem() const;
396 wxString GetTooltip( bool aIncludeHotkey = true ) const;
397 wxString GetButtonTooltip() const;
398 wxString GetDescription() const;
399
403 wxString GetFriendlyName() const;
404
406
410 template<typename T>
411 T GetParam() const
412 {
413 wxASSERT_MSG( m_param.has_value(),
414 "Attempted to get a parameter from an action with no parameter." );
415
416 T param;
417
418 try
419 {
420 param = ki::any_cast<T>( m_param );
421 }
422 catch( const ki::bad_any_cast& e )
423 {
424 wxASSERT_MSG( false,
425 wxString::Format( "Requested parameter type %s from action with "
426 "parameter type %s.",
427 typeid(T).name(), m_param.type().name() ) );
428 }
429
430 return param;
431 }
432
433 const std::optional<TOOL_ACTION_GROUP> GetActionGroup() const { return m_group; }
434
440 std::string GetToolName() const;
441
445 bool IsActivation() const
446 {
447 return m_flags & AF_ACTIVATE;
448 }
449
453 bool IsNotification() const
454 {
455 return m_flags & AF_NOTIFY;
456 }
457
464 {
465 // The value of 0 corresponds to BITMAPS::INVALID_BITMAP, but is just
466 // used here to make it so we don't need to include the full list in this
467 // header.
468 return m_icon.value_or( static_cast<BITMAPS>( 0 ) );
469 }
470
477 bool CheckToolbarState( TOOLBAR_STATE aState ) const
478 {
479 return m_toolbarState[static_cast<std::size_t>( aState )];
480 }
481
482
483protected:
484 TOOL_ACTION();
485
486 friend class ACTION_MANAGER;
487
489 static constexpr int ACTION_BASE_UI_ID = 20000;
490
492 std::string m_name;
494
495 std::optional<TOOL_ACTION_GROUP> m_group;
496
497 const int m_defaultHotKey;
500
503 const std::string m_legacyName;
504
505 wxString m_friendlyName;
506 std::optional<wxString> m_menuLabel;
507 wxString m_tooltip;
508 std::optional<wxString> m_description;
509
510 std::optional<BITMAPS> m_icon;
511
512 int m_id;
513 std::optional<int> m_uiid;
514
516
519};
520
521#endif
const char * name
BITMAPS
A list of all bitmap identifiers.
@ HIDDEN
Inactive layers are hidden.
Build up the properties of a TOOL_ACTION in an incremental manner that is static-construction safe.
TOOL_ACTION_ARGS & Parameter(T aParam)
Custom parameter to pass information to the tool.
TOOL_ACTION_ARGS & ToolbarState(std::initializer_list< TOOLBAR_STATE > aState)
TOOL_ACTION_ARGS & DefaultHotkeyAlt(int aDefaultHotkeyAlt)
The default alternate hotkey to assign to the action.
std::optional< std::string_view > m_description
TOOL_ACTION_ARGS & UIId(int aUIId)
The ID number to use for the action when interacting with any UI elements.
std::optional< int > m_defaultHotKey
TOOL_ACTION_ARGS & Flags(TOOL_ACTION_FLAGS aFlags)
Flags describing the type of the action.
std::optional< std::string_view > m_friendlyName
TOOL_ACTION_ARGS & Scope(TOOL_ACTION_SCOPE aScope)
The scope of the action.
std::optional< int > m_uiid
TOOL_ACTION_ARGS & FriendlyName(const std::string_view &aName)
TOOL_ACTION_ARGS & Icon(BITMAPS aIcon)
The bitmap to use as the icon for the action in toolbars and menus.
std::optional< TOOL_ACTION_GROUP > m_group
friend class TOOL_ACTION
TOOL_ACTION_ARGS & ToolbarState(TOOLBAR_STATE aState)
std::optional< TOOL_ACTION_SCOPE > m_scope
TOOL_ACTION_ARGS & Name(const std::string_view &aName)
The name of the action, the convention is "app.tool.actionName".
std::optional< std::string_view > m_tooltip
TOOL_ACTION_ARGS & Group(const TOOL_ACTION_GROUP &aGroup)
TOOL_ACTION_ARGS & DefaultHotkey(int aDefaultHotkey)
The default hotkey to assign to the action.
TOOL_ACTION_ARGS & MenuText(const std::string_view &aMenuText)
The string to use when displaying the action in a menu.
std::optional< std::string_view > m_name
std::optional< TOOL_ACTION_FLAGS > m_flags
TOOL_ACTION_ARGS & Description(const std::string_view &aDescription)
The description of the action.
std::optional< std::string_view > m_legacyName
TOOL_ACTION_ARGS & LegacyHotkeyName(const std::string_view &aLegacyName)
The legacy hotkey name from the old system.
std::optional< BITMAPS > m_icon
TOOL_ACTION_ARGS()=default
std::optional< TOOLBAR_STATE_FLAGS > m_toolbarState
TOOL_ACTION_ARGS & Tooltip(const std::string_view &aTooltip)
The string to use as a tooltip for the action in menus and toolbars.
std::optional< int > m_defaultHotKeyAlt
std::optional< std::string_view > m_menuText
Define a group that can be used to group actions (and their events) of similar operations.
Definition tool_action.h:75
bool operator==(const TOOL_ACTION_GROUP &aOther) const
Definition tool_action.h:96
TOOL_ACTION_GROUP(std::string aName)
Definition tool_action.h:77
const std::string & GetName() const
Definition tool_action.h:94
std::string m_name
TOOL_ACTION_GROUP(const TOOL_ACTION_GROUP &aOther)
Definition tool_action.h:85
int GetGroupID() const
Definition tool_action.h:93
int m_id
Unique ID for maps. Assigned by ACTION_MANAGER.
bool IsHotKeyUserBound(int aMatchedHotKey) const
Return true if the matched hotkey slot (primary or alternate) holds a value different from its compil...
bool HasCustomUIId() const
Return true if this action has a custom UI ID set.
std::optional< wxString > m_description
Description of the action.
TOOL_ACTION(const TOOL_ACTION_ARGS &aArgs)
TOOL_ACTION & operator=(const TOOL_ACTION &)=delete
static int GetBaseUIId()
Get the base value used to offset the user interface IDs for the actions.
wxString GetMenuLabel() const
Return the translated label for the action.
wxString GetTooltip(bool aIncludeHotkey=true) const
std::optional< wxString > m_menuLabel
Menu label.
BITMAPS GetIcon() const
Return an icon associated with the action.
TOOL_ACTION_SCOPE m_scope
bool operator!=(const TOOL_ACTION &aRhs) const
bool IsActivation() const
Return true if the action is intended to activate a tool.
bool operator==(const TOOL_ACTION &aRhs) const
TOOL_ACTION_FLAGS m_flags
void SetHotKey(int aKeycode, int aKeycodeAlt=0)
int GetDefaultHotKeyAlt() const
TOOL_ACTION_SCOPE GetScope() const
std::string GetToolName() const
Return name of the tool associated with the action.
std::optional< int > m_uiid
ID to use when interacting with the UI (if empty, generate one).
wxString m_friendlyName
User-friendly name.
int GetId() const
Return the unique id of the TOOL_ACTION object.
bool IsNotification() const
Return true if the action is a notification.
const std::optional< TOOL_ACTION_GROUP > GetActionGroup() const
const std::string & GetName() const
Return name of the action.
std::optional< BITMAPS > m_icon
Icon for the menu entry.
wxString m_tooltip
User facing tooltip help text.
int m_hotKeyAlt
The alternate hotkey (post-user-settings-application).
TOOL_ACTION(const TOOL_ACTION &)=delete
TOOLBAR_STATE_FLAGS m_toolbarState
Toolbar state behavior for the action.
wxString GetDescription() const
bool CheckToolbarState(TOOLBAR_STATE aState) const
Check if a specific toolbar state is required for this action.
int GetHotKeyAlt() const
wxString GetMenuItem() const
T GetParam() const
Return a non-standard parameter assigned to the action.
int GetHotKey() const
Return the hotkey keycode which initiates the action.
ki::any m_param
Generic parameter.
const int m_defaultHotKey
Default hot key.
int m_hotKey
The current hotkey (post-user-settings-application).
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
static constexpr int ACTION_BASE_UI_ID
Base ID to use inside the user interface system to offset the action IDs.
int GetDefaultHotKey() const
Return the default hotkey (if any) for the action.
const std::string m_legacyName
Name for reading legacy hotkey settings.
std::string m_name
Name of the action (convention is "app.tool.actionName")
wxString GetFriendlyName() const
Return the translated user-friendly name of the action.
friend class ACTION_MANAGER
wxString GetButtonTooltip() const
std::optional< TOOL_ACTION_GROUP > m_group
Optional group for the action to belong to.
int GetUIId() const
Get the unique ID for this action in the user interface system.
const int m_defaultHotKeyAlt
Default hot key alternate.
Generic, UI-independent tool event.
Definition tool_event.h:167
A type-safe container of any type.
Definition ki_any.h:92
Exception class thrown by a failed any_cast.
Definition ki_any.h:80
An implementation of std::any_cast, which uses type_info::hash_code to check validity of cast types.
ValueType any_cast(const any &any)
Access the contained object.
Definition ki_any.h:445
TOOLBAR_STATE
Flags that control how actions appear and interact on the toolbar.
Definition tool_action.h:58
@ TOGGLE
Action is a toggle button on the toolbar.
Definition tool_action.h:60
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition tool_action.h:42
@ AS_GLOBAL
Global action (toolbar/main menu event, global shortcut)
Definition tool_action.h:45
@ AS_ACTIVE
All active tools.
Definition tool_action.h:44
@ AS_CONTEXT
Action belongs to a particular tool (i.e. a part of a pop-up menu)
Definition tool_action.h:43
std::bitset< gToolbarStateNumber > TOOLBAR_STATE_FLAGS
Definition tool_action.h:69
const size_t gToolbarStateNumber
Definition tool_action.h:67
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition tool_action.h:50
@ AF_ACTIVATE
Action activates a tool.
Definition tool_action.h:52
@ AF_NOTIFY
Action is a notification (it is by default passed to all tools)
Definition tool_action.h:53
@ AF_NONE
Definition tool_action.h:51