QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 <QAction>
22 #include <QIcon>
23 #include <QString>
24 #include <QVariant>
25 
26 #include "qgis_core.h"
27 #include "qgslocatorcontext.h"
28 #include "qgslogger.h"
29 
30 class QgsFeedback;
31 class QgsLocatorFilter;
32 
39 class CORE_EXPORT QgsLocatorResult
40 {
41  public:
42 
46  QgsLocatorResult() = default;
47 
51  QgsLocatorResult( QgsLocatorFilter *filter, const QString &displayString, const QVariant &userData = QVariant() )
52  : filter( filter )
53  , displayString( displayString )
54  , userData( userData )
55  {}
56 
60  QgsLocatorFilter *filter = nullptr;
61 
65  QString displayString;
66 
70  QString description;
71 
75  QVariant userData;
76 
80  QIcon icon;
81 
85  double score = 0.5;
86 
94  QString group = QString();
95 
102  struct CORE_EXPORT ResultAction
103  {
104  public:
106  ResultAction() = default;
107 
114  ResultAction( int id, QString text, QString iconPath = QString() )
115  : id( id )
116  , text( text )
117  , iconPath( iconPath )
118  {}
119  int id = -1;
120  QString text;
121  QString iconPath;
122  };
123 
130  QList<QgsLocatorResult::ResultAction> actions;
131 };
132 
134 
135 
136 
145 class CORE_EXPORT QgsLocatorFilter : public QObject
146 {
147  Q_OBJECT
148 
149  public:
150 
152  enum Priority
153  {
157  Low,
158  Lowest
159  };
160  Q_ENUM( Priority )
161 
162 
163  enum Flag
164  {
165  FlagFast = 1 << 1,
166  };
167  Q_DECLARE_FLAGS( Flags, Flag )
168  Q_FLAG( Flags )
169 
170 
173  QgsLocatorFilter( QObject *parent = nullptr );
174 
179  virtual QgsLocatorFilter *clone() const = 0 SIP_FACTORY;
180 
185  virtual QString name() const = 0;
186 
191  virtual QString displayName() const = 0;
192 
196  virtual QgsLocatorFilter::Flags flags() const;
197 
202  virtual Priority priority() const { return Medium; }
203 
215  virtual QString prefix() const { return QString(); }
216 
224  virtual QStringList prepare( const QString &string, const QgsLocatorContext &context ) { Q_UNUSED( string ) Q_UNUSED( context ); return QStringList();}
225 
241  virtual void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) = 0;
242 
251  virtual void triggerResult( const QgsLocatorResult &result ) = 0;
252 
259  virtual void triggerResultFromAction( const QgsLocatorResult &result, const int actionId );
260 
269  virtual void clearPreviousResults() {}
270 
276  bool useWithoutPrefix() const;
277 
283  void setUseWithoutPrefix( bool useWithoutPrefix );
284 
291  QString activePrefix() const;
292 
299  void setActivePrefix( const QString &activePrefix ) SIP_SKIP;
300 
307  static bool stringMatches( const QString &candidate, const QString &search );
308 
314  static double fuzzyScore( const QString &candidate, const QString &search );
315 
320  bool enabled() const;
321 
326  void setEnabled( bool enabled );
327 
332  virtual bool hasConfigWidget() const;
333 
340  virtual void openConfigWidget( QWidget *parent = nullptr );
341 
348  void logMessage( const QString &message, Qgis::MessageLevel level = Qgis::Info );
349 
350 
351  signals:
352 
356  void finished();
357 
362  void resultFetched( const QgsLocatorResult &result );
363 
364  private:
365 
366  bool mEnabled = true;
367  bool mUseWithoutPrefix = true;
368  QString mActivePrefifx = QString();
369 
370 };
371 
373 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLocatorFilter::Flags )
374 
375 
376 #endif // QGSLOCATORFILTER_H
377 
378 
QgsLocatorFilter
Abstract base class for filters which collect locator results.
Definition: qgslocatorfilter.h:146
QgsLocatorFilter::Highest
@ Highest
Highest priority.
Definition: qgslocatorfilter.h:154
QgsLocatorFilter::Medium
@ Medium
Medium priority.
Definition: qgslocatorfilter.h:156
QgsLocatorFilter::prefix
virtual QString prefix() const
Returns the search prefix character(s) for this filter.
Definition: qgslocatorfilter.h:215
QgsLocatorResult::displayString
QString displayString
String displayed for result.
Definition: qgslocatorfilter.h:65
qgslocatorcontext.h
QgsLocatorFilter::clone
virtual QgsLocatorFilter * clone() const =0
Creates a clone of the filter.
QgsLocatorFilter::priority
virtual Priority priority() const
Returns the priority for the filter, which controls how results are ordered in the locator.
Definition: qgslocatorfilter.h:202
QgsLocatorFilter::finished
void finished()
Emitted when the filter finishes fetching results.
QgsLocatorFilter::name
virtual QString name() const =0
Returns the unique name for the filter.
QgsLocatorResult::actions
QList< QgsLocatorResult::ResultAction > actions
Additional actions to be used in a locator widget for the given result.
Definition: qgslocatorfilter.h:130
QgsLocatorResult::ResultAction::ResultAction
ResultAction(int id, QString text, QString iconPath=QString())
Constructor for ResultAction The id used to recognized the action when the result is triggered.
Definition: qgslocatorfilter.h:114
QgsLocatorResult
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
Definition: qgslocatorfilter.h:40
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLocatorFilter::clearPreviousResults
virtual void clearPreviousResults()
This method will be called on main thread on the original filter (not a clone) before fetching result...
Definition: qgslocatorfilter.h:269
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
QgsLocatorResult::ResultAction::text
QString text
Definition: qgslocatorfilter.h:120
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLocatorContext
Encapsulates the properties relating to the context of a locator search.
Definition: qgslocatorcontext.h:32
Qgis::Info
@ Info
Definition: qgis.h:90
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsLocatorFilter::displayName
virtual QString displayName() const =0
Returns a translated, user-friendly name for the filter.
QgsLocatorResult::QgsLocatorResult
QgsLocatorResult(QgsLocatorFilter *filter, const QString &displayString, const QVariant &userData=QVariant())
Constructor for QgsLocatorResult.
Definition: qgslocatorfilter.h:51
QgsLocatorResult::ResultAction::ResultAction
ResultAction()=default
Constructor for ResultAction.
QgsLocatorFilter::Priority
Priority
Filter priority. Controls the order of results in the locator.
Definition: qgslocatorfilter.h:153
QgsLocatorFilter::resultFetched
void resultFetched(const QgsLocatorResult &result)
Should be emitted by filters whenever they encounter a matching result during within their fetchResul...
QgsLocatorFilter::prepare
virtual QStringList prepare(const QString &string, const QgsLocatorContext &context)
Prepares the filter instance for an upcoming search for the specified string.
Definition: qgslocatorfilter.h:224
QgsLocatorResult::ResultAction
The ResultAction stores basic information for additional actions to be used in a locator widget for t...
Definition: qgslocatorfilter.h:103
QgsLocatorResult::icon
QIcon icon
Icon for result.
Definition: qgslocatorfilter.h:80
QgsLocatorFilter::Flag
Flag
Flags for locator behavior.
Definition: qgslocatorfilter.h:164
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:89
QgsLocatorFilter::Low
@ Low
Low priority.
Definition: qgslocatorfilter.h:157
QgsLocatorFilter::fetchResults
virtual void fetchResults(const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback)=0
Retrieves the filter results for a specified search string.
QgsLocatorResult::ResultAction::iconPath
QString iconPath
Definition: qgslocatorfilter.h:121
QgsLocatorFilter::triggerResult
virtual void triggerResult(const QgsLocatorResult &result)=0
Triggers a filter result from this filter.
QgsLocatorFilter::High
@ High
High priority.
Definition: qgslocatorfilter.h:155
qgslogger.h
QgsLocatorResult::description
QString description
Descriptive text for result.
Definition: qgslocatorfilter.h:70
QgsLocatorResult::QgsLocatorResult
QgsLocatorResult()=default
Constructor for QgsLocatorResult.
QgsLocatorResult::userData
QVariant userData
Custom reference or other data set by the filter.
Definition: qgslocatorfilter.h:75