Skip to content

Commit

Permalink
Add clone method to QgsProcessingModelComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent 16841ef commit d9cc2ae
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 0 deletions.
Expand Up @@ -32,6 +32,9 @@ should be set to a QgsProcessingAlgorithm algorithm ID.

QgsProcessingModelChildAlgorithm( const QgsProcessingModelChildAlgorithm &other );

virtual QgsProcessingModelChildAlgorithm *clone() /Factory/;


QString childId() const;
%Docstring
Returns the child algorithm's unique ID string, used the identify
Expand Down
Expand Up @@ -23,6 +23,8 @@ Represents a component of a model algorithm.
%End
public:

virtual ~QgsProcessingModelComponent();

QString description() const;
%Docstring
Returns the friendly description text for the component.
Expand All @@ -49,6 +51,13 @@ Returns the position of the model component within the graphical modeler.
Sets the ``position`` of the model component within the graphical modeler.

.. seealso:: :py:func:`position`
%End

virtual QgsProcessingModelComponent *clone() = 0 /Factory/;
%Docstring
Clones the component.

Ownership is transferred to the caller.
%End

protected:
Expand Down
Expand Up @@ -28,6 +28,9 @@ Represents a final output created by the model.
Constructor for QgsProcessingModelOutput with the specified ``name`` and ``description``.
%End

virtual QgsProcessingModelOutput *clone() /Factory/;


QString name() const;
%Docstring
Returns the model output name.
Expand Down
Expand Up @@ -30,6 +30,9 @@ Constructor for QgsProcessingModelParameter. The parameter name should match one
parameters from the parent model.
%End

virtual QgsProcessingModelParameter *clone() /Factory/;


QString parameterName() const;
%Docstring
Returns the associated parameter name. The parameter name should match one of the
Expand Down
Expand Up @@ -56,6 +56,11 @@ QgsProcessingModelChildAlgorithm &QgsProcessingModelChildAlgorithm::operator=( c
return *this;
}

QgsProcessingModelChildAlgorithm *QgsProcessingModelChildAlgorithm::clone()
{
return new QgsProcessingModelChildAlgorithm( *this );
}

const QgsProcessingAlgorithm *QgsProcessingModelChildAlgorithm::algorithm() const
{
return mAlgorithm.get();
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/models/qgsprocessingmodelchildalgorithm.h
Expand Up @@ -48,6 +48,8 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo
QgsProcessingModelChildAlgorithm( const QgsProcessingModelChildAlgorithm &other );
QgsProcessingModelChildAlgorithm &operator=( const QgsProcessingModelChildAlgorithm &other );

QgsProcessingModelChildAlgorithm *clone() override SIP_FACTORY;

/**
* Returns the child algorithm's unique ID string, used the identify
* this child algorithm within its parent model.
Expand Down
9 changes: 9 additions & 0 deletions src/core/processing/models/qgsprocessingmodelcomponent.h
Expand Up @@ -33,6 +33,8 @@ class CORE_EXPORT QgsProcessingModelComponent
{
public:

virtual ~QgsProcessingModelComponent() = default;

/**
* Returns the friendly description text for the component.
* \see setDescription()
Expand All @@ -57,6 +59,13 @@ class CORE_EXPORT QgsProcessingModelComponent
*/
void setPosition( QPointF position );

/**
* Clones the component.
*
* Ownership is transferred to the caller.
*/
virtual QgsProcessingModelComponent *clone() = 0 SIP_FACTORY;

protected:

//! Only subclasses can be created
Expand Down
5 changes: 5 additions & 0 deletions src/core/processing/models/qgsprocessingmodeloutput.cpp
Expand Up @@ -24,6 +24,11 @@ QgsProcessingModelOutput::QgsProcessingModelOutput( const QString &name, const Q
, mName( name )
{}

QgsProcessingModelOutput *QgsProcessingModelOutput::clone()
{
return new QgsProcessingModelOutput( *this );
}

QVariant QgsProcessingModelOutput::toVariant() const
{
QVariantMap map;
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/models/qgsprocessingmodeloutput.h
Expand Up @@ -39,6 +39,8 @@ class CORE_EXPORT QgsProcessingModelOutput : public QgsProcessingModelComponent
*/
QgsProcessingModelOutput( const QString &name = QString(), const QString &description = QString() );

QgsProcessingModelOutput *clone() override SIP_FACTORY;

/**
* Returns the model output name.
* \see setName()
Expand Down
5 changes: 5 additions & 0 deletions src/core/processing/models/qgsprocessingmodelparameter.cpp
Expand Up @@ -25,6 +25,11 @@ QgsProcessingModelParameter::QgsProcessingModelParameter( const QString &paramet

}

QgsProcessingModelParameter *QgsProcessingModelParameter::clone()
{
return new QgsProcessingModelParameter( *this );
}

QVariant QgsProcessingModelParameter::toVariant() const
{
QVariantMap map;
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/models/qgsprocessingmodelparameter.h
Expand Up @@ -40,6 +40,8 @@ class CORE_EXPORT QgsProcessingModelParameter : public QgsProcessingModelCompone
*/
QgsProcessingModelParameter( const QString &parameterName = QString() );

QgsProcessingModelParameter *clone() override SIP_FACTORY;

/**
* Returns the associated parameter name. The parameter name should match one of the
* parameters from the parent model.
Expand Down
7 changes: 7 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -7705,6 +7705,9 @@ void TestQgsProcessing::modelerAlgorithm()

QCOMPARE( child.asPythonCode( QgsProcessing::PythonQgsProcessingAlgorithmSubclass, extraParams, 4, 2, friendlyNames, friendlyOutputNames ).join( '\n' ), QStringLiteral( " # desc\n alg_params = {\n 'a': 5,\n 'b': [7,9],\n 'SOMETHING': SOMETHING_ELSE,\n 'SOMETHING2': SOMETHING_ELSE2\n }\n outputs['my_id'] = processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)" ) );

std::unique_ptr< QgsProcessingModelChildAlgorithm > childClone( child.clone() );
QCOMPARE( childClone->toVariant(), child.toVariant() );

QgsProcessingModelOutput testModelOut;
testModelOut.setChildId( QStringLiteral( "my_id" ) );
QCOMPARE( testModelOut.childId(), QStringLiteral( "my_id" ) );
Expand All @@ -7714,6 +7717,8 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( testModelOut.defaultValue().toString(), QStringLiteral( "my_value" ) );
testModelOut.setMandatory( true );
QVERIFY( testModelOut.isMandatory() );
std::unique_ptr< QgsProcessingModelOutput > outputClone( testModelOut.clone() );
QCOMPARE( outputClone->toVariant(), testModelOut.toVariant() );

QgsProcessingOutputLayerDefinition layerDef( QStringLiteral( "my_path" ) );
layerDef.createOptions["fileEncoding"] = QStringLiteral( "my_encoding" );
Expand Down Expand Up @@ -7841,6 +7846,8 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelParameter pc1;
pc1.setParameterName( QStringLiteral( "my_param" ) );
QCOMPARE( pc1.parameterName(), QStringLiteral( "my_param" ) );
std::unique_ptr< QgsProcessingModelParameter > paramClone( pc1.clone() );
QCOMPARE( paramClone->toVariant(), pc1.toVariant() );
pComponents.insert( QStringLiteral( "my_param" ), pc1 );
alg.setParameterComponents( pComponents );
QCOMPARE( alg.parameterComponents().count(), 1 );
Expand Down

0 comments on commit d9cc2ae

Please sign in to comment.