Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-kuhn committed Oct 1, 2016
1 parent 4b86838 commit 9a261cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/core/qgsfield.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
30 changes: 25 additions & 5 deletions src/core/qgsfield.h
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsfields.cpp
Expand Up @@ -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 );
Expand All @@ -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()
Expand Down

0 comments on commit 9a261cf

Please sign in to comment.