QGIS API Documentation  2.99.0-Master (9caa722)
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 
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 
77  QString getHelpText() const { return mHelpText; }
78 
83  void setHelpText( const QString &helpText ) { mHelpText = helpText; }
84 
89  QgsExpressionItem::ItemType getItemType() const { return mType; }
90 
92  static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
94  static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
95 
96  private:
97  QString mExpressionText;
98  QString mHelpText;
100 
101 };
102 
108 class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
109 {
110  Q_OBJECT
111 
112  public:
114 
115  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
116 
117  protected:
118 
119  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
120 };
121 
122 
127 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
128 {
129  Q_OBJECT
130  public:
131 
135  QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = 0 );
137 
141  void setLayer( QgsVectorLayer *layer );
142 
146  void loadFieldNames();
147 
148  void loadFieldNames( const QgsFields &fields );
149 
154  void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues );
155 
157  void setGeomCalculator( const QgsDistanceArea &da );
158 
161  QString expressionText();
162 
164  void setExpressionText( const QString &expression );
165 
171  QgsExpressionContext expressionContext() const { return mExpressionContext; }
172 
179  void setExpressionContext( const QgsExpressionContext &context );
180 
190  void registerItem( const QString &group, const QString &label, const QString &expressionText,
191  const QString &helpText = "",
193  bool highlightedItem = false, int sortOrder = 1 );
194 
195  bool isExpressionValid();
196 
201  void saveToRecent( const QString &collection = "generic" );
202 
207  void loadRecent( const QString &collection = "generic" );
208 
211  void newFunctionFile( const QString &fileName = "scratch" );
212 
215  void saveFunctionFile( QString fileName );
216 
219  void loadCodeFromFile( QString path );
220 
223  void loadFunctionCode( const QString &code );
224 
227  void updateFunctionFileList( const QString &path );
228 
234  QStandardItemModel *model();
235 
241  QgsProject *project();
242 
249  void setProject( QgsProject *project );
250 
251  public slots:
252 
256  void loadSampleValues();
257 
261  void loadAllValues();
262 
266  void autosave();
267 
273  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
274 
275  private slots:
276  void showContextMenu( QPoint );
277  void setExpressionState( bool state );
278  void currentChanged( const QModelIndex &index, const QModelIndex & );
279  void operatorButtonClicked();
280  void on_btnRun_pressed();
281  void on_btnNewFile_pressed();
282  void on_cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
283  void on_expressionTree_doubleClicked( const QModelIndex &index );
284  void on_txtExpressionString_textChanged();
285  void on_txtSearchEdit_textChanged();
286  void on_txtSearchEditValues_textChanged();
287  void on_lblPreview_linkActivated( const QString &link );
288  void on_mValuesListView_doubleClicked( const QModelIndex &index );
289  void on_txtPython_textChanged();
290 
291  signals:
292 
298  void expressionParsed( bool isValid );
299 
300  protected:
301  void showEvent( QShowEvent *e );
302 
303  private:
304  void runPythonCode( const QString &code );
305  void updateFunctionTree();
306  void fillFieldValues( const QString &fieldName, int countLimit );
307  QString loadFunctionHelp( QgsExpressionItem *functionName );
308  QString helpStylesheet() const;
309 
310  void loadExpressionContext();
311 
313  void loadRelations();
314 
316  void loadLayers();
317 
327  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
328  const QString &helpText = "",
330  bool highlightedItem = false, int sortOrder = 1 );
331 
335  QString formatRelationHelp( const QgsRelation &relation ) const;
336 
340  QString formatLayerHelp( const QgsMapLayer *layer ) const;
341 
342  bool mAutoSave;
343  QString mFunctionsPath;
344  QgsVectorLayer *mLayer = nullptr;
345  QStandardItemModel *mModel = nullptr;
346  QStringListModel *mValuesModel = nullptr;
347  QSortFilterProxyModel *mProxyValues = nullptr;
348  QgsExpressionItemSearchProxy *mProxyModel = nullptr;
349  QMap<QString, QgsExpressionItem *> mExpressionGroups;
350  QgsExpressionHighlighter *highlighter = nullptr;
351  bool mExpressionValid;
352  QgsDistanceArea mDa;
353  QString mRecentKey;
354  QMap<QString, QStringList> mFieldValues;
355  QgsExpressionContext mExpressionContext;
356  QPointer< QgsProject > mProject;
357 };
358 
359 #endif // QGSEXPRESSIONBUILDER_H
Base class for all map layer types.
Definition: qgsmaplayer.h:54
#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:41
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:79
QgsExpressionItem::ItemType getItemType() const
Get 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.
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
Get 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.