Skip to content

Commit

Permalink
Use feature from expression context in expression builder preview
Browse files Browse the repository at this point in the history
instead of always getting first feature from the layer

This means that e.g. expression builder previews in composer atlas
will use the current atlas preview feature

Also remove an unnecessary restriction that blocks the preview if
a layer has no features
  • Loading branch information
nyalldawson committed Jan 12, 2017
1 parent 55f6b11 commit a13ef7b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
32 changes: 9 additions & 23 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -543,33 +543,19 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
// Only set calculator if we have layer, else use default.
exp.setGeomCalculator( &mDa );

if ( !mFeature.isValid() )
if ( !mExpressionContext.feature().isValid() )
{
mLayer->getFeatures().nextFeature( mFeature );
}

if ( mFeature.isValid() )
{
mExpressionContext.setFeature( mFeature );
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
lblPreview->setText( QgsExpression::formatPreviewString( value ) );
}
else
{
// The feature is invalid because we don't have one but that doesn't mean user can't
// build a expression string. They just get no preview.
lblPreview->setText( QLatin1String( "" ) );
// no feature passed yet, try to get from layer
QgsFeature f;
mLayer->getFeatures().nextFeature( f );
mExpressionContext.setFeature( f );
}
}
else

QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
{
// No layer defined
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
{
lblPreview->setText( QgsExpression::formatPreviewString( value ) );
}
lblPreview->setText( QgsExpression::formatPreviewString( value ) );
}

if ( exp.hasParserError() || exp.hasEvalError() )
Expand Down
1 change: 0 additions & 1 deletion src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -307,7 +307,6 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QSortFilterProxyModel *mProxyValues;
QgsExpressionItemSearchProxy *mProxyModel;
QMap<QString, QgsExpressionItem*> mExpressionGroups;
QgsFeature mFeature;
QgsExpressionHighlighter* highlighter;
bool mExpressionValid;
QgsDistanceArea mDa;
Expand Down

1 comment on commit a13ef7b

@NathanW2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix.

Please sign in to comment.