Skip to content

Commit 70f2adb

Browse files
committedApr 20, 2023
[processing][api] Add mechanism to get basic algorithm properties
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)
1 parent 2f8bb3c commit 70f2adb

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
 

‎python/core/auto_generated/processing/qgsprocessingregistry.sip.in

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@
1010

1111

1212

13+
class QgsProcessingAlgorithmInformation
14+
{
15+
%Docstring(signature="appended")
16+
*************************************************************************
17+
18+
This program is free software; you can redistribute it and/or modify *
19+
it under the terms of the GNU General Public License as published by *
20+
the Free Software Foundation; either version 2 of the License, or *
21+
(at your option) any later version. *
22+
23+
**************************************************************************
24+
%End
25+
26+
%TypeHeaderCode
27+
#include "qgsprocessingregistry.h"
28+
%End
29+
public:
30+
31+
QString displayName;
32+
33+
QIcon icon;
34+
};
35+
36+
1337
class QgsProcessingRegistry : QObject
1438
{
1539
%Docstring(signature="appended")
@@ -78,6 +102,16 @@ Returns a matching provider by provider ID.
78102
Returns a list of all available algorithms from registered providers.
79103

80104
.. seealso:: :py:func:`algorithmById`
105+
%End
106+
107+
QgsProcessingAlgorithmInformation algorithmInformation( const QString &id ) const;
108+
%Docstring
109+
Returns basic algorithm information for the algorithm with matching ID.
110+
111+
This method uses an internal cache to ensure that information is quickly
112+
returned and is suitable for calling many times.
113+
114+
.. versionadded:: 3.32
81115
%End
82116

83117
const QgsProcessingAlgorithm *algorithmById( const QString &id ) const;

‎src/core/processing/qgsprocessingregistry.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ bool QgsProcessingRegistry::addProvider( QgsProcessingProvider *provider )
116116

117117
provider->setParent( this );
118118
mProviders[ provider->id()] = provider;
119+
120+
mCachedInformation.clear();
121+
connect( provider, &QgsProcessingProvider::algorithmsLoaded, this, [this]
122+
{
123+
mCachedInformation.clear();
124+
} );
125+
119126
emit providerAdded( provider->id() );
120127
return true;
121128
}
@@ -133,6 +140,9 @@ bool QgsProcessingRegistry::removeProvider( QgsProcessingProvider *provider )
133140
provider->unload();
134141

135142
delete mProviders.take( id );
143+
144+
mCachedInformation.clear();
145+
136146
emit providerRemoved( id );
137147
return true;
138148
}
@@ -159,6 +169,22 @@ QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() cons
159169
return algs;
160170
}
161171

