QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 {
85  if ( mCreateFunction )
86  {
87  return mCreateFunction( uri, options );
88  }
89  return nullptr;
90 }
91 
92 void QgsProviderMetadata::setBoolParameter( QVariantMap &uri, const QString &parameter, const QVariant &value )
93 {
94  if ( value.toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
95  value.toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
96  value.toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
97  {
98  uri[ parameter ] = true;
99  }
100  else if ( value.toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
101  value.toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
102  value.toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
103  {
104  uri[ parameter ] = false;
105  }
106 }
107 
108 bool QgsProviderMetadata::boolParameter( const QVariantMap &uri, const QString &parameter, bool defaultValue )
109 {
110  if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "yes" ), Qt::CaseInsensitive ) == 0 ||
111  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0 ||
112  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "true" ), Qt::CaseInsensitive ) == 0 )
113  {
114  return true;
115  }
116  else if ( uri.value( parameter, QString() ).toString().compare( QStringLiteral( "no" ), Qt::CaseInsensitive ) == 0 ||
117  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "0" ), Qt::CaseInsensitive ) == 0 ||
118  uri.value( parameter, QString() ).toString().compare( QStringLiteral( "false" ), Qt::CaseInsensitive ) == 0 )
119  {
120  return false;
121  }
122 
123  return defaultValue;
124 }
125 
126 QVariantMap QgsProviderMetadata::decodeUri( const QString & )
127 {
128  return QVariantMap();
129 }
130 
131 QString QgsProviderMetadata::encodeUri( const QVariantMap & )
132 {
133  return QString();
134 }
135 
137  const QString &, const QgsFields &,
139  bool, QMap<int, int> &,
140  QString &errorMessage, const QMap<QString, QVariant> * )
141 {
142  errorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createEmptyLayer" ) );
143  return QgsVectorLayerExporter::ExportError::ErrProviderUnsupportedFeature;
144 }
145 
147  const QString &, const QString &,
148  int, Qgis::DataType, int,
149  int, double *,
151  const QStringList & )
152 {
153  return nullptr;
154 }
155 
156 QList<QPair<QString, QString> > QgsProviderMetadata::pyramidResamplingMethods()
157 {
158  return QList<QPair<QString, QString> >();
159 }
160 
161 QList<QgsDataItemProvider *> QgsProviderMetadata::dataItemProviders() const
162 {
163  return QList<QgsDataItemProvider *>();
164 }
165 
166 int QgsProviderMetadata::listStyles( const QString &, QStringList &, QStringList &,
167  QStringList &, QString &errCause )
168 {
169  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "listStyles" ) );
170  return -1;
171 }
172 
173 QString QgsProviderMetadata::getStyleById( const QString &, QString, QString &errCause )
174 {
175  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "getStyleById" ) );
176  return QString();
177 }
178 
179 bool QgsProviderMetadata::deleteStyleById( const QString &, QString, QString &errCause )
180 {
181  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteStyleById" ) );
182  return false;
183 }
184 
185 bool QgsProviderMetadata::saveStyle( const QString &, const QString &, const QString &, const QString &,
186  const QString &, const QString &, bool, QString &errCause )
187 {
188  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveStyle" ) );
189  return false;
190 }
191 
192 QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
193 {
194  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStyle" ) );
195  return QString();
196 }
197 
198 bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
199 {
200  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
201  return false;
202 }
203 
205 {
206  return nullptr;
207 }
208 
209 QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
210 {
211  Q_UNUSED( cached );
212  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
213 }
214 
215 QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
216 {
217  return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
218 }
219 
221 {
222  const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
223  const QStringList constKeys { constConns.keys( ) };
224  for ( const QString &key : constKeys )
225  {
226  if ( key == name )
227  {
228  return constConns.value( key );
229  }
230  }
231  return nullptr;
232 }
233 
235 {
236  Q_UNUSED( name );
237  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
238 }
239 
240 
241 QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
242 {
243  Q_UNUSED( configuration );
244  Q_UNUSED( uri );
245  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
246 }
247 
248 void QgsProviderMetadata::deleteConnection( const QString &name )
249 {
250  Q_UNUSED( name );
251  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
252 }
253 
254 void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
255 {
256  Q_UNUSED( connection )
257  Q_UNUSED( name )
258  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
259 }
260 
262 void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
263 {
264  const bool isNewConnection = !connections().contains( name );
265  conn->store( name );
266  mProviderConnections.clear();
267 
268  if ( !isNewConnection )
269  emit connectionChanged( name );
270  else
271  emit connectionCreated( name );
272 }
274 
275 template<typename T>
276 QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
277 {
278  QMap<QString, T *> result;
279  const auto constConns { connections( cached ) };
280  const QStringList constConnKeys { constConns.keys() };
281  for ( const auto &c : constConnKeys )
282  {
283  T *casted { static_cast<T *>( constConns.value( c ) ) };
284  if ( casted )
285  {
286  result.insert( c, casted );
287  }
288  }
289  return result;
290 }
291 
293 
294 QgsMeshDriverMetadata::QgsMeshDriverMetadata( const QString &name, const QString &description, const MeshDriverCapabilities &capabilities )
295  : mName( name ), mDescription( description ), mCapabilities( capabilities )
296 {
297 }
298 
299 QgsMeshDriverMetadata::MeshDriverCapabilities QgsMeshDriverMetadata::capabilities() const
300 {
301  return mCapabilities;
302 }
303 
305 {
306  return mName;
307 }
308 
310 {
311  return mDescription;
312 }
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:215
QgsProviderMetadata::saveConnection
virtual void saveConnection(const QgsAbstractProviderConnection *connection, const QString &name) SIP_THROW(QgsProviderConnectionException)
Stores the connection in the settings.
Definition: qgsprovidermetadata.cpp:254
QgsMeshDriverMetadata::name
QString name() const
Returns the name (key) for this driver.
Definition: qgsprovidermetadata.cpp:304
Qgis::DataType
DataType
Raster data types.
Definition: qgis.h:114
QgsProviderMetadata::dataItemProviders
virtual QList< QgsDataItemProvider * > dataItemProviders() const
Returns data item providers.
Definition: qgsprovidermetadata.cpp:161
QgsDataProvider::ProviderOptions
Setting options for creating vector data providers.
Definition: qgsdataprovider.h:104
QgsProviderMetadata::loadStyle
virtual QString loadStyle(const QString &uri, QString &errCause)
Loads a layer style defined by uri.
Definition: qgsprovidermetadata.cpp:192
QgsDataProvider
Definition: qgsdataprovider.h:41
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:241
QgsProviderMetadata::CreateDataProviderFunction
std::function< QgsDataProvider *(const QString &, const QgsDataProvider::ProviderOptions &) > CreateDataProviderFunction
Typedef for data provider creation function.
Definition: qgsprovidermetadata.h:136
QgsProviderMetadata::~QgsProviderMetadata
virtual ~QgsProviderMetadata()
dtor
Definition: qgsprovidermetadata.cpp:38
QgsProviderMetadata::FilterType
FilterType
Type of file filters.
Definition: qgsprovidermetadata.h:205
QgsFields
Definition: qgsfields.h:44
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
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:136
QgsTransaction
Definition: qgstransaction.h:56
QgsProviderMetadata::getStyleById
virtual QString getStyleById(const QString &uri, QString styleId, QString &errCause)
Gets a layer style defined by uri.
Definition: qgsprovidermetadata.cpp:173
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:92
QgsProviderMetadata::deleteConnection
virtual void deleteConnection(const QString &name) SIP_THROW(QgsProviderConnectionException)
Removes the connection with the given name from the settings.
Definition: qgsprovidermetadata.cpp:248
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:146
QgsMeshDriverMetadata::description
QString description() const
Returns the description for this driver.
Definition: qgsprovidermetadata.cpp:309
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:220
QgsProviderMetadata::encodeUri
virtual QString encodeUri(const QVariantMap &parts)
Reassembles a provider data source URI from its component paths (e.g.
Definition: qgsprovidermetadata.cpp:131
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:299
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::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:166
qgsmaplayer.h
QgsProviderConnectionException
Definition: qgsexception.h:100
QgsProviderMetadata::description
QString description() const
This returns descriptive text for the provider.
Definition: qgsprovidermetadata.cpp:48
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsProviderMetadata::createTransaction
virtual QgsTransaction * createTransaction(const QString &connString)
Returns new instance of transaction.
Definition: qgsprovidermetadata.cpp:204
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:108
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:185
QgsProviderMetadata::deleteStyleById
virtual bool deleteStyleById(const QString &uri, QString styleId, QString &errCause)
Deletes a layer style defined by styleId.
Definition: qgsprovidermetadata.cpp:179
QgsVectorLayerExporter::ExportError
ExportError
Error codes.
Definition: qgsvectorlayerexporter.h:51
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:44
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:156
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:209
QgsRasterDataProvider
Definition: qgsrasterdataprovider.h:88
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:126
QgsProviderMetadata::createDb
virtual bool createDb(const QString &dbPath, QString &errCause)
Creates database by the provider on the path.
Definition: qgsprovidermetadata.cpp:198
QgsProviderMetadata::cleanupProvider
virtual void cleanupProvider()
Cleanup the provider.
Definition: qgsprovidermetadata.cpp:68
QgsProviderMetadata::createProvider
virtual QgsDataProvider * createProvider(const QString &uri, const QgsDataProvider::ProviderOptions &options)
Class factory to return a pointer to a newly created QgsDataProvider object.
Definition: qgsprovidermetadata.cpp:83