QGIS API Documentation  2.99.0-Master (314842d)
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 "ui_qgsexpressionbuilder.h"
21 #include "qgsdistancearea.h"
22 #include "qgsexpressioncontext.h"
23 #include "qgsfeature.h"
24 
25 #include "QStandardItemModel"
26 #include "QStandardItem"
27 #include "QSortFilterProxyModel"
28 #include "QStringListModel"
29 #include "qgis_gui.h"
30 
31 class QgsFields;
33 
37 class QgsExpressionItem : public QStandardItem
38 {
39  public:
40  enum ItemType
41  {
45  };
46 
47  QgsExpressionItem( const QString &label,
48  const QString &expressionText,
49  const QString &helpText,
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,
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 
75  QString getHelpText() const { return mHelpText; }
76 
81  void setHelpText( const QString &helpText ) { mHelpText = helpText; }
82 
87  QgsExpressionItem::ItemType getItemType() const { return mType; }
88 
90  static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
92  static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
93 
94  private:
95  QString mExpressionText;
96  QString mHelpText;
98 
99 };
100 
106 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
107 {
108  Q_OBJECT
109 
110  public:
112 
113  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
114 
115  protected:
116 
117  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
118 };
119 
120 
125 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
126 {
127  Q_OBJECT
128  public:
129 
133  QgsExpressionBuilderWidget( QWidget *parent = nullptr );
135 
139  void setLayer( QgsVectorLayer *layer );
140 
144  void loadFieldNames();
145 
146  void loadFieldNames( const QgsFields &fields );
147 
152  void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues );
153 
155  void setGeomCalculator( const QgsDistanceArea &da );
156 
159  QString expressionText();
160 
162  void setExpressionText( const QString &expression );
163 
169  QgsExpressionContext expressionContext() const { return mExpressionContext; }
170 
177  void setExpressionContext( const QgsExpressionContext &context );
178 
188  void registerItem( const QString &group, const QString &label, const QString &expressionText,
189  const QString &helpText = "",
191  bool highlightedItem = false, int sortOrder = 1 );
192 
193  bool isExpressionValid();
194 
199  void saveToRecent( const QString &collection = "generic" );
200 
205  void loadRecent( const QString &collection = "generic" );
206 
209  void newFunctionFile( const QString &fileName = "scratch" );
210 
213  void saveFunctionFile( QString fileName );
214 
217  void loadCodeFromFile( QString path );
218 
221  void loadFunctionCode( const QString &code );
222 
225  void updateFunctionFileList( const QString &path );
226 
227  public slots:
228 
232  void loadSampleValues();
233 
237  void loadAllValues();
238 
242  void autosave();
243 
249  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
250 
251  private slots:
252  void showContextMenu( QPoint );
253  void setExpressionState( bool state );
254  void currentChanged( const QModelIndex &index, const QModelIndex & );
255  void operatorButtonClicked();
256  void on_btnRun_pressed();
257  void on_btnNewFile_pressed();
258  void on_cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
259  void on_expressionTree_doubleClicked( const QModelIndex &index );
260  void on_txtExpressionString_textChanged();
261  void on_txtSearchEdit_textChanged();
262  void on_txtSearchEditValues_textChanged();
263  void on_lblPreview_linkActivated( const QString &link );
264  void on_mValuesListView_doubleClicked( const QModelIndex &index );
265  void on_txtPython_textChanged();
266 
267  signals:
268 
274  void expressionParsed( bool isValid );
275 
276  protected:
277  void showEvent( QShowEvent *e );
278 
279  private:
280  void runPythonCode( const QString &code );
281  void updateFunctionTree();
282  void fillFieldValues( const QString &fieldName, int countLimit );
283  QString loadFunctionHelp( QgsExpressionItem *functionName );
284  QString helpStylesheet() const;
285 
286  void loadExpressionContext();
287 
297  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
298  const QString &helpText = "",
300  bool highlightedItem = false, int sortOrder = 1 );
301 
302  bool mAutoSave;
303  QString mFunctionsPath;
304  QgsVectorLayer *mLayer = nullptr;
305  QStandardItemModel *mModel = nullptr;
306  QStringListModel *mValuesModel = nullptr;
307  QSortFilterProxyModel *mProxyValues = nullptr;
308  QgsExpressionItemSearchProxy *mProxyModel = nullptr;
309  QMap<QString, QgsExpressionItem *> mExpressionGroups;
310  QgsExpressionHighlighter *highlighter = nullptr;
311  bool mExpressionValid;
312  QgsDistanceArea mDa;
313  QString mRecentKey;
314  QMap<QString, QStringList> mFieldValues;
315  QgsExpressionContext mExpressionContext;
316 };
317 
318 #endif // QGSEXPRESSIONBUILDER_H
static unsigned index
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:39
Search proxy used to filter the QgsExpressionBuilderWidget tree.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static const int ITEM_TYPE_ROLE
Item type role.
QgsExpressionItem::ItemType getItemType() const
Get the type of expression item, e.g., header, field, ExpressionNode.
static const int CUSTOM_SORT_ROLE
Custom sort order role.
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
General purpose distance and area calculator.
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.