QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 <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
46class 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:
59 explicit QgsFieldExpressionWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
60
62 void setExpressionDialogTitle( const QString &title );
63
71 void appendScope( QgsExpressionContextScope *scope SIP_TRANSFER );
72
74 const QString expressionDialogTitle() { return mExpressionDialogTitle; }
75
77 void setFilters( QgsFieldProxyModel::Filters filters );
78
84 void setAllowEmptyFieldName( bool allowEmpty );
85
91 bool allowEmptyFieldName() const;
92
93 void setLeftHandButtonStyle( bool isLeft );
94
96 QgsFieldProxyModel::Filters filters() const { return mFieldProxyModel->filters(); }
97
99 void setGeomCalculator( const QgsDistanceArea &da );
100
106 QString currentField( bool *isExpression = nullptr, bool *isValid = nullptr ) const;
107
111 bool isValidExpression( QString *expressionError = nullptr ) const;
112
116 bool isExpression() const;
117
121 QString currentText() const;
122
128 QString asExpression() const;
129
138 QString expression() const;
139
144 QgsVectorLayer *layer() const;
145
152 void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
153
154#ifndef SIP_RUN
155
169 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
170#else
171
185 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
186 //%MethodCode
187 Py_XINCREF( a2 );
188 Py_BEGIN_ALLOW_THREADS
189 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
191 SIP_BLOCK_THREADS
192 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
193 int state;
194 int sipIsError = 0;
195 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
196 if ( sipIsError == 0 )
197 {
198 res = QgsExpressionContext( *t1 );
199 }
200 sipReleaseType( t1, sipType_QgsExpressionContext, state );
201 SIP_UNBLOCK_THREADS
202 return res;
203 } );
204
205 Py_END_ALLOW_THREADS
206 //%End
207#endif
208
214 bool allowEvalErrors() const;
215
221 void setAllowEvalErrors( bool allowEvalErrors );
222
230 bool buttonVisible() const;
231
239 void setButtonVisible( bool visible );
240
241 signals:
243 void fieldChanged( const QString &fieldName );
244
246 void fieldChanged( const QString &fieldName, bool isValid );
247
254
261
262 public slots:
263
268 void setLayer( QgsMapLayer *layer );
269
271 void setRow( int row ) { mCombo->setCurrentIndex( row ); }
272
274 void setField( const QString &fieldName );
275
280 void setFields( const QgsFields &fields );
281
287 void setExpression( const QString &expression );
288
289 protected slots:
291 void editExpression();
292
294 void expressionEdited( const QString &expression );
295
297 void expressionEditingFinished();
298
299 void currentFieldChanged();
300
306 void updateLineEditStyle( const QString &expression = QString() );
307
308 bool isExpressionValid( const QString &expressionStr );
309
310 protected:
311 void changeEvent( QEvent *event ) override;
312
313 bool eventFilter( QObject *watched, QEvent *event ) override;
314
315 private slots:
316 void reloadLayer();
317
318 void beforeResetModel();
319 void afterResetModel();
320
321 private:
322 QComboBox *mCombo = nullptr;
323 QToolButton *mButton = nullptr;
324 QgsFieldProxyModel *mFieldProxyModel = nullptr;
325 QString mExpressionDialogTitle;
326 std::shared_ptr<const QgsDistanceArea> mDistanceArea;
327 QgsExpressionContext mExpressionContext;
328 const QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
329 QString mBackupExpression;
330 bool mAllowEvalErrors = false;
331
332 QString mCustomPreviewLabel;
333 QList<QPair<QString, QVariant>> mCustomChoices;
334 std::function<QgsExpressionContext( const QVariant & )> mPreviewContextGenerator;
335
336 friend class TestQgsFieldExpressionWidget;
337};
338
339#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...
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:46
Base class for all map layer types.
Definition qgsmaplayer.h:76
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