QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
33#include "qgsmbtilesvectortileguiprovider.h"
34#include "qgsvtpkvectortileguiprovider.h"
35#include "qgssensorthingsguiprovider.h"
36#ifdef HAVE_EPT
37#include "qgseptproviderguimetadata.h"
38#endif
39
40#ifdef HAVE_COPC
41#include "qgscopcproviderguimetadata.h"
42#endif
43
44#ifdef HAVE_STATIC_PROVIDERS
45#include "qgswmsprovidergui.h"
46#include "qgswcsprovidergui.h"
47#include "qgsdelimitedtextprovidergui.h"
48#include "qgsarcgisrestprovidergui.h"
49#ifdef HAVE_SPATIALITE
50#include "qgsspatialiteprovidergui.h"
51#include "qgswfsprovidergui.h"
52#include "qgsvirtuallayerprovidergui.h"
53#endif
54#ifdef HAVE_POSTGRESQL
55#include "qgspostgresprovidergui.h"
56#endif
57#endif
58
67static QgsProviderGuiMetadata *findMetadata_( QgsProviderGuiRegistry::GuiProviders const &metaData, QString const &providerKey )
68{
69 const QgsProviderGuiRegistry::GuiProviders::const_iterator i = metaData.find( providerKey );
70 if ( i != metaData.end() )
71 {
72 return i->second;
73 }
74
75 return nullptr;
76} // findMetadata_
77
79{
80 loadStaticProviders();
81 loadDynamicProviders( pluginPath );
82}
83
84void QgsProviderGuiRegistry::loadStaticProviders()
85{
86 // Register static providers
87 QgsProviderGuiMetadata *gdal = new QgsGdalGuiProviderMetadata();
88 mProviders[gdal->key()] = gdal;
89
90 QgsProviderGuiMetadata *ogr = new QgsOgrGuiProviderMetadata();
91 mProviders[ogr->key()] = ogr;
92
93 QgsProviderGuiMetadata *vt = new QgsVectorTileProviderGuiMetadata();
94 mProviders[vt->key()] = vt;
95
96 QgsProviderGuiMetadata *mbtilesVectorTiles = new QgsMbtilesVectorTileGuiProviderMetadata();
97 mProviders[mbtilesVectorTiles->key()] = mbtilesVectorTiles;
98
99 QgsProviderGuiMetadata *vtpkVectorTiles = new QgsVtpkVectorTileGuiProviderMetadata();
100 mProviders[vtpkVectorTiles->key()] = vtpkVectorTiles;
101
102#ifdef HAVE_EPT
103 QgsProviderGuiMetadata *ept = new QgsEptProviderGuiMetadata();
104 mProviders[ept->key()] = ept;
105#endif
106
107#ifdef HAVE_COPC
108 QgsProviderGuiMetadata *copc = new QgsCopcProviderGuiMetadata();
109 mProviders[copc->key()] = copc;
110#endif
111
112 // only show point cloud option if we have at least one point cloud provider available!
113 if ( !QgsProviderRegistry::instance()->filePointCloudFilters().isEmpty() )
114 {
115 QgsProviderGuiMetadata *pointcloud = new QgsPointCloudProviderGuiMetadata();
116 mProviders[pointcloud->key()] = pointcloud;
117 }
118
119 QgsProviderGuiMetadata *tiledScene = new QgsTiledSceneProviderGuiMetadata();
120 mProviders[tiledScene->key()] = tiledScene;
121
122 QgsProviderGuiMetadata *sensorThings = new QgsSensorThingsProviderGuiMetadata();
123 mProviders[sensorThings->key()] = sensorThings;
124
125#ifdef HAVE_STATIC_PROVIDERS
126 QgsProviderGuiMetadata *wms = new QgsWmsProviderGuiMetadata();
127 mProviders[wms->key()] = wms;
128 QgsProviderGuiMetadata *wcs = new QgsWcsProviderGuiMetadata();
129 mProviders[wcs->key()] = wcs;
130 QgsProviderGuiMetadata *delimitedtext = new QgsDelimitedTextProviderGuiMetadata();
131 mProviders[delimitedtext->key()] = delimitedtext;
132 QgsProviderGuiMetadata *arc = new QgsArcGisRestProviderGuiMetadata();
133 mProviders[arc->key()] = arc;
134#ifdef HAVE_SPATIALITE
135 QgsProviderGuiMetadata *spatialite = new QgsSpatiaLiteProviderGuiMetadata();
136 mProviders[spatialite->key()] = spatialite;
137 QgsProviderGuiMetadata *wfs = new QgsWfsProviderGuiMetadata();
138 mProviders[wfs->key()] = wfs;
139 QgsProviderGuiMetadata *virtuallayer = new QgsVirtualLayerProviderGuiMetadata();
140 mProviders[virtuallayer->key()] = virtuallayer;
141#endif
142#ifdef HAVE_POSTGRESQL
143 QgsProviderGuiMetadata *postgres = new QgsPostgresProviderGuiMetadata();
144 mProviders[postgres->key()] = postgres;
145#endif
146#endif
147}
148
149void QgsProviderGuiRegistry::loadDynamicProviders( const QString &pluginPath )
150{
151#ifdef HAVE_STATIC_PROVIDERS
152 Q_UNUSED( pluginPath )
153 QgsDebugMsgLevel( QStringLiteral( "Forced only static GUI providers" ), 2 );
154#else
155 typedef QgsProviderGuiMetadata *factory_function();
156
157 // add dynamic providers
158 QDir mLibraryDirectory( pluginPath );
159 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
160 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
161
162#if defined( Q_OS_WIN ) || defined( __CYGWIN__ )
163 mLibraryDirectory.setNameFilters( QStringList( "*.dll" ) );
164#elif defined( ANDROID )
165 mLibraryDirectory.setNameFilters( QStringList( "*provider.so" ) );
166#else
167 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral( "*.so" ) ) );
168#endif
169
170 QgsDebugMsgLevel( QStringLiteral( "Checking %1 for GUI provider plugins" ).arg( mLibraryDirectory.path() ), 2 );
171
172 if ( mLibraryDirectory.count() == 0 )
173 {
174 QgsDebugError( QStringLiteral( "No dynamic QGIS GUI provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() ) );
175 }
176
177 // provider file regex pattern, only files matching the pattern are loaded if the variable is defined
178 const QString filePattern = getenv( "QGIS_PROVIDER_FILE" );
179 QRegularExpression fileRegexp;
180 if ( !filePattern.isEmpty() )
181 {
182 fileRegexp.setPattern( filePattern );
183 }
184
185 const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
186 for ( const QFileInfo &fi : constEntryInfoList )
187 {
188 if ( !fileRegexp.pattern().isEmpty() )
189 {
190 const QRegularExpressionMatch fileNameMatch = fileRegexp.match( fi.fileName() );
191 if ( !fileNameMatch.hasMatch() )
192 {
193 QgsDebugMsgLevel( "provider " + fi.fileName() + " skipped because doesn't match pattern " + filePattern, 2 );
194 continue;
195 }
196 }
197
198 QLibrary myLib( fi.filePath() );
199 if ( myLib.load() )
200 {
201 QFunctionPointer func = myLib.resolve( QStringLiteral( "providerGuiMetadataFactory" ).toLatin1().data() );
202 factory_function *function = reinterpret_cast<factory_function *>( cast_to_fptr( func ) );
203 if ( !function )
204 continue;
205
206 QgsProviderGuiMetadata *meta = function();
207
208 if ( !meta )
209 continue;
210
211 const QString providerKey = meta->key();
212
213 // check if such providers is already registered
214 if ( findMetadata_( mProviders, providerKey ) )
215 continue;
216
217 mProviders[providerKey] = meta;
218 }
219 }
220#endif
221}
222
224{
225 GuiProviders::const_iterator it = mProviders.begin();
226 while ( it != mProviders.end() )
227 {
228 delete it->second;
229 ++it;
230 }
231 mProviders.clear();
232}
233
234void QgsProviderGuiRegistry::registerGuis( QMainWindow *parent )
235{
236 for ( auto it = mProviders.begin(); it != mProviders.end(); ++it )
237 {
238 it->second->registerGui( parent );
239 }
240}
241
242const QList<QgsDataItemGuiProvider *> QgsProviderGuiRegistry::dataItemGuiProviders( const QString &providerKey )
243{
244 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
245 if ( meta )
246 return meta->dataItemGuiProviders();
247 return QList<QgsDataItemGuiProvider *>();
248}
249
250QList<QgsSourceSelectProvider *> QgsProviderGuiRegistry::sourceSelectProviders( const QString &providerKey )
251{
252 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
253 if ( meta )
254 return meta->sourceSelectProviders();
255 return QList<QgsSourceSelectProvider *>();
256}
257
258QList<QgsProjectStorageGuiProvider *> QgsProviderGuiRegistry::projectStorageGuiProviders( const QString &providerKey )
259{
260 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
261 if ( meta )
262 return meta->projectStorageGuiProviders();
263 return QList<QgsProjectStorageGuiProvider *>();
264}
265
266QList<QgsSubsetStringEditorProvider *> QgsProviderGuiRegistry::subsetStringEditorProviders( const QString &providerKey )
267{
268 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
269 if ( meta )
270 return meta->subsetStringEditorProviders();
271 return QList<QgsSubsetStringEditorProvider *>();
272}
273
274QList<QgsProviderSourceWidgetProvider *> QgsProviderGuiRegistry::sourceWidgetProviders( const QString &providerKey )
275{
276 QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
277 if ( meta )
278 return meta->sourceWidgetProviders();
279 return QList<QgsProviderSourceWidgetProvider *>();
280}
281
282QList<const QgsMapLayerConfigWidgetFactory *> QgsProviderGuiRegistry::mapLayerConfigWidgetFactories( QgsMapLayer *layer )
283{
284 QList<const QgsMapLayerConfigWidgetFactory *> res;
285 for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
286 {
287 const QList<const QgsMapLayerConfigWidgetFactory *> providerFactories = ( *it ).second->mapLayerConfigWidgetFactories();
288 for ( const QgsMapLayerConfigWidgetFactory *factory : providerFactories )
289 {
290 if ( !layer || factory->supportsLayer( layer ) )
291 res << factory;
292 }
293 }
294 return res;
295}
296
298{
299 QStringList lst;
300 for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
301 {
302 lst.append( it->first );
303 }
304 return lst;
305}
306
307const QgsProviderGuiMetadata *QgsProviderGuiRegistry::providerMetadata( const QString &providerKey ) const
308{
309 return findMetadata_( mProviders, providerKey );
310}
Factory class for creating custom map layer property pages.
Base class for all map layer types.
Definition qgsmaplayer.h:76
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:5876
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38