QGIS API Documentation  3.0.2-Girona (307d082)
qgsfieldexpressionwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldexpressionwidget.h
3  --------------------------------------
4  Date : 01.04.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
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 QGSFIELDEXPRESSIONWIDGET_H
17 #define QGSFIELDEXPRESSIONWIDGET_H
18 
19 #include <QColor>
20 #include <QComboBox>
21 #include <QToolButton>
22 #include <QWidget>
23 #include <memory>
24 
25 #include "qgis_gui.h"
26 #include "qgis.h"
27 #include "qgsdistancearea.h"
29 #include "qgsexpressioncontext.h"
30 #include "qgsfieldproxymodel.h"
31 
32 
33 class QgsMapLayer;
34 class QgsVectorLayer;
35 
36 
46 class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
47 {
48  Q_OBJECT
49  Q_PROPERTY( QString expressionDialogTitle READ expressionDialogTitle WRITE setExpressionDialogTitle )
50  Q_PROPERTY( QgsFieldProxyModel::Filters filters READ filters WRITE setFilters )
51  Q_PROPERTY( bool allowEvalErrors READ allowEvalErrors WRITE setAllowEvalErrors NOTIFY allowEvalErrorsChanged )
52 
53  public:
54 
58  explicit QgsFieldExpressionWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
59 
61  void setExpressionDialogTitle( const QString &title );
62 
64  const QString expressionDialogTitle() { return mExpressionDialogTitle; }
65 
67  void setFilters( QgsFieldProxyModel::Filters filters );
68 
69  void setLeftHandButtonStyle( bool isLeft );
70 
72  QgsFieldProxyModel::Filters filters() const { return mFieldProxyModel->filters(); }
73 
75  void setGeomCalculator( const QgsDistanceArea &da );
76 
82  QString currentField( bool *isExpression = nullptr, bool *isValid = nullptr ) const;
83 
87  bool isValidExpression( QString *expressionError = nullptr ) const;
88 
92  bool isExpression() const;
93 
97  QString currentText() const;
98 
105  QString asExpression() const;
106 
116  QString expression() const;
117 
122  QgsVectorLayer *layer() const;
123 
131  void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
132 
139  bool allowEvalErrors() const;
140 
147  void setAllowEvalErrors( bool allowEvalErrors );
148 
149  signals:
151  void fieldChanged( const QString &fieldName );
152 
154  void fieldChanged( const QString &fieldName, bool isValid );
155 
162  void allowEvalErrorsChanged();
163 
164  public slots:
165 
170  void setLayer( QgsMapLayer *layer );
171 
173  void setRow( int row ) { mCombo->setCurrentIndex( row ); }
174 
176  void setField( const QString &fieldName );
177 
184  void setExpression( const QString &expression );
185 
186  protected slots:
188  void editExpression();
189 
191  void expressionEdited( const QString &expression );
192 
194  void expressionEditingFinished();
195 
196  void currentFieldChanged();
197 
203  void updateLineEditStyle( const QString &expression = QString() );
204 
205  bool isExpressionValid( const QString &expressionStr );
206 
207  protected:
208  void changeEvent( QEvent *event ) override;
209 
210  private slots:
211  void reloadLayer();
212 
213  void beforeResetModel();
214  void afterResetModel();
215 
216  private:
217  QComboBox *mCombo = nullptr;
218  QToolButton *mButton = nullptr;
219  QgsFieldProxyModel *mFieldProxyModel = nullptr;
220  QString mExpressionDialogTitle;
221  std::shared_ptr<const QgsDistanceArea> mDa;
222  QgsExpressionContext mExpressionContext;
223  const QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
224  QString mBackupExpression;
225  bool mAllowEvalErrors = false;
226 
227  friend class TestQgsFieldExpressionWidget;
228 };
229 
230 #endif // QGSFIELDEXPRESSIONWIDGET_H
The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains ...
Base class for all map layer types.
Definition: qgsmaplayer.h:56
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QgsFieldProxyModel::Filters filters() const
currently used filter on list of fields
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract interface for generating an expression context.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
void setRow(int row)
sets the current row in the widget
Represents a vector layer which manages a vector based data sets.