Skip to content

Commit 0a6f88a

Browse files
committedMay 11, 2018
remove map from QgsLocator and rename custom to active prefix
the active/custom prefix info is now accessible only from QgsLocatorFilter
1 parent 6459d24 commit 0a6f88a

File tree

9 files changed

+113
-90
lines changed

9 files changed

+113
-90
lines changed
 

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,23 @@ deregisterFilter() to deregister their filters upon plugin unload to avoid crash
7373
.. seealso:: :py:func:`registerFilter`
7474
%End
7575

76-
QList< QgsLocatorFilter *> filters();
76+
QList< QgsLocatorFilter *> filters( const QString &prefix = QString() );
7777
%Docstring
7878
Returns the list of filters registered in the locator.
7979

80+
:param prefix: If prefix is not empty, the list returned corresponds to the filter with the given active prefix
81+
8082
.. seealso:: :py:func:`prefixedFilters`
8183
%End
8284

83-
8485
QMap<QString, QgsLocatorFilter *> prefixedFilters() const;
8586
%Docstring
8687
Returns a map of prefix to filter, for all registered filters
8788
with valid prefixes.
8889

89-
.. deprecated:: since QGIS 3.2 use prefixes() and filters( const QString &prefix )
90-
9190
.. seealso:: :py:func:`filters`
92-
%End
9391

94-
void setCustomPrefix( QgsLocatorFilter *filter, const QString &customPrefix );
95-
%Docstring
96-
Sets the custom prefix for a filter
97-
%End
98-
99-
QString customPrefix( QgsLocatorFilter *filter ) const;
100-
%Docstring
101-
Returns the custom prefix for a filter if defined, its regular prefix otherwise
92+
.. deprecated:: since QGIS 3.2 use filters() instead
10293
%End
10394

10495
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback = 0 );

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ results from this filter.
124124
as these are reserved for core QGIS functions. If a plugin registers
125125
a filter with a prefix shorter than 3 characters then the prefix will
126126
be ignored.
127+
128+
.. note::
129+
130+
Prefixes might be overriden by user preferences.
131+
132+
.. seealso:: :py:func:`activePrefix`
127133
%End
128134

129135
virtual void prepare( const QString &string, const QgsLocatorContext &context );
@@ -177,6 +183,17 @@ is entered.
177183
.. seealso:: :py:func:`useWithoutPrefix`
178184
%End
179185

186+
QString activePrefix() const;
187+
%Docstring
188+
Returns true if the filter should be used when no prefix
189+
is entered.
190+
191+
.. seealso:: :py:func:`setActivePrefix`
192+
193+
.. versionadded:: 3.2
194+
%End
195+
196+
180197
static bool stringMatches( const QString &candidate, const QString &search );
181198
%Docstring
182199
Tests a ``candidate`` string to see if it should be considered a match for

‎src/app/locator/qgslocatoroptionswidget.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
120120
return filterForIndex( index )->displayName();
121121

122122
case Prefix:
123-
return mPrefixes.value( filterForIndex( index ), mLocator->customPrefix( filterForIndex( index ) ) );
123+
return mPrefixes.value( filterForIndex( index ), filterForIndex( index )->activePrefix() );
124124

125125
case Active:
126126
case Default:
@@ -278,7 +278,17 @@ void QgsLocatorFiltersModel::commitChanges()
278278
for ( ; itp != mPrefixes.constEnd(); ++itp )
279279
{
280280
QgsLocatorFilter *filter = itp.key();
281-
mLocator->setCustomPrefix( filter, itp.value() );
281+
QString activePrefix = itp.value();
282+
if ( !activePrefix.isEmpty() && activePrefix != filter->prefix() )
283+
{
284+
filter->setActivePrefix( activePrefix );
285+
settings.setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), activePrefix, QgsSettings::Section::Gui );
286+
}
287+
else
288+
{
289+
filter->setActivePrefix( QString() );
290+
settings.remove( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), QgsSettings::Section::Gui );
291+
}
282292
}
283293
QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin();
284294
for ( ; it != mEnabledChanges.constEnd(); ++it )

