QGIS API Documentation  3.17.0-Master (8af46bc54f)
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 
60 {
61  return mLibrary;
62 }
63 
65 {
66  return mCreateFunction;
67 }
68 
70 {
71 
72 }
73 
75 {
76 
77 }
78 
80 {
81  return QString();
82 }
83 
84 QList<QgsMeshDriverMetadata> QgsProviderMetadata::meshDriversMetadata()
85 {
86  return QList<QgsMeshDriverMetadata>();
87 }
88 
89 int QgsProviderMetadata::priorityForUri( const QString & ) const
90 {
91  return 0;
92 }
93 
94 QList<QgsMapLayerType> QgsProviderMetadata::validLayerTypesForUri( const QString & ) const
95 {
96  return QList<QgsMapLayerType>();
97 }
98 
99 bool QgsProviderMetadata::uriIsBlocklisted( const QString & ) const
100 {
101  return false;
102 }
103 
105  const QgsDataProvider::ProviderOptions &options,
106  QgsDataProvider::ReadFlags flags )
107 {
108  if ( mCreateFunction )
109  {
110  return mCreateFunction( uri, options, flags );
111  }
112  return nullptr;
113 }
114 
115 void QgsProviderMetadata::setBoolParameter( QVariantMap &uri, const QString &parameter, const QVariant &value )
116 {
117  if ( value.toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
118  value.toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
119  value.toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
120  {
121  uri[ parameter ] = true;
122  }
123  else if ( value.toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
124  value.toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
125  value.toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
126  {
127  uri[ parameter ] = false;
128  }
129 }
130 
131 bool QgsProviderMetadata::boolParameter( const QVariantMap &uri, const QString &parameter, bool defaultValue )
132 {
133  if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
134  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
135  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
136  {
137  return true;
138  }
139  else if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
140  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
141  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
142  {
143  return false;
144  }
145 
146  return defaultValue;
147 }
148 
149 QVariantMap QgsProviderMetadata::decodeUri( const QString & ) const
150 {
151  return QVariantMap();
152 }
153 
154 QString QgsProviderMetadata::encodeUri( const QVariantMap & ) const
155 {
156  return QString();
157 }
158 
160  const QString &, const QgsFields &,
162  bool, QMap<int, int> &,
163  QString &errorMessage, const QMap<QString, QVariant> * )
164 {
165  errorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createEmptyLayer" ) );
166  return QgsVectorLayerExporter::ExportError::ErrProviderUnsupportedFeature;
167 }
168 
170  const QString &, const QString &,
171  int, Qgis::DataType, int,
172  int, double *,
174  const QStringList & )
175 {
176  return nullptr;
177 }
178 
180  const QgsMesh &,
181  const QString,
182  const QString &,
183  const QgsCoordinateReferenceSystem & ) const
184 {
185  return false;
186 }
187 
188 QList<QPair<QString, QString> > QgsProviderMetadata::pyramidResamplingMethods()
189 {
190  return QList<QPair<QString, QString> >();
191 }
192 
193 QList<QgsDataItemProvider *> QgsProviderMetadata::dataItemProviders() const
194 {
195  return QList<QgsDataItemProvider *>();
196 }
197 
198 int QgsProviderMetadata::listStyles( const QString &, QStringList &, QStringList &,
199  QStringList &, QString &errCause )
200 {
201  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "listStyles" ) );
202  return -1;
203 }
204 
205 QString QgsProviderMetadata::getStyleById( const QString &, QString, QString &errCause )
206 {
207  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "getStyleById" ) );
208  return QString();
209 }
210 
211 bool QgsProviderMetadata::deleteStyleById( const QString &, QString, QString &errCause )
212 {
213  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteStyleById" ) );
214  return false;
215 }
216 
217 bool QgsProviderMetadata::saveStyle( const QString &, const QString &, const QString &, const QString &,
218  const QString &, const QString &, bool, QString &errCause )
219 {
220  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveStyle" ) );
221  return false;
222 }
223 
224 QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
225 {
226  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStyle" ) );
227  return QString();
228 }
229 
230 bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
231 {
232  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
233  return false;
234 }
235 
237 {
238  return nullptr;
239 }
240 
241 QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
242 {
243  Q_UNUSED( cached );
244  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
245 }
246 
247 QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
248 {
249  return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
250 }
251 
253 {
254  const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
255  const QStringList constKeys { constConns.keys( ) };
256  for ( const QString &key : constKeys )
257  {
258  if ( key == name )
259  {
260  return constConns.value( key );
261  }
262  }
263  return nullptr;
264 }
265 
267 {
268  Q_UNUSED( name );
269  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
270 }
271 
272 
273 QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
274 {
275  Q_UNUSED( configuration );
276  Q_UNUSED( uri );
277  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
278 }
279 
280 void QgsProviderMetadata::deleteConnection( const QString &name )
281 {
282  Q_UNUSED( name );
283  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
284 }
285 
286 void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
287 {
288  Q_UNUSED( connection )
289  Q_UNUSED( name )
290  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
291 }
292 
294 void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
295 {
296  const bool isNewConnection = !connections().contains( name );
297  conn->store( name );
298  mProviderConnections.clear();
299 
300  if ( !isNewConnection )
301  emit connectionChanged( name );
302  else
303  emit connectionCreated( name );
304 }
306 
307 template<typename T>
308 QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
309 {
310  QMap<QString, T *> result;
311  const auto constConns { connections( cached ) };
312  const QStringList constConnKeys { constConns.keys() };
313  for ( const auto &c : constConnKeys )
314  {
315  T *casted { static_cast<T *>( constConns.value( c ) ) };
316  if ( casted )
317  {
318  result.insert( c, casted );
319  }
320  }
321  return result;
322 }
323 
325 
326 QgsMeshDriverMetadata::QgsMeshDriverMetadata( const QString &name, const QString &description, const MeshDriverCapabilities &capabilities, const QString &writeDatasetOnfileSuffix )
327  : mName( name ), mDescription( description ), mCapabilities( capabilities ), mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
328 {
329 }
330 
331 QgsMeshDriverMetadata::MeshDriverCapabilities QgsMeshDriverMetadata::capabilities() const
332 {
333  return mCapabilities;
334 }
335 
337 {
338  return mName;
339 }
340 
342 {
343  return mDescription;
344 }
345 
347 {
348  return mWriteDatasetOnFileSuffix;
349 }
QString name() const
Returns the name (key) for this driver.
virtual QgsTransaction * createTransaction(const QString &connString)
Returns new instance of transaction.
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...
QString description() const
This returns descriptive text for the provider.
MeshDriverCapabilities capabilities() const
Returns the capabilities for this driver.
virtual QString getStyleById(const QString &uri, QString styleId, QString &errCause)
Gets a layer style defined by uri.
QString key() const
This returns the unique key associated with the provider.
virtual void store(const QString &name) const =0
Stores the connection in the settings.
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.
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...
Q_DECL_DEPRECATED QString library() const
This returns the library file name.
DataType
Raster data types.
Definition: qgis.h:101
virtual bool createDb(const QString &dbPath, QString &errCause)
Creates database by the provider on the path.
Container of fields for a vector layer.
Definition: qgsfields.h:44
virtual int priorityForUri(const QString &uri) const
Returns an integer representing the priority which this provider should have when opening a dataset w...
void connectionCreated(const QString &name)
Emitted when a connection with the specified name is created.
Abstract base class for spatial data provider implementations.
virtual ~QgsProviderMetadata()
dtor
The QgsAbstractProviderConnection provides an interface for data provider connections.
virtual void deleteConnection(const QString &name) SIP_THROW(QgsProviderConnectionException)
Removes the connection with the given name from the settings.
virtual QVariantMap decodeUri(const QString &uri) const
Breaks a provider data source URI into its component paths (e.g.
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 QString filters(FilterType type)
Builds the list of file filter strings (supported formats)
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
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
void connectionChanged(const QString &name)
Emitted when the connection with the specified name is changed, e.g.
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...
QMap< QString, QgsAbstractDatabaseProviderConnection * > dbConnections(bool cached=true) SIP_THROW(QgsProviderConnectionException)
Returns a dictionary of database provider connections, the dictionary key is the connection identifie...
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 QList< QPair< QString, QString > > pyramidResamplingMethods()
Returns pyramid resampling methods available for provider.
virtual QString loadStyle(const QString &uri, QString &errCause)
Loads a layer style defined by uri.
virtual void initProvider()
Initialize the provider.
Q_DECL_DEPRECATED CreateDataProviderFunction createFunction() const
Returns a pointer to the direct provider creation function, if supported by the provider.
QString writeDatasetOnFileSuffix() const
Returns the suffix used to write datasets on file.
static bool boolParameter(const QVariantMap &uri, const QString &parameter, bool defaultValue=false)
Returns the parameter value in the uri as a bool.
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.
QgsAbstractProviderConnection * findConnection(const QString &name, bool cached=true) SIP_THROW(QgsProviderConnectionException)
Searches and returns a (possibly NULL) connection from the stored provider connections.
virtual QString encodeUri(const QVariantMap &parts) const
Reassembles a provider data source URI from its component paths (e.g.
static void setBoolParameter(QVariantMap &uri, const QString &parameter, const QVariant &value)
Sets the value into the uri parameter as a bool.
QgsProviderMetadata(const QString &key, const QString &description, const QString &library=QString())
Constructor for provider metadata.
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.
Setting options for creating vector data providers.
Mesh - vertices, edges and faces.
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 QgsProviderMetadata::ProviderMetadataCapabilities capabilities() const
Returns the provider metadata capabilities.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
virtual QList< QgsDataItemProvider *> dataItemProviders() const
Returns data item providers.
std::function< QgsDataProvider *(const QString &, const QgsDataProvider::ProviderOptions &, QgsDataProvider::ReadFlags &) > CreateDataProviderFunction
Typedef for data provider creation function.
This class represents a coordinate reference system (CRS).
virtual void cleanupProvider()
Cleanup the provider.
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 void saveConnection(const QgsAbstractProviderConnection *connection, const QString &name) SIP_THROW(QgsProviderConnectionException)
Stores the connection in the settings.
FilterType
Type of file filters.
virtual bool deleteStyleById(const QString &uri, QString styleId, QString &errCause)
Deletes a layer style defined by styleId.
virtual QList< QgsMeshDriverMetadata > meshDriversMetadata()
Builds the list of available mesh drivers metadata.
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:100
QString description() const
Returns the description for this driver.
Base class for raster data providers.
QgsMeshDriverMetadata()
Constructs default metadata without any capabilities.