Skip to content
Permalink
Browse files

Add method to processing registry to create a new instance of an algo…

…rithm directly
  • Loading branch information
nyalldawson committed Jul 9, 2017
1 parent ad31087 commit cd6e7d78cfcdb7a793a86805999c2feb274ce3dd
@@ -85,6 +85,16 @@ class QgsProcessingRegistry : QObject
Finds an algorithm by its ID. If no matching algorithm is found, a None
is returned.
.. seealso:: algorithms()
.. seealso:: createAlgorithmById()
:rtype: QgsProcessingAlgorithm
%End

QgsProcessingAlgorithm *createAlgorithmById( const QString &id ) const /Factory/;
%Docstring
Creates a new instance of an algorithm by its ID. If no matching algorithm is found, a None
is returned. Callers take responsibility for deleting the returned object.`
.. seealso:: algorithms()
.. seealso:: algorithmById()
:rtype: QgsProcessingAlgorithm
%End

@@ -98,3 +98,14 @@ const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QStrin
return nullptr;
}

QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id ) const
{
const QgsProcessingAlgorithm *alg = algorithmById( id );
if ( !alg )
return nullptr;

std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create() );
creation->setProvider( alg->provider() );
return creation.release();
}

@@ -96,9 +96,18 @@ class CORE_EXPORT QgsProcessingRegistry : public QObject
* Finds an algorithm by its ID. If no matching algorithm is found, a nullptr
* is returned.
* \see algorithms()
* \see createAlgorithmById()
*/
const QgsProcessingAlgorithm *algorithmById( const QString &id ) const;

/**
* Creates a new instance of an algorithm by its ID. If no matching algorithm is found, a nullptr
* is returned. Callers take responsibility for deleting the returned object.`
* \see algorithms()
* \see algorithmById()
*/
QgsProcessingAlgorithm *createAlgorithmById( const QString &id ) const SIP_FACTORY;

signals:

//! Emitted when a provider has been added to the registry.
@@ -827,6 +827,17 @@ void TestQgsProcessing::algorithm()
QVERIFY( !r.algorithmById( "p1:alg3" ) );
QVERIFY( !r.algorithmById( "px:alg1" ) );

// createAlgorithmById
QVERIFY( !r.createAlgorithmById( "p1:alg3" ) );
std::unique_ptr< QgsProcessingAlgorithm > creation( r.createAlgorithmById( "p1:alg1" ) );
QVERIFY( creation.get() );
QCOMPARE( creation->provider()->id(), QStringLiteral( "p1" ) );
QCOMPARE( creation->id(), QStringLiteral( "p1:alg1" ) );
creation.reset( r.createAlgorithmById( "p1:alg2" ) );
QVERIFY( creation.get() );
QCOMPARE( creation->provider()->id(), QStringLiteral( "p1" ) );
QCOMPARE( creation->id(), QStringLiteral( "p1:alg2" ) );

//test that loading a provider triggers an algorithm refresh
DummyProvider *p2 = new DummyProvider( "p2" );
QVERIFY( p2->algorithms().isEmpty() );

0 comments on commit cd6e7d7

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