Skip to content

Commit

Permalink
Fix some locator filters show results when no string is entered
Browse files Browse the repository at this point in the history
and filter prefix is not used

(cherry-picked from ebab64)
  • Loading branch information
nyalldawson committed Apr 6, 2018
1 parent ca73205 commit e9794c3
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions python/core/locator/qgslocatorcontext.sip.in
Expand Up @@ -31,6 +31,8 @@ Constructor for QgsLocatorContext.

QgsCoordinateReferenceSystem targetExtentCrs;

bool usingPrefix;

};


Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/gui/AlgorithmLocatorFilter.py
Expand Up @@ -65,7 +65,8 @@ def fetchResults(self, string, context, feedback):
if a.flags() & QgsProcessingAlgorithm.FlagHideFromToolbox:
continue

if QgsLocatorFilter.stringMatches(a.displayName(), string) or [t for t in a.tags() if QgsLocatorFilter.stringMatches(t, string)]:
if QgsLocatorFilter.stringMatches(a.displayName(), string) or [t for t in a.tags() if QgsLocatorFilter.stringMatches(t, string)] or \
(context.usingPrefix and not string):
result = QgsLocatorResult()
result.filter = self
result.displayString = a.displayName()
Expand Down
8 changes: 4 additions & 4 deletions src/app/locator/qgsinbuiltlocatorfilters.cpp
Expand Up @@ -36,13 +36,13 @@ QgsLayerTreeLocatorFilter *QgsLayerTreeLocatorFilter::clone() const
return new QgsLayerTreeLocatorFilter();
}

void QgsLayerTreeLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &, QgsFeedback * )
void QgsLayerTreeLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback * )
{
QgsLayerTree *tree = QgsProject::instance()->layerTreeRoot();
const QList<QgsLayerTreeLayer *> layers = tree->findLayers();
for ( QgsLayerTreeLayer *layer : layers )
{
if ( layer->layer() && stringMatches( layer->layer()->name(), string ) )
if ( layer->layer() && ( stringMatches( layer->layer()->name(), string ) || ( context.usingPrefix && string.isEmpty() ) ) )
{
QgsLocatorResult result;
result.displayString = layer->layer()->name();
Expand Down Expand Up @@ -74,12 +74,12 @@ QgsLayoutLocatorFilter *QgsLayoutLocatorFilter::clone() const
return new QgsLayoutLocatorFilter();
}

void QgsLayoutLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &, QgsFeedback * )
void QgsLayoutLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback * )
{
const QList< QgsMasterLayoutInterface * > layouts = QgsProject::instance()->layoutManager()->layouts();
for ( QgsMasterLayoutInterface *layout : layouts )
{
if ( layout && stringMatches( layout->name(), string ) )
if ( layout && ( stringMatches( layout->name(), string ) || ( context.usingPrefix && string.isEmpty() ) ) )
{
QgsLocatorResult result;
result.displayString = layout->name();
Expand Down
4 changes: 3 additions & 1 deletion src/core/locator/qgslocator.cpp
Expand Up @@ -81,8 +81,9 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter )
filter->setUseWithoutPrefix( byDefault );
}

void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback )
void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c, QgsFeedback *feedback )
{
QgsLocatorContext context( c );
// ideally this should not be required, as well behaved callers
// will NOT fire up a new fetchResults call while an existing one is
// operating/waiting to be canceled...
Expand Down Expand Up @@ -111,6 +112,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
activeFilters << mPrefixedFilters.value( prefix );
searchString = searchString.mid( prefix.length() + 1 );
}
context.usingPrefix = !activeFilters.empty();
}
if ( activeFilters.isEmpty() )
{
Expand Down
5 changes: 5 additions & 0 deletions src/core/locator/qgslocatorcontext.h
Expand Up @@ -51,6 +51,11 @@ class CORE_EXPORT QgsLocatorContext
*/
QgsCoordinateReferenceSystem targetExtentCrs;

/**
* Will be true if search is being conducted using a filter prefix.
*/
bool usingPrefix = false;

};

#endif // QGSLOCATORCONTEXT_H
Expand Down
2 changes: 1 addition & 1 deletion src/core/locator/qgslocatorfilter.cpp
Expand Up @@ -34,7 +34,7 @@ QgsLocatorFilter::Flags QgsLocatorFilter::flags() const

bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &search )
{
return candidate.contains( search, Qt::CaseInsensitive );
return !search.isEmpty() && candidate.contains( search, Qt::CaseInsensitive );
}

bool QgsLocatorFilter::enabled() const
Expand Down
6 changes: 6 additions & 0 deletions tests/src/python/test_qgslocator.py
Expand Up @@ -260,6 +260,12 @@ def testAutoModel(self):
self.assertEqual(m.data(m.index(2, 0)), 'a1')
self.assertEqual(m.data(m.index(3, 0)), 'a2')

def testStringMatches(self):
self.assertFalse(QgsLocatorFilter.stringMatches('xxx', 'yyyy'))
self.assertTrue(QgsLocatorFilter.stringMatches('axxxy', 'xxx'))
self.assertTrue(QgsLocatorFilter.stringMatches('aXXXXy', 'xxx'))
self.assertFalse(QgsLocatorFilter.stringMatches('aXXXXy', ''))


if __name__ == '__main__':
unittest.main()

0 comments on commit e9794c3

Please sign in to comment.