QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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"
21 
23  : QObject( parent )
24 {
55 }
56 
58 {
59  const auto constMProviders = mProviders;
60  for ( QgsProcessingProvider *p : constMProviders )
61  {
62  removeProvider( p );
63  }
64 
65  const auto parameterTypes = mParameterTypes.values();
66 
68  {
69  removeParameterType( type );
70  }
71 }
72 
74 {
75  if ( !provider )
76  return false;
77 
78  if ( mProviders.contains( provider->id() ) )
79  {
80  QgsLogger::warning( QStringLiteral( "Duplicate provider %1 registered" ).arg( provider->id() ) );
81  delete provider;
82  return false;
83  }
84 
85  if ( !provider->load() )
86  {
87  QgsLogger::warning( QStringLiteral( "Provider %1 cannot load" ).arg( provider->id() ) );
88  delete provider;
89  return false;
90  }
91 
92  provider->setParent( this );
93  mProviders[ provider->id()] = provider;
94  emit providerAdded( provider->id() );
95  return true;
96 }
97 
99 {
100  if ( !provider )
101  return false;
102 
103  QString id = provider->id();
104 
105  if ( !mProviders.contains( id ) )
106  return false;
107 
108  provider->unload();
109 
110  delete mProviders.take( id );
111  emit providerRemoved( id );
112  return true;
113 }
114 
115 bool QgsProcessingRegistry::removeProvider( const QString &providerId )
116 {
117  QgsProcessingProvider *p = providerById( providerId );
118  return removeProvider( p );
119 }
120 
122 {
123  return mProviders.value( id, nullptr );
124 }
125 
126 QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
127 {
128  QList< const QgsProcessingAlgorithm * > algs;
129  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
130  for ( ; it != mProviders.constEnd(); ++it )
131  {
132  algs.append( it.value()->algorithms() );
133  }
134  return algs;
135 }
136 
137 const QgsProcessingAlgorithm *QgsProcessingRegistry::algorithmById( const QString &constId ) const
138 {
139  // allow mapping of algorithm via registered algorithm aliases
140  QString id = mAlgorithmAliases.value( constId, constId );
141 
142  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
143  for ( ; it != mProviders.constEnd(); ++it )
144  {
145  const auto constAlgorithms = it.value()->algorithms();
146  for ( const QgsProcessingAlgorithm *alg : constAlgorithms )
147  if ( alg->id() == id )
148  return alg;
149  }
150 
151  // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
152  // from the python 'qgis' provider to the c++ 'native' provider without breaking API
153  // or existing models
154  if ( id.startsWith( QLatin1String( "qgis:" ) ) )
155  {
156  QString newId = QStringLiteral( "native:" ) + id.mid( 5 );
157  return algorithmById( newId );
158  }
159  return nullptr;
160 }
161 
162 QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
163 {
164  const QgsProcessingAlgorithm *alg = algorithmById( id );
165  if ( !alg )
166  return nullptr;
167 
168  std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
169  return creation.release();
170 }
171 
172 void QgsProcessingRegistry::addAlgorithmAlias( const QString &aliasId, const QString &actualId )
173 {
174  mAlgorithmAliases.insert( aliasId, actualId );
175 }
176 
178 {
179  if ( !mParameterTypes.contains( type->id() ) )
180  {
181  mParameterTypes.insert( type->id(), type );
182  emit parameterTypeAdded( type );
183  return true;
184  }
185  else
186  {
187  QgsLogger::warning( QStringLiteral( "Duplicate parameter type %1 (\"%2\") registered" ).arg( type->id(), type->name() ) );
188 
189  if ( mParameterTypes.value( type->id() ) != type )
190  delete type;
191 
192  return false;
193  }
194 }
195 
197 {
198  mParameterTypes.remove( type->id() );
199  emit parameterTypeRemoved( type );
200  delete type;
201 }
202 
204 {
205  return mParameterTypes.value( id );
206 }
207 
208 QList<QgsProcessingParameterType *> QgsProcessingRegistry::parameterTypes() const
209 {
210  return mParameterTypes.values();
211 }
A feature sink parameter for Processing algorithms.
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
bool removeProvider(QgsProcessingProvider *provider)
Removes a provider implementation from the registry (the provider object is deleted).
void providerAdded(const QString &id)
Emitted when a provider has been added to the registry.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A mesh layer parameter for processing algorithms.
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:121
virtual QString id() const =0
A static id for this type which will be used for storing this parameter type.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
void removeParameterType(QgsProcessingParameterType *type)
Unregister a custom parameter type from processing.
A numeric range parameter for processing algorithms.
An expression parameter for processing algorithms.
Abstract base class for processing providers.
const QgsProcessingAlgorithm * algorithmById(const QString &id) const
Finds an algorithm by its ID.
A vector layer or feature source field parameter for processing algorithms.
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
void parameterTypeAdded(QgsProcessingParameterType *type)
Emitted when a new parameter type has been added to the registry.
A numeric parameter for processing algorithms.
A distance parameter for processing algorithms.
Abstract base class for processing algorithms.
A string parameter for processing algorithms.
bool addParameterType(QgsProcessingParameterType *type)
Register a new parameter type for processing.
QgsProcessingProvider * providerById(const QString &id)
Returns a matching provider by provider ID.
void addAlgorithmAlias(const QString &aliasId, const QString &actualId)
Adds a new alias to an existing algorithm.
A vector layer parameter for processing algorithms.
A boolean parameter for processing algorithms.
A generic map layer parameter for processing algorithms.
An input file or folder parameter for processing algorithms.
void providerRemoved(const QString &id)
Emitted when a provider is removed from the registry.
A raster layer parameter for processing algorithms.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of all available algorithms from registered providers.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
virtual QString id() const =0
Returns the unique provider id, used for identifying the provider.
virtual QString name() const =0
A human readable and translatable short name for this parameter type.
A color parameter for Processing algorithms.
A scale parameter for processing algorithms.
A authentication configuration parameter for processing algorithms.
QgsProcessingRegistry(QObject *parent=nullptr)
Constructor for QgsProcessingRegistry.
virtual bool load()
Loads the provider.
QgsProcessingAlgorithm * createAlgorithmById(const QString &id, const QVariantMap &configuration=QVariantMap()) const
Creates a new instance of an algorithm by its ID.
A point parameter for processing algorithms.
bool addProvider(QgsProcessingProvider *provider)
Add a processing provider to the registry.
A table (matrix) parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A crs parameter for processing algorithms.
Makes metadata of processing parameters available.
void parameterTypeRemoved(QgsProcessingParameterType *type)
Emitted when a parameter type has been removed from the registry and is about to be deleted...
An input feature source (such as vector layers) parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
QList< QgsProcessingParameterType * > parameterTypes() const
Returns a list with all known parameter types.
An enum based parameter for processing algorithms, allowing for selection from predefined values...
virtual void unload()
Unloads the provider.
A print layout parameter for Processing algorithms.
A raster band parameter for Processing algorithms.
A print layout item parameter for Processing algorithms.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const SIP_THROW(QgsProcessingException)
Creates a copy of the algorithm, ready for execution.