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/tool_event.h>
27 #include <tool/action_manager.h>
28 
29 #include <algorithm>
30 #include <bitmaps.h>
31 #include <hotkeys_basic.h>
32 #include <wx/translation.h>
33 
34 TOOL_ACTION::TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope,
35  int aDefaultHotKey, const std::string& aLegacyHotKeyName,
36  const wxString& aLabel, const wxString& aTooltip,
37  BITMAPS aIcon, TOOL_ACTION_FLAGS aFlags, void* aParam ) :
38  m_name( aName ),
39  m_scope( aScope ),
40  m_defaultHotKey( aDefaultHotKey ),
41  m_legacyName( aLegacyHotKeyName ),
42  m_label( aLabel ),
43  m_tooltip( aTooltip ),
44  m_icon( aIcon ),
45  m_id( -1 ),
46  m_flags( aFlags ),
47  m_param( aParam )
48 {
49  SetHotKey( aDefaultHotKey );
50  ACTION_MANAGER::GetActionList().push_back( this );
51 }
52 
53 
55  m_scope( AS_GLOBAL ),
56  m_defaultHotKey( 0 ),
57  m_icon( BITMAPS::INVALID_BITMAP ),
58  m_id( -1 ),
59  m_flags( AF_NONE ),
60  m_param( nullptr )
61 {
62  SetHotKey( 0 );
63 }
64 
65 
67 {
68  ACTION_MANAGER::GetActionList().remove( this );
69 }
70 
71 
73 {
74  if( IsActivation() )
76  else if( IsNotification() )
78  else
80 }
81 
82 
83 wxString TOOL_ACTION::GetLabel() const
84 {
85  return wxGetTranslation( m_label );
86 }
87 
88 
89 wxString TOOL_ACTION::GetMenuItem() const
90 {
91  wxString label = wxGetTranslation( m_label );
92  label.Replace( "&", "&&" );
93  return AddHotkeyName( label, m_hotKey, IS_HOTKEY );
94 }
95 
96 
97 wxString TOOL_ACTION::GetDescription( bool aIncludeHotkey ) const
98 {
99  wxString tooltip = wxGetTranslation( m_tooltip );
100 
101  if( aIncludeHotkey && GetHotKey() )
102  tooltip += wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( GetHotKey() ) );
103 
104  return tooltip;
105 }
106 
107 
108 void TOOL_ACTION::SetHotKey( int aKeycode )
109 {
110  m_hotKey = aKeycode;
111 }
112 
113 
114 std::string TOOL_ACTION::GetToolName() const
115 {
116  int dotCount = std::count( m_name.begin(), m_name.end(), '.' );
117 
118  switch( dotCount )
119  {
120  case 0:
121  assert( false ); // Invalid action name format
122  return "";
123 
124  case 1:
125  return m_name;
126 
127  case 2:
128  return m_name.substr( 0, m_name.rfind( '.' ) );
129 
130  default:
131  assert( false ); // TODO not implemented
132  return "";
133  }
134 }
wxString m_label
Definition: tool_action.h:200
wxString AddHotkeyName(const wxString &aText, int aHotKey, HOTKEY_ACTION_TYPE aStyle)
bool IsActivation() const
Return true if the action is intended to activate a tool.
Definition: tool_action.h:161
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
std::string m_name
Definition: tool_action.h:193
TOOL_ACTION_SCOPE m_scope
Definition: tool_action.h:194
Generic, UI-independent tool event.
Definition: tool_event.h:152
bool IsNotification() const
Return true if the action is a notification.
Definition: tool_action.h:169
void SetHotKey(int aKeycode)
wxString m_tooltip
Definition: tool_action.h:201
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:97
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.
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_action.h:49
wxString GetLabel() const
Definition: tool_action.cpp:83
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
Global action (toolbar/main menu event, global shortcut)
void * m_param
Definition: tool_action.h:207
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
wxString GetMenuItem() const
Definition: tool_action.cpp:89
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:111
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_action.h:41