Skip to content

Commit

Permalink
use multimap to allow using the same prefix for several filters
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 10, 2018
1 parent 37e9f3a commit 0a0de2e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 33 deletions.
21 changes: 18 additions & 3 deletions python/core/locator/qgslocator.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@





class QgsLocator : QObject
{
%Docstring
Expand Down Expand Up @@ -71,22 +73,35 @@ deregisterFilter() to deregister their filters upon plugin unload to avoid crash
.. seealso:: :py:func:`registerFilter`
%End

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

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

QMap< QString, QgsLocatorFilter *> prefixedFilters() const;
QStringList prefixes();
%Docstring
Returns the list of prefixes in use.
A prefix can have several filters

.. seealso:: :py:func:`filter`

.. versionadded:: 3.2
%End


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

.. deprecated:: since QGIS 3.2 use prefixes() and filters( const QString &prefix )

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

void setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix );
void setCustomPrefix( QgsLocatorFilter *filter, const QString &customPrefix );
%Docstring
Sets the custom prefix for a filter
%End
Expand Down
57 changes: 39 additions & 18 deletions src/core/locator/qgslocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ void QgsLocator::deregisterFilter( QgsLocatorFilter *filter )
{
cancelRunningQuery();
mFilters.removeAll( filter );
QString key = mPrefixedFilters.key( filter );
if ( !key.isEmpty() )
mPrefixedFilters.remove( key );
mPrefixedFilters.remove( mPrefixedFilters.key( filter ), filter );
delete filter;
}

QList<QgsLocatorFilter *> QgsLocator::filters()
QList<QgsLocatorFilter *> QgsLocator::filters( const QString &prefix )
{
return mFilters;
if ( !prefix.isEmpty() )
return mPrefixedFilters.values( prefix );
else
return mFilters;
}

QStringList QgsLocator::prefixes()
{
return mPrefixedFilters.keys();
}

QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
QMultiMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
{
return mPrefixedFilters;
}
Expand All @@ -64,12 +70,18 @@ void QgsLocator::setCustomPrefix( QgsLocatorFilter *filter, const QString &prefi
{
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 );
mPrefixedFilters.remove( mPrefixedFilters.key( filter ), filter );
if ( !prefix.isEmpty() && prefix != filter->prefix() )
{
QgsSettings().setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), prefix, QgsSettings::Section::Gui );
mPrefixedFilters.insert( prefix, filter );
}
else
{
// reset the setting if the string is empty or equal to the native prefix
// one should the default checkbox instead
QgsSettings().remove( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), QgsSettings::Section::Gui );
}
}
}

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

QList< QgsLocatorFilter * > activeFilters;
QString searchString = string;
if ( searchString.indexOf( ' ' ) > 0 )
QString prefix = searchString.left( std::max( searchString.indexOf( ' ' ), 0 ) );
if ( !prefix.isEmpty() )
{
QString prefix = searchString.left( searchString.indexOf( ' ' ) );
if ( mPrefixedFilters.contains( prefix ) && mPrefixedFilters.value( prefix )->enabled() )
QMultiMap<QString, QgsLocatorFilter *>::const_iterator it = mPrefixedFilters.constFind( prefix );
while ( it != mPrefixedFilters.constEnd() && it.key() == prefix )
{
activeFilters << mPrefixedFilters.value( prefix );
searchString = searchString.mid( prefix.length() + 1 );
QgsLocatorFilter *filter = it.value();
if ( filter->enabled() )
{
activeFilters << filter;
}
++it;
}
context.usingPrefix = !activeFilters.empty();
}
if ( activeFilters.isEmpty() )
if ( !activeFilters.isEmpty() )
{
searchString = searchString.mid( prefix.length() + 1 );
}
else
{
for ( QgsLocatorFilter *filter : qgis::as_const( mFilters ) )
{
Expand Down
39 changes: 31 additions & 8 deletions src/core/locator/qgslocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
#ifndef QGSLOCATOR_H
#define QGSLOCATOR_H

#include <QObject>
#include <QFuture>
#include <QFutureWatcher>
#include <QMap>
#include <memory>

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgslocatorfilter.h"
#include "qgsfeedback.h"
#include "qgslocatorcontext.h"
#include <QObject>
#include <QFuture>
#include <QFutureWatcher>
#include <memory>


/**
* \class QgsLocator
Expand Down Expand Up @@ -93,19 +96,39 @@ class CORE_EXPORT QgsLocator : public QObject
* Returns the list of filters registered in the locator.
* \see prefixedFilters()
*/
QList< QgsLocatorFilter *> filters();
QList< QgsLocatorFilter *> filters( const QString &prefix = QString() );

/**
* Returns the list of prefixes in use.
* A prefix can have several filters
* \see filter()
* \since QGIS 3.2
*/
QStringList prefixes();

#ifndef SIP_RUN

/**
* Returns a map of prefix to filter, for all registered filters
* with valid prefixes.
* \see filters()
*/
QMultiMap<QString, QgsLocatorFilter *> prefixedFilters() const;
#else

/**
* Returns a map of prefix to filter, for all registered filters
* with valid prefixes.
* \deprecated since QGIS 3.2 use prefixes() and filters( const QString &prefix )
* \see filters()
*/
QMap< QString, QgsLocatorFilter *> prefixedFilters() const;
Q_DECL_DEPRECATED QMap<QString, QgsLocatorFilter *> prefixedFilters() const;
#endif

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

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

QList< QgsLocatorFilter * > mFilters;
QMap< QString, QgsLocatorFilter *> mPrefixedFilters;
QMultiMap< QString, QgsLocatorFilter *> mPrefixedFilters;
QList< QThread * > mActiveThreads;

void cancelRunningQuery();
Expand Down
8 changes: 4 additions & 4 deletions src/gui/locator/qgslocatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
void QgsLocatorWidget::configMenuAboutToShow()
{
mMenu->clear();
QMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
QMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
QMultiMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
QMultiMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
for ( ; fIt != filters.constEnd(); ++fIt )
{
if ( !fIt.value()->enabled() )
Expand Down Expand Up @@ -422,8 +422,8 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat
return;
}

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

0 comments on commit 0a0de2e

Please sign in to comment.