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