Skip to content
Permalink
Browse files

Port expression selection dialog to contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 0ed1644 commit 54f1b9bdd490c631d3d598b12edfb081cccf7554
@@ -39,7 +39,7 @@ class QgsExpressionSelectionDialog : QDialog
void on_mActionSelect_triggered();
void on_mActionAddToSelection_triggered();
void on_mActionRemoveFromSelection_triggered();
void on_mActionSelectInstersect_triggered();
void on_mActionSelectIntersect_triggered();
void on_mPbnClose_clicked();

protected:
@@ -30,12 +30,12 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer* laye
mActionSelect->setIcon( QgsApplication::getThemeIcon( "/mIconExpressionSelect.svg" ) );
mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectAdd.svg" ) );
mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectRemove.svg" ) );
mActionSelectInstersect->setIcon( QgsApplication::getThemeIcon( "/mIconSelectIntersect.svg" ) );
mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( "/mIconSelectIntersect.svg" ) );

mButtonSelect->addAction( mActionSelect );
mButtonSelect->addAction( mActionAddToSelection );
mButtonSelect->addAction( mActionRemoveFromSelection );
mButtonSelect->addAction( mActionSelectInstersect );
mButtonSelect->addAction( mActionSelectIntersect );
mButtonSelect->setDefaultAction( mActionSelect );

mExpressionBuilder->setLayer( layer );
@@ -73,19 +73,18 @@ void QgsExpressionSelectionDialog::on_mActionSelect_triggered()
QgsFeatureIds newSelection;
QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );

const QgsFields fields = mLayer->fields();
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

QgsFeatureIterator features = mLayer->getFeatures();

expression->prepare( fields );
QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() ).setExpressionContext( context );
QgsFeatureIterator features = mLayer->getFeatures( request );

QgsFeature feat;
while ( features.nextFeature( feat ) )
{
if ( expression->evaluate( &feat, fields ).toBool() )
{
newSelection << feat.id();
}
newSelection << feat.id();
}

features.close();
@@ -101,19 +100,18 @@ void QgsExpressionSelectionDialog::on_mActionAddToSelection_triggered()
QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );

const QgsFields fields = mLayer->fields();
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

QgsFeatureIterator features = mLayer->getFeatures();

expression->prepare( fields );
QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() ).setExpressionContext( context );
QgsFeatureIterator features = mLayer->getFeatures( request );

QgsFeature feat;
while ( features.nextFeature( feat ) )
{
if ( expression->evaluate( &feat, fields ).toBool() )
{
newSelection << feat.id();
}
newSelection << feat.id();
}

features.close();
@@ -124,16 +122,19 @@ void QgsExpressionSelectionDialog::on_mActionAddToSelection_triggered()
saveRecent();
}

void QgsExpressionSelectionDialog::on_mActionSelectInstersect_triggered()
void QgsExpressionSelectionDialog::on_mActionSelectIntersect_triggered()
{
const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
QgsFeatureIds newSelection;

QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );

const QgsFields fields = mLayer->fields();
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

expression->prepare( fields );
expression->prepare( &context );

QgsFeature feat;
foreach ( const QgsFeatureId fid, oldSelection )
@@ -142,7 +143,8 @@ void QgsExpressionSelectionDialog::on_mActionSelectInstersect_triggered()

if ( features.nextFeature( feat ) )
{
if ( expression->evaluate( &feat, fields ).toBool() )
context.setFeature( feat );
if ( expression->evaluate( &context ).toBool() )
{
newSelection << feat.id();
}
@@ -168,9 +170,12 @@ void QgsExpressionSelectionDialog::on_mActionRemoveFromSelection_triggered()

QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );

const QgsFields fields = mLayer->fields();
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

expression->prepare( fields );
expression->prepare( &context );

QgsFeature feat;
foreach ( const QgsFeatureId fid, oldSelection )
@@ -179,7 +184,8 @@ void QgsExpressionSelectionDialog::on_mActionRemoveFromSelection_triggered()

if ( features.nextFeature( feat ) )
{
if ( expression->evaluate( &feat, fields ).toBool() )
context.setFeature( feat );
if ( expression->evaluate( &context ).toBool() )
{
newSelection.remove( feat.id() );
}
@@ -66,7 +66,7 @@ class GUI_EXPORT QgsExpressionSelectionDialog : public QDialog, private Ui::QgsE
void on_mActionSelect_triggered();
void on_mActionAddToSelection_triggered();
void on_mActionRemoveFromSelection_triggered();
void on_mActionSelectInstersect_triggered();
void on_mActionSelectIntersect_triggered();
void on_mPbnClose_clicked();

protected:
@@ -75,7 +75,7 @@
<string>Remove from selection</string>
</property>
</action>
<action name="mActionSelectInstersect">
<action name="mActionSelectIntersect">
<property name="text">
<string>Select within selection</string>
</property>

0 comments on commit 54f1b9b

Please sign in to comment.
You can’t perform that action at this time.