From 9092dd0aecc0505b4cb35df82c4e6467bdaf33f3 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Tue, 31 May 2011 18:29:14 +0200 Subject: [PATCH] Try to fix bug 639683 : Illegal reference strings are now detected and replaced by default prefix for old schematics or rejected in schematic edition. --- .../dialog_edit_component_in_schematic.cpp | 8 ++- .../dialog_edit_libentry_fields_in_lib.cpp | 8 +++ eeschema/edit_component_in_schematic.cpp | 13 +++-- eeschema/libfield.cpp | 12 ++++- eeschema/sch_component.cpp | 51 ++++++++++++++++--- eeschema/sch_component.h | 14 ++++- 6 files changed, 94 insertions(+), 12 deletions(-) diff --git a/eeschema/dialogs/dialog_edit_component_in_schematic.cpp b/eeschema/dialogs/dialog_edit_component_in_schematic.cpp index 7018f73313..d920cf6b07 100644 --- a/eeschema/dialogs/dialog_edit_component_in_schematic.cpp +++ b/eeschema/dialogs/dialog_edit_component_in_schematic.cpp @@ -225,7 +225,13 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::OnOKButtonClick( wxCommandEvent& event if( !copyPanelToSelectedField() ) return; - // save old cmp in undo list if not already in edit, or moving ... + if( ! SCH_COMPONENT::IsReferenceStringValid( m_FieldsBuf[REFERENCE].m_Text ) ) + { + DisplayError( NULL, _( "Illegal reference. A reference must start by a letter" ) ); + return; + } + + // save old cmp in undo list if not already in edit, or moving ... if( m_Cmp->m_Flags == 0 ) m_Parent->SaveCopyInUndoList( m_Cmp, UR_CHANGED ); diff --git a/eeschema/dialogs/dialog_edit_libentry_fields_in_lib.cpp b/eeschema/dialogs/dialog_edit_libentry_fields_in_lib.cpp index e2b2af1239..48ff15aacf 100644 --- a/eeschema/dialogs/dialog_edit_libentry_fields_in_lib.cpp +++ b/eeschema/dialogs/dialog_edit_libentry_fields_in_lib.cpp @@ -16,6 +16,7 @@ #include "protos.h" #include "libeditframe.h" #include "class_library.h" +#include "sch_component.h" #include "sch_field.h" #include "template_fieldnames.h" #include "dialog_helpers.h" @@ -221,6 +222,13 @@ void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnOKButtonClick( wxCommandEvent& event if( !copyPanelToSelectedField() ) return; + // test if reference prefix is acceptable + if( ! SCH_COMPONENT::IsReferenceStringValid( m_FieldsBuf[REFERENCE].m_Text ) ) + { + DisplayError( NULL, _( "Illegal reference prefix. A reference must start by a letter" ) ); + return; + } + /* Note: this code is now (2010-dec-04) not used, because the value field is no more editable * because changing the value is equivalent to create a new component or alias. * This is now handled in libedit main frame, and no more in this dialog diff --git a/eeschema/edit_component_in_schematic.cpp b/eeschema/edit_component_in_schematic.cpp index d19c7580a9..1ed9c1c2dd 100644 --- a/eeschema/edit_component_in_schematic.cpp +++ b/eeschema/edit_component_in_schematic.cpp @@ -179,11 +179,18 @@ create a new power component with the new value." ), GetChars( entry->GetName() aField->m_Size.x = aField->m_Size.y = m_TextFieldSize; } - aField->m_Text = newtext; - if( fieldNdx == REFERENCE ) { - component->SetRef( GetSheet(), newtext ); + // Test is reference is acceptable: + if( SCH_COMPONENT::IsReferenceStringValid( newtext ) ) + { + component->SetRef( GetSheet(), newtext ); + aField->m_Text = newtext; + } + else + { + DisplayError( this, _( "Illegal reference string! No change" ) ); + } } } else diff --git a/eeschema/libfield.cpp b/eeschema/libfield.cpp index 38f61edad8..26c640192b 100644 --- a/eeschema/libfield.cpp +++ b/eeschema/libfield.cpp @@ -10,7 +10,8 @@ #include "class_sch_screen.h" #include "general.h" -#include "protos.h" +//#include "protos.h" +#include "sch_component.h" #include "libeditframe.h" #include "class_library.h" #include "template_fieldnames.h" @@ -51,6 +52,7 @@ void LIB_EDIT_FRAME::EditField( wxDC* DC, LIB_FIELD* aField ) text.Replace( wxT( " " ), wxT( "_" ) ); + // Perform some controls: if( ( aField->GetId() == REFERENCE || aField->GetId() == VALUE ) && text.IsEmpty ( ) ) { title.Printf( _( "A %s field cannot be empty." ), GetChars(aField->GetName().Lower() ) ); @@ -58,6 +60,14 @@ void LIB_EDIT_FRAME::EditField( wxDC* DC, LIB_FIELD* aField ) return; } + // Ensure the reference prefix is acceptable: + if( ( aField->GetId() == REFERENCE ) && + ! SCH_COMPONENT::IsReferenceStringValid( text ) ) + { + DisplayError( this, _( "Illegal reference. A reference must start by a letter" ) ); + return; + } + wxString fieldText = aField->GetFullText( m_unit ); /* If the value field is changed, this is equivalent to creating a new component from diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp index e2ab7c938e..c88b65f62a 100644 --- a/eeschema/sch_component.cpp +++ b/eeschema/sch_component.cpp @@ -369,6 +369,32 @@ const wxString SCH_COMPONENT::GetRef( SCH_SHEET_PATH* sheet ) } +/* Function IsReferenceStringValid (static function) + * Tests for an acceptable reference string + * An acceptable reference string must support unannotation + * i.e starts by letter + * returns true if OK + */ +bool SCH_COMPONENT::IsReferenceStringValid( const wxString & aReferenceString ) +{ + wxString text = aReferenceString; + bool ok = true; + + // Try to unannotate this reference + while( !text.IsEmpty() && + ( text.Last() == '?' || isdigit( text.Last() ) ) ) + text.RemoveLast(); + + if( text.IsEmpty() ) + ok = false; + + // Add here other constraints + // Currently:no other constraint + + return ok; +} + + void SCH_COMPONENT::SetRef( SCH_SHEET_PATH* sheet, const wxString& ref ) { wxString path = GetPath( sheet ); @@ -417,9 +443,13 @@ void SCH_COMPONENT::SetRef( SCH_SHEET_PATH* sheet, const wxString& ref ) // Reinit the m_prefix member if needed wxString prefix = ref; - - while( prefix.Last() == '?' or isdigit( prefix.Last() ) ) - prefix.RemoveLast(); + if( IsReferenceStringValid( prefix ) ) + { + while( prefix.Last() == '?' || isdigit( prefix.Last() ) ) + prefix.RemoveLast(); + } + else + prefix = wxT("U"); // Set to default ref prefix if( m_prefix != prefix ) m_prefix = prefix; @@ -598,7 +628,6 @@ void SCH_COMPONENT::Place( SCH_EDIT_FRAME* frame, wxDC* DC ) void SCH_COMPONENT::ClearAnnotation( SCH_SHEET_PATH* aSheetPath ) { - wxString defRef = m_prefix; bool keepMulti = false; LIB_COMPONENT* Entry; static const wxString separators( wxT( " " ) ); @@ -609,8 +638,18 @@ void SCH_COMPONENT::ClearAnnotation( SCH_SHEET_PATH* aSheetPath ) if( Entry && Entry->UnitsLocked() ) keepMulti = true; - while( defRef.Last() == '?' ) - defRef.RemoveLast(); + // Build a reference with no annotation, + // i.e. a reference ended by only one '?' + wxString defRef = m_prefix; + if( IsReferenceStringValid( defRef ) ) + { + while( defRef.Last() == '?' ) + defRef.RemoveLast(); + } + else + { // This is a malformed reference: reinit this reference + m_prefix = defRef = wxT("U"); // Set to default ref prefix + } defRef.Append( wxT( "?" ) ); diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h index 190c39f223..ae544a86cb 100644 --- a/eeschema/sch_component.h +++ b/eeschema/sch_component.h @@ -261,13 +261,25 @@ public: // returns a unique ID, in the form of a path. wxString GetPath( SCH_SHEET_PATH* sheet ); + /** + * Function IsReferenceStringValid (static) + * Tests for an acceptable reference string + * An acceptable reference string must support unannotation + * i.e starts by letter + * @param aReferenceString = the reference string to validate + * @return true if OK + */ + static bool IsReferenceStringValid( const wxString &aReferenceString ); + /** * Function GetRef * returns the reference, for the given sheet path. */ const wxString GetRef( SCH_SHEET_PATH* sheet ); - // Set the reference, for the given sheet path. + /** + * Set the reference, for the given sheet path. + */ void SetRef( SCH_SHEET_PATH* sheet, const wxString& ref ); /**