20 #include <QtConcurrent> 26 qRegisterMetaType<QgsLocatorResult>(
"QgsLocatorResult" );
32 qDeleteAll( mFilters );
38 mFilters.removeAll( filter );
39 QString key = mPrefixedFilters.key( filter );
41 mPrefixedFilters.remove( key );
52 return mPrefixedFilters;
57 mFilters.append( filter );
58 filter->setParent(
this );
60 if ( !filter->
prefix().isEmpty() )
62 if ( filter->
name() == QStringLiteral(
"actions" ) || filter->
name() == QStringLiteral(
"processing_alg" )
63 || filter->
name() == QStringLiteral(
"layertree" ) || filter->
name() == QStringLiteral(
"layouts" )
64 || filter->
name() == QStringLiteral(
"features" ) )
67 mPrefixedFilters.insert( filter->
prefix(), filter );
69 else if ( filter->
prefix().length() >= 3 )
71 mPrefixedFilters.insert( filter->
prefix(), filter );
77 bool enabled = settings.
value( QStringLiteral(
"locator_filters/enabled_%1" ).arg( filter->
name() ),
true, QgsSettings::Section::Gui ).toBool();
78 bool byDefault = settings.
value( QStringLiteral(
"locator_filters/default_%1" ).arg( filter->
name() ), filter->
useWithoutPrefix(), QgsSettings::Section::Gui ).toBool();
97 feedback = mOwnedFeedback.get();
101 mOwnedFeedback.reset(
nullptr );
103 mFeedback = feedback;
105 QList< QgsLocatorFilter * > activeFilters;
106 QString searchString = string;
107 if ( searchString.indexOf(
' ' ) > 0 )
109 QString prefix = searchString.left( searchString.indexOf(
' ' ) );
110 if ( mPrefixedFilters.contains( prefix ) && mPrefixedFilters.value( prefix )->enabled() )
112 activeFilters << mPrefixedFilters.value( prefix );
113 searchString = searchString.mid( prefix.length() + 1 );
117 if ( activeFilters.isEmpty() )
121 if ( filter->useWithoutPrefix() && filter->enabled() )
122 activeFilters << filter;
126 QList< QgsLocatorFilter *> threadedFilters;
129 std::unique_ptr< QgsLocatorFilter > clone( filter->clone() );
132 result.filter = filter;
133 emit filterSentResult( result );
135 clone->prepare( searchString, context );
140 clone->fetchResults( searchString, context, feedback );
145 threadedFilters.append( clone.release() );
149 mActiveThreads.clear();
152 QThread *thread =
new QThread();
153 mActiveThreads.append( thread );
154 filter->moveToThread( thread );
155 connect( thread, &QThread::started, filter, [filter, searchString, context, feedback]
158 filter->fetchResults( searchString, context, feedback );
160 }, Qt::QueuedConnection );
163 connect( thread, &QThread::finished, thread, [
this, thread]
165 mActiveThreads.removeAll( thread );
166 if ( mActiveThreads.empty() )
169 connect( thread, &QThread::finished, thread, &QThread::deleteLater );
173 if ( mActiveThreads.empty() )
179 cancelRunningQuery();
190 return !mActiveThreads.empty();
202 void QgsLocator::cancelRunningQuery()
204 if ( !mActiveThreads.empty() )
208 while ( !mActiveThreads.empty() )
210 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.
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.
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...
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 foundResult(const QgsLocatorResult &result)
Emitted whenever a filter encounters a matching result after the fetchResults() method is called...
QList< QgsLocatorFilter * > filters()
Returns the list of filters registered in the locator.
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.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
Filter finds results quickly and can be safely run in the main thread.
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.