diff --git a/pcbnew/footprint_libraries_utils.cpp b/pcbnew/footprint_libraries_utils.cpp index aa0b8a7fff..5d37f84ff2 100644 --- a/pcbnew/footprint_libraries_utils.cpp +++ b/pcbnew/footprint_libraries_utils.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include "footprint_viewer_frame.h" @@ -375,16 +376,25 @@ wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary( const wxString& aLibName, // because the legacy format cannot handle current features. // The footprint library is actually a directory + FP_LIB_TABLE* table = selectLibTable(); + + if( table == nullptr ) + { + return wxEmptyString; + } + wxString initialPath = aProposedName.IsEmpty() ? Prj().GetProjectPath() : aProposedName; wxFileName fn; bool doAdd = false; + bool isGlobal = ( table == &GFootprintTable ); if( aLibName.IsEmpty() ) { fn = initialPath; - if( !LibraryFileBrowser( false, fn, - KiCadFootprintLibPathWildcard(), KiCadFootprintLibPathExtension ) ) + if( !LibraryFileBrowser( false, fn, KiCadFootprintLibPathWildcard(), + KiCadFootprintLibPathExtension, false, isGlobal, + PATHS::GetDefaultUserFootprintsPath() ) ) { return wxEmptyString; } @@ -455,21 +465,77 @@ wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary( const wxString& aLibName, } if( doAdd ) - AddLibrary( libPath ); + AddLibrary( libPath, table ); return libPath; } -bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename ) +FP_LIB_TABLE* PCB_BASE_EDIT_FRAME::selectLibTable( bool aOptional ) { + // If no project is loaded, always work with the global table + if( Prj().IsNullProject() ) + { + FP_LIB_TABLE* ret = &GFootprintTable; + + if( aOptional ) + { + wxMessageDialog dlg( this, _( "Add the library to the global library table?" ), + _( "Add To Global Library Table" ), wxYES_NO ); + + if( dlg.ShowModal() != wxID_OK ) + ret = nullptr; + } + + return ret; + } + + wxArrayString libTableNames; + libTableNames.Add( _( "Global" ) ); + libTableNames.Add( _( "Project" ) ); + + wxSingleChoiceDialog dlg( this, _( "Choose the Library Table to add the library to:" ), + _( "Add To Library Table" ), libTableNames ); + + if( aOptional ) + { + dlg.FindWindow( wxID_CANCEL )->SetLabel( _( "Skip" ) ); + dlg.FindWindow( wxID_OK )->SetLabel( _( "Add" ) ); + } + + if( dlg.ShowModal() != wxID_OK ) + return nullptr; + + switch( dlg.GetSelection() ) + { + case 0: return &GFootprintTable; + case 1: return Prj().PcbFootprintLibs(); + default: return nullptr; + } +} + + +bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename, FP_LIB_TABLE* aTable ) +{ + if( aTable == nullptr ) + { + aTable = selectLibTable(); + + if( aTable == nullptr ) + { + return wxEmptyString; + } + } + + bool isGlobal = ( aTable == &GFootprintTable ); + wxFileName fn( aFilename ); if( aFilename.IsEmpty() ) { - if( !LibraryFileBrowser( true, fn, - KiCadFootprintLibPathWildcard(), KiCadFootprintLibPathExtension, - true ) ) + if( !LibraryFileBrowser( true, fn, KiCadFootprintLibPathWildcard(), + KiCadFootprintLibPathExtension, true, isGlobal, + PATHS::GetDefaultUserFootprintsPath() ) ) { return false; } @@ -481,28 +547,6 @@ bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename ) if( libName.IsEmpty() ) return false; - bool saveInGlobalTable = false; - bool saveInProjectTable = false; - - if( Prj().IsNullProject() ) - { - saveInGlobalTable = true; - } - else - { - wxArrayString libTableNames; - - libTableNames.Add( _( "Global" ) ); - libTableNames.Add( _( "Project" ) ); - - switch( SelectSingleOption( this, _( "Select Library Table" ), - _( "Choose the Library Table to add the library to:" ), libTableNames ) ) - { - case 0: saveInGlobalTable = true; break; - case 1: saveInProjectTable = true; break; - default: return false; - } - } wxString type = IO_MGR::ShowType( IO_MGR::GuessPluginTypeFromLibPath( libPath ) ); @@ -514,16 +558,15 @@ bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename ) try { - if( saveInGlobalTable ) + auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString ); + aTable->InsertRow( row ); + + if( isGlobal ) { - auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString ); - GFootprintTable.InsertRow( row ); GFootprintTable.Save( FP_LIB_TABLE::GetGlobalTableFileName() ); } - else if( saveInProjectTable ) + else { - auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString ); - Prj().PcbFootprintLibs()->InsertRow( row ); Prj().PcbFootprintLibs()->Save( Prj().FootprintLibTblName() ); } } diff --git a/pcbnew/pcb_base_edit_frame.h b/pcbnew/pcb_base_edit_frame.h index af92b9c805..f43b607a91 100644 --- a/pcbnew/pcb_base_edit_frame.h +++ b/pcbnew/pcb_base_edit_frame.h @@ -69,7 +69,7 @@ public: * @param aFileName the library to add; a file open dialog will be displayed if empty. * @return true if successfully added. */ - bool AddLibrary(const wxString& aLibName = wxEmptyString); + bool AddLibrary( const wxString& aLibName = wxEmptyString, FP_LIB_TABLE* aTable = nullptr ); /** * Install the corresponding dialog editor for the given item. @@ -224,6 +224,13 @@ protected: /// AUI panel for controlling layer and object visibility and appearance APPEARANCE_CONTROLS* m_appearancePanel; + + /** + * Prompts a user to select global or project library tables + * + * @return Pointer to library table selected or nullptr if none selected/canceled + */ + FP_LIB_TABLE* selectLibTable( bool aOptional = false ); }; #endif