QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 <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
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
108 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
109
111 void setGeomCalculator( const QgsDistanceArea &da );
112
117 QString expressionText();
118
120 void setExpressionText( const QString &expression );
121
127 QString expectedOutputFormat();
128
135 void setExpectedOutputFormat( const QString &expected );
136
143 QgsExpressionContext expressionContext() const { return mExpressionContext; }
144
152 void setExpressionContext( const QgsExpressionContext &context );
153
155 bool isExpressionValid();
156
162 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
163
169 Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) )SIP_DEPRECATED ;
170
175 QgsExpressionTreeView *expressionTree() const;
176
182 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
183
189 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
190
196 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
197
201 void newFunctionFile( const QString &fileName = "scratch" );
202
206 void saveFunctionFile( QString fileName );
207
211 void loadCodeFromFile( QString path );
212
216 void loadFunctionCode( const QString &code );
217
221 void updateFunctionFileList( const QString &path );
222
229 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
230
236 QgsProject *project();
237
244 void setProject( QgsProject *project );
245
252 bool evalError() const;
253
260 bool parserError() const;
261
267 void setExpressionPreviewVisible( bool isVisible );
268
269 public slots:
270
276 void loadSampleValues();
277
283 void loadAllValues();
284
292 void loadSampleUsedValues();
293
301 void loadAllUsedValues();
302
306 void autosave();
307
313 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
314
319 void storeCurrentUserExpression( );
320
326 void removeSelectedUserExpression( );
327
333 void editSelectedUserExpression();
334
340 const QList<QgsExpressionItem *> findExpressions( const QString &label );
341
342
343 private slots:
344 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
345 void onExpressionParsed( bool state );
346 void expressionTreeItemChanged( QgsExpressionItem *item );
347 void operatorButtonClicked();
348 void btnRun_pressed();
349 void btnNewFile_pressed();
350 void btnRemoveFile_pressed();
351
357 void exportUserExpressions_pressed();
358
364 void importUserExpressions_pressed();
365 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
366 void insertExpressionText( const QString &text );
367 void txtExpressionString_textChanged();
368 void txtSearchEditValues_textChanged();
369 void mValuesListView_doubleClicked( const QModelIndex &index );
370 void txtPython_textChanged();
371
372 signals:
373
380 void expressionParsed( bool isValid );
381
389
397
398 protected:
399 void showEvent( QShowEvent *e ) override;
400
401 private:
402 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
403 {
404 public:
405 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
406 : QgsExpressionTreeView::MenuProvider()
407 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
408
409 QMenu *createContextMenu( QgsExpressionItem *item ) override;
410
411 private:
412 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
413 };
414
415 int FUNCTION_MARKER_ID = 25;
416
417 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
418 void createMarkers( const QgsExpressionNode *node );
419 void clearFunctionMarkers();
420 void clearErrors();
421 void runPythonCode( const QString &code );
422 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
423 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
424 QString getFunctionHelp( QgsExpressionFunction *function );
425 QString loadFunctionHelp( QgsExpressionItem *functionName );
426 QString helpStylesheet() const;
427
428 // To be called whenever expression context has been updated
429 void expressionContextUpdated();
430
431 // Will hold items with
432 // * a display string that matches the represented field values
433 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
434 std::unique_ptr<QStandardItemModel> mValuesModel;
435 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
436
437 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
438
439 bool mAutoSave = true;
440 QString mFunctionsPath;
441 QgsVectorLayer *mLayer = nullptr;
442 QgsExpressionHighlighter *highlighter = nullptr;
443 bool mExpressionValid = false;
444 QgsExpressionContext mExpressionContext;
445 QPointer< QgsProject > mProject;
446
447 // Translated name of the user expressions group
448 QString mUserExpressionsGroupName;
449};
450
451// clazy:excludeall=qstring-allocations
452
453#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