Skip to content

Commit

Permalink
Provide a standard string comparison method in QgsLocatorFilter
Browse files Browse the repository at this point in the history
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 56c8baa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
11 changes: 10 additions & 1 deletion python/gui/locator/qgslocatorfilter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/AlgorithmLocatorFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/app/locator/qgsinbuiltlocatorfilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/gui/locator/qgslocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/gui/locator/qgslocatorfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class GUI_EXPORT QgsLocatorResult
/**
* Constructor for QgsLocatorResult.
*/
QgsLocatorResult() = default;
QgsLocatorResult()
: filter( nullptr )
, score( 0.5 )
{}

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

/**
Expand All @@ -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;

};

Expand Down Expand Up @@ -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:

/**
Expand Down

0 comments on commit 56c8baa

Please sign in to comment.