QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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;
102  static const int LAYER_ID_ROLE = Qt::UserRole + 5;
103 
104  private:
105  QString mExpressionText;
106  QString mHelpText;
108 };
109 
110 
117 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
118 {
119  Q_OBJECT
120 
121  public:
123 
124  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
125 
131  void setFilterString( const QString &string );
132 
133  protected:
134 
135  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
136 
137  private:
138 
139  QString mFilterString;
140 };
141 
150 class GUI_EXPORT QgsExpressionTreeView : public QTreeView
151 {
152  Q_OBJECT
153  public:
154 
163  {
164  public:
166  explicit MenuProvider() = default;
167  virtual ~MenuProvider() = default;
168 
170  virtual QMenu *createContextMenu( QgsExpressionItem *item ) SIP_FACTORY {Q_UNUSED( item ) return nullptr;}
171  };
172 
174  QgsExpressionTreeView( QWidget *parent = nullptr );
175 
179  void setLayer( QgsVectorLayer *layer );
180 
184  void loadFieldNames( const QgsFields &fields );
185 
192  void setExpressionContext( const QgsExpressionContext &context );
193 
199  QgsExpressionContext expressionContext() const { return mExpressionContext; }
200 
205  QgsProject *project();
206 
212  void setProject( QgsProject *project );
213 
218  void setMenuProvider( MenuProvider *provider );
219 
223  void refresh();
224 
228  QgsExpressionItem *currentItem() const;
229 
236  Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
237 
242  void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
243 
248  void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
249 
253  void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
254 
258  void removeFromUserExpressions( const QString &label );
259 
264  void loadUserExpressions( );
265 
269  const QList<QgsExpressionItem *> findExpressions( const QString &label );
270 
274  QStringList userExpressionLabels() const SIP_SKIP;
275 
280  QJsonDocument exportUserExpressions();
281 
286  void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
287 
288  signals:
290  void expressionItemDoubleClicked( const QString &text );
291 
293  void currentExpressionItemChanged( QgsExpressionItem *item );
294 
295  public slots:
297  void setSearchText( const QString &text );
298 
299 
300  private slots:
301  void onDoubleClicked( const QModelIndex &index );
302 
303  void showContextMenu( QPoint pt );
304 
305  void currentItemChanged( const QModelIndex &index, const QModelIndex & );
306 
307  private:
308  void updateFunctionTree();
309 
323  QgsExpressionItem *registerItem( const QString &group, const QString &label, const QString &expressionText,
324  const QString &helpText = QString(),
325  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
326  bool highlightedItem = false, int sortOrder = 1,
327  const QIcon &icon = QIcon(),
328  const QStringList &tags = QStringList(),
329  const QString &name = QString() );
330 
342  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
343  const QString &helpText = QString(),
344  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
345  bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
346 
347  void loadExpressionContext();
348  void loadRelations();
349  void loadLayers();
350  void loadLayerFields( QgsVectorLayer *layer, QgsExpressionItem *parentItem );
351  void loadFieldNames();
352 
364  void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
365 
366 
367  std::unique_ptr<QStandardItemModel> mModel;
368  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
369  QMap<QString, QgsExpressionItem *> mExpressionGroups;
370 
371  MenuProvider *mMenuProvider = nullptr;
372 
373  QgsVectorLayer *mLayer = nullptr;
374  QPointer< QgsProject > mProject;
375  QgsExpressionContext mExpressionContext;
376  QString mRecentKey;
377 
378  QStringList mUserExpressionLabels;
379 };
380 
381 #endif // QGSEXPRESSIONTREEVIEW_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsExpressionItem::Field
@ Field
Definition: qgsexpressiontreeview.h:44
QgsExpressionItem::ItemType
ItemType
Definition: qgsexpressiontreeview.h:41
QgsExpressionItem::QgsExpressionItem
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
Definition: qgsexpressiontreeview.h:48
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsExpressionTreeView
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Definition: qgsexpressiontreeview.h:150
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
qgsexpressioncontext.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsExpressionItemSearchProxy
Search proxy used to filter the QgsExpressionBuilderWidget tree. The default search for a tree model ...
Definition: qgsexpressiontreeview.h:117
QgsExpressionItem::getItemType
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
Definition: qgsexpressiontreeview.h:91
QgsExpressionTreeView::MenuProvider
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
Definition: qgsexpressiontreeview.h:162
qgis_sip.h
QgsExpressionTreeView::MenuProvider::createContextMenu
virtual QMenu * createContextMenu(QgsExpressionItem *item)
Returns a newly created menu instance.
Definition: qgsexpressiontreeview.h:170
QgsExpressionTreeView::expressionContext
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Definition: qgsexpressiontreeview.h:199
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsExpressionItem::QgsExpressionItem
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
Definition: qgsexpressiontreeview.h:60
QgsExpressionItem::getExpressionText
QString getExpressionText() const
Definition: qgsexpressiontreeview.h:70
QgsExpressionItem
An expression item that can be used in the QgsExpressionBuilderWidget tree.
Definition: qgsexpressiontreeview.h:38
QgsExpressionItem::Header
@ Header
Definition: qgsexpressiontreeview.h:43
QgsExpressionItem::setHelpText
void setHelpText(const QString &helpText)
Set the help text for the current item.
Definition: qgsexpressiontreeview.h:84
qgsproject.h
QgsExpressionItem::getHelpText
QString getHelpText() const
Gets the help text that is associated with this expression item.
Definition: qgsexpressiontreeview.h:77