Skip to content

Commit 6b8cd4b

Browse files
authored
[locator] show field where text has been found in current layer filter (#7904)
* [locator] show field where text has been found also fix an issue that a field was not returning a match for big number (e/f formats) * init layer tree model when testing QgisApp * add test for inbuilt active layer locator filter * use string comparison for numerical value this is how it is done in attribute table filter and seems to have better result than comparing numerical values * switch back to numerical precision and cleanup layers
1 parent 622fe8c commit 6b8cd4b

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

src/app/locator/qgsinbuiltlocatorfilters.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void QgsActiveLayerFeaturesLocatorFilter::prepare( const QString &string, const
212212
return;
213213

214214
bool allowNumeric = false;
215-
double numericValue = string.toDouble( &allowNumeric );
215+
double numericalValue = string.toDouble( &allowNumeric );
216216

217217
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgisApp::instance()->activeLayer() );
218218
if ( !layer )
@@ -234,7 +234,7 @@ void QgsActiveLayerFeaturesLocatorFilter::prepare( const QString &string, const
234234
}
235235
else if ( allowNumeric && field.isNumeric() )
236236
{
237-
expressionParts << QStringLiteral( "%1 = %2" ).arg( QgsExpression::quotedColumnRef( field.name() ) ).arg( numericValue );
237+
expressionParts << QStringLiteral( "%1 = %2" ).arg( QgsExpression::quotedColumnRef( field.name() ) ).arg( QString::number( numericalValue, 'g', 17 ) );
238238
}
239239
}
240240

@@ -248,6 +248,11 @@ void QgsActiveLayerFeaturesLocatorFilter::prepare( const QString &string, const
248248

249249
mLayerId = layer->id();
250250
mLayerIcon = QgsMapLayerModel::iconForLayer( layer );
251+
mAttributeAliases.clear();
252+
for ( int idx = 0; idx < layer->fields().size(); ++idx )
253+
{
254+
mAttributeAliases.append( layer->attributeDisplayName( idx ) );
255+
}
251256
}
252257

253258
void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &, QgsFeedback *feedback )
@@ -265,14 +270,20 @@ void QgsActiveLayerFeaturesLocatorFilter::fetchResults( const QString &string, c
265270
mContext.setFeature( f );
266271

267272
// find matching field content
268-
Q_FOREACH ( const QVariant &var, f.attributes() )
273+
int idx = 0;
274+
const QgsAttributes attributes = f.attributes();
275+
for ( const QVariant &var : attributes )
269276
{
270277
QString attrString = var.toString();
271278
if ( attrString.contains( string, Qt::CaseInsensitive ) )
272279
{
273-
result.displayString = attrString;
280+
if ( idx < mAttributeAliases.count() )
281+
result.displayString = QString( "%1 (%2)" ).arg( attrString, mAttributeAliases[idx] );
282+
else
283+
result.displayString = attrString;
274284
break;
275285
}
286+
idx++;
276287
}
277288
if ( result.displayString.isEmpty() )
278289
continue; //not sure how this result slipped through...

src/app/locator/qgsinbuiltlocatorfilters.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef QGSINBUILTLOCATORFILTERS_H
1919
#define QGSINBUILTLOCATORFILTERS_H
2020

21-
#include "qgisapp.h"
21+
#include "qgis_app.h"
2222
#include "qgslocatorfilter.h"
2323
#include "qgsexpressioncontext.h"
2424
#include "qgsfeatureiterator.h"
@@ -63,7 +63,7 @@ class APP_EXPORT QgsLayoutLocatorFilter : public QgsLocatorFilter
6363

6464
};
6565

66-
class QgsActionLocatorFilter : public QgsLocatorFilter
66+
class APP_EXPORT QgsActionLocatorFilter : public QgsLocatorFilter
6767
{
6868
Q_OBJECT
6969

@@ -87,7 +87,7 @@ class QgsActionLocatorFilter : public QgsLocatorFilter
8787

8888
};
8989

90-
class QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
90+
class APP_EXPORT QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
9191
{
9292
Q_OBJECT
9393

@@ -111,6 +111,7 @@ class QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
111111
QgsFeatureIterator mIterator;
112112
QString mLayerId;
113113
QIcon mLayerIcon;
114+
QStringList mAttributeAliases;
114115
};
115116

116117
class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
@@ -167,7 +168,7 @@ class APP_EXPORT QgsExpressionCalculatorLocatorFilter : public QgsLocatorFilter
167168
};
168169

169170

170-
class QgsBookmarkLocatorFilter : public QgsLocatorFilter
171+
class APP_EXPORT QgsBookmarkLocatorFilter : public QgsLocatorFilter
171172
{
172173
Q_OBJECT
173174

@@ -185,7 +186,7 @@ class QgsBookmarkLocatorFilter : public QgsLocatorFilter
185186
void triggerResult( const QgsLocatorResult &result ) override;
186187
};
187188

