Fixed duplicate field names

- Now works correctly even if users overload default field names
This commit is contained in:
Oliver Walters 2017-05-23 00:05:36 +10:00 committed by Wayne Stambaugh
parent 2a3a699d98
commit ea855c1abf
3 changed files with 49 additions and 18 deletions

View File

@ -569,7 +569,7 @@ bool BOM_TABLE_COMPONENT::AddUnit( SCH_REFERENCE aUnit )
// User fields
default:
value = cmp->GetFieldText( column->Title() );
value = cmp->GetFieldText( column->Title(), false );
break;
}
@ -705,7 +705,8 @@ void BOM_TABLE_COMPONENT::ApplyFieldChanges()
field = cmp->GetField( DATASHEET );
break;
default:
field = cmp->FindField( column->Title() );
// Find the field by name (but ignore default fields)
field = cmp->FindField( column->Title(), false );
break;
}
@ -985,12 +986,31 @@ void BOM_TABLE_MODEL::AddComponentFields( SCH_COMPONENT* aCmp )
fieldName = field->GetName();
auto existing = ColumnList.GetColumnByTitle( fieldName );
bool userMatchFound = false;
// As columns are sorted by ID, we can allow user to
// create a column with a "special" name
if( existing && existing->Id() >= MANDATORY_FIELDS)
// Search for the column within the existing columns
for( auto col : ColumnList.Columns )
{
if( !col )
{
continue;
}
if( col->Title().Cmp( fieldName ) == 0 )
{
if( col->Id() >= BOM_COL_ID_USER )
{
userMatchFound = true;
break;
}
}
}
// If a user-made column already exists with the same name, abort
if( userMatchFound )
{
continue;
}
ColumnList.AddColumn( new BOM_COLUMN( ColumnList.NextFieldId(),
BOM_COL_TYPE_USER,

View File

@ -769,20 +769,23 @@ SCH_FIELD* SCH_COMPONENT::GetField( int aFieldNdx ) const
return (SCH_FIELD*) field;
}
wxString SCH_COMPONENT::GetFieldText( wxString aFieldName ) const
wxString SCH_COMPONENT::GetFieldText( wxString aFieldName, bool aIncludeDefaultFields ) const
{
// Field name for comparison
wxString cmpFieldName;
// Default field names
for ( unsigned int i=0; i<MANDATORY_FIELDS; i++)
if( aIncludeDefaultFields )
{
cmpFieldName = TEMPLATE_FIELDNAME::GetDefaultFieldName( i );
if( cmpFieldName.Cmp( aFieldName ) == 0 )
// Default field names
for ( unsigned int i=0; i<MANDATORY_FIELDS; i++)
{
return m_Fields[i].GetText();
cmpFieldName = TEMPLATE_FIELDNAME::GetDefaultFieldName( i );
if( cmpFieldName.Cmp( aFieldName ) == 0 )
{
return m_Fields[i].GetText();
}
}
}
@ -819,13 +822,21 @@ SCH_FIELD* SCH_COMPONENT::AddField( const SCH_FIELD& aField )
return &m_Fields[newNdx];
}
SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName )
/*
* Find and return compnent field with the given name
* @aFieldName is the name of the field to search for
* @aIncludeDefaultFields excludes default fields from search if set to false
*/
SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName, bool aIncludeDefaultFields )
{
for( unsigned i = 0; i<m_Fields.size(); ++i )
unsigned start = aIncludeDefaultFields ? 0 : MANDATORY_FIELDS;
for( unsigned i = start; i<m_Fields.size(); ++i )
{
if( aFieldName == m_Fields[i].GetName( false ) )
{
return &m_Fields[i];
}
}
return NULL;

View File

@ -302,7 +302,7 @@ public:
* Returns text associated with a given field (if such a field exists)
* @aFieldName is the name of the field
*/
wxString GetFieldText( wxString aFieldName ) const;
wxString GetFieldText( wxString aFieldName, bool aIncludeDefaultFields = true ) const;
/**
* Function GetFields
@ -325,7 +325,7 @@ public:
* Function FindField
* searches for SCH_FIELD with \a aFieldName and returns it if found, else NULL.
*/
SCH_FIELD* FindField( const wxString& aFieldName );
SCH_FIELD* FindField( const wxString& aFieldName, bool aIncludeDefaultFields = true );
void SetFields( const SCH_FIELDS& aFields )
{