Skip to content
Permalink
Browse files

Fix generation of outputs for models

  • Loading branch information
nyalldawson committed Jun 21, 2017
1 parent fe0f249 commit 47f2cc895ccd8ba9b935bf50365450982b3a9e45
@@ -340,7 +340,11 @@ void QgsProcessingModelAlgorithm::updateDestinationParameters()
it.remove();
}
}
// also delete outputs
qDeleteAll( mOutputs );
mOutputs.clear();

// rebuild
QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
{
@@ -360,6 +364,13 @@ void QgsProcessingModelAlgorithm::updateDestinationParameters()
param->setName( outputIt->childId() + ':' + outputIt->outputName() );
param->setDescription( outputIt->description() );
addParameter( param );

if ( const QgsProcessingDestinationParameter *destParam = dynamic_cast< const QgsProcessingDestinationParameter *>( param ) )
{
QgsProcessingOutputDefinition *output = destParam->toOutputDefinition();
if ( output )
addOutput( output );
}
}
}
}
@@ -19,6 +19,7 @@
#include "qgsprocessingcontext.h"
#include "qgsprocessingutils.h"
#include "qgsvectorlayerfeatureiterator.h"
#include "qgsprocessingoutputs.h"

bool QgsProcessingParameters::isDynamic( const QVariantMap &parameters, const QString &name )
{
@@ -1998,7 +1999,7 @@ bool QgsProcessingParameterFeatureSource::fromVariantMap( const QVariantMap &map


QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
, mDataType( type )
{

@@ -2051,6 +2052,11 @@ QString QgsProcessingParameterFeatureSink::valueAsPythonString( const QVariant &
return value.toString().prepend( '\'' ).append( '\'' );
}

QgsProcessingOutputDefinition *QgsProcessingParameterFeatureSink::toOutputDefinition() const
{
return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
}

QgsProcessingParameterDefinition::LayerType QgsProcessingParameterFeatureSink::dataType() const
{
return mDataType;
@@ -2095,7 +2101,7 @@ bool QgsProcessingParameterFeatureSink::fromVariantMap( const QVariantMap &map )
}

QgsProcessingParameterRasterOutput::QgsProcessingParameterRasterOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
{}

bool QgsProcessingParameterRasterOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
@@ -2145,9 +2151,14 @@ QString QgsProcessingParameterRasterOutput::valueAsPythonString( const QVariant
return value.toString().prepend( '\'' ).append( '\'' );
}

QgsProcessingOutputDefinition *QgsProcessingParameterRasterOutput::toOutputDefinition() const
{
return new QgsProcessingOutputRasterLayer( name(), description() );
}


QgsProcessingParameterFileOutput::QgsProcessingParameterFileOutput( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
, mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
{

@@ -2202,6 +2213,11 @@ QString QgsProcessingParameterFileOutput::valueAsPythonString( const QVariant &v
return value.toString().prepend( '\'' ).append( '\'' );
}

QgsProcessingOutputDefinition *QgsProcessingParameterFileOutput::toOutputDefinition() const
{
return nullptr;
}

QString QgsProcessingParameterFileOutput::fileFilter() const
{
return mFileFilter;
@@ -2228,7 +2244,7 @@ bool QgsProcessingParameterFileOutput::fromVariantMap( const QVariantMap &map )
}

QgsProcessingParameterFolderOutput::QgsProcessingParameterFolderOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
: QgsProcessingDestinationParameter( name, description, defaultValue, optional )
{}

bool QgsProcessingParameterFolderOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
@@ -2251,6 +2267,17 @@ bool QgsProcessingParameterFolderOutput::checkValueIsAcceptable( const QVariant
return true;
}

QgsProcessingOutputDefinition *QgsProcessingParameterFolderOutput::toOutputDefinition() const
{
return nullptr;
}

QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
{

}

QgsProcessingParameterVectorOutput::QgsProcessingParameterVectorOutput( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mDataType( type )
@@ -30,7 +30,7 @@ class QgsRasterLayer;
class QgsVectorLayer;
class QgsFeatureSink;
class QgsFeatureSource;

class QgsProcessingOutputDefinition;

/**
* \class QgsProcessingFeatureSourceDefinition
@@ -1266,6 +1266,33 @@ class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingPara

};

/**
* \class QgsProcessingDestinationParameter
* \ingroup core
* Base class for all parameter definitions which represent file or layer destinations, e.g. parameters
* which are used for the destination for layers output by an algorithm.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingDestinationParameter : public QgsProcessingParameterDefinition
{
public:

/**
* Constructor for QgsProcessingDestinationParameter.
*/
QgsProcessingDestinationParameter( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false );

bool isDestination() const override { return true; }

/**
* Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination
* parameter.
*/
virtual QgsProcessingOutputDefinition *toOutputDefinition() const = 0 SIP_FACTORY;

};


/**
* \class QgsProcessingParameterFeatureSink
@@ -1275,7 +1302,7 @@ class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingPara
* A parameter which represents the destination feature sink for features created by an algorithm.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterFeatureSink : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterFeatureSink : public QgsProcessingDestinationParameter
{
public:

@@ -1286,9 +1313,9 @@ class CORE_EXPORT QgsProcessingParameterFeatureSink : public QgsProcessingParame
bool optional = false );

QString type() const override { return QStringLiteral( "sink" ); }
bool isDestination() const override { return true; }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;

/**
* Returns the layer type for sinks associated with the parameter.
@@ -1371,7 +1398,7 @@ class CORE_EXPORT QgsProcessingParameterVectorOutput : public QgsProcessingParam
* A raster layer output parameter.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterRasterOutput : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterRasterOutput : public QgsProcessingDestinationParameter
{
public:

@@ -1383,9 +1410,9 @@ class CORE_EXPORT QgsProcessingParameterRasterOutput : public QgsProcessingParam
bool optional = false );

QString type() const override { return QStringLiteral( "rasterOut" ); }
bool isDestination() const override { return true; }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;
};

/**
@@ -1394,7 +1421,7 @@ class CORE_EXPORT QgsProcessingParameterRasterOutput : public QgsProcessingParam
* A generic file based output parameter.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterFileOutput : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterFileOutput : public QgsProcessingDestinationParameter
{
public:

@@ -1407,9 +1434,9 @@ class CORE_EXPORT QgsProcessingParameterFileOutput : public QgsProcessingParamet
bool optional = false );

QString type() const override { return QStringLiteral( "fileOut" ); }
bool isDestination() const override { return true; }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;

/**
* Returns the file filter string for files compatible with this output.
@@ -1437,7 +1464,7 @@ class CORE_EXPORT QgsProcessingParameterFileOutput : public QgsProcessingParamet
* A folder output parameter.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterFolderOutput : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterFolderOutput : public QgsProcessingDestinationParameter
{
public:

@@ -1449,8 +1476,8 @@ class CORE_EXPORT QgsProcessingParameterFolderOutput : public QgsProcessingParam
bool optional = false );

QString type() const override { return QStringLiteral( "folderOut" ); }
bool isDestination() const override { return true; }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QgsProcessingOutputDefinition *toOutputDefinition() const override SIP_FACTORY;

};

@@ -3769,6 +3769,10 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( alg7.destinationParameterDefinitions().count(), 1 );
QCOMPARE( alg7.destinationParameterDefinitions().at( 0 )->name(), QStringLiteral( "cx1:OUTPUT_LAYER" ) );
QCOMPARE( alg7.destinationParameterDefinitions().at( 0 )->description(), QStringLiteral( "my output" ) );
QCOMPARE( alg7.outputDefinitions().count(), 1 );
QCOMPARE( alg7.outputDefinitions().at( 0 )->name(), QStringLiteral( "cx1:OUTPUT_LAYER" ) );
QCOMPARE( alg7.outputDefinitions().at( 0 )->type(), QStringLiteral( "outputVector" ) );
QCOMPARE( alg7.outputDefinitions().at( 0 )->description(), QStringLiteral( "my output" ) );

QgsProcessingModelAlgorithm::ChildAlgorithm alg7c2;
alg7c2.setChildId( "cx2" );
@@ -3787,12 +3791,22 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( alg7.destinationParameterDefinitions().at( 0 )->description(), QStringLiteral( "my output" ) );
QCOMPARE( alg7.destinationParameterDefinitions().at( 1 )->name(), QStringLiteral( "cx2:OUTPUT_LAYER" ) );
QCOMPARE( alg7.destinationParameterDefinitions().at( 1 )->description(), QStringLiteral( "my output2" ) );
QCOMPARE( alg7.outputDefinitions().count(), 2 );
QCOMPARE( alg7.outputDefinitions().at( 0 )->name(), QStringLiteral( "cx1:OUTPUT_LAYER" ) );
QCOMPARE( alg7.outputDefinitions().at( 0 )->type(), QStringLiteral( "outputVector" ) );
QCOMPARE( alg7.outputDefinitions().at( 0 )->description(), QStringLiteral( "my output" ) );
QCOMPARE( alg7.outputDefinitions().at( 1 )->name(), QStringLiteral( "cx2:OUTPUT_LAYER" ) );
QCOMPARE( alg7.outputDefinitions().at( 1 )->type(), QStringLiteral( "outputVector" ) );
QCOMPARE( alg7.outputDefinitions().at( 1 )->description(), QStringLiteral( "my output2" ) );

alg7.removeChildAlgorithm( "cx1" );
QCOMPARE( alg7.destinationParameterDefinitions().count(), 1 );
QCOMPARE( alg7.destinationParameterDefinitions().at( 0 )->name(), QStringLiteral( "cx2:OUTPUT_LAYER" ) );
QCOMPARE( alg7.destinationParameterDefinitions().at( 0 )->description(), QStringLiteral( "my output2" ) );

QCOMPARE( alg7.outputDefinitions().count(), 1 );
QCOMPARE( alg7.outputDefinitions().at( 0 )->name(), QStringLiteral( "cx2:OUTPUT_LAYER" ) );
QCOMPARE( alg7.outputDefinitions().at( 0 )->type(), QStringLiteral( "outputVector" ) );
QCOMPARE( alg7.outputDefinitions().at( 0 )->description(), QStringLiteral( "my output2" ) );
}

QGSTEST_MAIN( TestQgsProcessing )

0 comments on commit 47f2cc8

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