From a107f7ce5b632dcccf88ab55a60b3ae56c2d986e Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Mon, 20 Jul 2020 19:39:59 -0400 Subject: [PATCH] Fix a few issues with legacy project loading - Prevent crash on project tree for "Empty project" item - Correctly handle loading of legacy projects in tree view - Fix an issue where legacy projects would not get saved Fixes https://gitlab.com/kicad/code/kicad/-/issues/4944 --- common/settings/json_settings.cpp | 3 ++- kicad/tree_project_frame.cpp | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/common/settings/json_settings.cpp b/common/settings/json_settings.cpp index c4e69d33f6..8e78dae033 100644 --- a/common/settings/json_settings.cpp +++ b/common/settings/json_settings.cpp @@ -311,7 +311,8 @@ bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce ) return false; } - if( !path.IsFileWritable() ) + if( ( path.FileExists() && !path.IsFileWritable() ) || + ( !path.FileExists() && !path.IsDirWritable() ) ) { wxLogTrace( traceSettings, "File for %s is read-only; not saving", GetFullFilename() ); return false; diff --git a/kicad/tree_project_frame.cpp b/kicad/tree_project_frame.cpp index 553caf6662..727288a2f1 100644 --- a/kicad/tree_project_frame.cpp +++ b/kicad/tree_project_frame.cpp @@ -544,6 +544,7 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj() return; wxFileName fn = pro_dir; + bool prjReset = false; if( !fn.IsOk() ) { @@ -551,10 +552,22 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj() fn.SetPath( wxStandardPaths::Get().GetDocumentsDir() ); fn.SetName( NAMELESS_PROJECT ); fn.SetExt( ProjectFileExtension ); + prjReset = true; } bool prjOpened = fn.FileExists(); + // We may have opened a legacy project, in which case GetProjectFileName will return the + // name of the migrated (new format) file, which may not have been saved to disk yet. + if( !prjOpened && !prjReset ) + { + fn.SetExt( LegacyProjectFileExtension ); + prjOpened = fn.FileExists(); + + // Set the ext back so that in the tree view we see the (not-yet-saved) new file + fn.SetExt( ProjectFileExtension ); + } + // root tree: m_root = m_TreeProject->AddRoot( fn.GetFullName(), TREE_ROOT, TREE_ROOT ); m_TreeProject->SetItemBold( m_root, true ); @@ -631,6 +644,16 @@ void TREE_PROJECT_FRAME::OnRight( wxTreeEvent& Event ) for( TREEPROJECT_ITEM* item_data : tree_data ) { + // Check for empty project + if( !item_data ) + { + can_switch_to_project = false; + can_edit = false; + can_rename = false; + can_print = false; + continue; + } + int tree_id = item_data->GetType(); wxString full_file_name = item_data->GetFileName();