Skip to content

Commit 0a0de2e

Browse files
committed
use multimap to allow using the same prefix for several filters
1 parent 37e9f3a commit 0a0de2e

4 files changed

Lines changed: 92 additions & 33 deletions

File tree

‎python/core/locator/qgslocator.sip.in‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010

1111

12+
13+
1214
class QgsLocator : QObject
1315
{
1416
%Docstring
@@ -71,22 +73,35 @@ deregisterFilter() to deregister their filters upon plugin unload to avoid crash
7173
.. seealso:: :py:func:`registerFilter`
7274
%End
7375

74-
QList< QgsLocatorFilter *> filters();
76+
QList< QgsLocatorFilter *> filters( const QString &prefix = QString() );
7577
%Docstring
7678
Returns the list of filters registered in the locator.
7779

7880
.. seealso:: :py:func:`prefixedFilters`
7981
%End
8082

81-
QMap< QString, QgsLocatorFilter *> prefixedFilters() const;
83+
QStringList prefixes();
84+
%Docstring
85+
Returns the list of prefixes in use.
86+
A prefix can have several filters
87+
88+
.. seealso:: :py:func:`filter`
89+
90+
.. versionadded:: 3.2
91+
%End
92+
93+
94+
QMap<QString, QgsLocatorFilter *> prefixedFilters() const;
8295
%Docstring
8396
Returns a map of prefix to filter, for all registered filters
8497
with valid prefixes.
8598

99+
.. deprecated:: since QGIS 3.2 use prefixes() and filters( const QString &prefix )
100+
86101
.. seealso:: :py:func:`filters`
87102
%End
88103

89-
void setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix );
104+
void setCustomPrefix( QgsLocatorFilter *filter, const QString &customPrefix );
90105
%Docstring
91106
Sets the custom prefix for a filter
92107
%End

‎src/core/locator/qgslocator.cpp‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,24 @@ void QgsLocator::deregisterFilter( QgsLocatorFilter *filter )
4444
{
4545
cancelRunningQuery();
4646
mFilters.removeAll( filter );
47-
QString key = mPrefixedFilters.key( filter );
48-
if ( !key.isEmpty() )
49-
mPrefixedFilters.remove( key );
47+
mPrefixedFilters.remove( mPrefixedFilters.key( filter ), filter );
5048
delete filter;
5149
}
5250

53-
QList<QgsLocatorFilter *> QgsLocator::filters()
51+
QList<QgsLocatorFilter *> QgsLocator::filters( const QString &prefix )
5452
{
55-
return mFilters;
53+
if ( !prefix.isEmpty() )
54+
return mPrefixedFilters.values( prefix );
55+
else
56+
return mFilters;
57+
}
58+
59+
QStringList QgsLocator::prefixes()
60+
{
61+
return mPrefixedFilters.keys();
5662
}
5763

58-
QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
64+
QMultiMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
5965
{
6066
return mPrefixedFilters;
6167
}
@@ -64,12 +70,18 @@ void QgsLocator::setCustomPrefix( QgsLocatorFilter *filter, const QString &prefi
6470
{
6571
if ( filter )
6672
{
67-
QgsSettings().setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), prefix, QgsSettings::Section::Gui );
68-
QString oldPrefix = mPrefixedFilters.key( filter );
69-
if ( !oldPrefix.isEmpty() )
70-
mPrefixedFilters.remove( oldPrefix );
71-
72-
mPrefixedFilters.insert( prefix, filter );
73+
mPrefixedFilters.remove( mPrefixedFilters.key( filter ), filter );
74+
if ( !prefix.isEmpty() && prefix != filter->prefix() )
75+
{
76+
QgsSettings().setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), prefix, QgsSettings::Section::Gui );
77+
mPrefixedFilters.insert( prefix, filter );
78+
}
79+
else
80+
{
81+
// reset the setting if the string is empty or equal to the native prefix
82+
// one should the default checkbox instead
83+
QgsSettings().remove( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), QgsSettings::Section::Gui );
84+
}
7385
}
7486
}
7587

@@ -129,17 +141,26 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
129141

