QGIS API Documentation  2.13.0-Master
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();
228  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
229 
230  private slots:
231  void showContextMenu( QPoint );
232  void setExpressionState( bool state );
233  void currentChanged( const QModelIndex &index, const QModelIndex & );
234  void operatorButtonClicked();
235  void on_btnRun_pressed();
236  void on_btnNewFile_pressed();
237  void on_cmbFileNames_currentItemChanged( QListWidgetItem* item, QListWidgetItem* lastitem );
238  void on_expressionTree_doubleClicked( const QModelIndex &index );
239  void on_txtExpressionString_textChanged();
240  void on_txtSearchEdit_textChanged();
241  void on_txtSearchEditValues_textChanged();
242  void on_lblPreview_linkActivated( const QString& link );
243  void on_mValuesListView_doubleClicked( const QModelIndex &index );
244  void on_txtPython_textChanged();
245 
246  signals:
252  void expressionParsed( bool isValid );
253 
254  protected:
255  void showEvent( QShowEvent *e );
256 
257  private:
258  void runPythonCode( const QString& code );
259  void updateFunctionTree();
260  void fillFieldValues( const QString &fieldName, int countLimit );
261  QString loadFunctionHelp( QgsExpressionItem* functionName );
262  QString helpStylesheet() const;
263 
264  void loadExpressionContext();
265 
266  bool mAutoSave;
267  QString mFunctionsPath;
268  QgsVectorLayer *mLayer;
269  QStandardItemModel *mModel;
270  QStringListModel *mValuesModel;
271  QSortFilterProxyModel *mProxyValues;
272  QgsExpressionItemSearchProxy *mProxyModel;
273  QMap<QString, QgsExpressionItem*> mExpressionGroups;
274  QgsFeature mFeature;
275  QgsExpressionHighlighter* highlighter;
276  bool mExpressionValid;
277  QgsDistanceArea mDa;
278  QString mRecentKey;
279  QMap<QString, QStringList> mFieldValues;
280  QgsExpressionContext mExpressionContext;
281 };
282 
283 #endif // QGSEXPRESSIONBUILDER_H
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const
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: qgsfield.h:189
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)
virtual void showEvent(QShowEvent *event)
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.
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
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.