QGIS API Documentation  3.21.0-Master (5b68dc587e)
qgsprovidermetadata.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprovidermetadata.cpp - Metadata class for
3  describing a data provider.
4  -------------------
5  begin : Sat Jan 10 2004
6  copyright : (C) 2004 by Gary E.Sherman
7  email : sherman at mrcc.com
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "qgsprovidermetadata.h"
20 #include "qgsdataprovider.h"
21 #include "qgsmaplayer.h"
22 #include "qgsexception.h"
25 
27  QString const &description,
28  QString const &library )
29  : mKey( key )
30  , mDescription( description )
31  , mLibrary( library )
32 {}
33 
34 QgsProviderMetadata::QgsProviderMetadata( const QString &key, const QString &description, const CreateDataProviderFunction &createFunc )
35  : mKey( key )
36  , mDescription( description )
37  , mCreateFunction( createFunc )
38 {}
39 
41 {
42  qDeleteAll( mProviderConnections );
43 }
44 
45 QString QgsProviderMetadata::key() const
46 {
47  return mKey;
48 }
49 
51 {
52  return mDescription;
53 }
54 
55 QgsProviderMetadata::ProviderMetadataCapabilities QgsProviderMetadata::capabilities() const
56 {
57  return QgsProviderMetadata::ProviderMetadataCapabilities();
58 }
59 
60 QgsProviderMetadata::ProviderCapabilities QgsProviderMetadata::providerCapabilities() const
61 {
62  return QgsProviderMetadata::ProviderCapabilities();
63 }
64 
66 {
67  return mLibrary;
68 }
69 
71 {
72  return mCreateFunction;
73 }
74 
76 {
77 
78 }
79 
81 {
82 
83 }
84 
86 {
87  return QString();
88 }
89 
90 QList<QgsMeshDriverMetadata> QgsProviderMetadata::meshDriversMetadata()
91 {
92  return QList<QgsMeshDriverMetadata>();
93 }
94 
95 int QgsProviderMetadata::priorityForUri( const QString & ) const
96 {
97  return 0;
98 }
99 
100 QList<QgsMapLayerType> QgsProviderMetadata::validLayerTypesForUri( const QString & ) const
101 {
102  return QList<QgsMapLayerType>();
103 }
104 
105 bool QgsProviderMetadata::uriIsBlocklisted( const QString & ) const
106 {
107  return false;
108 }
109 
110 QStringList QgsProviderMetadata::sidecarFilesForUri( const QString & ) const
111 {
112  return QStringList();
113 }
114 
115 QList<QgsProviderSublayerDetails> QgsProviderMetadata::querySublayers( const QString &, Qgis::SublayerQueryFlags, QgsFeedback * ) const
116 {
117  return QList<QgsProviderSublayerDetails>();
118 }
119 
121  const QgsDataProvider::ProviderOptions &options,
122  QgsDataProvider::ReadFlags flags )
123 {
124  if ( mCreateFunction )
125  {
126  return mCreateFunction( uri, options, flags );
127  }
128  return nullptr;
129 }
130 
131 void QgsProviderMetadata::setBoolParameter( QVariantMap &uri, const QString &parameter, const QVariant &value )
132 {
133  if ( value.toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
134  value.toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
135  value.toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
136  {
137  uri[ parameter ] = true;
138  }
139  else if ( value.toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
140  value.toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
141  value.toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
142  {
143  uri[ parameter ] = false;
144  }
145 }
146 
147 bool QgsProviderMetadata::boolParameter( const QVariantMap &uri, const QString &parameter, bool defaultValue )
148 {
149  if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
150  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
151  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
152  {
153  return true;
154  }
155  else if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
156  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
157  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
158  {
159  return false;
160  }
161 
162  return defaultValue;
163 }
164 
165 QVariantMap QgsProviderMetadata::decodeUri( const QString & ) const
166 {
167  return QVariantMap();
168 }
169 
170 QString QgsProviderMetadata::encodeUri( const QVariantMap & ) const
171 {
172  return QString();
173 }
174 
176  const QString &, const QgsFields &,
178  bool, QMap<int, int> &,
179  QString &errorMessage, const QMap<QString, QVariant> * )
180 {
181  errorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createEmptyLayer" ) );
182  return Qgis::VectorExportResult::ErrorProviderUnsupportedFeature;
183 }
184 
186  const QString &, const QString &,
187  int, Qgis::DataType, int,
188  int, double *,
190  const QStringList & )
191 {
192  return nullptr;
193 }
194 
196  const QString &,
197  const QString &,
198  const QgsCoordinateReferenceSystem & ) const
199 {
200  return false;
201 }
202 
204  const QString &,
205  const QgsCoordinateReferenceSystem & ) const
206 {
207  return false;
208 }
209 
210 QList<QPair<QString, QString> > QgsProviderMetadata::pyramidResamplingMethods()
211 {
212  return QList<QPair<QString, QString> >();
213 }
214 
215 QList<QgsDataItemProvider *> QgsProviderMetadata::dataItemProviders() const
216 {
217  return QList<QgsDataItemProvider *>();
218 }
219 
220 int QgsProviderMetadata::listStyles( const QString &, QStringList &, QStringList &,
221  QStringList &, QString &errCause )
222 {
223  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "listStyles" ) );
224  return -1;
225 }
226 
227 QString QgsProviderMetadata::getStyleById( const QString &, QString, QString &errCause )
228 {
229  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "getStyleById" ) );
230  return QString();
231 }
232 
233 bool QgsProviderMetadata::deleteStyleById( const QString &, QString, QString &errCause )
234 {
235  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteStyleById" ) );
236  return false;
237 }
238 
239 bool QgsProviderMetadata::saveStyle( const QString &, const QString &, const QString &, const QString &,
240  const QString &, const QString &, bool, QString &errCause )
241 {
242  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveStyle" ) );
243  return false;
244 }
245 
246 QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
247 {
248  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStyle" ) );
249  return QString();
250 }
251 
252 bool QgsProviderMetadata::saveLayerMetadata( const QString &, const QgsLayerMetadata &, QString & )
253 {
254  throw QgsNotSupportedException( QObject::tr( "Provider %1 does not support writing layer metadata" ).arg( key() ) );
255 }
256 
257 bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
258 {
259  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
260  return false;
261 }
262 
264 {
265  return nullptr;
266 }
267 
268 QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
269 {
270  Q_UNUSED( cached );
271  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
272 }
273 
274 QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
275 {
276  return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
277 }
278 
280 {
281  const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
282  const QStringList constKeys { constConns.keys( ) };
283  for ( const QString &key : constKeys )
284  {
285  if ( key == name )
286  {
287  return constConns.value( key );
288  }
289  }
290  return nullptr;
291 }
292 
294 {
295  Q_UNUSED( name );
296  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
297 }
298 
299 
300 QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
301 {
302  Q_UNUSED( configuration );
303  Q_UNUSED( uri );
304  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
305 }
306 
307 void QgsProviderMetadata::deleteConnection( const QString &name )
308 {
309  Q_UNUSED( name );
310  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
311 }
312 
313 void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
314 {
315  Q_UNUSED( connection )
316  Q_UNUSED( name )
317  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
318 }
319 
321 void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
322 {
323  const bool isNewConnection = !connections().contains( name );
324  conn->store( name );
325  mProviderConnections.clear();
326 
327  if ( !isNewConnection )
328  emit connectionChanged( name );
329  else
330  emit connectionCreated( name );
331 }
333 
334 template<typename T>
335 QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
336 {
337  QMap<QString, T *> result;
338  const auto constConns { connections( cached ) };
339  const QStringList constConnKeys { constConns.keys() };
340  for ( const auto &c : constConnKeys )
341  {
342  T *casted { static_cast<T *>( constConns.value( c ) ) };
343  if ( casted )
344  {
345  result.insert( c, casted );
346  }
347  }
348  return result;
349 }
350 
352 
354  const QString &description,
355  const MeshDriverCapabilities &capabilities,
356  const QString &writeDatasetOnfileSuffix )
357  : mName( name )
358  , mDescription( description )
359  , mCapabilities( capabilities )
360  , mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
361 {
362 }
363 
365  const QString &description,
366  const MeshDriverCapabilities &capabilities,
367  const QString &writeDatasetOnfileSuffix,
368  const QString &writeMeshFrameOnFileSuffix,
369  int maxVerticesPerface )
370  : mName( name )
371  , mDescription( description )
372  , mCapabilities( capabilities )
373  , mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
374  , mWriteMeshFrameOnFileSuffix( ( writeMeshFrameOnFileSuffix ) )
375  , mMaxVerticesPerFace( maxVerticesPerface )
376 {
377 }
378 
379 QgsMeshDriverMetadata::MeshDriverCapabilities QgsMeshDriverMetadata::capabilities() const
380 {
381  return mCapabilities;
382 }
383 
385 {
386  return mName;
387 }
388 
390 {
391  return mDescription;
392 }
393 
395 {
396  return mWriteDatasetOnFileSuffix;
397 }
398 
400 {
401  return mWriteMeshFrameOnFileSuffix;
402 }
403 
405 {
406  return mMaxVerticesPerFace;
407 }
VectorExportResult
Vector layer export result codes.
Definition: qgis.h:347
DataType
Raster data types.
Definition: qgis.h:120
The QgsAbstractProviderConnection provides an interface for data provider connections.
virtual void store(const QString &name) const =0
Stores the connection in the settings.
This class represents a coordinate reference system (CRS).
Abstract base class for spatial data provider implementations.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Container of fields for a vector layer.
Definition: qgsfields.h:45
A structured metadata store for a map layer.
int maximumVerticesCountPerFace() const
Returns the maximum number of vertices per face supported by the driver.
QString writeDatasetOnFileSuffix() const
Returns the suffix used to write datasets on file.
MeshDriverCapabilities capabilities() const
Returns the capabilities for this driver.
QString writeMeshFrameOnFileSuffix() const
Returns the suffix used to write mesh on file.
QString description() const
Returns the description for this driver.
QgsMeshDriverMetadata()
Constructs default metadata without any capabilities.
QString name() const
Returns the name (key) for this driver.
Custom exception class which is raised when an operation is not supported.
Definition: qgsexception.h:118
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:101
virtual bool uriIsBlocklisted(const QString &uri) const
Returns true if the specified uri is known by this provider to be something which should be blocklist...
virtual QgsProviderMetadata::ProviderCapabilities providerCapabilities() const
Returns the provider's capabilities.
static void setBoolParameter(QVariantMap &uri, const QString &parameter, const QVariant &value)
Sets the value into the uri parameter as a bool.
virtual QgsRasterDataProvider * createRasterDataProvider(const QString &uri, const QString &format, int nBands, Qgis::DataType type, int width, int height, double *geoTransform, const QgsCoordinateReferenceSystem &crs, const QStringList &createOptions=QStringList())
Creates a new instance of the raster data provider.
Q_DECL_DEPRECATED CreateDataProviderFunction createFunction() const
Returns a pointer to the direct provider creation function, if supported by the provider.
QgsAbstractProviderConnection * findConnection(const QString &name, bool cached=true) SIP_THROW(QgsProviderConnectionException)
Searches and returns a (possibly NULL) connection from the stored provider connections.
virtual int priorityForUri(const QString &uri) const
Returns an integer representing the priority which this provider should have when opening a dataset w...
FilterType
Type of file filters.
static bool boolParameter(const QVariantMap &uri, const QString &parameter, bool defaultValue=false)
Returns the parameter value in the uri as a bool.
std::function< QgsDataProvider *(const QString &, const QgsDataProvider::ProviderOptions &, QgsDataProvider::ReadFlags &) > CreateDataProviderFunction
Typedef for data provider creation function.
virtual ~QgsProviderMetadata()
dtor
virtual bool saveStyle(const QString &uri, const QString &qmlStyle, const QString &sldStyle, const QString &styleName, const QString &styleDescription, const QString &uiFileContent, bool useAsDefault, QString &errCause)
Saves a layer style to provider.
virtual QgsDataProvider * createProvider(const QString &uri, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags=QgsDataProvider::ReadFlags())
Class factory to return a pointer to a newly created QgsDataProvider object.
virtual QString filters(FilterType type)
Builds the list of file filter strings (supported formats)
virtual bool saveLayerMetadata(const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage) SIP_THROW(QgsNotSupportedException)
Saves metadata to the layer corresponding to the specified uri.
virtual QString encodeUri(const QVariantMap &parts) const
Reassembles a provider data source URI from its component paths (e.g.
virtual bool deleteStyleById(const QString &uri, QString styleId, QString &errCause)
Deletes a layer style defined by styleId.
QMap< QString, QgsAbstractDatabaseProviderConnection * > dbConnections(bool cached=true) SIP_THROW(QgsProviderConnectionException)
Returns a dictionary of database provider connections, the dictionary key is the connection identifie...
QString description() const
This returns descriptive text for the provider.
virtual bool createMeshData(const QgsMesh &mesh, const QString &fileName, const QString &driverName, const QgsCoordinateReferenceSystem &crs) const
Creates mesh data source from a file name fileName and a driver driverName, that is the mesh frame st...
virtual QString getStyleById(const QString &uri, QString styleId, QString &errCause)
Gets a layer style defined by uri.
void connectionChanged(const QString &name)
Emitted when the connection with the specified name is changed, e.g.
virtual QStringList sidecarFilesForUri(const QString &uri) const
Given a uri, returns any sidecar files which are associated with the URI and this provider.
void connectionCreated(const QString &name)
Emitted when a connection with the specified name is created.
virtual void saveConnection(const QgsAbstractProviderConnection *connection, const QString &name) SIP_THROW(QgsProviderConnectionException)
Stores the connection in the settings.
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration) SIP_THROW(QgsProviderConnectionException)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
virtual Qgis::VectorExportResult createEmptyLayer(const QString &uri, const QgsFields &fields, QgsWkbTypes::Type wkbType, const QgsCoordinateReferenceSystem &srs, bool overwrite, QMap< int, int > &oldToNewAttrIdxMap, QString &errorMessage, const QMap< QString, QVariant > *options)
Creates new empty vector layer.
virtual void deleteConnection(const QString &name) SIP_THROW(QgsProviderConnectionException)
Removes the connection with the given name from the settings.
QString key() const
This returns the unique key associated with the provider.
virtual QgsTransaction * createTransaction(const QString &connString)
Returns new instance of transaction.
virtual void initProvider()
Initialize the provider.
QgsProviderMetadata(const QString &key, const QString &description, const QString &library=QString())
Constructor for provider metadata.
virtual QList< QgsProviderSublayerDetails > querySublayers(const QString &uri, Qgis::SublayerQueryFlags flags=Qgis::SublayerQueryFlags(), QgsFeedback *feedback=nullptr) const
Queries the specified uri and returns a list of any valid sublayers found in the dataset which can be...
virtual QgsProviderMetadata::ProviderMetadataCapabilities capabilities() const
Returns the provider metadata capabilities.
virtual void cleanupProvider()
Cleanup the provider.
virtual QString loadStyle(const QString &uri, QString &errCause)
Loads a layer style defined by uri.
virtual int listStyles(const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause)
Lists stored layer styles in the provider defined by uri.
virtual QList< QPair< QString, QString > > pyramidResamplingMethods()
Returns pyramid resampling methods available for provider.
virtual QList< QgsMapLayerType > validLayerTypesForUri(const QString &uri) const
Returns a list of valid layer types which the provider can be used with when opening the specified ur...
virtual QList< QgsDataItemProvider * > dataItemProviders() const
Returns data item providers.
virtual QMap< QString, QgsAbstractProviderConnection * > connections(bool cached=true) SIP_THROW(QgsProviderConnectionException)
Returns a dictionary of stored provider connections, the dictionary key is the connection identifier.
virtual QList< QgsMeshDriverMetadata > meshDriversMetadata()
Builds the list of available mesh drivers metadata.
virtual QVariantMap decodeUri(const QString &uri) const
Breaks a provider data source URI into its component paths (e.g.
Q_DECL_DEPRECATED QString library() const
This returns the library file name.
virtual bool createDb(const QString &dbPath, QString &errCause)
Creates database by the provider on the path.
Base class for raster data providers.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Setting options for creating vector data providers.
Mesh - vertices, edges and faces.