19 #include "qgssettings.h"
20 #include <QtConcurrent>
24 << QStringLiteral(
"processing_alg" )
25 << QStringLiteral(
"layertree" )
26 << QStringLiteral(
"layouts" )
27 << QStringLiteral(
"features" )
28 << QStringLiteral(
"allfeatures" )
29 << QStringLiteral(
"calculator" )
30 << QStringLiteral(
"bookmarks" )
31 << QStringLiteral(
"optionpages" )
32 << QStringLiteral(
"edit_features" )
33 << QStringLiteral(
"goto" )
34 << QStringLiteral(
"nominatimgeocoder" ) ;
39 qRegisterMetaType<QgsLocatorResult>(
"QgsLocatorResult" );
45 qDeleteAll( mFilters );
51 mFilters.removeAll( filter );
57 if ( !prefix.isEmpty() )
59 QList<QgsLocatorFilter *>
filters = QList<QgsLocatorFilter *>();
62 if ( !filter->activePrefix().isEmpty() && filter->activePrefix().compare( prefix, Qt::CaseInsensitive ) == 0 )
77 QMap<QString, QgsLocatorFilter *>
filters = QMap<QString, QgsLocatorFilter *>();
80 if ( !filter->activePrefix().isEmpty() && filter->enabled() )
82 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
83 filters.insertMulti( filter->activePrefix(), filter );
85 filters.insert( filter->activePrefix(), filter );
94 mFilters.append( filter );
95 filter->setParent(
this );
101 if ( prefix.isEmpty() )
103 prefix = filter->
prefix();
106 if ( !prefix.isEmpty() )
113 else if ( prefix.length() >= 3 || prefix != filter->
prefix() )
131 mAutocompletionList.clear();
137 cancelRunningQuery();
144 feedback = mOwnedFeedback.get();
148 mOwnedFeedback.reset(
nullptr );
150 mFeedback = feedback;
152 QList< QgsLocatorFilter * > activeFilters;
153 QString searchString = string;
154 QString prefix = searchString.left( std::max(
static_cast<int>( searchString.indexOf(
' ' ) ), 0 ) );
155 if ( !prefix.isEmpty() )
159 if ( filter->activePrefix().compare( prefix, Qt::CaseInsensitive ) == 0 && filter->enabled() )
161 activeFilters << filter;
166 if ( !activeFilters.isEmpty() )
168 searchString = searchString.mid( prefix.length() + 1 );
174 if ( filter->useWithoutPrefix() && filter->enabled() )
176 activeFilters << filter;
181 QList< QgsLocatorFilter *> threadedFilters;
184 filter->clearPreviousResults();
185 std::unique_ptr< QgsLocatorFilter > clone( filter->clone() );
188 result.filter = filter;
189 filterSentResult( result );
191 QStringList autoCompleteList = clone->prepare( searchString, context );
194 for (
int i = 0; i < autoCompleteList.length(); i++ )
196 autoCompleteList[i].prepend( QStringLiteral(
"%1 " ).arg( prefix ) );
199 mAutocompletionList.append( autoCompleteList );
204 clone->fetchResults( searchString, context, feedback );
208 threadedFilters.append( clone.release() );
212 mActiveThreads.clear();
215 QThread *thread =
new QThread();
216 mActiveThreads.append( thread );
217 filter->moveToThread( thread );
218 connect( thread, &QThread::started, filter, [filter, searchString, context, feedback]
220 int delay = filter->fetchResultsDelay();
225 QThread::msleep( 50 );
229 filter->fetchResults( searchString, context, feedback );
231 }, Qt::QueuedConnection );
234 connect( thread, &QThread::finished, thread, [
this, thread]
236 mActiveThreads.removeAll( thread );
237 if ( mActiveThreads.empty() )
240 connect( thread, &QThread::finished, thread, &QThread::deleteLater );
246 if ( mActiveThreads.empty() )
252 cancelRunningQuery();
263 return !mActiveThreads.empty();
270 if ( filter->enabled() )
272 filter->clearPreviousResults();
286 void QgsLocator::cancelRunningQuery()
288 if ( !mActiveThreads.empty() )
292 while ( !mActiveThreads.empty() )
294 QCoreApplication::processEvents();
Base class for feedback objects to be used for cancellation of something running in a worker thread.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Encapsulates the properties relating to the context of a locator search.
bool usingPrefix
Will be true if search is being conducted using a filter prefix.
Abstract base class for filters which collect locator results.
void setEnabled(bool enabled)
Sets whether the filter is enabled.
void setActivePrefix(const QString &activePrefix)
Sets the prefix as being used by the locator.
virtual QString prefix() const
Returns the search prefix character(s) for this filter.
bool useWithoutPrefix() const
Returns true if the filter should be used when no prefix is entered.
@ FlagFast
Filter finds results quickly and can be safely run in the main thread.
void setUseWithoutPrefix(bool useWithoutPrefix)
Sets whether the filter should be used when no prefix is entered.
void finished()
Emitted when the filter finishes fetching results.
virtual QString name() const =0
Returns the unique name for the filter.
void resultFetched(const QgsLocatorResult &result)
Should be emitted by filters whenever they encounter a matching result during within their fetchResul...
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
static const QList< QString > CORE_FILTERS
List of core filters (i.e. not plugin filters)
void searchPrepared()
Emitted when locator has prepared the search (.
void cancel()
Cancels any current running query, and blocks until query is completely canceled by all filters.
QgsLocator(QObject *parent=nullptr)
Constructor for QgsLocator.
void registerFilter(QgsLocatorFilter *filter)
Registers a filter within the locator.
static const QgsSettingsEntryBool settingsLocatorFilterEnabled
Settings entry locator filter enabled.
void finished()
Emitted when locator has finished a query, either as a result of successful completion or early cance...
void foundResult(const QgsLocatorResult &result)
Emitted whenever a filter encounters a matching result after the fetchResults() method is called.
void fetchResults(const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback=nullptr)
Triggers the background fetching of filter results for a specified search string.
void clearPreviousResults()
Will call clearPreviousResults on all filters.
static const QgsSettingsEntryBool settingsLocatorFilterDefault
Settings entry locator filter default value.
~QgsLocator() override
Destructor for QgsLocator.
Q_DECL_DEPRECATED QMap< QString, QgsLocatorFilter * > prefixedFilters() const
Returns a map of prefix to filter, for all registered filters with valid prefixes.
bool isRunning() const
Returns true if a query is currently being executed by the locator.
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
static const QgsSettingsEntryString settingsLocatorFilterPrefix
Settings entry locator filter prefix.
QList< QgsLocatorFilter * > filters(const QString &prefix=QString())
Returns the list of filters registered in the locator.
void deregisterFilter(QgsLocatorFilter *filter)
Deregisters a filter from the locator and deletes it.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c