Settings: allow multiple project access from Python standalone

We can't handle this in the UI, but we can externally, so allow
it for Python scripting and eventually we'll use this in the UI
also...

Fixes https://gitlab.com/kicad/code/kicad/-/issues/10540
This commit is contained in:
Jon Evans 2022-03-05 12:45:51 -05:00
parent 1ffe0da204
commit 1394509734
3 changed files with 36 additions and 21 deletions

View File

@ -954,15 +954,18 @@ std::vector<wxString> SETTINGS_MANAGER::GetOpenProjects() const
}
bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath )
bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath, PROJECT* aProject )
{
if( !aProject )
aProject = &Prj();
wxString path = aFullPath;
if( path.empty() )
path = Prj().GetProjectFullName();
path = aProject->GetProjectFullName();
// TODO: refactor for MDI
if( Prj().IsReadOnly() )
if( aProject->IsReadOnly() )
return false;
if( !m_project_files.count( path ) )
@ -972,24 +975,27 @@ bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath )
wxString projectPath = GetPathForSettingsFile( project );
project->SaveToFile( projectPath );
Prj().GetLocalSettings().SaveToFile( projectPath );
aProject->GetLocalSettings().SaveToFile( projectPath );
return true;
}
void SETTINGS_MANAGER::SaveProjectAs( const wxString& aFullPath )
void SETTINGS_MANAGER::SaveProjectAs( const wxString& aFullPath, PROJECT* aProject )
{
wxString oldName = Prj().GetProjectFullName();
if( !aProject )
aProject = &Prj();
wxString oldName = aProject->GetProjectFullName();
if( aFullPath.IsSameAs( oldName ) )
{
SaveProject( aFullPath );
SaveProject( aFullPath, aProject );
return;
}
// Changing this will cause UnloadProject to not save over the "old" project when loading below
Prj().setProjectFullName( aFullPath );
aProject->setProjectFullName( aFullPath );
wxFileName fn( aFullPath );
@ -997,14 +1003,14 @@ void SETTINGS_MANAGER::SaveProjectAs( const wxString& aFullPath )
// Ensure read-only flags are copied; this allows doing a "Save As" on a standalong board/sch
// without creating project files if the checkbox is turned off
project->SetReadOnly( Prj().IsReadOnly() );
Prj().GetLocalSettings().SetReadOnly( Prj().IsReadOnly() );
project->SetReadOnly( aProject->IsReadOnly() );
aProject->GetLocalSettings().SetReadOnly( aProject->IsReadOnly() );
project->SetFilename( fn.GetName() );
project->SaveToFile( fn.GetPath() );
Prj().GetLocalSettings().SetFilename( fn.GetName() );
Prj().GetLocalSettings().SaveToFile( fn.GetPath() );
aProject->GetLocalSettings().SetFilename( fn.GetName() );
aProject->GetLocalSettings().SaveToFile( fn.GetPath() );
m_project_files[fn.GetFullPath()] = project;
m_project_files.erase( oldName );
@ -1014,9 +1020,12 @@ void SETTINGS_MANAGER::SaveProjectAs( const wxString& aFullPath )
}
void SETTINGS_MANAGER::SaveProjectCopy( const wxString& aFullPath )
void SETTINGS_MANAGER::SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject )
{
PROJECT_FILE* project = m_project_files.at( Prj().GetProjectFullName() );
if( !aProject )
aProject = &Prj();
PROJECT_FILE* project = m_project_files.at( aProject->GetProjectFullName() );
wxString oldName = project->GetFilename();
wxFileName fn( aFullPath );
@ -1027,9 +1036,11 @@ void SETTINGS_MANAGER::SaveProjectCopy( const wxString& aFullPath )
project->SaveToFile( fn.GetPath() );
project->SetFilename( oldName );
Prj().GetLocalSettings().SetFilename( fn.GetName() );
Prj().GetLocalSettings().SaveToFile( fn.GetPath() );
Prj().GetLocalSettings().SetFilename( oldName );
PROJECT_LOCAL_SETTINGS& localSettings = aProject->GetLocalSettings();
localSettings.SetFilename( fn.GetName() );
localSettings.SaveToFile( fn.GetPath() );
localSettings.SetFilename( oldName );
project->SetReadOnly( readOnly );
}

View File

@ -276,23 +276,27 @@ public:
/**
* Saves a loaded project.
* @param aFullPath is the project name to save. If empty, will save the first loaded project.
* @param aProject is the project to save, or nullptr to save the active project (Prj() return)
* @return true if save was successful
*/
bool SaveProject( const wxString& aFullPath = wxEmptyString );
bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
/**
* Sets the currently loaded project path and saves it (pointers remain valid)
* Note that this will not modify the read-only state of the project, so it will have no effect
* if the project is marked as read-only!
* @param aFullPath is the full filename to set for the project
* @param aProject is the project to save, or nullptr to save the active project (Prj() return)
*/
void SaveProjectAs( const wxString& aFullPath );
void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
/**
* Saves a copy of the current project under the given path. Will save the copy even if the
* current project is marked as read-only.
* @param aFullPath is the full filename to set for the project
* @param aProject is the project to save, or nullptr to save the active project (Prj() return)
*/
void SaveProjectCopy( const wxString& aFullPath );
void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
/**
* @return the full path to where project backups should be stored

View File

@ -257,7 +257,7 @@ bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat )
pro.MakeAbsolute();
wxString projectPath = pro.GetFullPath();
GetSettingsManager()->SaveProjectAs( pro.GetFullPath() );
GetSettingsManager()->SaveProjectAs( pro.GetFullPath(), aBoard->GetProject() );
return true;
}