Skip to content

Commit

Permalink
[locator] Prefer exact match
Browse files Browse the repository at this point in the history
Fix #35418
  • Loading branch information
m-kuhn authored and nyalldawson committed Mar 30, 2020
1 parent 38ecf8f commit 8c2a301
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/app/locator/qgsinbuiltlocatorfilters.cpp
Expand Up @@ -386,7 +386,12 @@ void QgsAllLayersFeaturesLocatorFilter::prepare( const QString &string, const Qg
enhancedSearch.replace( ' ', '%' );
req.setFilterExpression( QStringLiteral( "%1 ILIKE '%%2%'" )
.arg( layer->displayExpression(), enhancedSearch ) );
req.setLimit( 30 );
req.setLimit( 6 );

QgsFeatureRequest exactMatchRequest = req;
exactMatchRequest.setFilterExpression( QStringLiteral( "%1 ILIKE '%2'" )
.arg( layer->displayExpression(), enhancedSearch ) );
exactMatchRequest.setLimit( 10 );

std::shared_ptr<PreparedLayer> preparedLayer( new PreparedLayer() );
preparedLayer->expression = expression;
Expand All @@ -395,6 +400,7 @@ void QgsAllLayersFeaturesLocatorFilter::prepare( const QString &string, const Qg
preparedLayer->layerName = layer->name();
preparedLayer->featureSource.reset( new QgsVectorLayerFeatureSource( layer ) );
preparedLayer->request = req;
preparedLayer->exactMatchRequest = exactMatchRequest;
preparedLayer->layerIcon = QgsMapLayerModel::iconForLayer( layer );

mPreparedLayers.append( preparedLayer );
Expand All @@ -411,12 +417,47 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
for ( auto preparedLayer : qgis::as_const( mPreparedLayers ) )
{
foundInCurrentLayer = 0;

QgsFeatureIds foundFeatureIds;

QgsFeatureIterator exactMatchIt = preparedLayer->featureSource->getFeatures( preparedLayer->exactMatchRequest );
while ( exactMatchIt.nextFeature( f ) )
{
if ( feedback->isCanceled() )
return;

QgsLocatorResult result;
result.group = preparedLayer->layerName;

preparedLayer->context.setFeature( f );

result.displayString = preparedLayer->expression.evaluate( &( preparedLayer->context ) ).toString();

result.userData = QVariantList() << f.id() << preparedLayer->layerId;
foundFeatureIds << f.id();
result.icon = preparedLayer->layerIcon;
result.score = static_cast< double >( string.length() ) / result.displayString.size();

result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form…" ) );
emit resultFetched( result );

foundInCurrentLayer++;
foundInTotal++;
if ( foundInCurrentLayer >= mMaxResultsPerLayer )
break;
}
if ( foundInTotal >= mMaxTotalResults )
break;

QgsFeatureIterator it = preparedLayer->featureSource->getFeatures( preparedLayer->request );
while ( it.nextFeature( f ) )
{
if ( feedback->isCanceled() )
return;

if ( foundFeatureIds.contains( f.id() ) )
continue;

QgsLocatorResult result;
result.group = preparedLayer->layerName;

Expand Down
1 change: 1 addition & 0 deletions src/app/locator/qgsinbuiltlocatorfilters.h
Expand Up @@ -134,6 +134,7 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
QgsExpressionContext context;
std::unique_ptr<QgsVectorLayerFeatureSource> featureSource;
QgsFeatureRequest request;
QgsFeatureRequest exactMatchRequest;
QString layerName;
QString layerId;
QIcon layerIcon;
Expand Down

0 comments on commit 8c2a301

Please sign in to comment.