QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
36 #include "providers/gdal/qgsgdalprovider.h"
37 #include "providers/ogr/qgsogrprovider.h"
38 #include "providers/meshmemory/qgsmeshmemorydataprovider.h"
41 #ifdef HAVE_STATIC_PROVIDERS
42 #include "qgswmsprovider.h"
43 #include "qgspostgresprovider.h"
53 QMutexLocker locker( &sMutex );
73 const QString &providerKey )
76 QgsProviderRegistry::Providers::const_iterator i =
77 metaData.find( providerKey );
79 if ( i != metaData.end() )
85 for (
auto it = metaData.begin(); it != metaData.end(); ++it )
87 if ( providerKey.compare( it->first, Qt::CaseInsensitive ) == 0 )
94 QgsProviderRegistry::QgsProviderRegistry(
const QString &pluginPath )
102 char **argv = qApp->argv();
103 QString appDir = argv[0];
104 int bin = appDir.findRev(
"/bin", -1,
false );
105 QString baseDir = appDir.left( bin );
106 QString mLibraryDirectory = baseDir +
"/lib";
110 mLibraryDirectory.setPath( pluginPath );
114 void QgsProviderRegistry::init()
120 mProviders[ QgsMemoryProvider::providerKey() ] =
new QgsProviderMetadata( QgsMemoryProvider::providerKey(), QgsMemoryProvider::providerDescription(), &QgsMemoryProvider::createProvider );
124 mProviders[ QgsMeshMemoryDataProvider::providerKey() ] =
new QgsProviderMetadata( QgsMeshMemoryDataProvider::providerKey(), QgsMeshMemoryDataProvider::providerDescription(), &QgsMeshMemoryDataProvider::createProvider );
129 mProviders[ QgsGdalProvider::providerKey() ] =
new QgsGdalProviderMetadata();
133 mProviders[ QgsOgrProvider::providerKey() ] =
new QgsOgrProviderMetadata();
138 mProviders[ vt->
key() ] = vt;
140 #ifdef HAVE_STATIC_PROVIDERS
141 mProviders[ QgsWmsProvider::providerKey() ] =
new QgsWmsProviderMetadata();
142 mProviders[ QgsPostgresProvider::providerKey() ] =
new QgsPostgresProviderMetadata();
146 #ifdef HAVE_STATIC_PROVIDERS
147 QgsDebugMsg( QStringLiteral(
"Forced only static providers" ) );
151 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
152 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
154 #if defined(Q_OS_WIN) || defined(__CYGWIN__)
155 mLibraryDirectory.setNameFilters( QStringList(
"*.dll" ) );
156 #elif defined(ANDROID)
157 mLibraryDirectory.setNameFilters( QStringList(
"*provider*.so" ) );
159 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral(
"*.so" ) ) );
162 QgsDebugMsgLevel( QStringLiteral(
"Checking %1 for provider plugins" ).arg( mLibraryDirectory.path() ), 2 );
164 if ( mLibraryDirectory.count() == 0 )
166 QgsDebugMsg( QStringLiteral(
"No dynamic QGIS data provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() ) );
170 QString filePattern = getenv(
"QGIS_PROVIDER_FILE" );
172 if ( !filePattern.isEmpty() )
174 fileRegexp.setPattern( filePattern );
177 typedef std::vector<QgsProviderMetadata *> *multiple_factory_function();
179 const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
180 for (
const QFileInfo &fi : constEntryInfoList )
182 if ( !fileRegexp.isEmpty() )
184 if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
186 QgsDebugMsg(
"provider " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
192 QLibrary myLib( fi.filePath() );
195 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (lib not loadable): %2" ).arg( myLib.fileName(), myLib.errorString() ) );
199 QFunctionPointer multi_func = myLib.resolve( QStringLiteral(
"multipleProviderMetadataFactory" ).toLatin1().data() );
200 multiple_factory_function *multi_function =
reinterpret_cast< multiple_factory_function *
>(
cast_to_fptr( multi_func ) );
201 if ( multi_function )
203 std::vector<QgsProviderMetadata *> *metadatas = multi_function();
204 for (
const auto meta : *metadatas )
206 if ( findMetadata_( mProviders, meta->key() ) )
208 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (key %2 already registered)" ).arg( myLib.fileName() ).arg( meta->key() ) );
213 mProviders[meta->key()] = meta;
219 QFunctionPointer func = myLib.resolve( QStringLiteral(
"providerMetadataFactory" ).toLatin1().data() );
220 factory_function *
function =
reinterpret_cast< factory_function *
>(
cast_to_fptr( func ) );
223 QgsDebugMsgLevel( QStringLiteral(
"Checking %1: ...invalid (no providerMetadataFactory method)" ).arg( myLib.fileName() ), 2 );
230 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no metadata returned)" ).arg( myLib.fileName() ) );
234 if ( findMetadata_( mProviders, meta->
key() ) )
236 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (key %2 already registered)" ).arg( myLib.fileName() ).arg( meta->
key() ) );
241 mProviders[meta->
key()] = meta;
245 QgsDebugMsg( QStringLiteral(
"Loaded %1 providers (%2) " ).arg( mProviders.size() ).arg(
providerList().join(
';' ) ) );
248 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
250 const QString &key = it->first;
278 QgsDebugMsgLevel( QStringLiteral(
"Checking %1: ...loaded OK (%2 file mesh filters)" ).arg( key ).arg( mMeshFileFilters.split(
";;" ).count() ), 2 );
286 QgsDebugMsgLevel( QStringLiteral(
"Checking %1: ...loaded OK (%2 file dataset filters)" ).arg( key ).arg( mMeshDatasetFileFilters.split(
";;" ).count() ), 2 );
294 mDatabaseDrivers = QgsOgrProviderUtils::databaseDrivers();
297 mDirectoryDrivers = QgsOgrProviderUtils::directoryDrivers();
300 mProtocolDrivers = QgsOgrProviderUtils::protocolDrivers();
307 void QgsProviderRegistry::clean()
310 if ( QgsProject::sProject )
313 Providers::const_iterator it = mProviders.begin();
315 while ( it != mProviders.end() )
318 it->second->cleanupProvider();
325 bool QgsProviderRegistry::exists()
327 return static_cast< bool >( sInstance );
333 if ( sInstance ==
this )
353 Providers::const_iterator it = mProviders.begin();
355 if ( mProviders.empty() )
356 return QObject::tr(
"No data provider plugins are available. No vector layers can be loaded" );
361 list += QLatin1String(
"<ol>" );
363 while ( it != mProviders.end() )
366 list += QLatin1String(
"<li>" );
368 list += it->second->description();
371 list += QLatin1String(
"<br></li>" );
379 list += QLatin1String(
"</ol>" );
386 mLibraryDirectory = path;
393 return mLibraryDirectory;
406 QgsDataProvider::ReadFlags flags )
423 const QList< QgsDataItemProvider * > itemProviders =
dataItemProviders( providerKey );
428 ret = ret | itemProvider->capabilities();
439 return QVariantMap();
456 bool overwrite, QMap<int, int> &oldToNewAttrIdxMap,
457 QString &errorMessage,
458 const QMap<QString, QVariant> *options )
464 return meta->
createEmptyLayer( uri, fields, wkbType, srs, overwrite, oldToNewAttrIdxMap, errorMessage, options );
468 errorMessage = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
477 const QStringList &createOptions )
492 return QList<QPair<QString, QString> >();
501 return QList<QgsDataItemProvider *>();
504 int QgsProviderRegistry::listStyles(
const QString &providerKey,
const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause )
510 res = meta->
listStyles( uri, ids, names, descriptions, errCause );
514 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
529 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
543 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
549 const QString &sldStyle,
const QString &styleName,
const QString &styleDescription,
550 const QString &uiFileContent,
bool useAsDefault, QString &errCause )
555 ret = meta->
saveStyle( uri, qmlStyle, sldStyle, styleName, styleDescription,
556 uiFileContent, useAsDefault, errCause );
559 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
572 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
581 return meta->
createDb( dbPath, errCause );
584 errCause = QStringLiteral(
"Resolving createDb(...) failed" );
601 Q_UNUSED( providerKey );
604 Q_UNUSED( widgetMode );
605 QgsDebugMsg(
"deprecated call - use QgsGui::sourceSelectProviderRegistry()->createDataSourceWidget() instead" );
610 QString
const &functionName )
613 QString lib =
library( providerKey );
618 QLibrary myLib( lib );
620 QgsDebugMsg(
"Library name is " + myLib.fileName() );
624 return myLib.resolve( functionName.toLatin1().data() );
628 QgsDebugMsg(
"Cannot load library: " + myLib.errorString() );
636 QString lib =
library( providerKey );
641 std::unique_ptr< QLibrary > myLib(
new QLibrary( lib ) );
643 QgsDebugMsg(
"Library name is " + myLib->fileName() );
646 return myLib.release();
648 QgsDebugMsg(
"Cannot load library: " + myLib->errorString() );
655 QgsDebugMsg(
"deprecated - use QgsGui::providerGuiRegistry() instead." );
674 QgsDebugMsgLevel( QStringLiteral(
"Trying to register a null metadata provider!" ), 2 );
681 return mVectorFileFilters;
686 return mRasterFileFilters;
691 return mMeshFileFilters;
696 return mMeshDatasetFileFilters;
701 return mDatabaseDrivers;
706 return mDirectoryDrivers;
711 return mProtocolDrivers;
717 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
719 lst.append( it->first );
726 return findMetadata_( mProviders, providerKey );
void cleanupProviderFunction_t()
DataType
Raster data types.
Setting options for creating vector data providers.
bool createDb(const QString &providerKey, const QString &dbPath, QString &errCause)
Creates database by the provider on the path.
Abstract base class for spatial data provider implementations.
#define QgsDebugMsgLevel(str, level)
A registry / canonical manager of data providers.
Q_DECL_DEPRECATED QLibrary * createProviderLibrary(const QString &providerKey) const
Returns a new QLibrary for the specified providerKey.
QgsTransaction * createTransaction(const QString &providerKey, const QString &connString)
Returns new instance of transaction.
const QgsCoordinateReferenceSystem & crs
virtual ~QgsProviderRegistry()
QString encodeUri(const QString &providerKey, const QVariantMap &parts)
Reassembles a provider data source URI from its component paths (e.g.
Container of fields for a vector layer.
int listStyles(const QString &providerKey, const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause)
Lists stored layer styles in the provider defined by providerKey and uri.
QStringList providerList() const
Returns list of available providers by their keys.
void removeAllMapLayers()
Removes all registered layers.
static QgsProject * instance()
Returns the QgsProject singleton instance.
virtual QString fileMeshFilters() const
Returns mesh file filter string.
bool deleteStyleById(const QString &providerKey, const QString &uri, QString styleId, QString &errCause)
Deletes a layer style defined by styleId.
Type
The WKB type describes the number of dimensions a geometry has.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
std::map< QString, QgsProviderMetadata * > Providers
Type for data provider metadata associative container.
QList< QgsDataItemProvider * > dataItemProviders(const QString &providerKey) const
Returns list of data item providers of the provider.
QList< QPair< QString, QString > > pyramidResamplingMethods(const QString &providerKey)
Returns list of raster pyramid resampling methods.
WidgetMode
Different ways a source select dialog can be used.
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)
Creates new empty vector layer.
#define Q_NOWARN_DEPRECATED_POP
void setLibraryDirectory(const QDir &path)
Sets library directory where to search for plugins.
QString getStyleById(const QString &providerKey, const QString &uri, QString styleId, QString &errCause)
Gets a layer style defined by styleId.
QString pluginList(bool asHtml=false) const
Returns list of provider plugins found.
virtual QgsRasterDataProvider * createRasterDataProvider(const QString &providerKey, 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 new instance of raster data provider.
This is the interface for those who want to add custom data items to the browser tree.
Q_DECL_DEPRECATED QFunctionPointer function(const QString &providerKey, const QString &functionName)
Gets pointer to provider function.
virtual QString fileRasterFilters() const
Returns raster file filter string.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
Q_DECL_DEPRECATED int providerCapabilities(const QString &providerKey) const
Returns the provider capabilities.
@ ErrInvalidProvider
Could not find a matching provider key.
This class represents a coordinate reference system (CRS).
virtual QString directoryDrivers() const
Returns a string containing the available directory drivers.
virtual QString fileMeshDatasetFilters() const
Returns mesh's dataset file filter string.
QString loadStyle(const QString &providerKey, const QString &uri, QString &errCause)
Loads a layer style defined by uri.
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource, const QgsDataProvider::ProviderOptions &options=QgsDataProvider::ProviderOptions(), QgsDataProvider::ReadFlags flags=QgsDataProvider::ReadFlags())
Creates a new instance of a provider.
bool registerProvider(QgsProviderMetadata *providerMetadata)
register a new vector data provider from its providerMetadata
bool saveStyle(const QString &providerKey, 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 databaseDrivers() const
Returns a string containing the available database drivers.
Scoped object for logging of the runtime for a single operation or group of operations.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
virtual QString fileVectorFilters() const
Returns vector file filter string.
virtual QString protocolDrivers() const
Returns a string containing the available protocol drivers.
Q_DECL_DEPRECATED QString library(const QString &providerKey) const
Returns path for the library of the provider.
QDir libraryDirectory() const
Returns the library directory where plugins are found.
Q_DECL_DEPRECATED QWidget * createSelectionWidget(const QString &providerKey, QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags(), QgsProviderRegistry::WidgetMode widgetMode=QgsProviderRegistry::WidgetMode::None)
Returns a new widget for selecting layers from a provider.
Q_DECL_DEPRECATED void registerGuis(QWidget *widget)
#define Q_NOWARN_DEPRECATED_PUSH
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Base class for raster data providers.