QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsexpressionbuilderwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgisexpressionbuilderwidget.h - A generic expression string 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"
27 #include "qgsexpressioncontext.h"
28 #include "qgsexpression.h"
29 #include "qgsexpressiontreeview.h"
30 
31 
32 class QgsFields;
34 class QgsRelation;
35 
36 
42 class 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 );
66  ~QgsExpressionBuilderWidget() override;
67 
72  void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
73 
78  void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
79 
84  void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const 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 
262  public slots:
263 
269  void loadSampleValues();
270 
276  void loadAllValues();
277 
285  void loadSampleUsedValues();
286 
294  void loadAllUsedValues();
295 
299  void autosave();
300 
306  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
307 
312  void storeCurrentUserExpression( );
313 
319  void removeSelectedUserExpression( );
320 
326  void editSelectedUserExpression();
327 
333  const QList<QgsExpressionItem *> findExpressions( const QString &label );
334 
335 
336  private slots:
337  void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
338  void onExpressionParsed( bool state );
339  void expressionTreeItemChanged( QgsExpressionItem *item );
340  void operatorButtonClicked();
341  void btnRun_pressed();
342  void btnNewFile_pressed();
343  void btnRemoveFile_pressed();
344 
350  void exportUserExpressions_pressed();
351 
357  void importUserExpressions_pressed();
358  void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
359  void insertExpressionText( const QString &text );
360  void txtExpressionString_textChanged();
361  void txtSearchEditValues_textChanged();
362  void mValuesListView_doubleClicked( const QModelIndex &index );
363  void txtPython_textChanged();
364 
365  signals:
366 
373  void expressionParsed( bool isValid );
374 
382 
390 
391  protected:
392  void showEvent( QShowEvent *e ) override;
393 
394  private:
395  class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
396  {
397  public:
398  ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
399  : QgsExpressionTreeView::MenuProvider()
400  , mExpressionBuilderWidget( expressionBuilderWidget ) {}
401 
402  QMenu *createContextMenu( QgsExpressionItem *item ) override;
403 
404  private:
405  QgsExpressionBuilderWidget *mExpressionBuilderWidget;
406  };
407 
408  int FUNCTION_MARKER_ID = 25;
409 
410  void createErrorMarkers( QList<QgsExpression::ParserError> errors );
411  void createMarkers( const QgsExpressionNode *node );
412  void clearFunctionMarkers();
413  void clearErrors();
414  void runPythonCode( const QString &code );
415  void fillFieldValues( const QString &fieldName, int countLimit, bool forceUsedValues = false );
416  QString getFunctionHelp( QgsExpressionFunction *function );
417  QString loadFunctionHelp( QgsExpressionItem *functionName );
418  QString helpStylesheet() const;
419 
420  // Will hold items with
421  // * a display string that matches the represented field values
422  // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
423  std::unique_ptr<QStandardItemModel> mValuesModel;
424  std::unique_ptr<QSortFilterProxyModel> mProxyValues;
425 
426  ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
427 
428  bool mAutoSave = true;
429  QString mFunctionsPath;
430  QgsVectorLayer *mLayer = nullptr;
431  QgsExpressionHighlighter *highlighter = nullptr;
432  bool mExpressionValid = false;
433  QgsExpressionContext mExpressionContext;
434  QPointer< QgsProject > mProject;
435 
436  // Translated name of the user expressions group
437  QString mUserExpressionsGroupName;
438 };
439 
440 // clazy:excludeall=qstring-allocations
441 
442 #endif // QGSEXPRESSIONBUILDER_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsExpressionBuilderWidget::loadFieldNames
Q_DECL_DEPRECATED void loadFieldNames()
Definition: qgsexpressionbuilderwidget.h:98
QgsExpressionBuilderWidget
A reusable widget that can be used to build a expression string.
Definition: qgsexpressionbuilderwidget.h:43
qgsexpression.h
QgsExpressionBuilderWidget::Flag
Flag
Flag to determine what should be loaded.
Definition: qgsexpressionbuilderwidget.h:52
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
QgsExpressionTreeView
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Definition: qgsexpressiontreeview.h:137
QgsExpressionHighlighter
Definition: qgsexpressionhighlighter.h:33
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
qgsexpressioncontext.h
QgsExpressionTreeView::MenuProvider
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
Definition: qgsexpressiontreeview.h:149
qgis_sip.h
qgsexpressiontreeview.h
QgsExpressionBuilderWidget::loadFieldNames
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
Definition: qgsexpressionbuilderwidget.h:101
QgsExpressionNode
Abstract base class for all nodes that can appear in an expression.
Definition: qgsexpressionnode.h:35
QgsExpressionBuilderWidget::evalErrorChanged
void evalErrorChanged()
Will be set to true if the current expression text reported an eval error with the context.
QgsExpressionBuilderWidget::expressionContext
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Definition: qgsexpressionbuilderwidget.h:143
QgsExpressionFunction
A abstract base class for defining QgsExpression functions.
Definition: qgsexpressionfunction.h:41
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsExpressionItem
An expression item that can be used in the QgsExpressionBuilderWidget tree.
Definition: qgsexpressiontreeview.h:38
QgsRelation
Definition: qgsrelation.h:42
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:50
QgsExpressionBuilderWidget::parserErrorChanged
void parserErrorChanged()
Will be set to true if the current expression text reported a parser error with the context.
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsExpressionBuilderWidget::expressionParsed
void expressionParsed(bool isValid)
Emitted when the user changes the expression in the widget.