Skip to content
Permalink
Browse files

[locator] active locator filter also search in display title

  • Loading branch information
3nids committed Sep 21, 2020
1 parent 0f2a834 commit 2ae418da24616b1f1fb809796f17d0d9d599f566
Showing with 49 additions and 5 deletions.
  1. +47 −4 src/app/locator/qgsinbuiltlocatorfilters.cpp
  2. +2 −1 src/app/locator/qgsinbuiltlocatorfilters.h
@@ -278,6 +278,25 @@ QStringList QgsActiveLayerFeaturesLocatorFilter::prepare( const QString &string,
bool allowNumeric = false;
double numericalValue = searchString.toDouble( &allowNumeric );

// search in display expression if no field restriction
if ( _fieldRestriction.isEmpty() )
{
QgsFeatureRequest req;
req.setSubsetOfAttributes( qgis::setToList( mDispExpression.referencedAttributeIndexes( layer->fields() ) ) );
if ( !mDispExpression.needsGeometry() )
req.setFlags( QgsFeatureRequest::NoGeometry );
QString enhancedSearch = searchString;
enhancedSearch.replace( ' ', '%' );
req.setFilterExpression( QStringLiteral( "%1 ILIKE '%%2%'" )
.arg( layer->displayExpression(), enhancedSearch ) );
req.setLimit( mMaxTotalResults );
mDisplayTitleIterator = layer->getFeatures( req );
}
else
{
mDisplayTitleIterator = QgsFeatureIterator();
}

// build up request expression
QStringList expressionParts;
QStringList completionList;
@@ -319,8 +338,8 @@ QStringList QgsActiveLayerFeaturesLocatorFilter::prepare( const QString &string,
if ( !_fieldRestriction.isEmpty() )
req.setSubsetOfAttributes( subsetOfAttributes );

req.setLimit( 30 );
mIterator = layer->getFeatures( req );
req.setLimit( mMaxTotalResults );
mFieldIterator = layer->getFeatures( req );

mLayerId = layer->id();
mLayerIcon = QgsMapLayerModel::iconForLayer( layer );
@@ -340,7 +359,32 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
QString searchString = string;
fieldRestriction( searchString );

while ( mIterator.nextFeature( f ) )
// search in display title
if ( mDisplayTitleIterator.isValid() )
{
while ( mDisplayTitleIterator.nextFeature( f ) )
{
if ( feedback->isCanceled() )
return;

mContext.setFeature( f );

QgsLocatorResult result;

result.displayString = mDispExpression.evaluate( &mContext ).toString();
result.userData = QVariantList() << f.id() << mLayerId;
result.icon = mLayerIcon;
result.score = static_cast< double >( searchString.length() ) / result.displayString.size();
emit resultFetched( result );

found++;
if ( found >= mMaxTotalResults )
break;
}
}

// search in fields
while ( mFieldIterator.nextFeature( f ) )
{
if ( feedback->isCanceled() )
return;
@@ -369,7 +413,6 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
continue; //not sure how this result slipped through...

result.description = mDispExpression.evaluate( &mContext ).toString();

result.userData = QVariantList() << f.id() << mLayerId;
result.icon = mLayerIcon;
result.score = static_cast< double >( searchString.length() ) / result.displayString.size();
@@ -118,7 +118,8 @@ class APP_EXPORT QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter

QgsExpression mDispExpression;
QgsExpressionContext mContext;
QgsFeatureIterator mIterator;
QgsFeatureIterator mDisplayTitleIterator;
QgsFeatureIterator mFieldIterator;
QString mLayerId;
QIcon mLayerIcon;
QStringList mAttributeAliases;

0 comments on commit 2ae418d

Please sign in to comment.
You can’t perform that action at this time.