Skip to content
Permalink
Browse files

allow customizing prefix of locator filters

  • Loading branch information
3nids committed May 8, 2018
1 parent 4c92de9 commit 37e9f3abca657c256842e7bf06f268ef716b739a
@@ -38,6 +38,8 @@ call to fetchResults().
%End
public:

static const QList<QString> CORE_FILTERS;

QgsLocator( QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsLocator.
@@ -82,6 +84,16 @@ Returns a map of prefix to filter, for all registered filters
with valid prefixes.

.. seealso:: :py:func:`filters`
%End

void setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix );
%Docstring
Sets the custom prefix for a filter
%End

QString customPrefix( QgsLocatorFilter *filter ) const;
%Docstring
Returns the custom prefix for a filter if defined, its regular prefix otherwise
%End

void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback = 0 );
@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgslocatoroptionswidget.h"

#include "qgslocatorwidget.h"
#include "qgssettings.h"

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

case Prefix:
return filterForIndex( index )->prefix();
return mPrefixes.value( filterForIndex( index ), mLocator->customPrefix( filterForIndex( index ) ) );

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

switch ( role )
{
case Qt::EditRole:
{
switch ( index.column() )
{
case Name:
case Active:
case Default:
return false;

case Prefix:
{
QString prefix = value.toString();
if ( prefix.isEmpty() )
return false;
mPrefixes.insert( filterForIndex( index ), prefix );
emit dataChanged( index, index );
return true;
}
}
}


case Qt::CheckStateRole:
{
bool checked = static_cast< Qt::CheckState >( value.toInt() ) == Qt::Checked;
@@ -203,7 +226,10 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
switch ( index.column() )
{
case Name:
break;

case Prefix:
flags = flags | Qt::ItemIsEditable;
break;

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

QHash< QgsLocatorFilter *, QString >::const_iterator itp = mPrefixes.constBegin();
for ( ; itp != mPrefixes.constEnd(); ++itp )
{
QgsLocatorFilter *filter = itp.key();
mLocator->setCustomPrefix( filter, itp.value() );
}
QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin();
for ( ; it != mEnabledChanges.constEnd(); ++it )
{
@@ -97,6 +97,7 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
QgsLocator *mLocator = nullptr;

// changes are deferred to support cancelation
QHash< QgsLocatorFilter *, QString > mPrefixes;
QHash< QgsLocatorFilter *, bool > mEnabledChanges;
QHash< QgsLocatorFilter *, bool > mDefaultChanges;

@@ -20,6 +20,14 @@
#include <QtConcurrent>
#include <functional>

const QList<QString> QgsLocator::CORE_FILTERS = QList<QString>() << QStringLiteral( "actions" )
<< QStringLiteral( "processing_alg" )
<< QStringLiteral( "layertree" )
<< QStringLiteral( "layouts" )
<< QStringLiteral( "features" )
<< QStringLiteral( "calculator" )
<< QStringLiteral( "bookmarks" );

QgsLocator::QgsLocator( QObject *parent )
: QObject( parent )
{
@@ -52,32 +60,48 @@ QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
return mPrefixedFilters;
}

void QgsLocator::setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix )
{
if ( filter )
{
QgsSettings().setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), prefix, QgsSettings::Section::Gui );
QString oldPrefix = mPrefixedFilters.key( filter );
if ( !oldPrefix.isEmpty() )
mPrefixedFilters.remove( oldPrefix );

mPrefixedFilters.insert( prefix, filter );
}
}

QString QgsLocator::customPrefix( QgsLocatorFilter *filter ) const
{
return mPrefixedFilters.key( filter, QString() );
}

void QgsLocator::registerFilter( QgsLocatorFilter *filter )
{
mFilters.append( filter );
filter->setParent( this );

if ( !filter->prefix().isEmpty() )
// restore settings
QgsSettings settings;
bool enabled = settings.value( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), true, QgsSettings::Section::Gui ).toBool();
bool byDefault = settings.value( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), filter->useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();
QString customPrefix = settings.value( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), filter->prefix(), QgsSettings::Section::Gui ).toString();

if ( !customPrefix.isEmpty() )
{
if ( filter->name() == QStringLiteral( "actions" ) || filter->name() == QStringLiteral( "processing_alg" )
|| filter->name() == QStringLiteral( "layertree" ) || filter->name() == QStringLiteral( "layouts" )
|| filter->name() == QStringLiteral( "features" ) || filter->name() == QStringLiteral( "calculator" )
|| filter->name() == QStringLiteral( "bookmarks" ) )
if ( CORE_FILTERS.contains( filter->name() ) )
{
//inbuilt filter, no prefix check
mPrefixedFilters.insert( filter->prefix(), filter );
mPrefixedFilters.insert( customPrefix, filter );
}
else if ( filter->prefix().length() >= 3 )
else if ( customPrefix.length() >= 3 || customPrefix != filter->prefix() )
{
mPrefixedFilters.insert( filter->prefix(), filter );
mPrefixedFilters.insert( customPrefix, filter );
}
}

// restore settings
QgsSettings settings;
bool enabled = settings.value( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), true, QgsSettings::Section::Gui ).toBool();
bool byDefault = settings.value( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), filter->useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();

filter->setEnabled( enabled );
filter->setUseWithoutPrefix( byDefault );
}
@@ -57,6 +57,8 @@ class CORE_EXPORT QgsLocator : public QObject

public:

static const QList<QString> CORE_FILTERS;

/**
* Constructor for QgsLocator.
*/
@@ -100,6 +102,16 @@ class CORE_EXPORT QgsLocator : public QObject
*/
QMap< QString, QgsLocatorFilter *> prefixedFilters() const;

/**
* Sets the custom prefix for a filter
*/
void setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix );

/**
* Returns the custom prefix for a filter if defined, its regular prefix otherwise
*/
QString customPrefix( QgsLocatorFilter *filter ) const;

/**
* Triggers the background fetching of filter results for a specified search \a string.
* The \a context argument encapsulates the context relating to the search (such as a map
@@ -15,16 +15,16 @@
* *
***************************************************************************/

#include <QThread>

#include "qgslocatorfilter.h"
#include "qgsstringutils.h"
#include "qgsfeedback.h"
#include <QThread>


QgsLocatorFilter::QgsLocatorFilter( QObject *parent )
: QObject( parent )
{

}

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

0 comments on commit 37e9f3a

Please sign in to comment.
You can’t perform that action at this time.