QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
79 {
80  if ( mCreateFunction )
81  {
82  return mCreateFunction( uri, options );
83  }
84  return nullptr;
85 }
86 
87 QVariantMap QgsProviderMetadata::decodeUri( const QString & )
88 {
89  return QVariantMap();
90 }
91 
93  const QString &, const QgsFields &,
95  bool, QMap<int, int> &,
96  QString &errorMessage, const QMap<QString, QVariant> * )
97 {
98  errorMessage = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createEmptyLayer" ) );
99  return QgsVectorLayerExporter::ExportError::ErrProviderUnsupportedFeature;
100 }
101 
103  const QString &, const QString &,
104  int, Qgis::DataType, int,
105  int, double *,
107  const QStringList & )
108 {
109  return nullptr;
110 }
111 
112 QList<QPair<QString, QString> > QgsProviderMetadata::pyramidResamplingMethods()
113 {
114  return QList<QPair<QString, QString> >();
115 }
116 
117 QList<QgsDataItemProvider *> QgsProviderMetadata::dataItemProviders() const
118 {
119  return QList<QgsDataItemProvider *>();
120 }
121 
122 int QgsProviderMetadata::listStyles( const QString &, QStringList &, QStringList &,
123  QStringList &, QString &errCause )
124 {
125  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "listStyles" ) );
126  return -1;
127 }
128 
129 QString QgsProviderMetadata::getStyleById( const QString &, QString, QString &errCause )
130 {
131  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "getStyleById" ) );
132  return QString();
133 }
134 
135 bool QgsProviderMetadata::deleteStyleById( const QString &, QString, QString &errCause )
136 {
137  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteStyleById" ) );
138  return false;
139 }
140 
141 bool QgsProviderMetadata::saveStyle( const QString &, const QString &, const QString &, const QString &,
142  const QString &, const QString &, bool, QString &errCause )
143 {
144  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveStyle" ) );
145  return false;
146 }
147 
148 QString QgsProviderMetadata::loadStyle( const QString &, QString &errCause )
149 {
150  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "loadStyle" ) );
151  return QString();
152 }
153 
154 bool QgsProviderMetadata::createDb( const QString &, QString &errCause )
155 {
156  errCause = QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "createDb" ) );
157  return false;
158 }
159 
161 {
162  return nullptr;
163 }
164 
165 QMap<QString, QgsAbstractProviderConnection *> QgsProviderMetadata::connections( bool cached )
166 {
167  Q_UNUSED( cached );
168  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connections" ) ) );
169 }
170 
171 QMap<QString, QgsAbstractDatabaseProviderConnection *> QgsProviderMetadata::dbConnections( bool cached )
172 {
173  return connections<QgsAbstractDatabaseProviderConnection>( cached ) ;
174 }
175 
177 {
178  const QMap<QString, QgsAbstractProviderConnection *> constConns { connections( cached ) };
179  const QStringList constKeys { constConns.keys( ) };
180  for ( const QString &key : constKeys )
181  {
182  if ( key == name )
183  {
184  return constConns.value( key );
185  }
186  }
187  return nullptr;
188 }
189 
191 {
192  Q_UNUSED( name );
193  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
194 }
195 
196 
197 QgsAbstractProviderConnection *QgsProviderMetadata::createConnection( const QString &uri, const QVariantMap &configuration )
198 {
199  Q_UNUSED( configuration );
200  Q_UNUSED( uri );
201  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "connection" ) ) );
202 }
203 
204 void QgsProviderMetadata::deleteConnection( const QString &name )
205 {
206  Q_UNUSED( name );
207  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "deleteConnection" ) ) );
208 }
209 
210 void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
211 {
212  Q_UNUSED( connection );
213  Q_UNUSED( name );
214  throw QgsProviderConnectionException( QObject::tr( "Provider %1 has no %2 method" ).arg( key(), QStringLiteral( "saveConnection" ) ) );
215 }
216 
218 void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
219 {
220  conn->store( name );
221  mProviderConnections.clear();
222 }
224 
225 template<typename T>
226 QMap<QString, T *> QgsProviderMetadata::connections( bool cached )
227 {
228  QMap<QString, T *> result;
229  const auto constConns { connections( cached ) };
230  const QStringList constConnKeys { constConns.keys() };
231  for ( const auto &c : constConnKeys )
232  {
233  T *casted { static_cast<T *>( constConns.value( c ) ) };
234  if ( casted )
235  {
236  result.insert( c, casted );
237  }
238  }
239  return result;
240 }
241 
242 
243 
244 
virtual QgsTransaction * createTransaction(const QString &connString)
Returns new instance of transaction.
QString description() const
This returns descriptive text for the provider.
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.
virtual QgsDataProvider * createProvider(const QString &uri, const QgsDataProvider::ProviderOptions &options)
Class factory to return a pointer to a newly created QgsDataProvider object.
DataType
Raster data types.
Definition: qgis.h:80
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:42
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
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 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:68
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 QVariantMap decodeUri(const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
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.
std::function< QgsDataProvider *(const QString &, const QgsDataProvider::ProviderOptions &) > CreateDataProviderFunction
Typedef for data provider creation function.
virtual void saveConnection(const QgsAbstractProviderConnection *connection, const QString &name)
Stores the connection in the settings.
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.
QgsProviderMetadata(const QString &key, const QString &description, const QString &library=QString())
Constructor for provider metadata.
Setting options for creating vector data providers.
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.
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.
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.
FilterType
Type of file filters.
virtual bool deleteStyleById(const QString &uri, QString styleId, QString &errCause)
Deletes a layer style defined by styleId.
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:100
Base class for raster data providers.