Skip to content

Commit

Permalink
Add descriptions for model algorithm variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 7, 2017
1 parent 35e16b2 commit 3ea7069
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
9 changes: 7 additions & 2 deletions python/core/processing/qgsprocessingmodelalgorithm.sip
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,10 @@ Copies are protected to avoid slicing
%End
public:

VariableDefinition( const QVariant &value, const QgsProcessingModelAlgorithm::ChildParameterSource &source );
VariableDefinition( const QVariant &value, const QgsProcessingModelAlgorithm::ChildParameterSource &source, const QString &description = QString() );
%Docstring
Constructor for a new VariableDefinition with the specified ``value`` and original
parameter ``source``.
parameter ``source``, and ``description``.
%End

QVariant value;
Expand All @@ -903,6 +903,11 @@ Value of variable
QgsProcessingModelAlgorithm::ChildParameterSource source;
%Docstring
Original source of variable's value
%End

QString description;
%Docstring
Translated description of variable
%End
};

Expand Down
68 changes: 47 additions & 21 deletions src/core/processing/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,27 +678,37 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
QStringList() << QgsProcessingOutputNumber::typeName() );
Q_FOREACH ( const ChildParameterSource &source, sources )
{
QString name;
QVariant value;
QString description;
switch ( source.source() )
{
case ChildParameterSource::ModelParameter:
{
variables.insert( safeName( source.parameterName() ), VariableDefinition( modelParameters.value( source.parameterName() ), source ) );
continue;
name = source.parameterName();
value = modelParameters.value( source.parameterName() );
description = parameterDefinition( source.parameterName() )->description();
break;
}
case ChildParameterSource::ChildOutput:
{
QString name = QStringLiteral( "%1_%2" ).arg( mChildAlgorithms.value( source.outputChildId() ).description().isEmpty() ?
source.outputChildId() : mChildAlgorithms.value( source.outputChildId() ).description(), source.outputName() );
variables.insert( safeName( name ),
VariableDefinition( results.value( source.outputChildId() ).toMap().value( source.outputName() ),
source ) );
continue;
const ChildAlgorithm &child = mChildAlgorithms.value( source.outputChildId() );
name = QStringLiteral( "%1_%2" ).arg( child.description().isEmpty() ?
source.outputChildId() : child.description(), source.outputName() );
if ( const QgsProcessingAlgorithm *alg = child.algorithm() )
{
description = QObject::tr( "Output '%1' from algorithm '%2'" ).arg( alg->outputDefinition( source.outputName() )->description(),
child.description() );
}
value = results.value( source.outputChildId() ).toMap().value( source.outputName() );
break;
}

case ChildParameterSource::Expression:
case ChildParameterSource::StaticValue:
continue;
};
variables.insert( safeName( name ), VariableDefinition( value, source, description ) );
}

// layer sources
Expand All @@ -712,20 +722,28 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
{
QString name;
QVariant value;
QString description;

switch ( source.source() )
{
case ChildParameterSource::ModelParameter:
{
name = source.parameterName();
value = modelParameters.value( source.parameterName() );
description = parameterDefinition( source.parameterName() )->description();
break;
}
case ChildParameterSource::ChildOutput:
{
name = QStringLiteral( "%1_%2" ).arg( mChildAlgorithms.value( source.outputChildId() ).description().isEmpty() ?
source.outputChildId() : mChildAlgorithms.value( source.outputChildId() ).description(), source.outputName() );
const ChildAlgorithm &child = mChildAlgorithms.value( source.outputChildId() );
name = QStringLiteral( "%1_%2" ).arg( child.description().isEmpty() ?
source.outputChildId() : child.description(), source.outputName() );
value = results.value( source.outputChildId() ).toMap().value( source.outputName() );
if ( const QgsProcessingAlgorithm *alg = child.algorithm() )
{
description = QObject::tr( "Output '%1' from algorithm '%2'" ).arg( alg->outputDefinition( source.outputName() )->description(),
child.description() );
}
break;
}

Expand All @@ -739,10 +757,10 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
if ( !layer )
layer = QgsProcessingUtils::mapLayerFromString( value.toString(), context );

variables.insert( safeName( QStringLiteral( "%1_minx" ).arg( name ) ), VariableDefinition( layer ? layer->extent().xMinimum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_miny" ).arg( name ) ), VariableDefinition( layer ? layer->extent().yMinimum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_maxx" ).arg( name ) ), VariableDefinition( layer ? layer->extent().xMaximum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_maxy" ).arg( name ) ), VariableDefinition( layer ? layer->extent().yMaximum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_minx" ).arg( name ) ), VariableDefinition( layer ? layer->extent().xMinimum() : QVariant(), source, QObject::tr( "Minimum X of %1" ).arg( description ) ) );
variables.insert( safeName( QStringLiteral( "%1_miny" ).arg( name ) ), VariableDefinition( layer ? layer->extent().yMinimum() : QVariant(), source, QObject::tr( "Minimum Y of %1" ).arg( description ) ) );
variables.insert( safeName( QStringLiteral( "%1_maxx" ).arg( name ) ), VariableDefinition( layer ? layer->extent().xMaximum() : QVariant(), source, QObject::tr( "Maximum X of %1" ).arg( description ) ) );
variables.insert( safeName( QStringLiteral( "%1_maxy" ).arg( name ) ), VariableDefinition( layer ? layer->extent().yMaximum() : QVariant(), source, QObject::tr( "Maximum Y of %1" ).arg( description ) ) );

