QGIS API Documentation  2.11.0-Master
qgsexpressionbuilderwidget.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgisexpressionbuilderwidget.h - A genric 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 "ui_qgsexpressionbuilder.h"
21 #include "qgsvectorlayer.h"
23 #include "qgsdistancearea.h"
24 
25 #include "QStandardItemModel"
26 #include "QStandardItem"
27 #include "QSortFilterProxyModel"
28 
34 {
35  public:
37  {
38  setFilterCaseSensitivity( Qt::CaseInsensitive );
39  }
40 
41  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
42  {
43  if ( source_parent == qobject_cast<QStandardItemModel*>( sourceModel() )->invisibleRootItem()->index() )
44  return true;
45 
46  return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
47  }
48 };
49 
53 {
54  public:
55  enum ItemType
56  {
60  };
61 
63  QString expressionText,
64  QString helpText,
66  : QStandardItem( label )
67  {
68  mExpressionText = expressionText;
69  mHelpText = helpText;
70  mType = itemType;
71  }
72 
74  QString expressionText,
76  : QStandardItem( label )
77  {
78  mExpressionText = expressionText;
79  mType = itemType;
80  }
81 
82  QString getExpressionText() { return mExpressionText; }
83 
88  QString getHelpText() { return mHelpText; }
93  void setHelpText( QString helpText ) { mHelpText = helpText; }
94 
100 
101  private:
102  QString mExpressionText;
103  QString mHelpText;
105 };
106 
110 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
111 {
112  Q_OBJECT
113  public:
116 
120  void setLayer( QgsVectorLayer* layer );
121 
125  void loadFieldNames();
126 
127  void loadFieldNames( const QgsFields& fields );
128 
133  void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );
134 
136  void setGeomCalculator( const QgsDistanceArea & da );
137 
140  QString expressionText();
141 
143  void setExpressionText( const QString& expression );
144 
152  void registerItem( QString group, QString label, QString expressionText,
153  QString helpText = "",
155 
156  bool isExpressionValid();
157 
158  void saveToRecent( QString key );
159 
160  void loadRecent( QString key );
161 
164  void newFunctionFile( QString fileName = "scratch" );
165 
168  void saveFunctionFile( QString fileName );
169 
172  void loadCodeFromFile( QString path );
173 
176  void loadFunctionCode( QString code );
177 
180  void updateFunctionFileList( QString path );
181 
182  public slots:
183  void currentChanged( const QModelIndex &index, const QModelIndex & );
184  void on_btnRun_pressed();
185  void on_btnNewFile_pressed();
186  void on_cmbFileNames_currentIndexChanged( int index );
187  void on_btnSaveFile_pressed();
188  void on_expressionTree_doubleClicked( const QModelIndex &index );
189  void on_txtExpressionString_textChanged();
190  void on_txtSearchEdit_textChanged();
191  void on_lblPreview_linkActivated( QString link );
192  void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item );
193  void operatorButtonClicked();
194  void showContextMenu( const QPoint & );
195  void loadSampleValues();
196  void loadAllValues();
197 
198  private slots:
199  void setExpressionState( bool state );
200 
201  signals:
207  void expressionParsed( bool isValid );
208 
209  private:
210  void runPythonCode( QString code );
211  void updateFunctionTree();
212  void fillFieldValues( const QString &fieldName, int countLimit );
213  QString loadFunctionHelp( QgsExpressionItem* functionName );
214 
219  QString formatPreviewString( const QString &previewString ) const;
220 
221  QString mFunctionsPath;
222  QgsVectorLayer *mLayer;
223  QStandardItemModel *mModel;
224  QgsExpressionItemSearchProxy *mProxyModel;
225  QMap<QString, QgsExpressionItem*> mExpressionGroups;
226  QgsFeature mFeature;
227  QgsExpressionHighlighter* highlighter;
228  bool mExpressionValid;
229  QgsDistanceArea mDa;
230  QString mRecentKey;
231  QMap<QString, QStringList> mFieldValues;
232 
233 };
234 
235 #endif // QGSEXPRESSIONBUILDER_H
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
static unsigned index
void setFilterCaseSensitivity(Qt::CaseSensitivity cs)
void setHelpText(QString helpText)
Set the help text for the current item.
Container of fields for a vector layer.
Definition: qgsfield.h:173
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:162
Search proxy used to filter the QgsExpressionBuilderWidget tree.
QgsExpressionItem(QString label, QString expressionText, QString helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
QgsExpressionItem::ItemType getItemType()
Get the type of expression item eg header, field, ExpressionNode.
General purpose distance and area calculator.
A reusable widget that can be used to build a expression string.
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
An expression item that can be used in the QgsExpressionBuilderWidget tree.
QgsExpressionItem(QString label, QString expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
QAbstractItemModel * sourceModel() const
Represents a vector layer which manages a vector based data sets.
QString getHelpText()
Get the help text that is associated with this expression item.