‎src/app/locator/qgslocatoroptionswidget.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class QgsLocatorOptionsWidget : public QWidget, private Ui::QgsLocatorOptionsWid
3939
void configureCurrentFilter();
4040

4141
private:
42-
4342
QgsLocatorWidget *mLocatorWidget = nullptr;
4443
QgsLocator *mLocator = nullptr;
4544
QgsLocatorFiltersModel *mModel = nullptr;

‎src/core/locator/qgslocator.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,40 @@ void QgsLocator::deregisterFilter( QgsLocatorFilter *filter )
4444
{
4545
cancelRunningQuery();
4646
mFilters.removeAll( filter );
47-
mPrefixedFilters.remove( mPrefixedFilters.key( filter ), filter );
4847
delete filter;
4948
}
5049

51-
QList<QgsLocatorFilter *> QgsLocator::filters()
50+
QList<QgsLocatorFilter *> QgsLocator::filters( const QString &prefix )
5251
{
53-
return mFilters;
54-
}
55-
56-
QMultiMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
57-
{
58-
return mPrefixedFilters;
59-
}
60-
61-
void QgsLocator::setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix )
62-
{
63-
if ( filter )
52+
if ( !prefix.isEmpty() )
6453
{
65-
mPrefixedFilters.remove( mPrefixedFilters.key( filter ), filter );
66-
if ( !prefix.isEmpty() && prefix != filter->prefix() )
67-
{
68-
QgsSettings().setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), prefix, QgsSettings::Section::Gui );
69-
mPrefixedFilters.insert( prefix, filter );
70-
}
71-
else
54+
QList<QgsLocatorFilter *> filters = QList<QgsLocatorFilter *>();
55+
for ( QgsLocatorFilter *filter : mFilters )
7256
{
73-
// reset the setting if the string is empty or equal to the native prefix
74-
// one should the default checkbox instead
75-
QgsSettings().remove( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), QgsSettings::Section::Gui );
57+
if ( !filter->activePrefix().isEmpty() && filter->activePrefix() == prefix )
58+
{
59+
filters << filter;
60+
}
7661
}
62+
return filters;
63+
}
64+
else
65+
{
66+
return mFilters;
7767
}
7868
}
7969

80-
QString QgsLocator::customPrefix( QgsLocatorFilter *filter ) const
70+
QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
8171
{
82-
return mPrefixedFilters.key( filter, QString() );
72+
QMap<QString, QgsLocatorFilter *> filters = QMap<QString, QgsLocatorFilter *>();
73+
for ( QgsLocatorFilter *filter : mFilters )
74+
{
75+
if ( !filter->activePrefix().isEmpty() && filter->enabled() )
76+
{
77+
filters.insertMulti( filter->activePrefix(), filter );
78+
}
79+
}
80+
return filters;
8381
}
8482

8583
void QgsLocator::registerFilter( QgsLocatorFilter *filter )
@@ -91,18 +89,22 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter )
9189
QgsSettings settings;
9290
bool enabled = settings.value( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), true, QgsSettings::Section::Gui ).toBool();
9391
bool byDefault = settings.value( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), filter->useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();
94-
QString customPrefix = settings.value( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), filter->prefix(), QgsSettings::Section::Gui ).toString();
92+
QString prefix = settings.value( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), filter->prefix(), QgsSettings::Section::Gui ).toString();
93+
if ( prefix.isEmpty() )
94+
{
95+
prefix = filter->prefix();
96+
}
9597

96-
if ( !customPrefix.isEmpty() )
98+
if ( !prefix.isEmpty() )
9799
{
98100
if ( CORE_FILTERS.contains( filter->name() ) )
99101
{
100102
//inbuilt filter, no prefix check
101-
mPrefixedFilters.insert( customPrefix, filter );
103+
filter->setActivePrefix( prefix );
102104
}
103-
else if ( customPrefix.length() >= 3 || customPrefix != filter->prefix() )
105+
else if ( prefix.length() >= 3 || prefix != filter->prefix() )
104106
{
105-
mPrefixedFilters.insert( customPrefix, filter );
107+
filter->setActivePrefix( prefix );
106108
}
107109
}
108110

