Skip to content
Permalink
Browse files

Final porting to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 54f1b9b commit 2edc85c086edbd9767b1481c22a1ea0d95796a20
@@ -73,9 +73,12 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const

mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );

const QgsFields fields = mFilterModel->layer()->fields();

return mExpression->evaluate( &feat, fields );
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mFilterModel->layer() );
context.setFeature( feat );
return mExpression->evaluate( &context );
}

if ( role == FeatureInfoRole )
@@ -144,11 +147,14 @@ QgsAttributeTableModel* QgsFeatureListModel::masterModel()

bool QgsFeatureListModel::setDisplayExpression( const QString expression )
{
const QgsFields fields = mFilterModel->layer()->dataProvider()->fields();

QgsExpression* exp = new QgsExpression( expression );

exp->prepare( fields );
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mFilterModel->layer() );

exp->prepare( &context );

if ( exp->hasParserError() )
{
@@ -35,6 +35,11 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
{
setupUi( this );

//TODO - allow setting context for widget, so that preview has access to full context for expression
//and variables etc can be shown in builder widget
mExpressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();

mValueGroupBox->hide();
mLoadGroupBox->hide();
// highlighter = new QgsExpressionHighlighter( txtExpressionString->document() );
@@ -98,6 +103,12 @@ QgsExpressionBuilderWidget::~QgsExpressionBuilderWidget()
void QgsExpressionBuilderWidget::setLayer( QgsVectorLayer *layer )
{
mLayer = layer;

mExpressionContext = QgsExpressionContext();
mExpressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
if ( mLayer )
mExpressionContext << QgsExpressionContextUtils::layerScope( mLayer );
}

void QgsExpressionBuilderWidget::currentChanged( const QModelIndex &index, const QModelIndex & )
@@ -487,6 +498,7 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
}

QgsExpression exp( text );
mExpressionContext.setFeature( QgsFeature() );

if ( mLayer )
{
@@ -500,7 +512,8 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()

if ( mFeature.isValid() )
{
QVariant value = exp.evaluate( &mFeature, mLayer->fields() );
mExpressionContext.setFeature( mFeature );
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
lblPreview->setText( formatPreviewString( value.toString() ) );
}
@@ -514,7 +527,7 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
else
{
// No layer defined
QVariant value = exp.evaluate();
QVariant value = exp.evaluate( &mExpressionContext );
if ( !exp.hasEvalError() )
{
lblPreview->setText( formatPreviewString( value.toString() ) );
@@ -229,6 +229,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
QgsDistanceArea mDa;
QString mRecentKey;
QMap<QString, QStringList> mFieldValues;
QgsExpressionContext mExpressionContext;

};

@@ -289,7 +289,11 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
if ( exprIdx >= 0 )
{
QgsExpression exp( mExpression[exprIdx] );
exp.prepare( mLayer ? mLayer->fields() : QgsFields() );
QgsExpressionContext context;
if ( mLayer )
context.setFields( mLayer->fields() );

exp.prepare( &context );
return !exp.hasParserError();
}
return true;
@@ -330,7 +334,11 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
{
// if expression, test validity
QgsExpression exp( mExpression[exprIdx] );
exp.prepare( mLayer ? mLayer->fields() : QgsFields() );
QgsExpressionContext context;
if ( mLayer )
context.setFields( mLayer->fields() );

exp.prepare( &context );
if ( exp.hasParserError() )
{
return QBrush( QColor( Qt::red ) );

0 comments on commit 2edc85c

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