QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
308  void registerItem( const QString &group, const QString &label, const QString &expressionText,
309  const QString &helpText = QString(),
310  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
311  bool highlightedItem = false, int sortOrder = 1,
312  QIcon icon = QIcon(),
313  const QStringList &tags = QStringList(),
314  const QString &name = QString() );
315 
327  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
328  const QString &helpText = QString(),
329  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
330  bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
331 
332  void loadExpressionContext();
333  void loadRelations();
334  void loadLayers();
335  void loadFieldNames();
336 
348  void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
349 
350 
351  std::unique_ptr<QStandardItemModel> mModel;
352  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
353  QMap<QString, QgsExpressionItem *> mExpressionGroups;
354 
355  MenuProvider *mMenuProvider = nullptr;
356 
357  QgsVectorLayer *mLayer = nullptr;
358  QPointer< QgsProject > mProject;
359  QgsExpressionContext mExpressionContext;
360  QString mRecentKey;
361 
362  QStringList mUserExpressionLabels;
363 };
364 
365 #endif // QGSEXPRESSIONTREEVIEW_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsExpressionItem::Field
@ Field
Definition: qgsexpressiontreeview.h:43
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:47
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
QgsExpressionTreeView
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Definition: qgsexpressiontreeview.h:137
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsExpressionTreeView::MenuProvider::~MenuProvider
virtual ~MenuProvider()=default
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.
Definition: qgsexpressiontreeview.h:115
QgsExpressionItem::getItemType
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
Definition: qgsexpressiontreeview.h:90
QgsExpressionTreeView::MenuProvider
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
Definition: qgsexpressiontreeview.h:149
qgis_sip.h
QgsExpressionTreeView::MenuProvider::MenuProvider
MenuProvider()=default
Constructor.
QgsExpressionTreeView::MenuProvider::createContextMenu
virtual QMenu * createContextMenu(QgsExpressionItem *item)
Returns a newly created menu instance.
Definition: qgsexpressiontreeview.h:156
QgsExpressionTreeView::expressionContext
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Definition: qgsexpressiontreeview.h:185
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsExpressionItem::QgsExpressionItem
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
Definition: qgsexpressiontreeview.h:59
QgsExpressionItem::getExpressionText
QString getExpressionText() const
Definition: qgsexpressiontreeview.h:69
QgsExpressionItem
An expression item that can be used in the QgsExpressionBuilderWidget tree.
Definition: qgsexpressiontreeview.h:38
QgsExpressionItem::Header
@ Header
Definition: qgsexpressiontreeview.h:42
QgsExpressionItem::setHelpText
void setHelpText(const QString &helpText)
Set the help text for the current item.
Definition: qgsexpressiontreeview.h:83
qgsproject.h
QgsExpressionItem::getHelpText
QString getHelpText() const
Gets the help text that is associated with this expression item.
Definition: qgsexpressiontreeview.h:76