continue;
}
Expand All @@ -753,20 +771,28 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
{
QString name;
QVariant value;
QString description;

switch ( source.source() )
{
case ChildParameterSource::ModelParameter:
{
name = source.parameterName();
value = modelParameters.value( source.parameterName() );
description = parameterDefinition( source.parameterName() )->description();
break;
}
case ChildParameterSource::ChildOutput:
{
name = QStringLiteral( "%1_%2" ).arg( mChildAlgorithms.value( source.outputChildId() ).description().isEmpty() ?
source.outputChildId() : mChildAlgorithms.value( source.outputChildId() ).description(), source.outputName() );
const ChildAlgorithm &child = mChildAlgorithms.value( source.outputChildId() );
name = QStringLiteral( "%1_%2" ).arg( child.description().isEmpty() ?
source.outputChildId() : child.description(), source.outputName() );
value = results.value( source.outputChildId() ).toMap().value( source.outputName() );
if ( const QgsProcessingAlgorithm *alg = child.algorithm() )
{
description = QObject::tr( "Output '%1' from algorithm '%2'" ).arg( alg->outputDefinition( source.outputName() )->description(),
child.description() );
}
break;
}

Expand All @@ -792,10 +818,10 @@ QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingMode
featureSource = vl;
}

variables.insert( safeName( QStringLiteral( "%1_minx" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().xMinimum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_miny" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().yMinimum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_maxx" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().xMaximum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_maxy" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().yMaximum() : QVariant(), source ) );
variables.insert( safeName( QStringLiteral( "%1_minx" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().xMinimum() : QVariant(), source, QObject::tr( "Minimum X of %1" ).arg( description ) ) );
variables.insert( safeName( QStringLiteral( "%1_miny" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().yMinimum() : QVariant(), source, QObject::tr( "Minimum Y of %1" ).arg( description ) ) );
variables.insert( safeName( QStringLiteral( "%1_maxx" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().xMaximum() : QVariant(), source, QObject::tr( "Maximum X of %1" ).arg( description ) ) );
variables.insert( safeName( QStringLiteral( "%1_maxy" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->sourceExtent().yMaximum() : QVariant(), source, QObject::tr( "Maximum Y of %1" ).arg( description ) ) );
}

return variables;
Expand All @@ -808,7 +834,7 @@ QgsExpressionContextScope *QgsProcessingModelAlgorithm::createExpressionContextS
QMap< QString, QgsProcessingModelAlgorithm::VariableDefinition>::const_iterator varIt = variables.constBegin();
for ( ; varIt != variables.constEnd(); ++varIt )
{
scope->addVariable( QgsExpressionContextScope::StaticVariable( varIt.key(), varIt->value, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( varIt.key(), varIt->value, true, false, varIt->description ) );
}
return scope.release();
}
Expand Down
8 changes: 6 additions & 2 deletions src/core/processing/qgsprocessingmodelalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -878,18 +878,22 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm

/**
* Constructor for a new VariableDefinition with the specified \a value and original
* parameter \a source.
* parameter \a source, and \a description.
*/
VariableDefinition( const QVariant &value, const QgsProcessingModelAlgorithm::ChildParameterSource &source )
VariableDefinition( const QVariant &value, const QgsProcessingModelAlgorithm::ChildParameterSource &source, const QString &description = QString() )
: value( value )
, source( source )
, description( description )
{}

//! Value of variable
QVariant value;

//! Original source of variable's value
QgsProcessingModelAlgorithm::ChildParameterSource source;

//! Translated description of variable
QString description;
};

/**
Expand Down

0 comments on commit 3ea7069

Please sign in to comment.