@@ -31,19 +31,20 @@ QgsLayerTreeLocatorFilter::QgsLayerTreeLocatorFilter( QObject *parent )
31
31
: QgsLocatorFilter( parent )
32
32
{}
33
33
34
- void QgsLayerTreeLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
34
+ QgsLayerTreeLocatorFilter *QgsLayerTreeLocatorFilter::clone () const
35
+ {
36
+ return new QgsLayerTreeLocatorFilter ();
37
+ }
38
+
39
+ void QgsLayerTreeLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback * )
35
40
{
36
41
QgsLayerTree *tree = QgsProject::instance ()->layerTreeRoot ();
37
- QList<QgsLayerTreeLayer *> layers = tree->findLayers ();
38
- Q_FOREACH ( QgsLayerTreeLayer *layer, layers )
42
+ const QList<QgsLayerTreeLayer *> layers = tree->findLayers ();
43
+ for ( QgsLayerTreeLayer *layer : layers )
39
44
{
40
- if ( feedback->isCanceled () )
41
- return ;
42
-
43
45
if ( layer->layer () && stringMatches ( layer->layer ()->name (), string ) )
44
46
{
45
47
QgsLocatorResult result;
46
- result.filter = this ;
47
48
result.displayString = layer->layer ()->name ();
48
49
result.userData = layer->layerId ();
49
50
result.icon = QgsMapLayerModel::iconForLayer ( layer->layer () );
@@ -68,18 +69,19 @@ QgsLayoutLocatorFilter::QgsLayoutLocatorFilter( QObject *parent )
68
69
: QgsLocatorFilter( parent )
69
70
{}
70
71
71
- void QgsLayoutLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
72
+ QgsLayoutLocatorFilter *QgsLayoutLocatorFilter::clone () const
73
+ {
74
+ return new QgsLayoutLocatorFilter ();
75
+ }
76
+
77
+ void QgsLayoutLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback * )
72
78
{
73
79
const QList< QgsMasterLayoutInterface * > layouts = QgsProject::instance ()->layoutManager ()->layouts ();
74
80
for ( QgsMasterLayoutInterface *layout : layouts )
75
81
{
76
- if ( feedback->isCanceled () )
77
- return ;
78
-
79
82
if ( layout && stringMatches ( layout->name (), string ) )
80
83
{
81
84
QgsLocatorResult result;
82
- result.filter = this ;
83
85
result.displayString = layout->name ();
84
86
result.userData = layout->name ();
85
87
// result.icon = QgsMapLayerModel::iconForLayer( layer->layer() );
@@ -109,15 +111,20 @@ QgsActionLocatorFilter::QgsActionLocatorFilter( const QList<QWidget *> &parentOb
109
111
setUseWithoutPrefix ( false );
110
112
}
111
113
112
- void QgsActionLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
114
+ QgsActionLocatorFilter * QgsActionLocatorFilter::clone () const
113
115
{
116
+ return new QgsActionLocatorFilter ( mActionParents );
117
+ }
118
+
119
+ void QgsActionLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback * )
120
+ {
121
+ // collect results in main thread, since this method is inexpensive and
122
+ // accessing the gui actions is not thread safe
123
+
114
124
QList<QAction *> found;
115
125
116
- Q_FOREACH ( QWidget *object, mActionParents )
126
+ for ( QWidget *object : qgis::as_const ( mActionParents ) )
117
127
{
118
- if ( feedback->isCanceled () )
119
- return ;
120
-
121
128
searchActions ( string, object, found );
122
129
}
123
130
}
@@ -131,8 +138,8 @@ void QgsActionLocatorFilter::triggerResult( const QgsLocatorResult &result )
131
138
132
139
void QgsActionLocatorFilter::searchActions ( const QString &string, QWidget *parent, QList<QAction *> &found )
133
140
{
134
- QList< QWidget *> children = parent->findChildren <QWidget *>();
135
- Q_FOREACH ( QWidget *widget, children )
141
+ const QList< QWidget *> children = parent->findChildren <QWidget *>();
142
+ for ( QWidget *widget : children )
136
143
{
137
144
searchActions ( string, widget, found );
138
145
}
@@ -154,7 +161,6 @@ void QgsActionLocatorFilter::searchActions( const QString &string, QWidget *pare
154
161
if ( stringMatches ( searchText, string ) )
155
162
{
156
163
QgsLocatorResult result;
157
- result.filter = this ;
158
164
result.displayString = searchText;
159
165
result.userData = QVariant::fromValue ( action );
160
166
result.icon = action->icon ();
@@ -171,7 +177,12 @@ QgsActiveLayerFeaturesLocatorFilter::QgsActiveLayerFeaturesLocatorFilter( QObjec
171
177
setUseWithoutPrefix ( false );
172
178
}
173
179
174
- void QgsActiveLayerFeaturesLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
180
+ QgsActiveLayerFeaturesLocatorFilter *QgsActiveLayerFeaturesLocatorFilter::clone () const
181
+ {
182
+ return new QgsActiveLayerFeaturesLocatorFilter ();
183
+ }
184
+
185
+ void QgsActiveLayerFeaturesLocatorFilter::prepare ( const QString &string, const QgsLocatorContext & )
175
186
{
176
187
if ( string.length () < 3 )
177
188
return ;
@@ -183,11 +194,9 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
183
194
if ( !layer )
184
195
return ;
185
196
186
- int found = 0 ;
187
- QgsExpression dispExpression ( layer->displayExpression () );
188
- QgsExpressionContext context;
189
- context.appendScopes ( QgsExpressionContextUtils::globalProjectLayerScopes ( layer ) );
190
- dispExpression.prepare ( &context );
197
+ mDispExpression = QgsExpression ( layer->displayExpression () );
198
+ mContext .appendScopes ( QgsExpressionContextUtils::globalProjectLayerScopes ( layer ) );
199
+ mDispExpression .prepare ( &mContext );
191
200
192
201
// build up request expression
193
202
QStringList expressionParts;
@@ -211,17 +220,25 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
211
220
req.setFlags ( QgsFeatureRequest::NoGeometry );
212
221
req.setFilterExpression ( expression );
213
222
req.setLimit ( 30 );
223
+ mIterator = layer->getFeatures ( req );
224
+
225
+ mLayerId = layer->id ();
226
+ mLayerIcon = QgsMapLayerModel::iconForLayer ( layer );
227
+ }
228
+
229
+ void QgsActiveLayerFeaturesLocatorFilter::fetchResults ( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
230
+ {
231
+ int found = 0 ;
214
232
QgsFeature f;
215
- QgsFeatureIterator it = layer-> getFeatures ( req );
216
- while ( it .nextFeature ( f ) )
233
+
234
+ while ( mIterator .nextFeature ( f ) )
217
235
{
218
236
if ( feedback->isCanceled () )
219
237
return ;
220
238
221
239
QgsLocatorResult result;
222
- result.filter = this ;
223
240
224
- context .setFeature ( f );
241
+ mContext .setFeature ( f );
225
242
226
243
// find matching field content
227
244
Q_FOREACH ( const QVariant &var, f.attributes () )
@@ -236,10 +253,10 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
236
253
if ( result.displayString .isEmpty () )
237
254
continue ; // not sure how this result slipped through...
238
255
239
- result.description = dispExpression .evaluate ( &context ).toString ();
256
+ result.description = mDispExpression .evaluate ( &mContext ).toString ();
240
257
241
- result.userData = QVariantList () << f.id () << layer-> id () ;
242
- result.icon = QgsMapLayerModel::iconForLayer ( layer ) ;
258
+ result.userData = QVariantList () << f.id () << mLayerId ;
259
+ result.icon = mLayerIcon ;
243
260
result.score = static_cast < double >( string.length () ) / result.displayString .size ();
244
261
emit resultFetched ( result );
245
262
0 commit comments