From f3e44fb02976a95fd8e47aa1b561c79dcb88dbb7 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Tue, 7 Jul 2020 01:14:27 +0100 Subject: [PATCH] Fix showing new formats in project tree * Differentiate between new JSON and old project files * Show both old and new schematic files in the tree --- kicad/kicad_manager_frame.h | 5 ++- kicad/tree_project_frame.cpp | 76 ++++++++++++++++++++++++++++-------- kicad/treeproject_item.cpp | 20 ++++------ kicad/treeprojectfiles.cpp | 3 +- 4 files changed, 73 insertions(+), 31 deletions(-) diff --git a/kicad/kicad_manager_frame.h b/kicad/kicad_manager_frame.h index ab602037d6..461a49e7e8 100644 --- a/kicad/kicad_manager_frame.h +++ b/kicad/kicad_manager_frame.h @@ -43,9 +43,10 @@ class KICAD_SETTINGS; enum TreeFileType { TREE_ROOT = 0, - TREE_PROJECT, + TREE_LEGACY_PROJECT, // Legacy project file (.pro) + TREE_JSON_PROJECT, // JSON formatted project file (.kicad_pro) TREE_LEGACY_SCHEMATIC, // Schematic file (.sch) - TREE_SEXPR_SCHEMATIC, // Schematic file (.sch) + TREE_SEXPR_SCHEMATIC, // Schematic file (.kicad_sch) TREE_LEGACY_PCB, // board file (.brd) legacy format TREE_SEXPR_PCB, // board file (.kicad_brd) new s expression format TREE_GERBER, // Gerber file (.pho, .g*) diff --git a/kicad/tree_project_frame.cpp b/kicad/tree_project_frame.cpp index d32aad898a..553caf6662 100644 --- a/kicad/tree_project_frame.cpp +++ b/kicad/tree_project_frame.cpp @@ -64,6 +64,7 @@ // Add extensions in a compatible regex format to see others files types static const wxChar* s_allowedExtensionsToList[] = { wxT( "^.*\\.pro$" ), + wxT( "^.*\\.kicad_pro$" ), wxT( "^.*\\.pdf$" ), wxT( "^.*\\.sch$" ), // Legacy Eeschema files wxT( "^.*\\.kicad_sch$" ), // S-expr Eeschema files @@ -264,7 +265,8 @@ wxString TREE_PROJECT_FRAME::GetFileExt( TreeFileType type ) { switch( type ) { - case TREE_PROJECT: return ProjectFileExtension; + case TREE_LEGACY_PROJECT: return LegacyProjectFileExtension; + case TREE_JSON_PROJECT: return ProjectFileExtension; case TREE_LEGACY_SCHEMATIC: return LegacySchematicFileExtension; case TREE_SEXPR_SCHEMATIC: return KiCadSchematicFileExtension; case TREE_LEGACY_PCB: return LegacyPcbFileExtension; @@ -294,8 +296,8 @@ wxTreeItemId TREE_PROJECT_FRAME::AddItemToTreeProject( const wxString& aName, wxTreeItemId& aRoot, bool aCanResetFileWatcher, bool aRecurse ) { wxTreeItemId newItemId; - TreeFileType type = TREE_UNKNOWN; - wxFileName fn( aName ); + TreeFileType type = TREE_UNKNOWN; + wxFileName fn( aName ); // Files/dirs names starting by "." are not visible files under unices. // Skip them also under Windows @@ -374,7 +376,8 @@ wxTreeItemId TREE_PROJECT_FRAME::AddItemToTreeProject( addFile = true; break; } - } else if( fn.GetExt() == "kicad_sch" ) + } + else if( fn.GetExt() == "kicad_sch" ) { char* start = line; @@ -396,14 +399,14 @@ wxTreeItemId TREE_PROJECT_FRAME::AddItemToTreeProject( } } - for( int i = TREE_PROJECT; i < TREE_MAX; i++ ) + for( int i = TREE_LEGACY_PROJECT; i < TREE_MAX; i++ ) { wxString ext = GetFileExt( (TreeFileType) i ); if( ext == wxT( "" ) ) continue; - reg.Compile( wxString::FromAscii( "^.*\\" ) + ext + + reg.Compile( wxString::FromAscii( "^.*\\." ) + ext + wxString::FromAscii( "$" ), wxRE_ICASE ); if( reg.Matches( aName ) ) @@ -414,9 +417,17 @@ wxTreeItemId TREE_PROJECT_FRAME::AddItemToTreeProject( } } + wxString file = wxFileNameFromPath( aName ); + wxFileName currfile( file ); + wxFileName project( m_Parent->GetProjectFileName() ); + + // Ignore legacy projects with the same name as the current project + if( ( type == TREE_LEGACY_PROJECT ) && ( currfile.GetName().CmpNoCase( project.GetName() ) == 0 ) ) + return newItemId; + // also check to see if it is already there. - wxTreeItemIdValue cookie; - wxTreeItemId kid = m_TreeProject->GetFirstChild( aRoot, cookie ); + wxTreeItemIdValue cookie; + wxTreeItemId kid = m_TreeProject->GetFirstChild( aRoot, cookie ); while( kid.IsOk() ) { @@ -431,8 +442,41 @@ wxTreeItemId TREE_PROJECT_FRAME::AddItemToTreeProject( kid = m_TreeProject->GetNextChild( aRoot, cookie ); } + // Only show the JSON project files if both legacy and JSON files are present + if( ( type == TREE_LEGACY_PROJECT ) || ( type == TREE_JSON_PROJECT ) ) + { + kid = m_TreeProject->GetFirstChild( aRoot, cookie ); + + while( kid.IsOk() ) + { + TREEPROJECT_ITEM* itemData = GetItemIdData( kid ); + + if( itemData ) + { + wxFileName fname( itemData->GetFileName() ); + + if( fname.GetName().CmpNoCase( currfile.GetName() ) == 0 ) + { + // If the tree item is the legacy project remove it. + if( itemData->GetType() == TREE_LEGACY_PROJECT ) + { + m_TreeProject->Delete( kid ); + break; + } + // If we are the legacy project and the tree was the JSON project, ignore this file + else if( ( itemData->GetType() == TREE_JSON_PROJECT ) + && ( type == TREE_LEGACY_PROJECT ) ) + { + return newItemId; + } + } + } + + kid = m_TreeProject->GetNextChild( aRoot, cookie ); + } + } + // Append the item (only appending the filename not the full path): - wxString file = wxFileNameFromPath( aName ); newItemId = m_TreeProject->AppendItem( aRoot, file ); TREEPROJECT_ITEM* data = new TREEPROJECT_ITEM( type, aName, m_TreeProject ); @@ -440,9 +484,6 @@ wxTreeItemId TREE_PROJECT_FRAME::AddItemToTreeProject( data->SetState( 0 ); // Mark root files (files which have the same aName as the project) - wxFileName project( m_Parent->GetProjectFileName() ); - wxFileName currfile( file ); - if( currfile.GetName().CmpNoCase( project.GetName() ) == 0 ) data->SetRootFile( true ); else @@ -456,11 +497,11 @@ wxTreeItemId TREE_PROJECT_FRAME::AddItemToTreeProject( // in this case AddFile is recursive, but for the first level only. if( TREE_DIRECTORY == type && aRecurse ) { - wxDir dir( aName ); + wxDir dir( aName ); if( dir.IsOpened() ) // protected dirs will not open properly. { - wxString dir_filename; + wxString dir_filename; data->SetPopulated( true ); #ifndef __WINDOWS__ @@ -517,7 +558,9 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj() // root tree: m_root = m_TreeProject->AddRoot( fn.GetFullName(), TREE_ROOT, TREE_ROOT ); m_TreeProject->SetItemBold( m_root, true ); - m_TreeProject->SetItemData( m_root, new TREEPROJECT_ITEM( TREE_PROJECT, fn.GetFullPath(), + + // The main project file is now a JSON file + m_TreeProject->SetItemData( m_root, new TREEPROJECT_ITEM( TREE_JSON_PROJECT, fn.GetFullPath(), m_TreeProject ) ); // Now adding all current files if available @@ -593,7 +636,8 @@ void TREE_PROJECT_FRAME::OnRight( wxTreeEvent& Event ) switch( tree_id ) { - case TREE_PROJECT: + case TREE_LEGACY_PROJECT: + case TREE_JSON_PROJECT: can_edit = false; can_rename = false; can_delete = false; diff --git a/kicad/treeproject_item.cpp b/kicad/treeproject_item.cpp index dbad4f53e6..a22bafb934 100644 --- a/kicad/treeproject_item.cpp +++ b/kicad/treeproject_item.cpp @@ -158,10 +158,12 @@ void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* aTreePrjFrame ) switch( GetType() ) { - case TREE_PROJECT: + case TREE_LEGACY_PROJECT: + case TREE_JSON_PROJECT: // Select a new project if this is not the current project: if( id != aTreePrjFrame->m_TreeProject->GetRootItem() ) frame->LoadProject( fullFileName ); + break; case TREE_DIRECTORY: @@ -170,28 +172,22 @@ void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* aTreePrjFrame ) case TREE_LEGACY_SCHEMATIC: case TREE_SEXPR_SCHEMATIC: - if( fullFileName == frame->SchFileName() ) - { + // Schematics not part of the project are opened in a separate process. + if( fullFileName == frame->SchFileName() || fullFileName == frame->SchLegacyFileName() ) toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editSchematic, true ); - } else - { - // schematics not part of the project are opened in a separate process. toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherSch, true, &fullFileName ); - } + break; case TREE_LEGACY_PCB: case TREE_SEXPR_PCB: + // Boards not part of the project are opened in a separate process. if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() ) - { toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editPCB, true ); - } else - { - // boards not part of the project are opened in a separate process. toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherPCB, true, &fullFileName ); - } + break; case TREE_GERBER: diff --git a/kicad/treeprojectfiles.cpp b/kicad/treeprojectfiles.cpp index 877bf8744a..caaa340c7d 100644 --- a/kicad/treeprojectfiles.cpp +++ b/kicad/treeprojectfiles.cpp @@ -55,7 +55,8 @@ TREEPROJECTFILES::TREEPROJECTFILES( TREE_PROJECT_FRAME* parent ) // Make an image list containing small icons m_ImageList = new wxImageList( iconsize.x, iconsize.y, true, TREE_MAX ); - m_ImageList->Add( KiBitmap( new_project_xpm ) ); // TREE_PROJECT + m_ImageList->Add( KiBitmap( new_project_xpm ) ); // TREE_LEGACY_PROJECT + m_ImageList->Add( KiBitmap( new_project_xpm ) ); // TREE_JSON_PROJECT m_ImageList->Add( KiBitmap( eeschema_xpm ) ); // TREE_LEGACY_SCHEMATIC m_ImageList->Add( KiBitmap( eeschema_xpm ) ); // TREE_SEXPR_SCHEMATIC m_ImageList->Add( KiBitmap( pcbnew_xpm ) ); // TREE_LEGACY_PCB