Skip to content

Commit 102a466

Browse files
committedMay 17, 2017
Implement prefix based locator searching
Filters can indicate their preferred search prefix. Searches which begin with this character will be restricted to the single matching filter. E.g. entering 'l buffer' will searching only layers containing 'buffer' Other prefixes are: - . search actions - pl search print layouts - a search algorithms Plugins are restricted to a minimum 3 character prefix. We do this to avoid plugins 'stealing' desirable prefixes, and instead we want to reserve them for future core filters.
1 parent b33ce0b commit 102a466

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed
 

‎python/gui/locator/qgslocatorfilter.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ class QgsLocatorFilter : QObject
101101
:rtype: Priority
102102
%End
103103

104+
virtual QString prefix() const;
105+
%Docstring
106+
Returns the search prefix character(s) for this filter. Prefix a search
107+
with these characters will restrict the locator search to only include
108+
results from this filter.
109+
.. note::
110+
111+
Plugins are not permitted to utilise prefixes with < 3 characters,
112+
as these are reserved for core QGIS functions. If a plugin registers
113+
a filter with a prefix shorter than 3 characters then the prefix will
114+
be ignored.
115+
:rtype: str
116+
%End
117+
104118
virtual void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) = 0;
105119
%Docstring
106120
Retrieves the filter results for a specified search ``string``. The ``context``

‎python/plugins/processing/gui/AlgorithmLocatorFilter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def displayName(self):
4848
def priority(self):
4949
return QgsLocatorFilter.Low
5050

51+
def prefix(self):
52+
return 'a'
53+
5154
def fetchResults(self,string,context,feedback):
5255
for a in QgsApplication.processingRegistry().algorithms():
5356
if feedback.isCanceled():

‎src/app/locator/qgsinbuiltlocatorfilters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void QgsActionLocatorFilter::searchActions( const QString &string, QWidget *pare
141141
continue;
142142
}
143143

144-
if ( !action->isEnabled() || !action->isVisible() )
144+
if ( !action->isEnabled() || !action->isVisible() || action->text().isEmpty() )
145145
continue;
146146
if ( found.contains( action ) )
147147
continue;

‎src/app/locator/qgsinbuiltlocatorfilters.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QgsLayerTreeLocatorFilter : public QgsLocatorFilter
3131
virtual QString name() const override { return QStringLiteral( "layertree" ); }
3232
virtual QString displayName() const override { return tr( "Project layers" ); }
3333
virtual Priority priority() const override { return Highest; }
34+
QString prefix() const override { return QStringLiteral( "l" ); }
3435

3536
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
3637
void triggerResult( const QgsLocatorResult &result ) override;
@@ -47,6 +48,7 @@ class QgsLayoutLocatorFilter : public QgsLocatorFilter
4748
virtual QString name() const override { return QStringLiteral( "layouts" ); }
4849
virtual QString displayName() const override { return tr( "Project layouts" ); }
4950
virtual Priority priority() const override { return Highest; }
51+
QString prefix() const override { return QStringLiteral( "pl" ); }
5052

5153
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
5254
void triggerResult( const QgsLocatorResult &result ) override;
@@ -63,6 +65,7 @@ class QgsActionLocatorFilter : public QgsLocatorFilter
6365
virtual QString name() const override { return QStringLiteral( "actions" ); }
6466
virtual QString displayName() const override { return tr( "Actions" ); }
6567
virtual Priority priority() const override { return Lowest; }
68+
QString prefix() const override { return QStringLiteral( "." ); }
6669

6770
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
6871
void triggerResult( const QgsLocatorResult &result ) override;

‎src/gui/locator/qgslocator.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ void QgsLocator::deregisterFilter( QgsLocatorFilter *filter )
3737
{
3838
cancelRunningQuery();
3939
mFilters.removeAll( filter );
40+
QString key = mPrefixedFilters.key( filter );
41+
if ( !key.isEmpty() )
42+
mPrefixedFilters.remove( key );
4043
delete filter;
4144
}
4245

@@ -50,6 +53,20 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter )
5053
mFilters.append( filter );
5154
filter->setParent( this );
5255
connect( filter, &QgsLocatorFilter::resultFetched, this, &QgsLocator::filterSentResult, Qt::QueuedConnection );
56+
57+
if ( !filter->prefix().isEmpty() )
58+
{
59+
if ( filter->name() == QStringLiteral( "actions" ) || filter->name() == QStringLiteral( "processing_alg" )
60+
|| filter->name() == QStringLiteral( "layertree" ) || filter->name() == QStringLiteral( "layouts" ) )
61+
{
62+
//inbuilt filter, no prefix check
63+
mPrefixedFilters.insert( filter->prefix(), filter );
64+
}
65+
else if ( filter->prefix().length() >= 3 )
66+
{
67+
mPrefixedFilters.insert( filter->prefix(), filter );
68+
}
69+
}
5370
}
5471

5572
void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback )
@@ -72,13 +89,26 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
7289
}
7390
mFeedback = feedback;
7491

75-
auto gatherFilterResults = [string, context, feedback]( QgsLocatorFilter * filter )
92+
mActiveFilters = mFilters;
93+
QString searchString = string;
94+
if ( searchString.indexOf( ' ' ) > 0 )
95+
{
96+
QString prefix = searchString.left( searchString.indexOf( ' ' ) );
97+
if ( mPrefixedFilters.contains( prefix ) )
98+
{
99+
mActiveFilters.clear();
100+
mActiveFilters << mPrefixedFilters.value( prefix );
101+
searchString = searchString.mid( prefix.length() + 1 );
102+
}
103+
}
104+
105+
auto gatherFilterResults = [searchString, context, feedback]( QgsLocatorFilter * filter )
76106
{
77107
if ( !feedback->isCanceled() )
78-
filter->fetchResults( string, context, feedback );
108+
filter->fetchResults( searchString, context, feedback );
79109
};
80110

81-
mFuture = QtConcurrent::map( mFilters, gatherFilterResults );
111+
mFuture = QtConcurrent::map( mActiveFilters, gatherFilterResults );
82112
mFutureWatcher.setFuture( mFuture );
83113
}
84114

‎src/gui/locator/qgslocator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class GUI_EXPORT QgsLocator : public QObject
147147
std::unique_ptr< QgsFeedback > mOwnedFeedback;
148148

149149
QList< QgsLocatorFilter * > mFilters;
150+
QList< QgsLocatorFilter * > mActiveFilters;
151+
QMap< QString, QgsLocatorFilter *> mPrefixedFilters;
150152
QFuture< void > mFuture;
151153
QFutureWatcher< void > mFutureWatcher;
152154

‎src/gui/locator/qgslocatorfilter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
119119
*/
120120
virtual Priority priority() const { return Medium; }
121121

122+
/**
123+
* Returns the search prefix character(s) for this filter. Prefix a search
124+
* with these characters will restrict the locator search to only include
125+
* results from this filter.
126+
* \note Plugins are not permitted to utilise prefixes with < 3 characters,
127+
* as these are reserved for core QGIS functions. If a plugin registers
128+
* a filter with a prefix shorter than 3 characters then the prefix will
129+
* be ignored.
130+
*/
131+
virtual QString prefix() const { return QString(); }
132+
122133
/**
123134
* Retrieves the filter results for a specified search \a string. The \a context
124135
* argument encapsulates the context relating to the search (such as a map

0 commit comments

Comments
 (0)
Please sign in to comment.