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 bool aReadOnly ) :
69 RESETTABLE_PANEL( aWindow, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
70 m_frame( aFrame ),
71 m_readOnly( aReadOnly ),
73{
74 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
75 wxBoxSizer* bMargins = new wxBoxSizer( wxVERTICAL );
76
77 m_filterSearch = CreateTextFilterBox( this, _( "Type filter text" ) );
78 bMargins->Add( m_filterSearch, 0, wxEXPAND | wxTOP | wxRIGHT, 5 );
79
81 bMargins->Add( m_hotkeyListCtrl, 1, wxEXPAND | wxTOP | wxRIGHT, 5 );
82
83 if( !m_readOnly )
84 installButtons( bMargins );
85
86 mainSizer->Add( bMargins, 1, wxEXPAND, 0 );
87
88#ifdef __WXGTK__
89
90 // It appears that this may have been fixed in wxWidgets 3.2.3.
91#if wxVERSION_NUMBER < 3203
92
93 // Work around a bug that clips the text vertically in the wxSearchCtrl on GTK
94 m_filterSearch->SetMinSize(
95 wxSize( m_filterSearch->GetSize().x, int( m_filterSearch->GetSize().y * 1.6 ) ) );
96#endif
97
98#endif
99
100 SetSizer( mainSizer );
101 Layout();
102 mainSizer->Fit( this );
103
104 // Connect Events
105 m_filterSearch->Bind( wxEVT_COMMAND_TEXT_UPDATED, &PANEL_HOTKEYS_EDITOR::OnFilterSearch, this );
106}
107
108
110{
111 m_filterSearch->Unbind( wxEVT_COMMAND_TEXT_UPDATED, &PANEL_HOTKEYS_EDITOR::OnFilterSearch,
112 this );
113}
114
115
117{
118 m_hotkeyListCtrl->ResetAllHotkeys( true );
119}
120
121
123{
124 BUTTON_ROW_PANEL::BTN_DEF_LIST l_btn_defs = {
125 {
126 wxID_RESET,
127 _( "Undo All Changes" ),
128 _( "Undo all changes made so far in this dialog" ),
129 [this]( wxCommandEvent& )
130 {
131 m_hotkeyListCtrl->ResetAllHotkeys( false );
132 }
133 },
134 {
135 wxID_ANY,
136 _( "Import Hotkeys..." ),
137 _( "Import hotkey definitions from an external file, replacing the current values" ),
138 [this]( wxCommandEvent& )
139 {
141 }
142 }
143 };
144
145 if( ADVANCED_CFG::GetCfg().m_HotkeysDumper )
146 {
147 // Add hotkeys dumper (does not need translation, it's a dev tool only)
148 l_btn_defs.push_back( {
149 wxID_ANY, wxT( "Dump Hotkeys" ), wxEmptyString,
150 [this]( wxCommandEvent& )
151 {
152 dumpHotkeys();
153 }
154 } );
155 }
156
157 const BUTTON_ROW_PANEL::BTN_DEF_LIST r_btn_defs = {
158 };
159
160 auto btnPanel = std::make_unique<BUTTON_ROW_PANEL>( this, l_btn_defs, r_btn_defs );
161
162 aSizer->Add( btnPanel.release(), 0, wxEXPAND | wxALL, KIUI::GetStdMargin() );
163}
164
165
167{
169
170 if( !m_hotkeyListCtrl->TransferDataToControl() )
171 return false;
172
173 return true;
174}
175
176
178{
179 if( m_readOnly )
180 return true;
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.
PANEL_HOTKEYS_EDITOR(EDA_BASE_FRAME *aFrame, wxWindow *aWindow, bool aReadOnly)
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.
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.