Move PROJECT storage to SETTINGS_MANAGER

This commit is contained in:
Jon Evans 2020-05-25 13:38:09 -04:00
parent a7708fa6dc
commit a5a19076fd
6 changed files with 88 additions and 16 deletions

View File

@ -31,6 +31,7 @@
#include <pgm_base.h>
#include <config.h>
#include <id.h>
#include <settings/settings_manager.h>
#include <wx/stdpaths.h>
#include <wx/debug.h>
@ -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();
}

View File

@ -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;
}

View File

@ -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",

View File

@ -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<PROJECT>();
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<PROJECT_FILE*>( 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 );
}

View File

@ -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().
};

View File

@ -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<std::unique_ptr<JSON_SETTINGS>> m_settings;
std::unordered_map<wxString, COLOR_SETTINGS*> 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<wxString, std::unique_ptr<PROJECT>> m_projects;
/// Loaded project files, mapped according to project full name
std::map<wxString, PROJECT_FILE*> m_project_files;
/// A list of project settings objects for each loaded project