QGIS API Documentation  3.2.0-Bonn (bc43194)
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 #include "qgsfeature.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 
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 
223  bool isExpressionValid();
224 
229  void saveToRecent( const QString &collection = "generic" );
230 
235  void loadRecent( const QString &collection = "generic" );
236 
240  void newFunctionFile( const QString &fileName = "scratch" );
241 
245  void saveFunctionFile( QString fileName );
246 
250  void loadCodeFromFile( QString path );
251 
255  void loadFunctionCode( const QString &code );
256 
260  void updateFunctionFileList( const QString &path );
261 
267  QStandardItemModel *model();
268 
274  QgsProject *project();
275 
282  void setProject( QgsProject *project );
283 
290  bool evalError() const;
291 
298  bool parserError() const;
299 
300  public slots:
301 
305  void loadSampleValues();
306 
310  void loadAllValues();
311 
315  void autosave();
316 
322  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
323 
324  private slots:
325  void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
326  void showContextMenu( QPoint );
327  void setExpressionState( bool state );
328  void currentChanged( const QModelIndex &index, const QModelIndex & );
329  void operatorButtonClicked();
330  void btnRun_pressed();
331  void btnNewFile_pressed();
332  void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
333  void expressionTree_doubleClicked( const QModelIndex &index );
334  void txtExpressionString_textChanged();
335  void txtSearchEdit_textChanged();
336  void txtSearchEditValues_textChanged();
337  void lblPreview_linkActivated( const QString &link );
338  void mValuesListView_doubleClicked( const QModelIndex &index );
339  void txtPython_textChanged();
340 
341  signals:
342 
349  void expressionParsed( bool isValid );
350 
357  void evalErrorChanged();
358 
365  void parserErrorChanged();
366 
367  protected:
368  void showEvent( QShowEvent *e ) override;
369 
370  private:
371  int FUNCTION_MARKER_ID = 25;
372  void createErrorMarkers( QList<QgsExpression::ParserError> errors );
373  void createMarkers( const QgsExpressionNode *node );
374  void clearFunctionMarkers();
375  void clearErrors();
376  void runPythonCode( const QString &code );
377  void updateFunctionTree();
378  void fillFieldValues( const QString &fieldName, int countLimit );
379  QString getFunctionHelp( QgsExpressionFunction *function );
380  QString loadFunctionHelp( QgsExpressionItem *functionName );
381  QString helpStylesheet() const;
382 
383  void loadExpressionContext();
384 
386  void loadRelations();
387 
389  void loadLayers();
390 
401  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
402  const QString &helpText = QString(),
404  bool highlightedItem = false, int sortOrder = 1 );
405 
409  QString formatRelationHelp( const QgsRelation &relation ) const;
410 
414  QString formatLayerHelp( const QgsMapLayer *layer ) const;
415 
422  void setEvalError( bool evalError );
423 
430  void setParserError( bool parserError );
431 
432  bool mAutoSave = true;
433  QString mFunctionsPath;
434  QgsVectorLayer *mLayer = nullptr;
435  QStandardItemModel *mModel = nullptr;
436  QStringListModel *mValuesModel = nullptr;
437  QSortFilterProxyModel *mProxyValues = nullptr;
438  QgsExpressionItemSearchProxy *mProxyModel = nullptr;
439  QMap<QString, QgsExpressionItem *> mExpressionGroups;
440  QgsExpressionHighlighter *highlighter = nullptr;
441  bool mExpressionValid = false;
442  QgsDistanceArea mDa;
443  QString mRecentKey;
444  QMap<QString, QStringList> mFieldValues;
445  QgsExpressionContext mExpressionContext;
446  QPointer< QgsProject > mProject;
447  bool mEvalError = true;
448  bool mParserError = true;
449 };
450 
451 // clazy:excludeall=qstring-allocations
452 
453 #endif // QGSEXPRESSIONBUILDER_H
Base class for all map layer types.
Definition: qgsmaplayer.h:61
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
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:85
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.