QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
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
34QgsProviderMetadata::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
46{
47 return mKey;
48}
49
51{
52 return mDescription;
53}
54
56{
57 return QIcon();
58}
59
64
69
70QList<Qgis::LayerType> QgsProviderMetadata::supportedLayerTypes() const
71{
72 return {};
73}
74
76{
77 return mLibrary;
78}
79
80QString QgsProviderMetadata::suggestGroupNameForUri( const QString & /*uri*/ ) const
81{
82 return QString();
83}
84
89
94
99
101{
102 return QString();
103}
104
105QList<QgsMeshDriverMetadata> QgsProviderMetadata::meshDriversMetadata()
106{
107 return QList<QgsMeshDriverMetadata>();
108}
109
110int QgsProviderMetadata::priorityForUri( const QString & ) const
111{
112 return 0;
113}
114
115QList<Qgis::LayerType> QgsProviderMetadata::validLayerTypesForUri( const QString & ) const
116{
117 return QList<Qgis::LayerType>();
118}
119
120bool QgsProviderMetadata::uriIsBlocklisted( const QString & ) const
121{
122 return false;
123}
124
125QStringList QgsProviderMetadata::sidecarFilesForUri( const QString & ) const
126{
127 return QStringList();
128}
129
130QList<QgsProviderSublayerDetails> QgsProviderMetadata::querySublayers( const QString &, Qgis::SublayerQueryFlags, QgsFeedback * ) const
131{
132 return QList<QgsProviderSublayerDetails>();
133}
134
138{
139 if ( mCreateFunction )
140 {
141 return mCreateFunction( uri, options, flags );
142 }
143 return nullptr;
144}
145
146void QgsProviderMetadata::setBoolParameter( QVariantMap &uri, const QString &parameter, const QVariant &value )
147{
148 if ( value.toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
149 value.toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
150 value.toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
151 {
152 uri[ parameter ] = true;
153 }
154 else if ( value.toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
155 value.toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
156 value.toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
157 {
158 uri[ parameter ] = false;
159 }
160}
161
162bool QgsProviderMetadata::boolParameter( const QVariantMap &uri, const QString &parameter, bool defaultValue )
163{
164 if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
165 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
166 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
167 {
168 return true;
169 }
170 else if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
171 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
172 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
173 {
174 return false;
175 }
176
177 return defaultValue;
178}
179
180QVariantMap QgsProviderMetadata::decodeUri( const QString & ) const
181{
182 return QVariantMap();
183}
184
185QString QgsProviderMetadata::encodeUri( const QVariantMap & ) const
186{
187 return QString();
188}
189
190QString QgsProviderMetadata::absoluteToRelativeUri( const QString &uri, const QgsReadWriteContext &context ) const
191{
192 return context.pathResolver().writePath( uri );
193}
194
195QString QgsProviderMetadata::relativeToAbsoluteUri( const QString &uri, const QgsReadWriteContext &context ) const
196{
197 return context.pathResolver().readPath( uri );
198}
199
202 bool, QMap<int, int> &,
203 QString &errorMessage, const QMap<QString, QVariant> * )
204{
205 errorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createEmptyLayer" ) );
207}
208
209bool QgsProviderMetadata::createDatabase( const QString &, QString &errorMessage )
210{
211 errorMessage = QObject::tr( "The %1 provider does not support database creation" ).arg( key() );
212 return false;
213}
214
216 const QString &, const QString &,
217 int, Qgis::DataType, int,
218 int, double *,
220 const QStringList & )
221{
222 return nullptr;
223}
224
226 const QString &,
227 const QString &,
229 const QMap<QString, QString> & ) const
230{
231 return false;
232}
233
235 const QString &,
237 const QMap<QString, QString> & ) const
238{
239 return false;
240}
241
242QList<QPair<QString, QString> > QgsProviderMetadata::pyramidResamplingMethods()
243{
244 return QList<QPair<QString, QString> >();
245}
246
247QList<QgsDataItemProvider *> QgsProviderMetadata::dataItemProviders() const
248{
249 return QList<QgsDataItemProvider *>();
250}
251
252int QgsProviderMetadata::listStyles( const QString &, QStringList &, QStringList &,
253 QStringList &, QString &errCause )
254{
255 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "listStyles" ) );
256 return -1;
257}
258
259
260bool QgsProviderMetadata::styleExists( const QString &, const QString &, QString &errorCause )
261{
262 errorCause.clear();
263 return false;
264}
265
266QString QgsProviderMetadata::getStyleById( const QString &, const QString &, QString &errCause )
267{
268 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "getStyleById" ) );
269 return QString();
270}
271
272bool QgsProviderMetadata::deleteStyleById( const QString &, const QString &, QString &errCause )
273{
274 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteStyleById" ) );
275 return false;
276}
277
278bool QgsProviderMetadata::saveStyle( const QString &, const QString &, const QString &, const QString &,
279 const QString &, const QString &, bool, QString &errCause )
280{
281 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveStyle" ) );
282 return false;
283}
284
285QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
286{
287 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStyle" ) );
288 return QString();
289}
290
291QString QgsProviderMetadata::loadStoredStyle( const QString &, QString &, QString &errCause )
292{
293 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStoredStyle" ) );
294 return QString();
295}
296
297bool QgsProviderMetadata::saveLayerMetadata( const QString &, const QgsLayerMetadata &, QString & )
298{
299 throw QgsNotSupportedException( QObject::tr( "Provider %1 does not support writing layer metadata" ).arg( key() ) );
300}
301
302bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
303{
304 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
305 return false;
306}
307
309{
310 return nullptr;
311}
312
313QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
314{
315 Q_UNUSED( cached );
316 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
317}
318
319QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
320{
321 return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
322}
323
325{
326 const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
327 const QStringList constKeys { constConns.keys( ) };
328 for ( const QString &key : constKeys )
329 {
330 if ( key == name )
331 {
332 return constConns.value( key );
333 }
334 }
335 return nullptr;
336}
337
339{
340 Q_UNUSED( name );
341 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createConnection" ) ) );
342}
343
344
345QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
346{
347 Q_UNUSED( configuration );
348 Q_UNUSED( uri );
349 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createConnection" ) ) );
350}
351
352void QgsProviderMetadata::deleteConnection( const QString &name )
353{
354 Q_UNUSED( name );
355 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
356}
357
358void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
359{
360 Q_UNUSED( connection )
361 Q_UNUSED( name )
362 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
363}
364
366void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
367{
368 const bool isNewConnection = !connections().contains( name );
369 conn->store( name );
370 mProviderConnections.clear();
371
372 if ( !isNewConnection )
373 emit connectionChanged( name );
374 else
375 emit connectionCreated( name );
376}
378
379template<typename T>
380QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
381{
382 QMap<QString, T *> result;
383 const auto constConns { connections( cached ) };
384 const QStringList constConnKeys { constConns.keys() };
385 for ( const auto &c : constConnKeys )
386 {
387 T *casted { static_cast<T *>( constConns.value( c ) ) };
388 if ( casted )
389 {
390 result.insert( c, casted );
391 }
392 }
393 return result;
394}
395
397
399 const QString &description,
400 const MeshDriverCapabilities &capabilities,
401 const QString &writeDatasetOnfileSuffix )
402 : mName( name )
403 , mDescription( description )
404 , mCapabilities( capabilities )
405 , mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
406{
407}
408
410 const QString &description,
411 const MeshDriverCapabilities &capabilities,
412 const QString &writeDatasetOnfileSuffix,
413 const QString &writeMeshFrameOnFileSuffix,
414 int maxVerticesPerface )
415 : mName( name )
416 , mDescription( description )
417 , mCapabilities( capabilities )
418 , mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
419 , mWriteMeshFrameOnFileSuffix( ( writeMeshFrameOnFileSuffix ) )
420 , mMaxVerticesPerFace( maxVerticesPerface )
421{
422}
423
428
430{
431 return mName;
432}
433
435{
436 return mDescription;
437}
438
440{
441 return mWriteDatasetOnFileSuffix;
442}
443
445{
446 return mWriteMeshFrameOnFileSuffix;
447}
448
450{
451 return mMaxVerticesPerFace;
452}
FileFilterType
Type of file filters.
Definition qgis.h:1285
VectorExportResult
Vector layer export result codes.
Definition qgis.h:983
@ ErrorProviderUnsupportedFeature
Provider does not support creation of empty layers.
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:450
DataType
Raster data types.
Definition qgis.h:351
QFlags< SublayerQueryFlag > SublayerQueryFlags
Sublayer query flags.
Definition qgis.h:1309
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
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:44
Container of fields for a vector layer.
Definition qgsfields.h:46
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.
QFlags< MeshDriverCapability > MeshDriverCapabilities
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.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
Custom exception class for provider connection related exceptions.
QFlags< ProviderMetadataCapability > ProviderMetadataCapabilities
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...
QgsAbstractProviderConnection * findConnection(const QString &name, bool cached=true)
Searches and returns a (possibly nullptr) connection from the stored provider connections.
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 void deleteConnection(const QString &name)
Removes the connection with the given name from 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.
Q_DECL_DEPRECATED CreateDataProviderFunction createFunction() const
Returns a pointer to the direct provider creation function, if supported by the provider.
QFlags< ProviderCapability > ProviderCapabilities
virtual QgsDataProvider * createProvider(const QString &uri, const QgsDataProvider::ProviderOptions &options, Qgis::DataProviderReadFlags flags=Qgis::DataProviderReadFlags())
Class factory to return a pointer to a newly created QgsDataProvider object.
virtual QIcon icon() const
Returns an icon representing the provider.
virtual int priorityForUri(const QString &uri) const
Returns an integer representing the priority which this provider should have when opening a dataset w...
QMap< QString, QgsAbstractDatabaseProviderConnection * > dbConnections(bool cached=true)
Returns a dictionary of database provider connections, the dictionary key is the connection identifie...
static bool boolParameter(const QVariantMap &uri, const QString &parameter, bool defaultValue=false)
Returns the parameter value in the uri as a bool.
virtual bool deleteStyleById(const QString &uri, const QString &styleId, QString &errCause)
Deletes a layer style defined by styleId.
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 QString suggestGroupNameForUri(const QString &uri) const
Returns a name that can be used as a group name for sublayers retrieved from the specified uri.
std::function< QgsDataProvider *(const QString &, const QgsDataProvider::ProviderOptions &, Qgis::DataProviderReadFlags &) > CreateDataProviderFunction
Typedef for data provider creation function.
virtual QString encodeUri(const QVariantMap &parts) const
Reassembles a provider data source URI from its component paths (e.g.
virtual QList< Qgis::LayerType > 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 QString absoluteToRelativeUri(const QString &uri, const QgsReadWriteContext &context) const
Converts absolute path(s) to relative path(s) in the given provider-specific URI.
QString description() const
This returns descriptive text for the provider.
virtual Qgis::VectorExportResult createEmptyLayer(const QString &uri, const QgsFields &fields, Qgis::WkbType 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 &fileName, const QString &driverName, const QgsCoordinateReferenceSystem &crs, const QMap< QString, QString > &metadata=QMap< QString, QString >()) const
Creates mesh data source from a file name fileName and a driver driverName, that is the mesh frame st...
virtual bool styleExists(const QString &uri, const QString &styleId, QString &errorCause)
Returns true if a layer style with the specified styleId exists in the provider defined by uri.
virtual bool saveLayerMetadata(const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage)
Saves metadata to the layer corresponding to the specified uri.
void connectionChanged(const QString &name)
Emitted when the connection with the specified name is changed, e.g.
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
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 QString filters(Qgis::FileFilterType type)
Builds the list of file filter strings (supported formats)
virtual void saveConnection(const QgsAbstractProviderConnection *connection, const QString &name)
Stores the connection in 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.
virtual QString getStyleById(const QString &uri, const QString &styleId, QString &errCause)
Gets a layer style defined by uri.
virtual QMap< QString, QgsAbstractProviderConnection * > connections(bool cached=true)
Returns a dictionary of stored provider connections, the dictionary key is the connection identifier.
virtual QString loadStoredStyle(const QString &uri, QString &styleName, QString &errCause)
Loads a layer style from the provider storage, reporting its name.
virtual QList< Qgis::LayerType > supportedLayerTypes() const
Returns a list of the map layer types supported by the provider.
QgsProviderMetadata(const QString &key, const QString &description, const QString &library=QString())
Constructor for provider metadata.
virtual bool createDatabase(const QString &uri, QString &errorMessage)
Creates a new empty database at the specified uri.
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 QString relativeToAbsoluteUri(const QString &uri, const QgsReadWriteContext &context) const
Converts relative path(s) to absolute path(s) in the given provider-specific URI.
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< QgsDataItemProvider * > dataItemProviders() const
Returns data item providers.
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.
The class is used as a container of context for various read/write operations on other objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
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.