QGIS API Documentation 4.3.0-Master (0ff9723465c)
Loading...
Searching...
No Matches
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
33#include "qgsvectorfilewriter.h"
34
35#include <QString>
36
37#include "moc_qgsprocessingregistry.cpp"
38
39using namespace Qt::StringLiterals;
40
42 : QObject( parent )
43 , mDefaultStyleRegistry( new QgsProcessingDefaultStyleRegistry( this ) )
44{
85 addParameterType( new QgsProcessingParameterTypeVectorTileWriterLayers() );
86 addParameterType( new QgsProcessingParameterTypeFieldMapping() );
87 addParameterType( new QgsProcessingParameterTypeAggregate() );
88 addParameterType( new QgsProcessingParameterTypeTinInputLayers() );
89 addParameterType( new QgsProcessingParameterTypeDxfLayers() );
90 addParameterType( new QgsProcessingParameterTypeMeshDatasetGroups() );
91 addParameterType( new QgsProcessingParameterTypeMeshDatasetTime() );
97 addParameterType( new QgsProcessingParameterTypeAlignRasterLayers() );
98 addParameterType( new QgsProcessingParameterTypeHeatmapPixelSize() );
99 addParameterType( new QgsProcessingParameterTypeReliefColors() );
100 addParameterType( new QgsProcessingParameterTypeInterpolationSource() );
101 addParameterType( new QgsProcessingParameterTypeInterpolationPixelSize() );
102}
103
105{
106 const auto constMProviders = mProviders;
107 for ( QgsProcessingProvider *p : constMProviders )
108 {
109 removeProvider( p );
110 }
111
112 const auto parameterTypes = mParameterTypes.values();
113
115 {
116 removeParameterType( type );
117 }
118}
119
121{
122 if ( !provider )
123 return false;
124
125 if ( providerById( provider->id() ) )
126 {
127 QgsLogger::warning( u"Duplicate provider %1 registered"_s.arg( provider->id() ) );
128 delete provider;
129 return false;
130 }
131
132 if ( !provider->load() )
133 {
134 QgsLogger::warning( u"Provider %1 cannot load"_s.arg( provider->id() ) );
135 delete provider;
136 return false;
137 }
138
139 provider->setParent( this );
140 mProviders[provider->id()] = provider;
141
142 mCachedInformation.clear();
143 connect( provider, &QgsProcessingProvider::algorithmsLoaded, this, [this] { mCachedInformation.clear(); } );
144
145 emit providerAdded( provider->id() );
146 return true;
147}
148
150{
151 if ( !provider )
152 return false;
153
154 const QString id = provider->id();
155
156 if ( !mProviders.contains( id ) )
157 return false;
158
159 provider->unload();
160
161 delete mProviders.take( id );
162
163 mCachedInformation.clear();
164
165 emit providerRemoved( id );
166 return true;
167}
168
169bool QgsProcessingRegistry::removeProvider( const QString &providerId )
170{
171 QgsProcessingProvider *p = providerById( providerId );
172 return removeProvider( p );
173}
174
176{
177 auto it = mProviders.constFind( id );
178 if ( it != mProviders.constEnd() )
179 return it.value();
180
181 // transparently map old references to "grass7" provider to "grass" provider
182 if ( id.compare( "grass7"_L1, Qt::CaseInsensitive ) == 0 )
183 return providerById( u"grass"_s );
184
185 return nullptr;
186}
187
188QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
189{
190 QList< const QgsProcessingAlgorithm * > algs;
191 QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
192 for ( ; it != mProviders.constEnd(); ++it )
193 {
194 algs.append( it.value()->algorithms() );
195 }
196 return algs;
197}
198
200{
201 const auto it = mCachedInformation.constFind( id );
202 if ( it != mCachedInformation.constEnd() )
203 return *it;
204
207 {
208 info.displayName = algorithm->displayName();
209 info.icon = algorithm->icon();
210 }
211 mCachedInformation.insert( id, info );
212 return info;
213}
214
216{
217 if ( constId.isEmpty() )
218 return nullptr;
219
220 // allow mapping of algorithm via registered algorithm aliases
221 const QString id = mAlgorithmAliases.value( constId, constId );
222
223 // try to match just the one target provider, if we can determine it from the id easily
224 static thread_local QRegularExpression reSplitProviderId( u"^(.*?):(.*)$"_s );
225 const QRegularExpressionMatch match = reSplitProviderId.match( id );
226 if ( match.hasMatch() )
227 {
228 if ( QgsProcessingProvider *provider = providerById( match.captured( 1 ) ) )
229 {
230 if ( const QgsProcessingAlgorithm *algorithm = provider->algorithm( match.captured( 2 ) ) )
231 return algorithm;
232 }
233
234 // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
235 // from the python 'qgis' provider to the c++ 'native' provider without breaking API
236 // or existing models
237 if ( match.captured( 1 ) == "qgis"_L1 )
238 {
239 const QString algorithmName = id.mid( 5 );
240 if ( QgsProcessingProvider *provider = mProviders.value( u"native"_s ) )
241 {
242 if ( const QgsProcessingAlgorithm *algorithm = provider->algorithm( algorithmName ) )
243 return algorithm;
244 }
245 }
246 }
247
248 // slow: iterate through ALL providers to find a match
249 QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
250 for ( ; it != mProviders.constEnd(); ++it )
251 {
252 const QList< const QgsProcessingAlgorithm * > algorithms = it.value()->algorithms();
253 for ( const QgsProcessingAlgorithm *alg : algorithms )
254 if ( alg->id() == id )
255 return alg;
256 }
257
258 return nullptr;
259}
260
261QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
262{
263 const QgsProcessingAlgorithm *alg = algorithmById( id );
264 if ( !alg )
265 return nullptr;
266
267 std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
268 return creation.release();
269}
270
271void QgsProcessingRegistry::addAlgorithmAlias( const QString &aliasId, const QString &actualId )
272{
273 mAlgorithmAliases.insert( aliasId, actualId );
274}
275
277{
278 if ( !mParameterTypes.contains( type->id() ) )
279 {
280 mParameterTypes.insert( type->id(), type );
281 emit parameterTypeAdded( type );
282 return true;
283 }
284 else
285 {
286 QgsLogger::warning( u"Duplicate parameter type %1 (\"%2\") registered"_s.arg( type->id(), type->name() ) );
287
288 if ( mParameterTypes.value( type->id() ) != type )
289 delete type;
290
291 return false;
292 }
293}
294
296{
297 mParameterTypes.remove( type->id() );
298 emit parameterTypeRemoved( type );
299 delete type;
300}
301
303{
304 return mParameterTypes.value( id );
305}
306
307QList<QgsProcessingParameterType *> QgsProcessingRegistry::parameterTypes() const
308{
309 return mParameterTypes.values();
310}
311
313{
314 return mDefaultStyleRegistry;
315}
static void warning(const QString &msg)
Goes to qWarning.
Contains basic properties for a Processing algorithm.
QString displayName
Algorithm display name.
Abstract base class for processing algorithms.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
A registry for default styles to apply to layers generated by Processing.
An annotation layer parameter for processing algorithms.
An area parameter for processing algorithms.
An 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...
A volume parameter for processing algorithms.
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.
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).
QgsProcessingDefaultStyleRegistry * defaultStyleRegistry() const
Returns the default style registry, responsible for styling layers generated by Processing tools.
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.
QgsProcessingProvider * providerById(const QString &id) const
Returns a matching provider by provider 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:52