QGIS API Documentation 3.37.0-Master (fa0c88a90b0)
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;
35
36
42class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
43{
44 Q_OBJECT
45 public:
46
51 enum Flag SIP_ENUM_BASETYPE( IntFlag )
52 {
53 LoadNothing = 0,
54 LoadRecent = 1 << 1,
55 LoadUserExpressions = 1 << 2,
56 LoadAll = LoadRecent | LoadUserExpressions,
57 };
58 Q_DECLARE_FLAGS( Flags, Flag )
59 Q_FLAG( Flag )
60
61
62
65 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
67
72 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
73
78 void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
79
84 void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
85
90 void setLayer( QgsVectorLayer *layer );
91
95 QgsVectorLayer *layer() const;
96
98 Q_DECL_DEPRECATED void loadFieldNames() {} SIP_DEPRECATED
99
101 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) {mExpressionTreeView->loadFieldNames( fields );} SIP_DEPRECATED
102
107 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
108
110 void setGeomCalculator( const QgsDistanceArea &da );
111
116 QString expressionText();
117
119 void setExpressionText( const QString &expression );
120
126 QString expectedOutputFormat();
127
134 void setExpectedOutputFormat( const QString &expected );
135
141 QgsExpressionContext expressionContext() const { return mExpressionContext; }
142
149 void setExpressionContext( const QgsExpressionContext &context );
150
152 bool isExpressionValid();
153
159 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
160
166 Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) )SIP_DEPRECATED ;
167
172 QgsExpressionTreeView *expressionTree() const;
173
179 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
180
186 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
187
193 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
194
198 void newFunctionFile( const QString &fileName = "scratch" );
199
203 void saveFunctionFile( QString fileName );
204
208 void loadCodeFromFile( QString path );
209
213 void loadFunctionCode( const QString &code );
214
218 void updateFunctionFileList( const QString &path );
219
225 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
226
231 QgsProject *project();
232
238 void setProject( QgsProject *project );
239
245 bool evalError() const;
246
252 bool parserError() const;
253
259 void setExpressionPreviewVisible( bool isVisible );
260
261 public slots:
262
268 void loadSampleValues();
269
275 void loadAllValues();
276
284 void loadSampleUsedValues();
285
293 void loadAllUsedValues();
294
298 void autosave();
299
305 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
306
311 void storeCurrentUserExpression( );
312
318 void removeSelectedUserExpression( );
319
325 void editSelectedUserExpression();
326
332 const QList<QgsExpressionItem *> findExpressions( const QString &label );
333
334
335 private slots:
336 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
337 void onExpressionParsed( bool state );
338 void expressionTreeItemChanged( QgsExpressionItem *item );
339 void operatorButtonClicked();
340 void btnRun_pressed();
341 void btnNewFile_pressed();
342 void btnRemoveFile_pressed();
343
349 void exportUserExpressions_pressed();
350
356 void importUserExpressions_pressed();
357 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
358 void insertExpressionText( const QString &text );
359 void txtExpressionString_textChanged();
360 void txtSearchEditValues_textChanged();
361 void mValuesListView_doubleClicked( const QModelIndex &index );
362 void txtPython_textChanged();
363
364 signals:
365
372 void expressionParsed( bool isValid );
373
380
387
388 protected:
389 void showEvent( QShowEvent *e ) override;
390
391 private:
392 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
393 {
394 public:
395 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
396 : QgsExpressionTreeView::MenuProvider()
397 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
398
399 QMenu *createContextMenu( QgsExpressionItem *item ) override;
400
401 private:
402 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
403 };
404
405 int FUNCTION_MARKER_ID = 25;
406
407 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
408 void createMarkers( const QgsExpressionNode *node );
409 void clearFunctionMarkers();
410 void clearErrors();
411 void runPythonCode( const QString &code );
412 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
413 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
414 QString getFunctionHelp( QgsExpressionFunction *function );
415 QString loadFunctionHelp( QgsExpressionItem *functionName );
416 QString helpStylesheet() const;
417
418 // To be called whenever expression context has been updated
419 void expressionContextUpdated();
420
421 // Will hold items with
422 // * a display string that matches the represented field values
423 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
424 std::unique_ptr<QStandardItemModel> mValuesModel;
425 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
426
427 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
428
429 bool mAutoSave = true;
430 QString mFunctionsPath;
431 QgsVectorLayer *mLayer = nullptr;
432 QgsExpressionHighlighter *highlighter = nullptr;
433 bool mExpressionValid = false;
434 QgsExpressionContext mExpressionContext;
435 QPointer< QgsProject > mProject;
436
437 // Translated name of the user expressions group
438 QString mUserExpressionsGroupName;
439};
440
441// clazy:excludeall=qstring-allocations
442
443#endif // QGSEXPRESSIONBUILDER_H
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
A reusable widget that can be used to build a 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...
A abstract base class for defining QgsExpression functions.
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...
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Container of fields for a vector layer.
Definition: qgsfields.h:45
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:107
Represents a vector layer which manages a vector based data sets.
#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