Skip to content
Permalink
Browse files

[processing] Correctly expose complete expression context for use

in Refactor Field expressions
  • Loading branch information
nyalldawson committed Jun 1, 2020
1 parent b4c3acd commit 4c2f2c09a2df497062bb647f48df8c80b591727b
@@ -124,6 +124,12 @@ Set source fields to ``sourceFields``
QgsExpressionContextGenerator *contextGenerator() const;
%Docstring
Returns the context generator with the source fields
%End

void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
%Docstring
Sets the base expression context ``generator``, which will generate the expression
contexts for expression based widgets used by the model.
%End

void setDestinationFields( const QgsFields &destinationFields,
@@ -92,6 +92,12 @@ corresponding expression.
void scrollTo( const QModelIndex &index ) const;
%Docstring
Scroll the fields view to ``index``
%End

void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
%Docstring
Register an expression context ``generator`` class that will be used to retrieve
an expression context for the widget.
%End

signals:
@@ -145,6 +145,11 @@ void QgsProcessingFieldMapPanelWidget::setValue( const QVariant &value )
emit changed();
}

void QgsProcessingFieldMapPanelWidget::registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
{
mFieldsView->registerExpressionContextGenerator( generator );
}

void QgsProcessingFieldMapPanelWidget::loadFieldsFromLayer()
{
if ( mLayer )
@@ -264,6 +269,7 @@ QWidget *QgsProcessingFieldMapWidgetWrapper::createWidget()
{
mPanel = new QgsProcessingFieldMapPanelWidget( nullptr );
mPanel->setToolTip( parameterDefinition()->toolTip() );
mPanel->registerExpressionContextGenerator( this );

connect( mPanel, &QgsProcessingFieldMapPanelWidget::changed, this, [ = ]
{
@@ -41,6 +41,12 @@ class GUI_EXPORT QgsProcessingFieldMapPanelWidget : public QgsPanelWidget, priva
QVariant value() const;
void setValue( const QVariant &value );

/**
* Register an expression context \a generator class that will be used to retrieve
* an expression context for the widget.
*/
void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );

signals:

void changed();
@@ -336,6 +336,11 @@ QgsExpressionContextGenerator *QgsFieldMappingModel::contextGenerator() const
return mExpressionContextGenerator.get();
}

void QgsFieldMappingModel::setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
{
mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
}

void QgsFieldMappingModel::setDestinationFields( const QgsFields &destinationFields,
const QMap<QString, QString> &expressions )
{
@@ -496,11 +501,27 @@ QgsFieldMappingModel::ExpressionContextGenerator::ExpressionContextGenerator( co

QgsExpressionContext QgsFieldMappingModel::ExpressionContextGenerator::createExpressionContext() const
{
QgsExpressionContext ctx;
ctx.appendScope( QgsExpressionContextUtils::globalScope() );
ctx.setFields( *mSourceFields );
QgsFeature feature { *mSourceFields };
feature.setValid( true );
ctx.setFeature( feature );
return ctx;
if ( mBaseGenerator )
{
QgsExpressionContext ctx = mBaseGenerator->createExpressionContext();
std::unique_ptr< QgsExpressionContextScope > fieldMappingScope = qgis::make_unique< QgsExpressionContextScope >( tr( "Field Mapping" ) );
fieldMappingScope->setFields( *mSourceFields );
ctx.appendScope( fieldMappingScope.release() );
return ctx;
}
else
{
QgsExpressionContext ctx;
ctx.appendScope( QgsExpressionContextUtils::globalScope() );
ctx.setFields( *mSourceFields );
QgsFeature feature { *mSourceFields };
feature.setValid( true );
ctx.setFeature( feature );
return ctx;
}
}

void QgsFieldMappingModel::ExpressionContextGenerator::setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
{
mBaseGenerator = generator;
}
@@ -131,6 +131,12 @@ class GUI_EXPORT QgsFieldMappingModel: public QAbstractTableModel
//! Returns the context generator with the source fields
QgsExpressionContextGenerator *contextGenerator() const;

/**
* Sets the base expression context \a generator, which will generate the expression
* contexts for expression based widgets used by the model.
*/
void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );

/**
* Set destination fields to \a destinationFields, initial values for the expressions can be
* optionally specified through \a expressions which is a map from the original
@@ -158,9 +164,12 @@ class GUI_EXPORT QgsFieldMappingModel: public QAbstractTableModel

// QgsExpressionContextGenerator interface
QgsExpressionContext createExpressionContext() const override;
void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );

private:

const QgsExpressionContextGenerator *mBaseGenerator = nullptr;

const QgsFields *mSourceFields;

};
@@ -101,6 +101,11 @@ void QgsFieldMappingWidget::scrollTo( const QModelIndex &index ) const
mTableView->scrollTo( index );
}

void QgsFieldMappingWidget::registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
{
model()->setBaseExpressionContextGenerator( generator );
}

void QgsFieldMappingWidget::appendField( const QgsField &field, const QString &expression )
{
model()->appendField( field, expression );
@@ -96,6 +96,12 @@ class GUI_EXPORT QgsFieldMappingWidget : public QgsPanelWidget, private Ui::QgsF
*/
void scrollTo( const QModelIndex &index ) const;

/**
* Register an expression context \a generator class that will be used to retrieve
* an expression context for the widget.
*/
void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );

signals:

/**

0 comments on commit 4c2f2c0

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