QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
30#include <QWidget>
31
32using namespace Qt::StringLiterals;
33
34class QgsFields;
36class QgsRelation;
38
39#ifndef SIP_RUN
40static const QString DEFAULT_PROJECT_FUNCTIONS_ITEM_NAME = u"[Project Functions]"_s;
41#endif
42
49class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
50{
51 Q_OBJECT
52 public:
64 Q_DECLARE_FLAGS( Flags, Flag )
65 Q_FLAG( Flag )
66
67
68
71 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
73
78 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = u"generic"_s, QgsExpressionBuilderWidget::Flags flags = LoadAll );
79
84 void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = u"generic"_s, QgsExpressionBuilderWidget::Flags flags = LoadAll );
85
90 void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = u"generic"_s, QgsExpressionBuilderWidget::Flags flags = LoadAll );
91
97
101 QgsVectorLayer *layer() const;
102
104 Q_DECL_DEPRECATED void loadFieldNames() SIP_DEPRECATED {}
105
107 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) SIP_DEPRECATED { mExpressionTreeView->loadFieldNames( fields ); }
108
113 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
114
116 void setGeomCalculator( const QgsDistanceArea &da );
117
122 QString expressionText();
123
125 void setExpressionText( const QString &expression );
126
132 QString expectedOutputFormat();
133
140 void setExpectedOutputFormat( const QString &expected );
141
147 QgsExpressionContext expressionContext() const { return mExpressionContext; }
148
155 void setExpressionContext( const QgsExpressionContext &context );
156
158 bool isExpressionValid();
159
160#ifndef SIP_RUN
161
175 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
176#else
177
191 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
192 //%MethodCode
193 Py_XINCREF( a2 );
194 Py_BEGIN_ALLOW_THREADS
195 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
197 SIP_BLOCK_THREADS
198 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
199 int state;
200 int sipIsError = 0;
201 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
202 if ( sipIsError == 0 )
203 {
204 res = QgsExpressionContext( *t1 );
205 }
206 sipReleaseType( t1, sipType_QgsExpressionContext, state );
207 SIP_UNBLOCK_THREADS
208 return res;
209 } );
210
211 Py_END_ALLOW_THREADS
212 //%End
213#endif
214
215
221 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
222
228 Q_DECL_DEPRECATED void loadRecent( const QString &collection = u"generic"_s ) SIP_DEPRECATED;
229
234 QgsExpressionTreeView *expressionTree() const;
235
241 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
242
248 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
249
255 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
256
260 void newFunctionFile( const QString &fileName = "scratch" );
261
265 void saveFunctionFile( QString fileName );
266
270 void loadCodeFromFile( QString path );
271
277 void loadCodeFromProjectFunctions();
278
282 void loadFunctionCode( const QString &code );
283
287 void updateFunctionFileList( const QString &path );
288
295 void saveProjectFunctionsEntry();
296
302 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
303
308 QgsProject *project();
309
315 void setProject( QgsProject *project );
316
322 bool evalError() const;
323
329 bool parserError() const;
330
336 void setExpressionPreviewVisible( bool isVisible );
337
338 public slots:
339
345 void loadSampleValues();
346
352 void loadAllValues();
353
361 void loadSampleUsedValues();
362
370 void loadAllUsedValues();
371
376 void autosave();
377
384 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
385
390 void storeCurrentUserExpression();
391
397 void removeSelectedUserExpression();
398
404 void editSelectedUserExpression();
405
411 const QList<QgsExpressionItem *> findExpressions( const QString &label );
412
413
414 private slots:
415 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
416 void onExpressionParsed( bool state );
417 void expressionTreeItemChanged( QgsExpressionItem *item );
418 void operatorButtonClicked();
419 void commentLinesClicked();
420 void btnRun_pressed();
421 void btnNewFile_pressed();
422 void btnRemoveFile_pressed();
423
429 void exportUserExpressions_pressed();
430
436 void importUserExpressions_pressed();
437 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
438 void insertExpressionText( const QString &text );
439 void txtExpressionString_textChanged();
440 void txtSearchEditValues_textChanged();
441 void mValuesListView_doubleClicked( const QModelIndex &index );
442 void txtPython_textChanged();
443
444 signals:
445
452 void expressionParsed( bool isValid );
453
460
467
468 protected:
469 void showEvent( QShowEvent *e ) override;
470
471 private:
472 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
473 {
474 public:
475 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
476 : QgsExpressionTreeView::MenuProvider()
477 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
478
479 QMenu *createContextMenu( QgsExpressionItem *item ) override;
480
481 private:
482 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
483 };
484
485 int FUNCTION_MARKER_ID = 25;
486
487 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
488 void createMarkers( const QgsExpressionNode *node );
489 void clearFunctionMarkers();
490 void clearErrors();
491 void runPythonCode( const QString &code );
492 void displayTemporaryLabel( const QString &text );
493 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
494 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
495 QString getFunctionHelp( QgsExpressionFunction *function );
496 QString loadFunctionHelp( QgsExpressionItem *functionName );
497 QString helpStylesheet() const;
498
499 // To be called whenever expression context has been updated
500 void expressionContextUpdated();
501
502 // Will hold items with
503 // * a display string that matches the represented field values
504 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
505 std::unique_ptr<QStandardItemModel> mValuesModel;
506 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
507
508 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
509
510 bool mAutoSave = true;
511 QString mFunctionsPath;
512 QgsVectorLayer *mLayer = nullptr;
513 bool mExpressionValid = false;
514 QgsExpressionContext mExpressionContext;
515 QPointer<QgsProject> mProject;
516
517 // Translated name of the user expressions group
518 QString mUserExpressionsGroupName;
519
520 QgsCodeEditorExpression *txtExpressionString = nullptr;
521};
522
523// clazy:excludeall=qstring-allocations
524
525#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 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.
void initWithLayer(QgsVectorLayer *layer, const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=u"generic"_s, QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize with a layer.
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
void showEvent(QShowEvent *e) override
void init(const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=u"generic"_s, QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize 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()
void initWithFields(const QgsFields &fields, const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=u"generic"_s, QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize with given fields without any layer.
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