QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 "moc_qgsprovidermetadata.cpp"
21#include "qgsdataprovider.h"
22#include "qgsmaplayer.h"
23#include "qgsexception.h"
26
28 QString const &description,
29 QString const &library )
30 : mKey( key )
31 , mDescription( description )
32 , mLibrary( library )
33{}
34
35QgsProviderMetadata::QgsProviderMetadata( const QString &key, const QString &description, const CreateDataProviderFunction &createFunc )
36 : mKey( key )
37 , mDescription( description )
38 , mCreateFunction( createFunc )
39{}
40
42{
43 qDeleteAll( mProviderConnections );
44}
45
47{
48 return mKey;
49}
50
52{
53 return mDescription;
54}
55
57{
58 return QIcon();
59}
60
65
70
71QList<Qgis::LayerType> QgsProviderMetadata::supportedLayerTypes() const
72{
73 return {};
74}
75
77{
78 return mLibrary;
79}
80
81QString QgsProviderMetadata::suggestGroupNameForUri( const QString & /*uri*/ ) const
82{
83 return QString();
84}
85
90
95
100
102{
103 return QString();
104}
105
106QList<QgsMeshDriverMetadata> QgsProviderMetadata::meshDriversMetadata()
107{
108 return QList<QgsMeshDriverMetadata>();
109}
110
111int QgsProviderMetadata::priorityForUri( const QString & ) const
112{
113 return 0;
114}
115
116QList<Qgis::LayerType> QgsProviderMetadata::validLayerTypesForUri( const QString & ) const
117{
118 return QList<Qgis::LayerType>();
119}
120
121bool QgsProviderMetadata::uriIsBlocklisted( const QString & ) const
122{
123 return false;
124}
125
126QStringList QgsProviderMetadata::sidecarFilesForUri( const QString & ) const
127{
128 return QStringList();
129}
130
131QList<QgsProviderSublayerDetails> QgsProviderMetadata::querySublayers( const QString &, Qgis::SublayerQueryFlags, QgsFeedback * ) const
132{
133 return QList<QgsProviderSublayerDetails>();
134}
135
139{
140 if ( mCreateFunction )
141 {
142 return mCreateFunction( uri, options, flags );
143 }
144 return nullptr;
145}
146
147void QgsProviderMetadata::setBoolParameter( QVariantMap &uri, const QString &parameter, const QVariant &value )
148{
149 if ( value.toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
150 value.toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
151 value.toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
152 {
153 uri[ parameter ] = true;
154 }
155 else if ( value.toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
156 value.toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
157 value.toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
158 {
159 uri[ parameter ] = false;
160 }
161}
162
163bool QgsProviderMetadata::boolParameter( const QVariantMap &uri, const QString &parameter, bool defaultValue )
164{
165 if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
166 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
167 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
168 {
169 return true;
170 }
171 else if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
172 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
173 uri.value( parameter, QString() ).toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
174 {
175 return false;
176 }
177
178 return defaultValue;
179}
180
181QVariantMap QgsProviderMetadata::decodeUri( const QString & ) const
182{
183 return QVariantMap();
184}
185
186QString QgsProviderMetadata::encodeUri( const QVariantMap & ) const
187{
188 return QString();
189}
190
191QString QgsProviderMetadata::absoluteToRelativeUri( const QString &uri, const QgsReadWriteContext &context ) const
192{
193 return context.pathResolver().writePath( uri );
194}
195
196QString QgsProviderMetadata::relativeToAbsoluteUri( const QString &uri, const QgsReadWriteContext &context ) const
197{
198 return context.pathResolver().readPath( uri );
199}
200
203 bool, QMap<int, int> &,
204 QString &errorMessage, const QMap<QString, QVariant> * )
205{
206 errorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createEmptyLayer" ) );
208}
209
210bool QgsProviderMetadata::createDatabase( const QString &, QString &errorMessage )
211{
212 errorMessage = QObject::tr( "The %1 provider does not support database creation" ).arg( key() );
213 return false;
214}
215
217 const QString &, const QString &,
218 int, Qgis::DataType, int,
219 int, double *,
221 const QStringList & )
222{
223 return nullptr;
224}
225
227 const QString &,
228 const QString &,
230 const QMap<QString, QString> & ) const
231{
232 return false;
233}
234
236 const QString &,
238 const QMap<QString, QString> & ) const
239{
240 return false;
241}
242
243QList<QPair<QString, QString> > QgsProviderMetadata::pyramidResamplingMethods()
244{
245 return QList<QPair<QString, QString> >();
246}
247
248QList<QgsDataItemProvider *> QgsProviderMetadata::dataItemProviders() const
249{
250 return QList<QgsDataItemProvider *>();
251}
252
253int QgsProviderMetadata::listStyles( const QString &, QStringList &, QStringList &,
254 QStringList &, QString &errCause )
255{
256 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "listStyles" ) );
257 return -1;
258}
259
260
261bool QgsProviderMetadata::styleExists( const QString &, const QString &, QString &errorCause )
262{
263 errorCause.clear();
264 return false;
265}
266
267QString QgsProviderMetadata::getStyleById( const QString &, const QString &, QString &errCause )
268{
269 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "getStyleById" ) );
270 return QString();
271}
272
273bool QgsProviderMetadata::deleteStyleById( const QString &, const QString &, QString &errCause )
274{
275 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteStyleById" ) );
276 return false;
277}
278
279bool QgsProviderMetadata::saveStyle( const QString &, const QString &, const QString &, const QString &,
280 const QString &, const QString &, bool, QString &errCause )
281{
282 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveStyle" ) );
283 return false;
284}
285
286QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
287{
288 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStyle" ) );
289 return QString();
290}
291
292QString QgsProviderMetadata::loadStoredStyle( const QString &, QString &, QString &errCause )
293{
294 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStoredStyle" ) );
295 return QString();
296}
297
298bool QgsProviderMetadata::saveLayerMetadata( const QString &, const QgsLayerMetadata &, QString & )
299{
300 throw QgsNotSupportedException( QObject::tr( "Provider %1 does not support writing layer metadata" ).arg( key() ) );
301}
302
303bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
304{
305 errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
306 return false;
307}
308
310{
311 return nullptr;
312}
313
314QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
315{
316 Q_UNUSED( cached );
317 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
318}
319
320QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
321{
322 return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
323}
324
326{
327 const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
328 const QStringList constKeys { constConns.keys( ) };
329 for ( const QString &key : constKeys )
330 {
331 if ( key == name )
332 {
333 return constConns.value( key );
334 }
335 }
336 return nullptr;
337}
338
340{
341 Q_UNUSED( name );
342 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createConnection" ) ) );
343}
344
345
346QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
347{
348 Q_UNUSED( configuration );
349 Q_UNUSED( uri );
350 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createConnection" ) ) );
351}
352
353void QgsProviderMetadata::deleteConnection( const QString &name )
354{
355 Q_UNUSED( name );
356 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
357}
358
359void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
360{
361 Q_UNUSED( connection )
362 Q_UNUSED( name )
363 throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
364}
365
367void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
368{
369 const bool isNewConnection = !connections().contains( name );
370 conn->store( name );
371 mProviderConnections.clear();
372
373 if ( !isNewConnection )
374 emit connectionChanged( name );
375 else
376 emit connectionCreated( name );
377}
379
380template<typename T>
381QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
382{
383 QMap<QString, T *> result;
384 const auto constConns { connections( cached ) };
385 const QStringList constConnKeys { constConns.keys() };
386 for ( const auto &c : constConnKeys )
387 {
388 T *casted { static_cast<T *>( constConns.value( c ) ) };
389 if ( casted )
390 {
391 result.insert( c, casted );
392 }
393 }
394 return result;
395}
396
398
400 const QString &description,
401 const MeshDriverCapabilities &capabilities,
402 const QString &writeDatasetOnfileSuffix )
403 : mName( name )
404 , mDescription( description )
405 , mCapabilities( capabilities )
406 , mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
407{
408}
409
411 const QString &description,
412 const MeshDriverCapabilities &capabilities,
413 const QString &writeDatasetOnfileSuffix,
414 const QString &writeMeshFrameOnFileSuffix,
415 int maxVerticesPerface )
416 : mName( name )
417 , mDescription( description )
418 , mCapabilities( capabilities )
419 , mWriteDatasetOnFileSuffix( writeDatasetOnfileSuffix )
420 , mWriteMeshFrameOnFileSuffix( ( writeMeshFrameOnFileSuffix ) )
421 , mMaxVerticesPerFace( maxVerticesPerface )
422{
423}
424
429
431{
432 return mName;
433}
434
436{
437 return mDescription;
438}
439
441{
442 return mWriteDatasetOnFileSuffix;
443}
444
446{
447 return mWriteMeshFrameOnFileSuffix;
448}
449
451{
452 return mMaxVerticesPerFace;
453}
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.