QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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 "qgis_core.h"
22#include "qgslocatorcontext.h"
23#include "qgslogger.h"
24#include "qgssettingstree.h"
25
26#include <QIcon>
27#include <QString>
28#include <QVariant>
29
30using namespace Qt::StringLiterals;
31
32class QgsFeedback;
34
40class CORE_EXPORT QgsLocatorResult
41{
42 Q_GADGET
43
44 Q_PROPERTY( QVariant userData READ userData WRITE setUserData )
45 Q_PROPERTY( QString displayString MEMBER displayString )
46 Q_PROPERTY( QString description MEMBER description )
47 Q_PROPERTY( double score MEMBER score )
48 Q_PROPERTY( QString group MEMBER group )
49 Q_PROPERTY( double groupScore MEMBER groupScore )
50
51 public:
52
53 QgsLocatorResult() = default;
54
58 QgsLocatorResult( QgsLocatorFilter *filter, const QString &displayString, const QVariant &userData = QVariant() )
59 : filter( filter )
61 , mUserData( userData )
62 {}
63
69 QVariant userData() const SIP_PYNAME( _userData );
70
76 void setUserData( const QVariant &userData );
77
82
86 QString displayString;
87
91 QString description;
92
96 QIcon icon;
97
101 double score = 0.5;
102
110 QString group;
111
119 double groupScore = 0;
120
127 struct CORE_EXPORT ResultAction
128 {
129 public:
130
131 ResultAction() = default;
132
139 ResultAction( int id, QString text, QString iconPath = QString() )
140 : id( id )
141 , text( text )
142 , iconPath( iconPath )
143 {}
144 int id = -1;
145 QString text;
146 QString iconPath;
147 };
148
155 QList<QgsLocatorResult::ResultAction> actions;
156
157 private:
158
162 QVariant mUserData;
163
164
165};
166
168
169
170
178class CORE_EXPORT QgsLocatorFilter : public QObject
179{
180 Q_OBJECT
181
182 public:
183
184#ifndef SIP_RUN
185 static inline QgsSettingsTreeNamedListNode *sTreeAppLocatorFilters = QgsSettingsTree::sTreeApp->createNamedListNode( u"locator-filters"_s );
186#endif
196 Q_ENUM( Priority )
197
198
199 enum Flag SIP_ENUM_BASETYPE( IntFlag )
200 {
201 FlagFast = 1 << 1,
202 };
203 Q_DECLARE_FLAGS( Flags, Flag )
204 Q_FLAG( Flags )
205
206
209 QgsLocatorFilter( QObject *parent = nullptr );
210
215 virtual QgsLocatorFilter *clone() const = 0 SIP_FACTORY;
216
221 virtual QString name() const = 0;
222
227 virtual QString displayName() const = 0;
228
233 virtual QString description() const { return QString(); }
234
238 virtual QgsLocatorFilter::Flags flags() const;
239
244 virtual Priority priority() const { return Medium; }
245
257 virtual QString prefix() const { return QString(); }
258
266 virtual QStringList prepare( const QString &string, const QgsLocatorContext &context ) { Q_UNUSED( string ) Q_UNUSED( context ); return QStringList();}
267
283 virtual void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) = 0;
284
293 virtual void triggerResult( const QgsLocatorResult &result ) = 0;
294
301 virtual void resultSelected( const QgsLocatorResult &result ) {Q_UNUSED( result )}
302
309 virtual void resultDeselected( const QgsLocatorResult &result ) {Q_UNUSED( result )}
310
317 virtual void triggerResultFromAction( const QgsLocatorResult &result, const int actionId );
318
327 virtual void clearPreviousResults() {}
328
334 bool useWithoutPrefix() const;
335
341 void setUseWithoutPrefix( bool useWithoutPrefix );
342
349 QString activePrefix() const;
350
357 void setActivePrefix( const QString &activePrefix ) SIP_SKIP;
358
365 static bool stringMatches( const QString &candidate, const QString &search );
366
372 static double fuzzyScore( const QString &candidate, const QString &search );
373
378 bool enabled() const;
379
384 void setEnabled( bool enabled );
385
390 virtual bool hasConfigWidget() const;
391
398 virtual void openConfigWidget( QWidget *parent = nullptr );
399
406 void logMessage( const QString &message, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
407
413 int fetchResultsDelay() const { return mFetchResultsDelay; }
414
421 void setFetchResultsDelay( int delay ) { mFetchResultsDelay = delay; }
422
423 signals:
424
428 void finished();
429
434 void resultFetched( const QgsLocatorResult &result );
435
436 private:
437
438 bool mEnabled = true;
439 bool mUseWithoutPrefix = true;
440 QString mActivePrefifx = QString();
441 int mFetchResultsDelay = 0;
442
443};
444
447
448
449#endif // QGSLOCATORFILTER_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:159
@ Info
Information message.
Definition qgis.h:160
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.
@ FlagFast
Filter finds results quickly and can be safely run in the main thread.
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.
@ Lowest
Lowest priority.
@ 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.
QgsLocatorFilter(QObject *parent=nullptr)
Constructor for QgsLocatorFilter.
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.
static QgsSettingsTreeNamedListNode * sTreeAppLocatorFilters
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.
QString description
Descriptive text for result.
double score
Match score, from 0 - 1, where 1 represents a perfect match.
void setUserData(const QVariant &userData)
Set userData for the locator result.
QgsLocatorResult()=default
double groupScore
Specifies the score of the group to allow ordering.
QString displayString
String displayed for result.
QString group
Group the results by categories If left as empty string, this means that results are all shown withou...
QgsLocatorFilter * filter
Filter from which the result was obtained.
QIcon icon
Icon for result.
A named list tree node for the settings tree to help organizing and introspecting the tree.
static QgsSettingsTreeNode * sTreeApp
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_PYNAME(name)
Definition qgis_sip.h:89
#define SIP_FACTORY
Definition qgis_sip.h:84
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)
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.