QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 "ui_qgsexpressionbuilder.h"
20
21#include "qgis_gui.h"
22#include "qgis_sip.h"
23#include "qgsexpression.h"
26
27#include <QSortFilterProxyModel>
28#include <QStandardItemModel>
29#include <QWidget>
30
31class QgsFields;
33class QgsRelation;
35
36#ifndef SIP_RUN
37static const QString DEFAULT_PROJECT_FUNCTIONS_ITEM_NAME = QStringLiteral( "[Project Functions]" );
38#endif
39
46class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
47{
48 Q_OBJECT
49 public:
61 Q_DECLARE_FLAGS( Flags, Flag )
62 Q_FLAG( Flag )
63
64
65
68 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
70
75 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
76
81 void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
82
87 void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
88
94
98 QgsVectorLayer *layer() const;
99
101 Q_DECL_DEPRECATED void loadFieldNames() SIP_DEPRECATED {}
102
104 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) SIP_DEPRECATED { mExpressionTreeView->loadFieldNames( fields ); }
105
110 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
111
113 void setGeomCalculator( const QgsDistanceArea &da );
114
119 QString expressionText();
120
122 void setExpressionText( const QString &expression );
123
129 QString expectedOutputFormat();
130
137 void setExpectedOutputFormat( const QString &expected );
138
144 QgsExpressionContext expressionContext() const { return mExpressionContext; }
145
152 void setExpressionContext( const QgsExpressionContext &context );
153
155 bool isExpressionValid();
156
157#ifndef SIP_RUN
158
172 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
173#else
174
188 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
189 //%MethodCode
190 Py_XINCREF( a2 );
191 Py_BEGIN_ALLOW_THREADS
192 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
194 SIP_BLOCK_THREADS
195 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
196 int state;
197 int sipIsError = 0;
198 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
199 if ( sipIsError == 0 )
200 {
201 res = QgsExpressionContext( *t1 );
202 }
203 sipReleaseType( t1, sipType_QgsExpressionContext, state );
204 SIP_UNBLOCK_THREADS
205 return res;
206 } );
207
208 Py_END_ALLOW_THREADS
209 //%End
210#endif
211
212
218 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
219
225 Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) ) SIP_DEPRECATED;
226
231 QgsExpressionTreeView *expressionTree() const;
232
238 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
239
245 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
246
252 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
253
257 void newFunctionFile( const QString &fileName = "scratch" );
258
262 void saveFunctionFile( QString fileName );
263
267 void loadCodeFromFile( QString path );
268
274 void loadCodeFromProjectFunctions();
275
279 void loadFunctionCode( const QString &code );
280
284 void updateFunctionFileList( const QString &path );
285
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
373 void autosave();
374
381 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
382
387 void storeCurrentUserExpression();
388
394 void removeSelectedUserExpression();
395
401 void editSelectedUserExpression();
402
408 const QList<QgsExpressionItem *> findExpressions( const QString &label );
409
410
411 private slots:
412 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
413 void onExpressionParsed( bool state );
414 void expressionTreeItemChanged( QgsExpressionItem *item );
415 void operatorButtonClicked();
416 void commentLinesClicked();
417 void btnRun_pressed();
418 void btnNewFile_pressed();
419 void btnRemoveFile_pressed();
420
426 void exportUserExpressions_pressed();
427
433 void importUserExpressions_pressed();
434 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
435 void insertExpressionText( const QString &text );
436 void txtExpressionString_textChanged();
437 void txtSearchEditValues_textChanged();
438 void mValuesListView_doubleClicked( const QModelIndex &index );
439 void txtPython_textChanged();
440
441 signals:
442
449 void expressionParsed( bool isValid );
450
457
464
465 protected:
466 void showEvent( QShowEvent *e ) override;
467
468 private:
469 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
470 {
471 public:
472 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
473 : QgsExpressionTreeView::MenuProvider()
474 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
475
476 QMenu *createContextMenu( QgsExpressionItem *item ) override;
477
478 private:
479 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
480 };
481
482 int FUNCTION_MARKER_ID = 25;
483
484 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
485 void createMarkers( const QgsExpressionNode *node );
486 void clearFunctionMarkers();
487 void clearErrors();
488 void runPythonCode( const QString &code );
489 void displayTemporaryLabel( const QString &text );
490 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
491 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
492 QString getFunctionHelp( QgsExpressionFunction *function );
493 QString loadFunctionHelp( QgsExpressionItem *functionName );
494 QString helpStylesheet() const;
495
496 // To be called whenever expression context has been updated
497 void expressionContextUpdated();
498
499 // Will hold items with
500 // * a display string that matches the represented field values
501 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
502 std::unique_ptr<QStandardItemModel> mValuesModel;
503 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
504
505 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
506
507 bool mAutoSave = true;
508 QString mFunctionsPath;
509 QgsVectorLayer *mLayer = nullptr;
510 bool mExpressionValid = false;
511 QgsExpressionContext mExpressionContext;
512 QPointer<QgsProject> mProject;
513
514 // Translated name of the user expressions group
515 QString mUserExpressionsGroupName;
516
517 QgsCodeEditorExpression *txtExpressionString = nullptr;
518};
519
520// clazy:excludeall=qstring-allocations
521
522#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.
@ LoadRecent
Load recent expressions given the collection key.
@ LoadUserExpressions
Load user expressions.
void parserErrorChanged()
Will be set to true if the current expression text reported a parser error with the context.
void init(const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=QStringLiteral("generic"), QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize without any layer.
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 setAutoSave(bool enabled)
Enabled or disable auto saving.
void expressionParsed(bool isValid)
Emitted when the user changes the expression in the widget.
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
void initWithLayer(QgsVectorLayer *layer, const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=QStringLiteral("generic"), QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize with a layer.
void showEvent(QShowEvent *e) override
void initWithFields(const QgsFields &fields, const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=QStringLiteral("generic"), QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize with given fields without any layer.
QgsVectorLayer * layer() const
Returns the current layer or a nullptr.
QgsExpressionBuilderWidget(QWidget *parent=nullptr)
Create a new expression builder widget with an optional parent.
void setLayer(QgsVectorLayer *layer)
Sets layer in order to get the fields and values.
Q_DECL_DEPRECATED void loadFieldNames()
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Syntax highlighter for QGIS expressions.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
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
Represents a relationship between two vector layers.
Definition qgsrelation.h:42
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275