Skip to content

Commit b33ce0b

Browse files
committed
Allow speciyfing the priority for filters
Higher priority (i.e. more important) filter results get shown first. This means filters like project layers & composers will show above 'cruder' filters like the actions/processing filters.
1 parent 0f80df0 commit b33ce0b

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

python/gui/locator/qgslocatorfilter.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ class QgsLocatorFilter : QObject
6666
%End
6767
public:
6868

69+
enum Priority
70+
{
71+
Highest,
72+
High,
73+
Medium,
74+
Low,
75+
Lowest
76+
};
77+
6978
QgsLocatorFilter( QObject *parent = 0 );
7079
%Docstring
7180
Constructor for QgsLocatorFilter.
@@ -85,6 +94,13 @@ class QgsLocatorFilter : QObject
8594
:rtype: str
8695
%End
8796

97+
virtual Priority priority() const;
98+
%Docstring
99+
Returns the priority for the filter, which controls how results are
100+
ordered in the locator.
101+
:rtype: Priority
102+
%End
103+
88104
virtual void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) = 0;
89105
%Docstring
90106
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
@@ -45,6 +45,9 @@ def name(self):
4545
def displayName(self):
4646
return self.tr('Processing Algorithms')
4747

48+
def priority(self):
49+
return QgsLocatorFilter.Low
50+
4851
def fetchResults(self,string,context,feedback):
4952
for a in QgsApplication.processingRegistry().algorithms():
5053
if feedback.isCanceled():

src/app/locator/qgsinbuiltlocatorfilters.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class QgsLayerTreeLocatorFilter : public QgsLocatorFilter
3030
QgsLayerTreeLocatorFilter( QObject *parent = nullptr );
3131
virtual QString name() const override { return QStringLiteral( "layertree" ); }
3232
virtual QString displayName() const override { return tr( "Project layers" ); }
33+
virtual Priority priority() const override { return Highest; }
3334

3435
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
3536
void triggerResult( const QgsLocatorResult &result ) override;
@@ -45,6 +46,7 @@ class QgsLayoutLocatorFilter : public QgsLocatorFilter
4546
QgsLayoutLocatorFilter( QObject *parent = nullptr );
4647
virtual QString name() const override { return QStringLiteral( "layouts" ); }
4748
virtual QString displayName() const override { return tr( "Project layouts" ); }
49+
virtual Priority priority() const override { return Highest; }
4850

4951
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
5052
void triggerResult( const QgsLocatorResult &result ) override;
@@ -60,6 +62,7 @@ class QgsActionLocatorFilter : public QgsLocatorFilter
6062
QgsActionLocatorFilter( const QList<QWidget *> &parentObjectsForActions, QObject *parent = nullptr );
6163
virtual QString name() const override { return QStringLiteral( "actions" ); }
6264
virtual QString displayName() const override { return tr( "Actions" ); }
65+
virtual Priority priority() const override { return Lowest; }
6366

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

src/gui/locator/qgslocatorfilter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
8686

8787
public:
8888

89+
//! Filter priority. Controls the order of results in the locator.
90+
enum Priority
91+
{
92+
Highest, //!< Highest priority
93+
High, //!< High priority
94+
Medium, //!< Medium priority
95+
Low, //!< Low priority
96+
Lowest //!< Lowest priority
97+
};
98+
8999
/**
90100
* Constructor for QgsLocatorFilter.
91101
*/
@@ -103,6 +113,12 @@ class GUI_EXPORT QgsLocatorFilter : public QObject
103113
*/
104114
virtual QString displayName() const = 0;
105115

