Skip to content
Permalink
Browse files

use multimap to allow using the same prefix for several filters

  • Loading branch information
3nids committed May 9, 2018
1 parent 37e9f3a commit 0a0de2ec9f1df427569f4447c17b3779c9b06f79
@@ -9,6 +9,8 @@





class QgsLocator : QObject
{
%Docstring
@@ -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
@@ -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;
}
@@ -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 );
}
}
}

@@ -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 ) )
{
@@ -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
@@ -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
@@ -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();
@@ -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() )
@@ -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() )

0 comments on commit 0a0de2e

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