Library Editor: display a list of libraries to save on close

This commit is contained in:
Maciej Suminski 2017-11-13 10:55:05 +01:00
parent 2c20a10242
commit 24ddbbaf25
3 changed files with 59 additions and 41 deletions

View File

@ -288,7 +288,7 @@ void LIB_EDIT_FRAME::OnSaveLibrary( wxCommandEvent& event )
void LIB_EDIT_FRAME::OnSaveAllLibraries( wxCommandEvent& event ) void LIB_EDIT_FRAME::OnSaveAllLibraries( wxCommandEvent& event )
{ {
wxASSERT( false ); saveAllLibraries();
} }
@ -461,9 +461,7 @@ bool LIB_EDIT_FRAME::saveLibrary( const wxString& aLibrary, bool aNewFile )
m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() ); m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
wxString lib = getTargetLib(); if( !aNewFile && ( aLibrary.empty() || !prj.SchSymbolLibTable()->HasLibrary( aLibrary ) ) )
if( !aNewFile && ( lib.empty() || !prj.SchSymbolLibTable()->HasLibrary( lib ) ) )
{ {
DisplayError( this, _( "No library specified." ) ); DisplayError( this, _( "No library specified." ) );
return false; return false;
@ -471,7 +469,7 @@ bool LIB_EDIT_FRAME::saveLibrary( const wxString& aLibrary, bool aNewFile )
if( aNewFile ) if( aNewFile )
{ {
SEARCH_STACK* search = prj.SchSearchS(); SEARCH_STACK* search = prj.SchSearchS();
// Get a new name for the library // Get a new name for the library
wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH ); wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
@ -479,8 +477,11 @@ bool LIB_EDIT_FRAME::saveLibrary( const wxString& aLibrary, bool aNewFile )
if( !default_path ) if( !default_path )
default_path = search->LastVisitedPath(); default_path = search->LastVisitedPath();
wxFileDialog dlg( this, _( "Symbol Library Name" ), default_path, fn.SetName( aLibrary );
wxEmptyString, SchematicLibraryFileWildcard(), fn.SetExt( SchematicLibraryFileExtension );
wxFileDialog dlg( this, wxString::Format( _( "Save Library '%s' As..." ), aLibrary ),
default_path, fn.GetFullName(), SchematicLibraryFileWildcard(),
wxFD_SAVE | wxFD_OVERWRITE_PROMPT ); wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
if( dlg.ShowModal() == wxID_CANCEL ) if( dlg.ShowModal() == wxID_CANCEL )
@ -492,17 +493,10 @@ bool LIB_EDIT_FRAME::saveLibrary( const wxString& aLibrary, bool aNewFile )
// file name so add it here. // file name so add it here.
if( fn.GetExt().IsEmpty() ) if( fn.GetExt().IsEmpty() )
fn.SetExt( SchematicLibraryFileExtension ); fn.SetExt( SchematicLibraryFileExtension );
prj.SetRString( PROJECT::SCH_LIB_PATH, fn.GetPath() );
} }
else else
{ {
fn = prj.SchSymbolLibTable()->GetFullURI( lib ); fn = prj.SchSymbolLibTable()->GetFullURI( aLibrary );
msg.Printf( _( "Modify symbol library file '%s' ?" ), fn.GetFullPath() );
if( !IsOK( this, msg ) )
return false;
} }
// Verify the user has write privileges before attempting to save the library file. // Verify the user has write privileges before attempting to save the library file.
@ -579,7 +573,7 @@ bool LIB_EDIT_FRAME::saveLibrary( const wxString& aLibrary, bool aNewFile )
// Update symbol changes in library. // Update symbol changes in library.
if( GetScreen()->IsModify() ) if( GetScreen()->IsModify() )
{ {
if( !m_libMgr->FlushLibrary( lib ) ) if( !m_libMgr->FlushLibrary( aLibrary ) )
{ {
msg.Printf( _( "Failed to save changes to symbol library file '%s'" ), msg.Printf( _( "Failed to save changes to symbol library file '%s'" ),
libFileName.GetFullPath() ); libFileName.GetFullPath() );
@ -600,6 +594,47 @@ bool LIB_EDIT_FRAME::saveLibrary( const wxString& aLibrary, bool aNewFile )
} }
bool LIB_EDIT_FRAME::saveAllLibraries()
{
wxArrayString unsavedLibraries;
// There are two stages: first try to save libraries to the original files.
// In case of problems, ask the user to save them in a new location.
bool firstRun = true;
bool allSaved = false;
while( !allSaved )
{
allSaved = true;
unsavedLibraries.Empty();
for( const auto& lib : m_libMgr->GetLibraryNames() )
{
if( m_libMgr->IsLibraryModified( lib ) )
unsavedLibraries.Add( lib );
}
if( !unsavedLibraries.IsEmpty() )
{
auto res = SelectMultipleOptions( this, _( "Save Libraries" ),
firstRun ? _( "Select libraries to save before closing" )
: _( "Some libraries could not be saved to their original files.\n\n"
"Do you want to save them to a new file?" ),
unsavedLibraries, true );
if( !res.first )
return false; // dialog has been cancelled
for( auto libIndex : res.second )
allSaved &= saveLibrary( unsavedLibraries[libIndex], !firstRun );
firstRun = false;
}
}
return true;
}
void LIB_EDIT_FRAME::DisplayCmpDoc() void LIB_EDIT_FRAME::DisplayCmpDoc()
{ {
LIB_ALIAS* alias; LIB_ALIAS* alias;

View File

@ -338,31 +338,10 @@ void LIB_EDIT_FRAME::SetDrawItem( LIB_ITEM* drawItem )
void LIB_EDIT_FRAME::OnCloseWindow( wxCloseEvent& Event ) void LIB_EDIT_FRAME::OnCloseWindow( wxCloseEvent& Event )
{ {
// TODO check all libraries for modifications if( saveAllLibraries() )
if( GetScreen()->IsModify() ) Destroy();
{ else
int ii = DisplayExitDialog( this, _( "Save the changes in the library before closing?" ) ); Event.Veto();
switch( ii )
{
case wxID_NO:
break;
case wxID_YES:
if( saveLibrary( GetCurLib(), false ) )
break;
// fall through: cancel the close because of an error
case wxID_CANCEL:
Event.Veto();
return;
}
GetScreen()->ClrModify();
}
Destroy();
} }

View File

@ -706,6 +706,10 @@ public:
LIB_ID getTargetLibId() const; LIB_ID getTargetLibId() const;
///> Returns true when the operation has succeded (all requested libraries have been saved or
///> none was selected and confirmed by OK).
bool saveAllLibraries();
wxString getTargetLib() const; wxString getTargetLib() const;
bool addLibraryFile( bool aCreateNew ); bool addLibraryFile( bool aCreateNew );