QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsproviderregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsproviderregistry.h - Singleton class for
3  registering data providers.
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 #ifndef QGSPROVIDERREGISTRY_H
20 #define QGSPROVIDERREGISTRY_H
21 
22 #include <map>
23 
24 #include <QDir>
25 #include <QLibrary>
26 #include <QString>
27 
28 #include "qgsvectorlayerexporter.h"
29 #include "qgsdataprovider.h"
30 #include "qgis_core.h"
31 #include "qgis_sip.h"
32 
33 #include <vector>
34 #include <memory>
35 
37 class QgsVectorLayer;
40 class QgsDataItem;
42 
57 class CORE_EXPORT QgsProviderRegistry
58 {
59 
60  public:
61 
62  // TODO QGIS 4 - either move to QgsAbstractDataSourceWidget or remove altogether
63 
68  {
69 
76 
82 
89  };
90 
92  static QgsProviderRegistry *instance( const QString &pluginPath = QString() );
93 
95 
104  Q_DECL_DEPRECATED QString library( const QString &providerKey ) const SIP_DEPRECATED;
105 
107  QString pluginList( bool asHtml = false ) const;
108 
112  QDir libraryDirectory() const;
113 
115  void setLibraryDirectory( const QDir &path );
116 
117  /*
118  * IMPORTANT: While it seems like /Factory/ would be the correct annotation here, that's not
119  * the case.
120  * Paraphrasing Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
121  *
122  * "
123  * /Factory/ is used when the instance returned is guaranteed to be new to Python.
124  * In this case it isn't because it has already been seen when being returned by by the python function
125  * creating the provider subclass.
126  *
127  * (However for a different sub-class implemented in C++ then it would be the first time it was seen
128  * by Python so the /Factory/ on create() would be correct.)
129  *
130  * You might try using /TransferBack/ on createProvider() instead - that might be the best compromise.
131  * "
132  */
133 
144  QgsDataProvider *createProvider( const QString &providerKey,
145  const QString &dataSource,
147  QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() ) SIP_TRANSFERBACK;
148 
155  Q_DECL_DEPRECATED int providerCapabilities( const QString &providerKey ) const SIP_DEPRECATED;
156 
162  SIP_SKIP QgsVectorLayerExporter::ExportError createEmptyLayer( const QString &providerKey, 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 );
163 
170  QgsRasterDataProvider *createRasterDataProvider(
171  const QString &providerKey,
172  const QString &uri,
173  const QString &format,
174  int nBands,
175  Qgis::DataType type,
176  int width, int height,
177  double *geoTransform,
179  const QStringList &createOptions = QStringList() ) SIP_FACTORY;
180 
186  QList<QPair<QString, QString> > pyramidResamplingMethods( const QString &providerKey );
187 
196  QVariantMap decodeUri( const QString &providerKey, const QString &uri );
197 
207  QString encodeUri( const QString &providerKey, const QVariantMap &parts );
208 
215  Q_DECL_DEPRECATED QWidget *createSelectionWidget( const QString &providerKey, QWidget *parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags(), QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None ) SIP_DEPRECATED;
216 
222  QList< QgsDataItemProvider * > dataItemProviders( const QString &providerKey ) const SIP_FACTORY;
223 
229  int listStyles( const QString &providerKey,
230  const QString &uri,
231  QStringList &ids,
232  QStringList &names,
233  QStringList &descriptions,
234  QString &errCause );
235 
241  QString getStyleById( const QString &providerKey, const QString &uri, QString styleId, QString &errCause );
242 
247  bool deleteStyleById( const QString &providerKey, const QString &uri, QString styleId, QString &errCause );
248 
254  bool saveStyle( const QString &providerKey, const QString &uri, const QString &qmlStyle, const QString &sldStyle,
255  const QString &styleName, const QString &styleDescription,
256  const QString &uiFileContent, bool useAsDefault, QString &errCause );
257 
262  QString loadStyle( const QString &providerKey, const QString &uri, QString &errCause );
263 
268  bool createDb( const QString &providerKey, const QString &dbPath, QString &errCause );
269 
274  QgsTransaction *createTransaction( const QString &providerKey, const QString &connString ) SIP_FACTORY;
275 
286  Q_DECL_DEPRECATED QFunctionPointer function( const QString &providerKey, const QString &functionName ) SIP_DEPRECATED;
287 
297  Q_DECL_DEPRECATED QLibrary *createProviderLibrary( const QString &providerKey ) const SIP_FACTORY SIP_DEPRECATED;
298 
300  QStringList providerList() const;
301 
303  QgsProviderMetadata *providerMetadata( const QString &providerKey ) const;
304 
312  class CORE_EXPORT ProviderCandidateDetails
313  {
314 
315  public:
316 
320  ProviderCandidateDetails( QgsProviderMetadata *metadata, const QList< QgsMapLayerType > &layerTypes )
321  : mMetadata( metadata )
322  , mLayerTypes( layerTypes )
323  {}
324 
328  QgsProviderMetadata *metadata() const { return mMetadata; }
329 
334  QList<QgsMapLayerType> layerTypes() const { return mLayerTypes; }
335 
336 #ifdef SIP_RUN
337  SIP_PYOBJECT __repr__();
338  % MethodCode
339  QString str = QStringLiteral( "<QgsProviderRegistry.ProviderCandidateDetails: %1>" ).arg( sipCpp->metadata()->key() );
340  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
341  % End
342 #endif
343 
344  private:
345  QgsProviderMetadata *mMetadata = nullptr;
346 
347  QList< QgsMapLayerType > mLayerTypes;
348 
349  };
350 
367  QList< QgsProviderRegistry::ProviderCandidateDetails > preferredProvidersForUri( const QString &uri ) const;
368 
380  class CORE_EXPORT UnusableUriDetails
381  {
382  public:
383 
390  UnusableUriDetails( const QString &uri = QString(), const QString &warning = QString(), const QList< QgsMapLayerType > &layerTypes = QList< QgsMapLayerType >() )
391  : uri( uri )
392  , warning( warning )
393  , layerTypes( layerTypes )
394  {}
395 
399  QString uri;
400 
404  QString warning;
405 
410 
415  QList<QgsMapLayerType> layerTypes;
416 
417 #ifdef SIP_RUN
418  SIP_PYOBJECT __repr__();
419  % MethodCode
420  QString str = QStringLiteral( "<QgsProviderRegistry.UnusableUriDetails: %1>" ).arg( sipCpp->warning );
421  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
422  % End
423 #endif
424 
425  };
426 
439  class CORE_EXPORT UnusableUriHandlerInterface
440  {
441 
442  public:
443 
444  virtual ~UnusableUriHandlerInterface() = default;
445 
449  virtual bool matchesUri( const QString &uri ) const = 0;
450 
454  virtual UnusableUriDetails details( const QString &uri ) const = 0;
455 
456  };
457 
468  bool registerUnusableUriHandler( UnusableUriHandlerInterface *handler SIP_TRANSFER );
469 
493  bool handleUnusableUri( const QString &uri, UnusableUriDetails &details SIP_OUT ) const;
494 
511  bool shouldDeferUriForOtherProviders( const QString &uri, const QString &providerKey ) const;
512 
526  bool uriIsBlocklisted( const QString &uri ) const;
527 
538  QString fileVectorFilters() const;
539 
552  QString fileRasterFilters() const;
553 
567  QString fileMeshFilters() const;
568 
579  QString fileMeshDatasetFilters() const;
580 
593  QString filePointCloudFilters() const;
594 
596  QString databaseDrivers() const;
598  QString directoryDrivers() const;
600  QString protocolDrivers() const;
601 
605  Q_DECL_DEPRECATED void registerGuis( QWidget *widget ) SIP_DEPRECATED;
606 
613  bool registerProvider( QgsProviderMetadata *providerMetadata SIP_TRANSFER );
614 
616  SIP_SKIP typedef std::map<QString, QgsProviderMetadata *> Providers;
617 
618  private:
620  QgsProviderRegistry( const QString &pluginPath );
621 
622 #ifdef SIP_RUN
623  QgsProviderRegistry( const QString &pluginPath );
624 #endif
625 
626  void init();
627  void clean();
628 
630  Providers mProviders;
631 
633  QDir mLibraryDirectory;
634 
644  QString mVectorFileFilters;
645 
649  QString mRasterFileFilters;
650 
654  QString mMeshFileFilters;
655 
659  QString mMeshDatasetFileFilters;
660 
664  QString mPointCloudFileFilters;
665 
672  QString mDatabaseDrivers;
673 
679  QString mDirectoryDrivers;
680 
687  QString mProtocolDrivers;
688 
689  QList< UnusableUriHandlerInterface * > mUnusableUriHandlers;
690 
694  static bool exists();
695 
696  friend class QgsApplication;
697 
698 }; // class QgsProviderRegistry
699 
700 #endif //QGSPROVIDERREGISTRY_H
701 
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:46
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
This class represents a coordinate reference system (CRS).
This is the interface for those who want to add custom data items to the browser tree.
Base class for all items in the model.
Definition: qgsdataitem.h:52
Abstract base class for spatial data provider implementations.
Container of fields for a vector layer.
Definition: qgsfields.h:45
Holds data provider key, description, and associated shared library file or function pointer informat...
Contains information pertaining to a candidate provider.
QList< QgsMapLayerType > layerTypes() const
Returns a list of map layer types which are valid options for opening the target using this candidate...
ProviderCandidateDetails(QgsProviderMetadata *metadata, const QList< QgsMapLayerType > &layerTypes)
Constructor for ProviderCandidateDetails, with the specified provider metadata and valid candidate la...
QgsProviderMetadata * metadata() const
Returns the candidate provider metadata.
Contains information about unusable URIs which aren't handled by any registered providers.
QString warning
Contains a short, user-friendly, translated message advising why the URI is not usable.
UnusableUriDetails(const QString &uri=QString(), const QString &warning=QString(), const QList< QgsMapLayerType > &layerTypes=QList< QgsMapLayerType >())
Constructor for UnusableUriDetails for the given uri, with the specified user-friendly,...
QString uri
URI which could not be handled.
QString detailedWarning
Contains a longer, user-friendly, translated message advising why the URI is not usable.
QList< QgsMapLayerType > layerTypes
Contains a list of map layer types which are usually valid options for opening the target URI.
An interface used to handle unusable URIs which aren't handled by any registered providers,...
virtual UnusableUriDetails details(const QString &uri) const =0
Returns the details for advising the user why the uri is not usable.
virtual bool matchesUri(const QString &uri) const =0
Returns true if the handle is an unusable URI handler for the specified uri.
A registry / canonical manager of data providers.
std::map< QString, QgsProviderMetadata * > Providers
Type for data provider metadata associative container.
WidgetMode
Different ways a source select dialog can be used.
@ Embedded
Used for the data source manager dialog where the widget is embedded as the main content for a partic...
@ None
Basic mode when the widget is used as a standalone dialog.
@ Manager
Used by data items for QgsDataItem::paramWidget().
Base class for raster data providers.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
A convenience class for exporting vector layers to a destination data provider.
Represents a vector layer which manages a vector based data sets.
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
#define SIP_FACTORY
Definition: qgis_sip.h:76
const QgsCoordinateReferenceSystem & crs
Setting options for creating vector data providers.