QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsexpressionbuilderwidget.h
Go to the documentation of this file.
1/***************************************************************************
2 qgisexpressionbuilderwidget.h - A generic expression builder widget.
3 --------------------------------------
4 Date : 29-May-2011
5 Copyright : (C) 2011 by Nathan Woodrow
6 Email : woodrow.nathan at gmail dot 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 QGSEXPRESSIONBUILDER_H
17#define QGSEXPRESSIONBUILDER_H
18
19#include <QWidget>
20#include <QStandardItemModel>
21#include <QSortFilterProxyModel>
22
23#include "ui_qgsexpressionbuilder.h"
24
25#include "qgis_sip.h"
26#include "qgis_gui.h"
28#include "qgsexpression.h"
30
31
32class QgsFields;
34class QgsRelation;
35
36
42class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
43{
44 Q_OBJECT
45 public:
46
51 enum Flag SIP_ENUM_BASETYPE( IntFlag )
52 {
53 LoadNothing = 0,
54 LoadRecent = 1 << 1,
55 LoadUserExpressions = 1 << 2,
56 LoadAll = LoadRecent | LoadUserExpressions,
57 };
58 Q_DECLARE_FLAGS( Flags, Flag )
59 Q_FLAG( Flag )
60
61
62
65 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
67
72 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
73
78 void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
79
84 void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
85
90 void setLayer( QgsVectorLayer *layer );
91
95 QgsVectorLayer *layer() const;
96
98 Q_DECL_DEPRECATED void loadFieldNames() {} SIP_DEPRECATED
99
101 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) {mExpressionTreeView->loadFieldNames( fields );} SIP_DEPRECATED
102
107 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
108
110 void setGeomCalculator( const QgsDistanceArea &da );
111
116 QString expressionText();
117
119 void setExpressionText( const QString &expression );
120
126 QString expectedOutputFormat();
127
134 void setExpectedOutputFormat( const QString &expected );
135
141 QgsExpressionContext expressionContext() const { return mExpressionContext; }
142
149 void setExpressionContext( const QgsExpressionContext &context );
150
152 bool isExpressionValid();
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
190 {
192 SIP_BLOCK_THREADS
193 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
194 int state;
195 int sipIsError = 0;
196 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
197 if ( sipIsError == 0 )
198 {
199 res = QgsExpressionContext( *t1 );
200 }
201 sipReleaseType( t1, sipType_QgsExpressionContext, state );
202 SIP_UNBLOCK_THREADS
203 return res;
204 } );
205
206 Py_END_ALLOW_THREADS
207 % End
208#endif
209
210
216 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
217
223 Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) )SIP_DEPRECATED ;
224
229 QgsExpressionTreeView *expressionTree() const;
230
236 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
237
243 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
244
250 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
251
255 void newFunctionFile( const QString &fileName = "scratch" );
256
260 void saveFunctionFile( QString fileName );
261
265 void loadCodeFromFile( QString path );
266
270 void loadFunctionCode( const QString &code );
271
275 void updateFunctionFileList( const QString &path );
276
282 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
283
288 QgsProject *project();
289
295 void setProject( QgsProject *project );
296
302 bool evalError() const;
303
309 bool parserError() const;
310
316 void setExpressionPreviewVisible( bool isVisible );
317
318 public slots:
319
325 void loadSampleValues();
326
332 void loadAllValues();
333
341 void loadSampleUsedValues();
342
350 void loadAllUsedValues();
351
355 void autosave();
356
362 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
363
368 void storeCurrentUserExpression( );
369
375 void removeSelectedUserExpression( );
376
382 void editSelectedUserExpression();
383
389 const QList<QgsExpressionItem *> findExpressions( const QString &label );
390
391
392 private slots:
393 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
394 void onExpressionParsed( bool state );
395 void expressionTreeItemChanged( QgsExpressionItem *item );
396 void operatorButtonClicked();
397 void btnRun_pressed();
398 void btnNewFile_pressed();
399 void btnRemoveFile_pressed();
400
406 void exportUserExpressions_pressed();
407
413 void importUserExpressions_pressed();
414 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
415 void insertExpressionText( const QString &text );
416 void txtExpressionString_textChanged();
417 void txtSearchEditValues_textChanged();
418 void mValuesListView_doubleClicked( const QModelIndex &index );
419 void txtPython_textChanged();
420
421 signals:
422
429 void expressionParsed( bool isValid );
430
437
444
445 protected:
446 void showEvent( QShowEvent *e ) override;
447
448 private:
449 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
450 {
451 public:
452 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
453 : QgsExpressionTreeView::MenuProvider()
454 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
455
456 QMenu *createContextMenu( QgsExpressionItem *item ) override;
457
458 private:
459 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
460 };
461
462 int FUNCTION_MARKER_ID = 25;
463
464 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
465 void createMarkers( const QgsExpressionNode *node );
466 void clearFunctionMarkers();
467 void clearErrors();
468 void runPythonCode( const QString &code );
469 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
470 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
471 QString getFunctionHelp( QgsExpressionFunction *function );
472 QString loadFunctionHelp( QgsExpressionItem *functionName );
473 QString helpStylesheet() const;
474
475 // To be called whenever expression context has been updated
476 void expressionContextUpdated();
477
478 // Will hold items with
479 // * a display string that matches the represented field values
480 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
481 std::unique_ptr<QStandardItemModel> mValuesModel;
482 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
483
484 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
485
486 bool mAutoSave = true;
487 QString mFunctionsPath;
488 QgsVectorLayer *mLayer = nullptr;
489 QgsExpressionHighlighter *highlighter = nullptr;
490 bool mExpressionValid = false;
491 QgsExpressionContext mExpressionContext;
492 QPointer< QgsProject > mProject;
493
494 // Translated name of the user expressions group
495 QString mUserExpressionsGroupName;
496};
497
498// clazy:excludeall=qstring-allocations
499
500#endif // QGSEXPRESSIONBUILDER_H
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
A reusable widget that can be used to build a expression string.
Flag
Flag to determine what should be loaded.
void parserErrorChanged()
Will be set to true if the current expression text reported a parser error with the context.
void evalErrorChanged()
Will be set to true if the current expression text reported an eval error with the context.
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
void expressionParsed(bool isValid)
Emitted when the user changes the expression in the widget.
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
Q_DECL_DEPRECATED void loadFieldNames()
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A abstract base class for defining QgsExpression functions.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
Abstract base class for all nodes that can appear in an expression.
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Container of fields for a vector layer.
Definition: qgsfields.h:45
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:107
Represents a vector layer which manages a vector based data sets.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition: qgis_sip.h:278