Skip to content

Commit

Permalink
Add method to processing registry to create a new instance of an algo…
Browse files Browse the repository at this point in the history
…rithm directly
  • Loading branch information
nyalldawson committed Jul 9, 2017
1 parent ad31087 commit cd6e7d7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/core/processing/qgsprocessingregistry.sip
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions src/core/processing/qgsprocessingregistry.cpp
Expand Up @@ -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();
}

9 changes: 9 additions & 0 deletions src/core/processing/qgsprocessingregistry.h
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -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() );
Expand Down

0 comments on commit cd6e7d7

Please sign in to comment.