QGIS API Documentation 3.43.0-Master (b60ef06885e)
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 : denis.rouzaud@gmail.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 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"
29#include "qgsfieldproxymodel.h"
30
31
32class QgsMapLayer;
33class QgsVectorLayer;
35
36
48class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
49{
50 Q_OBJECT
51 Q_PROPERTY( QString expressionDialogTitle READ expressionDialogTitle WRITE setExpressionDialogTitle )
52 Q_PROPERTY( QgsFieldProxyModel::Filters filters READ filters WRITE setFilters )
53 Q_PROPERTY( bool allowEmptyFieldName READ allowEmptyFieldName WRITE setAllowEmptyFieldName )
54 Q_PROPERTY( bool allowEvalErrors READ allowEvalErrors WRITE setAllowEvalErrors NOTIFY allowEvalErrorsChanged )
55 Q_PROPERTY( bool buttonVisible READ buttonVisible WRITE setButtonVisible NOTIFY buttonVisibleChanged )
56
57 public:
61 explicit QgsFieldExpressionWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
62
64 void setExpressionDialogTitle( const QString &title );
65
73 void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
74
76 const QString expressionDialogTitle() { return mExpressionDialogTitle; }
77
79 void setFilters( QgsFieldProxyModel::Filters filters );
80
86 void setAllowEmptyFieldName( bool allowEmpty );
87
93 bool allowEmptyFieldName() const;
94
95 void setLeftHandButtonStyle( bool isLeft );
96
98 QgsFieldProxyModel::Filters filters() const { return mFieldProxyModel->filters(); }
99
101 void setGeomCalculator( const QgsDistanceArea &da );
102
108 QString currentField( bool *isExpression = nullptr, bool *isValid = nullptr ) const;
109
113 bool isValidExpression( QString *expressionError = nullptr ) const;
114
118 bool isExpression() const;
119
123 QString currentText() const;
124
130 QString asExpression() const;
131
140 QString expression() const;
141
146 QgsVectorLayer *layer() const;
147
154 void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
155
156#ifndef SIP_RUN
157
171 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
172#else
173
187 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
188 //%MethodCode
189 Py_XINCREF( a2 );
190 Py_BEGIN_ALLOW_THREADS
191 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
193 SIP_BLOCK_THREADS
194 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
195 int state;
196 int sipIsError = 0;
197 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
198 if ( sipIsError == 0 )
199 {
200 res = QgsExpressionContext( *t1 );
201 }
202 sipReleaseType( t1, sipType_QgsExpressionContext, state );
203 SIP_UNBLOCK_THREADS
204 return res;
205 } );
206
207 Py_END_ALLOW_THREADS
208 //%End
209#endif
210
216 bool allowEvalErrors() const;
217
223 void setAllowEvalErrors( bool allowEvalErrors );
224
232 bool buttonVisible() const;
233
241 void setButtonVisible( bool visible );
242
243 signals:
245 void fieldChanged( const QString &fieldName );
246
248 void fieldChanged( const QString &fieldName, bool isValid );
249
256
263
264 public slots:
265
270 void setLayer( QgsMapLayer *layer );
271
273 void setRow( int row ) { mCombo->setCurrentIndex( row ); }
274
276 void setField( const QString &fieldName );
277
282 void setFields( const QgsFields &fields );
283
289 void setExpression( const QString &expression );
290
291 protected slots:
293 void editExpression();
294
296 void expressionEdited( const QString &expression );
297
299 void expressionEditingFinished();
300
301 void currentFieldChanged();
302
308 void updateLineEditStyle( const QString &expression = QString() );
309
310 bool isExpressionValid( const QString &expressionStr );
311
312 protected:
313 void changeEvent( QEvent *event ) override;
314
315 bool eventFilter( QObject *watched, QEvent *event ) override;
316
317 private slots:
318 void reloadLayer();
319
320 void beforeResetModel();
321 void afterResetModel();
322
323 private:
324 QComboBox *mCombo = nullptr;
325 QToolButton *mButton = nullptr;
326 QgsFieldProxyModel *mFieldProxyModel = nullptr;
327 QString mExpressionDialogTitle;
328 std::shared_ptr<const QgsDistanceArea> mDistanceArea;
329 QgsExpressionContext mExpressionContext;
330 const QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
331 QString mBackupExpression;
332 bool mAllowEvalErrors = false;
333
334 QString mCustomPreviewLabel;
335 QList<QPair<QString, QVariant>> mCustomChoices;
336 std::function<QgsExpressionContext( const QVariant & )> mPreviewContextGenerator;
337
338 friend class TestQgsFieldExpressionWidget;
339};
340
341#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...
A widget for selection of layer fields or expression creation.
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.
A proxy model to filter the list of fields of a layer.
QFlags< Filter > Filters
Container of fields for a vector layer.
Definition qgsfields.h:46
Base class for all map layer types.
Definition qgsmaplayer.h:77
Represents a vector layer which manages a vector based dataset.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_TRANSFER
Definition qgis_sip.h:36