From a5a19076fdaecc41266c1e36a08fb73f6a199dd0 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Mon, 25 May 2020 13:38:09 -0400 Subject: [PATCH] Move PROJECT storage to SETTINGS_MANAGER --- common/kiway.cpp | 3 +- common/pgm_base.cpp | 4 +++ common/settings/json_settings.cpp | 6 ++++ common/settings/settings_manager.cpp | 54 +++++++++++++++++++++++----- include/kiway.h | 2 -- include/settings/settings_manager.h | 35 +++++++++++++++--- 6 files changed, 88 insertions(+), 16 deletions(-) diff --git a/common/kiway.cpp b/common/kiway.cpp index 67868d747e..2099177e95 100644 --- a/common/kiway.cpp +++ b/common/kiway.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -170,7 +171,7 @@ const wxString KIWAY::dso_search_path( FACE_T aFaceId ) PROJECT& KIWAY::Prj() const { - return *(PROJECT*) &m_project; // strip const-ness, function really is const. + return Pgm().GetSettingsManager().Prj(); } diff --git a/common/pgm_base.cpp b/common/pgm_base.cpp index df2ea60161..02cdc9bf12 100644 --- a/common/pgm_base.cpp +++ b/common/pgm_base.cpp @@ -442,6 +442,10 @@ bool PGM_BASE::InitPgm() wxSystemOptions::SetOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES, 1 ); #endif + // TODO(JE): Remove this if apps are refactored to not assume Prj() always works + // Need to create a project early for now (it can have an empty path for the moment) + GetSettingsManager().LoadProject( "" ); + return true; } diff --git a/common/settings/json_settings.cpp b/common/settings/json_settings.cpp index 5b964f589b..0bd98218d1 100644 --- a/common/settings/json_settings.cpp +++ b/common/settings/json_settings.cpp @@ -252,6 +252,10 @@ bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce ) if( !m_writeFile ) return false; + // Default PROJECT won't have a filename set + if( m_filename.empty() ) + return false; + wxFileName path; if( aDirectory.empty() ) @@ -292,6 +296,8 @@ bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce ) return false; } + wxLogTrace( traceSettings, "Saving %s", m_filename ); + if( !path.DirExists() && !path.Mkdir() ) { wxLogTrace( traceSettings, "Warning: could not create path %s, can't save %s", diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp index 65d00a718c..53789402fd 100644 --- a/common/settings/settings_manager.cpp +++ b/common/settings/settings_manager.cpp @@ -68,6 +68,7 @@ SETTINGS_MANAGER::~SETTINGS_MANAGER() { m_settings.clear(); m_color_settings.clear(); + m_projects.clear(); } @@ -661,14 +662,52 @@ bool SETTINGS_MANAGER::extractVersion( const std::string& aVersionString, int* a } -bool SETTINGS_MANAGER::LoadProject( PROJECT& aProject ) +bool SETTINGS_MANAGER::LoadProject( const wxString& aFullPath ) { - wxString name = aProject.GetProjectFullName(); - std::string fn( name.ToUTF8() ); - // Unload if already loaded - if( m_project_files.count( name ) ) - UnloadProject( aProject ); + if( m_projects.count( aFullPath ) && !UnloadProject( *m_projects.at( aFullPath ).get() ) ) + return false; + + // No MDI yet + if( !m_projects.empty() && !UnloadProject( *m_projects.begin()->second ) ) + return false; + + wxLogTrace( traceSettings, "Load project %s", aFullPath ); + + m_projects[aFullPath] = std::make_unique(); + m_projects[aFullPath]->SetProjectFullName( aFullPath ); + + return loadProjectFile( *m_projects[aFullPath] ); +} + + +bool SETTINGS_MANAGER::UnloadProject( PROJECT& aProject ) +{ + if( !m_projects.count( aProject.GetProjectFullName() ) ) + return false; + + if( !unloadProjectFile( aProject ) ) + return false; + + wxLogTrace( traceSettings, "Unload project %s", aProject.GetProjectFullName() ); + + m_projects.erase( aProject.GetProjectFullName() ); + + return true; +} + + +PROJECT& SETTINGS_MANAGER::Prj() const +{ + // No MDI yet + wxASSERT( m_projects.size() == 1 ); + return *m_projects.begin()->second; +} + + +bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject ) +{ + std::string fn( aProject.GetProjectFullName().ToUTF8() ); PROJECT_FILE* file = static_cast( RegisterSettings( new PROJECT_FILE( fn ), false ) ); @@ -679,7 +718,7 @@ bool SETTINGS_MANAGER::LoadProject( PROJECT& aProject ) } -bool SETTINGS_MANAGER::UnloadProject( PROJECT& aProject ) +bool SETTINGS_MANAGER::unloadProjectFile( PROJECT& aProject ) { wxString name = aProject.GetProjectFullName(); @@ -696,7 +735,6 @@ bool SETTINGS_MANAGER::UnloadProject( PROJECT& aProject ) if( it != m_settings.end() ) { - wxLogTrace( traceSettings, "Unload project %s", ( *it )->GetFilename() ); ( *it )->SaveToFile(); m_settings.erase( it ); } diff --git a/include/kiway.h b/include/kiway.h index 92646f83cd..d590b234c5 100644 --- a/include/kiway.h +++ b/include/kiway.h @@ -441,8 +441,6 @@ private: // wxWindow::FindWindowByName( m_playerFrameName[aFrameType] ) // to know if still exists (or GetPlayerFrame( FRAME_T aFrameType ) wxArrayString m_playerFrameName; - - PROJECT m_project; // do not assume this is here, use Prj(). }; diff --git a/include/settings/settings_manager.h b/include/settings/settings_manager.h index 86488b6694..b1a2646a95 100644 --- a/include/settings/settings_manager.h +++ b/include/settings/settings_manager.h @@ -182,19 +182,25 @@ public: void ReloadColorSettings(); /** - * Registers a project and attempts to load the associated PROJECT_FILE - * @param aProject is the project object to load + * Loads a project + * @param aFullPath is the full path to the project * @return true if the PROJECT_FILE was successfully loaded */ - bool LoadProject( PROJECT& aProject ); + bool LoadProject( const wxString& aFullPath ); /** - * Saves, unloads and unregisters the given project + * Saves, unloads and unregisters the given PROJECT * @param aProject is the project object to unload * @return true if the PROJECT file was successfully saved */ bool UnloadProject( PROJECT& aProject ); + /** + * A helper while we are not MDI-capable -- return the one and only project + * @return the loaded project + */ + PROJECT& Prj() const; + /** * Checks if a given path is probably a valid KiCad configuration directory. * Actually it just checks if a file called "kicad_common" exists, because that's probably @@ -274,6 +280,22 @@ private: void loadAllColorSettings(); + /** + * Registers a PROJECT_FILE and attempts to load it from disk + * @param aProject is the project object to load the file for + * @return true if the PROJECT_FILE was successfully loaded + */ + bool loadProjectFile( PROJECT& aProject ); + + /** + * Saves, unloads and unregisters the given PROJECT_FILE + * @param aProject is the project object to unload the file for + * @return true if the PROJECT file was successfully saved + */ + bool unloadProjectFile( PROJECT& aProject ); + +private: + std::vector> m_settings; std::unordered_map m_color_settings; @@ -286,7 +308,10 @@ private: /// True if settings loaded successfully at construction bool m_ok; - /// Project files, mapped according to project full name + /// Loaded projects, mapped according to project full name + std::map> m_projects; + + /// Loaded project files, mapped according to project full name std::map m_project_files; /// A list of project settings objects for each loaded project