Skip to content

Commit 37e9f3a

Browse files
committedMay 9, 2018
allow customizing prefix of locator filters
1 parent 4c92de9 commit 37e9f3a

File tree

6 files changed

+98
-16
lines changed

6 files changed

+98
-16
lines changed
 

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ call to fetchResults().
3838
%End
3939
public:
4040

41+
static const QList<QString> CORE_FILTERS;
42+
4143
QgsLocator( QObject *parent /TransferThis/ = 0 );
4244
%Docstring
4345
Constructor for QgsLocator.
@@ -82,6 +84,16 @@ Returns a map of prefix to filter, for all registered filters
8284
with valid prefixes.
8385

8486
.. seealso:: :py:func:`filters`
87+
%End
88+
89+
void setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix );
90+
%Docstring
91+
Sets the custom prefix for a filter
92+
%End
93+
94+
QString customPrefix( QgsLocatorFilter *filter ) const;
95+
%Docstring
96+
Returns the custom prefix for a filter if defined, its regular prefix otherwise
8597
%End
8698

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

‎src/app/locator/qgslocatoroptionswidget.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgslocatoroptionswidget.h"
19+
1920
#include "qgslocatorwidget.h"
2021
#include "qgssettings.h"
2122

@@ -119,7 +120,7 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
119120
return filterForIndex( index )->displayName();
120121

121122
case Prefix:
122-
return filterForIndex( index )->prefix();
123+
return mPrefixes.value( filterForIndex( index ), mLocator->customPrefix( filterForIndex( index ) ) );
123124

