QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 
32 {
33  public:
34  enum ItemType
35  {
39  };
40 
42  QString expressionText,
43  QString helpText,
45  : QStandardItem( label )
46  {
47  mExpressionText = expressionText;
48  mHelpText = helpText;
49  mType = itemType;
50  setData( itemType, ItemTypeRole );
51  }
52 
54  QString expressionText,
56  : QStandardItem( label )
57  {
58  mExpressionText = expressionText;
59  mType = itemType;
60  setData( itemType, ItemTypeRole );
61  }
62 
63  QString getExpressionText() const { return mExpressionText; }
64 
69  QString getHelpText() const { return mHelpText; }
74  void setHelpText( QString helpText ) { mHelpText = helpText; }
75 
80  QgsExpressionItem::ItemType getItemType() const { return mType; }
81 
83  static const int CustomSortRole = Qt::UserRole + 1;
85  static const int ItemTypeRole = Qt::UserRole + 2;
86 
87  private:
88  QString mExpressionText;
89  QString mHelpText;
91 
92 };
93 
99 {
100  public:
102  {
103  setFilterCaseSensitivity( Qt::CaseInsensitive );
104  }
105 
106  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
107  {
108  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
110 
111  if ( itemType == QgsExpressionItem::Header )
112  return true;
113 
114  return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
115  }
116 
117  protected:
118 
119  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
120  {
121  int leftSort = sourceModel()->data( left, QgsExpressionItem::CustomSortRole ).toInt();
122  int rightSort = sourceModel()->data( right, QgsExpressionItem::CustomSortRole ).toInt();
123  if ( leftSort != rightSort )
124  return leftSort < rightSort;
125 
126  QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
127  QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();
128 
129  //ignore $ prefixes when sorting
130  if ( leftString.startsWith( "$" ) )
131  leftString = leftString.mid( 1 );
132  if ( rightString.startsWith( "$" ) )
133  rightString = rightString.mid( 1 );
134 
135  return QString::localeAwareCompare( leftString, rightString ) < 0;
136  }
137 };
138 
142 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
143 {
144  Q_OBJECT
145  public:
148 
152  void setLayer( QgsVectorLayer* layer );
153 
157  void loadFieldNames();
158 
159  void loadFieldNames( const QgsFields& fields );
160 
165  void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );
166 
168  void setGeomCalculator( const QgsDistanceArea & da );
169 
172  QString expressionText();
173 
175  void setExpressionText( const QString& expression );
176 
182  QgsExpressionContext expressionContext() const { return mExpressionContext; }
183 
190  void setExpressionContext( const QgsExpressionContext& context );
191 
201  void registerItem( QString group, QString label, QString expressionText,
202  QString helpText = "",
204  bool highlightedItem = false, int sortOrder = 1 );
205 
206  bool isExpressionValid();
207 
208  void saveToRecent( QString key );
209 
210  void loadRecent( QString key );
211 
214  void newFunctionFile( QString fileName = "scratch" );
215 
218  void saveFunctionFile( QString fileName );
219 
222  void loadCodeFromFile( QString path );
223 
226  void loadFunctionCode( QString code );
227 
230  void updateFunctionFileList( QString path );
231 
232  public slots:
233  void currentChanged( const QModelIndex &index, const QModelIndex & );
234  void on_btnRun_pressed();
235  void on_btnNewFile_pressed();
236  void on_cmbFileNames_currentIndexChanged( int index );
237  void on_btnSaveFile_pressed();
238  void on_expressionTree_doubleClicked( const QModelIndex &index );
239  void on_txtExpressionString_textChanged();
240  void on_txtSearchEdit_textChanged();
241  void on_lblPreview_linkActivated( QString link );
242  void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item );
243  void operatorButtonClicked();
244  void showContextMenu( const QPoint & );
245  void loadSampleValues();
246  void loadAllValues();
247 
248  private slots:
249  void setExpressionState( bool state );
250 
251  signals:
257  void expressionParsed( bool isValid );
258 
259  private:
260  void runPythonCode( QString code );
261  void updateFunctionTree();
262  void fillFieldValues( const QString &fieldName, int countLimit );
263  QString loadFunctionHelp( QgsExpressionItem* functionName );
264 
269  QString formatPreviewString( const QString &previewString ) const;
270 
271  void loadExpressionContext();
272 
273  QString mFunctionsPath;
274  QgsVectorLayer *mLayer;
275  QStandardItemModel *mModel;
276  QgsExpressionItemSearchProxy *mProxyModel;
277  QMap<QString, QgsExpressionItem*> mExpressionGroups;
278  QgsFeature mFeature;
279  QgsExpressionHighlighter* highlighter;
280  bool mExpressionValid;
281  QgsDistanceArea mDa;
282  QString mRecentKey;
283  QMap<QString, QStringList> mFieldValues;
284  QgsExpressionContext mExpressionContext;
285 
286 };
287 
288 #endif // QGSEXPRESSIONBUILDER_H
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
static unsigned index
QString getExpressionText() const
void setFilterCaseSensitivity(Qt::CaseSensitivity cs)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
void setHelpText(QString helpText)
Set the help text for the current item.
int localeAwareCompare(const QString &other) const
QString getHelpText() const
Get the help text that is associated with this expression item.
static const int CustomSortRole
Custom sort order role.
Container of fields for a vector layer.
Definition: qgsfield.h:177
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
virtual void setData(const QVariant &value, int role)
Search proxy used to filter the QgsExpressionBuilderWidget tree.
int toInt(bool *ok) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
virtual QVariant data(const QModelIndex &index, int role) const =0
QgsExpressionItem(QString label, QString expressionText, QString helpText, QgsExpressionItem::ItemType itemType=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
QString mid(int position, int n) const
static const int ItemTypeRole
Item type role.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
Represents a vector layer which manages a vector based data sets.
QString toString() const
virtual QVariant data(const QModelIndex &index, int role) const
QgsExpressionItem::ItemType getItemType() const
Get the type of expression item eg header, field, ExpressionNode.