QGIS API Documentation 3.31.0-Master (d8a37248f1)
qgsproviderguiregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsproviderrguiegistry.cpp
3 -------------------
4 begin : June 2019
5 copyright : (C) 2019 by Peter Petrik
6 email : zilolv at google dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include <QString>
21#include <QDir>
22#include <QLibrary>
23#include <QRegularExpression>
24
25#include "qgslogger.h"
26#include "qgsgdalguiprovider.h"
27#include "qgsogrguiprovider.h"
29#include "qgspointcloudproviderguimetadata.h"
31
32#include "qgsmbtilesvectortileguiprovider.h"
33#include "qgsvtpkvectortileguiprovider.h"
34
35#ifdef HAVE_EPT
36#include "qgseptproviderguimetadata.h"
37#endif
38
39#ifdef HAVE_COPC
40#include "qgscopcproviderguimetadata.h"
41#endif
42
43#ifdef HAVE_STATIC_PROVIDERS
44#include "qgswmsprovidergui.h"
45#include "qgswcsprovidergui.h"
46#include "qgsdelimitedtextprovidergui.h"
47#include "qgsarcgisrestprovidergui.h"
48#ifdef HAVE_SPATIALITE
49#include "qgsspatialiteprovidergui.h"
50#include "qgswfsprovidergui.h"
51#include "qgsvirtuallayerprovidergui.h"
52#endif
53#ifdef HAVE_POSTGRESQL
54#include "qgspostgresprovidergui.h"
55#endif
56#endif
57
66static
68 QString const &providerKey )
69{
70 const QgsProviderGuiRegistry::GuiProviders::const_iterator i = metaData.find( providerKey );
71 if ( i != metaData.end() )
72 {
73 return i->second;
74 }
75
76 return nullptr;
77} // findMetadata_
78
80{
81 loadStaticProviders();
82 loadDynamicProviders( pluginPath );
83}
84
85void QgsProviderGuiRegistry::loadStaticProviders( )
86{
87 // Register static providers
88 QgsProviderGuiMetadata *gdal = new QgsGdalGuiProviderMetadata();
89 mProviders[ gdal->key() ] = gdal;
90
91 QgsProviderGuiMetadata *ogr = new QgsOgrGuiProviderMetadata();
92 mProviders[ ogr->key() ] = ogr;
93
94 QgsProviderGuiMetadata *vt = new QgsVectorTileProviderGuiMetadata();
95 mProviders[ vt->key() ] = vt;
96
97 QgsProviderGuiMetadata *mbtilesVectorTiles = new QgsMbtilesVectorTileGuiProviderMetadata();
98 mProviders[ mbtilesVectorTiles->key() ] = mbtilesVectorTiles;
99
100 QgsProviderGuiMetadata *vtpkVectorTiles = new QgsVtpkVectorTileGuiProviderMetadata();
101 mProviders[ vtpkVectorTiles->key() ] = vtpkVectorTiles;
102
103#ifdef HAVE_EPT
104 QgsProviderGuiMetadata *ept = new QgsEptProviderGuiMetadata();
105 mProviders[ ept->key() ] = ept;
106#endif
107
108#ifdef HAVE_COPC
109 QgsProviderGuiMetadata *copc = new QgsCopcProviderGuiMetadata();
110 mProviders[ copc->key() ] = copc;
111#endif
112
113 // only show point cloud option if we have at least one point cloud provider available!
114 if ( !QgsProviderRegistry::instance()->filePointCloudFilters().isEmpty() )
115 {
116 QgsProviderGuiMetadata *pointcloud = new QgsPointCloudProviderGuiMetadata();
117 mProviders[ pointcloud->key() ] = pointcloud;
118 }
119
120#ifdef HAVE_STATIC_PROVIDERS
121 QgsProviderGuiMetadata *wms = new QgsWmsProviderGuiMetadata();
122 mProviders[ wms->key() ] = wms;
123 QgsProviderGuiMetadata *wcs = new QgsWcsProviderGuiMetadata();
124 mProviders[ wcs->key() ] = wcs;
125 QgsProviderGuiMetadata *delimitedtext = new QgsDelimitedTextProviderGuiMetadata();
126 mProviders[ delimitedtext->key() ] = delimitedtext;
127 QgsProviderGuiMetadata *arc = new QgsArcGisRestProviderGuiMetadata();
128 mProviders[ arc->key() ] = arc;
129#ifdef HAVE_SPATIALITE
130 QgsProviderGuiMetadata *spatialite = new QgsSpatiaLiteProviderGuiMetadata();
131 mProviders[ spatialite->key() ] = spatialite;
132 QgsProviderGuiMetadata *wfs = new QgsWfsProviderGuiMetadata();
133 mProviders[ wfs->key() ] = wfs;
134 QgsProviderGuiMetadata *virtuallayer = new QgsVirtualLayerProviderGuiMetadata();
135 mProviders[ virtuallayer->key() ] = virtuallayer;
136#endif
137#ifdef HAVE_POSTGRESQL
138 QgsProviderGuiMetadata *postgres = new QgsPostgresProviderGuiMetadata();
139 mProviders[ postgres->key() ] = postgres;
140#endif
141#endif
142}
143
144void QgsProviderGuiRegistry::loadDynamicProviders( const QString &pluginPath )
145{
146#ifdef HAVE_STATIC_PROVIDERS
147 Q_UNUSED( pluginPath )
148 QgsDebugMsgLevel( QStringLiteral( "Forced only static GUI providers" ), 2 );
149#else
150 typedef QgsProviderGuiMetadata *factory_function( );
151
152 // add dynamic providers
153 QDir mLibraryDirectory( pluginPath );
154 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
155 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
156
157#if defined(Q_OS_WIN) || defined(__CYGWIN__)
158 mLibraryDirectory.setNameFilters( QStringList( "*.dll" ) );
159#elif defined(ANDROID)
160 mLibraryDirectory.setNameFilters( QStringList( "*provider.so" ) );
161#else
162 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral( "*.so" ) ) );
163#endif
164
165 QgsDebugMsgLevel( QStringLiteral( "Checking %1 for GUI provider plugins" ).arg( mLibraryDirectory.path() ), 2 );
166
167 if ( mLibraryDirectory.count() == 0 )
168 {
169 QgsDebugError( QStringLiteral( "No dynamic QGIS GUI provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() ) );
170 }
171
172 // provider file regex pattern, only files matching the pattern are loaded if the variable is defined
173 const QString filePattern = getenv( "QGIS_PROVIDER_FILE" );
174 QRegularExpression fileRegexp;
175 if ( !filePattern.isEmpty() )
176 {
177 fileRegexp.setPattern( filePattern );
178 }
179
180 const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
181 for ( const QFileInfo &fi : constEntryInfoList )
182 {
183 if ( !fileRegexp.pattern().isEmpty() )
184 {
185 const QRegularExpressionMatch fileNameMatch = fileRegexp.match( fi.fileName() );
186 if ( !fileNameMatch.hasMatch() )
187 {
188 QgsDebugMsgLevel( "provider " + fi.fileName() + " skipped because doesn't match pattern " + filePattern, 2 );
189 continue;
190 }
191 }
192
193 QLibrary myLib( fi.filePath() );
194 if ( myLib.load() )
195 {
196 QFunctionPointer func = myLib.resolve( QStringLiteral( "providerGuiMetadataFactory" ).toLatin1().data() );
197 factory_function *function = reinterpret_cast< factory_function * >( cast_to_fptr( func ) );
198 if ( !function )
199 continue;
200
201 QgsProviderGuiMetadata *meta = function( );
202
203 if ( !meta )
204 continue;
205
206 const QString providerKey = meta->key();
207
208 // check if such providers is already registered
209 if ( findMetadata_( mProviders, providerKey ) )
210 continue;
211
212 mProviders[providerKey] = meta;
213 }
214 }
215#endif
216}
217
219{
220 GuiProviders::const_iterator it = mProviders.begin();
221 while ( it != mProviders.end() )
222 {
223 delete it->second;
224 ++it;
225 }
226 mProviders.clear();
227}
228
229void QgsProviderGuiRegistry::registerGuis( QMainWindow *parent )
230{
231 for ( auto it = mProviders.begin(); it != mProviders.end(); ++it )
232 {
233 it->second->registerGui( parent );
234 }
235}
236
237const QList<QgsDataItemGuiProvider *> QgsProviderGuiRegistry::dataItemGuiProviders( const QString &providerKey )
238{
239 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
240 if ( meta )
241 return meta->dataItemGuiProviders();
242 return QList<QgsDataItemGuiProvider *>();
243}
244
245QList<QgsSourceSelectProvider *> QgsProviderGuiRegistry::sourceSelectProviders( const QString &providerKey )
246{
247 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
248 if ( meta )
249 return meta->sourceSelectProviders();
250 return QList<QgsSourceSelectProvider *> ();
251}
252
253QList<QgsProjectStorageGuiProvider *> QgsProviderGuiRegistry::projectStorageGuiProviders( const QString &providerKey )
254{
255 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
256 if ( meta )
257 return meta->projectStorageGuiProviders();
258 return QList<QgsProjectStorageGuiProvider *>();
259}
260
261QList<QgsSubsetStringEditorProvider *> QgsProviderGuiRegistry::subsetStringEditorProviders( const QString &providerKey )
262{
263 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
264 if ( meta )
265 return meta->subsetStringEditorProviders();
266 return QList<QgsSubsetStringEditorProvider *>();
267}
268
269QList<QgsProviderSourceWidgetProvider *> QgsProviderGuiRegistry::sourceWidgetProviders( const QString &providerKey )
270{
271 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
272 if ( meta )
273 return meta->sourceWidgetProviders();
274 return QList<QgsProviderSourceWidgetProvider *>();
275}
276
277QList<const QgsMapLayerConfigWidgetFactory *> QgsProviderGuiRegistry::mapLayerConfigWidgetFactories( QgsMapLayer *layer )
278{
279 QList<const QgsMapLayerConfigWidgetFactory *> res;
280 for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
281 {
282 const QList<const QgsMapLayerConfigWidgetFactory *> providerFactories = ( *it ).second->mapLayerConfigWidgetFactories();
283 for ( const QgsMapLayerConfigWidgetFactory *factory : providerFactories )
284 {
285 if ( !layer || factory->supportsLayer( layer ) )
286 res << factory;
287 }
288 }
289 return res;
290}
291
293{
294 QStringList lst;
295 for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
296 {
297 lst.append( it->first );
298 }
299 return lst;
300}
301
302const QgsProviderGuiMetadata *QgsProviderGuiRegistry::providerMetadata( const QString &providerKey ) const
303{
304 return findMetadata_( mProviders, providerKey );
305}
Factory class for creating custom map layer property pages.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Holds data for GUI part of the data providers.
virtual QList< QgsSubsetStringEditorProvider * > subsetStringEditorProviders()
Returns subset string editor providers.
virtual QList< QgsDataItemGuiProvider * > dataItemGuiProviders()
Returns data item gui providers.
virtual QList< QgsProjectStorageGuiProvider * > projectStorageGuiProviders()
Returns project storage gui providers.
QString key() const
Returns unique provider key.
virtual QList< QgsProviderSourceWidgetProvider * > sourceWidgetProviders()
Returns source widget providers.
virtual QList< QgsSourceSelectProvider * > sourceSelectProviders()
Returns source select providers.
virtual const QList< QgsDataItemGuiProvider * > dataItemGuiProviders(const QString &providerKey)
Returns all data item gui providers registered in provider with providerKey.
virtual QList< QgsSubsetStringEditorProvider * > subsetStringEditorProviders(const QString &providerKey)
Returns all subset string editor providers registered in provider with providerKey.
const QgsProviderGuiMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
virtual QList< QgsProjectStorageGuiProvider * > projectStorageGuiProviders(const QString &providerKey)
Returns all project storage gui providers registered in provider with providerKey.
virtual QList< const QgsMapLayerConfigWidgetFactory * > mapLayerConfigWidgetFactories(QgsMapLayer *layer=nullptr)
Returns all map layer config widget factories associated with the registered providers.
std::map< QString, QgsProviderGuiMetadata * > GuiProviders
Type for data provider metadata associative container.
QgsProviderGuiRegistry(const QString &pluginPath)
Creates registry and loads static provider plugins.
virtual QList< QgsSourceSelectProvider * > sourceSelectProviders(const QString &providerKey)
Returns all source select providers registered in provider with providerKey.
virtual QList< QgsProviderSourceWidgetProvider * > sourceWidgetProviders(const QString &providerKey)
Returns all source widget providers registered in provider with providerKey.
void registerGuis(QMainWindow *widget)
Called during GUI initialization - allows providers to do its internal initialization of GUI componen...
QStringList providerList() const
Returns list of available providers by their keys.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
#define cast_to_fptr(f)
Definition: qgis.h:3803
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugError(str)
Definition: qgslogger.h:38