QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
130  void setGeomCalculator( const QgsDistanceArea & da );
131 
134  QString expressionText();
135 
137  void setExpressionText( const QString& expression );
138 
146  void registerItem( QString group, QString label, QString expressionText,
147  QString helpText = "",
149 
150  bool isExpressionValid();
151 
152  void saveToRecent( QString key );
153 
154  void loadRecent( QString key );
155 
158  void newFunctionFile( QString fileName = "scratch" );
159 
162  void saveFunctionFile( QString fileName );
163 
166  void loadCodeFromFile( QString path );
167 
170  void loadFunctionCode( QString code );
171 
174  void updateFunctionFileList( QString path );
175 
176  public slots:
177  void currentChanged( const QModelIndex &index, const QModelIndex & );
178  void on_btnRun_pressed();
179  void on_btnNewFile_pressed();
180  void on_cmbFileNames_currentIndexChanged( int index );
181  void on_btnSaveFile_pressed();
182  void on_expressionTree_doubleClicked( const QModelIndex &index );
183  void on_txtExpressionString_textChanged();
184  void on_txtSearchEdit_textChanged();
185  void on_lblPreview_linkActivated( QString link );
186  void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item );
187  void operatorButtonClicked();
188  void showContextMenu( const QPoint & );
189  void loadSampleValues();
190  void loadAllValues();
191 
192  private slots:
193  void setExpressionState( bool state );
194 
195  signals:
201  void expressionParsed( bool isValid );
202 
203  private:
204  void runPythonCode( QString code );
205  void updateFunctionTree();
206  void fillFieldValues( int fieldIndex, int countLimit );
207  QString loadFunctionHelp( QgsExpressionItem* functionName );
208 
213  QString formatPreviewString( const QString &previewString ) const;
214 
215  QString mFunctionsPath;
216  QgsVectorLayer *mLayer;
217  QStandardItemModel *mModel;
218  QgsExpressionItemSearchProxy *mProxyModel;
219  QMap<QString, QgsExpressionItem*> mExpressionGroups;
220  QgsFeature mFeature;
221  QgsExpressionHighlighter* highlighter;
222  bool mExpressionValid;
223  QgsDistanceArea mDa;
224  QString mRecentKey;
225 
226 };
227 
228 #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.