Model child algorithms store a copy of the algorithm itself

Instead of always retrieving it from the registry
This commit is contained in:
Nyall Dawson 2017-07-09 17:17:29 +10:00
parent cd6e7d78cf
commit 7753ba11f7
5 changed files with 51 additions and 8 deletions

View File

@ -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

View File

@ -1031,7 +1031,7 @@ QSet<QString> 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() )

View File

@ -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<QString, QgsProcessingModelOutput> &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

View File

@ -23,6 +23,7 @@
#include "qgsprocessingmodelcomponent.h"
#include "qgsprocessingmodelchildparametersource.h"
#include "qgsprocessingmodeloutput.h"
#include <memory>
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

View File

@ -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;
/**