QGIS API Documentation  3.13.0-Master (740be229cb)
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 "qgsdistancearea.h"
28 #include "qgsexpressioncontext.h"
29 #include "qgsexpression.h"
30 #include "qgsexpressiontreeview.h"
31 
32 
33 class QgsFields;
35 class QgsRelation;
36 
37 
43 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
44 {
45  Q_OBJECT
46  public:
47 
52  enum Flag
53  {
54  LoadNothing = 0,
55  LoadRecent = 1 << 1,
56  LoadUserExpressions = 1 << 2,
57  LoadAll = LoadRecent | LoadUserExpressions,
58  };
59  Q_DECLARE_FLAGS( Flags, Flag )
60  Q_FLAG( Flag )
61 
62 
63 
66  QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
67  ~QgsExpressionBuilderWidget() override;
68 
73  void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
74 
79  void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
80 
85  void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), const Flags &flags = LoadAll );
86 
91  void setLayer( QgsVectorLayer *layer );
92 
96  QgsVectorLayer *layer() const;
97 
99  Q_DECL_DEPRECATED void loadFieldNames() {} SIP_DEPRECATED
100 
102  Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) {mExpressionTreeView->loadFieldNames( fields );} SIP_DEPRECATED
103 
109  Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
110 
112  void setGeomCalculator( const QgsDistanceArea &da );
113 
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 setExpressionState( bool state );
339  void expressionTreeItemChanged( QgsExpressionItem *item );
340  void operatorButtonClicked();
341  void btnRun_pressed();
342  void btnNewFile_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 lblPreview_linkActivated( const QString &link );
362  void mValuesListView_doubleClicked( const QModelIndex &index );
363  void txtPython_textChanged();
364 
365  signals:
366 
373  void expressionParsed( bool isValid );
374 
381  void evalErrorChanged();
382 
389  void parserErrorChanged();
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 )
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 
426  void setEvalError( bool evalError );
427 
434  void setParserError( bool parserError );
435 
436  // Will hold items with
437  // * a display string that matches the represented field values
438  // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
439  std::unique_ptr<QStandardItemModel> mValuesModel;
440  std::unique_ptr<QSortFilterProxyModel> mProxyValues;
441 
442  ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
443 
444  bool mAutoSave = true;
445  QString mFunctionsPath;
446  QgsVectorLayer *mLayer = nullptr;
447  QgsExpressionHighlighter *highlighter = nullptr;
448  bool mExpressionValid = false;
449  QgsDistanceArea mDa;
450  QgsExpressionContext mExpressionContext;
451  QPointer< QgsProject > mProject;
452  bool mEvalError = true;
453  bool mParserError = true;
454  // Translated name of the user expressions group
455  QString mUserExpressionsGroupName;
456 };
457 
458 // clazy:excludeall=qstring-allocations
459 
460 #endif // QGSEXPRESSIONBUILDER_H
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
void setAutoSave(bool enabled)
Enabled or disable auto saving.
Flag
Flag to determine what should be loaded.
Container of fields for a vector layer.
Definition: qgsfields.h:42
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:92
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
Abstract base class for all nodes that can appear in an expression.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
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.
A abstract base class for defining QgsExpression functions.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
Q_DECL_DEPRECATED void loadFieldNames()
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Represents a vector layer which manages a vector based data sets.