QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  // To be called whenever expression context has been updated
421  void expressionContextUpdated();
422 
423  // Will hold items with
424  // * a display string that matches the represented field values
425  // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
426  std::unique_ptr<QStandardItemModel> mValuesModel;
427  std::unique_ptr<QSortFilterProxyModel> mProxyValues;
428 
429  ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
430 
431  bool mAutoSave = true;
432  QString mFunctionsPath;
433  QgsVectorLayer *mLayer = nullptr;
434  QgsExpressionHighlighter *highlighter = nullptr;
435  bool mExpressionValid = false;
436  QgsExpressionContext mExpressionContext;
437  QPointer< QgsProject > mProject;
438 
439  // Translated name of the user expressions group
440  QString mUserExpressionsGroupName;
441 };
442 
443 // clazy:excludeall=qstring-allocations
444 
445 #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:99
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