QGIS API Documentation  2.8.2-Wien
 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 
33 class QgsExpressionItemSearchProxy : public QSortFilterProxyModel
34 {
35  public:
37 
38  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
39  {
40  if ( source_parent == qobject_cast<QStandardItemModel*>( sourceModel() )->invisibleRootItem()->index() )
41  return true;
42 
43  return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
44  }
45 };
46 
49 class QgsExpressionItem : public QStandardItem
50 {
51  public:
52  enum ItemType
53  {
57  };
58 
59  QgsExpressionItem( QString label,
60  QString expressionText,
61  QString helpText,
63  : QStandardItem( label )
64  {
65  mExpressionText = expressionText;
66  mHelpText = helpText;
67  mType = itemType;
68  }
69 
70  QgsExpressionItem( QString label,
71  QString expressionText,
73  : QStandardItem( label )
74  {
75  mExpressionText = expressionText;
76  mType = itemType;
77  }
78 
79  QString getExpressionText() { return mExpressionText; }
80 
85  QString getHelpText() { return mHelpText; }
90  void setHelpText( QString helpText ) { mHelpText = helpText; }
91 
97 
98  private:
99  QString mExpressionText;
100  QString mHelpText;
102 };
103 
107 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
108 {
109  Q_OBJECT
110  public:
111  QgsExpressionBuilderWidget( QWidget *parent );
113 
117  void setLayer( QgsVectorLayer* layer );
118 
122  void loadFieldNames();
123 
124  void loadFieldNames( const QgsFields& fields );
125 
127  void setGeomCalculator( const QgsDistanceArea & da );
128 
131  QString expressionText();
132 
134  void setExpressionText( const QString& expression );
135 
143  void registerItem( QString group, QString label, QString expressionText,
144  QString helpText = "",
146 
147  bool isExpressionValid();
148 
149  void saveToRecent( QString key );
150 
151  void loadRecent( QString key );
152 
155  void newFunctionFile( QString fileName = "scratch" );
156 
159  void saveFunctionFile( QString fileName );
160 
163  void loadCodeFromFile( QString path );
164 
167  void loadFunctionCode( QString code );
168 
171  void updateFunctionFileList( QString path );
172 
173  public slots:
174  void currentChanged( const QModelIndex &index, const QModelIndex & );
175  void on_btnRun_pressed();
176  void on_btnNewFile_pressed();
177  void on_cmbFileNames_currentIndexChanged( int index );
178  void on_btnSaveFile_pressed();
179  void on_expressionTree_doubleClicked( const QModelIndex &index );
180  void on_txtExpressionString_textChanged();
181  void on_txtSearchEdit_textChanged();
182  void on_lblPreview_linkActivated( QString link );
183  void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item );
184  void operatorButtonClicked();
185  void showContextMenu( const QPoint & );
186  void loadSampleValues();
187  void loadAllValues();
188 
189  private slots:
190  void setExpressionState( bool state );
191 
192  signals:
198  void expressionParsed( bool isValid );
199 
200  private:
201  void runPythonCode( QString code );
202  void updateFunctionTree();
203  void fillFieldValues( int fieldIndex, int countLimit );
204  QString loadFunctionHelp( QgsExpressionItem* functionName );
205 
206  QString mFunctionsPath;
207  QgsVectorLayer *mLayer;
208  QStandardItemModel *mModel;
209  QgsExpressionItemSearchProxy *mProxyModel;
210  QMap<QString, QgsExpressionItem*> mExpressionGroups;
211  QgsFeature mFeature;
212  QgsExpressionHighlighter* highlighter;
213  bool mExpressionValid;
214  QgsDistanceArea mDa;
215 };
216 
217 #endif // QGSEXPRESSIONBUILDER_H