QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 {
57 }
58 
60 {
61  qDeleteAll( mAlgorithms );
62  mAlgorithms.clear();
63  if ( isActive() )
64  {
66  emit algorithmsLoaded();
67  }
68 }
69 
70 QList<const QgsProcessingAlgorithm *> QgsProcessingProvider::algorithms() const
71 {
72  return mAlgorithms.values();
73 }
74 
76 {
77  return mAlgorithms.value( name );
78 }
79 
81 {
82  if ( !algorithm )
83  return false;
84 
85  if ( mAlgorithms.contains( algorithm->name() ) )
86  {
87  QgsMessageLog::logMessage( tr( "Duplicate algorithm name %1 for provider %2" ).arg( algorithm->name(), id() ), QObject::tr( "Processing" ) );
88  return false;
89  }
90 
91  // init the algorithm - this allows direct querying of the algorithm's parameters
92  // and outputs from the provider's copy
93  algorithm->initAlgorithm( QVariantMap() );
94 
95  algorithm->setProvider( this );
96  mAlgorithms.insert( algorithm->name(), algorithm );
97  return true;
98 }
99 
101 {
103 }
104 
106 {
108 }
109 
110 bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
111 {
112  error.clear();
113  QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context ).trimmed();
114 
115  if ( outputPath.isEmpty() )
116  {
118  {
119  return true;
120  }
121  else
122  {
123  error = tr( "Missing parameter value %1" ).arg( parameter->description() );
124  return false;
125  }
126  }
127 
130  {
131  if ( outputPath.startsWith( QLatin1String( "memory:" ) ) )
132  {
134  {
135  error = tr( "This algorithm only supports disk-based outputs" );
136  return false;
137  }
138  return true;
139  }
140 
141  QString providerKey;
142  QString uri;
143  QString layerName;
144  QMap<QString, QVariant> options;
145  bool useWriter = false;
146  QString format;
147  QString extension;
148  QgsProcessingUtils::parseDestinationString( outputPath, providerKey, uri, layerName, format, options, useWriter, extension );
149 
150  if ( providerKey != QLatin1String( "ogr" ) )
151  {
153  {
154  error = tr( "This algorithm only supports disk-based outputs" );
155  return false;
156  }
157  return true;
158  }
159 
160  if ( !supportedOutputVectorLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
161  {
162  error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
163  return false;
164  }
165  return true;
166  }
167  else if ( parameter->type() == QgsProcessingParameterRasterDestination::typeName() )
168  {
169  QFileInfo fi( outputPath );
170  const QString extension = fi.completeSuffix();
171  if ( !supportedOutputRasterLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
172  {
173  error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
174  return false;
175  }
176  return true;
177  }
178  else
179  {
180  return true;
181  }
182 }
183 
184 QString QgsProcessingProvider::defaultVectorFileExtension( bool hasGeometry ) const
185 {
186  QgsSettings settings;
187  const QString defaultExtension = hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
188  const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), defaultExtension, QgsSettings::Core ).toString();
189 
190  const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
191  if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
192  {
193  // user set default is supported by provider, use that
194  return userDefault;
195  }
196  else if ( !supportedExtensions.empty() )
197  {
198  return supportedExtensions.at( 0 );
199  }
200  else
201  {
202  // who knows? provider says it has no file support at all...
203  // let's say shp. even MapInfo supports shapefiles.
204  return defaultExtension;
205  }
206 }
207 
209 {
210  QgsSettings settings;
211  const QString defaultExtension = QStringLiteral( "tif" );
212  const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), defaultExtension, QgsSettings::Core ).toString();
213 
214  const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
215  if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
216  {
217  // user set default is supported by provider, use that
218  return userDefault;
219  }
220  else if ( !supportedExtensions.empty() )
221  {
222  return supportedExtensions.at( 0 );
223  }
224  else
225  {
226  // who knows? provider says it has no file support at all...
227  return defaultExtension;
228  }
229 }
230 
232 {
233  return true;
234 }
void setProvider(QgsProcessingProvider *provider)
Associates this algorithm with its provider.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format 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. ...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of algorithms supplied by this provider.
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...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Base class for all parameter definitions which represent file or layer destinations, e.g.
Abstract base class for processing algorithms.
virtual bool isActive() const
Returns true if the provider is active and able to run algorithms.
virtual QString defaultVectorFileExtension(bool hasGeometry=true) const
Returns the default file extension to use for vector outputs created by the provider.
virtual QString helpId() const
Returns the provider help id string, used for creating QgsHelp urls for algorithms belong to this pro...
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.
virtual QString svgIconPath() const
Returns a path to an SVG version of the provider&#39;s icon.
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
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 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.
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported by this provider.
void refreshAlgorithms()
Refreshes the algorithms available from the provider, causing it to re-populate with all associated a...
QString description() const
Returns the description for the parameter.
virtual QString longName() const
Returns the longer version of the provider name, which can include extra details such as version numb...
virtual QStringList supportedOutputTableExtensions() const
Returns a list of the table (geometry-less vector layers) file extensions supported by this provider...
const QgsProcessingAlgorithm * algorithm(const QString &name) const
Returns the matching algorithm by name, or a nullptr if no matching algorithm is contained by this pr...
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.
Contains information about the context in which a processing algorithm is executed.
virtual QIcon icon() const
Returns an icon for the provider.
virtual QString defaultRasterFileExtension() const
Returns the default file extension to use for raster outputs created by the provider.
void algorithmsLoaded()
Emitted when the provider has loaded (or refreshed) its list of available algorithms.
virtual bool supportsNonFileBasedOutput() const
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...