20 #include <QtConcurrent> 24 << QStringLiteral(
"processing_alg" )
25 << QStringLiteral(
"layertree" )
26 << QStringLiteral(
"layouts" )
27 << QStringLiteral(
"features" )
28 << QStringLiteral(
"calculator" )
29 << QStringLiteral(
"bookmarks" )
30 << QStringLiteral(
"optionpages" );
35 qRegisterMetaType<QgsLocatorResult>(
"QgsLocatorResult" );
41 qDeleteAll( mFilters );
47 mFilters.removeAll( filter );
53 if ( !prefix.isEmpty() )
55 QList<QgsLocatorFilter *>
filters = QList<QgsLocatorFilter *>();
58 if ( !filter->activePrefix().isEmpty() && filter->activePrefix() == prefix )
73 QMap<QString, QgsLocatorFilter *>
filters = QMap<QString, QgsLocatorFilter *>();
76 if ( !filter->activePrefix().isEmpty() && filter->enabled() )
78 filters.insertMulti( filter->activePrefix(), filter );
86 mFilters.append( filter );
87 filter->setParent(
this );
91 bool enabled = settings.
value( QStringLiteral(
"locator_filters/enabled_%1" ).arg( filter->
name() ),
true, QgsSettings::Section::Gui ).toBool();
92 bool byDefault = settings.
value( QStringLiteral(
"locator_filters/default_%1" ).arg( filter->
name() ), filter->
useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();
93 QString prefix = settings.
value( QStringLiteral(
"locator_filters/prefix_%1" ).arg( filter->
name() ), filter->
prefix(), QgsSettings::Section::Gui ).toString();
94 if ( prefix.isEmpty() )
99 if ( !prefix.isEmpty() )
106 else if ( prefix.length() >= 3 || prefix != filter->
prefix() )
128 cancelRunningQuery();
135 feedback = mOwnedFeedback.get();
139 mOwnedFeedback.reset(
nullptr );
141 mFeedback = feedback;
143 QList< QgsLocatorFilter * > activeFilters;
144 QString searchString = string;
145 QString prefix = searchString.left( std::max( searchString.indexOf(
' ' ), 0 ) );
146 if ( !prefix.isEmpty() )
150 if ( filter->activePrefix() == prefix && filter->enabled() )
152 activeFilters << filter;
157 if ( !activeFilters.isEmpty() )
159 searchString = searchString.mid( prefix.length() + 1 );
165 if ( filter->useWithoutPrefix() && filter->enabled() )
167 activeFilters << filter;
172 QList< QgsLocatorFilter *> threadedFilters;
175 filter->clearPreviousResults();
176 std::unique_ptr< QgsLocatorFilter > clone( filter->clone() );
179 result.filter = filter;
180 emit filterSentResult( result );
182 clone->prepare( searchString, context );
187 clone->fetchResults( searchString, context, feedback );
192 threadedFilters.append( clone.release() );
196 mActiveThreads.clear();
199 QThread *thread =
new QThread();
200 mActiveThreads.append( thread );
201 filter->moveToThread( thread );
202 connect( thread, &QThread::started, filter, [filter, searchString, context, feedback]
205 filter->fetchResults( searchString, context, feedback );
207 }, Qt::QueuedConnection );
210 connect( thread, &QThread::finished, thread, [
this, thread]
212 mActiveThreads.removeAll( thread );
213 if ( mActiveThreads.empty() )
216 connect( thread, &QThread::finished, thread, &QThread::deleteLater );
220 if ( mActiveThreads.empty() )
226 cancelRunningQuery();
237 return !mActiveThreads.empty();
244 if ( filter->enabled() )
246 filter->clearPreviousResults();
260 void QgsLocator::cancelRunningQuery()
262 if ( !mActiveThreads.empty() )
266 while ( !mActiveThreads.empty() )
268 QCoreApplication::processEvents();
void registerFilter(QgsLocatorFilter *filter)
Registers a filter within the locator.
void cancelWithoutBlocking()
Triggers cancelation 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.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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.
bool isRunning() const
Returns true if a query is currently being executed by the locator.
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 cancelation 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
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.
bool useWithoutPrefix() const
Returns true if the filter should be used when no prefix is entered.
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...
bool usingPrefix
Will be true if search is being conducted using a filter prefix.
virtual QString prefix() const
Returns the search prefix character(s) for this filter.
bool isCanceled() const
Tells whether the operation has been canceled already.
virtual QString name() const =0
Returns the unique name for the filter.
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.
Q_DECL_DEPRECATED QMap< QString, QgsLocatorFilter * > prefixedFilters() const
Returns a map of prefix to filter, for all registered filters with valid prefixes.
void deregisterFilter(QgsLocatorFilter *filter)
Deregisters a filter from the locator and deletes it.
void clearPreviousResults()
Will call clearPreviousResults on all filters.