QGIS API Documentation 3.99.0-Master (26c88405ac0)
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:
50
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
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
293 void saveProjectFunctionsEntry();
294
300 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
301
306 QgsProject *project();
307
313 void setProject( QgsProject *project );
314
320 bool evalError() const;
321
327 bool parserError() const;
328
334 void setExpressionPreviewVisible( bool isVisible );
335
336 public slots:
337
343 void loadSampleValues();
344
350 void loadAllValues();
351
359 void loadSampleUsedValues();
360
368 void loadAllUsedValues();
369
374 void autosave();
375
382 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
383
388 void storeCurrentUserExpression();
389
395 void removeSelectedUserExpression();
396
402 void editSelectedUserExpression();
403
409 const QList<QgsExpressionItem *> findExpressions( const QString &label );
410
411
412 private slots:
413 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
414 void onExpressionParsed( bool state );
415 void expressionTreeItemChanged( QgsExpressionItem *item );
416 void operatorButtonClicked();
417 void commentLinesClicked();
418 void btnRun_pressed();
419 void btnNewFile_pressed();
420 void btnRemoveFile_pressed();
421
427 void exportUserExpressions_pressed();
428
434 void importUserExpressions_pressed();
435 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
436 void insertExpressionText( const QString &text );
437 void txtExpressionString_textChanged();
438 void txtSearchEditValues_textChanged();
439 void mValuesListView_doubleClicked( const QModelIndex &index );
440 void txtPython_textChanged();
441
442 signals:
443
450 void expressionParsed( bool isValid );
451
458
465
466 protected:
467 void showEvent( QShowEvent *e ) override;
468
469 private:
470 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
471 {
472 public:
473 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
474 : QgsExpressionTreeView::MenuProvider()
475 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
476
477 QMenu *createContextMenu( QgsExpressionItem *item ) override;
478
479 private:
480 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
481 };
482
483 int FUNCTION_MARKER_ID = 25;
484
485 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
486 void createMarkers( const QgsExpressionNode *node );
487 void clearFunctionMarkers();
488 void clearErrors();
489 void runPythonCode( const QString &code );
490 void displayTemporaryLabel( const QString &text );
491 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
492 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
493 QString getFunctionHelp( QgsExpressionFunction *function );
494 QString loadFunctionHelp( QgsExpressionItem *functionName );
495 QString helpStylesheet() const;
496
497 // To be called whenever expression context has been updated
498 void expressionContextUpdated();
499
500 // Will hold items with
501 // * a display string that matches the represented field values
502 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
503 std::unique_ptr<QStandardItemModel> mValuesModel;
504 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
505
506 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
507
508 bool mAutoSave = true;
509 QString mFunctionsPath;
510 QgsVectorLayer *mLayer = nullptr;
511 bool mExpressionValid = false;
512 QgsExpressionContext mExpressionContext;
513 QPointer<QgsProject> mProject;
514
515 // Translated name of the user expressions group
516 QString mUserExpressionsGroupName;
517
518 QgsCodeEditorExpression *txtExpressionString = nullptr;
519};
520
521// clazy:excludeall=qstring-allocations
522
523#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