QGIS API Documentation 3.43.0-Master (c67cf405802)
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 commentLinesClicked();
415 void btnRun_pressed();
416 void btnNewFile_pressed();
417 void btnRemoveFile_pressed();
418
424 void exportUserExpressions_pressed();
425
431 void importUserExpressions_pressed();
432 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
433 void insertExpressionText( const QString &text );
434 void txtExpressionString_textChanged();
435 void txtSearchEditValues_textChanged();
436 void mValuesListView_doubleClicked( const QModelIndex &index );
437 void txtPython_textChanged();
438
439 signals:
440
447 void expressionParsed( bool isValid );
448
455
462
463 protected:
464 void showEvent( QShowEvent *e ) override;
465
466 private:
467 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
468 {
469 public:
470 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
471 : QgsExpressionTreeView::MenuProvider()
472 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
473
474 QMenu *createContextMenu( QgsExpressionItem *item ) override;
475
476 private:
477 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
478 };
479
480 int FUNCTION_MARKER_ID = 25;
481
482 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
483 void createMarkers( const QgsExpressionNode *node );
484 void clearFunctionMarkers();
485 void clearErrors();
486 void runPythonCode( const QString &code );
487 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
488 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
489 QString getFunctionHelp( QgsExpressionFunction *function );
490 QString loadFunctionHelp( QgsExpressionItem *functionName );
491 QString helpStylesheet() const;
492
493 // To be called whenever expression context has been updated
494 void expressionContextUpdated();
495
496 // Will hold items with
497 // * a display string that matches the represented field values
498 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
499 std::unique_ptr<QStandardItemModel> mValuesModel;
500 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
501
502 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
503
504 bool mAutoSave = true;
505 QString mFunctionsPath;
506 QgsVectorLayer *mLayer = nullptr;
507 bool mExpressionValid = false;
508 QgsExpressionContext mExpressionContext;
509 QPointer<QgsProject> mProject;
510
511 // Translated name of the user expressions group
512 QString mUserExpressionsGroupName;
513
514 QgsCodeEditorExpression *txtExpressionString = nullptr;
515};
516
517// clazy:excludeall=qstring-allocations
518
519#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