KiCad PCB EDA Suite
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 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <tool/tool_action.h>
26 #include <tool/action_manager.h>
27 
28 #include <algorithm>
29 #include <hotkeys_basic.h>
30 
31 TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope,
32  int aDefaultHotKey, const std::string& aLegacyHotKeyName,
33  const wxString& aLabel, const wxString& aTooltip,
34  const BITMAP_OPAQUE* aIcon, TOOL_ACTION_FLAGS aFlags, void* aParam ) :
35  m_name( aName ),
36  m_scope( aScope ),
37  m_defaultHotKey( aDefaultHotKey ),
38  m_legacyName( aLegacyHotKeyName ),
39  m_label( aLabel ),
40  m_tooltip( aTooltip ),
41  m_icon( aIcon ),
42  m_id( -1 ),
43  m_flags( aFlags ),
44  m_param( aParam )
45 {
46  SetHotKey( aDefaultHotKey );
47  ACTION_MANAGER::GetActionList().push_back( this );
48 }
49 
50 
52  m_scope( AS_GLOBAL ),
53  m_defaultHotKey( 0 ),
54  m_icon( nullptr ),
55  m_id( -1 ),
56  m_flags( AF_NONE ),
57  m_param( nullptr )
58 {
59  SetHotKey( 0 );
60 }
61 
62 
64 {
65  ACTION_MANAGER::GetActionList().remove( this );
66 }
67 
68 
69 wxString TOOL_ACTION::GetLabel() const
70 {
71  return wxGetTranslation( m_label );
72 }
73 
74 
75 wxString TOOL_ACTION::GetMenuItem() const
76 {
77  wxString label = wxGetTranslation( m_label );
78  label.Replace( "&", "&&" );
79  return AddHotkeyName( label, m_hotKey, IS_HOTKEY );
80 }
81 
82 
83 wxString TOOL_ACTION::GetDescription( bool aIncludeHotkey ) const
84 {
85  wxString tooltip = wxGetTranslation( m_tooltip );
86 
87  if( aIncludeHotkey && GetHotKey() )
88  tooltip += wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( GetHotKey() ) );
89 
90  return tooltip;
91 }
92 
93 
94 void TOOL_ACTION::SetHotKey( int aKeycode )
95 {
96  m_hotKey = aKeycode;
97 }
98 
99 
100 std::string TOOL_ACTION::GetToolName() const
101 {
102  int dotCount = std::count( m_name.begin(), m_name.end(), '.' );
103 
104  switch( dotCount )
105  {
106  case 0:
107  assert( false ); // Invalid action name format
108  return "";
109 
110  case 1:
111  return m_name;
112 
113  case 2:
114  return m_name.substr( 0, m_name.rfind( '.' ) );
115 
116  default:
117  assert( false ); // TODO not implemented
118  return "";
119  }
120 }
wxString m_label
Definition: tool_action.h:176
PNG memory record (file in memory).
Definition: bitmap_def.h:29
wxString AddHotkeyName(const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle)
AddHotkeyName.
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_event.h:143
static std::list< TOOL_ACTION * > & GetActionList()
Returns list of TOOL_ACTIONs.
std::string m_name
Name of the action (convention is "app.tool.actionName")
Definition: tool_action.h:169
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_event.h:151
void SetHotKey(int aKeycode)
Definition: tool_action.cpp:94
wxString m_tooltip
Definition: tool_action.h:177
All active tools
Definition: tool_event.h:147
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:83
std::string GetToolName() const
Returns name of the tool associated with the action.
wxString GetLabel() const
Definition: tool_action.cpp:69
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Function KeyNameFromKeyCode return the key name from the key code Only some wxWidgets key values are ...
wxString GetMenuItem() const
Definition: tool_action.cpp:75
int GetHotKey() const
Returns the hotkey keycode which initiates the action.
Definition: tool_action.h:85