QGIS API Documentation 3.43.0-Master (b60ef06885e)
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;
36
37#ifndef SIP_RUN
38static const QString DEFAULT_PROJECT_FUNCTIONS_ITEM_NAME = QStringLiteral( "[Project Functions]" );
39#endif
40
47class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
48{
49 Q_OBJECT
50 public:
55 enum Flag SIP_ENUM_BASETYPE( IntFlag )
56 {
57 LoadNothing = 0,
58 LoadRecent = 1 << 1,
59 LoadUserExpressions = 1 << 2,
60 LoadAll = LoadRecent | LoadUserExpressions,
61 };
62 Q_DECLARE_FLAGS( Flags, Flag )
63 Q_FLAG( Flag )
64
65
66
69 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
71
76 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
77
82 void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
83
88 void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
89
94 void setLayer( QgsVectorLayer *layer );
95
99 QgsVectorLayer *layer() const;
100
102 Q_DECL_DEPRECATED void loadFieldNames() SIP_DEPRECATED {}
103
105 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) SIP_DEPRECATED { mExpressionTreeView->loadFieldNames( fields ); }
106
111 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
112
114 void setGeomCalculator( const QgsDistanceArea &da );
115
120 QString expressionText();
121
123 void setExpressionText( const QString &expression );
124
130 QString expectedOutputFormat();
131
138 void setExpectedOutputFormat( const QString &expected );
139
145 QgsExpressionContext expressionContext() const { return mExpressionContext; }
146
153 void setExpressionContext( const QgsExpressionContext &context );
154
156 bool isExpressionValid();
157
158#ifndef SIP_RUN
159
173 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
174#else
175
189 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
190 //%MethodCode
191 Py_XINCREF( a2 );
192 Py_BEGIN_ALLOW_THREADS
193 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
195 SIP_BLOCK_THREADS
196 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
197 int state;
198 int sipIsError = 0;
199 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
200 if ( sipIsError == 0 )
201 {
202 res = QgsExpressionContext( *t1 );
203 }
204 sipReleaseType( t1, sipType_QgsExpressionContext, state );
205 SIP_UNBLOCK_THREADS
206 return res;
207 } );
208
209 Py_END_ALLOW_THREADS
210 //%End
211#endif
212
213
219 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
220
226 Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) ) SIP_DEPRECATED;
227
232 QgsExpressionTreeView *expressionTree() const;
233
239 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
240
246 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
247
253 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
254
258 void newFunctionFile( const QString &fileName = "scratch" );
259
263 void saveFunctionFile( QString fileName );
264
268 void loadCodeFromFile( QString path );
269
275 void loadCodeFromProjectFunctions();
276
280 void loadFunctionCode( const QString &code );
281
285 void updateFunctionFileList( const QString &path );
286
292 void saveProjectFunctionsEntry();
293
299 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
300
305 QgsProject *project();
306
312 void setProject( QgsProject *project );
313
319 bool evalError() const;
320
326 bool parserError() const;
327
333 void setExpressionPreviewVisible( bool isVisible );
334
335 public slots:
336
342 void loadSampleValues();
343
349 void loadAllValues();
350
358 void loadSampleUsedValues();
359
367 void loadAllUsedValues();
368
372 void autosave();
373
379 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
380
385 void storeCurrentUserExpression();
386
392 void removeSelectedUserExpression();
393
399 void editSelectedUserExpression();
400
406 const QList<QgsExpressionItem *> findExpressions( const QString &label );
407
408
409 private slots:
410 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
411 void onExpressionParsed( bool state );
412 void expressionTreeItemChanged( QgsExpressionItem *item );
413 void operatorButtonClicked();
414 void btnRun_pressed();
415 void btnNewFile_pressed();
416 void btnRemoveFile_pressed();
417
423 void exportUserExpressions_pressed();
424
430 void importUserExpressions_pressed();
431 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
432 void insertExpressionText( const QString &text );
433 void txtExpressionString_textChanged();
434 void txtSearchEditValues_textChanged();
435 void mValuesListView_doubleClicked( const QModelIndex &index );
436 void txtPython_textChanged();
437
438 signals:
439
446 void expressionParsed( bool isValid );
447
454
461
462 protected:
463 void showEvent( QShowEvent *e ) override;
464
465 private:
466 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
467 {
468 public:
469 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
470 : QgsExpressionTreeView::MenuProvider()
471 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
472
473 QMenu *createContextMenu( QgsExpressionItem *item ) override;
474
475 private:
476 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
477 };
478
479 int FUNCTION_MARKER_ID = 25;
480
481 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
482 void createMarkers( const QgsExpressionNode *node );
483 void clearFunctionMarkers();
484 void clearErrors();
485 void runPythonCode( const QString &code );
486 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
487 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
488 QString getFunctionHelp( QgsExpressionFunction *function );
489 QString loadFunctionHelp( QgsExpressionItem *functionName );
490 QString helpStylesheet() const;
491
492 // To be called whenever expression context has been updated
493 void expressionContextUpdated();
494
495 // Will hold items with
496 // * a display string that matches the represented field values
497 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
498 std::unique_ptr<QStandardItemModel> mValuesModel;
499 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
500
501 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
502
503 bool mAutoSave = true;
504 QString mFunctionsPath;
505 QgsVectorLayer *mLayer = nullptr;
506 bool mExpressionValid = false;
507 QgsExpressionContext mExpressionContext;
508 QPointer<QgsProject> mProject;
509
510 // Translated name of the user expressions group
511 QString mUserExpressionsGroupName;
512
513 QgsCodeEditorExpression *txtExpressionString = nullptr;
514};
515
516// clazy:excludeall=qstring-allocations
517
518#endif // QGSEXPRESSIONBUILDER_H
A QGIS expression editor based on QScintilla2.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
A reusable widget that can be used to build an 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...
An abstract base class for defining QgsExpression functions.
Syntax highlighter for QGIS expressions.
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...
A tree view to list all expressions functions, variables and fields that can be used in an expression...
Container of fields for a vector layer.
Definition qgsfields.h:46
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
Represents a vector layer which manages a vector based dataset.
#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