@@ -180,55 +180,62 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
180180 if ( !layer )
181181 return ;
182182
183- int i = 0 ;
184183 int found = 0 ;
185184 QgsExpression dispExpression ( layer->displayExpression () );
186185 QgsExpressionContext context;
187186 context.appendScopes ( QgsExpressionContextUtils::globalProjectLayerScopes ( layer ) );
188187 dispExpression.prepare ( &context );
189188
189+ // build up request expression
190+ QStringList expressionParts;
190191 Q_FOREACH ( const QgsField &field, layer->fields () )
191192 {
192- if ( feedback->isCanceled () )
193- return ;
194-
195193 QString exp = QStringLiteral ( " %1 ILIKE '%%2%'" ).arg ( QgsExpression::quotedColumnRef ( field.name () ),
196194 string );
197- QgsFeatureRequest req;
198- req.setFlags ( QgsFeatureRequest::NoGeometry );
199- QStringList attrs;
200- attrs << field.name ();
201- attrs.append ( dispExpression.referencedColumns ().toList () );
202- req.setSubsetOfAttributes ( attrs, layer->fields () );
203- req.setFilterExpression ( exp );
204- req.setLimit ( 30 );
205- QgsFeature f;
206- QgsFeatureIterator it = layer->getFeatures ( req );
207- while ( it.nextFeature ( f ) )
208- {
209- if ( feedback->isCanceled () )
210- return ;
195+ expressionParts << exp;
196+ }
211197
212- QgsLocatorResult result;
213- result.filter = this ;
198+ QString expression = QStringLiteral ( " (%1)" ).arg ( expressionParts.join ( QStringLiteral ( " ) OR ( " ) ) );
214199
215- context.setFeature ( f );
216- result.displayString = dispExpression.evaluate ( &context ).toString ();
217- if ( result.displayString .isEmpty () )
218- result.displayString = f.attribute ( i ).toString ();
219- else
220- result.description = f.attribute ( i ).toString ();
200+ QgsFeatureRequest req;
201+ req.setFlags ( QgsFeatureRequest::NoGeometry );
202+ req.setFilterExpression ( expression );
203+ req.setLimit ( 30 );
204+ QgsFeature f;
205+ QgsFeatureIterator it = layer->getFeatures ( req );
206+ while ( it.nextFeature ( f ) )
207+ {
208+ if ( feedback->isCanceled () )
209+ return ;
221210
222- result.userData = f.id ();
223- result.icon = QgsMapLayerModel::iconForLayer ( layer );
224- result.score = static_cast < double >( string.length () ) / f.attribute ( i ).toString ().size ();
225- emit resultFetched ( result );
211+ QgsLocatorResult result;
212+ result.filter = this ;
226213
227- found++;
228- if ( found >= 30 )
229- return ;
214+ context.setFeature ( f );
215+
216+ // find matching field content
217+ Q_FOREACH ( const QVariant &var, f.attributes () )
218+ {
219+ QString attrString = var.toString ();
220+ if ( attrString.contains ( string, Qt::CaseInsensitive ) )
221+ {
222+ result.displayString = attrString;
223+ break ;
224+ }
230225 }
231- i++;
226+ if ( result.displayString .isEmpty () )
227+ continue ; // not sure how this result slipped through...
228+
229+ result.description = dispExpression.evaluate ( &context ).toString ();
230+
231+ result.userData = f.id ();
232+ result.icon = QgsMapLayerModel::iconForLayer ( layer );
233+ result.score = static_cast < double >( string.length () ) / result.displayString .size ();
234+ emit resultFetched ( result );
235+
236+ found++;
237+ if ( found >= 30 )
238+ return ;
232239 }
233240}
234241
0 commit comments