Skip to content

Commit

Permalink
[locator] add clearPreviousResults virtual method (#7256)
Browse files Browse the repository at this point in the history
* [locator] add clearPreviousResults virtual method

* also clear previous results when triggering result
  • Loading branch information
3nids committed Jun 17, 2018
1 parent 7a9b298 commit 8503896
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/locator/qgslocator.sip.in
Expand Up @@ -123,6 +123,13 @@ take some time to cancel after calling this.
bool isRunning() const;
%Docstring
Returns true if a query is currently being executed by the locator.
%End

void clearPreviousResults();
%Docstring
Will call clearPreviousResults on all filters

.. versionadded:: 3.2
%End

signals:
Expand Down
11 changes: 11 additions & 0 deletions python/core/auto_generated/locator/qgslocatorfilter.sip.in
Expand Up @@ -172,6 +172,17 @@ by a user. The filter subclass must implement logic here
to perform the desired operation for the search result.
E.g. a file search filter would open file associated with the triggered
result.
%End

virtual void clearPreviousResults();
%Docstring
This method will be called on main thread on the original filter (not a clone)
before fetching results or before triggering a result to clear any change made
by a former call to triggerResult.
For instance, this can be used to remove any on-canvas rubber bands which have been created
when a previous search result was triggered.

.. versionadded:: 3.2
%End

bool useWithoutPrefix() const;
Expand Down
12 changes: 12 additions & 0 deletions src/core/locator/qgslocator.cpp
Expand Up @@ -172,6 +172,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
QList< QgsLocatorFilter *> threadedFilters;
for ( QgsLocatorFilter *filter : qgis::as_const( activeFilters ) )
{
filter->clearPreviousResults();
std::unique_ptr< QgsLocatorFilter > clone( filter->clone() );
connect( clone.get(), &QgsLocatorFilter::resultFetched, clone.get(), [this, filter]( QgsLocatorResult result )
{
Expand Down Expand Up @@ -236,6 +237,17 @@ bool QgsLocator::isRunning() const
return !mActiveThreads.empty();
}

void QgsLocator::clearPreviousResults()
{
for ( QgsLocatorFilter *filter : qgis::as_const( mFilters ) )
{
if ( filter->enabled() )
{
filter->clearPreviousResults();
}
}
}

void QgsLocator::filterSentResult( QgsLocatorResult result )
{
// if query has been canceled then discard any results we receive
Expand Down
6 changes: 6 additions & 0 deletions src/core/locator/qgslocator.h
Expand Up @@ -139,6 +139,12 @@ class CORE_EXPORT QgsLocator : public QObject
*/
bool isRunning() const;

/**
* Will call clearPreviousResults on all filters
* \since QGIS 3.2
*/
void clearPreviousResults();

signals:

/**
Expand Down
10 changes: 10 additions & 0 deletions src/core/locator/qgslocatorfilter.h
Expand Up @@ -207,6 +207,16 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
*/
virtual void triggerResult( const QgsLocatorResult &result ) = 0;

/**
* This method will be called on main thread on the original filter (not a clone)
* before fetching results or before triggering a result to clear any change made
* by a former call to triggerResult.
* For instance, this can be used to remove any on-canvas rubber bands which have been created
* when a previous search result was triggered.
* \since QGIS 3.2
*/
virtual void clearPreviousResults() {}

/**
* Returns true if the filter should be used when no prefix
* is entered.
Expand Down
1 change: 1 addition & 0 deletions src/gui/locator/qgslocatorwidget.cpp
Expand Up @@ -339,6 +339,7 @@ void QgsLocatorWidget::acceptCurrentEntry()
QgsLocatorResult result = mProxyModel->data( index, QgsLocatorModel::ResultDataRole ).value< QgsLocatorResult >();
mResultsContainer->hide();
mLineEdit->clearFocus();
mLocator->clearPreviousResults();
result.filter->triggerResult( result );
}
}
Expand Down

0 comments on commit 8503896

Please sign in to comment.