KiCad PCB EDA Suite
hotkey_store.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) 1992-2019 Kicad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <hotkey_store.h>
25 #include <eda_base_frame.h>
26 #include <tool/tool_manager.h>
27 #include <tool/action_manager.h>
28 #include <tool/tool_event.h>
29 #include <tool/tool_action.h>
30 
31 
33 {
34 public:
35  GESTURE_PSEUDO_ACTION( const wxString& aLabel, int aHotKey )
36  {
37  m_label = aLabel;
38  m_hotKey = aHotKey;
39  }
40 };
41 
43  new GESTURE_PSEUDO_ACTION( _( "Pan Left/Right" ), MD_CTRL + PSEUDO_WXK_WHEEL ),
44  new GESTURE_PSEUDO_ACTION( _( "Pan Up/Down" ), MD_SHIFT + PSEUDO_WXK_WHEEL ),
45  new GESTURE_PSEUDO_ACTION( _( "Finish Drawing" ), PSEUDO_WXK_DBLCLICK ),
46 #ifdef __WXOSX_MAC__
47  new GESTURE_PSEUDO_ACTION( _( "Show Clarify Selection Menu" ), MD_ALT + PSEUDO_WXK_CLICK ),
48  new GESTURE_PSEUDO_ACTION( _( "Add to Selection" ), MD_SHIFT + PSEUDO_WXK_CLICK ),
49  new GESTURE_PSEUDO_ACTION( _( "Toggle Selection State" ), MD_CTRL + PSEUDO_WXK_CLICK ),
50  new GESTURE_PSEUDO_ACTION( _( "Remove from Selection" ), MD_SHIFT + MD_CTRL + PSEUDO_WXK_CLICK ),
51 #else
52  new GESTURE_PSEUDO_ACTION( _( "Show Clarify Selection Menu" ), MD_CTRL + PSEUDO_WXK_CLICK ),
53  new GESTURE_PSEUDO_ACTION( _( "Add to Selection" ), MD_SHIFT + PSEUDO_WXK_CLICK ),
54  new GESTURE_PSEUDO_ACTION( _( "Toggle Selection State" ), MD_ALT + PSEUDO_WXK_CLICK ),
55  new GESTURE_PSEUDO_ACTION( _( "Remove from Selection" ), MD_SHIFT + MD_ALT + PSEUDO_WXK_CLICK ),
56 #endif
57  new GESTURE_PSEUDO_ACTION( _( "Ignore Grid Snaps" ), MD_ALT ),
58  new GESTURE_PSEUDO_ACTION( _( "Ignore Other Snaps" ), MD_SHIFT ),
59 };
60 
61 
63 {
64  wxString name( aAction->GetName() );
65  return name.BeforeFirst( '.' );
66 }
67 
68 
70 {
71  std::map<wxString, wxString> s_AppNames = {
72  { wxT( "common" ), _( "Common" ) },
73  { wxT( "kicad" ), _( "Project Manager" ) },
74  { wxT( "eeschema" ), _( "Schematic Editor" ) },
75  { wxT( "pcbnew" ), _( "PCB Editor" ) },
76  { wxT( "plEditor" ), _( "Drawing Sheet Editor" ), },
77  { wxT( "3DViewer" ), _( "3D Viewer" ) }
78  };
79 
80  wxString appName = GetAppName( aAction );
81 
82  if( s_AppNames.count( appName ) )
83  return s_AppNames[ appName ];
84  else
85  return appName;
86 }
87 
88 
90 {
91 }
92 
93 
94 void HOTKEY_STORE::Init( std::vector<TOOL_MANAGER*> aToolManagerList, bool aIncludeGestures )
95 {
96  m_toolManagers = std::move( aToolManagerList );
97 
98  // Collect all action maps into a single master map. This will re-group everything
99  // and collect duplicates together
100  std::map<std::string, HOTKEY> masterMap;
101 
102  for( TOOL_MANAGER* toolMgr : m_toolManagers )
103  {
104  for( const auto& entry : toolMgr->GetActions() )
105  {
106  // Internal actions probably shouldn't be allowed hotkeys
107  if( entry.second->GetLabel().IsEmpty() )
108  continue;
109 
110  HOTKEY& hotkey = masterMap[ entry.first ];
111  hotkey.m_Actions.push_back( entry.second );
112  hotkey.m_EditKeycode = entry.second->GetHotKey();
113  }
114  }
115 
116  wxString currentApp;
117  HOTKEY_SECTION* currentSection = nullptr;
118 
119  // If a previous list was built, ensure this previous list is cleared:
120  m_hk_sections.clear();
121 
122  for( const auto& entry : masterMap )
123  {
124  TOOL_ACTION* entryAction = entry.second.m_Actions[ 0 ];
125  wxString entryApp = GetAppName( entryAction );
126 
127  if( !currentSection || entryApp != currentApp )
128  {
129  m_hk_sections.emplace_back( HOTKEY_SECTION() );
130  currentApp = entryApp;
131  currentSection = &m_hk_sections.back();
132  currentSection->m_SectionName = GetSectionName( entryAction );
133  }
134 
135  currentSection->m_HotKeys.emplace_back( HOTKEY( entry.second ) );
136  }
137 
138  if( aIncludeGestures )
139  {
140  m_hk_sections.emplace_back( HOTKEY_SECTION() );
141  currentSection = &m_hk_sections.back();
142  currentSection->m_SectionName = _( "Gestures" );
143 
144  for( TOOL_ACTION* gesture : g_gesturePseudoActions )
145  currentSection->m_HotKeys.emplace_back( HOTKEY( gesture ) );
146  }
147 }
148 
149 
150 std::vector<HOTKEY_SECTION>& HOTKEY_STORE::GetSections()
151 {
152  return m_hk_sections;
153 }
154 
155 
157 {
158  for( HOTKEY_SECTION& section : m_hk_sections )
159  {
160  for( HOTKEY& hotkey : section.m_HotKeys )
161  {
162  for( TOOL_ACTION* action : hotkey.m_Actions )
163  action->SetHotKey( hotkey.m_EditKeycode );
164  }
165  }
166 }
167 
168 
170 {
171  for( HOTKEY_SECTION& section : m_hk_sections )
172  {
173  for( HOTKEY& hotkey : section.m_HotKeys )
174  hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetDefaultHotKey();
175  }
176 }
177 
178 
180 {
181  for( HOTKEY_SECTION& section : m_hk_sections )
182  {
183  for( HOTKEY& hotkey : section.m_HotKeys )
184  hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetHotKey();
185  }
186 }
187 
188 
189 bool HOTKEY_STORE::CheckKeyConflicts( TOOL_ACTION* aAction, long aKey, HOTKEY** aConflict )
190 {
191  wxString sectionName = GetSectionName( aAction );
192 
193  // Create a fake "TOOL_ACTION" so we can get the section name for "Common" through the API.
194  // Simply declaring a wxString with the value "Common" works, but the goal is to futureproof
195  // the code here as much as possible.
196  TOOL_ACTION commonAction( "common.Control.Fake", AS_GLOBAL, 0, "", "", "" );
197  wxString commonName = GetSectionName( &commonAction );
198 
199  for( HOTKEY_SECTION& section : m_hk_sections )
200  {
201  // We can have the same hotkey in multiple sections (i.e. Kicad programs), but if a hotkey
202  // is in "Common" it can't be in any other section and vice versa.
203 
204  if( !( section.m_SectionName == sectionName || section.m_SectionName == commonName ) )
205  continue;
206 
207  for( HOTKEY& hotkey : section.m_HotKeys )
208  {
209  if( hotkey.m_Actions[0] == aAction )
210  continue;
211 
212  if( hotkey.m_EditKeycode == aKey )
213  {
214  *aConflict = &hotkey;
215  return true;
216  }
217  }
218  }
219 
220  return false;
221 }
wxString m_label
Definition: tool_action.h:190
static GESTURE_PSEUDO_ACTION * g_gesturePseudoActions[]
#define PSEUDO_WXK_WHEEL
Definition: hotkeys_basic.h:52
void SaveAllHotkeys()
Persist all changes to hotkeys in the store to the underlying data structures.
GESTURE_PSEUDO_ACTION(const wxString &aLabel, int aHotKey)
std::vector< HOTKEY > m_HotKeys
Definition: hotkey_store.h:54
Master controller class:
Definition: tool_manager.h:52
HOTKEY_STORE()
Construct a HOTKEY_STORE from a list of hotkey sections.
std::vector< TOOL_MANAGER * > m_toolManagers
Definition: hotkey_store.h:110
static wxString GetAppName(TOOL_ACTION *aAction)
#define PSEUDO_WXK_CLICK
Definition: hotkeys_basic.h:50
void ResetAllHotkeysToDefault()
Reset every hotkey in the store to the default values.
Base window classes and related definitions.
void SetHotKey(int aKeycode)
Definition: tool_action.cpp:95
bool CheckKeyConflicts(TOOL_ACTION *aAction, long aKey, HOTKEY **aConflict)
Check whether the given key conflicts with anything in this store.
Global action (toolbar/main menu event, global shortcut)
Definition: tool_event.h:151
std::vector< TOOL_ACTION * > m_Actions
Definition: hotkey_store.h:36
std::vector< HOTKEY_SECTION > & GetSections()
Get the list of sections managed by this store.
const char * name
Definition: DXF_plotter.cpp:59
#define _(s)
Definition: 3d_actions.cpp:33
std::vector< HOTKEY_SECTION > m_hk_sections
Definition: hotkey_store.h:111
Represent a single user action.
Definition: tool_action.h:49
void ResetAllHotkeysToOriginal()
Resets every hotkey to the original values.
void Init(std::vector< TOOL_MANAGER * > aToolManagerList, bool aIncludeGestures)
#define PSEUDO_WXK_DBLCLICK
Definition: hotkeys_basic.h:51
int m_EditKeycode
Definition: hotkey_store.h:37
wxString m_SectionName
Definition: hotkey_store.h:53
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:83
static wxString GetSectionName(TOOL_ACTION *aAction)