Skip to content
Permalink
Browse files

fix completion + add test

  • Loading branch information
3nids committed Sep 22, 2020
1 parent a509491 commit d6f9b07cf170d72acb2cc35c9c9d364a431c4eb4
@@ -244,12 +244,13 @@ QgsActiveLayerFeaturesLocatorFilter *QgsActiveLayerFeaturesLocatorFilter::clone(
return new QgsActiveLayerFeaturesLocatorFilter();
}

QString QgsActiveLayerFeaturesLocatorFilter::fieldRestriction( QString &searchString ) const
QString QgsActiveLayerFeaturesLocatorFilter::fieldRestriction( QString &searchString )
{
QString _fieldRestriction;
searchString = searchString.trimmed();
if ( searchString.startsWith( '@' ) )
{
_fieldRestriction = searchString.left( std::max( searchString.indexOf( ' ' ), 0 ) ).remove( 0, 1 );
_fieldRestriction = searchString.left( std::min( searchString.indexOf( ' ' ), searchString.length() ) ).remove( 0, 1 );
searchString = searchString.mid( _fieldRestriction.length() + 2 );
}
return _fieldRestriction;
@@ -318,6 +319,7 @@ QStringList QgsActiveLayerFeaturesLocatorFilter::prepare( const QString &string,
}

completionList.append( QStringLiteral( "@%1 " ).arg( field.name() ) );

if ( field.type() == QVariant::String )
{
expressionParts << QStringLiteral( "%1 ILIKE '%%2%'" ).arg( QgsExpression::quotedColumnRef( field.name() ),
@@ -114,7 +114,7 @@ class APP_EXPORT QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
* Returns the field restriction if defined (starting with @)
* The \a searchString is modified accordingly by removing the field restriction
*/
QString fieldRestriction( QString &searchString ) const;
static QString fieldRestriction( QString &searchString );

QgsExpression mDispExpression;
QgsExpressionContext mContext;
@@ -124,6 +124,8 @@ class APP_EXPORT QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
QIcon mLayerIcon;
QStringList mAttributeAliases;
int mMaxTotalResults = 30;

friend class TestQgsAppLocatorFilters;
};

class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
@@ -37,6 +37,8 @@ class TestQgsAppLocatorFilters : public QObject
void testLayers();
void testLayouts();
void testSearchActiveLayer();
void testActiveLayerFieldRestriction();
void testActiveLayerCompletion();
void testSearchAllLayers();
void testSearchAllLayersPrioritizeExactMatch();
void testGoto();
@@ -160,8 +162,11 @@ void TestQgsAppLocatorFilters::testSearchActiveLayer()
QgsFeature f;
f.setAttributes( QVector<QVariant>() << 1001 << "A nice feature" << 1234567890 << 12345.6789 );
f.setGeometry( QgsGeometry::fromWkt( "Point (-71.123 78.23)" ) );

vl->dataProvider()->addFeature( f );
QgsFeature f2;
f2.setAttributes( QVector<QVariant>() << 100 << "@home" << 13579 << 13.57 );
f2.setGeometry( QgsGeometry::fromWkt( "Point (-71.223 78.33)" ) );
vl->dataProvider()->addFeature( f2 );

mQgisApp->setActiveLayer( vl );

@@ -198,6 +203,39 @@ void TestQgsAppLocatorFilters::testSearchActiveLayer()
results = gatherResults( &filter, QStringLiteral( "nice .678" ), context );
QCOMPARE( results.count(), 1 );

results = gatherResults( &filter, QStringLiteral( "@my_text @home" ), context );
QCOMPARE( results.count(), 1 );

QgsProject::instance()->removeAllMapLayers();
}

void TestQgsAppLocatorFilters::testActiveLayerFieldRestriction()
{
QString search = QStringLiteral( "@my_field search" );
QString restr = QgsActiveLayerFeaturesLocatorFilter::fieldRestriction( search );
QCOMPARE( restr, QStringLiteral( "my_field" ) );
QCOMPARE( search, QStringLiteral( "search" ) );

search = QStringLiteral( "@home" );
restr = QgsActiveLayerFeaturesLocatorFilter::fieldRestriction( search );
QCOMPARE( restr, QStringLiteral( "home" ) );
QCOMPARE( search, QStringLiteral( "" ) );
}

void TestQgsAppLocatorFilters::testActiveLayerCompletion()
{
QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=my_integer:integer&field=my_double:double&key=pk" );
QgsVectorLayer *vl = new QgsVectorLayer( layerDef, QStringLiteral( "Layer" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( vl );
mQgisApp->setActiveLayer( vl );

QgsFeedback f;
QgsActiveLayerFeaturesLocatorFilter filter;
QgsLocatorContext context;
context.usingPrefix = true;

QCOMPARE( filter.prepare( QStringLiteral( "" ), context ), QStringList( { "@pk ", "@my_text ", "@my_integer ", "@my_double " } ) );
QCOMPARE( filter.prepare( QStringLiteral( "@my_i" ), context ), QStringList( { "@my_integer " } ) );

QgsProject::instance()->removeAllMapLayers();
}

0 comments on commit d6f9b07

Please sign in to comment.
You can’t perform that action at this time.