QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgslocator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslocator.cpp
3 --------------
4 begin : May 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgslocator.h"
19#include "qgsmessagelog.h"
21
22#include <QtConcurrent>
23#include <functional>
24
25const QgsSettingsEntryBool *QgsLocator::settingsLocatorFilterEnabled = new QgsSettingsEntryBool( QStringLiteral( "enabled" ), sTreeLocatorFilters, true, QObject::tr( "Locator filter enabled" ) );
26
27const QgsSettingsEntryBool *QgsLocator::settingsLocatorFilterDefault = new QgsSettingsEntryBool( QStringLiteral( "default" ), sTreeLocatorFilters, false, QObject::tr( "Locator filter default value" ) );
28
29const QgsSettingsEntryString *QgsLocator::settingsLocatorFilterPrefix = new QgsSettingsEntryString( QStringLiteral( "prefix" ), sTreeLocatorFilters, QString(), QObject::tr( "Locator filter prefix" ) );
30
31const QList<QString> QgsLocator::CORE_FILTERS = QList<QString>() << QStringLiteral( "actions" )
32 << QStringLiteral( "processing_alg" )
33 << QStringLiteral( "layertree" )
34 << QStringLiteral( "layouts" )
35 << QStringLiteral( "features" )
36 << QStringLiteral( "allfeatures" )
37 << QStringLiteral( "calculator" )
38 << QStringLiteral( "bookmarks" )
39 << QStringLiteral( "optionpages" )
40 << QStringLiteral( "edit_features" )
41 << QStringLiteral( "goto" )
42 << QStringLiteral( "nominatimgeocoder" ) ;
43
44QgsLocator::QgsLocator( QObject *parent )
45 : QObject( parent )
46{
47 qRegisterMetaType<QgsLocatorResult>( "QgsLocatorResult" );
48}
49
51{
52 cancelRunningQuery();
53 qDeleteAll( mFilters );
54}
55
57{
58 cancelRunningQuery();
59 mFilters.removeAll( filter );
60 delete filter;
61}
62
63QList<QgsLocatorFilter *> QgsLocator::filters( const QString &prefix )
64{
65 if ( !prefix.isEmpty() )
66 {
67 QList<QgsLocatorFilter *> filters = QList<QgsLocatorFilter *>();
68 for ( QgsLocatorFilter *filter : mFilters )
69 {
70 if ( !filter->activePrefix().isEmpty() && filter->activePrefix().compare( prefix, Qt::CaseInsensitive ) == 0 )
71 {
72 filters << filter;
73 }
74 }
75 return filters;
76 }
77 else
78 {
79 return mFilters;
80 }
81}
82
83QMap<QString, QgsLocatorFilter *> QgsLocator::prefixedFilters() const
84{
85 QMap<QString, QgsLocatorFilter *> filters = QMap<QString, QgsLocatorFilter *>();
86 for ( QgsLocatorFilter *filter : mFilters )
87 {
88 if ( !filter->activePrefix().isEmpty() && filter->enabled() )
89 {
90 filters.insert( filter->activePrefix(), filter );
91 }
92 }
93 return filters;
94}
95
97{
98 mFilters.append( filter );
99 filter->setParent( this );
100
101 // restore settings
102 bool enabled = QgsLocator::settingsLocatorFilterEnabled->value( filter->name() );
104 QString prefix = QgsLocator::settingsLocatorFilterPrefix->valueWithDefaultOverride( filter->prefix(), filter->name() );
105 if ( prefix.isEmpty() )
106 {
107 prefix = filter->prefix();
108 }
109
110 if ( !prefix.isEmpty() )
111 {
112 if ( CORE_FILTERS.contains( filter->name() ) )
113 {
114 //inbuilt filter, no prefix check
115 filter->setActivePrefix( prefix );
116 }
117 else if ( prefix.length() >= 3 || prefix != filter->prefix() )
118 {
119 // for plugins either the native prefix is >3 char or it has been customized by user
120 filter->setActivePrefix( prefix );
121 }
122 else
123 {
124 // otherwise set it to empty string (not NULL)
125 filter->setActivePrefix( QString( "" ) );
126 }
127 }
128
129 filter->setEnabled( enabled );
130 filter->setUseWithoutPrefix( byDefault );
131}
132
133void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c, QgsFeedback *feedback )
134{
135 mAutocompletionList.clear();
136
137 QgsLocatorContext context( c );
138 // ideally this should not be required, as well behaved callers
139 // will NOT fire up a new fetchResults call while an existing one is
140 // operating/waiting to be canceled...
141 cancelRunningQuery();
142
143 // if no feedback object was passed, create one that is owned by this object
144 // to ensure that filters ALWAYS receive a valid feedback
145 if ( !feedback )
146 {
147 mOwnedFeedback.reset( new QgsFeedback() );
148 feedback = mOwnedFeedback.get();
149 }
150 else
151 {
152 mOwnedFeedback.reset( nullptr );
153 }
154 mFeedback = feedback;
155
156 QList< QgsLocatorFilter * > activeFilters;
157 QString searchString = string;
158 QString prefix = searchString.left( std::max( static_cast<int>( searchString.indexOf( ' ' ) ), 0 ) );
159 if ( !prefix.isEmpty() )
160 {
161 for ( QgsLocatorFilter *filter : std::as_const( mFilters ) )
162 {
163 if ( filter->activePrefix().compare( prefix, Qt::CaseInsensitive ) == 0 && filter->enabled() )
164 {
165 activeFilters << filter;
166 }
167 }
168 context.usingPrefix = !activeFilters.empty();
169 }
170 if ( !activeFilters.isEmpty() )
171 {
172 searchString = searchString.mid( prefix.length() + 1 );
173 }
174 else
175 {
176 for ( QgsLocatorFilter *filter : std::as_const( mFilters ) )
177 {
178 if ( filter->useWithoutPrefix() && filter->enabled() )
179 {
180 activeFilters << filter;
181 }
182 }
183 }
184
185 QList< QgsLocatorFilter *> threadedFilters;
186 for ( QgsLocatorFilter *filter : std::as_const( activeFilters ) )
187 {
188 filter->clearPreviousResults();
189 std::unique_ptr< QgsLocatorFilter > clone( filter->clone() );
190 if ( ! clone )
191 {
192 QgsMessageLog::logMessage( tr( "QgsLocatorFilter '%1' could not provide a valid clone" ).arg( filter->name() ), QString(), Qgis::MessageLevel::Critical );
193 continue;
194 }
195 connect( clone.get(), &QgsLocatorFilter::resultFetched, clone.get(), [this, filter]( QgsLocatorResult result )
196 {
197 result.filter = filter;
198 filterSentResult( result );
199 } );
200 QStringList autoCompleteList = clone->prepare( searchString, context );
201 if ( context.usingPrefix )
202 {
203 for ( int i = 0; i < autoCompleteList.length(); i++ )
204 {
205 autoCompleteList[i].prepend( QStringLiteral( "%1 " ).arg( prefix ) );
206 }
207 }
208 mAutocompletionList.append( autoCompleteList );
209
210 if ( clone->flags() & QgsLocatorFilter::FlagFast )
211 {
212 // filter is fast enough to fetch results on the main thread
213 clone->fetchResults( searchString, context, feedback );
214 }
215 else
216 {
217 threadedFilters.append( clone.release() );
218 }
219 }
220
221 mActiveThreads.clear();
222 for ( QgsLocatorFilter *filter : std::as_const( threadedFilters ) )
223 {
224 QThread *thread = new QThread();
225 mActiveThreads.append( thread );
226 filter->moveToThread( thread );
227 connect( thread, &QThread::started, filter, [filter, searchString, context, feedback]
228 {
229 int delay = filter->fetchResultsDelay();
230 while ( delay > 0 )
231 {
232 if ( feedback->isCanceled() )
233 break;
234 QThread::msleep( 50 );
235 delay -= 50;
236 }
237 if ( !feedback->isCanceled() )
238 filter->fetchResults( searchString, context, feedback );
239 filter->emit finished();
240 }, Qt::QueuedConnection );
241 connect( filter, &QgsLocatorFilter::finished, thread, &QThread::quit );
242 connect( filter, &QgsLocatorFilter::finished, filter, &QgsLocatorFilter::deleteLater );
243 connect( thread, &QThread::finished, thread, [this, thread]
244 {
245 mActiveThreads.removeAll( thread );
246 if ( mActiveThreads.empty() )
247 emit finished();
248 } );
249 connect( thread, &QThread::finished, thread, &QThread::deleteLater );
250 thread->start();
251 }
252
253 emit searchPrepared();
254
255 if ( mActiveThreads.empty() )
256 emit finished();
257}
258
260{
261 cancelRunningQuery();
262}
263
265{
266 if ( mFeedback )
267 mFeedback->cancel();
268}
269
271{
272 return !mActiveThreads.empty();
273}
274
276{
277 for ( QgsLocatorFilter *filter : std::as_const( mFilters ) )
278 {
279 if ( filter->enabled() )
280 {
281 filter->clearPreviousResults();
282 }
283 }
284}
285
286void QgsLocator::filterSentResult( QgsLocatorResult result )
287{
288 // if query has been canceled then discard any results we receive
289 if ( mFeedback->isCanceled() )
290 return;
291
292 emit foundResult( result );
293}
294
295void QgsLocator::cancelRunningQuery()
296{
297 if ( !mActiveThreads.empty() )
298 {
299 // cancel existing job
300 mFeedback->cancel();
301 while ( !mActiveThreads.empty() )
302 {
303 QCoreApplication::processEvents();
304 }
305 }
306}
@ Critical
Critical/error message.
Definition qgis.h:157
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
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)
Definition qgslocator.h:67
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.
Definition qgslocator.h:164
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.
Definition qgslocator.h:162
~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.
Definition qgslocator.h:166
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.
A string 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