124125
case Active:
125126
case Default:
@@ -165,6 +166,28 @@ bool QgsLocatorFiltersModel::setData( const QModelIndex &index, const QVariant &
165166

166167
switch ( role )
167168
{
169+
case Qt::EditRole:
170+
{
171+
switch ( index.column() )
172+
{
173+
case Name:
174+
case Active:
175+
case Default:
176+
return false;
177+
178+
case Prefix:
179+
{
180+
QString prefix = value.toString();
181+
if ( prefix.isEmpty() )
182+
return false;
183+
mPrefixes.insert( filterForIndex( index ), prefix );
184+
emit dataChanged( index, index );
185+
return true;
186+
}
187+
}
188+
}
189+
190+
168191
case Qt::CheckStateRole:
169192
{
170193
bool checked = static_cast< Qt::CheckState >( value.toInt() ) == Qt::Checked;
@@ -203,7 +226,10 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
203226
switch ( index.column() )
204227
{
205228
case Name:
229+
break;
230+
206231
case Prefix:
232+
flags = flags | Qt::ItemIsEditable;
207233
break;
208234

209235
case Active:
@@ -242,6 +268,12 @@ void QgsLocatorFiltersModel::commitChanges()
242268
{
243269
QgsSettings settings;
244270

271+
QHash< QgsLocatorFilter *, QString >::const_iterator itp = mPrefixes.constBegin();
272+
for ( ; itp != mPrefixes.constEnd(); ++itp )
273+
{
274+
QgsLocatorFilter *filter = itp.key();
275+
mLocator->setCustomPrefix( filter, itp.value() );
276+
}
245277
QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin();
246278
for ( ; it != mEnabledChanges.constEnd(); ++it )
247279
{

‎src/app/locator/qgslocatoroptionswidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
9797
QgsLocator *mLocator = nullptr;
9898

9999
// changes are deferred to support cancelation
100+
QHash< QgsLocatorFilter *, QString > mPrefixes;
100101
QHash< QgsLocatorFilter *, bool > mEnabledChanges;
101102
QHash< QgsLocatorFilter *, bool > mDefaultChanges;
102103

‎src/core/locator/qgslocator.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
#include <QtConcurrent>
2121
#include <functional>
2222

23+
const QList<QString> QgsLocator::CORE_FILTERS = QList<QString>() << QStringLiteral( "actions" )
24+
<< QStringLiteral( "processing_alg" )
25+
<< QStringLiteral( "layertree" )
26+
<< QStringLiteral( "layouts" )
27+
<< QStringLiteral( "features" )
28+
<< QStringLiteral( "calculator" )
29+
<< QStringLiteral( "bookmarks" );
30+
2331
QgsLocator::QgsLocator( QObject *parent )
2432
: QObject( parent )
2533
{
@@ -52,32 +60,48 @@ QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
5260
return mPrefixedFilters;
5361
}
5462

63+
void QgsLocator::setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix )
64+
{
65+
if ( filter )
66+
{
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+
}
74+
}
75+
76+
QString QgsLocator::customPrefix( QgsLocatorFilter *filter ) const
77+
{
78+
return mPrefixedFilters.key( filter, QString() );
79+
}
80+
5581
void QgsLocator::registerFilter( QgsLocatorFilter *filter )
5682
{
5783
mFilters.append( filter );
5884
filter->setParent( this );
5985

60-
if ( !filter->prefix().isEmpty() )
86+
// restore settings
87+
QgsSettings settings;
88+
bool enabled = settings.value( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), true, QgsSettings::Section::Gui ).toBool();
89+
bool byDefault = settings.value( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), filter->useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();
90+
QString customPrefix = settings.value( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), filter->prefix(), QgsSettings::Section::Gui ).toString();
91+
92+
if ( !customPrefix.isEmpty() )
6193
{
62-
if ( filter->name() == QStringLiteral( "actions" ) || filter->name() == QStringLiteral( "processing_alg" )
63-
|| filter->name() == QStringLiteral( "layertree" ) || filter->name() == QStringLiteral( "layouts" )
64-
|| filter->name() == QStringLiteral( "features" ) || filter->name() == QStringLiteral( "calculator" )
65-
|| filter->name() == QStringLiteral( "bookmarks" ) )
94+
if ( CORE_FILTERS.contains( filter->name() ) )
6695
{
6796
//inbuilt filter, no prefix check
68-
mPrefixedFilters.insert( filter->prefix(), filter );
97+
mPrefixedFilters.insert( customPrefix, filter );
6998
}
70-
else if ( filter->prefix().length() >= 3 )
99+
else if ( customPrefix.length() >= 3 || customPrefix != filter->prefix() )
71100
{
72-
mPrefixedFilters.insert( filter->prefix(), filter );
101+
mPrefixedFilters.insert( customPrefix, filter );
73102
}
74103
}
75104

76-
// restore settings
77-
QgsSettings settings;
78-
bool enabled = settings.value( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), true, QgsSettings::Section::Gui ).toBool();
79-
bool byDefault = settings.value( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), filter->useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();
80-
81105
filter->setEnabled( enabled );
82106
filter->setUseWithoutPrefix( byDefault );
83107
}

‎src/core/locator/qgslocator.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class CORE_EXPORT QgsLocator : public QObject
5757

5858
public:
5959

60+
static const QList<QString> CORE_FILTERS;
61+
6062
/**
6163
* Constructor for QgsLocator.
6264
*/
@@ -100,6 +102,16 @@ class CORE_EXPORT QgsLocator : public QObject
100102
*/
101103
QMap< QString, QgsLocatorFilter *> prefixedFilters() const;
102104

105+
/**
106+
* Sets the custom prefix for a filter
107+
*/
108+
void setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix );
109+
110+
/**
111+
* Returns the custom prefix for a filter if defined, its regular prefix otherwise
112+
*/
113+
QString customPrefix( QgsLocatorFilter *filter ) const;
114+
103115
/**
104116
* Triggers the background fetching of filter results for a specified search \a string.
105117
* The \a context argument encapsulates the context relating to the search (such as a map

‎src/core/locator/qgslocatorfilter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
* *
1616
***************************************************************************/
1717

18+
#include <QThread>
1819

1920
#include "qgslocatorfilter.h"
2021
#include "qgsstringutils.h"
2122
#include "qgsfeedback.h"
22-
#include <QThread>
23+
2324

2425
QgsLocatorFilter::QgsLocatorFilter( QObject *parent )
2526
: QObject( parent )
2627
{
27-
2828
}
2929

3030
QgsLocatorFilter::Flags QgsLocatorFilter::flags() const
@@ -66,3 +66,4 @@ void QgsLocatorFilter::setUseWithoutPrefix( bool useWithoutPrefix )
6666
{
6767
mUseWithoutPrefix = useWithoutPrefix;
6868
}
69+

0 commit comments

Comments
 (0)
Please sign in to comment.