Skip to content
Permalink
Browse files

Use feature from expression context in expression builder preview

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 a13ef7bdc1210fdad6e7827ba2ccba8fd363423c
Showing with 9 additions and 24 deletions.
  1. +9 −23 src/gui/qgsexpressionbuilderwidget.cpp
  2. +0 −1 src/gui/qgsexpressionbuilderwidget.h
@@ -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() )
@@ -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;

1 comment on commit a13ef7b

@NathanW2

This comment has been minimized.

Copy link
Member

@NathanW2 NathanW2 commented on a13ef7b Jan 12, 2017

Nice fix.

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