QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsprocessingregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingregistry.cpp
3 --------------------------
4 begin : December 2016
5 copyright : (C) 2016 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsvectorfilewriter.h"
27
29 : QObject( parent )
30{
69 addParameterType( new QgsProcessingParameterTypeVectorTileWriterLayers() );
70 addParameterType( new QgsProcessingParameterTypeFieldMapping() );
71 addParameterType( new QgsProcessingParameterTypeAggregate() );
72 addParameterType( new QgsProcessingParameterTypeTinInputLayers() );
73 addParameterType( new QgsProcessingParameterTypeDxfLayers() );
74 addParameterType( new QgsProcessingParameterTypeMeshDatasetGroups() );
75 addParameterType( new QgsProcessingParameterTypeMeshDatasetTime() );
81}
82
84{
85 const auto constMProviders = mProviders;
86 for ( QgsProcessingProvider *p : constMProviders )
87 {
88 removeProvider( p );
89 }
90
91 const auto parameterTypes = mParameterTypes.values();
92
94 {
95 removeParameterType( type );
96 }
97}
98
100{
101 if ( !provider )
102 return false;
103
104 if ( mProviders.contains( provider->id() ) )
105 {
106 QgsLogger::warning( QStringLiteral( "Duplicate provider %1 registered" ).arg( provider->id() ) );
107 delete provider;
108 return false;
109 }
110
111 if ( !provider->load() )
112 {
113 QgsLogger::warning( QStringLiteral( "Provider %1 cannot load" ).arg( provider->id() ) );
114 delete provider;
115 return false;
116 }
117
118 provider->setParent( this );
119 mProviders[ provider->id()] = provider;
120
121 mCachedInformation.clear();
122 connect( provider, &QgsProcessingProvider::algorithmsLoaded, this, [this]
123 {
124 mCachedInformation.clear();
125 } );
126
127 emit providerAdded( provider->id() );
128 return true;
129}
130
132{
133 if ( !provider )
134 return false;
135
136 const QString id = provider->id();
137
138 if ( !mProviders.contains( id ) )
139 return false;
140
141 provider->unload();
142
143 delete mProviders.take( id );
144
145 mCachedInformation.clear();
146
147 emit providerRemoved( id );
148 return true;
149}
150
151bool QgsProcessingRegistry::removeProvider( const QString &providerId )
152{
153 QgsProcessingProvider *p = providerById( providerId );
154 return removeProvider( p );
155}
156
158{
159 return mProviders.value( id, nullptr );
160}
161
162QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
163{
164 QList< const QgsProcessingAlgorithm * > algs;
165 QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
166 for ( ; it != mProviders.constEnd(); ++it )
167 {
168 algs.append( it.value()->algorithms() );
169 }
170 return algs;
171}
172
174{
175 const auto it = mCachedInformation.constFind( id );
176 if ( it != mCachedInformation.constEnd() )
177 return *it;
178
181 {
183 info.icon = algorithm->icon();
184 }
185 mCachedInformation.insert( id, info );
186 return info;
187}
188
190{
191 // allow mapping of algorithm via registered algorithm aliases
192 const QString id = mAlgorithmAliases.value( constId, constId );
193
194 QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
195 for ( ; it != mProviders.constEnd(); ++it )
196 {
197 const auto constAlgorithms = it.value()->algorithms();
198 for ( const QgsProcessingAlgorithm *alg : constAlgorithms )
199 if ( alg->id() == id )
200 return alg;
201 }
202
203 // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
204 // from the python 'qgis' provider to the c++ 'native' provider without breaking API
205 // or existing models
206 if ( id.startsWith( QLatin1String( "qgis:" ) ) )
207 {
208 const QString newId = QStringLiteral( "native:" ) + id.mid( 5 );
209 return algorithmById( newId );
210 }
211 return nullptr;
212}
213
214QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
215{
216 const QgsProcessingAlgorithm *alg = algorithmById( id );
217 if ( !alg )
218 return nullptr;
219
220 std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
221 return creation.release();
222}
223
224void QgsProcessingRegistry::addAlgorithmAlias( const QString &aliasId, const QString &actualId )
225{
226 mAlgorithmAliases.insert( aliasId, actualId );
227}
228
230{
231 if ( !mParameterTypes.contains( type->id() ) )
232 {
233 mParameterTypes.insert( type->id(), type );
234 emit parameterTypeAdded( type );
235 return true;
236 }
237 else
238 {
239 QgsLogger::warning( QStringLiteral( "Duplicate parameter type %1 (\"%2\") registered" ).arg( type->id(), type->name() ) );
240
241 if ( mParameterTypes.value( type->id() ) != type )
242 delete type;
243
244 return false;
245 }
246}
247
249{
250 mParameterTypes.remove( type->id() );
251 emit parameterTypeRemoved( type );
252 delete type;
253}
254
256{
257 return mParameterTypes.value( id );
258}
259
260QList<QgsProcessingParameterType *> QgsProcessingRegistry::parameterTypes() const
261{
262 return mParameterTypes.values();
263}
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:131
Contains basic properties for a Processing algorithm.
QString displayName
Algorithm display name.
Abstract base class for processing algorithms.
virtual QIcon icon() const
Returns an icon for the algorithm.
virtual QString displayName() const =0
Returns the translated algorithm name, which should be used for any user-visible display of the algor...
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const SIP_THROW(QgsProcessingException)
Creates a copy of the algorithm, ready for execution.
An annotation layer parameter for processing algorithms.
A authentication configuration parameter for processing algorithms.
A raster band parameter for Processing algorithms.
A boolean parameter for processing algorithms.
A color parameter for Processing algorithms.
A coordinate operation parameter for Processing algorithms.
A crs parameter for processing algorithms.
A database schema name parameter for processing algorithms.
A database table name parameter for processing algorithms.
A datetime parameter for processing algorithms.
A distance parameter for processing algorithms.
A duration parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
An expression parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A feature sink parameter for Processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
An input file or folder parameter for processing algorithms.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
A geometry parameter for processing algorithms.
A print layout item parameter for Processing algorithms.
A print layout parameter for Processing algorithms.
A generic map layer parameter for processing algorithms.
A map theme parameter for Processing algorithms.
A table (matrix) parameter for processing algorithms.
A mesh layer parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
A point cloud layer attribute parameter for Processing algorithms.
A pointcloud layer destination parameter, for specifying the destination path for a point cloud layer...
A point cloud layer parameter for processing algorithms.
A point parameter for processing algorithms.
A provider connection name parameter for processing algorithms.
A numeric range parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
A scale parameter for processing algorithms.
A string parameter for processing algorithms.
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
A vector layer parameter for processing algorithms.
A vector tile layer destination parameter, for specifying the destination path for a vector tile laye...
Makes metadata of processing parameters available.
virtual QString name() const =0
A human readable and translatable short name for this parameter type.
virtual QString id() const =0
A static id for this type which will be used for storing this parameter type.
Abstract base class for processing providers.
void algorithmsLoaded()
Emitted when the provider has loaded (or refreshed) its list of available algorithms.
virtual void unload()
Unloads the provider.
virtual QString id() const =0
Returns the unique provider id, used for identifying the provider.
virtual bool load()
Loads the provider.
void removeParameterType(QgsProcessingParameterType *type)
Unregister a custom parameter type from processing.
void parameterTypeAdded(QgsProcessingParameterType *type)
Emitted when a new parameter type has been added to the registry.
QgsProcessingAlgorithm * createAlgorithmById(const QString &id, const QVariantMap &configuration=QVariantMap()) const
Creates a new instance of an algorithm by its ID.
QgsProcessingProvider * providerById(const QString &id)
Returns a matching provider by provider ID.
void parameterTypeRemoved(QgsProcessingParameterType *type)
Emitted when a parameter type has been removed from the registry and is about to be deleted.
QgsProcessingAlgorithmInformation algorithmInformation(const QString &id) const
Returns basic algorithm information for the algorithm with matching ID.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of all available algorithms from registered providers.
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
bool removeProvider(QgsProcessingProvider *provider)
Removes a provider implementation from the registry (the provider object is deleted).
bool addParameterType(QgsProcessingParameterType *type)
Register a new parameter type for processing.
QgsProcessingRegistry(QObject *parent=nullptr)
Constructor for QgsProcessingRegistry.
const QgsProcessingAlgorithm * algorithmById(const QString &id) const
Finds an algorithm by its ID.
void providerAdded(const QString &id)
Emitted when a provider has been added to the registry.
QList< QgsProcessingParameterType * > parameterTypes() const
Returns a list with all known parameter types.
bool addProvider(QgsProcessingProvider *provider)
Add a processing provider to the registry.
void providerRemoved(const QString &id)
Emitted when a provider is removed from the registry.
void addAlgorithmAlias(const QString &aliasId, const QString &actualId)
Adds a new alias to an existing algorithm.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53