QGIS API Documentation  3.0.2-Girona (307d082)
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 
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 
275  bool evalError() const;
276 
283  bool parserError() const;
284 
285  public slots:
286 
290  void loadSampleValues();
291 
295  void loadAllValues();
296 
300  void autosave();
301 
307  void setAutoSave( bool enabled ) { mAutoSave = enabled; }
308 
309  private slots:
310  void showContextMenu( QPoint );
311  void setExpressionState( bool state );
312  void currentChanged( const QModelIndex &index, const QModelIndex & );
313  void operatorButtonClicked();
314  void btnRun_pressed();
315  void btnNewFile_pressed();
316  void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
317  void expressionTree_doubleClicked( const QModelIndex &index );
318  void txtExpressionString_textChanged();
319  void txtSearchEdit_textChanged();
320  void txtSearchEditValues_textChanged();
321  void lblPreview_linkActivated( const QString &link );
322  void mValuesListView_doubleClicked( const QModelIndex &index );
323  void txtPython_textChanged();
324 
325  signals:
326 
333  void expressionParsed( bool isValid );
334 
341  void evalErrorChanged();
342 
349  void parserErrorChanged();
350 
351  protected:
352  void showEvent( QShowEvent *e ) override;
353 
354  private:
355  void runPythonCode( const QString &code );
356  void updateFunctionTree();
357  void fillFieldValues( const QString &fieldName, int countLimit );
358  QString loadFunctionHelp( QgsExpressionItem *functionName );
359  QString helpStylesheet() const;
360 
361  void loadExpressionContext();
362 
364  void loadRelations();
365 
367  void loadLayers();
368 
379  void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
380  const QString &helpText = QString(),
382  bool highlightedItem = false, int sortOrder = 1 );
383 
387  QString formatRelationHelp( const QgsRelation &relation ) const;
388 
392  QString formatLayerHelp( const QgsMapLayer *layer ) const;
393 
400  void setEvalError( bool evalError );
401 
408  void setParserError( bool parserError );
409 
410  bool mAutoSave = true;
411  QString mFunctionsPath;
412  QgsVectorLayer *mLayer = nullptr;
413  QStandardItemModel *mModel = nullptr;
414  QStringListModel *mValuesModel = nullptr;
415  QSortFilterProxyModel *mProxyValues = nullptr;
416  QgsExpressionItemSearchProxy *mProxyModel = nullptr;
417  QMap<QString, QgsExpressionItem *> mExpressionGroups;
418  QgsExpressionHighlighter *highlighter = nullptr;
419  bool mExpressionValid = false;
420  QgsDistanceArea mDa;
421  QString mRecentKey;
422  QMap<QString, QStringList> mFieldValues;
423  QgsExpressionContext mExpressionContext;
424  QPointer< QgsProject > mProject;
425  bool mEvalError = true;
426  bool mParserError = true;
427 };
428 
429 // clazy:excludeall=qstring-allocations
430 
431 #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:82
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.