QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 QgsVectorLayerExporter::ExportError::ErrProviderUnsupportedFeature;
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::createDb( const QString &, QString &errCause )
236 {
237  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
238  return false;
239 }
240 
242 {
243  return nullptr;
244 }
245 
246 QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
247 {
248  Q_UNUSED( cached );
249  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
250 }
251 
252 QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
253 {
254  return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
255 }
256 
258 {
259  const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
260  const QStringList constKeys { constConns.keys( ) };
261  for ( const QString &key : constKeys )
262  {
263  if ( key == name )
264  {
265  return constConns.value( key );
266  }
267  }
268  return nullptr;
269 }
270 
272 {
273  Q_UNUSED( name );
274  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
275 }
276 
277 
278 QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
279 {
280  Q_UNUSED( configuration );
281  Q_UNUSED( uri );
282  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
283 }
284 
285 void QgsProviderMetadata::deleteConnection( const QString &name )
286 {
287  Q_UNUSED( name );
288  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
289 }
290 
291 void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
292 {
293  Q_UNUSED( connection )
294  Q_UNUSED( name )
295  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
296 }
297 
299 void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
300 {
301  const bool isNewConnection = !connections().contains( name );
302  conn->store( name );
303  mProviderConnections.clear();
304 
305  if ( !isNewConnection )
306  emit connectionChanged( name );
307  else
308  emit connectionCreated( name );
309 }
311 
312 template<typename T>
313 QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
314 {
315  QMap<QString, T *> result;
316  const auto constConns { connections( cached ) };
317  const QStringList constConnKeys { constConns.keys() };
318  for ( const auto &c : constConnKeys )
319  {
320  T *casted { static_cast<T *>( constConns.value( c ) ) };
321  if ( casted )
322  {
323  result.insert( c, casted );
324  }
325  }
326  return result;
327 }
328 
330 
331 QgsMeshDriverMetadata::QgsMeshDriverMetadata( const QString &name, const QString &description, const MeshDriverCapabilities &capabilities, const QString &writeDatasetOnfileSuffix )
332  : mName( name ), mDescription( description ), mCapabilities( capabilities ), mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
333 {
334 }
335 
336 QgsMeshDriverMetadata::MeshDriverCapabilities QgsMeshDriverMetadata::capabilities() const
337 {
338  return mCapabilities;
339 }
340 
342 {
343  return mName;
344 }
345 
347 {
348  return mDescription;
349 }
350 
352 {
353  return mWriteDatasetOnFileSuffix;
354 }
DataType
Raster data types.
Definition: qgis.h:102
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
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 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 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 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 QgsVectorLayerExporter::ExportError 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 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.