Quantum GIS API Documentation
1.8
|
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 00024 #include "QStandardItemModel" 00025 #include "QStandardItem" 00026 #include "QSortFilterProxyModel" 00027 00032 class QgsExpressionItemSearchProxy : public QSortFilterProxyModel 00033 { 00034 public: 00035 QgsExpressionItemSearchProxy() { } 00036 00037 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const 00038 { 00039 if ( source_parent == qobject_cast<QStandardItemModel*>( sourceModel() )->invisibleRootItem()->index() ) 00040 return true; 00041 00042 return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent ); 00043 } 00044 }; 00045 00048 class QgsExpressionItem : public QStandardItem 00049 { 00050 public: 00051 enum ItemType 00052 { 00053 Header, 00054 Field, 00055 ExpressionNode 00056 }; 00057 00058 QgsExpressionItem( QString label, 00059 QString expressionText, 00060 QString helpText, 00061 QgsExpressionItem::ItemType itemType = ExpressionNode ) 00062 : QStandardItem( label ) 00063 { 00064 mExpressionText = expressionText; 00065 mHelpText = helpText; 00066 mType = itemType; 00067 } 00068 00069 QgsExpressionItem( QString label, 00070 QString expressionText, 00071 QgsExpressionItem::ItemType itemType = ExpressionNode ) 00072 : QStandardItem( label ) 00073 { 00074 mExpressionText = expressionText; 00075 mType = itemType; 00076 } 00077 00078 QString getExpressionText() { return mExpressionText; } 00079 00084 QString getHelpText() { return mHelpText; } 00089 void setHelpText( QString helpText ) { mHelpText = helpText; } 00090 00095 QgsExpressionItem::ItemType getItemType() { return mType ; } 00096 00097 private: 00098 QString mExpressionText; 00099 QString mHelpText; 00100 QgsExpressionItem::ItemType mType; 00101 }; 00102 00106 class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase 00107 { 00108 Q_OBJECT 00109 public: 00110 QgsExpressionBuilderWidget( QWidget *parent ); 00111 ~QgsExpressionBuilderWidget(); 00112 00116 void setLayer( QgsVectorLayer* layer ); 00117 00121 void loadFieldNames(); 00122 00123 void loadFieldNames( QgsFieldMap fields ); 00124 00127 QString expressionText(); 00128 00130 void setExpressionText( const QString& expression ); 00131 00139 void registerItem( QString group, QString label, QString expressionText, 00140 QString helpText = "", 00141 QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode ); 00142 00143 bool isExpressionValid(); 00144 00145 public slots: 00146 void currentChanged( const QModelIndex &index, const QModelIndex & ); 00147 void on_expressionTree_doubleClicked( const QModelIndex &index ); 00148 void on_txtExpressionString_textChanged(); 00149 void on_txtSearchEdit_textChanged(); 00150 void on_lblPreview_linkActivated( QString link ); 00151 void on_mValueListWidget_itemDoubleClicked( QListWidgetItem* item ); 00152 void operatorButtonClicked(); 00153 void showContextMenu( const QPoint & ); 00154 void loadSampleValues(); 00155 void loadAllValues(); 00156 00157 private slots: 00158 void setExpressionState( bool state ); 00159 00160 signals: 00166 void expressionParsed( bool isValid ); 00167 00168 private: 00169 void fillFieldValues( int fieldIndex, int countLimit ); 00170 QString loadFunctionHelp( QgsExpressionItem* functionName ); 00171 00172 QgsVectorLayer *mLayer; 00173 QStandardItemModel *mModel; 00174 QgsExpressionItemSearchProxy *mProxyModel; 00175 QMap<QString, QgsExpressionItem*> mExpressionGroups; 00176 QgsFeature mFeature; 00177 QgsExpressionHighlighter* highlighter; 00178 bool mExpressionValid; 00179 }; 00180 00181 #endif // QGSEXPRESSIONBUILDER_H