|
QGIS API Documentation
master-59fd5e0
|
00001 /*************************************************************************** 00002 qgisexpressionbuilderwidget.h - A genric expression string builder widget. 00003 -------------------------------------- 00004 Date : 29-May-2011 00005 Copyright : (C) 2011 by Nathan Woodrow 00006 Email : woodrow.nathan at gmail dot com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 00016 #ifndef QGSEXPRESSIONBUILDER_H 00017 #define QGSEXPRESSIONBUILDER_H 00018 00019 #include <QWidget> 00020 #include "ui_qgsexpressionbuilder.h" 00021 #include "qgsvectorlayer.h" 00022 #include "qgsexpressionhighlighter.h" 00023 #include "qgsdistancearea.h" 00024 00025 #include "QStandardItemModel" 00026 #include "QStandardItem" 00027 #include "QSortFilterProxyModel" 00028 00033 class QgsExpressionItemSearchProxy : public QSortFilterProxyModel 00034 { 00035 public: 00036 QgsExpressionItemSearchProxy() { } 00037 00038 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const 00039 { 00040 if ( source_parent == qobject_cast<QStandardItemModel*>( sourceModel() )->invisibleRootItem()->index() ) 00041 return true; 00042 00043 return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent ); 00044 } 00045 }; 00046 00049 class QgsExpressionItem : public QStandardItem 00050 { 00051 public: 00052 enum ItemType 00053 { 00054 Header, 00055 Field, 00056 ExpressionNode 00057 }; 00058 00059 QgsExpressionItem( QString label, 00060 QString expressionText, 00061 QString helpText, 00062 QgsExpressionItem::ItemType itemType = ExpressionNode ) 00063 : QStandardItem( label ) 00064 { 00065 mExpressionText = expressionText; 00066 mHelpText = helpText; 00067 mType = itemType; 00068 } 00069 00070 QgsExpressionItem( QString label, 00071 QString expressionText, 00072 QgsExpressionItem::ItemType itemType = ExpressionNode ) 00073 : QStandardItem( label ) 00074 { 00075 mExpressionText = expressionText; 00076 mType = itemType; 00077 } 00078 00079 QString getExpressionText() { return mExpressionText; } 00080 00085 QString getHelpText() { return mHelpText; } 00090 void setHelpText( QString helpText ) { mHelpText = helpText; } 00091 00096 QgsExpressionItem::ItemType getItemType() { return mType; } 00097 00098 private: 00099 QString mExpressionText; 00100 QString mHelpText; 00101 QgsExpressionItem::ItemType mType; 00102 }; 00103 00107 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase 00108 { 00109 Q_OBJECT 00110 public: 00111 QgsExpressionBuilderWidget( QWidget *parent ); 00112 ~QgsExpressionBuilderWidget(); 00113 00117 void setLayer( QgsVectorLayer* layer ); 00118 00122 void loadFieldNames(); 00123 00124 void loadFieldNames( const QgsFields& fields ); 00125 00129 void setGeomCalculator( const QgsDistanceArea & da ); 00130 00133 QString expressionText(); 00134 00136 void setExpressionText( const QString& expression ); 00137 00145 void registerItem( QString group, QString label, QString expressionText, 00146 QString helpText = "", 00147 QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode ); 00148 00149 bool isExpressionValid(); 00150 00151 public slots: 00152 void currentChanged( const QModelIndex &index, const QModelIndex & ); 00153 void on_expressionTree_doubleClicked( const QModelIndex &index ); 00154 void on_txtExpressionString_textChanged(); 00155 void on_txtSearchEdit_textChanged(); 00156 void on_lblPreview_linkActivated( QString link ); 00157 void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item ); 00158 void operatorButtonClicked(); 00159 void showContextMenu( const QPoint & ); 00160 void loadSampleValues(); 00161 void loadAllValues(); 00162 00163 private slots: 00164 void setExpressionState( bool state ); 00165 00166 signals: 00172 void expressionParsed( bool isValid ); 00173 00174 private: 00175 void fillFieldValues( int fieldIndex, int countLimit ); 00176 QString loadFunctionHelp( QgsExpressionItem* functionName ); 00177 00178 QgsVectorLayer *mLayer; 00179 QStandardItemModel *mModel; 00180 QgsExpressionItemSearchProxy *mProxyModel; 00181 QMap<QString, QgsExpressionItem*> mExpressionGroups; 00182 QgsFeature mFeature; 00183 QgsExpressionHighlighter* highlighter; 00184 bool mExpressionValid; 00185 QgsDistanceArea mDa; 00186 }; 00187 00188 #endif // QGSEXPRESSIONBUILDER_H