QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
18 #include "qgsprocessingregistry.h"
19 #include "qgsvectorfilewriter.h"
25 
27  : QObject( parent )
28 {
60 #if PROJ_VERSION_MAJOR>=6
62 #endif
69  addParameterType( new QgsProcessingParameterTypeFieldMapping() );
70  addParameterType( new QgsProcessingParameterTypeAggregate() );
72 }
73 
75 {
76  const auto constMProviders = mProviders;
77  for ( QgsProcessingProvider *p : constMProviders )
78  {
79  removeProvider( p );
80  }
81 
82  const auto parameterTypes = mParameterTypes.values();
83 
85  {
86  removeParameterType( type );
87  }
88 }
89 
91 {
92  if ( !provider )
93  return false;
94 
95  if ( mProviders.contains( provider->id() ) )
96  {
97  QgsLogger::warning( QStringLiteral( "Duplicate provider %1 registered" ).arg( provider->id() ) );
98  delete provider;
99  return false;
100  }
101 
102  if ( !provider->load() )
103  {
104  QgsLogger::warning( QStringLiteral( "Provider %1 cannot load" ).arg( provider->id() ) );
105  delete provider;
106  return false;
107  }
108 
109  provider->setParent( this );
110  mProviders[ provider->id()] = provider;
111  emit providerAdded( provider->id() );
112  return true;
113 }
114 
116 {
117  if ( !provider )
118  return false;
119 
120  QString id = provider->id();
121 
122  if ( !mProviders.contains( id ) )
123  return false;
124 
125  provider->unload();
126 
127  delete mProviders.take( id );
128  emit providerRemoved( id );
129  return true;
130 }
131 
132 bool QgsProcessingRegistry::removeProvider( const QString &providerId )
133 {
134  QgsProcessingProvider *p = providerById( providerId );
135  return removeProvider( p );
136 }
137 
139 {
140  return mProviders.value( id, nullptr );
141 }
142 
143 QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
144 {
145  QList< const QgsProcessingAlgorithm * > algs;
146  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
147  for ( ; it != mProviders.constEnd(); ++it )
148  {
149  algs.append( it.value()->algorithms() );
150  }
151  return algs;
152 }
153 
154 const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QString &constId ) const
155 {
156  // allow mapping of algorithm via registered algorithm aliases
157  QString id = mAlgorithmAliases.value( constId, constId );
158 
159  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
160  for ( ; it != mProviders.constEnd(); ++it )
161  {
162  const auto constAlgorithms = it.value()->algorithms();
163  for ( const QgsProcessingAlgorithm *alg : constAlgorithms )
164  if ( alg->id() == id )
165  return alg;
166  }
167 
168  // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
169  // from the python 'qgis' provider to the c++ 'native' provider without breaking API
170  // or existing models
171  if ( id.startsWith( QLatin1String( "qgis:" ) ) )
172  {
173  QString newId = QStringLiteral( "native:" ) + id.mid( 5 );
174  return algorithmById( newId );
175  }
176  return nullptr;
177 }
178 
179 QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
180 {
181  const QgsProcessingAlgorithm *alg = algorithmById( id );
182  if ( !alg )
183  return nullptr;
184 
185  std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
186  return creation.release();
187 }
188 
189 void QgsProcessingRegistry::addAlgorithmAlias( const QString &aliasId, const QString &actualId )
190 {
191  mAlgorithmAliases.insert( aliasId, actualId );
192 }
193 
195 {
196  if ( !mParameterTypes.contains( type->id() ) )
197  {
198  mParameterTypes.insert( type->id(), type );
199  emit parameterTypeAdded( type );
200  return true;
201  }
202  else
203  {
204  QgsLogger::warning( QStringLiteral( "Duplicate parameter type %1 (\"%2\") registered" ).arg( type->id(), type->name() ) );
205 
206  if ( mParameterTypes.value( type->id() ) != type )
207  delete type;
208 
209  return false;
210  }
211 }
212 
214 {
215  mParameterTypes.remove( type->id() );
216  emit parameterTypeRemoved( type );
217  delete type;
218 }
219 
221 {
222  return mParameterTypes.value( id );
223 }
224 
225 QList<QgsProcessingParameterType *> QgsProcessingRegistry::parameterTypes() const
226 {
227  return mParameterTypes.values();
228 }
QgsProcessingParameterTypeCrs
A crs parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:358
QgsProcessingParameterTypeAuthConfig
A authentication configuration parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1144
QgsProcessingRegistry::providerRemoved
void providerRemoved(const QString &id)
Emitted when a provider is removed from the registry.
QgsProcessingParameterTypeProviderConnection
A provider connection name parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1904
qgsprocessingparameterfieldmap.h
qgsprocessingparametertininputlayers.h
QgsProcessingProvider::load
virtual bool load()
Loads the provider.
Definition: qgsprocessingprovider.h:244
QgsProcessingRegistry::createAlgorithmById
QgsProcessingAlgorithm * createAlgorithmById(const QString &id, const QVariantMap &configuration=QVariantMap()) const
Creates a new instance of an algorithm by its ID.
Definition: qgsprocessingregistry.cpp:179
QgsProcessingRegistry::algorithmById
const QgsProcessingAlgorithm * algorithmById(const QString &id) const
Finds an algorithm by its ID.
Definition: qgsprocessingregistry.cpp:154
QgsProcessingProvider::id
virtual QString id() const =0
Returns the unique provider id, used for identifying the provider.
QgsProcessingParameterTypeCoordinateOperation
A coordinate operation parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1743
QgsProcessingParameterTypeMapTheme
A map theme parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1796
QgsProcessingRegistry::addParameterType
bool addParameterType(QgsProcessingParameterType *type)
Register a new parameter type for processing.
Definition: qgsprocessingregistry.cpp:194
QgsProcessingProvider
Abstract base class for processing providers.
Definition: qgsprocessingprovider.h:35
QgsProcessingRegistry::parameterTypes
QList< QgsProcessingParameterType * > parameterTypes() const
Returns a list with all known parameter types.
Definition: qgsprocessingregistry.cpp:225
QgsProcessingRegistry::removeProvider
bool removeProvider(QgsProcessingProvider *provider)
Removes a provider implementation from the registry (the provider object is deleted).
Definition: qgsprocessingregistry.cpp:115
QgsProcessingParameterTypeMultipleLayers
A parameter for processing algorithms which accepts multiple map layers.
Definition: qgsprocessingparametertypeimpl.h:1195
QgsProcessingParameterType::name
virtual QString name() const =0
A human readable and translatable short name for this parameter type.
QgsProcessingParameterTypeMatrix
A table (matrix) parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:694
QgsProcessingParameterTypeGeometry
A geometry parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:529
QgsProcessingParameterTypeVectorDestination
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
Definition: qgsprocessingparametertypeimpl.h:850
QgsProcessingRegistry::removeParameterType
void removeParameterType(QgsProcessingParameterType *type)
Unregister a custom parameter type from processing.
Definition: qgsprocessingregistry.cpp:213
QgsProcessingRegistry::addProvider
bool addProvider(QgsProcessingProvider *provider)
Add a processing provider to the registry.
Definition: qgsprocessingregistry.cpp:90
QgsProcessingParameterTypeExtent
A rectangular map extent parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:634
QgsProcessingParameterTypeDateTime
A datetime parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1848
QgsProcessingParameterType
Makes metadata of processing parameters available.
Definition: qgsprocessingparametertype.h:34
QgsProcessingParameterTypeExpression
An expression parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:307
qgsprocessingparameteraggregate.h
QgsProcessingParameterTypeTinInputLayers
Parameter type definition for QgsProcessingParameterTinInputLayers.
Definition: qgsprocessingparametertininputlayers.h:75
QgsProcessingRegistry::~QgsProcessingRegistry
~QgsProcessingRegistry() override
Definition: qgsprocessingregistry.cpp:74
QgsProcessingProvider::unload
virtual void unload()
Unloads the provider.
Definition: qgsprocessingprovider.h:250
QgsProcessingParameterTypeBoolean
A boolean parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:253
QgsProcessingRegistry::algorithms
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of all available algorithms from registered providers.
Definition: qgsprocessingregistry.cpp:143
QgsProcessingParameterTypeFolderDestination
A folder destination parameter, for specifying the destination path for a folder created by the algor...
Definition: qgsprocessingparametertypeimpl.h:972
QgsProcessingRegistry::providerAdded
void providerAdded(const QString &id)
Emitted when a provider has been added to the registry.
QgsProcessingParameterTypeDatabaseTable
A database table name parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:2009
QgsProcessingParameterTypeDatabaseSchema
A database schema name parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1956
qgsprocessingparametervectortilewriterlayers.h
QgsProcessingParameterTypeLayout
A print layout parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1579
QgsProcessingParameterTypeVectorLayer
A vector layer parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:143
QgsProcessingParameterType::id
virtual QString id() const =0
A static id for this type which will be used for storing this parameter type.
QgsProcessingParameterTypeRasterLayer
A raster layer parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:36
QgsProcessingParameterTypeScale
A scale parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1408
QgsProcessingParameterTypeFile
An input file or folder parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:746
QgsProcessingRegistry::parameterType
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
Definition: qgsprocessingregistry.cpp:220
QgsLogger::warning
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:122
QgsProcessingParameterTypeColor
A color parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1689
QgsProcessingRegistry::QgsProcessingRegistry
QgsProcessingRegistry(QObject *parent=nullptr)
Constructor for QgsProcessingRegistry.
Definition: qgsprocessingregistry.cpp:26
QgsProcessingParameterTypePoint
A point parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:475
QgsProcessingParameterTypeFeatureSource
An input feature source (such as vector layers) parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1244
QgsProcessingParameterTypeRange
A numeric range parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:422
qgsprocessingparametertypeimpl.h
QgsProcessingParameterTypeFeatureSink
A feature sink parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1515
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
QgsProcessingParameterTypeLayoutItem
A print layout item parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1634
QgsProcessingAlgorithm::create
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const SIP_THROW(QgsProcessingException)
Creates a copy of the algorithm, ready for execution.
Definition: qgsprocessingalgorithm.cpp:40
QgsProcessingParameterTypeField
A vector layer or feature source field parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:797
QgsProcessingParameterTypeVectorTileWriterLayers
Parameter type definition for QgsProcessingParameterVectorTileWriterLayers.
Definition: qgsprocessingparametervectortilewriterlayers.h:78
QgsProcessingParameterTypeFileDestination
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
Definition: qgsprocessingparametertypeimpl.h:911
QgsProcessingParameterTypeNumber
A numeric parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1300
QgsProcessingParameterTypeMeshLayer
A mesh layer parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:90
QgsProcessingParameterTypeString
A string parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1092
qgsvectorfilewriter.h
QgsProcessingRegistry::parameterTypeAdded
void parameterTypeAdded(QgsProcessingParameterType *type)
Emitted when a new parameter type has been added to the registry.
QgsProcessingParameterTypeDistance
A distance parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1353
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
qgsprocessingregistry.h
QgsProcessingRegistry::parameterTypeRemoved
void parameterTypeRemoved(QgsProcessingParameterType *type)
Emitted when a parameter type has been removed from the registry and is about to be deleted.
QgsProcessingParameterTypeMapLayer
A generic map layer parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:197
QgsProcessingParameterTypeBand
A raster band parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1462
QgsProcessingRegistry::providerById
QgsProcessingProvider * providerById(const QString &id)
Returns a matching provider by provider ID.
Definition: qgsprocessingregistry.cpp:138
QgsProcessingParameterTypeRasterDestination
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
Definition: qgsprocessingparametertypeimpl.h:1032
QgsProcessingRegistry::addAlgorithmAlias
void addAlgorithmAlias(const QString &aliasId, const QString &actualId)
Adds a new alias to an existing algorithm.
Definition: qgsprocessingregistry.cpp:189
QgsProcessingParameterTypeEnum
An enum based parameter for processing algorithms, allowing for selection from predefined values.
Definition: qgsprocessingparametertypeimpl.h:581