QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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"
27 
29  : QObject( parent )
30 {
62 #if PROJ_VERSION_MAJOR>=6
64 #endif
70  addParameterType( new QgsProcessingParameterTypeVectorTileWriterLayers() );
71  addParameterType( new QgsProcessingParameterTypeFieldMapping() );
72  addParameterType( new QgsProcessingParameterTypeAggregate() );
73  addParameterType( new QgsProcessingParameterTypeTinInputLayers() );
74  addParameterType( new QgsProcessingParameterTypeDxfLayers() );
75  addParameterType( new QgsProcessingParameterTypeMeshDatasetGroups() );
76  addParameterType( new QgsProcessingParameterTypeMeshDatasetTime() );
77 }
78 
80 {
81  const auto constMProviders = mProviders;
82  for ( QgsProcessingProvider *p : constMProviders )
83  {
84  removeProvider( p );
85  }
86 
87  const auto parameterTypes = mParameterTypes.values();
88 
90  {
91  removeParameterType( type );
92  }
93 }
94 
96 {
97  if ( !provider )
98  return false;
99 
100  if ( mProviders.contains( provider->id() ) )
101  {
102  QgsLogger::warning( QStringLiteral( "Duplicate provider %1 registered" ).arg( provider->id() ) );
103  delete provider;
104  return false;
105  }
106 
107  if ( !provider->load() )
108  {
109  QgsLogger::warning( QStringLiteral( "Provider %1 cannot load" ).arg( provider->id() ) );
110  delete provider;
111  return false;
112  }
113 
114  provider->setParent( this );
115  mProviders[ provider->id()] = provider;
116  emit providerAdded( provider->id() );
117  return true;
118 }
119 
121 {
122  if ( !provider )
123  return false;
124 
125  QString id = provider->id();
126 
127  if ( !mProviders.contains( id ) )
128  return false;
129 
130  provider->unload();
131 
132  delete mProviders.take( id );
133  emit providerRemoved( id );
134  return true;
135 }
136 
137 bool QgsProcessingRegistry::removeProvider( const QString &providerId )
138 {
139  QgsProcessingProvider *p = providerById( providerId );
140  return removeProvider( p );
141 }
142 
144 {
145  return mProviders.value( id, nullptr );
146 }
147 
148 QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
149 {
150  QList< const QgsProcessingAlgorithm * > algs;
151  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
152  for ( ; it != mProviders.constEnd(); ++it )
153  {
154  algs.append( it.value()->algorithms() );
155  }
156  return algs;
157 }
158 
159 const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QString &constId ) const
160 {
161  // allow mapping of algorithm via registered algorithm aliases
162  QString id = mAlgorithmAliases.value( constId, constId );
163 
164  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
165  for ( ; it != mProviders.constEnd(); ++it )
166  {
167  const auto constAlgorithms = it.value()->algorithms();
168  for ( const QgsProcessingAlgorithm *alg : constAlgorithms )
169  if ( alg->id() == id )
170  return alg;
171  }
172 
173  // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
174  // from the python 'qgis' provider to the c++ 'native' provider without breaking API
175  // or existing models
176  if ( id.startsWith( QLatin1String( "qgis:" ) ) )
177  {
178  QString newId = QStringLiteral( "native:" ) + id.mid( 5 );
179  return algorithmById( newId );
180  }
181  return nullptr;
182 }
183 
184 QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
185 {
186  const QgsProcessingAlgorithm *alg = algorithmById( id );
187  if ( !alg )
188  return nullptr;
189 
190  std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
191  return creation.release();
192 }
193 
194 void QgsProcessingRegistry::addAlgorithmAlias( const QString &aliasId, const QString &actualId )
195 {
196  mAlgorithmAliases.insert( aliasId, actualId );
197 }
198 
200 {
201  if ( !mParameterTypes.contains( type->id() ) )
202  {
203  mParameterTypes.insert( type->id(), type );
204  emit parameterTypeAdded( type );
205  return true;
206  }
207  else
208  {
209  QgsLogger::warning( QStringLiteral( "Duplicate parameter type %1 (\"%2\") registered" ).arg( type->id(), type->name() ) );
210 
211  if ( mParameterTypes.value( type->id() ) != type )
212  delete type;
213 
214  return false;
215  }
216 }
217 
219 {
220  mParameterTypes.remove( type->id() );
221  emit parameterTypeRemoved( type );
222  delete type;
223 }
224 
226 {
227  return mParameterTypes.value( id );
228 }
229 
230 QList<QgsProcessingParameterType *> QgsProcessingRegistry::parameterTypes() const
231 {
232  return mParameterTypes.values();
233 }
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:122
Abstract base class for processing algorithms.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const SIP_THROW(QgsProcessingException)
Creates a copy of the algorithm, ready for execution.
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.
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 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.
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.
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.
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.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53