Add import of hotkeys and fix bugs in reanding and writing hotkeys.

This commit is contained in:
Jeff Young 2019-06-10 16:58:32 +01:00
parent b0e8443383
commit aeadc768f6
8 changed files with 110 additions and 122 deletions

View File

@ -37,7 +37,7 @@ DIALOG_LIST_HOTKEYS::DIALOG_LIST_HOTKEYS( EDA_BASE_FRAME* aParent, TOOL_MANAGER*
auto main_sizer = new wxBoxSizer( wxVERTICAL ); auto main_sizer = new wxBoxSizer( wxVERTICAL );
m_hk_list = new PANEL_HOTKEYS_EDITOR( this, true ); m_hk_list = new PANEL_HOTKEYS_EDITOR( aParent, this, true );
m_hk_list->AddHotKeys( aToolMgr ); m_hk_list->AddHotKeys( aToolMgr );
main_sizer->Add( m_hk_list, 1, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, margin ); main_sizer->Add( m_hk_list, 1, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, margin );

View File

@ -27,9 +27,13 @@
#include <wx/panel.h> #include <wx/panel.h>
#include <wx/button.h> #include <wx/button.h>
#include <wx/sizer.h> #include <wx/sizer.h>
#include <hotkeys_basic.h>
#include <widgets/button_row_panel.h> #include <widgets/button_row_panel.h>
#include <widgets/ui_common.h> #include <widgets/ui_common.h>
#include <tool/tool_manager.h> #include <tool/tool_manager.h>
#include <tool/tool_action.h>
#include <wx/tokenzr.h>
#include <gestfich.h>
static const wxSize default_dialog_size { 500, 350 }; static const wxSize default_dialog_size { 500, 350 };
static const wxSize min_dialog_size { -1, 350 }; static const wxSize min_dialog_size { -1, 350 };
@ -55,8 +59,10 @@ static wxSearchCtrl* CreateTextFilterBox( wxWindow* aParent, const wxString& aDe
} }
PANEL_HOTKEYS_EDITOR::PANEL_HOTKEYS_EDITOR( wxWindow* aWindow, bool aReadOnly ) : PANEL_HOTKEYS_EDITOR::PANEL_HOTKEYS_EDITOR( EDA_BASE_FRAME* aFrame, wxWindow* aWindow,
bool aReadOnly ) :
wxPanel( aWindow, wxID_ANY, wxDefaultPosition, default_dialog_size ), wxPanel( aWindow, wxID_ANY, wxDefaultPosition, default_dialog_size ),
m_frame( aFrame ),
m_readOnly( aReadOnly ), m_readOnly( aReadOnly ),
m_hotkeyStore() m_hotkeyStore()
{ {
@ -118,8 +124,7 @@ void PANEL_HOTKEYS_EDITOR::installButtons( wxSizer* aSizer )
_( "Import Hotkeys..." ), _( "Import Hotkeys..." ),
_( "Import hotkey definitions from an external file, replacing the current values" ), _( "Import hotkey definitions from an external file, replacing the current values" ),
[this]( wxCommandEvent& ){ [this]( wxCommandEvent& ){
// JEY TODO: implement hotkey import.... ImportHotKeys();
/*m_frame->ImportHotkeyConfigFromFile( m_hotkeys, m_nickname );*/
} }
} }
}; };
@ -158,3 +163,34 @@ void PANEL_HOTKEYS_EDITOR::OnFilterSearch( wxCommandEvent& aEvent )
const auto searchStr = aEvent.GetString(); const auto searchStr = aEvent.GetString();
m_hotkeyListCtrl->ApplyFilterString( searchStr ); m_hotkeyListCtrl->ApplyFilterString( searchStr );
} }
void PANEL_HOTKEYS_EDITOR::ImportHotKeys()
{
wxString ext = DEFAULT_HOTKEY_FILENAME_EXT;
wxString mask = wxT( "*." ) + ext;
wxString filename = EDA_FILE_SELECTOR( _( "Read Hotkeys File:" ), m_frame->GetMruPath(),
wxEmptyString, ext, mask, this, wxFD_OPEN, true );
if( filename.IsEmpty() )
return;
std::map<std::string, int> importedHotKeys;
ReadHotKeyConfig( filename, importedHotKeys );
m_frame->SetMruPath( wxFileName( filename ).GetPath() );
// Overlay the imported hotkeys onto the hotkey store
for( HOTKEY_SECTION& section: m_hotkeyStore.GetSections() )
{
for( HOTKEY& hotkey: section.m_HotKeys )
{
if( importedHotKeys.count( hotkey.m_Parent->GetName() ) )
hotkey.m_EditKeycode = importedHotKeys[ hotkey.m_Parent->GetName() ];
}
}
m_hotkeyListCtrl->TransferDataToControl();
}

