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  new GESTURE_PSEUDO_ACTION( _( "Show Clarify Selection Menu" ), MD_ALT + PSEUDO_WXK_CLICK ),
47  new GESTURE_PSEUDO_ACTION( _( "Add to Selection" ), MD_SHIFT + PSEUDO_WXK_CLICK ),
48  new GESTURE_PSEUDO_ACTION( _( "Toggle Selection State" ), MD_CTRL + PSEUDO_WXK_CLICK ),
49  new GESTURE_PSEUDO_ACTION( _( "Remove from Selection" ), MD_SHIFT + MD_CTRL + PSEUDO_WXK_CLICK ),
50  new GESTURE_PSEUDO_ACTION( _( "Ignore Grid Snaps" ), MD_ALT ),
51  new GESTURE_PSEUDO_ACTION( _( "Ignore Other Snaps" ), MD_SHIFT ),
52 };
53 
54 
56 {
57  wxString name( aAction->GetName() );
58  return name.BeforeFirst( '.' );
59 }
60 
61 
63 {
64  std::map<wxString, wxString> s_AppNames = {
65  { wxT( "common" ), _( "Common" ) },
66  { wxT( "kicad" ), _( "Kicad Manager" ) },
67  { wxT( "eeschema" ), _( "Eeschema" ) },
68  { wxT( "pcbnew" ), _( "Pcbnew" ) },
69  { wxT( "plEditor" ), _( "Page Layout Editor" ), },
70  { wxT( "3DViewer" ), _( "3D Viewer" ) }
71  };
72 
73  wxString appName = GetAppName( aAction );
74 
75  if( s_AppNames.count( appName ) )
76  return s_AppNames[ appName ];
77  else
78  return appName;
79 }
80 
81 
83 {
84 }
85 
86 
87 void HOTKEY_STORE::Init( std::vector<TOOL_MANAGER*> aToolManagerList, bool aIncludeGestures )
88 {
89  m_toolManagers = std::move( aToolManagerList );
90 
91  // Collect all action maps into a single master map. This will re-group everything
92  // and collect duplicates together
93  std::map<std::string, HOTKEY> masterMap;
94 
95  for( TOOL_MANAGER* toolMgr : m_toolManagers )
96  {
97  for( const auto& entry : toolMgr->GetActions() )
98  {
99  // Internal actions probably shouldn't be allowed hotkeys
100  if( entry.second->GetLabel().IsEmpty() )
101  continue;
102 
103  HOTKEY& hotkey = masterMap[ entry.first ];
104  hotkey.m_Actions.push_back( entry.second );
105  hotkey.m_EditKeycode = entry.second->GetHotKey();
106  }
107  }
108 
109  wxString currentApp;
110  HOTKEY_SECTION* currentSection = nullptr;
111 
112  // If a previous list was built, ensure this previous list is cleared:
113  m_hk_sections.clear();
114 
115  for( const auto& entry : masterMap )
116  {
117  TOOL_ACTION* entryAction = entry.second.m_Actions[ 0 ];
118  wxString entryApp = GetAppName( entryAction );
119 
120  if( !currentSection || entryApp != currentApp )
121  {
122  m_hk_sections.emplace_back( HOTKEY_SECTION() );
123  currentApp = entryApp;
124  currentSection = &m_hk_sections.back();
125  currentSection->m_SectionName = GetSectionName( entryAction );
126  }
127 
128  currentSection->m_HotKeys.emplace_back( HOTKEY( entry.second ) );
129  }
130 
131  if( aIncludeGestures )
132  {
133  m_hk_sections.emplace_back( HOTKEY_SECTION() );
134  currentSection = &m_hk_sections.back();
135  currentSection->m_SectionName = _( "Gestures" );
136 
137  for( TOOL_ACTION* gesture : g_gesturePseudoActions )
138  currentSection->m_HotKeys.emplace_back( HOTKEY( gesture ) );
139  }
140 }
141 
142 
143 std::vector<HOTKEY_SECTION>& HOTKEY_STORE::GetSections()
144 {
145  return m_hk_sections;
146 }
147 
148 
150 {
151  for( HOTKEY_SECTION& section : m_hk_sections )
152  {
153  for( HOTKEY& hotkey : section.m_HotKeys )
154  {
155  for( TOOL_ACTION* action : hotkey.m_Actions )
156  action->SetHotKey( hotkey.m_EditKeycode );
157  }
158  }
159 }
160 
161 
163 {
164  for( HOTKEY_SECTION& section : m_hk_sections )
165  {
166  for( HOTKEY& hotkey : section.m_HotKeys )
167  hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetDefaultHotKey();
168  }
169 }
170 
171 
173 {
174  for( HOTKEY_SECTION& section : m_hk_sections )
175  {
176  for( HOTKEY& hotkey : section.m_HotKeys )
177  hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetHotKey();
178  }
179 }
180 
181 
182 bool HOTKEY_STORE::CheckKeyConflicts( TOOL_ACTION* aAction, long aKey, HOTKEY** aConflict )
183 {
184  wxString sectionName = GetSectionName( aAction );
185 
186  for( HOTKEY_SECTION& section: m_hk_sections )
187  {
188  if( section.m_SectionName != sectionName )
189  continue;
190 
191  for( HOTKEY& hotkey: section.m_HotKeys )
192  {
193  if( hotkey.m_Actions[ 0 ] == aAction )
194  continue;
195 
196  if( hotkey.m_EditKeycode == aKey )
197  {
198  *aConflict = &hotkey;
199  return true;
200  }
201  }
202  }
203 
204  return false;
205 }
wxString m_label
Definition: tool_action.h:167
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:53
TOOL_MANAGER.
Definition: tool_manager.h:50
HOTKEY_STORE()
Construct a HOTKEY_STORE from a list of hotkey sections.
std::vector< TOOL_MANAGER * > m_toolManagers
Definition: hotkey_store.h:109
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:94
bool CheckKeyConflicts(TOOL_ACTION *aAction, long aKey, HOTKEY **aConflict)
Check whether the given key conflicts with anything in this store.
std::vector< TOOL_ACTION * > m_Actions
Definition: hotkey_store.h:35
std::vector< HOTKEY_SECTION > & GetSections()
Get the list of sections managed by this store.
const char * name
Definition: DXF_plotter.cpp:60
#define _(s)
Definition: 3d_actions.cpp:31
std::vector< HOTKEY_SECTION > m_hk_sections
Definition: hotkey_store.h:110
TOOL_ACTION.
Definition: tool_action.h:46
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:36
wxString m_SectionName
Definition: hotkey_store.h:52
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78
static wxString GetSectionName(TOOL_ACTION *aAction)