Skip to content

Commit 56c8baa

Browse files
committed
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.
1 parent 6649d2b commit 56c8baa

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

python/gui/locator/qgslocatorfilter.sip

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class QgsLocatorResult
5252
Icon for result.
5353
%End
5454

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

162+
static bool stringMatches( const QString &candidate, const QString &search );
163+
%Docstring
164+
Tests a ``candidate`` string to see if it should be considered a match for
165+
a specified ``search`` string.
166+
Filter subclasses should use this method when comparing strings instead
167+
of directly using QString.contains() or Python 'in' checks.
168+
:rtype: bool
169+
%End
170+
162171
signals:
163172

164173
void resultFetched( const QgsLocatorResult &result );

python/plugins/processing/gui/AlgorithmLocatorFilter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def fetchResults(self,string,context,feedback):
5858
if a.flags() & QgsProcessingAlgorithm.FlagHideFromToolbox:
5959
continue
6060

61-
if string.lower() in a.displayName().lower() or [t for t in a.tags() if string.lower() in t.lower()]:
61+
if QgsLocatorFilter.stringMatches(a.displayName(),string) or [t for t in a.tags() if QgsLocatorFilter.stringMatches(t,string)]:
6262
result = QgsLocatorResult()
6363
result.filter = self
6464
result.displayString = a.displayName()
6565
result.icon = a.icon()
6666
result.userData = a.id()
67-
if string.lower() in a.displayName().lower():
67+
if QgsLocatorFilter.stringMatches(a.displayName(),string):
6868
result.score = float(len(string)) / len(a.displayName())
6969
else:
7070
result.score = 0

src/app/locator/qgsinbuiltlocatorfilters.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void QgsLayerTreeLocatorFilter::fetchResults( const QString &string, const QgsLo
4040
if ( feedback->isCanceled() )
4141
return;
4242

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

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

151151
QString searchText = action->text();
152152
searchText.replace( '&', QString() );
153-
if ( searchText.contains( string, Qt::CaseInsensitive ) )
153+
if ( stringMatches( searchText, string ) )
154154
{
155155
QgsLocatorResult result;
156156
result.filter = this;

src/gui/locator/qgslocatorfilter.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717

1818

1919
#include "qgslocatorfilter.h"
20+
#include "qgsstringutils.h"
2021

2122
QgsLocatorFilter::QgsLocatorFilter( QObject *parent )
2223
: QObject( parent )
2324
{
2425

2526
}
2627

27-
bool QgsLocatorFilter::stringMatches( const QString &test, const QString &match )
28+
bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &search )
2829
{
29-
return QgsStringUtils::containsFuzzy( match, test );
30+
return candidate.contains( search, Qt::CaseInsensitive );
3031
}
3132

3233
bool QgsLocatorFilter::useWithoutPrefix() const

src/gui/locator/qgslocatorfilter.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class GUI_EXPORT QgsLocatorResult
4141
/**
4242
* Constructor for QgsLocatorResult.
4343
*/
44-
QgsLocatorResult() = default;
44+
QgsLocatorResult()
45+
: filter( nullptr )
46+
, score( 0.5 )
47+
{}
4548

4649
/**
4750
* Constructor for QgsLocatorResult.
@@ -50,6 +53,7 @@ class GUI_EXPORT QgsLocatorResult
5053
: filter( filter )
5154
, displayString( displayString )
5255
, userData( userData )
56+
, score( 0.5 )
5357
{}
5458

5559
/**
@@ -75,7 +79,7 @@ class GUI_EXPORT QgsLocatorResult
7579
/**
7680
* Match score, from 0 - 1, where 1 represents a perfect match.
7781
*/
78-
double score = 0.5;
82+
double score;
7983

8084
};
8185

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

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

178190
/**

0 commit comments

Comments
 (0)