QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
70  void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
71 
73  const QString expressionDialogTitle() { return mExpressionDialogTitle; }
74 
76  void setFilters( QgsFieldProxyModel::Filters filters );
77 
78  void setLeftHandButtonStyle( bool isLeft );
79 
81  QgsFieldProxyModel::Filters filters() const { return mFieldProxyModel->filters(); }
82 
84  void setGeomCalculator( const QgsDistanceArea &da );
85 
91  QString currentField( bool *isExpression = nullptr, bool *isValid = nullptr ) const;
92 
96  bool isValidExpression( QString *expressionError = nullptr ) const;
97 
101  bool isExpression() const;
102 
106  QString currentText() const;
107 
114  QString asExpression() const;
115 
125  QString expression() const;
126 
131  QgsVectorLayer *layer() const;
132 
140  void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
141 
148  bool allowEvalErrors() const;
149 
156  void setAllowEvalErrors( bool allowEvalErrors );
157 
158  signals:
160  void fieldChanged( const QString &fieldName );
161 
163  void fieldChanged( const QString &fieldName, bool isValid );
164 
171  void allowEvalErrorsChanged();
172 
173  public slots:
174 
179  void setLayer( QgsMapLayer *layer );
180 
182  void setRow( int row ) { mCombo->setCurrentIndex( row ); }
183 
185  void setField( const QString &fieldName );
186 
193  void setExpression( const QString &expression );
194 
195  protected slots:
197  void editExpression();
198 
200  void expressionEdited( const QString &expression );
201 
203  void expressionEditingFinished();
204 
205  void currentFieldChanged();
206 
212  void updateLineEditStyle( const QString &expression = QString() );
213 
214  bool isExpressionValid( const QString &expressionStr );
215 
216  protected:
217  void changeEvent( QEvent *event ) override;
218 
219  private slots:
220  void reloadLayer();
221 
222  void beforeResetModel();
223  void afterResetModel();
224 
225  private:
226  QComboBox *mCombo = nullptr;
227  QToolButton *mButton = nullptr;
228  QgsFieldProxyModel *mFieldProxyModel = nullptr;
229  QString mExpressionDialogTitle;
230  std::shared_ptr<const QgsDistanceArea> mDa;
231  QgsExpressionContext mExpressionContext;
232  const QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
233  QString mBackupExpression;
234  bool mAllowEvalErrors = false;
235 
236  friend class TestQgsFieldExpressionWidget;
237 };
238 
239 #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:61
#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...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Single scope for storing variables and functions for use within a QgsExpressionContext.
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.