Skip to content

Commit

Permalink
allow customizing prefix of locator filters
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 9, 2018
1 parent 4c92de9 commit 37e9f3a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 16 deletions.
12 changes: 12 additions & 0 deletions python/core/locator/qgslocator.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ call to fetchResults().
%End
public:

static const QList<QString> CORE_FILTERS;

QgsLocator( QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsLocator.
Expand Down Expand Up @@ -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 );
Expand Down
34 changes: 33 additions & 1 deletion src/app/locator/qgslocatoroptionswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgslocatoroptionswidget.h"

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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 )
{
Expand Down
1 change: 1 addition & 0 deletions src/app/locator/qgslocatoroptionswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
50 changes: 37 additions & 13 deletions src/core/locator/qgslocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 );
}
Expand Down
12 changes: 12 additions & 0 deletions src/core/locator/qgslocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CORE_EXPORT QgsLocator : public QObject

public:

static const QList<QString> CORE_FILTERS;

/**
* Constructor for QgsLocator.
*/
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/core/locator/qgslocatorfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,3 +66,4 @@ void QgsLocatorFilter::setUseWithoutPrefix( bool useWithoutPrefix )
{
mUseWithoutPrefix = useWithoutPrefix;
}

0 comments on commit 37e9f3a

Please sign in to comment.