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

View File

@ -769,12 +769,14 @@ SCH_FIELD* SCH_COMPONENT::GetField( int aFieldNdx ) const
return (SCH_FIELD*) field; return (SCH_FIELD*) field;
} }
wxString SCH_COMPONENT::GetFieldText( wxString aFieldName ) const wxString SCH_COMPONENT::GetFieldText( wxString aFieldName, bool aIncludeDefaultFields ) const
{ {
// Field name for comparison // Field name for comparison
wxString cmpFieldName; wxString cmpFieldName;
if( aIncludeDefaultFields )
{
// Default field names // Default field names
for ( unsigned int i=0; i<MANDATORY_FIELDS; i++) for ( unsigned int i=0; i<MANDATORY_FIELDS; i++)
{ {
@ -785,6 +787,7 @@ wxString SCH_COMPONENT::GetFieldText( wxString aFieldName ) const
return m_Fields[i].GetText(); return m_Fields[i].GetText();
} }
} }
}
// Search custom fields // Search custom fields
for( unsigned int ii=MANDATORY_FIELDS; ii<m_Fields.size(); ii++ ) for( unsigned int ii=MANDATORY_FIELDS; ii<m_Fields.size(); ii++ )
@ -819,14 +822,22 @@ SCH_FIELD* SCH_COMPONENT::AddField( const SCH_FIELD& aField )
return &m_Fields[newNdx]; 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 ) ) if( aFieldName == m_Fields[i].GetName( false ) )
{
return &m_Fields[i]; return &m_Fields[i];
} }
}
return NULL; return NULL;
} }

View File

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