QGIS API Documentation  3.0.2-Girona (307d082)
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"
20 
22  : QObject( parent )
23 {}
24 
26 {
27  Q_FOREACH ( QgsProcessingProvider *p, mProviders )
28  {
29  removeProvider( p );
30  }
31 }
32 
34 {
35  if ( !provider )
36  return false;
37 
38  if ( mProviders.contains( provider->id() ) )
39  {
40  QgsLogger::warning( QStringLiteral( "Duplicate provider %1 registered" ).arg( provider->id() ) );
41  delete provider;
42  return false;
43  }
44 
45  if ( !provider->load() )
46  {
47  QgsLogger::warning( QStringLiteral( "Provider %1 cannot load" ).arg( provider->id() ) );
48  delete provider;
49  return false;
50  }
51 
52  provider->setParent( this );
53  mProviders[ provider->id()] = provider;
54  emit providerAdded( provider->id() );
55  return true;
56 }
57 
59 {
60  if ( !provider )
61  return false;
62 
63  QString id = provider->id();
64 
65  if ( !mProviders.contains( id ) )
66  return false;
67 
68  provider->unload();
69 
70  delete mProviders.take( id );
71  emit providerRemoved( id );
72  return true;
73 }
74 
75 bool QgsProcessingRegistry::removeProvider( const QString &providerId )
76 {
77  QgsProcessingProvider *p = providerById( providerId );
78  return removeProvider( p );
79 }
80 
82 {
83  return mProviders.value( id, nullptr );
84 }
85 
86 QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
87 {
88  QList< const QgsProcessingAlgorithm * > algs;
89  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
90  for ( ; it != mProviders.constEnd(); ++it )
91  {
92  algs.append( it.value()->algorithms() );
93  }
94  return algs;
95 }
96 
98 {
99  QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
100  for ( ; it != mProviders.constEnd(); ++it )
101  {
102  Q_FOREACH ( const QgsProcessingAlgorithm *alg, it.value()->algorithms() )
103  if ( alg->id() == id )
104  return alg;
105  }
106 
107  // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
108  // from the python 'qgis' provider to the c++ 'native' provider without breaking API
109  // or existing models
110  if ( id.startsWith( QStringLiteral( "qgis:" ) ) )
111  {
112  QString newId = QStringLiteral( "native:" ) + id.mid( 5 );
113  return algorithmById( newId );
114  }
115  return nullptr;
116 }
117 
118 QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
119 {
120  const QgsProcessingAlgorithm *alg = algorithmById( id );
121  if ( !alg )
122  return nullptr;
123 
124  std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
125  return creation.release();
126 }
127 
bool removeProvider(QgsProcessingProvider *provider)
Removes a provider implementation from the registry (the provider object is deleted).
QString id() const
Returns the unique ID for the algorithm, which is a combination of the algorithm provider&#39;s ID and th...
void providerAdded(const QString &id)
Emitted when a provider has been added to the registry.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:121
Abstract base class for processing providers.
const QgsProcessingAlgorithm * algorithmById(const QString &id) const
Finds an algorithm by its ID.
Abstract base class for processing algorithms.
QgsProcessingProvider * providerById(const QString &id)
Returns a matching provider by provider ID.
void providerRemoved(const QString &id)
Emitted when a provider is removed from the registry.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of all available algorithms from registered providers.
virtual QString id() const =0
Returns the unique provider id, used for identifying the provider.
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.
bool addProvider(QgsProcessingProvider *provider)
Add a processing provider to the registry.
virtual void unload()
Unloads the provider.