QGIS API Documentation  2.15.0-Master (972fc9f)
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 
33 {
34  public:
35  enum ItemType
36  {
40  };
41 
42  QgsExpressionItem( const QString& label,
43  const QString& expressionText,
44  const QString& helpText,
46  : QStandardItem( label )
47  {
48  mExpressionText = expressionText;
49  mHelpText = helpText;
50  mType = itemType;
51  setData( itemType, ItemTypeRole );
52  }
53 
54  QgsExpressionItem( const QString& label,
55  const QString& expressionText,
57  : QStandardItem( label )
58  {
59  mExpressionText = expressionText;
60  mType = itemType;
61  setData( itemType, ItemTypeRole );
62  }
63 
64  QString getExpressionText() const { return mExpressionText; }
65 
70  QString getHelpText() const { return mHelpText; }
75  void setHelpText( const QString& helpText ) { mHelpText = helpText; }
76 
81  QgsExpressionItem::ItemType getItemType() const { return mType; }
82 
84  static const int CustomSortRole = Qt::UserRole + 1;
86  static const int ItemTypeRole = Qt::UserRole + 2;
87 
88  private:
89  QString mExpressionText;
90  QString mHelpText;
92 
93 };
94 
100 {
101  Q_OBJECT
102 
103  public:
105 
106  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
107 
108  protected:
109 
110  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
111 };
112 
113 
117 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
118 {
119  Q_OBJECT
120  public:
126 
130  void setLayer( QgsVectorLayer* layer );
131 
135  void loadFieldNames();
136 
137  void loadFieldNames( const QgsFields& fields );
138 
143  void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );
144 
146  void setGeomCalculator( const QgsDistanceArea & da );
147 
150  QString expressionText();
151 
153  void setExpressionText( const QString& expression );
154 
160  QgsExpressionContext expressionContext() const { return mExpressionContext; }
161 
168  void setExpressionContext( const QgsExpressionContext& context );
169 
179  void registerItem( const QString& group, const QString& label, const QString& expressionText,
180  const QString& helpText = "",
182  bool highlightedItem = false, int sortOrder = 1 );
183 
184  bool isExpressionValid();
185 
186  void saveToRecent( const QString& key );
187 
188  void loadRecent( const QString& key );
189 
192  void newFunctionFile( const QString& fileName = "scratch" );
193 
196  void saveFunctionFile( QString fileName );
197 
200  void loadCodeFromFile( QString path );
201 
204  void loadFunctionCode( const QString& code );
205 
208  void updateFunctionFileList( const QString& path );
209 
210  public slots:
211 
215  void loadSampleValues();
216 
220  void loadAllValues();
221 
225  void autosave();
226 
232  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
233 
234  private slots:
235  void showContextMenu( QPoint );
236  void setExpressionState( bool state );
237  void currentChanged( const QModelIndex &index, const QModelIndex & );
238  void operatorButtonClicked();
239  void on_btnRun_pressed();
240  void on_btnNewFile_pressed();
241  void on_cmbFileNames_currentItemChanged( QListWidgetItem* item, QListWidgetItem* lastitem );
242  void on_expressionTree_doubleClicked( const QModelIndex &index );
243  void on_txtExpressionString_textChanged();
244  void on_txtSearchEdit_textChanged();
245  void on_txtSearchEditValues_textChanged();
246  void on_lblPreview_linkActivated( const QString& link );
247  void on_mValuesListView_doubleClicked( const QModelIndex &index );
248  void on_txtPython_textChanged();
249 
250  signals:
256  void expressionParsed( bool isValid );
257 
258  protected:
259  void showEvent( QShowEvent *e );
260 
261  private:
262  void runPythonCode( const QString& code );
263  void updateFunctionTree();
264  void fillFieldValues( const QString &fieldName, int countLimit );
265  QString loadFunctionHelp( QgsExpressionItem* functionName );
266  QString helpStylesheet() const;
267 
268  void loadExpressionContext();
269 
270  bool mAutoSave;
271  QString mFunctionsPath;
272  QgsVectorLayer *mLayer;
273  QStandardItemModel *mModel;
274  QStringListModel *mValuesModel;
275  QSortFilterProxyModel *mProxyValues;
276  QgsExpressionItemSearchProxy *mProxyModel;
277  QMap<QString, QgsExpressionItem*> mExpressionGroups;
278  QgsFeature mFeature;
279  QgsExpressionHighlighter* highlighter;
280  bool mExpressionValid;
281  QgsDistanceArea mDa;
282  QString mRecentKey;
283  QMap<QString, QStringList> mFieldValues;
284  QgsExpressionContext mExpressionContext;
285 };
286 
287 #endif // QGSEXPRESSIONBUILDER_H
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: qgsfield.h:193
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(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.
QModelIndex index() const
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.