KiCad PCB EDA Suite
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 (C) 1992-2018 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 <panel_hotkeys_editor.h>
25 #include <kiway_player.h>
26 #include <wx/srchctrl.h>
27 #include <wx/panel.h>
28 #include <wx/button.h>
29 #include <wx/sizer.h>
30 #include <hotkeys_basic.h>
32 #include <widgets/ui_common.h>
33 #include <tool/tool_manager.h>
34 #include <tool/tool_action.h>
35 #include <wx/tokenzr.h>
36 #include <gestfich.h>
37 
38 static const wxSize default_dialog_size { 500, 350 };
39 static const wxSize min_dialog_size { -1, 350 };
40 
49 static wxSearchCtrl* CreateTextFilterBox( wxWindow* aParent, const wxString& aDescriptiveText )
50 {
51  auto search_widget = new wxSearchCtrl( aParent, wxID_ANY );
52 
53  search_widget->ShowSearchButton( false );
54  search_widget->ShowCancelButton( true );
55 
56  search_widget->SetDescriptiveText( aDescriptiveText);
57 
58  return search_widget;
59 }
60 
61 
63  bool aReadOnly ) :
64  wxPanel( aWindow, wxID_ANY, wxDefaultPosition, default_dialog_size ),
65  m_frame( aFrame ),
66  m_readOnly( aReadOnly ),
67  m_hotkeyStore()
68 {
69  const auto margin = KIUI::GetStdMargin();
70  auto mainSizer = new wxBoxSizer( wxVERTICAL );
71 
72  // Sub-sizer for setting a wider side margin
73  // TODO: Can this be set by the parent widget- doesn't seem to be
74  // this panel's responsibility?
75  const int side_margins = 10; // seems to be hardcoded in wxFB
76  auto bMargins = new wxBoxSizer( wxVERTICAL );
77 
78  auto filterSearch = CreateTextFilterBox( this, _( "Type filter text" ) );
79  bMargins->Add( filterSearch, 0, wxBOTTOM | wxEXPAND | wxTOP, margin );
80 
82  bMargins->Add( m_hotkeyListCtrl, 1, wxALL | wxEXPAND, margin );
83 
84  if( !m_readOnly )
85  installButtons( bMargins );
86 
87  mainSizer->Add( bMargins, 1, wxEXPAND | wxRIGHT | wxLEFT, side_margins );
88 
89  this->SetSizer( mainSizer );
90  this->Layout();
91 
92  // Connect Events
93  filterSearch->Bind( wxEVT_COMMAND_TEXT_UPDATED, &PANEL_HOTKEYS_EDITOR::OnFilterSearch, this );
94 }
95 
96 
98 {
99  m_toolManagers.push_back( aToolMgr );
100 }
101 
102 
104 {
105  const BUTTON_ROW_PANEL::BTN_DEF_LIST l_btn_defs = {
106  {
107  wxID_RESET,
108  _( "Undo All Changes" ),
109  _( "Undo all changes made so far in this dialog" ),
110  [this]( wxCommandEvent& ){
112  }
113  },
114  {
115  wxID_ANY,
116  _( "Restore All to Defaults" ),
117  _( "Set all hotkeys to the built-in KiCad defaults" ),
118  [this]( wxCommandEvent& ){
120  }
121  },
122  {
123  wxID_ANY,
124  _( "Import Hotkeys..." ),
125  _( "Import hotkey definitions from an external file, replacing the current values" ),
126  [this]( wxCommandEvent& ){
127  ImportHotKeys();
128  }
129  }
130  };
131 
132  const BUTTON_ROW_PANEL::BTN_DEF_LIST r_btn_defs = {
133  };
134 
135  auto btnPanel = std::make_unique<BUTTON_ROW_PANEL>( this, l_btn_defs, r_btn_defs );
136 
137  aSizer->Add( btnPanel.release(), 0, wxEXPAND | wxTOP, KIUI::GetStdMargin() );
138 }
139 
140 
142 {
145 }
146 
147 
149 {
151  return false;
152 
153  if( m_readOnly )
154  return true;
155 
156  // save the hotkeys
157  for( TOOL_MANAGER* toolMgr : m_toolManagers )
158  WriteHotKeyConfig( toolMgr->GetActions() );
159 
160  return true;
161 }
162 
163 
164 void PANEL_HOTKEYS_EDITOR::OnFilterSearch( wxCommandEvent& aEvent )
165 {
166  const auto searchStr = aEvent.GetString();
167  m_hotkeyListCtrl->ApplyFilterString( searchStr );
168 }
169 
170 
172 {
173  wxString ext = DEFAULT_HOTKEY_FILENAME_EXT;
174  wxString mask = wxT( "*." ) + ext;
175  wxString filename = EDA_FILE_SELECTOR( _( "Import Hotkeys File:" ), m_frame->GetMruPath(),
176  wxEmptyString, ext, mask, this, wxFD_OPEN, true );
177 
178  if( filename.IsEmpty() )
179  return;
180 
181  std::map<std::string, int> importedHotKeys;
182  ReadHotKeyConfig( filename, importedHotKeys );
183  m_frame->SetMruPath( wxFileName( filename ).GetPath() );
184 
185  // Overlay the imported hotkeys onto the hotkey store
186  for( HOTKEY_SECTION& section: m_hotkeyStore.GetSections() )
187  {
188  for( HOTKEY& hotkey: section.m_HotKeys )
189  {
190  if( importedHotKeys.count( hotkey.m_Actions[ 0 ]->GetName() ) )
191  hotkey.m_EditKeycode = importedHotKeys[ hotkey.m_Actions[ 0 ]->GetName() ];
192  }
193  }
194 
196 }
197 
198 
199 
void OnFilterSearch(wxCommandEvent &aEvent)
Function OnFilterSearch Handle a change in the hoteky filter text.
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.
void SetMruPath(const wxString &aPath)
static const wxSize default_dialog_size
This file is part of the common library TODO brief description.
void ImportHotKeys()
Function ImportHotKeys Puts up a dialog allowing the user to select a hotkeys file and then overlays ...
bool TransferDataToControl()
Method TransferDataToControl Load the hotkey data from the store into the control.
void AddHotKeys(TOOL_MANAGER *aToolMgr)
int GetStdMargin()
Get the standard margin around a widget in the KiCad UI.
Definition: ui_common.cpp:23
std::vector< TOOL_MANAGER * > m_toolManagers
void ReadHotKeyConfig(wxString fileName, std::map< std::string, int > &aHotKeys)
Function ReadotKeyConfig Reads a hotkey config file into a map.
EDA_BASE_FRAME * m_frame
Class TOOL_MANAGER.
Definition: tool_manager.h:50
wxString GetMruPath() const
static const wxSize min_dialog_size
void installButtons(wxSizer *aSizer)
Install the button panel (global reset/default, import/export)
wxString EDA_FILE_SELECTOR(const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition, wxString *aMruPath)
Function EDA_FILE_SELECTOR.
Definition: gestfich.cpp:82
Functions to provide common constants and other functions to assist in making a consistent UI.
void ApplyFilterString(const wxString &aFilterStr)
Method ApplyFilterString Apply a filter string to the hotkey list, selecting which hotkeys to show.
#define _(s)
bool TransferDataFromWindow() override
std::vector< TOOL_ACTION * > m_Actions
Definition: hotkey_store.h:35
bool TransferDataToWindow() override
std::vector< HOTKEY_SECTION > & GetSections()
Get the list of sections managed by this store.
The base frame for deriving all KiCad main window classes.
std::vector< BTN_DEF > BTN_DEF_LIST
A list of BTN_DEFs, used to group buttons into the left/right groups.
PANEL_HOTKEYS_EDITOR(EDA_BASE_FRAME *aFrame, wxWindow *aWindow, bool aReadOnly)
WIDGET_HOTKEY_LIST * m_hotkeyListCtrl
#define DEFAULT_HOTKEY_FILENAME_EXT
Definition: hotkeys_basic.h:30
void Init(std::vector< TOOL_MANAGER * > aToolManagerList, bool aIncludeGestures)
int m_EditKeycode
Definition: hotkey_store.h:36
int WriteHotKeyConfig(const std::map< std::string, TOOL_ACTION * > &aActionMap)
Function WriteHotKeyConfig Updates the hotkeys config file with the hotkeys from the given actions ma...
void ResetAllHotkeys(bool aResetToDefault)
Set hotkeys in the control to default or original values.
bool TransferDataFromControl()
Method TransferDataFromControl Save the hotkey data from the control.