172+
QgsProcessingAlgorithmInformation QgsProcessingRegistry::algorithmInformation( const QString &id ) const
173+
{
174+
const auto it = mCachedInformation.constFind( id );
175+
if ( it != mCachedInformation.constEnd() )
176+
return *it;
177+
178+
QgsProcessingAlgorithmInformation info;
179+
if ( const QgsProcessingAlgorithm *algorithm = algorithmById( id ) )
180+
{
181+
info.displayName = algorithm->displayName();
182+
info.icon = algorithm->icon();
183+
}
184+
mCachedInformation.insert( id, info );
185+
return info;
186+
}
187+
162188
const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QString &constId ) const
163189
{
164190
// allow mapping of algorithm via registered algorithm aliases

‎src/core/processing/qgsprocessingregistry.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
class QgsProcessingParameterType;
2727
class QgsProcessingAlgorithmConfigurationWidgetFactory;
2828

29+
class CORE_EXPORT QgsProcessingAlgorithmInformation
30+
{
31+
public:
32+
33+
//! Algorithm display name
34+
QString displayName;
35+
36+
//! Algorithm icon
37+
QIcon icon;
38+
};
39+
40+
2941
/**
3042
* \class QgsProcessingRegistry
3143
* \ingroup core
@@ -95,6 +107,16 @@ class CORE_EXPORT QgsProcessingRegistry : public QObject
95107
*/
96108
QList< const QgsProcessingAlgorithm *> algorithms() const;
97109

110+
/**
111+
* Returns basic algorithm information for the algorithm with matching ID.
112+
*
113+
* This method uses an internal cache to ensure that information is quickly
114+
* returned and is suitable for calling many times.
115+
*
116+
* \since QGIS 3.32
117+
*/
118+
QgsProcessingAlgorithmInformation algorithmInformation( const QString &id ) const;
119+
98120
/**
99121
* Finds an algorithm by its ID. If no matching algorithm is found, NULLPTR
100122
* is returned.
@@ -215,9 +237,13 @@ class CORE_EXPORT QgsProcessingRegistry : public QObject
215237

216238
QMap< QString, QString > mAlgorithmAliases;
217239

240+
mutable QMap< QString, QgsProcessingAlgorithmInformation > mCachedInformation;
241+
218242
#ifdef SIP_RUN
219243
QgsProcessingRegistry( const QgsProcessingRegistry &other );
220244
#endif
245+
246+
friend class TestQgsProcessing;
221247
};
222248

223249
#endif // QGSPROCESSINGREGISTRY_H

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,10 @@ void TestQgsProcessing::algorithm()
16931693
QVERIFY( !p->algorithms().empty() );
16941694

16951695
QgsProcessingRegistry r;
1696+
1697+
QgsProcessingAlgorithmInformation info = r.algorithmInformation( QStringLiteral( "p1:alg1" ) );
1698+
QVERIFY( info.displayName.isEmpty() );
1699+
16961700
QVERIFY( r.addProvider( p ) );
16971701
QCOMPARE( r.algorithms().size(), 2 );
16981702
QVERIFY( r.algorithms().contains( p->algorithm( "alg1" ) ) );
@@ -1704,6 +1708,13 @@ void TestQgsProcessing::algorithm()
17041708
QVERIFY( !r.algorithmById( "p1:alg3" ) );
17051709
QVERIFY( !r.algorithmById( "px:alg1" ) );
17061710

1711+
info = r.algorithmInformation( QStringLiteral( "p1:alg1" ) );
1712+
QCOMPARE( info.displayName, QStringLiteral( "alg1" ) );
1713+
info = r.algorithmInformation( QStringLiteral( "p1:alg2" ) );
1714+
QCOMPARE( info.displayName, QStringLiteral( "alg2" ) );
1715+
info = r.algorithmInformation( QStringLiteral( "p1:alg3" ) );
1716+
QVERIFY( info.displayName.isEmpty() );
1717+
17071718
// alias support
17081719
QVERIFY( !r.algorithmById( QStringLiteral( "fake:fakealg" ) ) );
17091720
r.addAlgorithmAlias( QStringLiteral( "fake:fakealg" ), QStringLiteral( "nope:none" ) );
@@ -1739,7 +1750,15 @@ void TestQgsProcessing::algorithm()
17391750
DummyProvider *p3 = new DummyProvider( "p3" );
17401751
QVERIFY( p3->algorithms().isEmpty() );
17411752
QVERIFY( r.addProvider( p3 ) );
1753+
QVERIFY( r.mCachedInformation.empty() );
17421754
QCOMPARE( p3->algorithms().size(), 2 );
1755+
1756+
info = r.algorithmInformation( QStringLiteral( "p3:alg2" ) );
1757+
QCOMPARE( info.displayName, QStringLiteral( "alg2" ) );
1758+
1759+
// a provider refresh should clear the registry's cache
1760+
p3->refreshAlgorithms();
1761+
QVERIFY( r.mCachedInformation.empty() );
17431762
}
17441763

17451764
void TestQgsProcessing::features()

0 commit comments

Comments
 (0)
Please sign in to comment.