You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.0 KiB

5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2023 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. #include <advanced_config.h>
  24. #include <gestfich.h>
  25. #include <hotkeys_basic.h>
  26. #include <kiway_player.h>
  27. #include <locale_io.h>
  28. #include <panel_hotkeys_editor.h>
  29. #include <wildcards_and_files_ext.h>
  30. #include <tool/tool_manager.h>
  31. #include <widgets/button_row_panel.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. * Helper function to add a filter box to a panel, with some
  40. * sensible defaults for that purpose.
  41. *
  42. * @param aParent The parent widget/panel
  43. * @param aDescriptiveText The text to show when the box is empty.
  44. * @return A newly constructed filter box - the caller owns it
  45. */
  46. static wxSearchCtrl* CreateTextFilterBox( wxWindow* aParent, const wxString& aDescriptiveText )
  47. {
  48. wxSearchCtrl* search_widget = new wxSearchCtrl( aParent, wxID_ANY );
  49. search_widget->ShowSearchButton( false );
  50. search_widget->ShowCancelButton( true );
  51. search_widget->SetDescriptiveText( aDescriptiveText );
  52. #ifdef __WXGTK__
  53. // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
  54. // See https://gitlab.com/kicad/code/kicad/-/issues/9019
  55. search_widget->SetMinSize( wxSize( -1, aParent->GetTextExtent( wxT( "qb" ) ).y + 10 ) );
  56. #endif
  57. return search_widget;
  58. }
  59. PANEL_HOTKEYS_EDITOR::PANEL_HOTKEYS_EDITOR( EDA_BASE_FRAME* aFrame, wxWindow* aWindow,
  60. bool aReadOnly ) :
  61. RESETTABLE_PANEL( aWindow, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
  62. m_frame( aFrame ),
  63. m_readOnly( aReadOnly ),
  64. m_hotkeyStore()
  65. {
  66. wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
  67. wxBoxSizer* bMargins = new wxBoxSizer( wxVERTICAL );
  68. wxSearchCtrl* filterSearch = CreateTextFilterBox( this, _( "Type filter text" ) );
  69. bMargins->Add( filterSearch, 0, wxEXPAND, 0 );
  70. m_hotkeyListCtrl = new WIDGET_HOTKEY_LIST( this, m_hotkeyStore, m_readOnly );
  71. bMargins->Add( m_hotkeyListCtrl, 1, wxEXPAND, 0 );
  72. if( !m_readOnly )
  73. installButtons( bMargins );
  74. mainSizer->Add( bMargins, 1, wxEXPAND, 0 );
  75. #ifdef __WXGTK__
  76. // Work around a bug that clips the text vertically in the wxSearchCtrl on GTK
  77. filterSearch->SetMinSize( wxSize( filterSearch->GetSize().x,
  78. int( filterSearch->GetSize().y * 1.6 ) ) );
  79. #endif
  80. SetSizer( mainSizer );
  81. Layout();
  82. // Connect Events
  83. filterSearch->Bind( wxEVT_COMMAND_TEXT_UPDATED, &PANEL_HOTKEYS_EDITOR::OnFilterSearch, this );
  84. }
  85. void PANEL_HOTKEYS_EDITOR::ResetPanel()
  86. {
  87. m_hotkeyListCtrl->ResetAllHotkeys( true );
  88. }
  89. void PANEL_HOTKEYS_EDITOR::installButtons( wxSizer* aSizer )
  90. {
  91. BUTTON_ROW_PANEL::BTN_DEF_LIST l_btn_defs = {
  92. {
  93. wxID_RESET,
  94. _( "Undo All Changes" ),
  95. _( "Undo all changes made so far in this dialog" ),
  96. [this]( wxCommandEvent& )
  97. {
  98. m_hotkeyListCtrl->ResetAllHotkeys( false );
  99. }
  100. },
  101. {
  102. wxID_ANY,
  103. _( "Import Hotkeys..." ),
  104. _( "Import hotkey definitions from an external file, replacing the current values" ),
  105. [this]( wxCommandEvent& )
  106. {
  107. ImportHotKeys();
  108. }
  109. }
  110. };
  111. if( ADVANCED_CFG::GetCfg().m_HotkeysDumper )
  112. {
  113. // Add hotkeys dumper (does not need translation, it's a dev tool only)
  114. l_btn_defs.push_back( {
  115. wxID_ANY, wxT( "Dump Hotkeys" ), wxEmptyString,
  116. [this]( wxCommandEvent& )
  117. {
  118. dumpHotkeys();
  119. }
  120. } );
  121. }
  122. const BUTTON_ROW_PANEL::BTN_DEF_LIST r_btn_defs = {
  123. };
  124. auto btnPanel = std::make_unique<BUTTON_ROW_PANEL>( this, l_btn_defs, r_btn_defs );
  125. aSizer->Add( btnPanel.release(), 0, wxEXPAND | wxALL, KIUI::GetStdMargin() * 2 );
  126. }
  127. bool PANEL_HOTKEYS_EDITOR::TransferDataToWindow()
  128. {
  129. m_hotkeyStore.Init( m_actions, m_readOnly );
  130. if( !m_hotkeyListCtrl->TransferDataToControl() )
  131. return false;
  132. return true;
  133. }
  134. bool PANEL_HOTKEYS_EDITOR::TransferDataFromWindow()
  135. {
  136. if( m_readOnly )
  137. return true;
  138. if( !m_hotkeyListCtrl->TransferDataFromControl() )
  139. return false;
  140. WriteHotKeyConfig( m_actions );
  141. return true;
  142. }
  143. void PANEL_HOTKEYS_EDITOR::OnFilterSearch( wxCommandEvent& aEvent )
  144. {
  145. const auto searchStr = aEvent.GetString();
  146. m_hotkeyListCtrl->ApplyFilterString( searchStr );
  147. }
  148. void PANEL_HOTKEYS_EDITOR::ImportHotKeys()
  149. {
  150. wxString filename = wxFileSelector( _( "Import Hotkeys File:" ), m_frame->GetMruPath(),
  151. wxEmptyString, HotkeyFileExtension,
  152. HotkeyFileWildcard(), wxFD_OPEN, this );
  153. if( filename.IsEmpty() )
  154. return;
  155. std::map<std::string, std::pair<int, int>> importedHotKeys;
  156. ReadHotKeyConfig( filename, importedHotKeys );
  157. m_frame->SetMruPath( wxFileName( filename ).GetPath() );
  158. // Overlay the imported hotkeys onto the hotkey store
  159. for( HOTKEY_SECTION& section: m_hotkeyStore.GetSections() )
  160. {
  161. for( HOTKEY& hotkey: section.m_HotKeys )
  162. {
  163. if( importedHotKeys.count( hotkey.m_Actions[ 0 ]->GetName() ) )
  164. {
  165. hotkey.m_EditKeycode = importedHotKeys[ hotkey.m_Actions[ 0 ]->GetName() ].first;
  166. hotkey.m_EditKeycodeAlt = importedHotKeys[ hotkey.m_Actions[ 0 ]->GetName() ].second;
  167. }
  168. }
  169. }
  170. m_hotkeyListCtrl->TransferDataToControl();
  171. }
  172. void PANEL_HOTKEYS_EDITOR::dumpHotkeys()
  173. {
  174. wxString filename = wxFileSelector( wxT( "Hotkeys File" ), m_frame->GetMruPath(),
  175. wxEmptyString, TextFileExtension, TextFileWildcard(),
  176. wxFD_SAVE, this );
  177. if( filename.IsEmpty() )
  178. return;
  179. wxFileName fn( filename );
  180. wxFFileOutputStream fileStream( fn.GetFullPath(), "w" );
  181. wxTextOutputStream stream( fileStream );
  182. if( !fn.IsDirWritable() || ( fn.Exists() && !fn.IsFileWritable() ) )
  183. return;
  184. for( HOTKEY_SECTION& section : m_hotkeyStore.GetSections() )
  185. {
  186. stream << wxT( "=== " ) << section.m_SectionName << endl << endl;
  187. stream << wxT( "[width=\"100%\",options=\"header\",cols=\"20%,15%,65%\"]" ) << endl;
  188. stream << wxT( "|===" ) << endl;
  189. stream << _( "| Action | Default Hotkey | Description" ) << endl;
  190. for( HOTKEY& hk : section.m_HotKeys )
  191. {
  192. stream << wxT( "| " ) << hk.m_Actions[0]->GetLabel() << endl;
  193. if( hk.m_EditKeycode > 0 )
  194. {
  195. stream << wxT( " | kbd:[" ) << KeyNameFromKeyCode( hk.m_EditKeycode ) << ']'
  196. << endl;
  197. }
  198. else
  199. {
  200. stream << wxT( " |" ) << endl;
  201. }
  202. stream << wxT( " | " ) << hk.m_Actions[0]->GetDescription() << endl;
  203. }
  204. stream << wxT( "|===" ) << endl << endl;
  205. }
  206. stream.Flush();
  207. fileStream.Close();
  208. }