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 <bitmaps.h>
30 #include <hotkeys_basic.h>
31 
32 TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope,
33  int aDefaultHotKey, const std::string& aLegacyHotKeyName,
34  const wxString& aLabel, const wxString& aTooltip,
35  BITMAPS aIcon, TOOL_ACTION_FLAGS aFlags, void* aParam ) :
36  m_name( aName ),
37  m_scope( aScope ),
38  m_defaultHotKey( aDefaultHotKey ),
39  m_legacyName( aLegacyHotKeyName ),
40  m_label( aLabel ),
41  m_tooltip( aTooltip ),
42  m_icon( aIcon ),
43  m_id( -1 ),
44  m_flags( aFlags ),
45  m_param( aParam )
46 {
47  SetHotKey( aDefaultHotKey );
48  ACTION_MANAGER::GetActionList().push_back( this );
49 }
50 
51 
53  m_scope( AS_GLOBAL ),
54  m_defaultHotKey( 0 ),
55  m_icon( BITMAPS::INVALID_BITMAP ),
56  m_id( -1 ),
57  m_flags( AF_NONE ),
58  m_param( nullptr )
59 {
60  SetHotKey( 0 );
61 }
62 
63 
65 {
66  ACTION_MANAGER::GetActionList().remove( this );
67 }
68 
69 
70 wxString TOOL_ACTION::GetLabel() const
71 {
72  return wxGetTranslation( m_label );
73 }
74 
75 
76 wxString TOOL_ACTION::GetMenuItem() const
77 {
78  wxString label = wxGetTranslation( m_label );
79  label.Replace( "&", "&&" );
80  return AddHotkeyName( label, m_hotKey, IS_HOTKEY );
81 }
82 
83 
84 wxString TOOL_ACTION::GetDescription( bool aIncludeHotkey ) const
85 {
86  wxString tooltip = wxGetTranslation( m_tooltip );
87 
88  if( aIncludeHotkey && GetHotKey() )
89  tooltip += wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( GetHotKey() ) );
90 
91  return tooltip;
92 }
93 
94 
95 void TOOL_ACTION::SetHotKey( int aKeycode )
96 {
97  m_hotKey = aKeycode;
98 }
99 
100 
101 std::string TOOL_ACTION::GetToolName() const
102 {
103  int dotCount = std::count( m_name.begin(), m_name.end(), '.' );
104 
105  switch( dotCount )
106  {
107  case 0:
108  assert( false ); // Invalid action name format
109  return "";
110 
111  case 1:
112  return m_name;
113 
114  case 2:
115  return m_name.substr( 0, m_name.rfind( '.' ) );
116 
117  default:
118  assert( false ); // TODO not implemented
119  return "";
120  }
121 }
wxString m_label
Definition: tool_action.h:190
wxString AddHotkeyName(const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle)
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_event.h:147
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
std::string m_name
Definition: tool_action.h:183
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_event.h:155
void SetHotKey(int aKeycode)
Definition: tool_action.cpp:95
wxString m_tooltip
Definition: tool_action.h:191
Global action (toolbar/main menu event, global shortcut)
Definition: tool_event.h:151
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:84
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
std::string GetToolName() const
Return name of the tool associated with the action.
wxString GetLabel() const
Definition: tool_action.cpp:70
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
wxString GetMenuItem() const
Definition: tool_action.cpp:76
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:93