116+
/**
117+
* Returns the priority for the filter, which controls how results are
118+
* ordered in the locator.
119+
*/
120+
virtual Priority priority() const { return Medium; }
121+
106122
/**
107123
* Retrieves the filter results for a specified search \a string. The \a context
108124
* argument encapsulates the context relating to the search (such as a map

src/gui/locator/qgslocatorwidget.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
4242
setSizePolicy( sizePolicy );
4343
setMinimumSize( QSize( 200, 0 ) );
4444

45-
QHBoxLayout *layout = new QHBoxLayout( this );
45+
QHBoxLayout *layout = new QHBoxLayout();
4646
layout->setMargin( 0 );
4747
layout->setContentsMargins( 0, 0, 0, 0 );
4848
layout->addWidget( mLineEdit );
@@ -293,32 +293,38 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
293293
case Qt::DisplayRole:
294294
case Qt::EditRole:
295295
{
296-
if ( mResults.at( index.row() ).filterTitle.isEmpty() )
296+
if ( !mResults.at( index.row() ).filter )
297297
return mResults.at( index.row() ).result.displayString;
298298
else
299299
return mResults.at( index.row() ).filterTitle;
300300
}
301301

302302
case Qt::DecorationRole:
303-
if ( mResults.at( index.row() ).filterTitle.isEmpty() )
303+
if ( !mResults.at( index.row() ).filter )
304304
return mResults.at( index.row() ).result.icon;
305305
else
306306
return QVariant();
307307

308308
case ResultDataRole:
309-
if ( mResults.at( index.row() ).filterTitle.isEmpty() )
309+
if ( !mResults.at( index.row() ).filter )
310310
return QVariant::fromValue( mResults.at( index.row() ).result );
311311
else
312312
return QVariant();
313313

314314
case ResultTypeRole:
315-
if ( mResults.at( index.row() ).filterTitle.isEmpty() )
315+
if ( mResults.at( index.row() ).filter )
316+
return 0;
317+
else
316318
return 1;
319+
320+
case ResultFilterPriorityRole:
321+
if ( !mResults.at( index.row() ).filter )
322+
return mResults.at( index.row() ).result.filter->priority();
317323
else
318-
return 0;
324+
return mResults.at( index.row() ).filter->priority();
319325

320326
case ResultFilterNameRole:
321-
if ( mResults.at( index.row() ).filterTitle.isEmpty() )
327+
if ( !mResults.at( index.row() ).filter )
322328
return mResults.at( index.row() ).result.filter->displayName();
323329
else
324330
return mResults.at( index.row() ).filterTitle;
@@ -354,6 +360,7 @@ void QgsLocatorModel::addResult( const QgsLocatorResult &result )
354360
{
355361
Entry entry;
356362
entry.filterTitle = result.filter->displayName();
363+
entry.filter = result.filter;
357364
mResults << entry;
358365
}
359366
Entry entry;
@@ -418,16 +425,25 @@ QgsLocatorProxyModel::QgsLocatorProxyModel( QObject *parent )
418425

419426
bool QgsLocatorProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
420427
{
428+
// first go by filter priority
429+
int leftFilterPriority = sourceModel()->data( left, QgsLocatorModel::ResultFilterPriorityRole ).toInt();
430+
int rightFilterPriority = sourceModel()->data( right, QgsLocatorModel::ResultFilterPriorityRole ).toInt();
431+
if ( leftFilterPriority != rightFilterPriority )
432+
return leftFilterPriority < rightFilterPriority;
433+
434+
// then filter name
421435
QString leftFilter = sourceModel()->data( left, QgsLocatorModel::ResultFilterNameRole ).toString();
422436
QString rightFilter = sourceModel()->data( right, QgsLocatorModel::ResultFilterNameRole ).toString();
423437
if ( leftFilter != rightFilter )
424438
return QString::localeAwareCompare( leftFilter, rightFilter ) < 0;
425439

440+
// then make sure filter title appears before filter's results
426441
int leftTypeRole = sourceModel()->data( left, QgsLocatorModel::ResultTypeRole ).toInt();
427442
int rightTypeRole = sourceModel()->data( right, QgsLocatorModel::ResultTypeRole ).toInt();
428443
if ( leftTypeRole != rightTypeRole )
429444
return leftTypeRole < rightTypeRole;
430445

446+
// lastly sort filter's results by string
431447
leftFilter = sourceModel()->data( left, Qt::DisplayRole ).toString();
432448
rightFilter = sourceModel()->data( right, Qt::DisplayRole ).toString();
433449
return QString::localeAwareCompare( leftFilter, rightFilter ) < 0;

src/gui/locator/qgslocatorwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class QgsLocatorModel : public QAbstractListModel
127127
{
128128
ResultDataRole = Qt::UserRole + 1, //!< QgsLocatorResult data
129129
ResultTypeRole,
130+
ResultFilterPriorityRole,
130131
ResultFilterNameRole,
131132
};
132133

@@ -158,6 +159,7 @@ class QgsLocatorModel : public QAbstractListModel
158159
{
159160
QgsLocatorResult result;
160161
QString filterTitle;
162+
QgsLocatorFilter *filter = nullptr;
161163
};
162164

163165
QList<Entry> mResults;

0 commit comments

Comments
 (0)