@@ -191,6 +191,10 @@ void QgsActionLocatorFilter::searchActions( const QString &string, QWidget *pare
191
191
}
192
192
}
193
193
194
+ //
195
+ // QgsActiveLayerFeaturesLocatorFilter
196
+ //
197
+
194
198
QgsActiveLayerFeaturesLocatorFilter::QgsActiveLayerFeaturesLocatorFilter ( QObject *parent )
195
199
: QgsLocatorFilter( parent )
196
200
{
@@ -298,6 +302,106 @@ void QgsActiveLayerFeaturesLocatorFilter::triggerResult( const QgsLocatorResult
298
302
QgisApp::instance ()->mapCanvas ()->zoomToFeatureIds ( layer, QgsFeatureIds () << id );
299
303
}
300
304
305
+ //
306
+ // QgsAllLayersFeaturesLocatorFilter
307
+ //
308
+
309
+ QgsAllLayersFeaturesLocatorFilter::QgsAllLayersFeaturesLocatorFilter ( QObject *parent )
310
+ : QgsLocatorFilter( parent )
311
+ {
312
+ setUseWithoutPrefix ( false );
313
+ }
314
+
315
+ QgsAllLayersFeaturesLocatorFilter *QgsAllLayersFeaturesLocatorFilter::clone () const
316
+ {
317
+ return new QgsAllLayersFeaturesLocatorFilter ();
318
+ }
319
+
320
+ void QgsAllLayersFeaturesLocatorFilter::prepare ( const QString &string, const QgsLocatorContext & )
321
+ {
322
+ if ( string.length () < 3 )
323
+ return ;
324
+
325
+ const QMap<QString, QgsMapLayer *> layers = QgsProject::instance ()->mapLayers ();
326
+ for ( auto it = layers.constBegin (); it != layers.constEnd (); ++it )
327
+ {
328
+ QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( it.value () );
329
+ if ( !layer )
330
+ continue ;
331
+
332
+ QgsExpression expression ( layer->displayExpression () );
333
+ QgsExpressionContext context;
334
+ context.appendScopes ( QgsExpressionContextUtils::globalProjectLayerScopes ( layer ) );
335
+ expression.prepare ( &context );
336
+
337
+ QgsFeatureRequest req;
338
+ req.setFlags ( QgsFeatureRequest::NoGeometry );
339
+ req.setFilterExpression ( QStringLiteral ( " %1 ILIKE '%%2%'" )
340
+ .arg ( layer->displayExpression () )
341
+ .arg ( string ) );
342
+ req.setLimit ( 30 );
343
+
344
+ PreparedLayer preparedLayer;
345
+ preparedLayer.expression = expression;
346
+ preparedLayer.context = context;
347
+ preparedLayer.layerId = layer->id ();
348
+ preparedLayer.layerName = layer->name ();
349
+ preparedLayer.iterator = layer->getFeatures ( req );
350
+ preparedLayer.layerIcon = QgsMapLayerModel::iconForLayer ( layer );
351
+
352
+ mPreparedLayers .append ( preparedLayer );
353
+ }
354
+ }
355
+
356
+ void QgsAllLayersFeaturesLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
357
+ {
358
+ int foundInCurrentLayer;
359
+ int foundInTotal = 0 ;
360
+ QgsFeature f;
361
+
362
+ // we cannot used const loop since iterator::nextFeature is not const
363
+ for ( PreparedLayer preparedLayer : mPreparedLayers )
364
+ {
365
+ foundInCurrentLayer = 0 ;
366
+ while ( preparedLayer.iterator .nextFeature ( f ) )
367
+ {
368
+ if ( feedback->isCanceled () )
369
+ return ;
370
+
371
+ QgsLocatorResult result;
372
+ result.group = preparedLayer.layerName ;
373
+
374
+ preparedLayer.context .setFeature ( f );
375
+
376
+ result.displayString = preparedLayer.expression .evaluate ( &( preparedLayer.context ) ).toString ();
377
+
378
+ result.userData = QVariantList () << f.id () << preparedLayer.layerId ;
379
+ result.icon = preparedLayer.layerIcon ;
380
+ result.score = static_cast < double >( string.length () ) / result.displayString .size ();
381
+ emit resultFetched ( result );
382
+
383
+ foundInCurrentLayer++;
384
+ foundInTotal++;
385
+ if ( foundInCurrentLayer >= mMaxResultsPerLayer )
386
+ break ;
387
+ }
388
+ if ( foundInTotal >= mMaxTotalResults )
389
+ break ;
390
+ }
391
+ }
392
+
393
+ void QgsAllLayersFeaturesLocatorFilter::triggerResult ( const QgsLocatorResult &result )
394
+ {
395
+ QVariantList dataList = result.userData .toList ();
396
+ QgsFeatureId id = dataList.at ( 0 ).toLongLong ();
397
+ QString layerId = dataList.at ( 1 ).toString ();
398
+ QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProject::instance ()->mapLayer ( layerId ) );
399
+ if ( !layer )
400
+ return ;
401
+
402
+ QgisApp::instance ()->mapCanvas ()->zoomToFeatureIds ( layer, QgsFeatureIds () << id );
403
+ }
404
+
301
405
//
302
406
// QgsExpressionCalculatorLocatorFilter
303
407
//
0 commit comments