QGIS API Documentation 3.43.0-Master (3ee7834ace6)
qgslocatorfilter.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslocatorfilter.h
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#ifndef QGSLOCATORFILTER_H
19#define QGSLOCATORFILTER_H
20
21#include <QIcon>
22#include <QString>
23#include <QVariant>
24
25#include "qgis_core.h"
26#include "qgslocatorcontext.h"
27#include "qgslogger.h"
28#include "qgssettingstree.h"
29
30class QgsFeedback;
32
38class CORE_EXPORT QgsLocatorResult
39{
40 Q_GADGET
41
42 Q_PROPERTY( QVariant userData READ userData WRITE setUserData )
43 Q_PROPERTY( QString displayString MEMBER displayString )
44 Q_PROPERTY( QString description MEMBER description )
45 Q_PROPERTY( double score MEMBER score )
46 Q_PROPERTY( QString group MEMBER group )
47 Q_PROPERTY( double groupScore MEMBER groupScore )
48
49 public:
50
51 QgsLocatorResult() = default;
52
56 QgsLocatorResult( QgsLocatorFilter *filter, const QString &displayString, const QVariant &userData = QVariant() )
57 : filter( filter )
58 , displayString( displayString )
59 , mUserData( userData )
60 {}
61
67 QVariant userData() const SIP_PYNAME( _userData );
68
74 void setUserData( const QVariant &userData );
75
79 QgsLocatorFilter *filter = nullptr;
80
84 QString displayString;
85
89 QString description;
90
94 QIcon icon;
95
99 double score = 0.5;
100
108 QString group;
109
117 double groupScore = 0;
118
125 struct CORE_EXPORT ResultAction
126 {
127 public:
128
129 ResultAction() = default;
130
137 ResultAction( int id, QString text, QString iconPath = QString() )
138 : id( id )
139 , text( text )
140 , iconPath( iconPath )
141 {}
142 int id = -1;
143 QString text;
144 QString iconPath;
145 };
146
153 QList<QgsLocatorResult::ResultAction> actions;
154
155 private:
156
160 QVariant mUserData;
161
162
163};
164
166
167
168
176class CORE_EXPORT QgsLocatorFilter : public QObject
177{
178 Q_OBJECT
179
180 public:
181
182#ifndef SIP_RUN
183 static inline QgsSettingsTreeNamedListNode *sTreeAppLocatorFilters = QgsSettingsTree::sTreeApp->createNamedListNode( QStringLiteral( "locator-filters" ) );
184#endif
187 {
192 Lowest
193 };
194 Q_ENUM( Priority )
195
196
197 enum Flag SIP_ENUM_BASETYPE( IntFlag )
198 {
199 FlagFast = 1 << 1,
200 };
201 Q_DECLARE_FLAGS( Flags, Flag )
202 Q_FLAG( Flags )
203
204
207 QgsLocatorFilter( QObject *parent = nullptr );
208
213 virtual QgsLocatorFilter *clone() const = 0 SIP_FACTORY;
214
219 virtual QString name() const = 0;
220
225 virtual QString displayName() const = 0;
226
231 virtual QString description() const { return QString(); }
232
236 virtual QgsLocatorFilter::Flags flags() const;
237
242 virtual Priority priority() const { return Medium; }
243
255 virtual QString prefix() const { return QString(); }
256
264 virtual QStringList prepare( const QString &string, const QgsLocatorContext &context ) { Q_UNUSED( string ) Q_UNUSED( context ); return QStringList();}
265
281 virtual void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) = 0;
282
291 virtual void triggerResult( const QgsLocatorResult &result ) = 0;
292
299 virtual void resultSelected( const QgsLocatorResult &result ) {Q_UNUSED( result )}
300
307 virtual void resultDeselected( const QgsLocatorResult &result ) {Q_UNUSED( result )}
308
315 virtual void triggerResultFromAction( const QgsLocatorResult &result, const int actionId );
316
325 virtual void clearPreviousResults() {}
326
332 bool useWithoutPrefix() const;
333
339 void setUseWithoutPrefix( bool useWithoutPrefix );
340
347 QString activePrefix() const;
348
355 void setActivePrefix( const QString &activePrefix ) SIP_SKIP;
356
363 static bool stringMatches( const QString &candidate, const QString &search );
364
370 static double fuzzyScore( const QString &candidate, const QString &search );
371
376 bool enabled() const;
377
382 void setEnabled( bool enabled );
383
388 virtual bool hasConfigWidget() const;
389
396 virtual void openConfigWidget( QWidget *parent = nullptr );
397
404 void logMessage( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
405
411 int fetchResultsDelay() const { return mFetchResultsDelay; }
412
419 void setFetchResultsDelay( int delay ) { mFetchResultsDelay = delay; }
420
421 signals:
422
426 void finished();
427
432 void resultFetched( const QgsLocatorResult &result );
433
434 private:
435
436 bool mEnabled = true;
437 bool mUseWithoutPrefix = true;
438 QString mActivePrefifx = QString();
439 int mFetchResultsDelay = 0;
440
441};
442
445
446
447#endif // QGSLOCATORFILTER_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ Info
Information message.
Definition qgis.h:155
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Encapsulates the properties relating to the context of a locator search.
Abstract base class for filters which collect locator results.
QFlags< Flag > Flags
virtual QString prefix() const
Returns the search prefix character(s) for this filter.
Flag
Flags for locator behavior.
int fetchResultsDelay() const
Returns the delay (in milliseconds) for the filter to wait prior to fetching results.
Priority
Filter priority. Controls the order of results in the locator.
@ Low
Low priority.
@ Highest
Highest priority.
@ Medium
Medium priority.
@ High
High priority.
void setFetchResultsDelay(int delay)
Sets a delay (in milliseconds) for the filter to wait prior to fetching results.
virtual QString description() const
Returns a translated, description for the filter.
void finished()
Emitted when the filter finishes fetching results.
virtual QString displayName() const =0
Returns a translated, user-friendly name for the filter.
virtual Priority priority() const
Returns the priority for the filter, which controls how results are ordered in the locator.
virtual QString name() const =0
Returns the unique name for the filter.
virtual void triggerResult(const QgsLocatorResult &result)=0
Triggers a filter result from this filter.
virtual QgsLocatorFilter * clone() const =0
Creates a clone of the filter.
virtual void resultDeselected(const QgsLocatorResult &result)
This is called when a result is deselected.
virtual void clearPreviousResults()
This method will be called on main thread on the original filter (not a clone) before fetching result...
virtual void resultSelected(const QgsLocatorResult &result)
This is called when the result is selected by the user.
void resultFetched(const QgsLocatorResult &result)
Should be emitted by filters whenever they encounter a matching result during within their fetchResul...
virtual void fetchResults(const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback)=0
Retrieves the filter results for a specified search string.
virtual QStringList prepare(const QString &string, const QgsLocatorContext &context)
Prepares the filter instance for an upcoming search for the specified string.
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
QList< QgsLocatorResult::ResultAction > actions
Additional actions to be used in a locator widget for the given result.
QgsSettingsTreeNamedListNode is a named list tree node for the settings tree to help organizing and i...
QgsSettingsTreeNamedListNode * createNamedListNode(const QString &key, const Qgis::SettingsTreeNodeOptions &options=Qgis::SettingsTreeNodeOptions())
Creates a named list tree node.
static QgsSettingsTreeNode * sTreeApp
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_PYNAME(name)
Definition qgis_sip.h:81
#define SIP_FACTORY
Definition qgis_sip.h:76
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
The ResultAction stores basic information for additional actions to be used in a locator widget for t...
ResultAction(int id, QString text, QString iconPath=QString())
Constructor for ResultAction The id used to recognized the action when the result is triggered.