QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsexpressiontreeview.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressiontreeview.h
3  --------------------------------------
4  Date : march 2020 - quarantine day 9
5  Copyright : (C) 2020 by Denis Rouzaud
6  Email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSEXPRESSIONTREEVIEW_H
17 #define QGSEXPRESSIONTREEVIEW_H
18 
19 #include <QTreeView>
20 #include <QStandardItemModel>
21 #include <QSortFilterProxyModel>
22 #include <QPointer>
23 
24 #include "qgis_gui.h"
25 #include "qgis_sip.h"
26 #include "qgsexpressioncontext.h"
27 #include "qgsproject.h"
28 
29 
30 class QgsVectorLayer;
31 
32 
33 
38 class GUI_EXPORT QgsExpressionItem : public QStandardItem
39 {
40  public:
41  enum ItemType
42  {
45  ExpressionNode
46  };
47 
48  QgsExpressionItem( const QString &label,
49  const QString &expressionText,
50  const QString &helpText,
51  QgsExpressionItem::ItemType itemType = ExpressionNode )
52  : QStandardItem( label )
53  {
54  mExpressionText = expressionText;
55  mHelpText = helpText;
56  mType = itemType;
57  setData( itemType, ITEM_TYPE_ROLE );
58  }
59 
60  QgsExpressionItem( const QString &label,
61  const QString &expressionText,
62  QgsExpressionItem::ItemType itemType = ExpressionNode )
63  : QStandardItem( label )
64  {
65  mExpressionText = expressionText;
66  mType = itemType;
67  setData( itemType, ITEM_TYPE_ROLE );
68  }
69 
70  QString getExpressionText() const { return mExpressionText; }
71 
77  QString getHelpText() const { return mHelpText; }
78 
84  void setHelpText( const QString &helpText ) { mHelpText = helpText; }
85 
91  QgsExpressionItem::ItemType getItemType() const { return mType; }
92 
94  static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
96  static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
98  static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;
100  static const int ITEM_NAME_ROLE = Qt::UserRole + 4;
101 
102  private:
103  QString mExpressionText;
104  QString mHelpText;
106 };
107 
108 
115 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
116 {
117  Q_OBJECT
118 
119  public:
121 
122  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
123 
129  void setFilterString( const QString &string );
130 
131  protected:
132 
133  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
134 
135  private:
136 
137  QString mFilterString;
138 };
139 
148 class GUI_EXPORT QgsExpressionTreeView : public QTreeView
149 {
150  Q_OBJECT
151  public:
152 
161  {
162  public:
164  explicit MenuProvider() = default;
165  virtual ~MenuProvider() = default;
166 
168  virtual QMenu *createContextMenu( QgsExpressionItem *item ) SIP_FACTORY {Q_UNUSED( item ) return nullptr;}
169  };
170 
172  QgsExpressionTreeView( QWidget *parent = nullptr );
173 
177  void setLayer( QgsVectorLayer *layer );
178 
182  void loadFieldNames( const QgsFields &fields );
183 
190  void setExpressionContext( const QgsExpressionContext &context );
191 
197  QgsExpressionContext expressionContext() const { return mExpressionContext; }
198 
203  QgsProject *project();
204 
210  void setProject( QgsProject *project );
211 
216  void setMenuProvider( MenuProvider *provider );
217 
221  void refresh();
222 
226  QgsExpressionItem *currentItem() const;
227 
234  Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
235 
240  void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
241 
246  void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
247 
251  void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
252 
256  void removeFromUserExpressions( const QString &label );
257 
262  void loadUserExpressions( );
263 
267  const QList<QgsExpressionItem *> findExpressions( const QString &label );
268 
272  QStringList userExpressionLabels() const SIP_SKIP;
273 
278  QJsonDocument exportUserExpressions();
279 
284  void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
285 
286  signals:
288  void expressionItemDoubleClicked( const QString &text );
289 
291  void currentExpressionItemChanged( QgsExpressionItem *item );
292 
293  public slots:
295  void setSearchText( const QString &text );
296 
297 
298  private slots:
299  void onDoubleClicked( const QModelIndex &index );
300 
301  void showContextMenu( QPoint pt );
302 
303  void currentItemChanged( const QModelIndex &index, const QModelIndex & );
304 
305  private:
306  void updateFunctionTree();
307 
321  void registerItem( const QString &group, const QString &label, const QString &expressionText,
322  const QString &helpText = QString(),
323  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
324  bool highlightedItem = false, int sortOrder = 1,
325  const QIcon &icon = QIcon(),
326  const QStringList &tags = QStringList(),
327  const QString &name = QString() );
328 
340  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
341  const QString &helpText = QString(),
342  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
343  bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
344 
345  void loadExpressionContext();
346  void loadRelations();
347  void loadLayers();
348  void loadFieldNames();
349 
361  void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
362 
363 
364  std::unique_ptr<QStandardItemModel> mModel;
365  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
366  QMap<QString, QgsExpressionItem *> mExpressionGroups;
367 
368  MenuProvider *mMenuProvider = nullptr;
369 
370  QgsVectorLayer *mLayer = nullptr;
371  QPointer< QgsProject > mProject;
372  QgsExpressionContext mExpressionContext;
373  QString mRecentKey;
374 
375  QStringList mUserExpressionLabels;
376 };
377 
378 #endif // QGSEXPRESSIONTREEVIEW_H
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Search proxy used to filter the QgsExpressionBuilderWidget tree.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
void setHelpText(const QString &helpText)
Set the help text for the current item.
QString getExpressionText() const
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
QString getHelpText() const
Gets the help text that is associated with this expression item.
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
virtual QMenu * createContextMenu(QgsExpressionItem *item)
Returns a newly created menu instance.
MenuProvider()=default
Constructor.
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Container of fields for a vector layer.
Definition: qgsfields.h:45
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:101
Represents a vector layer which manages a vector based data sets.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76