diff --git a/eeschema/dialogs/dialog_edit_label.cpp b/eeschema/dialogs/dialog_edit_label.cpp index 5dd74f58ab..04baa496ba 100644 --- a/eeschema/dialogs/dialog_edit_label.cpp +++ b/eeschema/dialogs/dialog_edit_label.cpp @@ -143,117 +143,6 @@ DIALOG_LABEL_EDITOR::~DIALOG_LABEL_EDITOR() } -wxString DIALOG_LABEL_EDITOR::convertKIIDsToReferences( const wxString& aSource ) const -{ - wxString newbuf; - size_t sourceLen = aSource.length(); - - for( size_t i = 0; i < sourceLen; ++i ) - { - if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' ) - { - wxString token; - bool isCrossRef = false; - - for( i = i + 2; i < sourceLen; ++i ) - { - if( aSource[i] == '}' ) - break; - - if( aSource[i] == ':' ) - isCrossRef = true; - - token.append( aSource[i] ); - } - - if( isCrossRef ) - { - SCH_SHEET_LIST sheetList = m_Parent->Schematic().GetSheets(); - wxString remainder; - wxString ref = token.BeforeFirst( ':', &remainder ); - - SCH_SHEET_PATH refSheetPath; - SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &refSheetPath ); - - if( refItem && refItem->Type() == SCH_COMPONENT_T ) - { - SCH_COMPONENT* refComponent = static_cast( refItem ); - token = refComponent->GetRef( &refSheetPath, true ) + ":" + remainder; - } - } - - newbuf.append( "${" + token + "}" ); - } - else - { - newbuf.append( aSource[i] ); - } - } - - return newbuf; -} - - -wxString DIALOG_LABEL_EDITOR::convertReferencesToKIIDs( const wxString& aSource ) const -{ - wxString newbuf; - size_t sourceLen = aSource.length(); - - for( size_t i = 0; i < sourceLen; ++i ) - { - if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' ) - { - wxString token; - bool isCrossRef = false; - - for( i = i + 2; i < sourceLen; ++i ) - { - if( aSource[i] == '}' ) - break; - - if( aSource[i] == ':' ) - isCrossRef = true; - - token.append( aSource[i] ); - } - - if( isCrossRef ) - { - SCH_SHEET_LIST sheets = m_Parent->Schematic().GetSheets(); - wxString remainder; - wxString ref = token.BeforeFirst( ':', &remainder ); - SCH_REFERENCE_LIST references; - - sheets.GetComponents( references ); - - for( size_t jj = 0; jj < references.GetCount(); jj++ ) - { - SCH_COMPONENT* refComponent = references[ jj ].GetComp(); - - if( ref == refComponent->GetRef( &references[ jj ].GetSheetPath(), true ) ) - { - wxString test( remainder ); - - if( refComponent->ResolveTextVar( &test ) ) - token = refComponent->m_Uuid.AsString() + ":" + remainder; - - break; - } - } - } - - newbuf.append( "${" + token + "}" ); - } - else - { - newbuf.append( aSource[i] ); - } - } - - return newbuf; -} - - bool DIALOG_LABEL_EDITOR::TransferDataToWindow() { if( !wxDialog::TransferDataToWindow() ) @@ -261,8 +150,10 @@ bool DIALOG_LABEL_EDITOR::TransferDataToWindow() if( m_CurrentText->Type() == SCH_TEXT_T ) { + SCHEMATIC& schematic = m_Parent->Schematic(); + // show text variable cross-references in a human-readable format - m_valueMultiLine->SetValue( convertKIIDsToReferences( m_CurrentText->GetText() ) ); + m_valueMultiLine->SetValue( schematic.ConvertKIIDsToRefs( m_CurrentText->GetText() ) ); } else { @@ -407,7 +298,7 @@ bool DIALOG_LABEL_EDITOR::TransferDataFromWindow() if( m_CurrentText->Type() == SCH_TEXT_T ) { // convert any text variable cross-references to their UUIDs - text = convertReferencesToKIIDs( m_valueMultiLine->GetValue() ); + text = m_Parent->Schematic().ConvertRefsToKIIDs( m_valueMultiLine->GetValue() ); } else { diff --git a/eeschema/dialogs/dialog_edit_label.h b/eeschema/dialogs/dialog_edit_label.h index ee8f5bdc62..c36db76357 100644 --- a/eeschema/dialogs/dialog_edit_label.h +++ b/eeschema/dialogs/dialog_edit_label.h @@ -71,10 +71,6 @@ private: bool TransferDataToWindow() override; bool TransferDataFromWindow() override; - wxString convertKIIDsToReferences( const wxString& aSource ) const; - - wxString convertReferencesToKIIDs( const wxString& aSource ) const; - SCH_EDIT_FRAME* m_Parent; SCH_TEXT* m_CurrentText; wxWindow* m_activeTextCtrl; diff --git a/eeschema/dialogs/dialog_edit_one_field.cpp b/eeschema/dialogs/dialog_edit_one_field.cpp index e87eae68cb..b72d7d97c8 100644 --- a/eeschema/dialogs/dialog_edit_one_field.cpp +++ b/eeschema/dialogs/dialog_edit_one_field.cpp @@ -287,6 +287,9 @@ DIALOG_SCH_EDIT_ONE_FIELD::DIALOG_SCH_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, } } + // show text variable cross-references in a human-readable format + m_text = aField->Schematic()->ConvertKIIDsToRefs( aField->GetText() ); + m_isPower = false; m_textLabel->SetLabel( m_field->GetName() + ":" ); @@ -410,6 +413,9 @@ void DIALOG_SCH_EDIT_ONE_FIELD::UpdateField( SCH_FIELD* aField, SCH_SHEET_PATH* if( aField->GetVertJustify() != EDA_TEXT::MapVertJustify( m_verticalJustification - 1 ) ) positioningModified = true; + // convert any text variable cross-references to their UUIDs + m_text = aField->Schematic()->ConvertRefsToKIIDs( m_text ); + aField->SetText( m_text ); updateText( aField ); diff --git a/eeschema/sch_field.cpp b/eeschema/sch_field.cpp index 7ce5a6761b..705c7befd5 100644 --- a/eeschema/sch_field.cpp +++ b/eeschema/sch_field.cpp @@ -104,16 +104,24 @@ wxString SCH_FIELD::GetShownText( int aDepth ) const std::function symbolResolver = [&]( wxString* token ) -> bool { - SCH_COMPONENT* component = static_cast( m_Parent ); + if( token->Contains( ':' ) ) + { + if( Schematic()->ResolveCrossReference( token, aDepth ) ) + return true; + } + else + { + SCH_COMPONENT* component = static_cast( m_Parent ); - if( component->ResolveTextVar( token, aDepth + 1 ) ) - return true; + if( component->ResolveTextVar( token, aDepth + 1 ) ) + return true; - SCHEMATIC* schematic = component->Schematic(); - SCH_SHEET* sheet = schematic ? schematic->CurrentSheet().Last() : nullptr; + SCHEMATIC* schematic = component->Schematic(); + SCH_SHEET* sheet = schematic ? schematic->CurrentSheet().Last() : nullptr; - if( sheet && sheet->ResolveTextVar( token, aDepth + 1 ) ) - return true; + if( sheet && sheet->ResolveTextVar( token, aDepth + 1 ) ) + return true; + } return false; }; diff --git a/eeschema/sch_text.cpp b/eeschema/sch_text.cpp index 443ef4d428..353015c3b0 100644 --- a/eeschema/sch_text.cpp +++ b/eeschema/sch_text.cpp @@ -507,35 +507,8 @@ wxString SCH_TEXT::GetShownText( int aDepth ) const { if( token->Contains( ':' ) ) { - wxCHECK_MSG( Schematic(), wxEmptyString, - "No parent SCHEMATIC set for SCH_TEXT!" ); - - SCH_SHEET_LIST sheetList = Schematic()->GetSheets(); - wxString remainder; - wxString ref = token->BeforeFirst( ':', &remainder ); - SCH_SHEET_PATH dummy; - SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &dummy ); - - if( refItem && refItem->Type() == SCH_COMPONENT_T ) - { - SCH_COMPONENT* refComponent = static_cast( refItem ); - - if( refComponent->ResolveTextVar( &remainder, aDepth + 1 ) ) - { - *token = remainder; - return true; - } - } - else if( refItem && refItem->Type() == SCH_SHEET_T ) - { - SCH_SHEET* refSheet = static_cast( refItem ); - - if( refSheet->ResolveTextVar( &remainder, aDepth + 1 ) ) - { - *token = remainder; - return true; - } - } + if( Schematic()->ResolveCrossReference( token, aDepth ) ) + return true; } else { diff --git a/eeschema/schematic.cpp b/eeschema/schematic.cpp index e2dbcbd38f..63b4baf723 100644 --- a/eeschema/schematic.cpp +++ b/eeschema/schematic.cpp @@ -240,3 +240,147 @@ std::vector SCHEMATIC::GetNetClassAssignmentCandidates() } +bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const +{ + SCH_SHEET_LIST sheetList = GetSheets(); + wxString remainder; + wxString ref = token->BeforeFirst( ':', &remainder ); + SCH_SHEET_PATH dummy; + SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &dummy ); + + if( refItem && refItem->Type() == SCH_COMPONENT_T ) + { + SCH_COMPONENT* refComponent = static_cast( refItem ); + + if( refComponent->ResolveTextVar( &remainder, aDepth + 1 ) ) + { + *token = remainder; + return true; + } + } + else if( refItem && refItem->Type() == SCH_SHEET_T ) + { + SCH_SHEET* refSheet = static_cast( refItem ); + + if( refSheet->ResolveTextVar( &remainder, aDepth + 1 ) ) + { + *token = remainder; + return true; + } + } + + return false; +} + + +wxString SCHEMATIC::ConvertRefsToKIIDs( const wxString& aSource ) const +{ + wxString newbuf; + size_t sourceLen = aSource.length(); + + for( size_t i = 0; i < sourceLen; ++i ) + { + if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' ) + { + wxString token; + bool isCrossRef = false; + + for( i = i + 2; i < sourceLen; ++i ) + { + if( aSource[i] == '}' ) + break; + + if( aSource[i] == ':' ) + isCrossRef = true; + + token.append( aSource[i] ); + } + + if( isCrossRef ) + { + SCH_SHEET_LIST sheetList = GetSheets(); + wxString remainder; + wxString ref = token.BeforeFirst( ':', &remainder ); + SCH_REFERENCE_LIST references; + + sheetList.GetComponents( references ); + + for( size_t jj = 0; jj < references.GetCount(); jj++ ) + { + SCH_COMPONENT* refComponent = references[ jj ].GetComp(); + + if( ref == refComponent->GetRef( &references[ jj ].GetSheetPath(), true ) ) + { + wxString test( remainder ); + + if( refComponent->ResolveTextVar( &test ) ) + token = refComponent->m_Uuid.AsString() + ":" + remainder; + + break; + } + } + } + + newbuf.append( "${" + token + "}" ); + } + else + { + newbuf.append( aSource[i] ); + } + } + + return newbuf; +} + + +wxString SCHEMATIC::ConvertKIIDsToRefs( const wxString& aSource ) const +{ + wxString newbuf; + size_t sourceLen = aSource.length(); + + for( size_t i = 0; i < sourceLen; ++i ) + { + if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' ) + { + wxString token; + bool isCrossRef = false; + + for( i = i + 2; i < sourceLen; ++i ) + { + if( aSource[i] == '}' ) + break; + + if( aSource[i] == ':' ) + isCrossRef = true; + + token.append( aSource[i] ); + } + + if( isCrossRef ) + { + SCH_SHEET_LIST sheetList = GetSheets(); + wxString remainder; + wxString ref = token.BeforeFirst( ':', &remainder ); + + SCH_SHEET_PATH refSheetPath; + SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &refSheetPath ); + + if( refItem && refItem->Type() == SCH_COMPONENT_T ) + { + SCH_COMPONENT* refComponent = static_cast( refItem ); + token = refComponent->GetRef( &refSheetPath, true ) + ":" + remainder; + } + } + + newbuf.append( "${" + token + "}" ); + } + else + { + newbuf.append( aSource[i] ); + } + } + + return newbuf; +} + + diff --git a/eeschema/schematic.h b/eeschema/schematic.h index 7c72505ba0..15608e6226 100644 --- a/eeschema/schematic.h +++ b/eeschema/schematic.h @@ -151,6 +151,17 @@ public: */ std::vector GetNetClassAssignmentCandidates(); + /** + * Resolves text vars that refer to other items. + * Note that the actual resolve is delegated to the symbol/sheet in question. This routine + * just does the look-up and delegation. + */ + bool ResolveCrossReference( wxString* token, int aDepth ) const; + + wxString ConvertRefsToKIIDs( const wxString& aSource ) const; + wxString ConvertKIIDsToRefs( const wxString& aSource ) const; + + #if defined(DEBUG) void Show( int nestLevel, std::ostream& os ) const override {} #endif