QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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;
98 
99  private:
100  QString mExpressionText;
101  QString mHelpText;
103 };
104 
105 
112 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
113 {
114  Q_OBJECT
115 
116  public:
118 
119  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
120 
121  protected:
122 
123  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
124 };
125 
134 class GUI_EXPORT QgsExpressionTreeView : public QTreeView
135 {
136  Q_OBJECT
137  public:
138 
147  {
148  public:
150  explicit MenuProvider() = default;
151  virtual ~MenuProvider() = default;
152 
154  virtual QMenu *createContextMenu( QgsExpressionItem *item ) SIP_FACTORY {Q_UNUSED( item ) return nullptr;}
155  };
156 
158  QgsExpressionTreeView( QWidget *parent = nullptr );
159 
163  void setLayer( QgsVectorLayer *layer );
164 
168  void loadFieldNames( const QgsFields &fields );
169 
176  void setExpressionContext( const QgsExpressionContext &context );
177 
183  QgsExpressionContext expressionContext() const { return mExpressionContext; }
184 
189  QgsProject *project();
190 
196  void setProject( QgsProject *project );
197 
202  void setMenuProvider( MenuProvider *provider );
203 
207  void refresh();
208 
212  QgsExpressionItem *currentItem() const;
213 
220  Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
221 
226  void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
227 
232  void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
233 
237  void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText );
238 
242  void removeFromUserExpressions( const QString &label );
243 
248  void loadUserExpressions( );
249 
253  const QList<QgsExpressionItem *> findExpressions( const QString &label );
254 
258  QStringList userExpressionLabels() const SIP_SKIP;
259 
264  QJsonDocument exportUserExpressions();
265 
270  void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
271 
272  signals:
274  void expressionItemDoubleClicked( const QString &text );
275 
277  void currentExpressionItemChanged( QgsExpressionItem *item );
278 
279  public slots:
281  void setSearchText( const QString &text );
282 
283 
284  private slots:
285  void onDoubleClicked( const QModelIndex &index );
286 
287  void showContextMenu( QPoint pt );
288 
289  void currentItemChanged( const QModelIndex &index, const QModelIndex & );
290 
291  private:
292  void updateFunctionTree();
293 
306  void registerItem( const QString &group, const QString &label, const QString &expressionText,
307  const QString &helpText = QString(),
308  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
309  bool highlightedItem = false, int sortOrder = 1,
310  QIcon icon = QIcon(),
311  const QStringList &tags = QStringList() );
312 
324  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
325  const QString &helpText = QString(),
326  QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
327  bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
328 
329  void loadExpressionContext();
330  void loadRelations();
331  void loadLayers();
332  void loadFieldNames();
333 
345  void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
346 
347 
348  std::unique_ptr<QStandardItemModel> mModel;
349  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
350  QMap<QString, QgsExpressionItem *> mExpressionGroups;
351 
352  MenuProvider *mMenuProvider = nullptr;
353 
354  QgsVectorLayer *mLayer = nullptr;
355  QPointer< QgsProject > mProject;
356  QgsExpressionContext mExpressionContext;
357  QString mRecentKey;
358 
359  QStringList mUserExpressionLabels;
360 };
361 
362 #endif // QGSEXPRESSIONTREEVIEW_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsFields
Definition: qgsfields.h:44
QgsExpressionItem::Field
@ Field
Definition: qgsexpressiontreeview.h:43
QgsExpressionItem::ItemType
ItemType
Definition: qgsexpressiontreeview.h:40
QgsExpressionItem::QgsExpressionItem
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
Definition: qgsexpressiontreeview.h:47
QgsProject
Definition: qgsproject.h:92
QgsExpressionTreeView
Definition: qgsexpressiontreeview.h:134
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
Definition: qgsexpressiontreeview.h:112
QgsExpressionItem::getItemType
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
Definition: qgsexpressiontreeview.h:90
QgsExpressionTreeView::MenuProvider
Definition: qgsexpressiontreeview.h:146
qgis_sip.h
QgsExpressionTreeView::MenuProvider::createContextMenu
virtual QMenu * createContextMenu(QgsExpressionItem *item)
Returns a newly created menu instance.
Definition: qgsexpressiontreeview.h:154
QgsExpressionTreeView::expressionContext
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Definition: qgsexpressiontreeview.h:183
QgsVectorLayer
Definition: qgsvectorlayer.h:385
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
Definition: qgsexpressiontreeview.h:37
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