QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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_sip.h"
21 #include "ui_qgsexpressionbuilder.h"
22 #include "qgsdistancearea.h"
23 #include "qgsexpressioncontext.h"
24 #include "qgsexpression.h"
25 
26 #include "QStandardItemModel"
27 #include "QStandardItem"
28 #include "QSortFilterProxyModel"
29 #include "QStringListModel"
30 #include "qgis_gui.h"
31 
32 class QgsFields;
34 class QgsRelation;
35 
40 class GUI_EXPORT QgsExpressionItem : public QStandardItem
41 {
42  public:
43  enum ItemType
44  {
47  ExpressionNode
48  };
49 
50  QgsExpressionItem( const QString &label,
51  const QString &expressionText,
52  const QString &helpText,
53  QgsExpressionItem::ItemType itemType = ExpressionNode )
54  : QStandardItem( label )
55  {
56  mExpressionText = expressionText;
57  mHelpText = helpText;
58  mType = itemType;
59  setData( itemType, ITEM_TYPE_ROLE );
60  }
61 
62  QgsExpressionItem( const QString &label,
63  const QString &expressionText,
64  QgsExpressionItem::ItemType itemType = ExpressionNode )
65  : QStandardItem( label )
66  {
67  mExpressionText = expressionText;
68  mType = itemType;
69  setData( itemType, ITEM_TYPE_ROLE );
70  }
71 
72  QString getExpressionText() const { return mExpressionText; }
73 
79  QString getHelpText() const { return mHelpText; }
80 
86  void setHelpText( const QString &helpText ) { mHelpText = helpText; }
87 
93  QgsExpressionItem::ItemType getItemType() const { return mType; }
94 
96  static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
98  static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
99 
100  private:
101  QString mExpressionText;
102  QString mHelpText;
104 
105 };
106 
113 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
114 {
115  Q_OBJECT
116 
117  public:
119 
120  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
121 
122  protected:
123 
124  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
125 };
126 
127 
133 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
134 {
135  Q_OBJECT
136  public:
137 
141  QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
142  ~QgsExpressionBuilderWidget() override;
143 
148  void setLayer( QgsVectorLayer *layer );
149 
154  void loadFieldNames();
155 
156  void loadFieldNames( const QgsFields &fields );
157 
163  void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues );
164 
166  void setGeomCalculator( const QgsDistanceArea &da );
167 
171  QString expressionText();
172 
174  void setExpressionText( const QString &expression );
175 
181  QString expectedOutputFormat();
182 
189  void setExpectedOutputFormat( const QString &expected );
190 
197  QgsExpressionContext expressionContext() const { return mExpressionContext; }
198 
206  void setExpressionContext( const QgsExpressionContext &context );
207 
219  void registerItem( const QString &group, const QString &label, const QString &expressionText,
220  const QString &helpText = QString(),
222  bool highlightedItem = false, int sortOrder = 1,
223  QIcon icon = QIcon() );
224 
225  bool isExpressionValid();
226 
231  void saveToRecent( const QString &collection = "generic" );
232 
237  void loadRecent( const QString &collection = "generic" );
238 
242  void newFunctionFile( const QString &fileName = "scratch" );
243 
247  void saveFunctionFile( QString fileName );
248 
252  void loadCodeFromFile( QString path );
253 
257  void loadFunctionCode( const QString &code );
258 
262  void updateFunctionFileList( const QString &path );
263 
269  QStandardItemModel *model();
270 
276  QgsProject *project();
277 
284  void setProject( QgsProject *project );
285 
292  bool evalError() const;
293 
300  bool parserError() const;
301 
302  public slots:
303 
307  void loadSampleValues();
308 
312  void loadAllValues();
313 
317  void autosave();
318 
324  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
325 
326  private slots:
327  void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
328  void showContextMenu( QPoint );
329  void setExpressionState( bool state );
330  void currentChanged( const QModelIndex &index, const QModelIndex & );
331  void operatorButtonClicked();
332  void btnRun_pressed();
333  void btnNewFile_pressed();
334  void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
335  void expressionTree_doubleClicked( const QModelIndex &index );
336  void txtExpressionString_textChanged();
337  void txtSearchEdit_textChanged();
338  void txtSearchEditValues_textChanged();
339  void lblPreview_linkActivated( const QString &link );
340  void mValuesListView_doubleClicked( const QModelIndex &index );
341  void txtPython_textChanged();
342 
343  signals:
344 
351  void expressionParsed( bool isValid );
352 
359  void evalErrorChanged();
360 
367  void parserErrorChanged();
368 
369  protected:
370  void showEvent( QShowEvent *e ) override;
371 
372  private:
373  int FUNCTION_MARKER_ID = 25;
374  void createErrorMarkers( QList<QgsExpression::ParserError> errors );
375  void createMarkers( const QgsExpressionNode *node );
376  void clearFunctionMarkers();
377  void clearErrors();
378  void runPythonCode( const QString &code );
379  void updateFunctionTree();
380  void fillFieldValues( const QString &fieldName, int countLimit );
381  QString getFunctionHelp( QgsExpressionFunction *function );
382  QString loadFunctionHelp( QgsExpressionItem *functionName );
383  QString helpStylesheet() const;
384 
385  void loadExpressionContext();
386 
388  void loadRelations();
389 
391  void loadLayers();
392 
403  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
404  const QString &helpText = QString(),
406  bool highlightedItem = false, int sortOrder = 1 );
407 
411  QString formatRelationHelp( const QgsRelation &relation ) const;
412 
416  QString formatLayerHelp( const QgsMapLayer *layer ) const;
417 
424  void setEvalError( bool evalError );
425 
432  void setParserError( bool parserError );
433 
434  void loadFieldValues( const QVariantMap &values );
435 
436  void loadFieldsAndValues( const QMap<QString, QVariantMap> &fieldValues );
437 
438  bool mAutoSave = true;
439  QString mFunctionsPath;
440  QgsVectorLayer *mLayer = nullptr;
441  std::unique_ptr<QStandardItemModel> mModel;
442  // Will hold items with
443  // * a display string that matches the represented field values
444  // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
445  std::unique_ptr<QStandardItemModel> mValuesModel;
446  std::unique_ptr<QSortFilterProxyModel> mProxyValues;
447  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
448  QMap<QString, QgsExpressionItem *> mExpressionGroups;
449  QgsExpressionHighlighter *highlighter = nullptr;
450  bool mExpressionValid = false;
451  QgsDistanceArea mDa;
452  QString mRecentKey;
453  QMap<QString, QVariantMap> mFieldValues;
454  QgsExpressionContext mExpressionContext;
455  QPointer< QgsProject > mProject;
456  bool mEvalError = true;
457  bool mParserError = true;
458 };
459 
460 // clazy:excludeall=qstring-allocations
461 
462 #endif // QGSEXPRESSIONBUILDER_H
Base class for all map layer types.
Definition: qgsmaplayer.h:79
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
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...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:89
Abstract base class for all nodes that can appear in an expression.
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
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.
QString getExpressionText() const
QString getHelpText() const
Gets the help text that is associated with this expression item.
Represents a vector layer which manages a vector based data sets.
void setHelpText(const QString &helpText)
Set the help text for the current item.