QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
24 #include "qgslogger.h"
25 #include "qgsgdalguiprovider.h"
26 #include "qgsogrguiprovider.h"
28 #include "qgspointcloudproviderguimetadata.h"
30 
31 #ifdef HAVE_EPT
32 #include "qgseptproviderguimetadata.h"
33 #endif
34 
35 #ifdef HAVE_STATIC_PROVIDERS
36 #include "qgswmsprovidergui.h"
37 #include "qgspostgresprovidergui.h"
38 #endif
39 
48 static
49 QgsProviderGuiMetadata *findMetadata_( QgsProviderGuiRegistry::GuiProviders const &metaData,
50  QString const &providerKey )
51 {
52  QgsProviderGuiRegistry::GuiProviders::const_iterator i = metaData.find( providerKey );
53  if ( i != metaData.end() )
54  {
55  return i->second;
56  }
57 
58  return nullptr;
59 } // findMetadata_
60 
62 {
63  loadStaticProviders();
64  loadDynamicProviders( pluginPath );
65 }
66 
67 void QgsProviderGuiRegistry::loadStaticProviders( )
68 {
69  // Register static providers
70  QgsProviderGuiMetadata *gdal = new QgsGdalGuiProviderMetadata();
71  mProviders[ gdal->key() ] = gdal;
72 
73  QgsProviderGuiMetadata *ogr = new QgsOgrGuiProviderMetadata();
74  mProviders[ ogr->key() ] = ogr;
75 
76  QgsProviderGuiMetadata *vt = new QgsVectorTileProviderGuiMetadata();
77  mProviders[ vt->key() ] = vt;
78 
79 #ifdef HAVE_EPT
80  QgsProviderGuiMetadata *ept = new QgsEptProviderGuiMetadata();
81  mProviders[ ept->key() ] = ept;
82 #endif
83 
84  // only show point cloud option if we have at least one point cloud provider available!
85  if ( !QgsProviderRegistry::instance()->filePointCloudFilters().isEmpty() )
86  {
87  QgsProviderGuiMetadata *pointcloud = new QgsPointCloudProviderGuiMetadata();
88  mProviders[ pointcloud->key() ] = pointcloud;
89  }
90 
91 #ifdef HAVE_STATIC_PROVIDERS
92  QgsProviderGuiMetadata *wms = new QgsWmsProviderGuiMetadata();
93  mProviders[ wms->key() ] = wms;
94 
95  QgsProviderGuiMetadata *postgres = new QgsPostgresProviderGuiMetadata();
96  mProviders[ postgres->key() ] = postgres;
97 #endif
98 }
99 
100 void QgsProviderGuiRegistry::loadDynamicProviders( const QString &pluginPath )
101 {
102 #ifdef HAVE_STATIC_PROVIDERS
103  QgsDebugMsg( QStringLiteral( "Forced only static GUI providers" ) );
104 #else
105  typedef QgsProviderGuiMetadata *factory_function( );
106 
107  // add dynamic providers
108  QDir mLibraryDirectory( pluginPath );
109  mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
110  mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
111 
112 #if defined(Q_OS_WIN) || defined(__CYGWIN__)
113  mLibraryDirectory.setNameFilters( QStringList( "*.dll" ) );
114 #elif defined(ANDROID)
115  mLibraryDirectory.setNameFilters( QStringList( "*provider.so" ) );
116 #else
117  mLibraryDirectory.setNameFilters( QStringList( QStringLiteral( "*.so" ) ) );
118 #endif
119 
120  QgsDebugMsgLevel( QStringLiteral( "Checking %1 for GUI provider plugins" ).arg( mLibraryDirectory.path() ), 2 );
121 
122  if ( mLibraryDirectory.count() == 0 )
123  {
124  QgsDebugMsg( QStringLiteral( "No dynamic QGIS GUI provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() ) );
125  }
126 
127  // provider file regex pattern, only files matching the pattern are loaded if the variable is defined
128  QString filePattern = getenv( "QGIS_PROVIDER_FILE" );
129  QRegExp fileRegexp;
130  if ( !filePattern.isEmpty() )
131  {
132  fileRegexp.setPattern( filePattern );
133  }
134 
135  const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
136  for ( const QFileInfo &fi : constEntryInfoList )
137  {
138  if ( !fileRegexp.isEmpty() )
139  {
140  if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
141  {
142  QgsDebugMsg( "provider " + fi.fileName() + " skipped because doesn't match pattern " + filePattern );
143  continue;
144  }
145  }
146 
147  QLibrary myLib( fi.filePath() );
148  if ( myLib.load() )
149  {
150  QFunctionPointer func = myLib.resolve( QStringLiteral( "providerGuiMetadataFactory" ).toLatin1().data() );
151  factory_function *function = reinterpret_cast< factory_function * >( cast_to_fptr( func ) );
152  if ( !function )
153  continue;
154 
155  QgsProviderGuiMetadata *meta = function( );
156 
157  if ( !meta )
158  continue;
159 
160  const QString providerKey = meta->key();
161 
162  // check if such providers is already registered
163  if ( findMetadata_( mProviders, providerKey ) )
164  continue;
165 
166  mProviders[providerKey] = meta;
167  }
168  }
169 #endif
170 }
171 
173 {
174  GuiProviders::const_iterator it = mProviders.begin();
175  while ( it != mProviders.end() )
176  {
177  delete it->second;
178  ++it;
179  }
180  mProviders.clear();
181 }
182 
183 void QgsProviderGuiRegistry::registerGuis( QMainWindow *parent )
184 {
185  GuiProviders::const_iterator it = mProviders.begin();
186  while ( it != mProviders.end() )
187  {
188  it->second->registerGui( parent );
189  ++it;
190  }
191 }
192 
193 const QList<QgsDataItemGuiProvider *> QgsProviderGuiRegistry::dataItemGuiProviders( const QString &providerKey )
194 {
195  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
196  if ( meta )
197  return meta->dataItemGuiProviders();
198  return QList<QgsDataItemGuiProvider *>();
199 }
200 
201 QList<QgsSourceSelectProvider *> QgsProviderGuiRegistry::sourceSelectProviders( const QString &providerKey )
202 {
203  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
204  if ( meta )
205  return meta->sourceSelectProviders();
206  return QList<QgsSourceSelectProvider *> ();
207 }
208 
209 QList<QgsProjectStorageGuiProvider *> QgsProviderGuiRegistry::projectStorageGuiProviders( const QString &providerKey )
210 {
211  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
212  if ( meta )
213  return meta->projectStorageGuiProviders();
214  return QList<QgsProjectStorageGuiProvider *>();
215 }
216 
217 QList<QgsSubsetStringEditorProvider *> QgsProviderGuiRegistry::subsetStringEditorProviders( const QString &providerKey )
218 {
219  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
220  if ( meta )
221  return meta->subsetStringEditorProviders();
222  return QList<QgsSubsetStringEditorProvider *>();
223 }
224 
225 QList<QgsProviderSourceWidgetProvider *> QgsProviderGuiRegistry::sourceWidgetProviders( const QString &providerKey )
226 {
227  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
228  if ( meta )
229  return meta->sourceWidgetProviders();
230  return QList<QgsProviderSourceWidgetProvider *>();
231 }
232 
233 QList<const QgsMapLayerConfigWidgetFactory *> QgsProviderGuiRegistry::mapLayerConfigWidgetFactories( QgsMapLayer *layer )
234 {
235  QList<const QgsMapLayerConfigWidgetFactory *> res;
236  for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
237  {
238  const QList<const QgsMapLayerConfigWidgetFactory *> providerFactories = ( *it ).second->mapLayerConfigWidgetFactories();
239  for ( const QgsMapLayerConfigWidgetFactory *factory : providerFactories )
240  {
241  if ( !layer || factory->supportsLayer( layer ) )
242  res << factory;
243  }
244  }
245  return res;
246 }
247 
249 {
250  QStringList lst;
251  for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
252  {
253  lst.append( it->first );
254  }
255  return lst;
256 }
257 
258 const QgsProviderGuiMetadata *QgsProviderGuiRegistry::providerMetadata( const QString &providerKey ) const
259 {
260  return findMetadata_( mProviders, providerKey );
261 }
Factory class for creating custom map layer property pages.
Base class for all map layer types.
Definition: qgsmaplayer.h:70
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:483
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugMsg(str)
Definition: qgslogger.h:38