QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
24 
26  QString const &description,
27  QString const &library )
28  : mKey( key )
29  , mDescription( description )
30  , mLibrary( library )
31 {}
32 
33 QgsProviderMetadata::QgsProviderMetadata( const QString &key, const QString &description, const CreateDataProviderFunction &createFunc )
34  : mKey( key )
35  , mDescription( description )
36  , mCreateFunction( createFunc )
37 {}
38 
40 {
41  qDeleteAll( mProviderConnections );
42 }
43 
44 QString QgsProviderMetadata::key() const
45 {
46  return mKey;
47 }
48 
50 {
51  return mDescription;
52 }
53 
54 QgsProviderMetadata::ProviderMetadataCapabilities QgsProviderMetadata::capabilities() const
55 {
56  return QgsProviderMetadata::ProviderMetadataCapabilities();
57 }
58 
59 QgsProviderMetadata::ProviderCapabilities QgsProviderMetadata::providerCapabilities() const
60 {
61  return QgsProviderMetadata::ProviderCapabilities();
62 }
63 
65 {
66  return mLibrary;
67 }
68 
70 {
71  return mCreateFunction;
72 }
73 
75 {
76 
77 }
78 
80 {
81 
82 }
83 
85 {
86  return QString();
87 }
88 
89 QList<QgsMeshDriverMetadata> QgsProviderMetadata::meshDriversMetadata()
90 {
91  return QList<QgsMeshDriverMetadata>();
92 }
93 
94 int QgsProviderMetadata::priorityForUri( const QString & ) const
95 {
96  return 0;
97 }
98 
99 QList<QgsMapLayerType> QgsProviderMetadata::validLayerTypesForUri( const QString & ) const
100 {
101  return QList<QgsMapLayerType>();
102 }
103 
104 bool QgsProviderMetadata::uriIsBlocklisted( const QString & ) const
105 {
106  return false;
107 }
108 
110  const QgsDataProvider::ProviderOptions &options,
111  QgsDataProvider::ReadFlags flags )
112 {
113  if ( mCreateFunction )
114  {
115  return mCreateFunction( uri, options, flags );
116  }
117  return nullptr;
118 }
119 
120 void QgsProviderMetadata::setBoolParameter( QVariantMap &uri, const QString &parameter, const QVariant &value )
121 {
122  if ( value.toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
123  value.toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
124  value.toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
125  {
126  uri[ parameter ] = true;
127  }
128  else if ( value.toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
129  value.toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
130  value.toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
131  {
132  uri[ parameter ] = false;
133  }
134 }
135 
136 bool QgsProviderMetadata::boolParameter( const QVariantMap &uri, const QString &parameter, bool defaultValue )
137 {
138  if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
139  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
140  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
141  {
142  return true;
143  }
144  else if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
145  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
146  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
147  {
148  return false;
149  }
150 
151  return defaultValue;
152 }
153 
154 QVariantMap QgsProviderMetadata::decodeUri( const QString & ) const
155 {
156  return QVariantMap();
157 }
158 
159 QString QgsProviderMetadata::encodeUri( const QVariantMap & ) const
160 {
161  return QString();
162 }
163 
165  const QString &, const QgsFields &,
167  bool, QMap<int, int> &,
168  QString &errorMessage, const QMap<QString, QVariant> * )
169 {
170  errorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createEmptyLayer" ) );
171  return Qgis::VectorExportResult::ErrorProviderUnsupportedFeature;
172 }
173 
175  const QString &, const QString &,
176  int, Qgis::DataType, int,
177  int, double *,
179  const QStringList & )
180 {
181  return nullptr;
182 }
183 
185  const QgsMesh &,
186  const QString,
187  const QString &,
188  const QgsCoordinateReferenceSystem & ) const
189 {
190  return false;
191 }
192 
193 QList<QPair<QString, QString> > QgsProviderMetadata::pyramidResamplingMethods()
194 {
195  return QList<QPair<QString, QString> >();
196 }
197 
198 QList<QgsDataItemProvider *> QgsProviderMetadata::dataItemProviders() const
199 {
200  return QList<QgsDataItemProvider *>();
201 }
202 
203 int QgsProviderMetadata::listStyles( const QString &, QStringList &, QStringList &,
204  QStringList &, QString &errCause )
205 {
206  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "listStyles" ) );
207  return -1;
208 }
209 
210 QString QgsProviderMetadata::getStyleById( const QString &, QString, QString &errCause )
211 {
212  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "getStyleById" ) );
213  return QString();
214 }
215 
216 bool QgsProviderMetadata::deleteStyleById( const QString &, QString, QString &errCause )
217 {
218  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteStyleById" ) );
219  return false;
220 }
221 
222 bool QgsProviderMetadata::saveStyle( const QString &, const QString &, const QString &, const QString &,
223  const QString &, const QString &, bool, QString &errCause )
224 {
225  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveStyle" ) );
226  return false;
227 }
228 
229 QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
230 {
231  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStyle" ) );
232  return QString();
233 }
234 
235 bool QgsProviderMetadata::saveLayerMetadata( const QString &, const QgsLayerMetadata &, QString & )
236 {
237  throw QgsNotSupportedException( QObject::tr( "Provider %1 does not support writing layer metadata" ).arg( key() ) );
238 }
239 
240 bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
241 {
242  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
243  return false;
244 }
245 
247 {
248  return nullptr;
249 }
250 
251 QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
252 {
253  Q_UNUSED( cached );
254  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
255 }
256 
257 QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
258 {
259  return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
260 }
261 
263 {
264  const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
265  const QStringList constKeys { constConns.keys( ) };
266  for ( const QString &key : constKeys )
267  {
268  if ( key == name )
269  {
270  return constConns.value( key );
271  }
272  }
273  return nullptr;
274 }
275 
277 {
278  Q_UNUSED( name );
279  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
280 }
281 
282 
283 QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
284 {
285  Q_UNUSED( configuration );
286  Q_UNUSED( uri );
287  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
288 }
289 
290 void QgsProviderMetadata::deleteConnection( const QString &name )
291 {
292  Q_UNUSED( name );
293  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
294 }
295 
296 void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
297 {
298  Q_UNUSED( connection )
299  Q_UNUSED( name )
300  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
301 }
302 
304 void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
305 {
306  const bool isNewConnection = !connections().contains( name );
307  conn->store( name );
308  mProviderConnections.clear();
309 
310  if ( !isNewConnection )
311  emit connectionChanged( name );
312  else
313  emit connectionCreated( name );
314 }
316 
317 template<typename T>
318 QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
319 {
320  QMap<QString, T *> result;
321  const auto constConns { connections( cached ) };
322  const QStringList constConnKeys { constConns.keys() };
323  for ( const auto &c : constConnKeys )
324  {
325  T *casted { static_cast<T *>( constConns.value( c ) ) };
326  if ( casted )
327  {
328  result.insert( c, casted );
329  }
330  }
331  return result;
332 }
333 
335 
336 QgsMeshDriverMetadata::QgsMeshDriverMetadata( const QString &name, const QString &description, const MeshDriverCapabilities &capabilities, const QString &writeDatasetOnfileSuffix )
337  : mName( name ), mDescription( description ), mCapabilities( capabilities ), mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
338 {
339 }
340 
341 QgsMeshDriverMetadata::MeshDriverCapabilities QgsMeshDriverMetadata::capabilities() const
342 {
343  return mCapabilities;
344 }
345 
347 {
348  return mName;
349 }
350 
352 {
353  return mDescription;
354 }
355 
357 {
358  return mWriteDatasetOnFileSuffix;
359 }
VectorExportResult
Vector layer export result codes.
Definition: qgis.h:316
DataType
Raster data types.
Definition: qgis.h:119
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.
Container of fields for a vector layer.
Definition: qgsfields.h:45
A structured metadata store for a map layer.
QString writeDatasetOnFileSuffix() const
Returns the suffix used to write datasets on file.
MeshDriverCapabilities capabilities() const
Returns the capabilities for this driver.
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 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.
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 bool createMeshData(const QgsMesh &mesh, const QString uri, const QString &driverName, const QgsCoordinateReferenceSystem &crs) const
Creates mesh data source, that is the mesh frame stored in file, memory or with other way (depending ...
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 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.