Skip to content
Permalink
Browse files

remove map from QgsLocator and rename custom to active prefix

the active/custom prefix info is now accessible only from QgsLocatorFilter
  • Loading branch information
3nids committed May 11, 2018
1 parent 6459d24 commit 0a6f88a4e54e67b64f7f69f9f39fe7bcb1f25e8d
@@ -73,32 +73,23 @@ 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.

:param prefix: If prefix is not empty, the list returned corresponds to the filter with the given active prefix

.. seealso:: :py:func:`prefixedFilters`
%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 &customPrefix );
%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
.. deprecated:: since QGIS 3.2 use filters() instead
%End

void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback = 0 );
@@ -124,6 +124,12 @@ results from this filter.
as these are reserved for core QGIS functions. If a plugin registers
a filter with a prefix shorter than 3 characters then the prefix will
be ignored.

.. note::

Prefixes might be overriden by user preferences.

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

virtual void prepare( const QString &string, const QgsLocatorContext &context );
@@ -177,6 +183,17 @@ is entered.
.. seealso:: :py:func:`useWithoutPrefix`
%End

QString activePrefix() const;
%Docstring
Returns true if the filter should be used when no prefix
is entered.

.. seealso:: :py:func:`setActivePrefix`

.. versionadded:: 3.2
%End


static bool stringMatches( const QString &candidate, const QString &search );
%Docstring
Tests a ``candidate`` string to see if it should be considered a match for
@@ -120,7 +120,7 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
return filterForIndex( index )->displayName();

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

case Active:
case Default:
@@ -278,7 +278,17 @@ void QgsLocatorFiltersModel::commitChanges()
for ( ; itp != mPrefixes.constEnd(); ++itp )
{
QgsLocatorFilter *filter = itp.key();
mLocator->setCustomPrefix( filter, itp.value() );
QString activePrefix = itp.value();
if ( !activePrefix.isEmpty() && activePrefix != filter->prefix() )
{
filter->setActivePrefix( activePrefix );
settings.setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), activePrefix, QgsSettings::Section::Gui );
}
else
{
filter->setActivePrefix( QString() );
settings.remove( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), QgsSettings::Section::Gui );
}
}
QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin();
for ( ; it != mEnabledChanges.constEnd(); ++it )
@@ -39,7 +39,6 @@ class QgsLocatorOptionsWidget : public QWidget, private Ui::QgsLocatorOptionsWid
void configureCurrentFilter();

private:

QgsLocatorWidget *mLocatorWidget = nullptr;
QgsLocator *mLocator = nullptr;
QgsLocatorFiltersModel *mModel = nullptr;
@@ -44,42 +44,40 @@ void QgsLocator::deregisterFilter( QgsLocatorFilter *filter )
{
cancelRunningQuery();
mFilters.removeAll( filter );
mPrefixedFilters.remove( mPrefixedFilters.key( filter ), filter );
delete filter;
}

QList<QgsLocatorFilter *> QgsLocator::filters()
QList<QgsLocatorFilter *> QgsLocator::filters( const QString &prefix )
{
return mFilters;
}

QMultiMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
{
return mPrefixedFilters;
}

void QgsLocator::setCustomPrefix( QgsLocatorFilter *filter, const QString &prefix )
{
if ( filter )
if ( !prefix.isEmpty() )
{
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
QList<QgsLocatorFilter *> filters = QList<QgsLocatorFilter *>();
for ( QgsLocatorFilter *filter : mFilters )
{
// 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 );
if ( !filter->activePrefix().isEmpty() && filter->activePrefix() == prefix )
{
filters << filter;
}
}
return filters;
}
else
{
return mFilters;
}
}

QString QgsLocator::customPrefix( QgsLocatorFilter *filter ) const
QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
{
return mPrefixedFilters.key( filter, QString() );
QMap<QString, QgsLocatorFilter *> filters = QMap<QString, QgsLocatorFilter *>();
for ( QgsLocatorFilter *filter : mFilters )
{
if ( !filter->activePrefix().isEmpty() && filter->enabled() )
{
filters.insertMulti( filter->activePrefix(), filter );
}
}
return filters;
}

void QgsLocator::registerFilter( QgsLocatorFilter *filter )
@@ -91,18 +89,22 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter )
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();
QString prefix = settings.value( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), filter->prefix(), QgsSettings::Section::Gui ).toString();
if ( prefix.isEmpty() )
{
prefix = filter->prefix();
}

if ( !customPrefix.isEmpty() )
if ( !prefix.isEmpty() )
{
if ( CORE_FILTERS.contains( filter->name() ) )
{
//inbuilt filter, no prefix check
mPrefixedFilters.insert( customPrefix, filter );
filter->setActivePrefix( prefix );
}
else if ( customPrefix.length() >= 3 || customPrefix != filter->prefix() )
else if ( prefix.length() >= 3 || prefix != filter->prefix() )
{
mPrefixedFilters.insert( customPrefix, filter );
filter->setActivePrefix( prefix );
}
}

