Fix a few issues with Close Projects MR

Store list of previously open projects only in kicad.json
Keep all management of open projects in SETTINGS_MANAGER

Also add kicadSettings() for convenience
This commit is contained in:
Jon Evans 2020-08-08 16:00:29 -04:00
parent bc357dff8a
commit 79e40cf765
8 changed files with 41 additions and 76 deletions

View File

@ -751,6 +751,17 @@ PROJECT* SETTINGS_MANAGER::GetProject( const wxString& aFullPath ) const
}
std::vector<wxString> SETTINGS_MANAGER::GetOpenProjects() const
{
std::vector<wxString> ret;
for( const std::pair<const wxString, std::unique_ptr<PROJECT>>& pair : m_projects )
ret.emplace_back( pair.first );
return ret;
}
bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath )
{
wxString path = aFullPath;

View File

@ -128,8 +128,6 @@ public:
{
bool first_run_shown;
int max_undo_items;
std::vector<wxString> open_projects; // using a vector if in the future
// we want to open multiple projects at once.
std::vector<wxString> file_history;
int units;
};

View File

@ -213,6 +213,11 @@ public:
*/
PROJECT* GetProject( const wxString& aFullPath ) const;
/**
* @return a list of open projects
*/
std::vector<wxString> GetOpenProjects() const;
/**
* Saves a loaded project.
* @param aFullPath is the project name to save. If empty, will save the first loaded project.

View File

@ -133,15 +133,18 @@ bool PGM_KICAD::OnPgmInit()
Kiway.SetTop( frame );
KICAD_SETTINGS* settings = static_cast<KICAD_SETTINGS*>( PgmSettings() );
wxString projToLoad;
if( App().argc > 1 )
{
projToLoad = App().argv[1];
}
else if( frame->GetOpenProjects().size() ) // Check that there was a file open.
else if( settings->m_OpenProjects.size() ) // Check that there was a file open.
{
wxString last_pro = frame->PopOpenProjects();
wxString last_pro = settings->m_OpenProjects.front();
settings->m_OpenProjects.erase( settings->m_OpenProjects.begin() );
if( !wxFileExists( last_pro ) )
{
@ -159,6 +162,7 @@ bool PGM_KICAD::OnPgmInit()
if( !projToLoad.empty() )
{
wxFileName fn( projToLoad );
if( fn.Exists() )
{
fn.MakeAbsolute();

View File

@ -176,6 +176,9 @@ KICAD_MANAGER_FRAME::KICAD_MANAGER_FRAME( wxWindow* parent, const wxString& titl
KICAD_MANAGER_FRAME::~KICAD_MANAGER_FRAME()
{
KICAD_SETTINGS* settings = kicadSettings();
settings->m_OpenProjects = GetSettingsManager()->GetOpenProjects();
// Shutdown all running tools
if( m_toolManager )
m_toolManager->ShutdownAllTools();
@ -202,6 +205,14 @@ APP_SETTINGS_BASE* KICAD_MANAGER_FRAME::config() const
}
KICAD_SETTINGS* KICAD_MANAGER_FRAME::kicadSettings() const
{
KICAD_SETTINGS* ret = dynamic_cast<KICAD_SETTINGS*>( config() );
wxASSERT( ret );
return ret;
}
void KICAD_MANAGER_FRAME::SetProjectFileName( const wxString& aFullProjectProFileName )
{
// ensure file name is absolute:
@ -220,53 +231,9 @@ void KICAD_MANAGER_FRAME::SetProjectFileName( const wxString& aFullProjectProFil
}
std::vector<wxString> KICAD_MANAGER_FRAME::GetOpenProjects()
{
KICAD_SETTINGS* conf = dynamic_cast<KICAD_SETTINGS*>( config() );
if( conf == NULL )
{
// Build an empty vector to return
std::vector<wxString> dummy;
return dummy;
}
return conf->m_System.open_projects;
}
wxString KICAD_MANAGER_FRAME::PopOpenProjects()
{
KICAD_SETTINGS* conf = dynamic_cast<KICAD_SETTINGS*>( config() );
if( conf == NULL )
{
return wxString( "" );
}
std::vector<wxString>* vector = &( conf->m_System.open_projects );
if( vector->size() > 0 )
{
wxString value = vector->front();
vector->erase( vector->begin() );
return value;
}
else
{
return wxString( "" );
}
}
const wxString KICAD_MANAGER_FRAME::GetProjectFileName() const
{
if( m_active_project )
return Prj().GetProjectFullName();
else
{
return wxString( "" );
}
return m_active_project ? Prj().GetProjectFullName() : wxString( wxEmptyString );
}
@ -387,21 +354,6 @@ bool KICAD_MANAGER_FRAME::CloseProject( bool aSave )
// Save the project file for the currently loaded project.
if( m_active_project )
{
// Remove the project from the list of active projects
std::vector<wxString>::iterator ptr;
std::vector<wxString>* prjList;
prjList = &( config()->m_System.open_projects );
for( ptr = prjList->begin(); ptr < prjList->end(); ptr++ )
{
if( *ptr == Prj().GetProjectFullName() )
{
prjList->erase( ptr );
break;
}
}
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
mgr.TriggerBackupIfNeeded( NULL_REPORTER::GetInstance() );
@ -439,10 +391,6 @@ void KICAD_MANAGER_FRAME::LoadProject( const wxFileName& aProjectFileName )
Pgm().GetSettingsManager().LoadProject( aProjectFileName.GetFullPath() );
SetProjectFileName( Prj().GetProjectFullName() );
std::vector<wxString>::iterator ptr;
ptr = config()->m_System.open_projects.begin();
config()->m_System.open_projects.insert( ptr, Prj().GetProjectFullName() );
if( aProjectFileName.IsDirWritable() )
SetMruPath( Prj().GetProjectPath() ); // Only set MRU path if we have write access. Why?

View File

@ -100,12 +100,6 @@ public:
void RecreateBaseHToolbar();
void RecreateLauncher();
std::vector<wxString> GetOpenProjects();
/**
* Get element at index 0, and remove it.
*/
wxString PopOpenProjects();
wxString GetCurrentFileName() const override
{
return GetProjectFileName();
@ -190,6 +184,8 @@ public:
private:
APP_SETTINGS_BASE* config() const override;
KICAD_SETTINGS* kicadSettings() const;
const SEARCH_STACK& sys_search() override;
wxString help_name() override;

View File

@ -29,8 +29,9 @@ const int kicadSchemaVersion = 0;
KICAD_SETTINGS::KICAD_SETTINGS() : APP_SETTINGS_BASE( "kicad", kicadSchemaVersion )
{
m_params.emplace_back( new PARAM<int>( "appearance.left_frame_width", &m_LeftWinWidth, 200 ) );
m_params.emplace_back(
new PARAM_LIST<wxString>( "system.open_projects", &m_System.open_projects, {} ) );
m_params.emplace_back( new PARAM_LIST<wxString>( "system.open_projects",
&m_OpenProjects, {} ) );
}

View File

@ -36,6 +36,8 @@ public:
int m_LeftWinWidth;
std::vector<wxString> m_OpenProjects;
protected:
virtual std::string getLegacyFrameName() const override { return "KicadFrame"; }