Skip to content
Permalink
Browse files

[FEATURE][locator] Add delayed fetching of results

  • Loading branch information
nirvn committed Dec 16, 2020
1 parent 748ad68 commit d03d3b3ea0cd0b255641f55f4a0a9eb53c1b3c8a
@@ -315,6 +315,27 @@ Logs a ``message`` to the log panel
.. versionadded:: 3.2
%End

int fetchResultsDelay() const;
%Docstring
Returns the delay (in milliseconds) for the filter to wait prior to fetching results.

.. seealso:: :py:func:`setFetchResultsDelay`

.. versionadded:: 3.18
%End

void setFetchResultsDelay( int delay );
%Docstring
Sets a ``delay`` (in milliseconds) for the filter to wait prior to fetching results.

.. seealso:: :py:func:`fetchResultsDelay`

.. note::

If the locator filter has a FastFlag, this value is ignored.

.. versionadded:: 3.18
%End

signals:

@@ -217,6 +217,14 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
filter->moveToThread( thread );
connect( thread, &QThread::started, filter, [filter, searchString, context, feedback]
{
int delay = filter->fetchResultsDelay();
while ( delay > 0 )
{
if ( feedback->isCanceled() )
break;
QThread::msleep( 50 );
delay -= 50;
}
if ( !feedback->isCanceled() )
filter->fetchResults( searchString, context, feedback );
filter->emit finished();
@@ -355,6 +355,20 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
*/
void logMessage( const QString &message, Qgis::MessageLevel level = Qgis::Info );

/**
* Returns the delay (in milliseconds) for the filter to wait prior to fetching results.
* \see setFetchResultsDelay()
* \since QGIS 3.18
*/
int fetchResultsDelay() const { return mFetchResultsDelay; }

/**
* Sets a \a delay (in milliseconds) for the filter to wait prior to fetching results.
* \see fetchResultsDelay()
* \note If the locator filter has a FastFlag, this value is ignored.
* \since QGIS 3.18
*/
void setFetchResultsDelay( int delay ) { mFetchResultsDelay = delay; }

signals:

@@ -374,6 +388,7 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
bool mEnabled = true;
bool mUseWithoutPrefix = true;
QString mActivePrefifx = QString();
int mFetchResultsDelay = 0;

};

@@ -147,6 +147,43 @@ def got_hit(result):

self.assertEqual(set(got_hit._results_), {'a0', 'a1', 'a2', 'b0', 'b1', 'b2'})

def testFetchingResultsDelayed(self):

def got_hit(result):
got_hit._results_.append(result.displayString)

got_hit._results_ = []

context = QgsLocatorContext()

# one filter
l = QgsLocator()
filter_a = test_filter('a')
filter_a.setFetchResultsDelay(100)

l.registerFilter(filter_a)

l.foundResult.connect(got_hit)
l.fetchResults('a', context)

for i in range(500):
sleep(0.002)
QCoreApplication.processEvents()

self.assertEqual(set(got_hit._results_), {'a0', 'a1', 'a2'})

# two filters
filter_b = test_filter('b')
l.registerFilter(filter_b)
got_hit._results_ = []
l.fetchResults('a', context)

for i in range(500):
sleep(0.002)
QCoreApplication.processEvents()

self.assertEqual(set(got_hit._results_), {'a0', 'a1', 'a2', 'b0', 'b1', 'b2'})

def testDeleteWhileFetchingResults(self):
"""
Delete locator whilst fetching results

0 comments on commit d03d3b3

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