Skip to content

Commit

Permalink
Port expression selection dialog to contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 0ed1644 commit 54f1b9b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion python/gui/qgsexpressionselectiondialog.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
56 changes: 31 additions & 25 deletions src/gui/qgsexpressionselectiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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 )
Expand All @@ -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();
}
Expand All @@ -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 )
Expand All @@ -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() );
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsexpressionselectiondialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/ui/qgsexpressionselectiondialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 54f1b9b

Please sign in to comment.