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" );
37 qRegisterMetaType<QgsLocatorResult>(
"QgsLocatorResult" );
43 qDeleteAll( mFilters );
49 mFilters.removeAll( filter );
55 if ( !prefix.isEmpty() )
57 QList<QgsLocatorFilter *>
filters = QList<QgsLocatorFilter *>();
60 if ( !filter->activePrefix().isEmpty() && filter->activePrefix() == prefix )
75 QMap<QString, QgsLocatorFilter *>
filters = QMap<QString, QgsLocatorFilter *>();
78 if ( !filter->activePrefix().isEmpty() && filter->enabled() )
80 filters.insertMulti( filter->activePrefix(), filter );
88 mFilters.append( filter );
89 filter->setParent(
this );
93 bool enabled = settings.
value( QStringLiteral(
"locator_filters/enabled_%1" ).arg( filter->
name() ),
true, QgsSettings::Section::Gui ).toBool();
94 bool byDefault = settings.
value( QStringLiteral(
"locator_filters/default_%1" ).arg( filter->
name() ), filter->
useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();
95 QString prefix = settings.
value( QStringLiteral(
"locator_filters/prefix_%1" ).arg( filter->
name() ), filter->
prefix(), QgsSettings::Section::Gui ).toString();
96 if ( prefix.isEmpty() )
101 if ( !prefix.isEmpty() )
108 else if ( prefix.length() >= 3 || prefix != filter->
prefix() )
130 cancelRunningQuery();
137 feedback = mOwnedFeedback.get();
141 mOwnedFeedback.reset(
nullptr );
143 mFeedback = feedback;
145 QList< QgsLocatorFilter * > activeFilters;
146 QString searchString = string;
147 QString prefix = searchString.left( std::max( searchString.indexOf(
' ' ), 0 ) );
148 if ( !prefix.isEmpty() )
152 if ( filter->activePrefix() == prefix && filter->enabled() )
154 activeFilters << filter;
159 if ( !activeFilters.isEmpty() )
161 searchString = searchString.mid( prefix.length() + 1 );
167 if ( filter->useWithoutPrefix() && filter->enabled() )
169 activeFilters << filter;
174 QList< QgsLocatorFilter *> threadedFilters;
177 filter->clearPreviousResults();
178 std::unique_ptr< QgsLocatorFilter > clone( filter->clone() );
181 result.filter = filter;
182 emit filterSentResult( result );
184 clone->prepare( searchString, context );
189 clone->fetchResults( searchString, context, feedback );
194 threadedFilters.append( clone.release() );
198 mActiveThreads.clear();
201 QThread *thread =
new QThread();
202 mActiveThreads.append( thread );
203 filter->moveToThread( thread );
204 connect( thread, &QThread::started, filter, [filter, searchString, context, feedback]
207 filter->fetchResults( searchString, context, feedback );
209 }, Qt::QueuedConnection );
212 connect( thread, &QThread::finished, thread, [
this, thread]
214 mActiveThreads.removeAll( thread );
215 if ( mActiveThreads.empty() )
218 connect( thread, &QThread::finished, thread, &QThread::deleteLater );
222 if ( mActiveThreads.empty() )
228 cancelRunningQuery();
239 return !mActiveThreads.empty();
246 if ( filter->enabled() )
248 filter->clearPreviousResults();
262 void QgsLocator::cancelRunningQuery()
264 if ( !mActiveThreads.empty() )
268 while ( !mActiveThreads.empty() )
270 QCoreApplication::processEvents();
void registerFilter(QgsLocatorFilter *filter)
Registers a filter within the locator.
bool isCanceled() const
Tells whether the operation has been canceled already.
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
void resultFetched(const QgsLocatorResult &result)
Should be emitted by filters whenever they encounter a matching result during within their fetchResul...
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
This class is a composition of two QSettings instances:
QgsLocator(QObject *parent=nullptr)
Constructor for QgsLocator.
void fetchResults(const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback=nullptr)
Triggers the background fetching of filter results for a specified search string. ...
~QgsLocator() override
Destructor for QgsLocator.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setEnabled(bool enabled)
Sets whether the filter is enabled.
QList< QgsLocatorFilter * > filters(const QString &prefix=QString())
Returns the list of filters registered in the locator.
void finished()
Emitted when locator has finished a query, either as a result of successful completion or early cance...
Base class for feedback objects to be used for cancellation of something running in a worker thread...
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
bool isRunning() const
Returns true if a query is currently being executed by the locator.
void setUseWithoutPrefix(bool useWithoutPrefix)
Sets whether the filter should be used when no prefix is entered.
void cancel()
Cancels any current running query, and blocks until query is completely canceled by all filters...
void finished()
Emitted when the filter finishes fetching results.
Encapsulates the properties relating to the context of a locator search.
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
Abstract base class for filters which collect locator results.
void setActivePrefix(const QString &activePrefix)
Sets the prefix as being used by the locator.
void foundResult(const QgsLocatorResult &result)
Emitted whenever a filter encounters a matching result after the fetchResults() method is called...
Q_DECL_DEPRECATED QMap< QString, QgsLocatorFilter * > prefixedFilters() const
Returns a map of prefix to filter, for all registered filters with valid prefixes.
bool usingPrefix
Will be true if search is being conducted using a filter prefix.
virtual QString name() const =0
Returns the unique name for the filter.
bool useWithoutPrefix() const
Returns true if the filter should be used when no prefix is entered.
static const QList< QString > CORE_FILTERS
List of core filters (i.e. not plugin filters)
Filter finds results quickly and can be safely run in the main thread.
void deregisterFilter(QgsLocatorFilter *filter)
Deregisters a filter from the locator and deletes it.
virtual QString prefix() const
Returns the search prefix character(s) for this filter.
void clearPreviousResults()
Will call clearPreviousResults on all filters.