| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| /*************************************************************************** | ||
| qgisexpressionselectiondialog.cpp | ||
| -------------------------------------- | ||
| Date : 24.1.2013 | ||
| Copyright : (C) 2013 by Matthias kuhn | ||
| Email : matthias dot kuhn at gmx dot ch | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #include "qgsexpressionselectiondialog.h" | ||
| #include "qgsapplication.h" | ||
| #include "qgsexpression.h" | ||
|
|
||
| #include <QSettings> | ||
|
|
||
| QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer* layer, QString startText, QWidget* parent ) | ||
| : QDialog( parent ), | ||
| mLayer( layer ) | ||
| { | ||
| setupUi( this ); | ||
|
|
||
| mActionSelect->setIcon( QgsApplication::getThemeIcon( "/mIconExpressionSelect.svg" ) ); | ||
| mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectAdd.svg" ) ); | ||
| mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectRemove.svg" ) ); | ||
| mActionSelectInstersect->setIcon( QgsApplication::getThemeIcon( "/mIconSelectIntersect.svg" ) ); | ||
|
|
||
| mButtonSelect->addAction( mActionSelect ); | ||
| mButtonSelect->addAction( mActionAddToSelection ); | ||
| mButtonSelect->addAction( mActionRemoveFromSelection ); | ||
| mButtonSelect->addAction( mActionSelectInstersect ); | ||
| mButtonSelect->setDefaultAction( mActionSelect ); | ||
|
|
||
| mExpressionBuilder->setLayer( layer ); | ||
| mExpressionBuilder->setExpressionText( startText ); | ||
| mExpressionBuilder->loadFieldNames(); | ||
|
|
||
| QSettings settings; | ||
| restoreGeometry( settings.value( "/Windows/ExpressionSelectionDialog/geometry" ).toByteArray() ); | ||
| } | ||
|
|
||
| QgsExpressionBuilderWidget* QgsExpressionSelectionDialog::expressionBuilder() | ||
| { | ||
| return mExpressionBuilder; | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::setExpressionText( const QString& text ) | ||
| { | ||
| mExpressionBuilder->setExpressionText( text ); | ||
| } | ||
|
|
||
| QString QgsExpressionSelectionDialog::expressionText() | ||
| { | ||
| return mExpressionBuilder->expressionText(); | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::setGeomCalculator( const QgsDistanceArea & da ) | ||
| { | ||
| // Store in child widget only. | ||
| mExpressionBuilder->setGeomCalculator( da ); | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::on_mActionSelect_triggered() | ||
| { | ||
| QgsFeatureIds newSelection; | ||
| QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() ); | ||
|
|
||
| const QgsFields fields = mLayer->pendingFields(); | ||
|
|
||
| QgsFeatureIterator features = mLayer->getFeatures(); | ||
|
|
||
| expression->prepare( fields ); | ||
|
|
||
| QgsFeature feat; | ||
| while ( features.nextFeature( feat ) ) | ||
| { | ||
| if ( expression->evaluate( &feat, fields ).toBool() ) | ||
| { | ||
| newSelection << feat.id(); | ||
| } | ||
| } | ||
|
|
||
| features.close(); | ||
|
|
||
| mLayer->setSelectedFeatures( newSelection ); | ||
|
|
||
| delete expression; | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::on_mActionAddToSelection_triggered() | ||
| { | ||
| QgsFeatureIds newSelection = mLayer->selectedFeaturesIds(); | ||
| QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() ); | ||
|
|
||
| const QgsFields fields = mLayer->pendingFields(); | ||
|
|
||
| QgsFeatureIterator features = mLayer->getFeatures(); | ||
|
|
||
| expression->prepare( fields ); | ||
|
|
||
| QgsFeature feat; | ||
| while ( features.nextFeature( feat ) ) | ||
| { | ||
| if ( expression->evaluate( &feat, fields ).toBool() ) | ||
| { | ||
| newSelection << feat.id(); | ||
| } | ||
| } | ||
|
|
||
| features.close(); | ||
|
|
||
| mLayer->setSelectedFeatures( newSelection ); | ||
|
|
||
| delete expression; | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::on_mActionSelectInstersect_triggered() | ||
| { | ||
| const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds(); | ||
| QgsFeatureIds newSelection; | ||
|
|
||
| QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() ); | ||
|
|
||
| const QgsFields fields = mLayer->pendingFields(); | ||
|
|
||
| expression->prepare( fields ); | ||
|
|
||
| QgsFeature feat; | ||
| foreach( const QgsFeatureId fid, oldSelection ) | ||
| { | ||
| QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ); | ||
|
|
||
| if ( features.nextFeature( feat ) ) | ||
| { | ||
| if ( expression->evaluate( &feat, fields ).toBool() ) | ||
| { | ||
| newSelection << feat.id(); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Q_ASSERT( false ); | ||
| } | ||
|
|
||
| features.close(); | ||
| } | ||
|
|
||
| mLayer->setSelectedFeatures( newSelection ); | ||
|
|
||
| delete expression; | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::on_mActionRemoveFromSelection_triggered() | ||
| { | ||
| const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds(); | ||
| QgsFeatureIds newSelection = mLayer->selectedFeaturesIds(); | ||
|
|
||
| QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() ); | ||
|
|
||
| const QgsFields fields = mLayer->pendingFields(); | ||
|
|
||
| expression->prepare( fields ); | ||
|
|
||
| QgsFeature feat; | ||
| foreach( const QgsFeatureId fid, oldSelection ) | ||
| { | ||
| QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ); | ||
|
|
||
| if ( features.nextFeature( feat ) ) | ||
| { | ||
| if ( expression->evaluate( &feat, fields ).toBool() ) | ||
| { | ||
| newSelection.remove( feat.id() ); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Q_ASSERT( false ); | ||
| } | ||
|
|
||
| features.close(); | ||
| } | ||
|
|
||
| mLayer->setSelectedFeatures( newSelection ); | ||
|
|
||
| delete expression; | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::closeEvent( QCloseEvent *closeEvent ) | ||
| { | ||
| QDialog::closeEvent( closeEvent ); | ||
|
|
||
| QSettings settings; | ||
| settings.setValue( "/Windows/ExpressionSelectionDialog/geometry", saveGeometry() ); | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::on_mPbnClose_clicked() | ||
| { | ||
| close(); | ||
| } | ||
|
|
||
| void QgsExpressionSelectionDialog::done( int r ) | ||
| { | ||
| QDialog::done( r ); | ||
| close(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /*************************************************************************** | ||
| qgisexpressionselectiondialog.h | ||
| -------------------------------------- | ||
| Date : 24.1.2013 | ||
| Copyright : (C) 2013 by Matthias kuhn | ||
| Email : matthias dot kuhn at gmx dot ch | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #ifndef QGSEXPRESSIONSELECTIONDIALOG_H | ||
| #define QGSEXPRESSIONSELECTIONDIALOG_H | ||
|
|
||
| #include <QDialog> | ||
| #include "qgsdistancearea.h" | ||
| #include "ui_qgsexpressionselectiondialogbase.h" | ||
|
|
||
| /** | ||
| * This class offers a dialog to change feature selections. | ||
| * To do so, a QgsExpressionBuilderWidget is shown in a dialog. | ||
| * It offers the possibilities to create a new selection, add to the current selection | ||
| * remove from the current selection or select within the current selection. | ||
| * @note added in 2.0 | ||
| */ | ||
| class GUI_EXPORT QgsExpressionSelectionDialog : public QDialog, private Ui::QgsExpressionSelectionDialogBase | ||
| { | ||
| Q_OBJECT | ||
|
|
||
| public: | ||
| /** | ||
| * Creates a new selection dialog. | ||
| * @param layer The layer on which the selection is to be performed. | ||
| * @param startText A default expression text to be applied (Defaults to empty) | ||
| */ | ||
| QgsExpressionSelectionDialog( QgsVectorLayer* layer, QString startText = QString(), QWidget* parent = NULL ); | ||
|
|
||
| /** | ||
| * The builder widget that is used by the dialog | ||
| * @return The builder widget that is used by the dialog | ||
| */ | ||
| QgsExpressionBuilderWidget* expressionBuilder(); | ||
|
|
||
| /** | ||
| * Sets the current expression text | ||
| * @param text the expression text to set | ||
| */ | ||
| void setExpressionText( const QString& text ); | ||
|
|
||
| /** | ||
| * Returns the current expression text | ||
| * @return The expression text | ||
| */ | ||
| QString expressionText(); | ||
|
|
||
| /** | ||
| *Sets geometry calculator used in distance/area calculations. | ||
| */ | ||
| void setGeomCalculator( const QgsDistanceArea & da ); | ||
|
|
||
| public slots: | ||
| void on_mActionSelect_triggered(); | ||
| void on_mActionAddToSelection_triggered(); | ||
| void on_mActionRemoveFromSelection_triggered(); | ||
| void on_mActionSelectInstersect_triggered(); | ||
| void on_mPbnClose_clicked(); | ||
|
|
||
| protected: | ||
| /** | ||
| * Implementation for closeEvent | ||
| * Saves the window geometry | ||
| * @param closeEvent Event object. Unused. | ||
| */ | ||
| virtual void closeEvent( QCloseEvent *closeEvent ); | ||
|
|
||
| /** | ||
| * Implementation for done (default behavior when pressing esc) | ||
| * Calls close, so the window geometry gets saved and the object deleted. | ||
| * @param r Result value. Unused. | ||
| */ | ||
| virtual void done( int r ); | ||
|
|
||
| private: | ||
| QgsVectorLayer* mLayer; | ||
| }; | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ui version="4.0"> | ||
| <class>QgsExpressionSelectionDialogBase</class> | ||
| <widget class="QDialog" name="QgsExpressionSelectionDialogBase"> | ||
| <property name="geometry"> | ||
| <rect> | ||
| <x>0</x> | ||
| <y>0</y> | ||
| <width>536</width> | ||
| <height>401</height> | ||
| </rect> | ||
| </property> | ||
| <property name="windowTitle"> | ||
| <string>Dialog</string> | ||
| </property> | ||
| <layout class="QGridLayout" name="gridLayout"> | ||
| <item row="1" column="0"> | ||
| <spacer name="horizontalSpacer"> | ||
| <property name="orientation"> | ||
| <enum>Qt::Horizontal</enum> | ||
| </property> | ||
| <property name="sizeHint" stdset="0"> | ||
| <size> | ||
| <width>40</width> | ||
| <height>20</height> | ||
| </size> | ||
| </property> | ||
| </spacer> | ||
| </item> | ||
| <item row="1" column="2"> | ||
| <widget class="QPushButton" name="mPbnClose"> | ||
| <property name="text"> | ||
| <string>Close</string> | ||
| </property> | ||
| </widget> | ||
| </item> | ||
| <item row="1" column="1"> | ||
| <widget class="QToolButton" name="mButtonSelect"> | ||
| <property name="minimumSize"> | ||
| <size> | ||
| <width>0</width> | ||
| <height>31</height> | ||
| </size> | ||
| </property> | ||
| <property name="text"> | ||
| <string>...</string> | ||
| </property> | ||
| <property name="popupMode"> | ||
| <enum>QToolButton::MenuButtonPopup</enum> | ||
| </property> | ||
| <property name="toolButtonStyle"> | ||
| <enum>Qt::ToolButtonTextBesideIcon</enum> | ||
| </property> | ||
| </widget> | ||
| </item> | ||
| <item row="0" column="0" colspan="4"> | ||
| <widget class="QgsExpressionBuilderWidget" name="mExpressionBuilder" native="true"/> | ||
| </item> | ||
| </layout> | ||
| <action name="mActionSelect"> | ||
| <property name="text"> | ||
| <string>Select</string> | ||
| </property> | ||
| </action> | ||
| <action name="mActionAddToSelection"> | ||
| <property name="text"> | ||
| <string>Add to selection</string> | ||
| </property> | ||
| </action> | ||
| <action name="mActionRemoveFromSelection"> | ||
| <property name="text"> | ||
| <string>Remove from selection</string> | ||
| </property> | ||
| </action> | ||
| <action name="mActionSelectInstersect"> | ||
| <property name="text"> | ||
| <string>Select within selection</string> | ||
| </property> | ||
| </action> | ||
| </widget> | ||
| <customwidgets> | ||
| <customwidget> | ||
| <class>QgsExpressionBuilderWidget</class> | ||
| <extends>QWidget</extends> | ||
| <header>qgsexpressionbuilderwidget.h</header> | ||
| <container>1</container> | ||
| </customwidget> | ||
| </customwidgets> | ||
| <resources/> | ||
| <connections/> | ||
| </ui> |