QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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"
24 
26  : QObject( parent )
27 {
58 #if PROJ_VERSION_MAJOR>=6
60 #endif
67  addParameterType( new QgsProcessingParameterTypeFieldMapping() );
68  addParameterType( new QgsProcessingParameterTypeAggregate() );
69 }
70 
72 {
73  const auto constMProviders = mProviders;
74  for ( QgsProcessingProvider *p : constMProviders )
75  {
76  removeProvider( p );
77  }
78 
79  const auto parameterTypes = mParameterTypes.values();
80 
82  {
83  removeParameterType( type );
84  }
85 }
86 
88 {
89  if ( !provider )
90  return false;
91 
92  if ( mProviders.contains( provider->id() ) )
93  {
94  QgsLogger::warning( QStringLiteral( "Duplicate provider %1 registered" ).arg( provider->id() ) );
95  delete provider;
96  return false;
97  }
98 
99  if ( !provider->load() )
100  {
101  QgsLogger::warning( QStringLiteral( "Provider %1 cannot load" ).arg( provider->id() ) );
102  delete provider;
103  return false;
104  }
105 
106  provider->setParent( this );
107  mProviders[ provider->id()] = provider;
108  emit providerAdded( provider->id() );
109  return true;
110 }
111 
113 {
114  if ( !provider )
115  return false;
116 
117  QString id = provider->id();
118 
119  if ( !mProviders.contains( id ) )
120  return false;
121 
122  provider->unload();
123 
124  delete mProviders.take( id );
125  emit providerRemoved( id );
126  return true;
127 }
128 
129 bool QgsProcessingRegistry::removeProvider( const QString &providerId )
130 {
131  QgsProcessingProvider *p = providerById( providerId );
132  return removeProvider( p );
133 }
134 
136 {
137  return mProviders.value( id, nullptr );
138 }
139 
140 QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
141 {
142  QList< const QgsProcessingAlgorithm * > algs;
143  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
144  for ( ; it != mProviders.constEnd(); ++it )
145  {
146  algs.append( it.value()->algorithms() );
147  }
148  return algs;
149 }
150 
151 const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QString &constId ) const
152 {
153  // allow mapping of algorithm via registered algorithm aliases
154  QString id = mAlgorithmAliases.value( constId, constId );
155 
156  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
157  for ( ; it != mProviders.constEnd(); ++it )
158  {
159  const auto constAlgorithms = it.value()->algorithms();
160  for ( const QgsProcessingAlgorithm *alg : constAlgorithms )
161  if ( alg->id() == id )
162  return alg;
163  }
164 
165  // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
166  // from the python 'qgis' provider to the c++ 'native' provider without breaking API
167  // or existing models
168  if ( id.startsWith( QLatin1String( "qgis:" ) ) )
169  {
170  QString newId = QStringLiteral( "native:" ) + id.mid( 5 );
171  return algorithmById( newId );
172  }
173  return nullptr;
174 }
175 
176 QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
177 {
178  const QgsProcessingAlgorithm *alg = algorithmById( id );
179  if ( !alg )
180  return nullptr;
181 
182  std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
183  return creation.release();
184 }
185 
186 void QgsProcessingRegistry::addAlgorithmAlias( const QString &aliasId, const QString &actualId )
187 {
188  mAlgorithmAliases.insert( aliasId, actualId );
189 }
190 
192 {
193  if ( !mParameterTypes.contains( type->id() ) )
194  {
195  mParameterTypes.insert( type->id(), type );
196  emit parameterTypeAdded( type );
197  return true;
198  }
199  else
200  {
201  QgsLogger::warning( QStringLiteral( "Duplicate parameter type %1 (\"%2\") registered" ).arg( type->id(), type->name() ) );
202 
203  if ( mParameterTypes.value( type->id() ) != type )
204  delete type;
205 
206  return false;
207  }
208 }
209 
211 {
212  mParameterTypes.remove( type->id() );
213  emit parameterTypeRemoved( type );
214  delete type;
215 }
216 
218 {
219  return mParameterTypes.value( id );
220 }
221 
222 QList<QgsProcessingParameterType *> QgsProcessingRegistry::parameterTypes() const
223 {
224  return mParameterTypes.values();
225 }
QgsProcessingParameterTypeCrs
A crs parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:357
QgsProcessingParameterTypeAuthConfig
A authentication configuration parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1091
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:1851
qgsprocessingparameterfieldmap.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:176
QgsProcessingRegistry::algorithmById
const QgsProcessingAlgorithm * algorithmById(const QString &id) const
Finds an algorithm by its ID.
Definition: qgsprocessingregistry.cpp:151
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:1690
QgsProcessingParameterTypeMapTheme
A map theme parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1743
QgsProcessingRegistry::addParameterType
bool addParameterType(QgsProcessingParameterType *type)
Register a new parameter type for processing.
Definition: qgsprocessingregistry.cpp:191
QgsProcessingProvider
Definition: qgsprocessingprovider.h:34
QgsProcessingRegistry::parameterTypes
QList< QgsProcessingParameterType * > parameterTypes() const
Returns a list with all known parameter types.
Definition: qgsprocessingregistry.cpp:222
QgsProcessingRegistry::removeProvider
bool removeProvider(QgsProcessingProvider *provider)
Removes a provider implementation from the registry (the provider object is deleted).
Definition: qgsprocessingregistry.cpp:112
QgsProcessingParameterTypeMultipleLayers
A parameter for processing algorithms which accepts multiple map layers.
Definition: qgsprocessingparametertypeimpl.h:1142
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:641
QgsProcessingParameterTypeVectorDestination
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
Definition: qgsprocessingparametertypeimpl.h:797
QgsProcessingRegistry::removeParameterType
void removeParameterType(QgsProcessingParameterType *type)
Unregister a custom parameter type from processing.
Definition: qgsprocessingregistry.cpp:210
QgsProcessingRegistry::addProvider
bool addProvider(QgsProcessingProvider *provider)
Add a processing provider to the registry.
Definition: qgsprocessingregistry.cpp:87
QgsProcessingParameterTypeExtent
A rectangular map extent parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:581
QgsProcessingParameterTypeDateTime
A datetime parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1795
QgsProcessingParameterType
Makes metadata of processing parameters available.
Definition: qgsprocessingparametertype.h:33
QgsProcessingParameterTypeExpression
An expression parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:306
qgsprocessingparameteraggregate.h
QgsProcessingRegistry::~QgsProcessingRegistry
~QgsProcessingRegistry() override
Definition: qgsprocessingregistry.cpp:71
QgsProcessingProvider::unload
virtual void unload()
Unloads the provider.
Definition: qgsprocessingprovider.h:250
QgsProcessingParameterTypeBoolean
A boolean parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:252
QgsProcessingRegistry::algorithms
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of all available algorithms from registered providers.
Definition: qgsprocessingregistry.cpp:140
QgsProcessingParameterTypeFolderDestination
A folder destination parameter, for specifying the destination path for a folder created by the algor...
Definition: qgsprocessingparametertypeimpl.h:919
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:1956
QgsProcessingParameterTypeDatabaseSchema
A database schema name parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1903
qgsprocessingparametervectortilewriterlayers.h
QgsProcessingParameterTypeLayout
A print layout parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1526
QgsProcessingParameterTypeVectorLayer
A vector layer parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:142
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:35
QgsProcessingParameterTypeScale
A scale parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1355
QgsProcessingParameterTypeFile
An input file or folder parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:693
QgsProcessingRegistry::parameterType
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
Definition: qgsprocessingregistry.cpp:217
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:1636
QgsProcessingRegistry::QgsProcessingRegistry
QgsProcessingRegistry(QObject *parent=nullptr)
Constructor for QgsProcessingRegistry.
Definition: qgsprocessingregistry.cpp:25
QgsProcessingParameterTypePoint
A point parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:474
QgsProcessingParameterTypeFeatureSource
An input feature source (such as vector layers) parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1191
QgsProcessingParameterTypeRange
A numeric range parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:421
qgsprocessingparametertypeimpl.h
QgsProcessingParameterTypeFeatureSink
A feature sink parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1462
QgsProcessingAlgorithm
Definition: qgsprocessingalgorithm.h:51
QgsProcessingParameterTypeLayoutItem
A print layout item parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1581
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:744
QgsProcessingParameterTypeVectorTileWriterLayers
Parameter type definition for QgsProcessingParameterVectorTileWriterLayers.
Definition: qgsprocessingparametervectortilewriterlayers.h:77
QgsProcessingParameterTypeFileDestination
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
Definition: qgsprocessingparametertypeimpl.h:858
QgsProcessingParameterTypeNumber
A numeric parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1247
QgsProcessingParameterTypeMeshLayer
A mesh layer parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:89
QgsProcessingParameterTypeString
A string parameter for processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1039
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:1300
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:196
QgsProcessingParameterTypeBand
A raster band parameter for Processing algorithms.
Definition: qgsprocessingparametertypeimpl.h:1409
QgsProcessingRegistry::providerById
QgsProcessingProvider * providerById(const QString &id)
Returns a matching provider by provider ID.
Definition: qgsprocessingregistry.cpp:135
QgsProcessingParameterTypeRasterDestination
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
Definition: qgsprocessingparametertypeimpl.h:979
QgsProcessingRegistry::addAlgorithmAlias
void addAlgorithmAlias(const QString &aliasId, const QString &actualId)
Adds a new alias to an existing algorithm.
Definition: qgsprocessingregistry.cpp:186
QgsProcessingParameterTypeEnum
An enum based parameter for processing algorithms, allowing for selection from predefined values.
Definition: qgsprocessingparametertypeimpl.h:528