From d3311ee23123e537df4f47c8b710ae92df05ae98 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Thu, 27 Apr 2023 12:56:15 +0100 Subject: [PATCH] Implement better sheet-path awareness for label text var resolution. (cherry picked from commit 4b0027a5d7dfd0127f1779ec93d17d9f63b56df5) --- eeschema/sch_field.cpp | 7 ++++--- eeschema/sch_field.h | 8 +++++++- eeschema/sch_label.cpp | 25 ++++++++++++++++--------- eeschema/sch_label.h | 17 ++++++++++++++--- eeschema/sch_sheet.cpp | 6 +++--- eeschema/sch_sheet.h | 6 +++++- eeschema/sch_symbol.cpp | 12 +++++++----- eeschema/sch_symbol.h | 5 +++-- eeschema/sch_text.cpp | 13 ++++++++++--- eeschema/sch_text.h | 8 +++++++- 10 files changed, 76 insertions(+), 31 deletions(-) diff --git a/eeschema/sch_field.cpp b/eeschema/sch_field.cpp index 3cc5f2dbe9..0c43ee7c53 100644 --- a/eeschema/sch_field.cpp +++ b/eeschema/sch_field.cpp @@ -172,12 +172,13 @@ void SCH_FIELD::SetId( int aId ) } -wxString SCH_FIELD::GetShownText( int aDepth, bool aAllowExtraText ) const +wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, int aDepth, + bool aAllowExtraText ) const { std::function symbolResolver = [&]( wxString* token ) -> bool { - return static_cast( m_parent )->ResolveTextVar( token, aDepth + 1 ); + return static_cast( m_parent )->ResolveTextVar( token, aDepth + 1, aPath ); }; std::function sheetResolver = @@ -189,7 +190,7 @@ wxString SCH_FIELD::GetShownText( int aDepth, bool aAllowExtraText ) const std::function labelResolver = [&]( wxString* token ) -> bool { - return static_cast( m_parent )->ResolveTextVar( token, + return static_cast( m_parent )->ResolveTextVar( aPath, token, aDepth + 1 ); }; diff --git a/eeschema/sch_field.h b/eeschema/sch_field.h index 7335a4c066..f787f6048a 100644 --- a/eeschema/sch_field.h +++ b/eeschema/sch_field.h @@ -126,7 +126,13 @@ public: void SetId( int aId ); - wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const override; + wxString GetShownText( const SCH_SHEET_PATH* aPath, int aDepth = 0, + bool aAllowExtraText = true ) const; + + wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const override + { + return GetShownText( nullptr, aDepth, aAllowExtraText ); + } COLOR4D GetFieldColor() const; diff --git a/eeschema/sch_label.cpp b/eeschema/sch_label.cpp index 6d924e47af..e909aa494b 100644 --- a/eeschema/sch_label.cpp +++ b/eeschema/sch_label.cpp @@ -494,7 +494,8 @@ void SCH_LABEL_BASE::GetIntersheetRefs( std::vectorIsSameAs( m_fields[i].GetName() ) ) + if( token->IsSameAs( field.GetName() ) ) { - *token = m_fields[i].GetShownText( aDepth + 1 ); + *token = field.GetShownText( aDepth + 1 ); return true; } } @@ -561,9 +562,14 @@ bool SCH_LABEL_BASE::ResolveTextVar( wxString* token, int aDepth ) const if( sheet->ResolveTextVar( token, aDepth + 1 ) ) return true; } + else if( aPath && aPath->Last() ) + { + if( aPath->Last()->ResolveTextVar( aPath, token, aDepth + 1 ) ) + return true; + } else if( SCH_SHEET* sheet = Schematic()->CurrentSheet().Last() ) { - if( sheet->ResolveTextVar( token, aDepth + 1 ) ) + if( sheet->ResolveTextVar( aPath, token, aDepth + 1 ) ) return true; } @@ -571,12 +577,12 @@ bool SCH_LABEL_BASE::ResolveTextVar( wxString* token, int aDepth ) const } -wxString SCH_LABEL_BASE::GetShownText( int aDepth, bool aAllowExtraText ) const +wxString SCH_LABEL_BASE::GetShownText( const SCH_SHEET_PATH* aPath, int aDepth, bool aAllowExtraText ) const { std::function textResolver = [&]( wxString* token ) -> bool { - return ResolveTextVar( token, aDepth ); + return ResolveTextVar( aPath, token, aDepth ); }; wxString text = EDA_TEXT::GetShownText(); @@ -1386,7 +1392,8 @@ void SCH_GLOBALLABEL::MirrorVertically( int aCenter ) } -bool SCH_GLOBALLABEL::ResolveTextVar( wxString* token, int aDepth ) const +bool SCH_GLOBALLABEL::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, + int aDepth ) const { if( token->IsSameAs( wxT( "INTERSHEET_REFS" ) ) && Schematic() ) { @@ -1433,7 +1440,7 @@ bool SCH_GLOBALLABEL::ResolveTextVar( wxString* token, int aDepth ) const return true; } - return SCH_LABEL_BASE::ResolveTextVar( token, aDepth ); + return SCH_LABEL_BASE::ResolveTextVar( aPath, token, aDepth ); } diff --git a/eeschema/sch_label.h b/eeschema/sch_label.h index 0f6f7d61e5..516f0508c6 100644 --- a/eeschema/sch_label.h +++ b/eeschema/sch_label.h @@ -126,9 +126,20 @@ public: */ void GetIntersheetRefs( std::vector>* pages ); - virtual bool ResolveTextVar( wxString* token, int aDepth ) const; + /** + * Resolve any references to system tokens supported by the label. + * + * @param aDepth a counter to limit recursion and circular references. + */ + virtual bool ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const; - wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const override; + wxString GetShownText( const SCH_SHEET_PATH* aPath, int aDepth = 0, + bool aAllowExtraText = true ) const override; + + wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const override + { + return GetShownText( nullptr, aDepth, aAllowExtraText ); + } void RunOnChildren( const std::function& aFunction ) override; @@ -353,7 +364,7 @@ public: void CreateGraphicShape( const RENDER_SETTINGS* aRenderSettings, std::vector& aPoints, const VECTOR2I& aPos ) const override; - bool ResolveTextVar( wxString* token, int aDepth ) const override; + bool ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const override; bool IsConnectable() const override { return true; } diff --git a/eeschema/sch_sheet.cpp b/eeschema/sch_sheet.cpp index 2269b2bb59..d0b801d4b4 100644 --- a/eeschema/sch_sheet.cpp +++ b/eeschema/sch_sheet.cpp @@ -234,7 +234,7 @@ void SCH_SHEET::GetContextualTextVars( wxArrayString* aVars ) const } -bool SCH_SHEET::ResolveTextVar( wxString* token, int aDepth ) const +bool SCH_SHEET::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const { if( !Schematic() ) return false; @@ -291,13 +291,13 @@ bool SCH_SHEET::ResolveTextVar( wxString* token, int aDepth ) const // See if parent can resolve it (these will recurse to ancestors) - SCH_SHEET_PATH sheetPath = findSelf(); + SCH_SHEET_PATH sheetPath = aPath ? *aPath : findSelf(); if( sheetPath.size() >= 2 ) { sheetPath.pop_back(); - if( sheetPath.Last()->ResolveTextVar( token, aDepth + 1 ) ) + if( sheetPath.Last()->ResolveTextVar( aPath, token, aDepth + 1 ) ) return true; } else diff --git a/eeschema/sch_sheet.h b/eeschema/sch_sheet.h index 7a4a06ab26..c924e9845b 100644 --- a/eeschema/sch_sheet.h +++ b/eeschema/sch_sheet.h @@ -152,7 +152,11 @@ public: * * @param aDepth is a counter to limit recursion and circular references. */ - bool ResolveTextVar( wxString* token, int aDepth = 0 ) const; + bool ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth = 0 ) const; + bool ResolveTextVar( wxString* token, int aDepth = 0 ) + { + return ResolveTextVar( nullptr, token, aDepth ); + } void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector& aList ) override; diff --git a/eeschema/sch_symbol.cpp b/eeschema/sch_symbol.cpp index 851485ab3d..c37cca18ee 100644 --- a/eeschema/sch_symbol.cpp +++ b/eeschema/sch_symbol.cpp @@ -827,10 +827,10 @@ void SCH_SYMBOL::SetUnitSelection( int aUnitSelection ) } -const wxString SCH_SYMBOL::GetValueFieldText( bool aResolve ) const +const wxString SCH_SYMBOL::GetValueFieldText( bool aResolve, const SCH_SHEET_PATH* aPath ) const { if( aResolve ) - return GetField( VALUE_FIELD )->GetShownText(); + return GetField( VALUE_FIELD )->GetShownText( aPath ); return GetField( VALUE_FIELD )->GetText(); } @@ -1155,7 +1155,7 @@ void SCH_SYMBOL::GetContextualTextVars( wxArrayString* aVars ) const } -bool SCH_SYMBOL::ResolveTextVar( wxString* token, int aDepth ) const +bool SCH_SYMBOL::ResolveTextVar( wxString* token, int aDepth, const SCH_SHEET_PATH* aPath ) const { SCHEMATIC* schematic = Schematic(); @@ -1163,6 +1163,8 @@ bool SCH_SYMBOL::ResolveTextVar( wxString* token, int aDepth ) const if( !schematic ) return false; + SCH_SHEET* sheet = aPath ? aPath->Last() : schematic->CurrentSheet().Last(); + if( token->Contains( ':' ) ) { if( schematic->ResolveCrossReference( token, aDepth + 1 ) ) @@ -1278,9 +1280,9 @@ bool SCH_SYMBOL::ResolveTextVar( wxString* token, int aDepth ) const // See if parent can resolve it (this will recurse to ancestors) - if( SCH_SHEET* sheet = schematic->CurrentSheet().Last() ) + if( sheet ) { - if( sheet->ResolveTextVar( token, aDepth + 1 ) ) + if( sheet->ResolveTextVar( aPath, token, aDepth + 1 ) ) return true; } diff --git a/eeschema/sch_symbol.h b/eeschema/sch_symbol.h index b6b812b4cd..bca97f4344 100644 --- a/eeschema/sch_symbol.h +++ b/eeschema/sch_symbol.h @@ -327,7 +327,8 @@ public: * * @param aDepth a counter to limit recursion and circular references. */ - bool ResolveTextVar( wxString* token, int aDepth = 0 ) const; + bool ResolveTextVar( wxString* token, int aDepth = 0, + const SCH_SHEET_PATH* aPath = nullptr ) const; void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector& aList ) override; @@ -461,7 +462,7 @@ public: m_fields = aFields; // vector copying, length is changed possibly } - const wxString GetValueFieldText( bool aResolve ) const; + const wxString GetValueFieldText( bool aResolve, const SCH_SHEET_PATH* aPath = nullptr ) const; void SetValueFieldText( const wxString& aValue ); const wxString GetFootprintFieldText( bool aResolve ) const; diff --git a/eeschema/sch_text.cpp b/eeschema/sch_text.cpp index ffa67f9493..2f9ea9db15 100644 --- a/eeschema/sch_text.cpp +++ b/eeschema/sch_text.cpp @@ -339,14 +339,21 @@ const BOX2I SCH_TEXT::GetBoundingBox() const } -wxString SCH_TEXT::GetShownText( int aDepth, bool aAllowExtraText ) const +wxString SCH_TEXT::GetShownText( const SCH_SHEET_PATH* aPath, int aDepth, bool aAllowExtraText ) const { + SCH_SHEET* sheet = nullptr; + + if( aPath ) + sheet = aPath->Last(); + else if( Schematic() ) + sheet = Schematic()->CurrentSheet().Last(); + std::function textResolver = [&]( wxString* token ) -> bool { - if( SCH_SHEET* sheet = Schematic()->CurrentSheet().Last() ) + if( sheet ) { - if( sheet->ResolveTextVar( token, aDepth + 1 ) ) + if( sheet->ResolveTextVar( aPath, token, aDepth + 1 ) ) return true; } diff --git a/eeschema/sch_text.h b/eeschema/sch_text.h index c7fbc8faa2..b9d7b6a26f 100644 --- a/eeschema/sch_text.h +++ b/eeschema/sch_text.h @@ -128,7 +128,13 @@ public: return wxT( "SCH_TEXT" ); } - wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const override; + virtual wxString GetShownText( const SCH_SHEET_PATH* aPath, int aDepth = 0, + bool aAllowExtraText = true ) const; + + wxString GetShownText( int aDepth = 0, bool aAllowExtraText = true ) const override + { + return GetShownText( nullptr, aDepth, aAllowExtraText ); + } bool IsHypertext() const override {