QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
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;
421 const QList< QgsDataItemProvider * > itemProviders =
dataItemProviders( providerKey );
426 ret = ret | itemProvider->capabilities();
437 return QVariantMap();
454 bool overwrite, QMap<int, int> &oldToNewAttrIdxMap,
455 QString &errorMessage,
456 const QMap<QString, QVariant> *options )
462 return meta->
createEmptyLayer( uri, fields, wkbType, srs, overwrite, oldToNewAttrIdxMap, errorMessage, options );
466 errorMessage = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
475 const QStringList &createOptions )
490 return QList<QPair<QString, QString> >();
499 return QList<QgsDataItemProvider *>();
502 int QgsProviderRegistry::listStyles(
const QString &providerKey,
const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause )
508 res = meta->
listStyles( uri, ids, names, descriptions, errCause );
512 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
527 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
541 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
547 const QString &sldStyle,
const QString &styleName,
const QString &styleDescription,
548 const QString &uiFileContent,
bool useAsDefault, QString &errCause )
553 ret = meta->
saveStyle( uri, qmlStyle, sldStyle, styleName, styleDescription,
554 uiFileContent, useAsDefault, errCause );
557 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
570 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
579 return meta->
createDb( dbPath, errCause );
582 errCause = QStringLiteral(
"Resolving createDb(...) failed" );
599 Q_UNUSED( providerKey );
602 Q_UNUSED( widgetMode );
603 QgsDebugMsg(
"deprecated call - use QgsGui::sourceSelectProviderRegistry()->createDataSourceWidget() instead" );
608 QString
const &functionName )
611 QString lib =
library( providerKey );
616 QLibrary myLib( lib );
618 QgsDebugMsg(
"Library name is " + myLib.fileName() );
622 return myLib.resolve( functionName.toLatin1().data() );
626 QgsDebugMsg(
"Cannot load library: " + myLib.errorString() );
634 QString lib =
library( providerKey );
639 std::unique_ptr< QLibrary > myLib(
new QLibrary( lib ) );
641 QgsDebugMsg(
"Library name is " + myLib->fileName() );
644 return myLib.release();
646 QgsDebugMsg(
"Cannot load library: " + myLib->errorString() );
653 QgsDebugMsg(
"deprecated - use QgsGui::providerGuiRegistry() instead." );
672 QgsDebugMsgLevel( QStringLiteral(
"Trying to register a null metadata provider!" ), 2 );
679 return mVectorFileFilters;
684 return mRasterFileFilters;
689 return mMeshFileFilters;
694 return mMeshDatasetFileFilters;
699 return mDatabaseDrivers;
704 return mDirectoryDrivers;
709 return mProtocolDrivers;
715 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
717 lst.append( it->first );
724 return findMetadata_( mProviders, providerKey );
void cleanupProviderFunction_t()
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource, const QgsDataProvider::ProviderOptions &options=QgsDataProvider::ProviderOptions())
Creates a new instance of a provider.
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.
#define QgsDebugMsgLevel(str, level)
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.
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.
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.
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.
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.
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.
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.