Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 19, 2019
1 parent fd15c3e commit 892224c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 44 deletions.
98 changes: 54 additions & 44 deletions src/gui/processing/qgsprocessingwidgetwrapper.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.h
Expand Up @@ -36,6 +36,7 @@ class QgsPropertyOverrideButton;
class QgsVectorLayer;
class QgsProcessingModelAlgorithm;
class QgsMapCanvas;
class QgsProcessingAlgorithm;

/**
* \class QgsProcessingContextGenerator
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 892224c

Please sign in to comment.