QGIS API Documentation 3.43.0-Master (0bee5d6404c)
qgsexpressionpreviewwidget.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressionpreviewwidget.h
3 --------------------------------------
4 Date : march 2020 - quarantine day 12
5 Copyright : (C) 2020 by Denis Rouzaud
6 Email : denis@opengis.ch
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 QGSEXPRESSIONPREVIEWWIDGET_H
17#define QGSEXPRESSIONPREVIEWWIDGET_H
18
19#include <QWidget>
20
21#include "ui_qgsexpressionpreviewbase.h"
22
23#include "qgis_gui.h"
24#include "qgsdistancearea.h"
25#include "qgsexpression.h"
27
28#include <functional>
29
30class QAction;
31class QgsVectorLayer;
32
41class GUI_EXPORT QgsExpressionPreviewWidget : public QWidget, private Ui::QgsExpressionPreviewWidgetBase
42{
43 Q_OBJECT
44 public:
46 explicit QgsExpressionPreviewWidget( QWidget *parent = nullptr );
47
49 void setLayer( QgsVectorLayer *layer );
50
51#ifndef SIP_RUN
52
66 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
67#else
68
82 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
83 //%MethodCode
84 Py_XINCREF( a2 );
85 Py_BEGIN_ALLOW_THREADS
86 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
88 SIP_BLOCK_THREADS
89 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
90 int state;
91 int sipIsError = 0;
92 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
93 if ( sipIsError == 0 )
94 {
95 res = QgsExpressionContext( *t1 );
96 }
97 sipReleaseType( t1, sipType_QgsExpressionContext, state );
98 SIP_UNBLOCK_THREADS
99 return res;
100 } );
101
102 Py_END_ALLOW_THREADS
103 //%End
104#endif
105
107 void setExpressionText( const QString &expression );
108
114 QgsExpressionContext expressionContext() const { return mExpressionContext; }
115
121 void setExpressionContext( const QgsExpressionContext &context );
122
124 void setGeomCalculator( const QgsDistanceArea &da );
125
130 bool evalError() const;
131
136 bool parserError() const;
137
139 const QgsExpressionNode *rootNode() const { return mExpression.rootNode(); }
140
142 QList<QgsExpression::ParserError> parserErrors() const { return mExpression.parserErrors(); }
143
149 QString currentPreviewText() const;
150
151 signals:
152
159 void expressionParsed( bool isValid );
160
166
172
174 void toolTipChanged( const QString &toolTip );
175
176 public slots:
178 void setCurrentFeature( const QgsFeature &feature );
179
180
181 private slots:
182 void linkActivated( const QString & );
183 void setEvalError( bool evalError );
184 void setParserError( bool parserError );
185 void copyFullExpressionValue();
186 void setCustomChoice( int );
187
188 private:
189 void setExpressionToolTip( const QString &toolTip );
190 void refreshPreview();
191
192 QgsVectorLayer *mLayer = nullptr;
193 QgsExpressionContext mExpressionContext;
194 QgsDistanceArea mDa;
195 bool mUseGeomCalculator = false;
196 QString mToolTip;
197 bool mEvalError = true;
198 bool mParserError = true;
199 QString mExpressionText;
200 QgsExpression mExpression;
201 QAction *mCopyPreviewAction = nullptr;
202
203 std::function<QgsExpressionContext( const QVariant & )> mCustomPreviewGeneratorFunction;
204};
205
206#endif // QGSEXPRESSIONPREVIEWWIDGET_H
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for all nodes that can appear in an expression.
A widget for previewing an expression result.
const QgsExpressionNode * rootNode() const
Returns the root node of the expression.
void parserErrorChanged()
Will be set to true if the current expression text reported a parser error with the context.
QList< QgsExpression::ParserError > parserErrors() const
Returns the expression parser errors.
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
void evalErrorChanged()
Will be set to true if the current expression text reported an eval error with the context.
void toolTipChanged(const QString &toolTip)
Emitted whenever the tool tip changed.
void expressionParsed(bool isValid)
Emitted when the user changes the expression in the widget.
Handles parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Represents a vector layer which manages a vector based dataset.