-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
activelayerfeatureslocatorfilter.cpp
440 lines (390 loc) · 15.5 KB
/
activelayerfeatureslocatorfilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/***************************************************************************
activelayerfeatureslocatorfilter.cpp
---------------------
begin : 30.08.2023
copyright : (C) 2023 by Mathieu Pellerin
email : mathieu@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "activelayerfeatureslocatorfilter.h"
#include "featurelistextentcontroller.h"
#include "locatormodelsuperbridge.h"
#include "qgsgeometrywrapper.h"
#include "qgsquickmapsettings.h"
#include <QAction>
#include <qgsexpressioncontextutils.h>
#include <qgsfeedback.h>
#include <qgsmaplayermodel.h>
#include <qgsproject.h>
#include <qgsvectorlayer.h>
#include <math.h>
ActiveLayerFeaturesLocatorFilter::ActiveLayerFeaturesLocatorFilter( LocatorModelSuperBridge *locatorBridge, QObject *parent )
: QgsLocatorFilter( parent )
, mLocatorBridge( locatorBridge )
{
setUseWithoutPrefix( false );
}
ActiveLayerFeaturesLocatorFilter *ActiveLayerFeaturesLocatorFilter::clone() const
{
return new ActiveLayerFeaturesLocatorFilter( mLocatorBridge );
}
QString ActiveLayerFeaturesLocatorFilter::fieldRestriction( QString &searchString, bool *isRestricting )
{
QString _fieldRestriction;
searchString = searchString.trimmed();
if ( isRestricting )
{
*isRestricting = searchString.startsWith( '@' );
}
if ( searchString.startsWith( '@' ) )
{
_fieldRestriction = searchString.left( std::min( searchString.indexOf( ' ' ), searchString.length() ) ).remove( 0, 1 );
searchString = searchString.mid( _fieldRestriction.length() + 2 );
}
return _fieldRestriction;
}
QStringList ActiveLayerFeaturesLocatorFilter::prepare( const QString &string, const QgsLocatorContext &locatorContext )
{
// Normally skip very short search strings, unless when specifically searching using this filter or try to match fields
if ( string.length() < 3 && !locatorContext.usingPrefix && !string.startsWith( '@' ) )
return QStringList();
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mLocatorBridge->activeLayer() );
if ( !layer )
return QStringList();
mLayerIsSpatial = layer->isSpatial();
mDispExpression = QgsExpression( layer->displayExpression() );
mContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
mDispExpression.prepare( &mContext );
// determine if search is restricted to a specific field
QString searchString = string;
bool isRestricting = false;
QString _fieldRestriction = fieldRestriction( searchString, &isRestricting );
bool allowNumeric = false;
double numericalValue = searchString.toDouble( &allowNumeric );
// search in display expression if no field restriction
if ( !isRestricting )
{
QgsFeatureRequest req;
req.setSubsetOfAttributes( qgis::setToList( mDispExpression.referencedAttributeIndexes( layer->fields() ) ) );
if ( !mDispExpression.needsGeometry() )
{
req.setFlags( QgsFeatureRequest::NoGeometry );
}
QString enhancedSearch = searchString;
enhancedSearch.replace( ' ', '%' );
req.setFilterExpression( QStringLiteral( "%1 ILIKE '%%2%'" )
.arg( layer->displayExpression(), enhancedSearch ) );
req.setLimit( mMaxTotalResults );
mDisplayTitleIterator = layer->getFeatures( req );
}
else
{
mDisplayTitleIterator = QgsFeatureIterator();
}
// build up request expression
QStringList expressionParts;
QStringList completionList;
const QgsFields fields = layer->fields();
QgsAttributeList subsetOfAttributes = qgis::setToList( mDispExpression.referencedAttributeIndexes( layer->fields() ) );
for ( const QgsField &field : fields )
{
#if _QGIS_VERSION_INT >= 33300
if ( field.configurationFlags().testFlag( Qgis::FieldConfigurationFlag::NotSearchable ) )
#else
if ( field.configurationFlags().testFlag( QgsField::ConfigurationFlag::NotSearchable ) )
#endif
{
continue;
}
if ( isRestricting )
{
if ( !field.name().startsWith( _fieldRestriction ) )
{
continue;
}
int index = layer->fields().indexFromName( field.name() );
if ( !subsetOfAttributes.contains( index ) )
{
subsetOfAttributes << index;
}
// if we are trying to find a field (and not searching anything yet)
// keep the list of matching fields to display them as results
if ( searchString.isEmpty() && _fieldRestriction != field.name() )
{
mFieldsCompletion << field.name();
}
}
else if ( searchString.isEmpty() )
{
mFieldsCompletion << field.name();
}
// the completion list (returned by the current method) is used by the locator line edit directly
completionList.append( QStringLiteral( "@%1 " ).arg( field.name() ) );
if ( field.type() == QVariant::String )
{
expressionParts << QStringLiteral( "%1 ILIKE '%%2%'" ).arg( QgsExpression::quotedColumnRef( field.name() ), searchString );
}
else if ( allowNumeric && field.isNumeric() )
{
expressionParts << QStringLiteral( "%1 = %2" ).arg( QgsExpression::quotedColumnRef( field.name() ), QString::number( numericalValue, 'g', 17 ) );
}
}
QString expression = QStringLiteral( "(%1)" ).arg( expressionParts.join( QLatin1String( " ) OR ( " ) ) );
QgsFeatureRequest req;
if ( !mDispExpression.needsGeometry() )
{
req.setFlags( QgsFeatureRequest::NoGeometry );
}
req.setFilterExpression( expression );
if ( isRestricting )
{
req.setSubsetOfAttributes( subsetOfAttributes );
}
req.setLimit( mMaxTotalResults );
mFieldIterator = layer->getFeatures( req );
mLayerId = layer->id();
mLayerName = layer->name();
mAttributeAliases.clear();
for ( int idx = 0; idx < layer->fields().size(); ++idx )
{
mAttributeAliases.append( layer->attributeDisplayName( idx ) );
}
return completionList;
}
void ActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
{
QgsFeatureIds featuresFound;
QgsFeature f;
QString searchString = string;
fieldRestriction( searchString );
if ( searchString.trimmed().isEmpty() )
{
// propose available fields for restriction
for ( const QString &field : std::as_const( mFieldsCompletion ) )
{
QgsLocatorResult result;
result.displayString = QStringLiteral( "@%1" ).arg( field );
result.group = mLayerName;
result.description = tr( "Limit the search to the field '%1'" ).arg( field );
#if _QGIS_VERSION_INT >= 33300
result.setUserData( QVariantMap( { { QStringLiteral( "type" ), QVariant::fromValue( ResultType::FieldRestriction ) },
{ QStringLiteral( "search_text" ), QStringLiteral( "%1 @%2 " ).arg( prefix(), field ) } } ) );
#else
result.userData = QVariantMap( { { QStringLiteral( "type" ), QVariant::fromValue( ResultType::FieldRestriction ) },
{ QStringLiteral( "search_text" ), QStringLiteral( "%1 @%2 " ).arg( prefix(), field ) } } );
#endif
result.score = 1;
emit resultFetched( result );
}
return;
}
// search in display title
if ( mDisplayTitleIterator.isValid() )
{
while ( mDisplayTitleIterator.nextFeature( f ) )
{
if ( feedback->isCanceled() )
return;
mContext.setFeature( f );
QgsLocatorResult result;
result.displayString = mDispExpression.evaluate( &mContext ).toString();
result.group = mLayerName;
#if _QGIS_VERSION_INT >= 33300
result.setUserData( QVariantList() << f.id() << mLayerId );
#else
result.userData = QVariantList() << f.id() << mLayerId;
#endif
result.score = static_cast<double>( searchString.length() ) / result.displayString.size();
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form" ), QStringLiteral( "ic_baseline-list_alt-24px" ) );
if ( mLayerIsSpatial )
{
result.actions << QgsLocatorResult::ResultAction( Navigation, tr( "Set feature as destination" ), QStringLiteral( "ic_navigation_flag_purple_24dp" ) );
}
emit resultFetched( result );
featuresFound << f.id();
if ( featuresFound.count() >= mMaxTotalResults )
break;
}
}
// search in fields
while ( mFieldIterator.nextFeature( f ) )
{
if ( feedback->isCanceled() )
return;
// do not display twice the same feature
if ( featuresFound.contains( f.id() ) )
continue;
QgsLocatorResult result;
mContext.setFeature( f );
// find matching field content
int idx = 0;
const QgsAttributes attributes = f.attributes();
for ( const QVariant &var : attributes )
{
QString attrString = var.toString();
if ( attrString.contains( searchString, Qt::CaseInsensitive ) )
{
if ( idx < mAttributeAliases.count() )
{
result.displayString = QStringLiteral( "%1 (%2)" ).arg( attrString, mAttributeAliases[idx] );
}
else
{
result.displayString = attrString;
}
break;
}
idx++;
}
if ( result.displayString.isEmpty() )
continue; //not sure how this result slipped through...
result.group = mLayerName;
result.description = mDispExpression.evaluate( &mContext ).toString();
#if _QGIS_VERSION_INT >= 33300
result.setUserData( QVariantList() << f.id() << mLayerId );
#else
result.userData = QVariantList() << f.id() << mLayerId;
#endif
result.score = static_cast<double>( searchString.length() ) / result.displayString.size();
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form" ), QStringLiteral( "ic_baseline-list_alt-24px" ) );
if ( mLayerIsSpatial )
{
result.actions << QgsLocatorResult::ResultAction( Navigation, tr( "Set feature as destination" ), QStringLiteral( "ic_navigation_flag_purple_24dp" ) );
}
emit resultFetched( result );
featuresFound << f.id();
if ( featuresFound.count() >= mMaxTotalResults )
break;
}
}
void ActiveLayerFeaturesLocatorFilter::triggerResult( const QgsLocatorResult &result )
{
triggerResultFromAction( result, Normal );
}
void ActiveLayerFeaturesLocatorFilter::triggerResultFromAction( const QgsLocatorResult &result, const int actionId )
{
#if _QGIS_VERSION_INT >= 33300
QVariantMap data = result.getUserData().toMap();
#else
QVariantMap data = result.userData.toMap();
#endif
switch ( data.value( QStringLiteral( "type" ) ).value<ResultType>() )
{
case ResultType::FieldRestriction:
{
QTimer::singleShot( 100, [=] { emit mLocatorBridge->requestSearchTextChange( data.value( "search_text" ).toString() ); } );
break;
}
case ResultType::Feature:
{
#if _QGIS_VERSION_INT >= 33300
QVariantList dataList = result.getUserData().toList();
#else
QVariantList dataList = result.userData.toList();
#endif
QgsFeatureId fid = dataList.at( 0 ).toLongLong();
QString layerId = dataList.at( 1 ).toString();
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
if ( !layer )
return;
QgsFeature feature;
QgsFeatureRequest featureRequest = QgsFeatureRequest().setFilterFid( fid );
switch ( actionId )
{
case OpenForm:
{
QMap<QgsVectorLayer *, QgsFeatureRequest> requests;
requests.insert( layer, featureRequest );
mLocatorBridge->featureListController()->model()->setFeatures( requests );
mLocatorBridge->featureListController()->selection()->setFocusedItem( 0 );
mLocatorBridge->featureListController()->requestFeatureFormState();
break;
}
case Navigation:
{
if ( !mLocatorBridge->navigation() )
return;
QgsFeatureIterator it = layer->getFeatures( featureRequest );
it.nextFeature( feature );
if ( feature.hasGeometry() )
{
mLocatorBridge->navigation()->setDestinationFeature( feature, layer );
}
else
{
mLocatorBridge->emitMessage( tr( "Feature has no geometry" ) );
}
break;
}
case Normal:
{
QgsFeatureIterator it = layer->getFeatures( featureRequest.setNoAttributes() );
it.nextFeature( feature );
const QgsGeometry geom = feature.geometry();
if ( geom.isNull() || geom.constGet()->isEmpty() )
{
mLocatorBridge->emitMessage( tr( "Feature has no geometry" ) );
return;
}
QgsRectangle r = mLocatorBridge->mapSettings()->mapSettings().layerExtentToOutputExtent( layer, geom.boundingBox() );
// zoom in if point cannot be distinguished from others
// code taken from QgsMapCanvas::zoomToSelected
if ( !mLocatorBridge->keepScale() )
{
if ( layer->geometryType() == Qgis::GeometryType::Point && r.isEmpty() )
{
int scaleFactor = 5;
const QgsPointXY center = mLocatorBridge->mapSettings()->mapSettings().mapToLayerCoordinates( layer, r.center() );
const QgsRectangle extentRect = mLocatorBridge->mapSettings()->mapSettings().mapToLayerCoordinates( layer, mLocatorBridge->mapSettings()->visibleExtent() ).scaled( 1.0 / scaleFactor, ¢er );
const QgsFeatureRequest pointRequest = QgsFeatureRequest().setFilterRect( extentRect ).setLimit( 1000 ).setNoAttributes();
QgsFeatureIterator fit = layer->getFeatures( pointRequest );
QgsFeature pointFeature;
QgsPointXY closestPoint;
double closestSquaredDistance = pow( extentRect.width() + extentRect.height(), 2.0 );
bool pointFound = false;
while ( fit.nextFeature( pointFeature ) )
{
QgsPointXY point = pointFeature.geometry().asPoint();
double sqrDist = point.sqrDist( center );
if ( sqrDist > closestSquaredDistance || sqrDist < 4 * std::numeric_limits<double>::epsilon() )
continue;
pointFound = true;
closestPoint = point;
closestSquaredDistance = sqrDist;
}
if ( pointFound )
{
// combine selected point with closest point and scale this rect
r.combineExtentWith( mLocatorBridge->mapSettings()->mapSettings().layerToMapCoordinates( layer, closestPoint ) );
const QgsPointXY rCenter = r.center();
r.scale( scaleFactor, &rCenter );
}
}
else if ( !r.isEmpty() )
{
r.scale( 1.25 );
}
}
if ( r.isEmpty() || mLocatorBridge->keepScale() )
{
mLocatorBridge->mapSettings()->setCenter( QgsPoint( r.center() ) );
}
else
{
mLocatorBridge->mapSettings()->setExtent( r );
}
mLocatorBridge->locatorHighlightGeometry()->setProperty( "qgsGeometry", geom );
mLocatorBridge->locatorHighlightGeometry()->setProperty( "crs", layer->crs() );
break;
}
}
break;
}
}
}