QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgis.h"
21 #include "ui_qgsexpressionbuilder.h"
22 #include "qgsdistancearea.h"
23 #include "qgsexpressioncontext.h"
24 
25 #include "QStandardItemModel"
26 #include "QStandardItem"
27 #include "QSortFilterProxyModel"
28 #include "QStringListModel"
29 #include "qgis_gui.h"
30 
31 class QgsFields;
33 class QgsRelation;
34 
39 class GUI_EXPORT QgsExpressionItem : public QStandardItem
40 {
41  public:
42  enum ItemType
43  {
46  ExpressionNode
47  };
48 
49  QgsExpressionItem( const QString &label,
50  const QString &expressionText,
51  const QString &helpText,
52  QgsExpressionItem::ItemType itemType = ExpressionNode )
53  : QStandardItem( label )
54  {
55  mExpressionText = expressionText;
56  mHelpText = helpText;
57  mType = itemType;
58  setData( itemType, ITEM_TYPE_ROLE );
59  }
60 
61  QgsExpressionItem( const QString &label,
62  const QString &expressionText,
63  QgsExpressionItem::ItemType itemType = ExpressionNode )
64  : QStandardItem( label )
65  {
66  mExpressionText = expressionText;
67  mType = itemType;
68  setData( itemType, ITEM_TYPE_ROLE );
69  }
70 
71  QString getExpressionText() const { return mExpressionText; }
72 
78  QString getHelpText() const { return mHelpText; }
79 
85  void setHelpText( const QString &helpText ) { mHelpText = helpText; }
86 
92  QgsExpressionItem::ItemType getItemType() const { return mType; }
93 
95  static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
97  static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
98 
99  private:
100  QString mExpressionText;
101  QString mHelpText;
103 
104 };
105 
112 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
113 {
114  Q_OBJECT
115 
116  public:
118 
119  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
120 
121  protected:
122 
123  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
124 };
125 
126 
132 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
133 {
134  Q_OBJECT
135  public:
136 
140  QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
141  ~QgsExpressionBuilderWidget() override;
142 
147  void setLayer( QgsVectorLayer *layer );
148 
153  void loadFieldNames();
154 
155  void loadFieldNames( const QgsFields &fields );
156 
162  void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues );
163 
165  void setGeomCalculator( const QgsDistanceArea &da );
166 
170  QString expressionText();
171 
173  void setExpressionText( const QString &expression );
174 
180  QString expectedOutputFormat();
181 
188  void setExpectedOutputFormat( const QString &expected );
189 
196  QgsExpressionContext expressionContext() const { return mExpressionContext; }
197 
205  void setExpressionContext( const QgsExpressionContext &context );
206 
218  void registerItem( const QString &group, const QString &label, const QString &expressionText,
219  const QString &helpText = QString(),
221  bool highlightedItem = false, int sortOrder = 1,
222  QIcon icon = QIcon() );
223 
224  bool isExpressionValid();
225 
230  void saveToRecent( const QString &collection = "generic" );
231 
236  void loadRecent( const QString &collection = "generic" );
237 
241  void newFunctionFile( const QString &fileName = "scratch" );
242 
246  void saveFunctionFile( QString fileName );
247 
251  void loadCodeFromFile( QString path );
252 
256  void loadFunctionCode( const QString &code );
257 
261  void updateFunctionFileList( const QString &path );
262 
268  QStandardItemModel *model();
269 
275  QgsProject *project();
276 
283  void setProject( QgsProject *project );
284 
291  bool evalError() const;
292 
299  bool parserError() const;
300 
301  public slots:
302 
306  void loadSampleValues();
307 
311  void loadAllValues();
312 
316  void autosave();
317 
323  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
324 
325  private slots:
326  void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
327  void showContextMenu( QPoint );
328  void setExpressionState( bool state );
329  void currentChanged( const QModelIndex &index, const QModelIndex & );
330  void operatorButtonClicked();
331  void btnRun_pressed();
332  void btnNewFile_pressed();
333  void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
334  void expressionTree_doubleClicked( const QModelIndex &index );
335  void txtExpressionString_textChanged();
336  void txtSearchEdit_textChanged();
337  void txtSearchEditValues_textChanged();
338  void lblPreview_linkActivated( const QString &link );
339  void mValuesListView_doubleClicked( const QModelIndex &index );
340  void txtPython_textChanged();
341 
342  signals:
343 
350  void expressionParsed( bool isValid );
351 
358  void evalErrorChanged();
359 
366  void parserErrorChanged();
367 
368  protected:
369  void showEvent( QShowEvent *e ) override;
370 
371  private:
372  int FUNCTION_MARKER_ID = 25;
373  void createErrorMarkers( QList<QgsExpression::ParserError> errors );
374  void createMarkers( const QgsExpressionNode *node );
375  void clearFunctionMarkers();
376  void clearErrors();
377  void runPythonCode( const QString &code );
378  void updateFunctionTree();
379  void fillFieldValues( const QString &fieldName, int countLimit );
380  QString getFunctionHelp( QgsExpressionFunction *function );
381  QString loadFunctionHelp( QgsExpressionItem *functionName );
382  QString helpStylesheet() const;
383 
384  void loadExpressionContext();
385 
387  void loadRelations();
388 
390  void loadLayers();
391 
402  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
403  const QString &helpText = QString(),
405  bool highlightedItem = false, int sortOrder = 1 );
406 
410  QString formatRelationHelp( const QgsRelation &relation ) const;
411 
415  QString formatLayerHelp( const QgsMapLayer *layer ) const;
416 
423  void setEvalError( bool evalError );
424 
431  void setParserError( bool parserError );
432 
433  bool mAutoSave = true;
434  QString mFunctionsPath;
435  QgsVectorLayer *mLayer = nullptr;
436  QStandardItemModel *mModel = nullptr;
437  QStringListModel *mValuesModel = nullptr;
438  QSortFilterProxyModel *mProxyValues = nullptr;
439  QgsExpressionItemSearchProxy *mProxyModel = nullptr;
440  QMap<QString, QgsExpressionItem *> mExpressionGroups;
441  QgsExpressionHighlighter *highlighter = nullptr;
442  bool mExpressionValid = false;
443  QgsDistanceArea mDa;
444  QString mRecentKey;
445  QMap<QString, QStringList> mFieldValues;
446  QgsExpressionContext mExpressionContext;
447  QPointer< QgsProject > mProject;
448  bool mEvalError = true;
449  bool mParserError = true;
450 };
451 
452 // clazy:excludeall=qstring-allocations
453 
454 #endif // QGSEXPRESSIONBUILDER_H
Base class for all map layer types.
Definition: qgsmaplayer.h:63
QString getExpressionText() const
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString getHelpText() const
Gets the help text that is associated with this expression item.
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
void setAutoSave(bool enabled)
Enabled or disable auto saving.
Container of fields for a vector layer.
Definition: qgsfields.h:42
Search proxy used to filter the QgsExpressionBuilderWidget tree.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Reads and writes project states.
Definition: qgsproject.h:89
Abstract base class for all nodes that can appear in an expression.
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
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.
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Represents a vector layer which manages a vector based data sets.
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
void setHelpText(const QString &helpText)
Set the help text for the current item.