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