QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
116  QString expressionText();
117 
119  void setExpressionText( const QString &expression );
120 
126  QString expectedOutputFormat();
127 
134  void setExpectedOutputFormat( const QString &expected );
135 
142  QgsExpressionContext expressionContext() const { return mExpressionContext; }
143 
151  void setExpressionContext( const QgsExpressionContext &context );
152 
154  bool isExpressionValid();
155 
161  Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
162 
168  Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) )SIP_DEPRECATED ;
169 
174  QgsExpressionTreeView *expressionTree() const;
175 
181  Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
182 
188  Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText ) SIP_DEPRECATED;
189 
195  Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
196 
200  void newFunctionFile( const QString &fileName = "scratch" );
201 
205  void saveFunctionFile( QString fileName );
206 
210  void loadCodeFromFile( QString path );
211 
215  void loadFunctionCode( const QString &code );
216 
220  void updateFunctionFileList( const QString &path );
221 
228  Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
229 
235  QgsProject *project();
236 
243  void setProject( QgsProject *project );
244 
251  bool evalError() const;
252 
259  bool parserError() const;
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  void evalErrorChanged();
381 
388  void parserErrorChanged();
389 
390  protected:
391  void showEvent( QShowEvent *e ) override;
392 
393  private:
394  class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
395  {
396  public:
397  ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
398  : QgsExpressionTreeView::MenuProvider()
399  , mExpressionBuilderWidget( expressionBuilderWidget ) {}
400 
401  QMenu *createContextMenu( QgsExpressionItem *item ) override;
402 
403  private:
404  QgsExpressionBuilderWidget *mExpressionBuilderWidget;
405  };
406 
407  int FUNCTION_MARKER_ID = 25;
408 
409  void createErrorMarkers( QList<QgsExpression::ParserError> errors );
410  void createMarkers( const QgsExpressionNode *node );
411  void clearFunctionMarkers();
412  void clearErrors();
413  void runPythonCode( const QString &code );
414  void fillFieldValues( const QString &fieldName, int countLimit, bool forceUsedValues = false );
415  QString getFunctionHelp( QgsExpressionFunction *function );
416  QString loadFunctionHelp( QgsExpressionItem *functionName );
417  QString helpStylesheet() const;
418 
419  // Will hold items with
420  // * a display string that matches the represented field values
421  // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
422  std::unique_ptr<QStandardItemModel> mValuesModel;
423  std::unique_ptr<QSortFilterProxyModel> mProxyValues;
424 
425  ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
426 
427  bool mAutoSave = true;
428  QString mFunctionsPath;
429  QgsVectorLayer *mLayer = nullptr;
430  QgsExpressionHighlighter *highlighter = nullptr;
431  bool mExpressionValid = false;
432  QgsExpressionContext mExpressionContext;
433  QPointer< QgsProject > mProject;
434 
435  // Translated name of the user expressions group
436  QString mUserExpressionsGroupName;
437 };
438 
439 // clazy:excludeall=qstring-allocations
440 
441 #endif // QGSEXPRESSIONBUILDER_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsExpressionBuilderWidget::loadFieldNames
Q_DECL_DEPRECATED void loadFieldNames()
Definition: qgsexpressionbuilderwidget.h:98
QgsExpressionBuilderWidget
Definition: qgsexpressionbuilderwidget.h:42
qgsexpression.h
QgsExpressionBuilderWidget::Flag
Flag
Flag to determine what should be loaded.
Definition: qgsexpressionbuilderwidget.h:51
QgsFields
Definition: qgsfields.h:44
QgsProject
Definition: qgsproject.h:92
QgsExpressionTreeView
Definition: qgsexpressiontreeview.h:134
QgsExpressionHighlighter
Definition: qgsexpressionhighlighter.h:32
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
qgsexpressioncontext.h
QgsExpressionTreeView::MenuProvider
Definition: qgsexpressiontreeview.h:146
qgis_sip.h
qgsexpressiontreeview.h
QgsExpressionBuilderWidget::loadFieldNames
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
Definition: qgsexpressionbuilderwidget.h:101
QgsExpressionNode
Definition: qgsexpressionnode.h:34
QgsExpressionBuilderWidget::expressionContext
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Definition: qgsexpressionbuilderwidget.h:142
QgsExpressionFunction
Definition: qgsexpressionfunction.h:40
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsExpressionItem
Definition: qgsexpressiontreeview.h:37
QgsRelation
Definition: qgsrelation.h:41
QgsDistanceArea
Definition: qgsdistancearea.h:49
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53