130142
QList< QgsLocatorFilter * > activeFilters;
131143
QString searchString = string;
132-
if ( searchString.indexOf( ' ' ) > 0 )
144+
QString prefix = searchString.left( std::max( searchString.indexOf( ' ' ), 0 ) );
145+
if ( !prefix.isEmpty() )
133146
{
134-
QString prefix = searchString.left( searchString.indexOf( ' ' ) );
135-
if ( mPrefixedFilters.contains( prefix ) && mPrefixedFilters.value( prefix )->enabled() )
147+
QMultiMap<QString, QgsLocatorFilter *>::const_iterator it = mPrefixedFilters.constFind( prefix );
148+
while ( it != mPrefixedFilters.constEnd() && it.key() == prefix )
136149
{
137-
activeFilters << mPrefixedFilters.value( prefix );
138-
searchString = searchString.mid( prefix.length() + 1 );
150+
QgsLocatorFilter *filter = it.value();
151+
if ( filter->enabled() )
152+
{
153+
activeFilters << filter;
154+
}
155+
++it;
139156
}
140157
context.usingPrefix = !activeFilters.empty();
141158
}
142-
if ( activeFilters.isEmpty() )
159+
if ( !activeFilters.isEmpty() )
160+
{
161+
searchString = searchString.mid( prefix.length() + 1 );
162+
}
163+
else
143164
{
144165
for ( QgsLocatorFilter *filter : qgis::as_const( mFilters ) )
145166
{

‎src/core/locator/qgslocator.h‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
#ifndef QGSLOCATOR_H
1919
#define QGSLOCATOR_H
2020

21+
#include <QObject>
22+
#include <QFuture>
23+
#include <QFutureWatcher>
24+
#include <QMap>
25+
#include <memory>
26+
2127
#include "qgis_core.h"
2228
#include "qgis_sip.h"
2329
#include "qgslocatorfilter.h"
2430
#include "qgsfeedback.h"
2531
#include "qgslocatorcontext.h"
26-
#include <QObject>
27-
#include <QFuture>
28-
#include <QFutureWatcher>
29-
#include <memory>
32+
3033

3134
/**
3235
* \class QgsLocator
@@ -93,19 +96,39 @@ class CORE_EXPORT QgsLocator : public QObject
9396
* Returns the list of filters registered in the locator.
9497
* \see prefixedFilters()
9598
*/
96-
QList< QgsLocatorFilter *> filters();
99+
QList< QgsLocatorFilter *> filters( const QString &prefix = QString() );
100+
101+
/**
102+
* Returns the list of prefixes in use.
103+
* A prefix can have several filters
104+
* \see filter()
105+
* \since QGIS 3.2
106+
*/
107+
QStringList prefixes();
108+
109+
#ifndef SIP_RUN
110+
111+
/**
112+
* Returns a map of prefix to filter, for all registered filters
113+
* with valid prefixes.
114+
* \see filters()
115+
*/
116+
QMultiMap<QString, QgsLocatorFilter *> prefixedFilters() const;
117+
#else
97118

98119
/**
99120
* Returns a map of prefix to filter, for all registered filters
100121
* with valid prefixes.
122+
* \deprecated since QGIS 3.2 use prefixes() and filters( const QString &prefix )
101123
* \see filters()
102124
*/
103-
QMap< QString, QgsLocatorFilter *> prefixedFilters() const;
125+
Q_DECL_DEPRECATED QMap<QString, QgsLocatorFilter *> prefixedFilters() const;
126+
#endif
104127

105128
/**
106129
* Sets the custom prefix for a filter
107130
*/
108-
void setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix );
131+
void setCustomPrefix( QgsLocatorFilter *filter, const QString &customPrefix );
109132

110133
/**
111134
* Returns the custom prefix for a filter if defined, its regular prefix otherwise
@@ -167,7 +190,7 @@ class CORE_EXPORT QgsLocator : public QObject
167190
std::unique_ptr< QgsFeedback > mOwnedFeedback;
168191

169192
QList< QgsLocatorFilter * > mFilters;
170-
QMap< QString, QgsLocatorFilter *> mPrefixedFilters;
193+
QMultiMap< QString, QgsLocatorFilter *> mPrefixedFilters;
171194
QList< QThread * > mActiveThreads;
172195

173196
void cancelRunningQuery();

‎src/gui/locator/qgslocatorwidget.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
260260
void QgsLocatorWidget::configMenuAboutToShow()
261261
{
262262
mMenu->clear();
263-
QMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
264-
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
263+
QMultiMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
264+
QMultiMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
265265
for ( ; fIt != filters.constEnd(); ++fIt )
266266
{
267267
if ( !fIt.value()->enabled() )
@@ -422,8 +422,8 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat
422422
return;
423423
}
424424

425-
QMap< QString, QgsLocatorFilter *> filters = mLocator->locator()->prefixedFilters();
426-
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
425+
QMultiMap< QString, QgsLocatorFilter *> filters = mLocator->locator()->prefixedFilters();
426+
QMultiMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
427427
for ( ; fIt != filters.constEnd(); ++fIt )
428428
{
429429
if ( feedback->isCanceled() )

0 commit comments

Comments
 (0)