188-
class QgsSettingsLocatorFilter : public QgsLocatorFilter
189+
class APP_EXPORT QgsSettingsLocatorFilter : public QgsLocatorFilter
189190
{
190191
Q_OBJECT
191192

src/app/qgisapp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,8 @@ QgisApp::QgisApp()
14331433
connect( mMapCanvas, &QgsMapCanvas::messageEmitted, this, &QgisApp::displayMessage );
14341434
mMapCanvas->freeze();
14351435
mLayerTreeView = new QgsLayerTreeView( this );
1436+
QgsLayerTreeModel *model = new QgsLayerTreeModel( QgsProject::instance()->layerTreeRoot(), this );
1437+
mLayerTreeView->setModel( model );
14361438
mUndoWidget = new QgsUndoWidget( nullptr, mMapCanvas );
14371439
mUserInputDockWidget = new QgsUserInputWidget( this );
14381440
mInfoBar = new QgsMessageBar( centralWidget() );

src/core/qgsvectorlayer.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,13 +2791,7 @@ QString QgsVectorLayer::attributeDisplayName( int index ) const
27912791

27922792
QgsStringMap QgsVectorLayer::attributeAliases() const
27932793
{
2794-
QgsStringMap map;
2795-
Q_FOREACH ( const QgsField &field, fields() )
2796-
{
2797-
if ( !field.alias().isEmpty() )
2798-
map.insert( field.name(), field.alias() );
2799-
}
2800-
return map;
2794+
return mAttributeAliasMap;
28012795
}
28022796

28032797
bool QgsVectorLayer::deleteAttribute( int index )

tests/src/app/testqgsapplocatorfilters.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestQgsAppLocatorFilters : public QObject
3636
void testCalculator();
3737
void testLayers();
3838
void testLayouts();
39+
void testSearchActiveLayer();
3940
void testSearchAllLayers();
4041

4142
private:
@@ -148,6 +149,38 @@ void TestQgsAppLocatorFilters::testLayouts()
148149
QCOMPARE( results.at( 2 ).userData.toString(), pl3->name() );
149150
}
150151

152+
void TestQgsAppLocatorFilters::testSearchActiveLayer()
153+
{
154+
QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=my_integer:integer&field=my_double:double&key=pk" );
155+
QgsVectorLayer *vl = new QgsVectorLayer( layerDef, QStringLiteral( "Layer" ), QStringLiteral( "memory" ) );
156+
QgsProject::instance()->addMapLayer( vl );
157+
158+
QgsFeature f;
159+
f.setAttributes( QVector<QVariant>() << 1001 << "A nice feature" << 1234567890 << 12345.6789 );
160+
f.setGeometry( QgsGeometry::fromWkt( "Point (-71.123 78.23)" ) );
161+
162+
vl->dataProvider()->addFeature( f );
163+
164+
mQgisApp->setActiveLayer( vl );
165+
166+
QgsActiveLayerFeaturesLocatorFilter filter;
167+
QgsLocatorContext context;
168+
169+
QList< QgsLocatorResult > results = gatherResults( &filter, QStringLiteral( "12345.6789" ), context );
170+
QCOMPARE( results.count(), 1 );
171+
172+
results = gatherResults( &filter, QStringLiteral( "12345.67" ), context );
173+
QCOMPARE( results.count(), 0 );
174+
175+
results = gatherResults( &filter, QStringLiteral( "1234567890" ), context );
176+
QCOMPARE( results.count(), 1 );
177+
178+
results = gatherResults( &filter, QStringLiteral( "nice" ), context );
179+
QCOMPARE( results.count(), 1 );
180+
181+
QgsProject::instance()->removeAllMapLayers();
182+
}
183+
151184
void TestQgsAppLocatorFilters::testSearchAllLayers()
152185
{
153186
QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=my_number:integer&key=pk" );
@@ -173,7 +206,6 @@ void TestQgsAppLocatorFilters::testSearchAllLayers()
173206
QgsLocatorContext context;
174207

175208
QList< QgsLocatorResult > results = gatherResults( &filter, QStringLiteral( "100" ), context );
176-
QCOMPARE( results.count(), 2 );
177209

178210
l1->setDisplayExpression( QStringLiteral( "\"my_text\" || ' is ' || \"my_number\"" ) );
179211
l2->setDisplayExpression( QStringLiteral( "\"my_text\" || ' is ' || \"my_number\"" ) );
@@ -185,6 +217,8 @@ void TestQgsAppLocatorFilters::testSearchAllLayers()
185217

186218
results = gatherResults( &filter, QStringLiteral( "feature is 6789" ), context );
187219
QCOMPARE( results.count(), 1 );
220+
221+
QgsProject::instance()->removeAllMapLayers();
188222
}
189223

190224
QList<QgsLocatorResult> TestQgsAppLocatorFilters::gatherResults( QgsLocatorFilter *filter, const QString &string, const QgsLocatorContext &context )

0 commit comments

Comments
 (0)