View File

@ -518,7 +518,7 @@ void EDA_BASE_FRAME::OnPreferences( wxCommandEvent& event )
book->AddPage( new PANEL_COMMON_SETTINGS( &dlg, book ), _( "Common" ) ); book->AddPage( new PANEL_COMMON_SETTINGS( &dlg, book ), _( "Common" ) );
PANEL_HOTKEYS_EDITOR* hotkeysPanel = new PANEL_HOTKEYS_EDITOR( book, false ); PANEL_HOTKEYS_EDITOR* hotkeysPanel = new PANEL_HOTKEYS_EDITOR( this, book, false );
book->AddPage( hotkeysPanel, _( "Hotkeys" ) ); book->AddPage( hotkeysPanel, _( "Hotkeys" ) );
for( unsigned i = 0; i < KIWAY_PLAYER_COUNT; ++i ) for( unsigned i = 0; i < KIWAY_PLAYER_COUNT; ++i )

View File

@ -55,12 +55,11 @@ wxString g_CommonSectionTag( wxT( "[common]" ) );
* This class allows the real key code changed by user from a key code list * This class allows the real key code changed by user from a key code list
* file. * file.
*/ */
EDA_HOTKEY::EDA_HOTKEY( const wxChar* infomsg, int idcommand, int keycode ) :
EDA_HOTKEY::EDA_HOTKEY( const wxChar* infomsg, int idcommand, int keycode, int idmenuevent ) : m_KeyCode( keycode ),
m_KeyCode( keycode ), m_InfoMsg( infomsg ), m_Idcommand( idcommand ), m_InfoMsg( infomsg ),
m_IdMenuEvent( idmenuevent ) m_Idcommand( idcommand )
{ { }
}
/* class to handle the printable name and the keycode /* class to handle the printable name and the keycode
@ -320,11 +319,7 @@ wxString KeyNameFromCommandId( EDA_HOTKEY** aList, int aCommandId )
/** /**
* Function KeyCodeFromKeyName * Function KeyCodeFromKeyName
* return the key code from its key name * return the key code from its user-friendly key name (ie: "Ctrl+M")
* Only some wxWidgets key values are handled for function key
* @param keyname = wxString key name to find in hotkeyNameList[],
* like F2 or space or an usual (ascii) char.
* @return the key code
*/ */
int KeyCodeFromKeyName( const wxString& keyname ) int KeyCodeFromKeyName( const wxString& keyname )
{ {
@ -400,26 +395,25 @@ void DisplayHotkeyList( EDA_BASE_FRAME* aParent, TOOL_MANAGER* aToolManager )
} }
int WriteHotKeyConfig( std::map<std::string, TOOL_ACTION*> aActionMap ) void ReadHotKeyConfig( wxString fileName, std::map<std::string, int>& aHotKeys )
{
if( fileName.IsEmpty() )
{ {
wxFileName fn( "user" ); wxFileName fn( "user" );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT ); fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
fn.SetPath( GetKicadConfigPath() ); fn.SetPath( GetKicadConfigPath() );
fileName = fn.GetFullPath();
}
if( !wxFile::Exists( fn.GetFullPath() ) ) if( !wxFile::Exists( fileName ) )
return 0; return;
wxFile file( fn.GetFullPath(), wxFile::OpenMode::read ); wxFile file( fileName, wxFile::OpenMode::read );
if( !file.IsOpened() ) // There is a problem to open file if( !file.IsOpened() ) // There is a problem to open file
return 0; return;
// Read entire hotkey set into map
//
wxString input; wxString input;
std::map<wxString, int> hotkeys;
file.ReadAll( &input ); file.ReadAll( &input );
input.Replace( "\r\n", "\n" ); // Convert Windows files to Unix line-ends input.Replace( "\r\n", "\n" ); // Convert Windows files to Unix line-ends
wxStringTokenizer fileTokenizer( input, "\n", wxTOKEN_STRTOK ); wxStringTokenizer fileTokenizer( input, "\n", wxTOKEN_STRTOK );
@ -432,12 +426,24 @@ int WriteHotKeyConfig( std::map<std::string, TOOL_ACTION*> aActionMap )
wxString keyName = lineTokenizer.GetNextToken(); wxString keyName = lineTokenizer.GetNextToken();
if( !cmdName.IsEmpty() ) if( !cmdName.IsEmpty() )
hotkeys[ cmdName ] = KeyCodeFromKeyName( keyName ); aHotKeys[ cmdName.ToStdString() ] = KeyCodeFromKeyName( keyName );
}
} }
file.Close();
// Overlay this app's hotkey definitions onto the map int WriteHotKeyConfig( std::map<std::string, TOOL_ACTION*> aActionMap )
{
std::map<std::string, int> hotkeys;
wxFileName fn( "user" );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
fn.SetPath( GetKicadConfigPath() );
// Read the existing config (all hotkeys)
//
ReadHotKeyConfig( fn.GetFullPath(), hotkeys );
// Overlay the current app's hotkey definitions onto the map
// //
for( const auto& ii : aActionMap ) for( const auto& ii : aActionMap )
{ {
@ -447,13 +453,11 @@ int WriteHotKeyConfig( std::map<std::string, TOOL_ACTION*> aActionMap )
// Write entire hotkey set // Write entire hotkey set
// //
file.Open( fn.GetFullPath(), wxFile::OpenMode::write ); wxFile file( fn.GetFullPath(), wxFile::OpenMode::write );
for( const auto& ii : hotkeys ) for( const auto& ii : hotkeys )
file.Write( wxString::Format( "%s\t%s\n", ii.first, KeyNameFromKeyCode( ii.second ) ) ); file.Write( wxString::Format( "%s\t%s\n", ii.first, KeyNameFromKeyCode( ii.second ) ) );
file.Close();
return 1; return 1;
} }
@ -545,64 +549,3 @@ int ReadLegacyHotkeyConfigFile( const wxString& aFilename, std::map<std::string,
} }
void EDA_BASE_FRAME::ImportHotkeyConfigFromFile( EDA_HOTKEY_CONFIG* aDescList,
const wxString& aDefaultShortname )
{
wxString ext = DEFAULT_HOTKEY_FILENAME_EXT;
wxString mask = wxT( "*." ) + ext;
wxString path = GetMruPath();
wxFileName fn( aDefaultShortname );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
wxString filename = EDA_FILE_SELECTOR( _( "Read Hotkey Configuration File:" ),
path,
fn.GetFullPath(),
ext,
mask,
this,
wxFD_OPEN,
true );
if( filename.IsEmpty() )
return;
// JEY TODO: implement import of new hotkeys file....
//::ReadHotkeyConfigFile( filename, aDescList, false );
//WriteHotKeyConfig( aDescList );
SetMruPath( wxFileName( filename ).GetPath() );
}
void EDA_BASE_FRAME::ExportHotkeyConfigToFile( EDA_HOTKEY_CONFIG* aDescList,
const wxString& aDefaultShortname )
{
wxString ext = DEFAULT_HOTKEY_FILENAME_EXT;
wxString mask = wxT( "*." ) + ext;
#if 0
wxString path = wxPathOnly( Prj().GetProjectFullName() );
#else
wxString path = GetMruPath();
#endif
wxFileName fn( aDefaultShortname );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
wxString filename = EDA_FILE_SELECTOR( _( "Write Hotkey Configuration File:" ),
path,
fn.GetFullPath(),
ext,
mask,
this,
wxFD_SAVE,
true );
if( filename.IsEmpty() )
return;
// JEY TODO: make this whole routine oboslete?
//WriteHotKeyConfig( aDescList, &filename );
SetMruPath( wxFileName( filename ).GetPath() );
}

