QGIS API Documentation  2.6.0-Brighton
 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
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 
128  void setGeomCalculator( const QgsDistanceArea & da );
129 
132  QString expressionText();
133 
135  void setExpressionText( const QString& expression );
136 
144  void registerItem( QString group, QString label, QString expressionText,
145  QString helpText = "",
147 
148  bool isExpressionValid();
149 
150  void saveToRecent( QString key );
151 
152  void loadRecent( QString key );
153 
154  public slots:
155  void currentChanged( const QModelIndex &index, const QModelIndex & );
156  void on_expressionTree_doubleClicked( const QModelIndex &index );
157  void on_txtExpressionString_textChanged();
158  void on_txtSearchEdit_textChanged();
159  void on_lblPreview_linkActivated( QString link );
160  void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item );
161  void operatorButtonClicked();
162  void showContextMenu( const QPoint & );
163  void loadSampleValues();
164  void loadAllValues();
165 
166  private slots:
167  void setExpressionState( bool state );
168 
169  signals:
175  void expressionParsed( bool isValid );
176 
177  private:
178  void fillFieldValues( int fieldIndex, int countLimit );
179  QString loadFunctionHelp( QgsExpressionItem* functionName );
180 
181  QgsVectorLayer *mLayer;
182  QStandardItemModel *mModel;
183  QgsExpressionItemSearchProxy *mProxyModel;
184  QMap<QString, QgsExpressionItem*> mExpressionGroups;
185  QgsFeature mFeature;
186  QgsExpressionHighlighter* highlighter;
187  bool mExpressionValid;
188  QgsDistanceArea mDa;
189 };
190 
191 #endif // QGSEXPRESSIONBUILDER_H