From 7753ba11f7fe74cbc19a96c61b3bee5214f350ae Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 9 Jul 2017 17:17:29 +1000 Subject: [PATCH] Model child algorithms store a copy of the algorithm itself Instead of always retrieving it from the registry --- .../qgsprocessingmodelchildalgorithm.sip | 2 + .../models/qgsprocessingmodelalgorithm.cpp | 2 +- .../qgsprocessingmodelchildalgorithm.cpp | 42 +++++++++++++++++-- .../models/qgsprocessingmodelchildalgorithm.h | 8 +++- src/core/processing/qgsprocessingalgorithm.h | 5 +-- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/python/core/processing/models/qgsprocessingmodelchildalgorithm.sip b/python/core/processing/models/qgsprocessingmodelchildalgorithm.sip index 212b3d1a3600..14a9dddd6571 100644 --- a/python/core/processing/models/qgsprocessingmodelchildalgorithm.sip +++ b/python/core/processing/models/qgsprocessingmodelchildalgorithm.sip @@ -29,6 +29,8 @@ class QgsProcessingModelChildAlgorithm : QgsProcessingModelComponent should be set to a QgsProcessingAlgorithm algorithm ID. %End + QgsProcessingModelChildAlgorithm( const QgsProcessingModelChildAlgorithm &other ); + QString childId() const; %Docstring Returns the child algorithm's unique ID string, used the identify diff --git a/src/core/processing/models/qgsprocessingmodelalgorithm.cpp b/src/core/processing/models/qgsprocessingmodelalgorithm.cpp index afcde6597bfc..845196d051d1 100644 --- a/src/core/processing/models/qgsprocessingmodelalgorithm.cpp +++ b/src/core/processing/models/qgsprocessingmodelalgorithm.cpp @@ -1031,7 +1031,7 @@ QSet QgsProcessingModelAlgorithm::dependentChildAlgorithms( const QStri void QgsProcessingModelAlgorithm::dependsOnChildAlgorithmsRecursive( const QString &childId, QSet< QString > &depends ) const { - QgsProcessingModelChildAlgorithm alg = mChildAlgorithms.value( childId ); + const QgsProcessingModelChildAlgorithm &alg = mChildAlgorithms.value( childId ); // add direct dependencies Q_FOREACH ( const QString &c, alg.dependencies() ) diff --git a/src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp b/src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp index 99d7659089af..76a28c55707e 100644 --- a/src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp +++ b/src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp @@ -23,14 +23,40 @@ ///@cond NOT_STABLE QgsProcessingModelChildAlgorithm::QgsProcessingModelChildAlgorithm( const QString &algorithmId ) - : mAlgorithmId( algorithmId ) { + setAlgorithmId( algorithmId ); +} +QgsProcessingModelChildAlgorithm::QgsProcessingModelChildAlgorithm( const QgsProcessingModelChildAlgorithm &other ) + : QgsProcessingModelComponent( other ) + , mId( other.mId ) + , mParams( other.mParams ) + , mModelOutputs( other.mModelOutputs ) + , mActive( other.mActive ) + , mDependencies( other.mDependencies ) + , mParametersCollapsed( other.mParametersCollapsed ) + , mOutputsCollapsed( other.mOutputsCollapsed ) +{ + setAlgorithmId( other.algorithmId() ); +} + +QgsProcessingModelChildAlgorithm &QgsProcessingModelChildAlgorithm::operator=( const QgsProcessingModelChildAlgorithm &other ) +{ + QgsProcessingModelComponent::operator =( other ); + mId = other.mId; + setAlgorithmId( other.algorithmId() ); + mParams = other.mParams; + mModelOutputs = other.mModelOutputs; + mActive = other.mActive; + mDependencies = other.mDependencies; + mParametersCollapsed = other.mParametersCollapsed; + mOutputsCollapsed = other.mOutputsCollapsed; + return *this; } const QgsProcessingAlgorithm *QgsProcessingModelChildAlgorithm::algorithm() const { - return QgsApplication::processingRegistry()->algorithmById( mAlgorithmId ); + return mAlgorithm.get(); } void QgsProcessingModelChildAlgorithm::setModelOutputs( const QMap &modelOutputs ) @@ -86,7 +112,7 @@ bool QgsProcessingModelChildAlgorithm::loadVariant( const QVariant &child ) QVariantMap map = child.toMap(); mId = map.value( QStringLiteral( "id" ) ).toString(); - mAlgorithmId = map.value( QStringLiteral( "alg_id" ) ).toString(); + setAlgorithmId( map.value( QStringLiteral( "alg_id" ) ).toString() ); mActive = map.value( QStringLiteral( "active" ) ).toBool(); mDependencies = map.value( QStringLiteral( "dependencies" ) ).toStringList(); mParametersCollapsed = map.value( QStringLiteral( "parameters_collapsed" ) ).toBool(); @@ -178,4 +204,14 @@ void QgsProcessingModelChildAlgorithm::generateChildId( const QgsProcessingModel mId = id; } +void QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmId ) +{ + mAlgorithmId = algorithmId; + mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId ) ); + if ( mAlgorithm ) + { + mAlgorithm->init( QVariantMap() ); + } +} + ///@endcond diff --git a/src/core/processing/models/qgsprocessingmodelchildalgorithm.h b/src/core/processing/models/qgsprocessingmodelchildalgorithm.h index 95cc5083bc4b..1b509a6b4e3d 100644 --- a/src/core/processing/models/qgsprocessingmodelchildalgorithm.h +++ b/src/core/processing/models/qgsprocessingmodelchildalgorithm.h @@ -23,6 +23,7 @@ #include "qgsprocessingmodelcomponent.h" #include "qgsprocessingmodelchildparametersource.h" #include "qgsprocessingmodeloutput.h" +#include class QgsProcessingModelAlgorithm; class QgsProcessingAlgorithm; @@ -44,6 +45,9 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo */ QgsProcessingModelChildAlgorithm( const QString &algorithmId = QString() ); + QgsProcessingModelChildAlgorithm( const QgsProcessingModelChildAlgorithm &other ); + QgsProcessingModelChildAlgorithm &operator=( const QgsProcessingModelChildAlgorithm &other ); + /** * Returns the child algorithm's unique ID string, used the identify * this child algorithm within its parent model. @@ -81,7 +85,7 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo * \see algorithm() * \see algorithmId() */ - void setAlgorithmId( const QString &algorithmId ) { mAlgorithmId = algorithmId; } + void setAlgorithmId( const QString &algorithmId ); /** * Returns the underlying child algorithm, or a nullptr @@ -231,6 +235,7 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo QString mId; QString mAlgorithmId; + std::unique_ptr< QgsProcessingAlgorithm > mAlgorithm; //! A map of parameter sources. Keys are algorithm parameter names. QMap< QString, QgsProcessingModelChildParameterSources > mParams; @@ -247,6 +252,7 @@ class CORE_EXPORT QgsProcessingModelChildAlgorithm : public QgsProcessingModelCo bool mParametersCollapsed = true; //! Whether list of outputs should be collapsed in the graphical modeller bool mOutputsCollapsed = true; + }; ///@endcond diff --git a/src/core/processing/qgsprocessingalgorithm.h b/src/core/processing/qgsprocessingalgorithm.h index 5fbaefff686f..35eb6351721d 100644 --- a/src/core/processing/qgsprocessingalgorithm.h +++ b/src/core/processing/qgsprocessingalgorithm.h @@ -62,10 +62,9 @@ class CORE_EXPORT QgsProcessingAlgorithm virtual ~QgsProcessingAlgorithm(); - - //! Algorithms cannot be copied - clone() should be used instead + //! Algorithms cannot be copied - create() should be used instead QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other ) = delete; - //! Algorithms cannot be copied- clone() should be used instead + //! Algorithms cannot be copied- create() should be used instead QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete; /**