QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
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
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 <memory>
20
21#include "qgis_gui.h"
22#include "qgis_sip.h"
23#include "qgsdistancearea.h"
25#include "qgsfieldproxymodel.h"
26
27#include <QColor>
28#include <QComboBox>
29#include <QToolButton>
30#include <QWidget>
31
32class QgsMapLayer;
33class QgsVectorLayer;
35
36
48class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
49{
50 Q_OBJECT
52 Q_PROPERTY( QgsFieldProxyModel::Filters filters READ filters WRITE setFilters )
56
57 public:
58
62 explicit QgsFieldExpressionWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
63
65 void setExpressionDialogTitle( const QString &title );
66
75
77 QString expressionDialogTitle() const { return mExpressionDialogTitle; }
78
80 void setFilters( QgsFieldProxyModel::Filters filters );
81
87 void setAllowEmptyFieldName( bool allowEmpty );
88
94 bool allowEmptyFieldName() const;
95
96 void setLeftHandButtonStyle( bool isLeft );
97
99 QgsFieldProxyModel::Filters filters() const { return mFieldProxyModel->filters(); }
100
102 void setGeomCalculator( const QgsDistanceArea &da );
103
109 QString currentField( bool *isExpression = nullptr, bool *isValid = nullptr ) const;
110
114 bool isValidExpression( QString *expressionError = nullptr ) const;
115
119 bool isExpression() const;
120
124 QString currentText() const;
125
131 QString asExpression() const;
132
141 QString expression() const;
142
147 QgsVectorLayer *layer() const;
148
155 void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
156
157#ifndef SIP_RUN
158
172 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
173#else
174
188 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
189 //%MethodCode
190 Py_XINCREF( a2 );
191 Py_BEGIN_ALLOW_THREADS
192 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
194 SIP_BLOCK_THREADS
195 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
196 int state;
197 int sipIsError = 0;
198 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
199 if ( sipIsError == 0 )
200 {
201 res = QgsExpressionContext( *t1 );
202 }
203 sipReleaseType( t1, sipType_QgsExpressionContext, state );
204 SIP_UNBLOCK_THREADS
205 return res;
206 } );
207
208 Py_END_ALLOW_THREADS
209 //%End
210#endif
211
217 bool allowEvalErrors() const;
218
224 void setAllowEvalErrors( bool allowEvalErrors );
225
233 bool buttonVisible() const;
234
242 void setButtonVisible( bool visible );
243
244 signals:
246 void fieldChanged( const QString &fieldName );
247
249 void fieldChanged( const QString &fieldName, bool isValid );
250
257
264
265 public slots:
266
271 void setLayer( QgsMapLayer *layer );
272
274 void setRow( int row ) { mCombo->setCurrentIndex( row ); }
275
277 void setField( const QString &fieldName );
278
283 void setFields( const QgsFields &fields );
284
290 void setExpression( const QString &expression );
291
292 protected slots:
294 void editExpression();
295
297 void expressionEdited( const QString &expression );
298
300 void expressionEditingFinished();
301
302 void currentFieldChanged();
303
309 void updateLineEditStyle( const QString &expression = QString() );
310
311 bool isExpressionValid( const QString &expressionStr );
312
313 protected:
314 void changeEvent( QEvent *event ) override;
315
316 bool eventFilter( QObject *watched, QEvent *event ) override;
317
318 private slots:
319 void reloadLayer();
320
321 void beforeResetModel();
322 void afterResetModel();
323
324 private:
325 QComboBox *mCombo = nullptr;
326 QToolButton *mButton = nullptr;
327 QgsFieldProxyModel *mFieldProxyModel = nullptr;
328 QString mExpressionDialogTitle;
329 std::shared_ptr<const QgsDistanceArea> mDistanceArea;
330 QgsExpressionContext mExpressionContext;
331 const QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
332 QString mBackupExpression;
333 bool mAllowEvalErrors = false;
334
335 QString mCustomPreviewLabel;
336 QList<QPair<QString, QVariant>> mCustomChoices;
337 std::function<QgsExpressionContext( const QVariant & )> mPreviewContextGenerator;
338
340};
341
342#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...
void setExpressionDialogTitle(const QString &title)
define the title used in the expression dialog
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is shown in the combo box.
QgsFieldExpressionWidget(QWidget *parent=nullptr)
QgsFieldExpressionWidget creates a widget with a combo box to display the fields and expression and a...
void setButtonVisible(bool visible)
Set the visibility of the button.
QgsFieldProxyModel::Filters filters() const
currently used filter on list of fields
QgsFieldProxyModel::Filters filters
void setRow(int row)
sets the current row in the widget
QgsVectorLayer * layer() const
Returns the layer currently associated with the widget.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the current expression context.
void setLayer(QgsMapLayer *layer)
Sets the layer used to display the fields and expression.
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 setAllowEvalErrors(bool allowEvalErrors)
Allow accepting expressions with evaluation errors.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows filtering according to the type of field
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:80
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