QGIS API Documentation  2.99.0-Master (9fdd060)
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 = 0 );
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 
182  QgsExpressionContext expressionContext() const { return mExpressionContext; }
183 
191  void setExpressionContext( const QgsExpressionContext &context );
192 
203  void registerItem( const QString &group, const QString &label, const QString &expressionText,
204  const QString &helpText = QString(),
206  bool highlightedItem = false, int sortOrder = 1 );
207 
208  bool isExpressionValid();
209 
214  void saveToRecent( const QString &collection = "generic" );
215 
220  void loadRecent( const QString &collection = "generic" );
221 
225  void newFunctionFile( const QString &fileName = "scratch" );
226 
230  void saveFunctionFile( QString fileName );
231 
235  void loadCodeFromFile( QString path );
236 
240  void loadFunctionCode( const QString &code );
241 
245  void updateFunctionFileList( const QString &path );
246 
252  QStandardItemModel *model();
253 
259  QgsProject *project();
260 
267  void setProject( QgsProject *project );
268 
269  public slots:
270 
274  void loadSampleValues();
275 
279  void loadAllValues();
280 
284  void autosave();
285 
291  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
292 
293  private slots:
294  void showContextMenu( QPoint );
295  void setExpressionState( bool state );
296  void currentChanged( const QModelIndex &index, const QModelIndex & );
297  void operatorButtonClicked();
298  void btnRun_pressed();
299  void btnNewFile_pressed();
300  void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
301  void expressionTree_doubleClicked( const QModelIndex &index );
302  void txtExpressionString_textChanged();
303  void txtSearchEdit_textChanged();
304  void txtSearchEditValues_textChanged();
305  void lblPreview_linkActivated( const QString &link );
306  void mValuesListView_doubleClicked( const QModelIndex &index );
307  void txtPython_textChanged();
308 
309  signals:
310 
317  void expressionParsed( bool isValid );
318 
319  protected:
320  void showEvent( QShowEvent *e );
321 
322  private:
323  void runPythonCode( const QString &code );
324  void updateFunctionTree();
325  void fillFieldValues( const QString &fieldName, int countLimit );
326  QString loadFunctionHelp( QgsExpressionItem *functionName );
327  QString helpStylesheet() const;
328 
329  void loadExpressionContext();
330 
332  void loadRelations();
333 
335  void loadLayers();
336 
347  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
348  const QString &helpText = QString(),
350  bool highlightedItem = false, int sortOrder = 1 );
351 
355  QString formatRelationHelp( const QgsRelation &relation ) const;
356 
360  QString formatLayerHelp( const QgsMapLayer *layer ) const;
361 
362  bool mAutoSave = true;
363  QString mFunctionsPath;
364  QgsVectorLayer *mLayer = nullptr;
365  QStandardItemModel *mModel = nullptr;
366  QStringListModel *mValuesModel = nullptr;
367  QSortFilterProxyModel *mProxyValues = nullptr;
368  QgsExpressionItemSearchProxy *mProxyModel = nullptr;
369  QMap<QString, QgsExpressionItem *> mExpressionGroups;
370  QgsExpressionHighlighter *highlighter = nullptr;
371  bool mExpressionValid = false;
372  QgsDistanceArea mDa;
373  QString mRecentKey;
374  QMap<QString, QStringList> mFieldValues;
375  QgsExpressionContext mExpressionContext;
376  QPointer< QgsProject > mProject;
377 };
378 
379 // clazy:excludeall=qstring-allocations
380 
381 #endif // QGSEXPRESSIONBUILDER_H
Base class for all map layer types.
Definition: qgsmaplayer.h:56
#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:81
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.