QgsFields::fieldNameIndex also matches field alias

This method is also doing case insensitive "fuzzy" matching now, this
just adds yet another level of tolerance.
This changes the expressions to also take the alias into account if no
matches have been found.
This commit is contained in:
Matthias Kuhn 2016-09-19 14:12:15 +02:00
parent 4b86838909
commit 9a261cfbbc
3 changed files with 39 additions and 7 deletions

View File

@ -481,9 +481,9 @@ int QgsFields::fieldOriginIndex( int fieldIdx ) const
return d->fields[fieldIdx].originIndex;
}
int QgsFields::indexFromName( const QString &name ) const
int QgsFields::indexFromName( const QString &fieldName ) const
{
return d->nameToIndex.value( name, -1 );
return d->nameToIndex.value( fieldName, -1 );
}
QList<QgsField> QgsFields::toList() const
@ -605,6 +605,12 @@ int QgsFields::fieldNameIndex( const QString& fieldName ) const
return idx;
}
for ( int idx = 0; idx < count(); ++idx )
{
if ( QString::compare( d->fields[idx].field.alias(), fieldName, Qt::CaseInsensitive ) == 0 )
return idx;
}
return -1;
}

View File

@ -365,12 +365,32 @@ class CORE_EXPORT QgsFields
//! Get field's origin index (its meaning is specific to each type of origin)
int fieldOriginIndex( int fieldIdx ) const;
//! Look up field's index from name. Returns -1 on error
int indexFromName( const QString& name ) const;
/**
* Look up field's index from the field name.
* This method takes is case sensitive and only matches the data source
* name of the field.
*
* @param fieldName The name of the field.
*
* @return The field index if found or -1 in case it cannot be found.
* @see fieldNameIndex For a more tolerant alternative.
*/
int indexFromName( const QString& fieldName ) const;
//! Look up field's index from name
//! also looks up case-insensitive if there is no match otherwise
//! @note added in 2.4
/**
* Look up field's index from the field name.
* This method matches in the following order:
*
* 1. The exact field name taking case sensitivity into account
* 2. Looks for the field name by case insensitive comparison
* 3. The field alias (case insensitive)
*
* @param fieldName The name to look for.
*
* @return The field index if found or -1 in case it cannot be found.
* @see indexFromName For a more performant and precise but less tolerant alternative.
* @note added in 2.4
*/
int fieldNameIndex( const QString& fieldName ) const;
//! Utility function to get list of attribute indexes

View File

@ -330,6 +330,7 @@ void TestQgsFields::indexFromName()
{
QgsFields fields;
QgsField field( QString( "testfield" ) );
field.setAlias( "testfieldAlias" );
fields.append( field );
QgsField field2( QString( "testfield2" ) );
fields.append( field2 );
@ -351,6 +352,11 @@ void TestQgsFields::indexFromName()
QgsField sameNameDifferentCase( QString( "teStFielD" ) );
fields.append( sameNameDifferentCase );
QCOMPARE( fields.fieldNameIndex( QString( "teStFielD" ) ), 3 );
//test that the alias is only matched with fieldNameIndex
QCOMPARE( fields.indexFromName( "testfieldAlias" ), -1 );
QCOMPARE( fields.fieldNameIndex( "testfieldAlias" ), 0 );
QCOMPARE( fields.fieldNameIndex( "testfieldalias" ), 0 );
}
void TestQgsFields::toList()