QGIS API Documentation  3.17.0-Master (a035f434f4)
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 
23 #include "qgis_gui.h"
24 #include "qgis_sip.h"
25 #include "qgsexpressioncontext.h"
26 #include "qgsproject.h"
27 
28 
29 class QgsVectorLayer;
30 
31 
32 
37 class GUI_EXPORT QgsExpressionItem : public QStandardItem
38 {
39  public:
40  enum ItemType
41  {
44  ExpressionNode
45  };
46 
47  QgsExpressionItem( const QString &label,
48  const QString &expressionText,
49  const QString &helpText,
50  QgsExpressionItem::ItemType itemType = ExpressionNode )
51  : QStandardItem( label )
52  {
53  mExpressionText = expressionText;
54  mHelpText = helpText;
55  mType = itemType;
56  setData( itemType, ITEM_TYPE_ROLE );
57  }
58 
59  QgsExpressionItem( const QString &label,
60  const QString &expressionText,
61  QgsExpressionItem::ItemType itemType = ExpressionNode )
62  : QStandardItem( label )
63  {
64  mExpressionText = expressionText;
65  mType = itemType;
66  setData( itemType, ITEM_TYPE_ROLE );
67  }
68 
69  QString getExpressionText() const { return mExpressionText; }
70 
76  QString getHelpText() const { return mHelpText; }
77 
83  void setHelpText( const QString &helpText ) { mHelpText = helpText; }
84 
90  QgsExpressionItem::ItemType getItemType() const { return mType; }
91 
93  static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
95  static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
97  static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;
99  static const int ITEM_NAME_ROLE = Qt::UserRole + 4;
100 
101  private:
102  QString mExpressionText;
103  QString mHelpText;
105 };
106 
107 
114 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
115 {
116  Q_OBJECT
117 
118  public:
120 
121  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
122 
123  protected:
124 
125  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
126 };
127 
136 class GUI_EXPORT QgsExpressionTreeView : public QTreeView
137 {
138  Q_OBJECT
139  public:
140 
149  {
150  public:
152  explicit MenuProvider() = default;
153  virtual ~MenuProvider() = default;
154 
156  virtual QMenu *createContextMenu( QgsExpressionItem *item ) SIP_FACTORY {Q_UNUSED( item ) return nullptr;}
157  };
158 
160  QgsExpressionTreeView( QWidget *parent = nullptr );
161 
165  void setLayer( QgsVectorLayer *layer );
166 
170  void loadFieldNames( const QgsFields &fields );
171 
178  void setExpressionContext( const QgsExpressionContext &context );
179 
185  QgsExpressionContext expressionContext() const { return mExpressionContext; }
186 
191  QgsProject *project();
192 
198  void setProject( QgsProject *project );
199 
204  void setMenuProvider( MenuProvider *provider );
205 
209  void refresh();
210 
214  QgsExpressionItem *currentItem() const;
215 
222  Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
223 
228  void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
229 
234  void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
235 
239  void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText );
240 
244  void removeFromUserExpressions( const QString &label );
245 
250  void loadUserExpressions( );
251 
255  const QList<QgsExpressionItem *> findExpressions( const QString &label );
256 
260  QStringList userExpressionLabels() const SIP_SKIP;
261 
266  QJsonDocument exportUserExpressions();
267 
272  void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
273 
274  signals:
276  void expressionItemDoubleClicked( const QString &text );
277 
279  void currentExpressionItemChanged( QgsExpressionItem *item );
280 
281  public slots:
283  void setSearchText( const QString &text );
284 
285 
286  private slots:
287  void onDoubleClicked( const QModelIndex &index );
288 
289  void showContextMenu( QPoint pt );
290 
291  void currentItemChanged( const QModelIndex &index, const QModelIndex & );
292 
293  private:
294  void updateFunctionTree();
295 
309  void registerItem( const QString &group, const QString &label, const QString &expressionText,
310  const QString &helpText = QString(),
312  bool highlightedItem = false, int sortOrder = 1,
313  QIcon icon = QIcon(),
314  const QStringList &tags = QStringList(),
315  const QString &name = QString() );
316 
328  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
329  const QString &helpText = QString(),
331  bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
332 
333  void loadExpressionContext();
334  void loadRelations();
335  void loadLayers();
336  void loadFieldNames();
337 
349  void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
350 
351 
352  std::unique_ptr<QStandardItemModel> mModel;
353  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
354  QMap<QString, QgsExpressionItem *> mExpressionGroups;
355 
356  MenuProvider *mMenuProvider = nullptr;
357 
358  QgsVectorLayer *mLayer = nullptr;
359  QPointer< QgsProject > mProject;
360  QgsExpressionContext mExpressionContext;
361  QString mRecentKey;
362 
363  QStringList mUserExpressionLabels;
364 };
365 
366 #endif // QGSEXPRESSIONTREEVIEW_H
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Search proxy used to filter the QgsExpressionBuilderWidget tree.
#define SIP_SKIP
Definition: qgis_sip.h:126
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:94
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
An expression item that can be used in the QgsExpressionBuilderWidget tree.
virtual QMenu * createContextMenu(QgsExpressionItem *item)
Returns a newly created menu instance.
QString getExpressionText() const
QString getHelpText() const
Gets the help text that is associated with this expression item.
Represents a vector layer which manages a vector based data sets.
void setHelpText(const QString &helpText)
Set the help text for the current item.