QGIS API Documentation  2.99.0-Master (c558d51)
qgsexpressionbuilderwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgisexpressionbuilderwidget.h - A genric 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 
30 class QgsFields;
32 
36 class QgsExpressionItem : public QStandardItem
37 {
38  public:
39  enum ItemType
40  {
44  };
45 
46  QgsExpressionItem( const QString& label,
47  const QString& expressionText,
48  const QString& helpText,
50  : QStandardItem( label )
51  {
52  mExpressionText = expressionText;
53  mHelpText = helpText;
54  mType = itemType;
55  setData( itemType, ItemTypeRole );
56  }
57 
58  QgsExpressionItem( const QString& label,
59  const QString& expressionText,
61  : QStandardItem( label )
62  {
63  mExpressionText = expressionText;
64  mType = itemType;
65  setData( itemType, ItemTypeRole );
66  }
67 
68  QString getExpressionText() const { return mExpressionText; }
69 
74  QString getHelpText() const { return mHelpText; }
75 
80  void setHelpText( const QString& helpText ) { mHelpText = helpText; }
81 
86  QgsExpressionItem::ItemType getItemType() const { return mType; }
87 
89  static const int CustomSortRole = Qt::UserRole + 1;
91  static const int ItemTypeRole = Qt::UserRole + 2;
92 
93  private:
94  QString mExpressionText;
95  QString mHelpText;
97 
98 };
99 
105 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
106 {
107  Q_OBJECT
108 
109  public:
111 
112  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
113 
114  protected:
115 
116  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
117 };
118 
119 
124 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
125 {
126  Q_OBJECT
127  public:
128 
132  QgsExpressionBuilderWidget( QWidget* parent = nullptr );
134 
138  void setLayer( QgsVectorLayer* layer );
139 
143  void loadFieldNames();
144 
145  void loadFieldNames( const QgsFields& fields );
146 
151  void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );
152 
154  void setGeomCalculator( const QgsDistanceArea & da );
155 
158  QString expressionText();
159 
161  void setExpressionText( const QString& expression );
162 
168  QgsExpressionContext expressionContext() const { return mExpressionContext; }
169 
176  void setExpressionContext( const QgsExpressionContext& context );
177 
187  void registerItem( const QString& group, const QString& label, const QString& expressionText,
188  const QString& helpText = "",
190  bool highlightedItem = false, int sortOrder = 1 );
191 
192  bool isExpressionValid();
193 
198  void saveToRecent( const QString& collection = "generic" );
199 
204  void loadRecent( const QString& collection = "generic" );
205 
208  void newFunctionFile( const QString& fileName = "scratch" );
209 
212  void saveFunctionFile( QString fileName );
213 
216  void loadCodeFromFile( QString path );
217 
220  void loadFunctionCode( const QString& code );
221 
224  void updateFunctionFileList( const QString& path );
225 
226  public slots:
227 
231  void loadSampleValues();
232 
236  void loadAllValues();
237 
241  void autosave();
242 
248  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
249 
250  private slots:
251  void showContextMenu( QPoint );
252  void setExpressionState( bool state );
253  void currentChanged( const QModelIndex &index, const QModelIndex & );
254  void operatorButtonClicked();
255  void on_btnRun_pressed();
256  void on_btnNewFile_pressed();
257  void on_cmbFileNames_currentItemChanged( QListWidgetItem* item, QListWidgetItem* lastitem );
258  void on_expressionTree_doubleClicked( const QModelIndex &index );
259  void on_txtExpressionString_textChanged();
260  void on_txtSearchEdit_textChanged();
261  void on_txtSearchEditValues_textChanged();
262  void on_lblPreview_linkActivated( const QString& link );
263  void on_mValuesListView_doubleClicked( const QModelIndex &index );
264  void on_txtPython_textChanged();
265 
266  signals:
267 
273  void expressionParsed( bool isValid );
274 
275  protected:
276  void showEvent( QShowEvent *e );
277 
278  private:
279  void runPythonCode( const QString& code );
280  void updateFunctionTree();
281  void fillFieldValues( const QString &fieldName, int countLimit );
282  QString loadFunctionHelp( QgsExpressionItem* functionName );
283  QString helpStylesheet() const;
284 
285  void loadExpressionContext();
286 
296  void registerItemForAllGroups( const QStringList& groups, const QString& label, const QString& expressionText,
297  const QString& helpText = "",
299  bool highlightedItem = false, int sortOrder = 1 );
300 
301  bool mAutoSave;
302  QString mFunctionsPath;
303  QgsVectorLayer *mLayer;
304  QStandardItemModel *mModel;
305  QStringListModel *mValuesModel;
306  QSortFilterProxyModel *mProxyValues;
307  QgsExpressionItemSearchProxy *mProxyModel;
308  QMap<QString, QgsExpressionItem*> mExpressionGroups;
309  QgsFeature mFeature;
310  QgsExpressionHighlighter* highlighter;
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
QString getExpressionText() const
QString getHelpText() const
Get the help text that is associated with this expression item.
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
static const int CustomSortRole
Custom sort order role.
void setAutoSave(bool enabled)
Enabled or disable auto saving.
Container of fields for a vector layer.
Definition: qgsfields.h:36
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
Search proxy used to filter the QgsExpressionBuilderWidget tree.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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.
static const int ItemTypeRole
Item type role.
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Represents a vector layer which manages a vector based data sets.
QgsExpressionItem::ItemType getItemType() const
Get the type of expression item eg header, field, ExpressionNode.
void setHelpText(const QString &helpText)
Set the help text for the current item.