QGIS API Documentation  2.14.0-Essen
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:
123 
127  void setLayer( QgsVectorLayer* layer );
128 
132  void loadFieldNames();
133 
134  void loadFieldNames( const QgsFields& fields );
135 
140  void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );
141 
143  void setGeomCalculator( const QgsDistanceArea & da );
144 
147  QString expressionText();
148 
150  void setExpressionText( const QString& expression );
151 
157  QgsExpressionContext expressionContext() const { return mExpressionContext; }
158 
165  void setExpressionContext( const QgsExpressionContext& context );
166 
176  void registerItem( const QString& group, const QString& label, const QString& expressionText,
177  const QString& helpText = "",
179  bool highlightedItem = false, int sortOrder = 1 );
180 
181  bool isExpressionValid();
182 
183  void saveToRecent( const QString& key );
184 
185  void loadRecent( const QString& key );
186 
189  void newFunctionFile( const QString& fileName = "scratch" );
190 
193  void saveFunctionFile( QString fileName );
194 
197  void loadCodeFromFile( QString path );
198 
201  void loadFunctionCode( const QString& code );
202 
205  void updateFunctionFileList( const QString& path );
206 
207  public slots:
208 
212  void loadSampleValues();
213 
217  void loadAllValues();
218 
222  void autosave();
223 
229  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
230 
231  private slots:
232  void showContextMenu( QPoint );
233  void setExpressionState( bool state );
234  void currentChanged( const QModelIndex &index, const QModelIndex & );
235  void operatorButtonClicked();
236  void on_btnRun_pressed();
237  void on_btnNewFile_pressed();
238  void on_cmbFileNames_currentItemChanged( QListWidgetItem* item, QListWidgetItem* lastitem );
239  void on_expressionTree_doubleClicked( const QModelIndex &index );
240  void on_txtExpressionString_textChanged();
241  void on_txtSearchEdit_textChanged();
242  void on_txtSearchEditValues_textChanged();
243  void on_lblPreview_linkActivated( const QString& link );
244  void on_mValuesListView_doubleClicked( const QModelIndex &index );
245  void on_txtPython_textChanged();
246 
247  signals:
253  void expressionParsed( bool isValid );
254 
255  protected:
256  void showEvent( QShowEvent *e );
257 
258  private:
259  void runPythonCode( const QString& code );
260  void updateFunctionTree();
261  void fillFieldValues( const QString &fieldName, int countLimit );
262  QString loadFunctionHelp( QgsExpressionItem* functionName );
263  QString helpStylesheet() const;
264 
265  void loadExpressionContext();
266 
267  bool mAutoSave;
268  QString mFunctionsPath;
269  QgsVectorLayer *mLayer;
270  QStandardItemModel *mModel;
271  QStringListModel *mValuesModel;
272  QSortFilterProxyModel *mProxyValues;
273  QgsExpressionItemSearchProxy *mProxyModel;
274  QMap<QString, QgsExpressionItem*> mExpressionGroups;
275  QgsFeature mFeature;
276  QgsExpressionHighlighter* highlighter;
277  bool mExpressionValid;
278  QgsDistanceArea mDa;
279  QString mRecentKey;
280  QMap<QString, QStringList> mFieldValues;
281  QgsExpressionContext mExpressionContext;
282 };
283 
284 #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:187
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.