QGIS API Documentation  2.0.1-Dufour
 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 
129  void setGeomCalculator( const QgsDistanceArea & da );
130 
133  QString expressionText();
134 
136  void setExpressionText( const QString& expression );
137 
145  void registerItem( QString group, QString label, QString expressionText,
146  QString helpText = "",
148 
149  bool isExpressionValid();
150 
151  public slots:
152  void currentChanged( const QModelIndex &index, const QModelIndex & );
153  void on_expressionTree_doubleClicked( const QModelIndex &index );
154  void on_txtExpressionString_textChanged();
155  void on_txtSearchEdit_textChanged();
156  void on_lblPreview_linkActivated( QString link );
157  void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item );
158  void operatorButtonClicked();
159  void showContextMenu( const QPoint & );
160  void loadSampleValues();
161  void loadAllValues();
162 
163  private slots:
164  void setExpressionState( bool state );
165 
166  signals:
172  void expressionParsed( bool isValid );
173 
174  private:
175  void fillFieldValues( int fieldIndex, int countLimit );
176  QString loadFunctionHelp( QgsExpressionItem* functionName );
177 
179  QStandardItemModel *mModel;
181  QMap<QString, QgsExpressionItem*> mExpressionGroups;
186 };
187 
188 #endif // QGSEXPRESSIONBUILDER_H