28
28
#include " qgslayoutmanager.h"
29
29
#include " qgsmapcanvas.h"
30
30
#include " qgsfeatureaction.h"
31
+ #include " qgsvectorlayerfeatureiterator.h"
32
+
31
33
32
34
QgsLayerTreeLocatorFilter::QgsLayerTreeLocatorFilter ( QObject *parent )
33
35
: QgsLocatorFilter( parent )
@@ -343,6 +345,7 @@ void QgsAllLayersFeaturesLocatorFilter::prepare( const QString &string, const Qg
343
345
if ( string.length () < 3 && !context.usingPrefix )
344
346
return ;
345
347
348
+ mPreparedLayers .clear ();
346
349
const QMap<QString, QgsMapLayer *> layers = QgsProject::instance ()->mapLayers ();
347
350
for ( auto it = layers.constBegin (); it != layers.constEnd (); ++it )
348
351
{
@@ -360,17 +363,17 @@ void QgsAllLayersFeaturesLocatorFilter::prepare( const QString &string, const Qg
360
363
if ( !expression.needsGeometry () )
361
364
req.setFlags ( QgsFeatureRequest::NoGeometry );
362
365
req.setFilterExpression ( QStringLiteral ( " %1 ILIKE '%%2%'" )
363
- .arg ( layer->displayExpression (),
364
- string ) );
366
+ .arg ( layer->displayExpression (), string ) );
365
367
req.setLimit ( 30 );
366
368
367
- PreparedLayer preparedLayer;
368
- preparedLayer.expression = expression;
369
- preparedLayer.context = context;
370
- preparedLayer.layerId = layer->id ();
371
- preparedLayer.layerName = layer->name ();
372
- preparedLayer.iterator = layer->getFeatures ( req );
373
- preparedLayer.layerIcon = QgsMapLayerModel::iconForLayer ( layer );
369
+ std::shared_ptr<PreparedLayer> preparedLayer ( new PreparedLayer () );
370
+ preparedLayer->expression = expression;
371
+ preparedLayer->context = context;
372
+ preparedLayer->layerId = layer->id ();
373
+ preparedLayer->layerName = layer->name ();
374
+ preparedLayer->featureSource .reset ( new QgsVectorLayerFeatureSource ( layer ) );
375
+ preparedLayer->request = req;
376
+ preparedLayer->layerIcon = QgsMapLayerModel::iconForLayer ( layer );
374
377
375
378
mPreparedLayers .append ( preparedLayer );
376
379
}
@@ -383,23 +386,24 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
383
386
QgsFeature f;
384
387
385
388
// we cannot used const loop since iterator::nextFeature is not const
386
- for ( PreparedLayer preparedLayer : mPreparedLayers )
389
+ for ( auto preparedLayer : qgis::as_const ( mPreparedLayers ) )
387
390
{
388
391
foundInCurrentLayer = 0 ;
389
- while ( preparedLayer.iterator .nextFeature ( f ) )
392
+ QgsFeatureIterator it = preparedLayer->featureSource ->getFeatures ( preparedLayer->request );
393
+ while ( it.nextFeature ( f ) )
390
394
{
391
395
if ( feedback->isCanceled () )
392
396
return ;
393
397
394
398
QgsLocatorResult result;
395
- result.group = preparedLayer. layerName ;
399
+ result.group = preparedLayer-> layerName ;
396
400
397
- preparedLayer. context .setFeature ( f );
401
+ preparedLayer-> context .setFeature ( f );
398
402
399
- result.displayString = preparedLayer. expression .evaluate ( &( preparedLayer. context ) ).toString ();
403
+ result.displayString = preparedLayer-> expression .evaluate ( &( preparedLayer-> context ) ).toString ();
400
404
401
- result.userData = QVariantList () << f.id () << preparedLayer. layerId ;
402
- result.icon = preparedLayer. layerIcon ;
405
+ result.userData = QVariantList () << f.id () << preparedLayer-> layerId ;
406
+ result.icon = preparedLayer-> layerIcon ;
403
407
result.score = static_cast < double >( string.length () ) / result.displayString .size ();
404
408
405
409
result.actions << QgsLocatorResult::ResultAction ( OpenForm, tr ( " Open form…" ) );
0 commit comments