Skip to content
Permalink
Browse files

Provide a standard string comparison method in QgsLocatorFilter

Subclasses should use this method instead of directly calling
QString::contains or using Python 's in search' type matches.
This ensures consistent matching behaviour across different filters.
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent 6649d2b commit 56c8baaccbe02e593877537681563fc8d6484e89
@@ -52,7 +52,7 @@ class QgsLocatorResult
Icon for result.
%End

double score = 0.5;
double score;
%Docstring
Match score, from 0 - 1, where 1 represents a perfect match.
%End
@@ -159,6 +159,15 @@ class QgsLocatorFilter : QObject
.. seealso:: useWithoutPrefix()
%End

static bool stringMatches( const QString &candidate, const QString &search );
%Docstring
Tests a ``candidate`` string to see if it should be considered a match for
a specified ``search`` string.
Filter subclasses should use this method when comparing strings instead
of directly using QString.contains() or Python 'in' checks.
:rtype: bool
%End

signals:

void resultFetched( const QgsLocatorResult &result );
@@ -58,13 +58,13 @@ def fetchResults(self,string,context,feedback):
if a.flags() & QgsProcessingAlgorithm.FlagHideFromToolbox:
continue

if string.lower() in a.displayName().lower() or [t for t in a.tags() if string.lower() in t.lower()]:
if QgsLocatorFilter.stringMatches(a.displayName(),string) or [t for t in a.tags() if QgsLocatorFilter.stringMatches(t,string)]:
result = QgsLocatorResult()
result.filter = self
result.displayString = a.displayName()
result.icon = a.icon()
result.userData = a.id()
if string.lower() in a.displayName().lower():
if QgsLocatorFilter.stringMatches(a.displayName(),string):
result.score = float(len(string)) / len(a.displayName())
else:
result.score = 0
@@ -40,7 +40,7 @@ void QgsLayerTreeLocatorFilter::fetchResults( const QString &string, const QgsLo
if ( feedback->isCanceled() )
return;

if ( layer->layer() && layer->layer()->name().contains( string, Qt::CaseInsensitive ) )
if ( layer->layer() && stringMatches( layer->layer()->name(), string ) )
{
QgsLocatorResult result;
result.filter = this;
@@ -75,7 +75,7 @@ void QgsLayoutLocatorFilter::fetchResults( const QString &string, const QgsLocat
if ( feedback->isCanceled() )
return;

if ( composition && composition->name().contains( string, Qt::CaseInsensitive ) )
if ( composition && stringMatches( composition->name(), string ) )
{
QgsLocatorResult result;
result.filter = this;
@@ -150,7 +150,7 @@ void QgsActionLocatorFilter::searchActions( const QString &string, QWidget *pare

QString searchText = action->text();
searchText.replace( '&', QString() );
if ( searchText.contains( string, Qt::CaseInsensitive ) )
if ( stringMatches( searchText, string ) )
{
QgsLocatorResult result;
result.filter = this;
@@ -17,16 +17,17 @@


#include "qgslocatorfilter.h"
#include "qgsstringutils.h"

QgsLocatorFilter::QgsLocatorFilter( QObject *parent )
: QObject( parent )
{

}

bool QgsLocatorFilter::stringMatches( const QString &test, const QString &match )
bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &search )
{
return QgsStringUtils::containsFuzzy( match, test );
return candidate.contains( search, Qt::CaseInsensitive );
}

bool QgsLocatorFilter::useWithoutPrefix() const
@@ -41,7 +41,10 @@ class GUI_EXPORT QgsLocatorResult
/**
* Constructor for QgsLocatorResult.
*/
QgsLocatorResult() = default;
QgsLocatorResult()
: filter( nullptr )
, score( 0.5 )
{}

/**
* Constructor for QgsLocatorResult.
@@ -50,6 +53,7 @@ class GUI_EXPORT QgsLocatorResult
: filter( filter )
, displayString( displayString )
, userData( userData )
, score( 0.5 )
{}

/**
@@ -75,7 +79,7 @@ class GUI_EXPORT QgsLocatorResult
/**
* Match score, from 0 - 1, where 1 represents a perfect match.
*/
double score = 0.5;
double score;

};

@@ -173,6 +177,14 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
*/
void setUseWithoutPrefix( bool useWithoutPrefix );

/**
* Tests a \a candidate string to see if it should be considered a match for
* a specified \a search string.
* Filter subclasses should use this method when comparing strings instead
* of directly using QString::contains() or Python 'in' checks.
*/
static bool stringMatches( const QString &candidate, const QString &search );

signals:

/**

0 comments on commit 56c8baa

Please sign in to comment.