From 2e760483b023d6ce0a5e5777b9fde824979907c4 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Thu, 25 Jan 2024 18:20:43 +0100 Subject: [PATCH] HIERARCHY_PANE: Update tree labels when editing a sheet name or number Fixes #16650 https://gitlab.com/kicad/code/kicad/-/issues/16650 --- eeschema/dialogs/dialog_field_properties.cpp | 12 +++++- eeschema/sch_edit_frame.cpp | 8 ++++ eeschema/sch_edit_frame.h | 9 ++++- eeschema/tools/sch_edit_tool.cpp | 4 ++ eeschema/widgets/hierarchy_pane.cpp | 41 ++++++++++++++++++++ eeschema/widgets/hierarchy_pane.h | 9 ++++- 6 files changed, 80 insertions(+), 3 deletions(-) diff --git a/eeschema/dialogs/dialog_field_properties.cpp b/eeschema/dialogs/dialog_field_properties.cpp index 07327ee51a..ac94a1cefa 100644 --- a/eeschema/dialogs/dialog_field_properties.cpp +++ b/eeschema/dialogs/dialog_field_properties.cpp @@ -3,7 +3,7 @@ * * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.com * Copyright (C) 2016 Wayne Stambaugh, stambaughw@gmail.com - * Copyright (C) 2004-2023 KiCad Developers, see AITHORS.txt for contributors. + * Copyright (C) 2004-2024 KiCad Developers, see AITHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -617,6 +617,12 @@ void DIALOG_SCH_FIELD_PROPERTIES::UpdateField( SCH_COMMIT* aCommit, SCH_FIELD* a // convert any text variable cross-references to their UUIDs m_text = aField->Schematic()->ConvertRefsToKIIDs( m_text ); + // Changing a sheetname need to update the hierarchy navigator + bool needUpdateHierNav = false; + + if( parent && parent->Type() == SCH_SHEET_T && fieldType == SHEETNAME ) + needUpdateHierNav = m_text != aField->GetText(); + aField->SetText( m_text ); updateText( aField ); aField->SetPosition( m_position ); @@ -676,4 +682,8 @@ void DIALOG_SCH_FIELD_PROPERTIES::UpdateField( SCH_COMMIT* aCommit, SCH_FIELD* a if( positioningModified && parent ) parent->ClearFieldsAutoplaced(); + + //Update the hierarchy navigator labels if needed + if( needUpdateHierNav ) + editFrame->UpdateLabelsHierarchyNavigator(); } diff --git a/eeschema/sch_edit_frame.cpp b/eeschema/sch_edit_frame.cpp index 15fd4d3766..1a52659fcc 100644 --- a/eeschema/sch_edit_frame.cpp +++ b/eeschema/sch_edit_frame.cpp @@ -1143,6 +1143,14 @@ void SCH_EDIT_FRAME::UpdateHierarchyNavigator() } +void SCH_EDIT_FRAME::UpdateLabelsHierarchyNavigator() +{ + // Update only the hierarchy navigation tree labels. + // The tree list is expectyed to be up to date + m_hierarchy->UpdateLabelsHierarchyTree(); +} + + void SCH_EDIT_FRAME::UpdateHierarchySelection() { m_hierarchy->UpdateHierarchySelection(); diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h index 89144a8939..9e9670e6f7 100644 --- a/eeschema/sch_edit_frame.h +++ b/eeschema/sch_edit_frame.h @@ -4,7 +4,7 @@ * Copyright (C) 2015 Jean-Pierre Charras, jp.charras wanadoo.fr * Copyright (C) 2008 Wayne Stambaugh * Copyright (C) 2023 CERN (www.cern.ch) - * Copyright (C) 2004-2023 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2004-2024 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -233,6 +233,13 @@ public: */ void UpdateHierarchyNavigator(); + /** + * Update the hierarchy navigation tree labels. + * No change for the tree, only the labels are updated, after editing a sheet + * name or a sheet number. + */ + void UpdateLabelsHierarchyNavigator(); + /** * Update the hierarchy navigation tree selection (cross-probe from schematic to hierarchy * pane). diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp index fa2216bfd4..b9d745379f 100644 --- a/eeschema/tools/sch_edit_tool.cpp +++ b/eeschema/tools/sch_edit_tool.cpp @@ -2496,6 +2496,10 @@ int SCH_EDIT_TOOL::EditPageNumber( const TOOL_EVENT& aEvent ) m_frame->OnModify(); + // Update the hierarchy navigator labels if needed + if( pageNumber != dlg.GetValue() ) + m_frame->UpdateLabelsHierarchyNavigator(); + return 0; } diff --git a/eeschema/widgets/hierarchy_pane.cpp b/eeschema/widgets/hierarchy_pane.cpp index b1f33d79cd..99c411296f 100644 --- a/eeschema/widgets/hierarchy_pane.cpp +++ b/eeschema/widgets/hierarchy_pane.cpp @@ -263,6 +263,45 @@ void HIERARCHY_PANE::onSelectSheetPath( wxTreeEvent& aEvent ) } +void HIERARCHY_PANE::UpdateLabelsHierarchyTree() +{ + // Update the labels of the hierarchical tree of the schematic. + // Must be called only for an up to date tree, to update displayed labels after + // a sheet name or a sheet number change. + + std::function updateLabel = + [&]( const wxTreeItemId& id ) + { + TREE_ITEM_DATA* itemData = static_cast( m_tree->GetItemData( id ) ); + SCH_SHEET* sheet = itemData->m_SheetPath.Last(); + wxString sheetNameLabel = formatPageString( sheet->GetFields()[SHEETNAME].GetShownText( false ), + itemData->m_SheetPath.GetPageNumber() ); + if( m_tree->GetItemText( id ) != sheetNameLabel ) + m_tree->SetItemText( id, sheetNameLabel ); + }; + + wxTreeItemId rootId = m_tree->GetRootItem(); + updateLabel( rootId ); + + std::function recursiveDescent = + [&]( const wxTreeItemId& id ) + { + wxCHECK_RET( id.IsOk(), wxT( "Invalid tree item" ) ); + wxTreeItemIdValue cookie; + wxTreeItemId child = m_tree->GetFirstChild( id, cookie ); + + while( child.IsOk() ) + { + updateLabel( child ); + recursiveDescent( child ); + child = m_tree->GetNextChild( id, cookie ); + } + }; + + recursiveDescent( rootId ); +} + + void HIERARCHY_PANE::onRightClick( wxTreeEvent& aEvent ) { wxTreeItemId itemSel = aEvent.GetItem(); @@ -310,6 +349,8 @@ void HIERARCHY_PANE::onRightClick( wxTreeEvent& aEvent ) } m_frame->OnModify(); + + UpdateLabelsHierarchyTree(); } } } diff --git a/eeschema/widgets/hierarchy_pane.h b/eeschema/widgets/hierarchy_pane.h index a1b5774d61..d39b39bcf9 100644 --- a/eeschema/widgets/hierarchy_pane.h +++ b/eeschema/widgets/hierarchy_pane.h @@ -4,7 +4,7 @@ * Copyright (C) 2004 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2008 Wayne Stambaugh * Copyright (C) 2022 Mike Williams - * Copyright (C) 2004-2022 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2004-2024 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -81,6 +81,13 @@ public: */ void UpdateHierarchySelection(); + /** + * Update the labels of the hierarchical tree of the schematic. + * Must be called only for an up to date tree, to update displayed labels after + * a sheet name or a sheet number change. + */ + void UpdateLabelsHierarchyTree(); + private: /** * Create the hierarchical tree of the schematic.