KiCad PCB EDA Suite
Loading...
Searching...
No Matches
tool_action.cpp
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) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <optional>
27#include <tool/tool_action.h>
28#include <tool/tool_event.h>
29#include <tool/action_manager.h>
30
31#include <algorithm>
32#include <bitmaps.h>
33#include <hotkeys_basic.h>
34
35#include <core/wx_stl_compat.h>
36#include <wx/string.h>
37#include <wx/translation.h>
38
39TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope,
40 int aDefaultHotKey, const std::string& aLegacyHotKeyName,
41 const wxString& aLabel, const wxString& aTooltip,
42 BITMAPS aIcon, TOOL_ACTION_FLAGS aFlags ) :
43 m_name( aName ),
44 m_scope( aScope ),
45 m_group( std::nullopt ),
46 m_defaultHotKey( aDefaultHotKey ),
47 m_defaultHotKeyAlt( 0 ),
48 m_legacyName( aLegacyHotKeyName ),
49 m_menuLabel( aLabel ),
50 m_tooltip( aTooltip ),
51 m_icon( aIcon ),
52 m_id( -1 ),
53 m_flags( aFlags )
54{
55 SetHotKey( aDefaultHotKey );
56 ACTION_MANAGER::GetActionList().push_back( this );
57}
58
59
61 m_scope( AS_GLOBAL ),
62 m_group( std::nullopt ),
63 m_defaultHotKey( 0 ),
64 m_defaultHotKeyAlt( 0 ),
65 m_icon( BITMAPS::INVALID_BITMAP ),
66 m_id( -1 ),
67 m_flags( AF_NONE )
68{
69 SetHotKey( 0 );
70}
71
72
74 m_name( aArgs.m_name.value_or( "" ) ),
75 m_scope( aArgs.m_scope.value_or( AS_CONTEXT ) ),
76 m_defaultHotKey( aArgs.m_defaultHotKey.value_or( 0 ) ),
77 m_defaultHotKeyAlt( aArgs.m_defaultHotKeyAlt.value_or( 0 ) ),
78 m_hotKey( aArgs.m_defaultHotKey.value_or( 0 ) ),
79 m_hotKeyAlt( 0 ),
80 m_legacyName( aArgs.m_legacyName.value_or( "" ) ),
81 m_friendlyName( TowxString( aArgs.m_friendlyName.value_or( "" ) ) ),
82 m_tooltip( TowxString( aArgs.m_tooltip.value_or( "" ) ) ),
83 m_icon( aArgs.m_icon.value_or( BITMAPS::INVALID_BITMAP) ),
84 m_id( -1 ),
85 m_uiid( std::nullopt ),
86 m_flags( aArgs.m_flags.value_or( AF_NONE ) )
87{
88 // Action name is the only mandatory part
89 assert( !m_name.empty() );
90
91 if( aArgs.m_menuText.has_value() )
92 m_menuLabel = TowxString( aArgs.m_menuText.value() );
93
94 if( aArgs.m_uiid.has_value() )
95 m_uiid = aArgs.m_uiid.value();
96
97 if( aArgs.m_param.has_value() )
98 m_param = aArgs.m_param;
99
100 if( aArgs.m_description.has_value() )
101 m_description = TowxString( aArgs.m_description.value() );
102
103 if( aArgs.m_group.has_value() )
104 m_group = aArgs.m_group;
105
106 ACTION_MANAGER::GetActionList().push_back( this );
107}
108
109
111{
112 ACTION_MANAGER::GetActionList().remove( this );
113}
114
115
117{
118 TOOL_EVENT evt;
119
120 if( IsActivation() )
122 else if( IsNotification() )
124 else
126
127 if( m_group.has_value() )
128 {
129 evt.SetActionGroup( m_group.value() );
130 }
131
132 if( m_param.has_value() )
133 evt.SetParameter( m_param );
134
135 return evt;
136}
137
138
140{
141 if( m_friendlyName.empty() )
142 return wxEmptyString;
143
144 return wxGetTranslation( m_friendlyName );
145}
146
147
149{
150 if( m_menuLabel.has_value() )
151 return wxGetTranslation( m_menuLabel.value() );
152
153 return GetFriendlyName();
154}
155
156
158{
159 wxString label = GetMenuLabel();
160 label.Replace( wxS( "&" ), wxS( "&&" ) );
161 return AddHotkeyName( label, m_hotKey, IS_HOTKEY );
162}
163
164
166{
167 // If no description provided, use the tooltip without a hotkey
168 if( !m_description.has_value() )
169 return GetTooltip( false );
170
171 return wxGetTranslation( m_description.value() );
172}
173
174
175wxString TOOL_ACTION::GetTooltip( bool aIncludeHotkey ) const
176{
177 wxString tooltip = wxGetTranslation( m_tooltip );
178
179 if( aIncludeHotkey && GetHotKey() )
180 tooltip += wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( GetHotKey() ) );
181
182 return tooltip;
183}
184
185
187{
188 // We don't show button text so use the action name as the first line of the tooltip
189 wxString tooltip = GetFriendlyName();
190
191 if( GetHotKey() )
192 tooltip += wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( GetHotKey() ) );
193
194 if( !GetTooltip( false ).IsEmpty() )
195 tooltip += '\n' + GetTooltip( false );
196
197 return tooltip;
198}
199
200
201void TOOL_ACTION::SetHotKey( int aKeycode, int aKeycodeAlt )
202{
203 m_hotKey = aKeycode;
204 m_hotKeyAlt = aKeycodeAlt;
205}
206
207
208std::string TOOL_ACTION::GetToolName() const
209{
210 int dotCount = std::count( m_name.begin(), m_name.end(), '.' );
211
212 switch( dotCount )
213 {
214 case 0:
215 assert( false ); // Invalid action name format
216 return "";
217
218 case 1:
219 return m_name;
220
221 case 2:
222 return m_name.substr( 0, m_name.rfind( '.' ) );
223
224 default:
225 assert( false ); // TODO not implemented
226 return "";
227 }
228}
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
@ INVALID_BITMAP
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
Build up the properties of a TOOL_ACTION in an incremental manner that is static-construction safe.
Definition: tool_action.h:102
std::optional< std::string_view > m_description
Definition: tool_action.h:248
std::optional< int > m_uiid
Definition: tool_action.h:240
std::optional< TOOL_ACTION_GROUP > m_group
Definition: tool_action.h:252
std::any m_param
Definition: tool_action.h:254
std::optional< std::string_view > m_menuText
Definition: tool_action.h:246
std::optional< wxString > m_description
Definition: tool_action.h:450
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
TOOL_ACTION_SCOPE m_scope
Definition: tool_action.h:437
bool IsActivation() const
Return true if the action is intended to activate a tool.
Definition: tool_action.h:404
void SetHotKey(int aKeycode, int aKeycodeAlt=0)
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
bool IsNotification() const
Return true if the action is a notification.
Definition: tool_action.h:412
wxString m_tooltip
Definition: tool_action.h:449
wxString GetDescription() const
wxString GetMenuItem() const
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:313
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
std::any m_param
Definition: tool_action.h:458
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
Generic, UI-independent tool event.
Definition: tool_event.h:167
void SetActionGroup(const TOOL_ACTION_GROUP &aGroup)
Definition: tool_event.h:530
void SetParameter(T aParam)
Set a non-standard parameter assigned to the event.
Definition: tool_event.h:515
wxString AddHotkeyName(const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle)
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
@ IS_HOTKEY
Definition: hotkeys_basic.h:77
STL namespace.
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_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_NONE
Definition: tool_action.h:54
@ TA_ACTIVATE
Definition: tool_event.h:114
@ TA_ACTION
Definition: tool_event.h:111
@ TA_NONE
Definition: tool_event.h:65
@ TC_COMMAND
Definition: tool_event.h:56
@ TC_MESSAGE
Definition: tool_event.h:57
wxString TowxString(const std::string_view &view)