QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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;
100  static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;
101 
102  private:
103  QString mExpressionText;
104  QString mHelpText;
106 
107 };
108 
115 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
116 {
117  Q_OBJECT
118 
119  public:
121 
122  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
123 
124  protected:
125 
126  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
127 };
128 
129 
135 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
136 {
137  Q_OBJECT
138  public:
139 
143  QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
144  ~QgsExpressionBuilderWidget() override;
145 
150  void setLayer( QgsVectorLayer *layer );
151 
156  void loadFieldNames();
157 
158  void loadFieldNames( const QgsFields &fields );
159 
165  void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues );
166 
168  void setGeomCalculator( const QgsDistanceArea &da );
169 
173  QString expressionText();
174 
176  void setExpressionText( const QString &expression );
177 
183  QString expectedOutputFormat();
184 
191  void setExpectedOutputFormat( const QString &expected );
192 
199  QgsExpressionContext expressionContext() const { return mExpressionContext; }
200 
208  void setExpressionContext( const QgsExpressionContext &context );
209 
222  void registerItem( const QString &group, const QString &label, const QString &expressionText,
223  const QString &helpText = QString(),
225  bool highlightedItem = false, int sortOrder = 1,
226  QIcon icon = QIcon(),
227  const QStringList &tags = QStringList() );
228 
229  bool isExpressionValid();
230 
235  void saveToRecent( const QString &collection = "generic" );
236 
241  void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
242 
247  void loadUserExpressions( );
248 
253  void saveToUserExpressions( const QString &label, const QString expression, const QString &helpText );
254 
259  void removeFromUserExpressions( const QString &label );
260 
264  void newFunctionFile( const QString &fileName = "scratch" );
265 
269  void saveFunctionFile( QString fileName );
270 
274  void loadCodeFromFile( QString path );
275 
279  void loadFunctionCode( const QString &code );
280 
284  void updateFunctionFileList( const QString &path );
285 
291  QStandardItemModel *model();
292 
298  QgsProject *project();
299 
306  void setProject( QgsProject *project );
307 
314  bool evalError() const;
315 
322  bool parserError() const;
323 
324  public slots:
325 
331  void loadSampleValues();
332 
338  void loadAllValues();
339 
347  void loadSampleUsedValues();
348 
356  void loadAllUsedValues();
357 
361  void autosave();
362 
368  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
369 
374  void storeCurrentUserExpression( );
375 
381  void removeSelectedUserExpression( );
382 
387  const QList<QgsExpressionItem *> findExpressions( const QString &label );
388 
389 
390  private slots:
391  void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
392  void showContextMenu( QPoint );
393  void setExpressionState( bool state );
394  void currentChanged( const QModelIndex &index, const QModelIndex & );
395  void operatorButtonClicked();
396  void btnRun_pressed();
397  void btnNewFile_pressed();
398  void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
399  void expressionTree_doubleClicked( const QModelIndex &index );
400  void txtExpressionString_textChanged();
401  void txtSearchEdit_textChanged();
402  void txtSearchEditValues_textChanged();
403  void lblPreview_linkActivated( const QString &link );
404  void mValuesListView_doubleClicked( const QModelIndex &index );
405  void txtPython_textChanged();
406 
407  signals:
408 
415  void expressionParsed( bool isValid );
416 
423  void evalErrorChanged();
424 
431  void parserErrorChanged();
432 
433  protected:
434  void showEvent( QShowEvent *e ) override;
435 
436  private:
437  int FUNCTION_MARKER_ID = 25;
438  void createErrorMarkers( QList<QgsExpression::ParserError> errors );
439  void createMarkers( const QgsExpressionNode *node );
440  void clearFunctionMarkers();
441  void clearErrors();
442  void runPythonCode( const QString &code );
443  void updateFunctionTree();
444  void fillFieldValues( const QString &fieldName, int countLimit, bool forceUsedValues = false );
445  bool formatterCanProvideAvailableValues( const QString &fieldName );
446  QString getFunctionHelp( QgsExpressionFunction *function );
447  QString loadFunctionHelp( QgsExpressionItem *functionName );
448  QString helpStylesheet() const;
449 
450  void loadExpressionContext();
451 
453  void loadRelations();
454 
456  void loadLayers();
457 
469  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
470  const QString &helpText = QString(),
472  bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
473 
477  QString formatRelationHelp( const QgsRelation &relation ) const;
478 
482  QString formatLayerHelp( const QgsMapLayer *layer ) const;
483 
490  void setEvalError( bool evalError );
491 
498  void setParserError( bool parserError );
499 
500  void loadFieldValues( const QVariantMap &values );
501 
502  void loadFieldsAndValues( const QMap<QString, QVariantMap> &fieldValues );
503 
504  bool mAutoSave = true;
505  QString mFunctionsPath;
506  QgsVectorLayer *mLayer = nullptr;
507  std::unique_ptr<QStandardItemModel> mModel;
508  // Will hold items with
509  // * a display string that matches the represented field values
510  // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
511  std::unique_ptr<QStandardItemModel> mValuesModel;
512  std::unique_ptr<QSortFilterProxyModel> mProxyValues;
513  std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
514  QMap<QString, QgsExpressionItem *> mExpressionGroups;
515  QgsExpressionHighlighter *highlighter = nullptr;
516  bool mExpressionValid = false;
517  QgsDistanceArea mDa;
518  QString mRecentKey;
519  QMap<QString, QVariantMap> mFieldValues;
520  QgsExpressionContext mExpressionContext;
521  QPointer< QgsProject > mProject;
522  bool mEvalError = true;
523  bool mParserError = true;
524  // Translated name of the user expressions group
525  QString mUserExpressionsGroupName;
526  QStringList mUserExpressionLabels;
527 };
528 
529 // clazy:excludeall=qstring-allocations
530 
531 #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:91
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.