Skip to content

Commit

Permalink
[processing][api] Add mechanism to get basic algorithm properties
Browse files Browse the repository at this point in the history
efficiently for many calls

Allows retrieval of some basic algorithm properties (display name,
icon) in an optimised way for many calls. (Avoiding multiple
expensive algorithmById lookups)
  • Loading branch information
nyalldawson committed Apr 20, 2023
1 parent 2f8bb3c commit 70f2adb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
34 changes: 34 additions & 0 deletions python/core/auto_generated/processing/qgsprocessingregistry.sip.in
Expand Up @@ -10,6 +10,30 @@



class QgsProcessingAlgorithmInformation
{
%Docstring(signature="appended")
*************************************************************************

This program is free software; you can redistribute it and/or modify *
it under the terms of the GNU General Public License as published by *
the Free Software Foundation; either version 2 of the License, or *
(at your option) any later version. *

**************************************************************************
%End

%TypeHeaderCode
#include "qgsprocessingregistry.h"
%End
public:

QString displayName;

QIcon icon;
};


class QgsProcessingRegistry : QObject
{
%Docstring(signature="appended")
Expand Down Expand Up @@ -78,6 +102,16 @@ Returns a matching provider by provider ID.
Returns a list of all available algorithms from registered providers.

.. seealso:: :py:func:`algorithmById`
%End

QgsProcessingAlgorithmInformation algorithmInformation( const QString &id ) const;
%Docstring
Returns basic algorithm information for the algorithm with matching ID.

This method uses an internal cache to ensure that information is quickly
returned and is suitable for calling many times.

.. versionadded:: 3.32
%End

const QgsProcessingAlgorithm *algorithmById( const QString &id ) const;
Expand Down
26 changes: 26 additions & 0 deletions src/core/processing/qgsprocessingregistry.cpp
Expand Up @@ -116,6 +116,13 @@ bool QgsProcessingRegistry::addProvider( QgsProcessingProvider *provider )

provider->setParent( this );
mProviders[ provider->id()] = provider;

mCachedInformation.clear();
connect( provider, &QgsProcessingProvider::algorithmsLoaded, this, [this]
{
mCachedInformation.clear();
} );

emit providerAdded( provider->id() );
return true;
}
Expand All @@ -133,6 +140,9 @@ bool QgsProcessingRegistry::removeProvider( QgsProcessingProvider *provider )
provider->unload();

delete mProviders.take( id );

mCachedInformation.clear();

emit providerRemoved( id );
return true;
}
Expand All @@ -159,6 +169,22 @@ QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() cons
return algs;
}

QgsProcessingAlgorithmInformation QgsProcessingRegistry::algorithmInformation( const QString &id ) const
{
const auto it = mCachedInformation.constFind( id );
if ( it != mCachedInformation.constEnd() )
return *it;

QgsProcessingAlgorithmInformation info;
if ( const QgsProcessingAlgorithm *algorithm = algorithmById( id ) )
{
info.displayName = algorithm->displayName();
info.icon = algorithm->icon();
}
mCachedInformation.insert( id, info );
return info;
}

const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QString &constId ) const
{
// allow mapping of algorithm via registered algorithm aliases
Expand Down
26 changes: 26 additions & 0 deletions src/core/processing/qgsprocessingregistry.h
Expand Up @@ -26,6 +26,18 @@
class QgsProcessingParameterType;
class QgsProcessingAlgorithmConfigurationWidgetFactory;

class CORE_EXPORT QgsProcessingAlgorithmInformation
{
public:

//! Algorithm display name
QString displayName;

//! Algorithm icon
QIcon icon;
};


/**
* \class QgsProcessingRegistry
* \ingroup core
Expand Down Expand Up @@ -95,6 +107,16 @@ class CORE_EXPORT QgsProcessingRegistry : public QObject
*/
QList< const QgsProcessingAlgorithm *> algorithms() const;

/**
* Returns basic algorithm information for the algorithm with matching ID.
*
* This method uses an internal cache to ensure that information is quickly
* returned and is suitable for calling many times.
*
* \since QGIS 3.32
*/
QgsProcessingAlgorithmInformation algorithmInformation( const QString &id ) const;

/**
* Finds an algorithm by its ID. If no matching algorithm is found, NULLPTR
* is returned.
Expand Down Expand Up @@ -215,9 +237,13 @@ class CORE_EXPORT QgsProcessingRegistry : public QObject

QMap< QString, QString > mAlgorithmAliases;

mutable QMap< QString, QgsProcessingAlgorithmInformation > mCachedInformation;

#ifdef SIP_RUN
QgsProcessingRegistry( const QgsProcessingRegistry &other );
#endif

friend class TestQgsProcessing;
};

#endif // QGSPROCESSINGREGISTRY_H
Expand Down
19 changes: 19 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -1693,6 +1693,10 @@ void TestQgsProcessing::algorithm()
QVERIFY( !p->algorithms().empty() );

QgsProcessingRegistry r;

QgsProcessingAlgorithmInformation info = r.algorithmInformation( QStringLiteral( "p1:alg1" ) );
QVERIFY( info.displayName.isEmpty() );

QVERIFY( r.addProvider( p ) );
QCOMPARE( r.algorithms().size(), 2 );
QVERIFY( r.algorithms().contains( p->algorithm( "alg1" ) ) );
Expand All @@ -1704,6 +1708,13 @@ void TestQgsProcessing::algorithm()
QVERIFY( !r.algorithmById( "p1:alg3" ) );
QVERIFY( !r.algorithmById( "px:alg1" ) );

info = r.algorithmInformation( QStringLiteral( "p1:alg1" ) );
QCOMPARE( info.displayName, QStringLiteral( "alg1" ) );
info = r.algorithmInformation( QStringLiteral( "p1:alg2" ) );
QCOMPARE( info.displayName, QStringLiteral( "alg2" ) );
info = r.algorithmInformation( QStringLiteral( "p1:alg3" ) );
QVERIFY( info.displayName.isEmpty() );

// alias support
QVERIFY( !r.algorithmById( QStringLiteral( "fake:fakealg" ) ) );
r.addAlgorithmAlias( QStringLiteral( "fake:fakealg" ), QStringLiteral( "nope:none" ) );
Expand Down Expand Up @@ -1739,7 +1750,15 @@ void TestQgsProcessing::algorithm()
DummyProvider *p3 = new DummyProvider( "p3" );
QVERIFY( p3->algorithms().isEmpty() );
QVERIFY( r.addProvider( p3 ) );
QVERIFY( r.mCachedInformation.empty() );
QCOMPARE( p3->algorithms().size(), 2 );

info = r.algorithmInformation( QStringLiteral( "p3:alg2" ) );
QCOMPARE( info.displayName, QStringLiteral( "alg2" ) );

// a provider refresh should clear the registry's cache
p3->refreshAlgorithms();
QVERIFY( r.mCachedInformation.empty() );
}

void TestQgsProcessing::features()
Expand Down

0 comments on commit 70f2adb

Please sign in to comment.