QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsprocessingprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingprovider.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 "qgsprocessingprovider.h"
19 #include "qgsapplication.h"
20 #include "qgsvectorfilewriter.h"
21 #include "qgsrasterfilewriter.h"
22 #include "qgssettings.h"
23 
25  : QObject( parent )
26 {}
27 
28 
30 {
31  qDeleteAll( mAlgorithms );
32 }
33 
35 {
36  return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
37 }
38 
40 {
41  return QgsApplication::iconPath( QStringLiteral( "processingAlgorithm.svg" ) );
42 }
43 
45 {
46  return QString();
47 }
48 
50 {
51  return name();
52 }
53 
55 {
56  return QString();
57 }
58 
60 {
62 }
63 
65 {
66  qDeleteAll( mAlgorithms );
67  mAlgorithms.clear();
68  if ( isActive() )
69  {
71  emit algorithmsLoaded();
72  }
73 }
74 
75 QList<const QgsProcessingAlgorithm *> QgsProcessingProvider::algorithms() const
76 {
77  return mAlgorithms.values();
78 }
79 
81 {
82  return mAlgorithms.value( name );
83 }
84 
86 {
87  if ( !algorithm )
88  return false;
89 
90  if ( mAlgorithms.contains( algorithm->name() ) )
91  {
92  QgsMessageLog::logMessage( tr( "Duplicate algorithm name %1 for provider %2" ).arg( algorithm->name(), id() ), QObject::tr( "Processing" ) );
93  return false;
94  }
95 
96  // init the algorithm - this allows direct querying of the algorithm's parameters
97  // and outputs from the provider's copy
98  algorithm->initAlgorithm( QVariantMap() );
99 
100  algorithm->setProvider( this );
101  mAlgorithms.insert( algorithm->name(), algorithm );
102  return true;
103 }
104 
106 {
108 }
109 
111 {
113 }
114 
115 bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
116 {
117  error.clear();
118  QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context ).trimmed();
119 
120  if ( outputPath.isEmpty() )
121  {
123  {
124  return true;
125  }
126  else
127  {
128  error = tr( "Missing parameter value %1" ).arg( parameter->description() );
129  return false;
130  }
131  }
132 
135  {
136  if ( outputPath.startsWith( QLatin1String( "memory:" ) ) )
137  {
139  {
140  error = tr( "This algorithm only supports disk-based outputs" );
141  return false;
142  }
143  return true;
144  }
145 
146  QString providerKey;
147  QString uri;
148  QString layerName;
149  QMap<QString, QVariant> options;
150  bool useWriter = false;
151  QString format;
152  QString extension;
153  QgsProcessingUtils::parseDestinationString( outputPath, providerKey, uri, layerName, format, options, useWriter, extension );
154 
155  if ( providerKey != QLatin1String( "ogr" ) )
156  {
158  {
159  error = tr( "This algorithm only supports disk-based outputs" );
160  return false;
161  }
162  return true;
163  }
164 
165  if ( !supportedOutputVectorLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
166  {
167  error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
168  return false;
169  }
170  return true;
171  }
172  else if ( parameter->type() == QgsProcessingParameterRasterDestination::typeName() )
173  {
174  QFileInfo fi( outputPath );
175  const QString extension = fi.completeSuffix();
176  if ( !supportedOutputRasterLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
177  {
178  error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
179  return false;
180  }
181  return true;
182  }
183  else
184  {
185  return true;
186  }
187 }
188 
189 QString QgsProcessingProvider::defaultVectorFileExtension( bool hasGeometry ) const
190 {
191  QgsSettings settings;
192  const QString defaultExtension = hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
193  const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), defaultExtension, QgsSettings::Core ).toString();
194 
195  const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
196  if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
197  {
198  // user set default is supported by provider, use that
199  return userDefault;
200  }
201  else if ( !supportedExtensions.empty() )
202  {
203  return supportedExtensions.at( 0 );
204  }
205  else
206  {
207  // who knows? provider says it has no file support at all...
208  // let's say shp. even MapInfo supports shapefiles.
209  return defaultExtension;
210  }
211 }
212 
214 {
215  QgsSettings settings;
216  const QString defaultExtension = QStringLiteral( "tif" );
217  const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), defaultExtension, QgsSettings::Core ).toString();
218 
219  const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
220  if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
221  {
222  // user set default is supported by provider, use that
223  return userDefault;
224  }
225  else if ( !supportedExtensions.empty() )
226  {
227  return supportedExtensions.at( 0 );
228  }
229  else
230  {
231  // who knows? provider says it has no file support at all...
232  return defaultExtension;
233  }
234 }
235 
237 {
238  return true;
239 }
void setProvider(QgsProcessingProvider *provider)
Associates this algorithm with its provider.
virtual QStringList supportedOutputTableExtensions() const
Returns a list of the table (geometry-less vector layers) file extensions supported by this provider...
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI. ...
QList< const QgsProcessingAlgorithm *> algorithms() const
Returns a list of algorithms supplied by this provider.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
virtual QString helpId() const
Returns the provider help id string, used for creating QgsHelp urls for algorithms belong to this pro...
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported by this provider.
virtual QString versionInfo() const
Returns a version information string for the provider, or an empty string if this is not applicable (...
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
virtual bool supportsNonFileBasedOutput() const
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...
virtual QString svgIconPath() const
Returns a path to an SVG version of the provider&#39;s icon.
Base class for all parameter definitions which represent file or layer destinations, e.g.
Abstract base class for processing algorithms.
virtual void initAlgorithm(const QVariantMap &configuration=QVariantMap())=0
Initializes the algorithm using the specified configuration.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingProvider(QObject *parent=nullptr)
Constructor for QgsProcessingProvider.
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
virtual bool isActive() const
Returns true if the provider is active and able to run algorithms.
virtual QString id() const =0
Returns the unique provider id, used for identifying the provider.
Flags flags() const
Returns any flags associated with the parameter.
virtual QString defaultRasterFileExtension() const
Returns the default file extension to use for raster outputs created by the provider.
virtual void loadAlgorithms()=0
Loads all algorithms belonging to this provider.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
virtual QString type() const =0
Unique parameter type name.
void refreshAlgorithms()
Refreshes the algorithms available from the provider, causing it to re-populate with all associated a...
virtual QString defaultVectorFileExtension(bool hasGeometry=true) const
Returns the default file extension to use for vector outputs created by the provider.
const QgsProcessingAlgorithm * algorithm(const QString &name) const
Returns the matching algorithm by name, or nullptr if no matching algorithm is contained by this prov...
virtual bool isSupportedOutputValue(const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error) const
Returns true if the specified outputValue is of a supported file format for the given destination par...
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
bool addAlgorithm(QgsProcessingAlgorithm *algorithm)
Adds an algorithm to the provider.
virtual QIcon icon() const
Returns an icon for the provider.
virtual QString longName() const
Returns the longer version of the provider name, which can include extra details such as version numb...
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this provider.
Contains information about the context in which a processing algorithm is executed.
QString description() const
Returns the description for the parameter.
void algorithmsLoaded()
Emitted when the provider has loaded (or refreshed) its list of available algorithms.