19#include "moc_qgslocator.cpp"
23#include <QtConcurrent>
33 << QStringLiteral(
"processing_alg" )
34 << QStringLiteral(
"layertree" )
35 << QStringLiteral(
"layouts" )
36 << QStringLiteral(
"features" )
37 << QStringLiteral(
"allfeatures" )
38 << QStringLiteral(
"calculator" )
39 << QStringLiteral(
"bookmarks" )
40 << QStringLiteral(
"optionpages" )
41 << QStringLiteral(
"edit_features" )
42 << QStringLiteral(
"goto" )
43 << QStringLiteral(
"nominatimgeocoder" ) ;
48 qRegisterMetaType<QgsLocatorResult>(
"QgsLocatorResult" );
54 qDeleteAll( mFilters );
60 mFilters.removeAll( filter );
66 if ( !prefix.isEmpty() )
68 QList<QgsLocatorFilter *>
filters = QList<QgsLocatorFilter *>();
71 if ( !filter->activePrefix().isEmpty() && filter->activePrefix().compare( prefix, Qt::CaseInsensitive ) == 0 )
86 QMap<QString, QgsLocatorFilter *>
filters = QMap<QString, QgsLocatorFilter *>();
89 if ( !filter->activePrefix().isEmpty() && filter->enabled() )
91 filters.insert( filter->activePrefix(), filter );
99 mFilters.append( filter );
100 filter->setParent(
this );
106 if ( prefix.isEmpty() )
108 prefix = filter->
prefix();
111 if ( !prefix.isEmpty() )
118 else if ( prefix.length() >= 3 || prefix != filter->
prefix() )
136 mAutocompletionList.clear();
142 cancelRunningQuery();
149 feedback = mOwnedFeedback.get();
153 mOwnedFeedback.reset(
nullptr );
155 mFeedback = feedback;
157 QList< QgsLocatorFilter * > activeFilters;
158 QString searchString = string;
159 QString prefix = searchString.left( std::max(
static_cast<int>( searchString.indexOf(
' ' ) ), 0 ) );
160 if ( !prefix.isEmpty() )
164 if ( filter->activePrefix().compare( prefix, Qt::CaseInsensitive ) == 0 && filter->enabled() )
166 activeFilters << filter;
171 if ( !activeFilters.isEmpty() )
173 searchString = searchString.mid( prefix.length() + 1 );
179 if ( filter->useWithoutPrefix() && filter->enabled() )
181 activeFilters << filter;
186 QList< QgsLocatorFilter *> threadedFilters;
189 filter->clearPreviousResults();
190 std::unique_ptr< QgsLocatorFilter > clone( filter->clone() );
198 result.filter = filter;
199 filterSentResult( result );
201 QStringList autoCompleteList = clone->prepare( searchString, context );
204 for (
int i = 0; i < autoCompleteList.length(); i++ )
206 autoCompleteList[i].prepend( QStringLiteral(
"%1 " ).arg( prefix ) );
209 mAutocompletionList.append( autoCompleteList );
214 clone->fetchResults( searchString, context, feedback );
218 threadedFilters.append( clone.release() );
222 mActiveThreads.clear();
225 QThread *thread =
new QThread();
226 mActiveThreads.append( thread );
227 filter->moveToThread( thread );
228 connect( thread, &QThread::started, filter, [filter, searchString, context, feedback]
230 int delay = filter->fetchResultsDelay();
235 QThread::msleep( 50 );
239 filter->fetchResults( searchString, context, feedback );
241 }, Qt::QueuedConnection );
244 connect( thread, &QThread::finished, thread, [
this, thread]
246 mActiveThreads.removeAll( thread );
247 if ( mActiveThreads.empty() )
250 connect( thread, &QThread::finished, thread, &QThread::deleteLater );
256 if ( mActiveThreads.empty() )
262 cancelRunningQuery();
273 return !mActiveThreads.empty();
280 if ( filter->enabled() )
282 filter->clearPreviousResults();
296void QgsLocator::cancelRunningQuery()
298 if ( !mActiveThreads.empty() )
302 while ( !mActiveThreads.empty() )
304 QCoreApplication::processEvents();
@ Critical
Critical/error message.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
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.
static const QgsSettingsEntryBool * settingsLocatorFilterDefault
Settings entry locator filter default value.
void registerFilter(QgsLocatorFilter *filter)
Registers a filter within the locator.
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 * settingsLocatorFilterEnabled
Settings entry locator filter enabled.
~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.
static const QgsSettingsEntryString * settingsLocatorFilterPrefix
Settings entry locator filter prefix.
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
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.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
T valueWithDefaultOverride(const T &defaultValueOverride, const QString &dynamicKeyPart=QString()) const
Returns the settings value with a defaultValueOverride and with an optional dynamicKeyPart.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
A boolean settings entry.
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