From 13945097349d603044e3d51e9b53fd308010583c Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Sat, 5 Mar 2022 12:45:51 -0500 Subject: [PATCH] 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 --- common/settings/settings_manager.cpp | 45 ++++++++++++------- include/settings/settings_manager.h | 10 +++-- .../scripting/pcbnew_scripting_helpers.cpp | 2 +- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp index 7975b8230d..eeb46a3572 100644 --- a/common/settings/settings_manager.cpp +++ b/common/settings/settings_manager.cpp @@ -954,15 +954,18 @@ std::vector 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 ); } diff --git a/include/settings/settings_manager.h b/include/settings/settings_manager.h index b11ef412ee..383022b6a0 100644 --- a/include/settings/settings_manager.h +++ b/include/settings/settings_manager.h @@ -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 diff --git a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp index 59d0be12a3..675edb5b1e 100644 --- a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp +++ b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp @@ -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; }