Skip to content
Permalink
Browse files

Fix some locator filters show results when no string is entered

and filter prefix is not used

(cherry-picked from ebab64)
  • Loading branch information
nyalldawson committed Apr 6, 2018
1 parent ca73205 commit e9794c3a52fcfabc68ac45a7470480ce2db66b82
@@ -31,6 +31,8 @@ Constructor for QgsLocatorContext.

QgsCoordinateReferenceSystem targetExtentCrs;

bool usingPrefix;

};


@@ -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()
@@ -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();
@@ -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();
@@ -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...
@@ -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() )
{
@@ -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
@@ -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
@@ -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.
You can’t perform that action at this time.