Skip to content
Permalink
Browse files

Minor refactoring

  • Loading branch information
nyalldawson committed Feb 19, 2019
1 parent fd15c3e commit 892224c62b63af227ba73f39390891dc91e7a3f8
Showing with 71 additions and 44 deletions.
  1. +54 −44 src/gui/processing/qgsprocessingwidgetwrapper.cpp
  2. +17 −0 src/gui/processing/qgsprocessingwidgetwrapper.h
@@ -225,50 +225,7 @@ void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<Qg

QgsExpressionContext QgsAbstractProcessingParameterWidgetWrapper::createExpressionContext() const
{
// Get a processing context to start with
QgsProcessingContext *context = nullptr;
std::unique_ptr< QgsProcessingContext > tmpContext;
if ( mProcessingContextGenerator )
context = mProcessingContextGenerator->processingContext();

if ( !context )
{
tmpContext = qgis::make_unique< QgsProcessingContext >();
context = tmpContext.get();
}

QgsExpressionContext c = context->expressionContext();

if ( mWidgetContext.model() )
{
c << QgsExpressionContextUtils::processingModelAlgorithmScope( mWidgetContext.model(), QVariantMap(), *context );

const QgsProcessingAlgorithm *alg = nullptr;
if ( mWidgetContext.model()->childAlgorithms().contains( mWidgetContext.modelChildAlgorithmId() ) )
alg = mWidgetContext.model()->childAlgorithm( mWidgetContext.modelChildAlgorithmId() ).algorithm();

QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( alg ? alg : mParameterDefinition->algorithm(), QVariantMap(), *context );
c << algorithmScope;
QgsExpressionContextScope *childScope = mWidgetContext.model()->createExpressionContextScopeForChildAlgorithm( mWidgetContext.modelChildAlgorithmId(), *context, QVariantMap(), QVariantMap() );
c << childScope;

QStringList highlightedVariables = childScope->variableNames();
QStringList highlightedFunctions = childScope->functionNames();
highlightedVariables += algorithmScope->variableNames();
highlightedFunctions += algorithmScope->functionNames();
c.setHighlightedVariables( highlightedVariables );
c.setHighlightedFunctions( highlightedFunctions );
}
else
{
if ( mParameterDefinition->algorithm() )
c << QgsExpressionContextUtils::processingAlgorithmScope( mParameterDefinition->algorithm(), QVariantMap(), *context );
}

if ( linkedVectorLayer() )
c << QgsExpressionContextUtils::layerScope( linkedVectorLayer() );

return c;
return QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
}

void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstractProcessingParameterWidgetWrapper *wrapper )
@@ -332,3 +289,56 @@ QString QgsProcessingParameterWidgetFactoryInterface::modelerExpressionFormatStr
return QString();
}

//
// QgsProcessingGuiUtils
//

///@cond PRIVATE
QgsExpressionContext QgsProcessingGuiUtils::createExpressionContext( QgsProcessingContextGenerator *processingContextGenerator, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingAlgorithm *algorithm, const QgsVectorLayer *linkedLayer )
{
// Get a processing context to start with
QgsProcessingContext *context = nullptr;
std::unique_ptr< QgsProcessingContext > tmpContext;
if ( processingContextGenerator )
context = processingContextGenerator->processingContext();

if ( !context )
{
tmpContext = qgis::make_unique< QgsProcessingContext >();
context = tmpContext.get();
}

QgsExpressionContext c = context->expressionContext();

if ( widgetContext.model() )
{
c << QgsExpressionContextUtils::processingModelAlgorithmScope( widgetContext.model(), QVariantMap(), *context );

const QgsProcessingAlgorithm *alg = nullptr;
if ( widgetContext.model()->childAlgorithms().contains( widgetContext.modelChildAlgorithmId() ) )
alg = widgetContext.model()->childAlgorithm( widgetContext.modelChildAlgorithmId() ).algorithm();

QgsExpressionContextScope *algorithmScope = QgsExpressionContextUtils::processingAlgorithmScope( alg ? alg : algorithm, QVariantMap(), *context );
c << algorithmScope;
QgsExpressionContextScope *childScope = widgetContext.model()->createExpressionContextScopeForChildAlgorithm( widgetContext.modelChildAlgorithmId(), *context, QVariantMap(), QVariantMap() );
c << childScope;

QStringList highlightedVariables = childScope->variableNames();
QStringList highlightedFunctions = childScope->functionNames();
highlightedVariables += algorithmScope->variableNames();
highlightedFunctions += algorithmScope->functionNames();
c.setHighlightedVariables( highlightedVariables );
c.setHighlightedFunctions( highlightedFunctions );
}
else
{
if ( algorithm )
c << QgsExpressionContextUtils::processingAlgorithmScope( algorithm, QVariantMap(), *context );
}

if ( linkedLayer )
c << QgsExpressionContextUtils::layerScope( linkedLayer );

return c;
}
///@endcond
@@ -36,6 +36,7 @@ class QgsPropertyOverrideButton;
class QgsVectorLayer;
class QgsProcessingModelAlgorithm;
class QgsMapCanvas;
class QgsProcessingAlgorithm;

/**
* \class QgsProcessingContextGenerator
@@ -134,6 +135,22 @@ class GUI_EXPORT QgsProcessingParameterWidgetContext

};

#ifndef SIP_RUN
///@cond PRIVATE
class GUI_EXPORT QgsProcessingGuiUtils
{
public:

static QgsExpressionContext createExpressionContext( QgsProcessingContextGenerator *processingContextGenerator = nullptr,
const QgsProcessingParameterWidgetContext &widgetContext = QgsProcessingParameterWidgetContext(),
const QgsProcessingAlgorithm *algorithm = nullptr,
const QgsVectorLayer *linkedLayer = nullptr );


};
///@endcond
#endif

/**
* \class QgsAbstractProcessingParameterWidgetWrapper
*

0 comments on commit 892224c

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