@@ -136,15 +138,12 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
136138
QString prefix = searchString.left( std::max( searchString.indexOf( ' ' ), 0 ) );
137139
if ( !prefix.isEmpty() )
138140
{
139-
QMultiMap<QString, QgsLocatorFilter *>::const_iterator it = mPrefixedFilters.constFind( prefix );
140-
while ( it != mPrefixedFilters.constEnd() && it.key() == prefix )
141+
for ( QgsLocatorFilter *filter : qgis::as_const( mFilters ) )
141142
{
142-
QgsLocatorFilter *filter = it.value();
143-
if ( filter->enabled() )
143+
if ( filter->activePrefix() == prefix && filter->enabled() )
144144
{
145145
activeFilters << filter;
146146
}
147-
++it;
148147
}
149148
context.usingPrefix = !activeFilters.empty();
150149
}

‎src/core/locator/qgslocator.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,38 +95,18 @@ class CORE_EXPORT QgsLocator : public QObject
9595

9696
/**
9797
* Returns the list of filters registered in the locator.
98+
* \param prefix If prefix is not empty, the list returned corresponds to the filter with the given active prefix
9899
* \see prefixedFilters()
99100
*/
100-
QList< QgsLocatorFilter *> filters();
101-
102-
#ifndef SIP_RUN
103-
104-
/**
105-
* Returns a map of prefix to filter, for all registered filters
106-
* with valid prefixes.
107-
* \see filters()
108-
*/
109-
QMultiMap<QString, QgsLocatorFilter *> prefixedFilters() const;
110-
#else
101+
QList< QgsLocatorFilter *> filters( const QString &prefix = QString() );
111102

112103
/**
113104
* Returns a map of prefix to filter, for all registered filters
114105
* with valid prefixes.
115-
* \deprecated since QGIS 3.2 use prefixes() and filters( const QString &prefix )
116106
* \see filters()
107+
* \deprecated since QGIS 3.2 use filters() instead
117108
*/
118109
Q_DECL_DEPRECATED QMap<QString, QgsLocatorFilter *> prefixedFilters() const;
119-
#endif
120-
121-
/**
122-
* Sets the custom prefix for a filter
123-
*/
124-
void setCustomPrefix( QgsLocatorFilter *filter, const QString &customPrefix );
125-
126-
/**
127-
* Returns the custom prefix for a filter if defined, its regular prefix otherwise
128-
*/
129-
QString customPrefix( QgsLocatorFilter *filter ) const;
130110

131111
/**
132112
* Triggers the background fetching of filter results for a specified search \a string.
@@ -183,7 +163,6 @@ class CORE_EXPORT QgsLocator : public QObject
183163
std::unique_ptr< QgsFeedback > mOwnedFeedback;
184164

185165
QList< QgsLocatorFilter * > mFilters;
186-
QMultiMap< QString, QgsLocatorFilter *> mPrefixedFilters;
187166
QList< QThread * > mActiveThreads;
188167

189168
void cancelRunningQuery();

‎src/core/locator/qgslocatorfilter.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ void QgsLocatorFilter::setUseWithoutPrefix( bool useWithoutPrefix )
6767
mUseWithoutPrefix = useWithoutPrefix;
6868
}
6969

70+
QString QgsLocatorFilter::activePrefix() const
71+
{
72+
if ( mActivePrefifx.isEmpty() )
73+
return prefix();
74+
else
75+
return mActivePrefifx;
76+
}
77+
78+
void QgsLocatorFilter::setActivePrefix( const QString &activePrefix )
79+
{
80+
mActivePrefifx = activePrefix;
81+
}
82+

‎src/core/locator/qgslocatorfilter.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
155155
* as these are reserved for core QGIS functions. If a plugin registers
156156
* a filter with a prefix shorter than 3 characters then the prefix will
157157
* be ignored.
158+
* \note Prefixes might be overriden by user preferences.
159+
* \see activePrefix()
158160
*/
159161
virtual QString prefix() const { return QString(); }
160162

