QGIS API Documentation  2.12.0-Lyon
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 #include "QStringListModel"
29 
33 {
34  public:
35  enum ItemType
36  {
40  };
41 
42  QgsExpressionItem( const QString& label,
43  const QString& expressionText,
44  const QString& helpText,
46  : QStandardItem( label )
47  {
48  mExpressionText = expressionText;
49  mHelpText = helpText;
50  mType = itemType;
51  setData( itemType, ItemTypeRole );
52  }
53 
54  QgsExpressionItem( const QString& label,
55  const QString& expressionText,
57  : QStandardItem( label )
58  {
59  mExpressionText = expressionText;
60  mType = itemType;
61  setData( itemType, ItemTypeRole );
62  }
63 
64  QString getExpressionText() const { return mExpressionText; }
65 
70  QString getHelpText() const { return mHelpText; }
75  void setHelpText( const QString& helpText ) { mHelpText = helpText; }
76 
81  QgsExpressionItem::ItemType getItemType() const { return mType; }
82 
84  static const int CustomSortRole = Qt::UserRole + 1;
86  static const int ItemTypeRole = Qt::UserRole + 2;
87 
88  private:
89  QString mExpressionText;
90  QString mHelpText;
92 
93 };
94 
100 {
101  public:
103  {
104  setFilterCaseSensitivity( Qt::CaseInsensitive );
105  }
106 
107  bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
108  {
109  QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
111 
112  if ( itemType == QgsExpressionItem::Header )
113  return true;
114 
115  return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
116  }
117 
118  protected:
119 
120  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
121  {
122  int leftSort = sourceModel()->data( left, QgsExpressionItem::CustomSortRole ).toInt();
123  int rightSort = sourceModel()->data( right, QgsExpressionItem::CustomSortRole ).toInt();
124  if ( leftSort != rightSort )
125  return leftSort < rightSort;
126 
127  QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
128  QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();
129 
130  //ignore $ prefixes when sorting
131  if ( leftString.startsWith( "$" ) )
132  leftString = leftString.mid( 1 );
133  if ( rightString.startsWith( "$" ) )
134  rightString = rightString.mid( 1 );
135 
136  return QString::localeAwareCompare( leftString, rightString ) < 0;
137  }
138 };
139 
140 
144 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
145 {
146  Q_OBJECT
147  public:
150 
154  void setLayer( QgsVectorLayer* layer );
155 
159  void loadFieldNames();
160 
161  void loadFieldNames( const QgsFields& fields );
162 
167  void loadFieldsAndValues( const QMap<QString, QStringList>& fieldValues );
168 
170  void setGeomCalculator( const QgsDistanceArea & da );
171 
174  QString expressionText();
175 
177  void setExpressionText( const QString& expression );
178 
184  QgsExpressionContext expressionContext() const { return mExpressionContext; }
185 
192  void setExpressionContext( const QgsExpressionContext& context );
193 
203  void registerItem( const QString& group, const QString& label, const QString& expressionText,
204  const QString& helpText = "",
206  bool highlightedItem = false, int sortOrder = 1 );
207 
208  bool isExpressionValid();
209 
210  void saveToRecent( const QString& key );
211 
212  void loadRecent( const QString& key );
213 
216  void newFunctionFile( const QString& fileName = "scratch" );
217 
220  void saveFunctionFile( QString fileName );
221 
224  void loadCodeFromFile( QString path );
225 
228  void loadFunctionCode( const QString& code );
229 
232  void updateFunctionFileList( const QString& path );
233 
234  public slots:
235  void currentChanged( const QModelIndex &index, const QModelIndex & );
236  void on_btnRun_pressed();
237  void on_btnNewFile_pressed();
238  void on_cmbFileNames_currentIndexChanged( int index );
239  void on_btnSaveFile_pressed();
240  void on_expressionTree_doubleClicked( const QModelIndex &index );
241  void on_txtExpressionString_textChanged();
242  void on_txtSearchEdit_textChanged();
243  void on_txtSearchEditValues_textChanged();
244  void on_lblPreview_linkActivated( const QString& link );
245  void on_mValuesListView_doubleClicked( const QModelIndex &index );
246  void operatorButtonClicked();
247  void showContextMenu( const QPoint & );
248  void loadSampleValues();
249  void loadAllValues();
250 
251  private slots:
252  void setExpressionState( bool state );
253 
254  signals:
260  void expressionParsed( bool isValid );
261 
262  private:
263  void runPythonCode( const QString& code );
264  void updateFunctionTree();
265  void fillFieldValues( const QString &fieldName, int countLimit );
266  QString loadFunctionHelp( QgsExpressionItem* functionName );
267  QString helpStylesheet() const;
268 
273  QString formatPreviewString( const QString &previewString ) const;
274 
275  void loadExpressionContext();
276 
277  QString mFunctionsPath;
278  QgsVectorLayer *mLayer;
279  QStandardItemModel *mModel;
280  QStringListModel *mValuesModel;
281  QSortFilterProxyModel *mProxyValues;
282  QgsExpressionItemSearchProxy *mProxyModel;
283  QMap<QString, QgsExpressionItem*> mExpressionGroups;
284  QgsFeature mFeature;
285  QgsExpressionHighlighter* highlighter;
286  bool mExpressionValid;
287  QgsDistanceArea mDa;
288  QString mRecentKey;
289  QMap<QString, QStringList> mFieldValues;
290  QgsExpressionContext mExpressionContext;
291 
292 };
293 
294 #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
int localeAwareCompare(const QString &other) const
QString getHelpText() const
Get the help text that is associated with this expression item.
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
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:176
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(const QString &label, const QString &expressionText, 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.
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.
void setHelpText(const QString &helpText)
Set the help text for the current item.