QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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_sip.h"
27 #include "qgsdistancearea.h"
28 #include "qgsexpressioncontext.h"
29 #include "qgsfieldproxymodel.h"
30 
31 
32 class QgsMapLayer;
33 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 allowEmptyFieldName READ allowEmptyFieldName WRITE setAllowEmptyFieldName )
52  Q_PROPERTY( bool allowEvalErrors READ allowEvalErrors WRITE setAllowEvalErrors NOTIFY allowEvalErrorsChanged )
53  Q_PROPERTY( bool buttonVisible READ buttonVisible WRITE setButtonVisible NOTIFY buttonVisibleChanged )
54 
55  public:
56 
60  explicit QgsFieldExpressionWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
61 
63  void setExpressionDialogTitle( const QString &title );
64 
72  void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
73 
75  const QString expressionDialogTitle() { return mExpressionDialogTitle; }
76 
78  void setFilters( QgsFieldProxyModel::Filters filters );
79 
85  void setAllowEmptyFieldName( bool allowEmpty );
86 
92  bool allowEmptyFieldName() const;
93 
94  void setLeftHandButtonStyle( bool isLeft );
95 
97  QgsFieldProxyModel::Filters filters() const { return mFieldProxyModel->filters(); }
98 
100  void setGeomCalculator( const QgsDistanceArea &da );
101 
107  QString currentField( bool *isExpression = nullptr, bool *isValid = nullptr ) const;
108 
112  bool isValidExpression( QString *expressionError = nullptr ) const;
113 
117  bool isExpression() const;
118 
122  QString currentText() const;
123 
129  QString asExpression() const;
130 
139  QString expression() const;
140 
145  QgsVectorLayer *layer() const;
146 
153  void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
154 
160  bool allowEvalErrors() const;
161 
167  void setAllowEvalErrors( bool allowEvalErrors );
168 
176  bool buttonVisible() const;
177 
185  void setButtonVisible( bool visible );
186 
187  signals:
189  void fieldChanged( const QString &fieldName );
190 
192  void fieldChanged( const QString &fieldName, bool isValid );
193 
200 
207 
208  public slots:
209 
214  void setLayer( QgsMapLayer *layer );
215 
217  void setRow( int row ) { mCombo->setCurrentIndex( row ); }
218 
220  void setField( const QString &fieldName );
221 
226  void setFields( const QgsFields &fields );
227 
233  void setExpression( const QString &expression );
234 
235  protected slots:
237  void editExpression();
238 
240  void expressionEdited( const QString &expression );
241 
243  void expressionEditingFinished();
244 
245  void currentFieldChanged();
246 
252  void updateLineEditStyle( const QString &expression = QString() );
253 
254  bool isExpressionValid( const QString &expressionStr );
255 
256  protected:
257  void changeEvent( QEvent *event ) override;
258 
259  bool eventFilter( QObject *watched, QEvent *event ) override;
260 
261  private slots:
262  void reloadLayer();
263 
264  void beforeResetModel();
265  void afterResetModel();
266 
267  private:
268  QComboBox *mCombo = nullptr;
269  QToolButton *mButton = nullptr;
270  QgsFieldProxyModel *mFieldProxyModel = nullptr;
271  QString mExpressionDialogTitle;
272  std::shared_ptr<const QgsDistanceArea> mDistanceArea;
273  QgsExpressionContext mExpressionContext;
274  const QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
275  QString mBackupExpression;
276  bool mAllowEvalErrors = false;
277 
278  friend class TestQgsFieldExpressionWidget;
279 };
280 
281 #endif // QGSFIELDEXPRESSIONWIDGET_H
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Abstract interface for generating an expression context.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains...
const QString expressionDialogTitle()
Returns the title used for the expression dialog.
QgsFieldProxyModel::Filters filters() const
currently used filter on list of fields
void setRow(int row)
sets the current row in the widget
void buttonVisibleChanged()
Emitted when the button visibility changes.
void fieldChanged(const QString &fieldName, bool isValid)
fieldChanged signal with indication of the validity of the expression
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void allowEvalErrorsChanged()
Allow accepting expressions with evaluation errors.
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer.
QFlags< Filter > Filters
Container of fields for a vector layer.
Definition: qgsfields.h:45
Base class for all map layer types.
Definition: qgsmaplayer.h:75
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_TRANSFER
Definition: qgis_sip.h:36