Allow caller to specify which library table to add library to.

Fixes https://gitlab.com/kicad/code/kicad/issues/8013
This commit is contained in:
Jeff Young 2021-06-18 11:38:08 +01:00
parent c35cceca62
commit 3072422538
3 changed files with 45 additions and 29 deletions

View File

@ -804,7 +804,7 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
// The footprints are saved in a new .pretty library. // The footprints are saved in a new .pretty library.
// If this library already exists, all previous footprints will be deleted // If this library already exists, all previous footprints will be deleted
std::vector<FOOTPRINT*> loadedFootprints = pi->GetImportedCachedLibraryFootprints(); std::vector<FOOTPRINT*> loadedFootprints = pi->GetImportedCachedLibraryFootprints();
wxString newLibPath = CreateNewLibrary( libNickName ); wxString newLibPath = CreateNewProjectLibrary( libNickName );
// Only create the new library if CreateNewLibrary succeeded (note that this fails if // Only create the new library if CreateNewLibrary succeeded (note that this fails if
// the library already exists and the user aborts after seeing the warning message // the library already exists and the user aborts after seeing the warning message

View File

@ -356,7 +356,7 @@ void FOOTPRINT_EDIT_FRAME::ExportFootprint( FOOTPRINT* aFootprint )
if( fp == nullptr ) if( fp == nullptr )
{ {
wxMessageBox( wxString::Format( _( "Unable to create or write file \"%s\"" ), wxMessageBox( wxString::Format( _( "Unable to create or write file '%s'." ),
dlg.GetPath() ) ); dlg.GetPath() ) );
return; return;
} }
@ -370,29 +370,42 @@ void FOOTPRINT_EDIT_FRAME::ExportFootprint( FOOTPRINT* aFootprint )
return; return;
} }
wxString msg = wxString::Format( _( "Footprint exported to file \"%s\"" ), dlg.GetPath() ); wxString msg = wxString::Format( _( "Footprint exported to file '%s'." ), dlg.GetPath() );
DisplayInfoMessage( this, msg ); DisplayInfoMessage( this, msg );
} }
wxString PCB_BASE_EDIT_FRAME::CreateNewProjectLibrary( const wxString& aLibName,
const wxString& aProposedName )
{
return createNewLibrary( aLibName, aProposedName, Prj().PcbFootprintLibs() );
}
wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary( const wxString& aLibName, wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary( const wxString& aLibName,
const wxString& aProposedName ) const wxString& aProposedName )
{ {
// Kicad cannot write legacy format libraries, only .pretty new format
// because the legacy format cannot handle current features.
// The footprint library is actually a directory
FP_LIB_TABLE* table = selectLibTable(); FP_LIB_TABLE* table = selectLibTable();
if( table == nullptr ) return createNewLibrary( aLibName, aProposedName, table );
{
return wxEmptyString;
} }
wxString PCB_BASE_EDIT_FRAME::createNewLibrary( const wxString& aLibName,
const wxString& aProposedName,
FP_LIB_TABLE* aTable )
{
// Kicad cannot write legacy format libraries, only .pretty new format because the legacy
// format cannot handle current features.
// The footprint library is actually a directory.
if( aTable == nullptr )
return wxEmptyString;
wxString initialPath = aProposedName.IsEmpty() ? Prj().GetProjectPath() : aProposedName; wxString initialPath = aProposedName.IsEmpty() ? Prj().GetProjectPath() : aProposedName;
wxFileName fn; wxFileName fn;
bool doAdd = false; bool doAdd = false;
bool isGlobal = ( table == &GFootprintTable ); bool isGlobal = ( aTable == &GFootprintTable );
if( aLibName.IsEmpty() ) if( aLibName.IsEmpty() )
{ {
@ -438,13 +451,15 @@ wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary( const wxString& aLibName,
exists = true; // no exception was thrown, lib must exist. exists = true; // no exception was thrown, lib must exist.
} }
catch( const IO_ERROR& ) catch( const IO_ERROR& )
{ } {
// best efforts....
}
if( exists ) if( exists )
{ {
if( !writable ) if( !writable )
{ {
wxString msg = wxString::Format( _( "Library \"%s\" is read only." ), libPath ); wxString msg = wxString::Format( _( "Library %s is read only." ), libPath );
ShowInfoBarError( msg ); ShowInfoBarError( msg );
return wxEmptyString; return wxEmptyString;
} }
@ -471,7 +486,7 @@ wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary( const wxString& aLibName,
} }
if( doAdd ) if( doAdd )
AddLibrary( libPath, table ); AddLibrary( libPath, aTable );
return libPath; return libPath;
} }
@ -524,14 +539,10 @@ FP_LIB_TABLE* PCB_BASE_EDIT_FRAME::selectLibTable( bool aOptional )
bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename, FP_LIB_TABLE* aTable ) bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename, FP_LIB_TABLE* aTable )
{ {
if( aTable == nullptr ) if( aTable == nullptr )
{
aTable = selectLibTable(); aTable = selectLibTable();
if( aTable == nullptr ) if( aTable == nullptr )
{
return wxEmptyString; return wxEmptyString;
}
}
bool isGlobal = ( aTable == &GFootprintTable ); bool isGlobal = ( aTable == &GFootprintTable );
@ -553,7 +564,6 @@ bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename, FP_LIB_TABLE* a
if( libName.IsEmpty() ) if( libName.IsEmpty() )
return false; return false;
wxString type = IO_MGR::ShowType( IO_MGR::GuessPluginTypeFromLibPath( libPath ) ); wxString type = IO_MGR::ShowType( IO_MGR::GuessPluginTypeFromLibPath( libPath ) );
// try to use path normalized to an environmental variable or project path // try to use path normalized to an environmental variable or project path
@ -564,18 +574,14 @@ bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename, FP_LIB_TABLE* a
try try
{ {
auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString ); FP_LIB_TABLE_ROW* row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString );
aTable->InsertRow( row ); aTable->InsertRow( row );
if( isGlobal ) if( isGlobal )
{
GFootprintTable.Save( FP_LIB_TABLE::GetGlobalTableFileName() ); GFootprintTable.Save( FP_LIB_TABLE::GetGlobalTableFileName() );
}
else else
{
Prj().PcbFootprintLibs()->Save( Prj().FootprintLibTblName() ); Prj().PcbFootprintLibs()->Save( Prj().FootprintLibTblName() );
} }
}
catch( const IO_ERROR& ioe ) catch( const IO_ERROR& ioe )
{ {
DisplayError( this, ioe.What() ); DisplayError( this, ioe.What() );

View File

@ -63,6 +63,9 @@ public:
wxString CreateNewLibrary( const wxString& aLibName = wxEmptyString, wxString CreateNewLibrary( const wxString& aLibName = wxEmptyString,
const wxString& aProposedName = wxEmptyString ); const wxString& aProposedName = wxEmptyString );
wxString CreateNewProjectLibrary( const wxString& aLibName = wxEmptyString,
const wxString& aProposedName = wxEmptyString );
/** /**
* Add an existing library to either the global or project library table. * Add an existing library to either the global or project library table.
* *
@ -216,6 +219,13 @@ protected:
*/ */
FP_LIB_TABLE* selectLibTable( bool aOptional = false ); FP_LIB_TABLE* selectLibTable( bool aOptional = false );
/**
* Create a new library in the given table (presumed to be either the global or project
* library table).
*/
wxString createNewLibrary( const wxString& aLibName, const wxString& aProposedName,
FP_LIB_TABLE* aTable );
void handleActivateEvent( wxActivateEvent& aEvent ) override; void handleActivateEvent( wxActivateEvent& aEvent ) override;
void unitsChangeRefresh() override; void unitsChangeRefresh() override;