@@ -207,6 +209,22 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
207209
*/
208210
void setUseWithoutPrefix( bool useWithoutPrefix );
209211

212+
/**
213+
* Returns true if the filter should be used when no prefix
214+
* is entered.
215+
* \see setActivePrefix()
216+
* \since QGIS 3.2
217+
*/
218+
QString activePrefix() const;
219+
220+
/**
221+
* Sets whether the filter should be used when no prefix
222+
* is entered.
223+
* \see activePrefix()
224+
* \since QGIS 3.2
225+
*/
226+
void setActivePrefix( const QString &activePrefix ) SIP_SKIP;
227+
210228
/**
211229
* Tests a \a candidate string to see if it should be considered a match for
212230
* a specified \a search string.
@@ -258,6 +276,7 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
258276

259277
bool mEnabled = true;
260278
bool mUseWithoutPrefix = true;
279+
QString mActivePrefifx = QString();
261280

262281
};
263282

‎src/gui/locator/qgslocatorwidget.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,12 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
260260
void QgsLocatorWidget::configMenuAboutToShow()
261261
{
262262
mMenu->clear();
263-
QMultiMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
264-
QMultiMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
265-
for ( ; fIt != filters.constEnd(); ++fIt )
263+
for ( QgsLocatorFilter *filter : mLocator->filters() )
266264
{
267-
if ( !fIt.value()->enabled() )
265+
if ( !filter->enabled() )
268266
continue;
269267

270-
QAction *action = new QAction( fIt.value()->displayName(), mMenu );
268+
QAction *action = new QAction( filter->displayName(), mMenu );
271269
connect( action, &QAction::triggered, this, [ = ]
272270
{
273271
QString currentText = mLineEdit->text();
@@ -276,15 +274,15 @@ void QgsLocatorWidget::configMenuAboutToShow()
276274
else
277275
{
278276
QStringList parts = currentText.split( ' ' );
279-
if ( parts.count() > 1 && mLocator->prefixedFilters().contains( parts.at( 0 ) ) )
277+
if ( parts.count() > 1 && mLocator->filters( parts.at( 0 ) ).count() > 0 )
280278
{
281279
parts.pop_front();
282280
currentText = parts.join( ' ' );
283281
}
284282
}
285283

286-
mLineEdit->setText( fIt.key() + ' ' + currentText );
287-
mLineEdit->setSelection( fIt.key().length() + 1, currentText.length() );
284+
mLineEdit->setText( filter->activePrefix() + ' ' + currentText );
285+
mLineEdit->setSelection( filter->activePrefix().length() + 1, currentText.length() );
288286
} );
289287
mMenu->addAction( action );
290288
}
@@ -422,20 +420,18 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat
422420
return;
423421
}
424422

425-
QMultiMap< QString, QgsLocatorFilter *> filters = mLocator->locator()->prefixedFilters();
426-
QMultiMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
427-
for ( ; fIt != filters.constEnd(); ++fIt )
423+
for ( QgsLocatorFilter *filter : mLocator->locator()->filters() )
428424
{
429425
if ( feedback->isCanceled() )
430426
return;
431427

432-
if ( fIt.value() == this || !fIt.value() || !fIt.value()->enabled() )
428+
if ( filter == this || !filter || !filter->enabled() )
433429
continue;
434430

435431
QgsLocatorResult result;
436-
result.displayString = fIt.key();
437-
result.description = fIt.value()->displayName();
438-
result.userData = fIt.key() + ' ';
432+
result.displayString = filter->activePrefix();
433+
result.description = filter->displayName();
434+
result.userData = filter->activePrefix() + ' ';
439435
result.icon = QgsApplication::getThemeIcon( QStringLiteral( "/search.svg" ) );
440436
emit resultFetched( result );
441437
}

0 commit comments

Comments
 (0)
Please sign in to comment.