Skip to content

Commit

Permalink
Model child algorithms store a copy of the algorithm itself
Browse files Browse the repository at this point in the history
Instead of always retrieving it from the registry
  • Loading branch information
nyalldawson committed Jul 9, 2017
1 parent cd6e7d7 commit 7753ba1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down
42 changes: 39 additions & 3 deletions src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "qgsprocessingmodelcomponent.h"
#include "qgsprocessingmodelchildparametersource.h"
#include "qgsprocessingmodeloutput.h"
#include <memory>

class QgsProcessingModelAlgorithm;
class QgsProcessingAlgorithm;
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/core/processing/qgsprocessingalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down

0 comments on commit 7753ba1

Please sign in to comment.