QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
124  protected:
125 
126  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
127 };
128 
137 class GUI_EXPORT QgsExpressionTreeView : public QTreeView
138 {
139  Q_OBJECT
140  public:
141 
150  {
151  public:
153  explicit MenuProvider() = default;
154  virtual ~MenuProvider() = default;
155 
157  virtual QMenu *createContextMenu( QgsExpressionItem *item ) SIP_FACTORY {Q_UNUSED( item ) return nullptr;}
158  };
159 
161  QgsExpressionTreeView( QWidget *parent = nullptr );
162 
166  void setLayer( QgsVectorLayer *layer );
167 
171  void loadFieldNames( const QgsFields &fields );
172 
179  void setExpressionContext( const QgsExpressionContext &context );
180 
186  QgsExpressionContext expressionContext() const { return mExpressionContext; }
187 
192  QgsProject *project();
193 
199  void setProject( QgsProject *project );
200 
205  void setMenuProvider( MenuProvider *provider );
206 
210  void refresh();
211 
215  QgsExpressionItem *currentItem() const;
216 
223  Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
224 
229  void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
230 
235  void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
236 
240  void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText );
241 
245  void removeFromUserExpressions( const QString &label );
246 
251  void loadUserExpressions( );
252 
256  const QList<QgsExpressionItem *> findExpressions( const QString &label );
257 
261  QStringList userExpressionLabels() const SIP_SKIP;
262 
267  QJsonDocument exportUserExpressions();
268 
273  void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
274 
275  signals:
277  void expressionItemDoubleClicked( const QString &text );
278 
280  void currentExpressionItemChanged( QgsExpressionItem *item );
281 
282  public slots:
284  void setSearchText( const QString &text );
285 
286 
287  private slots:
288  void onDoubleClicked( const QModelIndex &index );
289 
290  void showContextMenu( QPoint pt );
291 
292  void currentItemChanged( const QModelIndex &index, const QModelIndex & );
293 
294  private:
295  void updateFunctionTree();
296 
310  void registerItem( const QString &group, const QString &label, const QString &expressionText,
311  const QString &helpText = QString(),
312  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
313  bool highlightedItem = false, int sortOrder = 1,
314  QIcon icon = QIcon(),
315  const QStringList &tags = QStringList(),
316  const QString &name = QString() );
317 
329  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
330  const QString &helpText = QString(),
331  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
332  bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
333 
334  void loadExpressionContext();
335  void loadRelations();
336  void loadLayers();
337  void loadFieldNames();
338 
350  void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
351 
352 
353  std::unique_ptr<QStandardItemModel> mModel;
354  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
355  QMap<QString, QgsExpressionItem *> mExpressionGroups;
356 
357  MenuProvider *mMenuProvider = nullptr;
358 
359  QgsVectorLayer *mLayer = nullptr;
360  QPointer< QgsProject > mProject;
361  QgsExpressionContext mExpressionContext;
362  QString mRecentKey;
363 
364  QStringList mUserExpressionLabels;
365 };
366 
367 #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:99
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