@@ -191,6 +191,10 @@ void QgsActionLocatorFilter::searchActions( const QString &string, QWidget *pare
191191 }
192192}
193193
194+ //
195+ // QgsActiveLayerFeaturesLocatorFilter
196+ //
197+
194198QgsActiveLayerFeaturesLocatorFilter::QgsActiveLayerFeaturesLocatorFilter ( QObject *parent )
195199 : QgsLocatorFilter( parent )
196200{
@@ -298,6 +302,108 @@ void QgsActiveLayerFeaturesLocatorFilter::triggerResult( const QgsLocatorResult
298302 QgisApp::instance ()->mapCanvas ()->zoomToFeatureIds ( layer, QgsFeatureIds () << id );
299303}
300304
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 || !layer->flags ().testFlag ( QgsMapLayer::Searchable ) )
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.setSubsetOfAttributes ( expression.referencedAttributeIndexes ( layer->fields () ).toList () );
339+ if ( !expression.needsGeometry () )
340+ req.setFlags ( QgsFeatureRequest::NoGeometry );
341+ req.setFilterExpression ( QStringLiteral ( " %1 ILIKE '%%2%'" )
342+ .arg ( layer->displayExpression () )
343+ .arg ( string ) );
344+ req.setLimit ( 30 );
345+
346+ PreparedLayer preparedLayer;
347+ preparedLayer.expression = expression;
348+ preparedLayer.context = context;
349+ preparedLayer.layerId = layer->id ();
350+ preparedLayer.layerName = layer->name ();
351+ preparedLayer.iterator = layer->getFeatures ( req );
352+ preparedLayer.layerIcon = QgsMapLayerModel::iconForLayer ( layer );
353+
354+ mPreparedLayers .append ( preparedLayer );
355+ }
356+ }
357+
358+ void QgsAllLayersFeaturesLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
359+ {
360+ int foundInCurrentLayer;
361+ int foundInTotal = 0 ;
362+ QgsFeature f;
363+
364+ // we cannot used const loop since iterator::nextFeature is not const
365+ for ( PreparedLayer preparedLayer : mPreparedLayers )
366+ {
367+ foundInCurrentLayer = 0 ;
368+ while ( preparedLayer.iterator .nextFeature ( f ) )
369+ {
370+ if ( feedback->isCanceled () )
371+ return ;
372+
373+ QgsLocatorResult result;
374+ result.group = preparedLayer.layerName ;
375+
376+ preparedLayer.context .setFeature ( f );
377+
378+ result.displayString = preparedLayer.expression .evaluate ( &( preparedLayer.context ) ).toString ();
379+
380+ result.userData = QVariantList () << f.id () << preparedLayer.layerId ;
381+ result.icon = preparedLayer.layerIcon ;
382+ result.score = static_cast < double >( string.length () ) / result.displayString .size ();
383+ emit resultFetched ( result );
384+
385+ foundInCurrentLayer++;
386+ foundInTotal++;
387+ if ( foundInCurrentLayer >= mMaxResultsPerLayer )
388+ break ;
389+ }
390+ if ( foundInTotal >= mMaxTotalResults )
391+ break ;
392+ }
393+ }
394+
395+ void QgsAllLayersFeaturesLocatorFilter::triggerResult ( const QgsLocatorResult &result )
396+ {
397+ QVariantList dataList = result.userData .toList ();
398+ QgsFeatureId id = dataList.at ( 0 ).toLongLong ();
399+ QString layerId = dataList.at ( 1 ).toString ();
400+ QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProject::instance ()->mapLayer ( layerId ) );
401+ if ( !layer )
402+ return ;
403+
404+ QgisApp::instance ()->mapCanvas ()->zoomToFeatureIds ( layer, QgsFeatureIds () << id );
405+ }
406+
301407//
302408// QgsExpressionCalculatorLocatorFilter
303409//
0 commit comments