QGIS API Documentation  2.17.0-Master (0497e4a)
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 "qgsvectorlayer.h"
23 #include "qgsdistancearea.h"
24 
25 #include "QStandardItemModel"
26 #include "QStandardItem"
27 #include "QSortFilterProxyModel"
28 #include "QStringListModel"
29 
34 {
35  public:
36  enum ItemType
37  {
41  };
42 
43  QgsExpressionItem( const QString& label,
44  const QString& expressionText,
45  const QString& helpText,
47  : QStandardItem( label )
48  {
49  mExpressionText = expressionText;
50  mHelpText = helpText;
51  mType = itemType;
52  setData( itemType, ItemTypeRole );
53  }
54 
55  QgsExpressionItem( const QString& label,
56  const QString& expressionText,
58  : QStandardItem( label )
59  {
60  mExpressionText = expressionText;
61  mType = itemType;
62  setData( itemType, ItemTypeRole );
63  }
64 
65  QString getExpressionText() const { return mExpressionText; }
66 
71  QString getHelpText() const { return mHelpText; }
76  void setHelpText( const QString& helpText ) { mHelpText = helpText; }
77 
82  QgsExpressionItem::ItemType getItemType() const { return mType; }
83 
85  static const int CustomSortRole = Qt::UserRole + 1;
87  static const int ItemTypeRole = Qt::UserRole + 2;
88 
89  private:
90  QString mExpressionText;
91  QString mHelpText;
93 
94 };
95 
102 {
103  Q_OBJECT
104 
105  public:
107 
108  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
109 
110  protected:
111 
112  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
113 };
114 
115 
120 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
121 {
122  Q_OBJECT
123  public:
129 
133  void setLayer( QgsVectorLayer* layer );
134 
138  void loadFieldNames();
139 
140  void loadFieldNames( const QgsFields& fields );
141 
146  void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );
147 
149  void setGeomCalculator( const QgsDistanceArea & da );
150 
153  QString expressionText();
154 
156  void setExpressionText( const QString& expression );
157 
163  QgsExpressionContext expressionContext() const { return mExpressionContext; }
164 
171  void setExpressionContext( const QgsExpressionContext& context );
172 
182  void registerItem( const QString& group, const QString& label, const QString& expressionText,
183  const QString& helpText = "",
185  bool highlightedItem = false, int sortOrder = 1 );
186 
187  bool isExpressionValid();
188 
193  void saveToRecent( const QString& collection = "generic" );
194 
199  void loadRecent( const QString& collection = "generic" );
200 
203  void newFunctionFile( const QString& fileName = "scratch" );
204 
207  void saveFunctionFile( QString fileName );
208 
211  void loadCodeFromFile( QString path );
212 
215  void loadFunctionCode( const QString& code );
216 
219  void updateFunctionFileList( const QString& path );
220 
221  public slots:
222 
226  void loadSampleValues();
227 
231  void loadAllValues();
232 
236  void autosave();
237 
243  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
244 
245  private slots:
246  void showContextMenu( QPoint );
247  void setExpressionState( bool state );
248  void currentChanged( const QModelIndex &index, const QModelIndex & );
249  void operatorButtonClicked();
250  void on_btnRun_pressed();
251  void on_btnNewFile_pressed();
252  void on_cmbFileNames_currentItemChanged( QListWidgetItem* item, QListWidgetItem* lastitem );
253  void on_expressionTree_doubleClicked( const QModelIndex &index );
254  void on_txtExpressionString_textChanged();
255  void on_txtSearchEdit_textChanged();
256  void on_txtSearchEditValues_textChanged();
257  void on_lblPreview_linkActivated( const QString& link );
258  void on_mValuesListView_doubleClicked( const QModelIndex &index );
259  void on_txtPython_textChanged();
260 
261  signals:
267  void expressionParsed( bool isValid );
268 
269  protected:
270  void showEvent( QShowEvent *e );
271 
272  private:
273  void runPythonCode( const QString& code );
274  void updateFunctionTree();
275  void fillFieldValues( const QString &fieldName, int countLimit );
276  QString loadFunctionHelp( QgsExpressionItem* functionName );
277  QString helpStylesheet() const;
278 
279  void loadExpressionContext();
280 
281  bool mAutoSave;
282  QString mFunctionsPath;
283  QgsVectorLayer *mLayer;
284  QStandardItemModel *mModel;
285  QStringListModel *mValuesModel;
286  QSortFilterProxyModel *mProxyValues;
287  QgsExpressionItemSearchProxy *mProxyModel;
288  QMap<QString, QgsExpressionItem*> mExpressionGroups;
289  QgsFeature mFeature;
290  QgsExpressionHighlighter* highlighter;
291  bool mExpressionValid;
292  QgsDistanceArea mDa;
293  QString mRecentKey;
294  QMap<QString, QStringList> mFieldValues;
295  QgsExpressionContext mExpressionContext;
296 };
297 
298 #endif // QGSEXPRESSIONBUILDER_H
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: qgsfield.h:252
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
virtual void setData(const QVariant &value, int role)
Search proxy used to filter the QgsExpressionBuilderWidget tree.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QStandardItem * parent() const
virtual int type() const
QgsExpressionItem::ItemType getItemType() const
Get the type of expression item eg header, field, ExpressionNode.
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
static const int ItemTypeRole
Item type role.
QModelIndex index() 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.