@@ -136,15 +138,12 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
QString prefix = searchString.left( std::max( searchString.indexOf( ' ' ), 0 ) );
if ( !prefix.isEmpty() )
{
QMultiMap<QString, QgsLocatorFilter *>::const_iterator it = mPrefixedFilters.constFind( prefix );
while ( it != mPrefixedFilters.constEnd() && it.key() == prefix )
for ( QgsLocatorFilter *filter : qgis::as_const( mFilters ) )
{
QgsLocatorFilter *filter = it.value();
if ( filter->enabled() )
if ( filter->activePrefix() == prefix && filter->enabled() )
{
activeFilters << filter;
}
++it;
}
context.usingPrefix = !activeFilters.empty();
}
@@ -95,38 +95,18 @@ class CORE_EXPORT QgsLocator : public QObject

/**
* Returns the list of filters registered in the locator.
* \param prefix If prefix is not empty, the list returned corresponds to the filter with the given active prefix
* \see prefixedFilters()
*/
QList< QgsLocatorFilter *> filters();

#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
QList< QgsLocatorFilter *> filters( const QString &prefix = QString() );

/**
* 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()
* \deprecated since QGIS 3.2 use filters() instead
*/
Q_DECL_DEPRECATED QMap<QString, QgsLocatorFilter *> prefixedFilters() const;
#endif

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

/**
* 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.
@@ -183,7 +163,6 @@ class CORE_EXPORT QgsLocator : public QObject
std::unique_ptr< QgsFeedback > mOwnedFeedback;

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

void cancelRunningQuery();
@@ -67,3 +67,16 @@ void QgsLocatorFilter::setUseWithoutPrefix( bool useWithoutPrefix )
mUseWithoutPrefix = useWithoutPrefix;
}

QString QgsLocatorFilter::activePrefix() const
{
if ( mActivePrefifx.isEmpty() )
return prefix();
else
return mActivePrefifx;
}

void QgsLocatorFilter::setActivePrefix( const QString &activePrefix )
{
mActivePrefifx = activePrefix;
}

@@ -155,6 +155,8 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
* as these are reserved for core QGIS functions. If a plugin registers
* a filter with a prefix shorter than 3 characters then the prefix will
* be ignored.
* \note Prefixes might be overriden by user preferences.
* \see activePrefix()
*/
virtual QString prefix() const { return QString(); }

@@ -207,6 +209,22 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
*/
void setUseWithoutPrefix( bool useWithoutPrefix );

/**
* Returns true if the filter should be used when no prefix
* is entered.
* \see setActivePrefix()
* \since QGIS 3.2
*/
QString activePrefix() const;

/**
* Sets whether the filter should be used when no prefix
* is entered.
* \see activePrefix()
* \since QGIS 3.2
*/
void setActivePrefix( const QString &activePrefix ) SIP_SKIP;

/**
* Tests a \a candidate string to see if it should be considered a match for
* a specified \a search string.
@@ -258,6 +276,7 @@ class CORE_EXPORT QgsLocatorFilter : public QObject

bool mEnabled = true;
bool mUseWithoutPrefix = true;
QString mActivePrefifx = QString();

};

@@ -260,14 +260,12 @@ void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
void QgsLocatorWidget::configMenuAboutToShow()
{
mMenu->clear();
QMultiMap< QString, QgsLocatorFilter *> filters = mLocator->prefixedFilters();
QMultiMap< QString, QgsLocatorFilter *>::const_iterator fIt = filters.constBegin();
for ( ; fIt != filters.constEnd(); ++fIt )
for ( QgsLocatorFilter *filter : mLocator->filters() )
{
if ( !fIt.value()->enabled() )
if ( !filter->enabled() )
continue;

QAction *action = new QAction( fIt.value()->displayName(), mMenu );
QAction *action = new QAction( filter->displayName(), mMenu );
connect( action, &QAction::triggered, this, [ = ]
{
QString currentText = mLineEdit->text();
@@ -276,15 +274,15 @@ void QgsLocatorWidget::configMenuAboutToShow()
else
{
QStringList parts = currentText.split( ' ' );
if ( parts.count() > 1 && mLocator->prefixedFilters().contains( parts.at( 0 ) ) )
if ( parts.count() > 1 && mLocator->filters( parts.at( 0 ) ).count() > 0 )
{
parts.pop_front();
currentText = parts.join( ' ' );
}
}

mLineEdit->setText( fIt.key() + ' ' + currentText );
mLineEdit->setSelection( fIt.key().length() + 1, currentText.length() );
mLineEdit->setText( filter->activePrefix() + ' ' + currentText );
mLineEdit->setSelection( filter->activePrefix().length() + 1, currentText.length() );
} );
mMenu->addAction( action );
}
@@ -422,20 +420,18 @@ void QgsLocatorFilterFilter::fetchResults( const QString &string, const QgsLocat
return;
}

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

if ( fIt.value() == this || !fIt.value() || !fIt.value()->enabled() )
if ( filter == this || !filter || !filter->enabled() )
continue;

QgsLocatorResult result;
result.displayString = fIt.key();
result.description = fIt.value()->displayName();
result.userData = fIt.key() + ' ';
result.displayString = filter->activePrefix();
result.description = filter->displayName();
result.userData = filter->activePrefix() + ' ';
result.icon = QgsApplication::getThemeIcon( QStringLiteral( "/search.svg" ) );
emit resultFetched( result );
}

0 comments on commit 0a6f88a

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