View File

@ -204,7 +204,7 @@ void ACTION_MANAGER::UpdateHotKeys()
m_hotkeys.clear(); m_hotkeys.clear();
ReadLegacyHotkeyConfig( m_toolMgr->GetEditFrame()->ConfigBaseName(), legacyHotKeyMap ); ReadLegacyHotkeyConfig( m_toolMgr->GetEditFrame()->ConfigBaseName(), legacyHotKeyMap );
// JEY TODO: read user hotkey config... ReadHotKeyConfig( wxEmptyString, userHotKeyMap );
for( const auto& actionName : m_actionNameIndex ) for( const auto& actionName : m_actionNameIndex )
{ {

View File

@ -307,23 +307,13 @@ public:
// Read/Save and Import/export hotkeys config // Read/Save and Import/export hotkeys config
/** /**
* Prompt the user for an old hotkey file to read, and read it. * Prompt the user for a hotkey file to read, and read it.
* *
* @param aDescList = current hotkey list descr. to initialize. * @param aActionMap = current hotkey map (over which the imported hotkeys will be applied)
* @param aDefaultShortname = a default short name (extension not needed) * @param aDefaultShortname = a default short name (extension not needed)
* like eechema, kicad... * like eechema, kicad...
*/ */
void ImportHotkeyConfigFromFile( EDA_HOTKEY_CONFIG* aDescList, void ImportHotkeyConfigFromFile( std::map<std::string, TOOL_ACTION*> aActionMap,
const wxString& aDefaultShortname );
/**
* Prompt the user for an old hotkey file to read, and read it.
*
* @param aDescList = current hotkey list descr. to initialize.
* @param aDefaultShortname = a default short name (extension not needed)
* like eechema, kicad...
*/
void ExportHotkeyConfigToFile( EDA_HOTKEY_CONFIG* aDescList,
const wxString& aDefaultShortname ); const wxString& aDefaultShortname );
/** /**

View File

@ -60,10 +60,9 @@ public:
int m_KeyCode; // Key code (ascii value for ascii keys or wxWidgets code for function key int m_KeyCode; // Key code (ascii value for ascii keys or wxWidgets code for function key
wxString m_InfoMsg; // info message. wxString m_InfoMsg; // info message.
int m_Idcommand; // internal id for the corresponding command (see hotkey_id_command list) int m_Idcommand; // internal id for the corresponding command (see hotkey_id_command list)
int m_IdMenuEvent; // id to call the corresponding event (if any) (see id.h)
public: public:
EDA_HOTKEY( const wxChar* infomsg, int idcommand, int keycode, int idmenuevent = 0 ); EDA_HOTKEY( const wxChar* infomsg, int idcommand, int keycode );
}; };
@ -88,14 +87,17 @@ public:
}; };
/**
* Function KeyCodeFromKeyName
* return the key code from its user-friendly key name (ie: "Ctrl+M")
*/
int KeyCodeFromKeyName( const wxString& keyname );
/** /**
* Function KeyNameFromKeyCode * Function KeyNameFromKeyCode
* return the key name from the key code * return the user-friendly key name (ie: "Ctrl+M") from the key code
* * Only some wxWidgets key values are handled for function key ( see
* s_Hotkey_Name_List[] )
* @param aKeycode = key code (ascii value, or wxWidgets value for function keys) * @param aKeycode = key code (ascii value, or wxWidgets value for function keys)
* @param aIsFound = a pointer to a bool to return true if found, or false. an be NULL default) * @param aIsFound = a pointer to a bool to return true if found, or false
* @return the key name in a wxString
*/ */
wxString KeyNameFromKeyCode( int aKeycode, bool * aIsFound = nullptr ); wxString KeyNameFromKeyCode( int aKeycode, bool * aIsFound = nullptr );
@ -155,6 +157,14 @@ wxString AddHotkeyName( const wxString& aText,
*/ */
void DisplayHotkeyList( EDA_BASE_FRAME* aFrame, TOOL_MANAGER* aToolMgr ); void DisplayHotkeyList( EDA_BASE_FRAME* aFrame, TOOL_MANAGER* aToolMgr );
/**
* Function ReadotKeyConfig
* Reads a hotkey config file into a map. If aFileName is empty it will read in the defualt
* hotkeys file.
* @param aHotKeys
*/
void ReadHotKeyConfig( wxString aFileName, std::map<std::string, int>& aHotKeys );
/** /**
* Function WriteHotKeyConfig * Function WriteHotKeyConfig
* Updates the hotkeys config file with the hotkeys from the given actions map. * Updates the hotkeys config file with the hotkeys from the given actions map.

View File

@ -40,14 +40,15 @@ class TOOL_MANAGER;
class PANEL_HOTKEYS_EDITOR : public wxPanel class PANEL_HOTKEYS_EDITOR : public wxPanel
{ {
protected: protected:
EDA_BASE_FRAME* m_frame;
bool m_readOnly; bool m_readOnly;
std::vector<TOOL_MANAGER*> m_toolManagers;
std::vector<TOOL_MANAGER*> m_toolManagers;
HOTKEY_STORE m_hotkeyStore; HOTKEY_STORE m_hotkeyStore;
WIDGET_HOTKEY_LIST* m_hotkeyListCtrl; WIDGET_HOTKEY_LIST* m_hotkeyListCtrl;
public: public:
PANEL_HOTKEYS_EDITOR( wxWindow* aWindow, bool aReadOnly ); PANEL_HOTKEYS_EDITOR( EDA_BASE_FRAME* aFrame, wxWindow* aWindow, bool aReadOnly );
void AddHotKeys( TOOL_MANAGER* aToolMgr ); void AddHotKeys( TOOL_MANAGER* aToolMgr );
@ -69,6 +70,14 @@ private:
* @param aEvent: the search event, used to get the search query * @param aEvent: the search event, used to get the search query
*/ */
void OnFilterSearch( wxCommandEvent& aEvent ); void OnFilterSearch( wxCommandEvent& aEvent );
/**
* Function ImportHotKeys
* Puts up a dialog allowing the user to select a hotkeys file and then overlays those
* hotkeys onto the current hotkey store.
*/
void ImportHotKeys();
}; };