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