KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_hotkeys_editor.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 The 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 <advanced_config.h>
25#include <gestfich.h>
26#include <hotkeys_basic.h>
27#include <kiway_player.h>
30#include <tool/tool_manager.h>
32#include <widgets/ui_common.h>
33#include <wx/filedlg.h>
34#include <wx/sizer.h>
35#include <wx/srchctrl.h>
36#include <wx/txtstrm.h>
37#include <wx/wfstream.h>
38
39
48static wxSearchCtrl* CreateTextFilterBox( wxWindow* aParent, const wxString& aDescriptiveText )
49{
50 wxSearchCtrl* search_widget = new wxSearchCtrl( aParent, wxID_ANY );
51
52 search_widget->ShowSearchButton( false );
53 search_widget->ShowCancelButton( true );
54
55 search_widget->SetDescriptiveText( aDescriptiveText );
56
57#ifdef __WXGTK__
58 // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
59 // See https://gitlab.com/kicad/code/kicad/-/issues/9019
60 search_widget->SetMinSize( wxSize( -1, aParent->GetTextExtent( wxT( "qb" ) ).y + 10 ) );
61#endif
62
63 return search_widget;
64}
65
66
68 RESETTABLE_PANEL( aWindow, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
69 m_frame( aFrame ),
71{
72 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
73 wxBoxSizer* bMargins = new wxBoxSizer( wxVERTICAL );
74
75 m_filterSearch = CreateTextFilterBox( this, _( "Type filter text" ) );
76 bMargins->Add( m_filterSearch, 0, wxEXPAND | wxTOP | wxRIGHT, 5 );
77
79 bMargins->Add( m_hotkeyListCtrl, 1, wxEXPAND | wxTOP | wxRIGHT, 5 );
80
81 m_bottomSizer = new wxBoxSizer( wxHORIZONTAL );
83
84 bMargins->Add( m_bottomSizer, 0, wxEXPAND, 5 );
85 mainSizer->Add( bMargins, 1, wxEXPAND, 0 );
86
87#ifdef __WXGTK__
88
89 // It appears that this may have been fixed in wxWidgets 3.2.3.
90#if wxVERSION_NUMBER < 3203
91
92 // Work around a bug that clips the text vertically in the wxSearchCtrl on GTK
93 m_filterSearch->SetMinSize(
94 wxSize( m_filterSearch->GetSize().x, int( m_filterSearch->GetSize().y * 1.6 ) ) );
95#endif
96
97#endif
98
99 SetSizer( mainSizer );
100 Layout();
101 mainSizer->Fit( this );
102
103 // Connect Events
104 m_filterSearch->Bind( wxEVT_COMMAND_TEXT_UPDATED, &PANEL_HOTKEYS_EDITOR::OnFilterSearch, this );
105}
106
107
109{
110 m_filterSearch->Unbind( wxEVT_COMMAND_TEXT_UPDATED, &PANEL_HOTKEYS_EDITOR::OnFilterSearch,
111 this );
112}
113
114
116{
117 m_hotkeyListCtrl->ResetAllHotkeys( true );
118}
119
120
122{
123 BUTTON_ROW_PANEL::BTN_DEF_LIST l_btn_defs = {
124 {
125 wxID_RESET,
126 _( "Undo All Changes" ),
127 _( "Undo all changes made so far in this dialog" ),
128 [this]( wxCommandEvent& )
129 {
130 m_hotkeyListCtrl->ResetAllHotkeys( false );
131 }
132 },
133 {
134 wxID_ANY,
135 _( "Import Hotkeys..." ),
136 _( "Import hotkey definitions from an external file, replacing the current values" ),
137 [this]( wxCommandEvent& )
138 {
140 }
141 }
142 };
143
144 if( ADVANCED_CFG::GetCfg().m_HotkeysDumper )
145 {
146 // Add hotkeys dumper (does not need translation, it's a dev tool only)
147 l_btn_defs.push_back( {
148 wxID_ANY, wxT( "Dump Hotkeys" ), wxEmptyString,
149 [this]( wxCommandEvent& )
150 {
151 dumpHotkeys();
152 }
153 } );
154 }
155
156 const BUTTON_ROW_PANEL::BTN_DEF_LIST r_btn_defs = {
157 };
158
159 auto btnPanel = std::make_unique<BUTTON_ROW_PANEL>( this, l_btn_defs, r_btn_defs );
160
161 aSizer->Add( btnPanel.release(), 1, wxEXPAND | wxALL, KIUI::GetStdMargin() );
162}
163
164
166{
167 m_hotkeyStore.Init( m_actions, true );
168
169 if( !m_hotkeyListCtrl->TransferDataToControl() )
170 return false;
171
172 // we may have loaded a query from the saved dialog state, so make sure to run it
173 if( !m_filterSearch->IsEmpty() )
174 m_hotkeyListCtrl->ApplyFilterString( m_filterSearch->GetValue() );
175
176 return true;
177}
178
179
181{
182 if( !m_hotkeyListCtrl->TransferDataFromControl() )
183 return false;
184
186
187 return true;
188}
189
190
191void PANEL_HOTKEYS_EDITOR::OnFilterSearch( wxCommandEvent& aEvent )
192{
193 const auto searchStr = aEvent.GetString();
194 m_hotkeyListCtrl->ApplyFilterString( searchStr );
195}
196
197
199{
200 wxString filename = wxFileSelector( _( "Import Hotkeys File:" ), m_frame->GetMruPath(),
201 wxEmptyString, FILEEXT::HotkeyFileExtension,
202 FILEEXT::HotkeyFileWildcard(), wxFD_OPEN,
203 wxGetTopLevelParent( this ) );
204
205 if( filename.IsEmpty() )
206 return;
207
208 std::map<std::string, std::pair<int, int>> importedHotKeys;
209 ReadHotKeyConfig( filename, importedHotKeys );
210 m_frame->SetMruPath( wxFileName( filename ).GetPath() );
211
212 // Overlay the imported hotkeys onto the hotkey store
213 for( HOTKEY_SECTION& section: m_hotkeyStore.GetSections() )
214 {
215 for( HOTKEY& hotkey: section.m_HotKeys )
216 {
217 if( importedHotKeys.count( hotkey.m_Actions[ 0 ]->GetName() ) )
218 {
219 hotkey.m_EditKeycode = importedHotKeys[ hotkey.m_Actions[ 0 ]->GetName() ].first;
220 hotkey.m_EditKeycodeAlt = importedHotKeys[ hotkey.m_Actions[ 0 ]->GetName() ].second;
221 }
222 }
223 }
224
225 m_hotkeyListCtrl->TransferDataToControl();
226}
227
228
230{
231 wxString filename = wxFileSelector( wxT( "Hotkeys File" ), m_frame->GetMruPath(),
232 wxEmptyString, FILEEXT::TextFileExtension,
234 wxFD_SAVE, wxGetTopLevelParent( this ) );
235
236 if( filename.IsEmpty() )
237 return;
238
239 wxFileName fn( filename );
240
241 wxFFileOutputStream fileStream( fn.GetFullPath(), "w" );
242 wxTextOutputStream stream( fileStream );
243
244 if( !fn.IsDirWritable() || ( fn.Exists() && !fn.IsFileWritable() ) )
245 return;
246
247 for( HOTKEY_SECTION& section : m_hotkeyStore.GetSections() )
248 {
249 stream << wxT( "=== " ) << section.m_SectionName << endl << endl;
250
251 stream << wxT( "[width=\"100%\",options=\"header\",cols=\"20%,15%,65%\"]" ) << endl;
252 stream << wxT( "|===" ) << endl;
253 stream << _( "| Action | Default Hotkey | Description" ) << endl;
254
255 for( HOTKEY& hk : section.m_HotKeys )
256 {
257 stream << wxT( "| " ) << hk.m_Actions[0]->GetFriendlyName() << endl;
258
259 if( hk.m_EditKeycode > 0 )
260 {
261 stream << wxT( " | kbd:[" ) << KeyNameFromKeyCode( hk.m_EditKeycode ) << ']'
262 << endl;
263 }
264 else
265 {
266 stream << wxT( " |" ) << endl;
267 }
268
269 stream << wxT( " | " ) << hk.m_Actions[0]->GetDescription() << endl;
270 }
271
272 stream << wxT( "|===" ) << endl << endl;
273 }
274
275 stream.Flush();
276 fileStream.Close();
277}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
std::vector< BTN_DEF > BTN_DEF_LIST
A list of BTN_DEFs, used to group buttons into the left/right groups.
The base frame for deriving all KiCad main window classes.
void OnFilterSearch(wxCommandEvent &aEvent)
Handle a change in the hotkey filter text.
void ResetPanel() override
Reset the contents of this panel.
std::vector< TOOL_ACTION * > m_actions
void installButtons(wxSizer *aSizer)
Install the button panel (global reset/default, import/export)
WIDGET_HOTKEY_LIST * m_hotkeyListCtrl
bool TransferDataToWindow() override
void ImportHotKeys()
Put up a dialog allowing the user to select a hotkeys file and then overlays those hotkeys onto the c...
void dumpHotkeys()
Dump all actions and their hotkeys to a text file for inclusion in documentation.
PANEL_HOTKEYS_EDITOR(EDA_BASE_FRAME *aFrame, wxWindow *aWindow)
bool TransferDataFromWindow() override
RESETTABLE_PANEL(wxWindow *aParent, wxWindowID aId=wxID_ANY, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxSize(-1,-1), long aStyle=wxTAB_TRAVERSAL, const wxString &aName=wxEmptyString)
#define _(s)
static const std::string TextFileExtension
static const std::string HotkeyFileExtension
static wxString HotkeyFileWildcard()
static wxString TextFileWildcard()
void ReadHotKeyConfig(const wxString &aFileName, std::map< std::string, std::pair< int, int > > &aHotKeys)
Read a hotkey config file into a map.
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
int WriteHotKeyConfig(const std::vector< TOOL_ACTION * > &aActions)
Update the hotkeys config file with the hotkeys from the given actions map.
KICOMMON_API int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition ui_common.cpp:49
static wxSearchCtrl * CreateTextFilterBox(wxWindow *aParent, const wxString &aDescriptiveText)
Helper function to add a filter box to a panel, with some sensible defaults for that purpose.
int m_EditKeycodeAlt
int m_EditKeycode
std::vector< TOOL_ACTION * > m_Actions
Functions to provide common constants and other functions to assist in making a consistent UI.
Definition of file extensions used in Kicad.