QGIS API Documentation  2.99.0-Master (23ddace)
qgsexpressionbuilderwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgisexpressionbuilderwidget.h - A generic expression string builder widget.
3  --------------------------------------
4  Date : 29-May-2011
5  Copyright : (C) 2011 by Nathan Woodrow
6  Email : woodrow.nathan at gmail dot com
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 QGSEXPRESSIONBUILDER_H
17 #define QGSEXPRESSIONBUILDER_H
18 
19 #include <QWidget>
20 #include "qgis.h"
21 #include "ui_qgsexpressionbuilder.h"
22 #include "qgsdistancearea.h"
23 #include "qgsexpressioncontext.h"
24 #include "qgsfeature.h"
25 
26 #include "QStandardItemModel"
27 #include "QStandardItem"
28 #include "QSortFilterProxyModel"
29 #include "QStringListModel"
30 #include "qgis_gui.h"
31 
32 class QgsFields;
34 
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 
76  QString getHelpText() const { return mHelpText; }
77 
82  void setHelpText( const QString &helpText ) { mHelpText = helpText; }
83 
88  QgsExpressionItem::ItemType getItemType() const { return mType; }
89 
91  static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
93  static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
94 
95  private:
96  QString mExpressionText;
97  QString mHelpText;
99 
100 };
101 
107 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
108 {
109  Q_OBJECT
110 
111  public:
113 
114  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
115 
116  protected:
117 
118  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
119 };
120 
121 
126 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
127 {
128  Q_OBJECT
129  public:
130 
134  QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = 0 );
136 
140  void setLayer( QgsVectorLayer *layer );
141 
145  void loadFieldNames();
146 
147  void loadFieldNames( const QgsFields &fields );
148 
153  void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues );
154 
156  void setGeomCalculator( const QgsDistanceArea &da );
157 
160  QString expressionText();
161 
163  void setExpressionText( const QString &expression );
164 
170  QgsExpressionContext expressionContext() const { return mExpressionContext; }
171 
178  void setExpressionContext( const QgsExpressionContext &context );
179 
189  void registerItem( const QString &group, const QString &label, const QString &expressionText,
190  const QString &helpText = "",
192  bool highlightedItem = false, int sortOrder = 1 );
193 
194  bool isExpressionValid();
195 
200  void saveToRecent( const QString &collection = "generic" );
201 
206  void loadRecent( const QString &collection = "generic" );
207 
210  void newFunctionFile( const QString &fileName = "scratch" );
211 
214  void saveFunctionFile( QString fileName );
215 
218  void loadCodeFromFile( QString path );
219 
222  void loadFunctionCode( const QString &code );
223 
226  void updateFunctionFileList( const QString &path );
227 
228  public slots:
229 
233  void loadSampleValues();
234 
238  void loadAllValues();
239 
243  void autosave();
244 
250  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
251 
252  private slots:
253  void showContextMenu( QPoint );
254  void setExpressionState( bool state );
255  void currentChanged( const QModelIndex &index, const QModelIndex & );
256  void operatorButtonClicked();
257  void on_btnRun_pressed();
258  void on_btnNewFile_pressed();
259  void on_cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
260  void on_expressionTree_doubleClicked( const QModelIndex &index );
261  void on_txtExpressionString_textChanged();
262  void on_txtSearchEdit_textChanged();
263  void on_txtSearchEditValues_textChanged();
264  void on_lblPreview_linkActivated( const QString &link );
265  void on_mValuesListView_doubleClicked( const QModelIndex &index );
266  void on_txtPython_textChanged();
267 
268  signals:
269 
275  void expressionParsed( bool isValid );
276 
277  protected:
278  void showEvent( QShowEvent *e );
279 
280  private:
281  void runPythonCode( const QString &code );
282  void updateFunctionTree();
283  void fillFieldValues( const QString &fieldName, int countLimit );
284  QString loadFunctionHelp( QgsExpressionItem *functionName );
285  QString helpStylesheet() const;
286 
287  void loadExpressionContext();
288 
298  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
299  const QString &helpText = "",
301  bool highlightedItem = false, int sortOrder = 1 );
302 
303  bool mAutoSave;
304  QString mFunctionsPath;
305  QgsVectorLayer *mLayer = nullptr;
306  QStandardItemModel *mModel = nullptr;
307  QStringListModel *mValuesModel = nullptr;
308  QSortFilterProxyModel *mProxyValues = nullptr;
309  QgsExpressionItemSearchProxy *mProxyModel = nullptr;
310  QMap<QString, QgsExpressionItem *> mExpressionGroups;
311  QgsExpressionHighlighter *highlighter = nullptr;
312  bool mExpressionValid;
313  QgsDistanceArea mDa;
314  QString mRecentKey;
315  QMap<QString, QStringList> mFieldValues;
316  QgsExpressionContext mExpressionContext;
317 };
318 
319 #endif // QGSEXPRESSIONBUILDER_H
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:34
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
void setAutoSave(bool enabled)
Enabled or disable auto saving.
Container of fields for a vector layer.
Definition: qgsfields.h:41
Search proxy used to filter the QgsExpressionBuilderWidget tree.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionItem::ItemType getItemType() const
Get the type of expression item, e.g., header, field, ExpressionNode.
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
A reusable widget that can be used to build a expression string.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
QString getExpressionText() const
QString getHelpText() const
Get 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.