QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsdataitemproviderregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdataitemproviderregistry.cpp
3  --------------------------------------
4  Date : March 2015
5  Copyright : (C) 2015 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgsdataitem.h"
19 #include "qgsdataitemprovider.h"
20 #include "qgsdataprovider.h"
21 #include "qgslogger.h"
22 #include "qgsproviderregistry.h"
23 
25 
26 
37 {
38  public:
39  QgsDataItemProviderFromPlugin( const QString& name, dataCapabilities_t* capabilitiesFunc, dataItem_t* dataItemFunc )
40  : mName( name )
41  , mCapabilitiesFunc( capabilitiesFunc )
42  , mDataItemFunc( dataItemFunc )
43  {
44  }
45 
46  virtual QString name() override { return mName; }
47 
48  virtual int capabilities() override { return mCapabilitiesFunc(); }
49 
50  virtual QgsDataItem* createDataItem( const QString& path, QgsDataItem* parentItem ) override { return mDataItemFunc( path, parentItem ); }
51 
52  protected:
56 };
57 
58 
59 QgsDataItemProviderRegistry::QgsDataItemProviderRegistry()
60 {
62 
63  Q_FOREACH ( const QString& key, providersList )
64  {
66  if ( !library )
67  continue;
68 
69  // new / better way of returning data items from providers
70 
71  dataItemProviders_t* dataItemProvidersFn = reinterpret_cast< dataItemProviders_t * >( cast_to_fptr( library->resolve( "dataItemProviders" ) ) );
72  if ( dataItemProvidersFn )
73  {
74  // the function is a factory - we keep ownership of the returned providers
75  mProviders << dataItemProvidersFn();
76  }
77 
78  // legacy support - using dataItem() and dataCapabilities() methods
79 
80  dataCapabilities_t * dataCapabilities = reinterpret_cast< dataCapabilities_t * >( cast_to_fptr( library->resolve( "dataCapabilities" ) ) );
81  if ( !dataCapabilities )
82  {
83  QgsDebugMsg( library->fileName() + " does not have dataCapabilities" );
84  continue;
85  }
86 
87  dataItem_t *dataItem = reinterpret_cast< dataItem_t * >( cast_to_fptr( library->resolve( "dataItem" ) ) );
88  if ( !dataItem )
89  {
90  QgsDebugMsg( library->fileName() + " does not have dataItem" );
91  continue;
92  }
93 
94  mProviders.append( new QgsDataItemProviderFromPlugin( library->fileName(), dataCapabilities, dataItem ) );
95  }
96 }
97 
99 {
100  static QgsDataItemProviderRegistry sInstance;
101  return &sInstance;
102 }
103 
105 {
106  qDeleteAll( mProviders );
107 }
108 
110 {
111  mProviders.append( provider );
112 }
113 
115 {
116  int index = mProviders.indexOf( provider );
117  if ( index >= 0 )
118  delete mProviders.takeAt( index );
119 }
This singleton class keeps a list of data item providers that may add items to the browser tree...
static unsigned index
static QgsProviderRegistry * instance(const QString &pluginPath=QString::null)
Means of accessing canonical single instance.
static QgsDataItemProviderRegistry * instance()
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QLibrary * providerLibrary(const QString &providerKey) const
QList< QgsDataItemProvider * > dataItemProviders_t()
QgsDataItemProviderFromPlugin(const QString &name, dataCapabilities_t *capabilitiesFunc, dataItem_t *dataItemFunc)
void removeProvider(QgsDataItemProvider *provider)
Remove provider implementation from the list (provider object is deleted)
Base class for all items in the model.
Definition: qgsdataitem.h:79
virtual QString name() override
Human-readable name of the provider name.
Simple data item provider implementation that handles the support for provider plugins (which may con...
virtual int capabilities() override
Return combination of flags from QgsDataProvider::DataCapabilities.
void * resolve(const char *symbol)
void addProvider(QgsDataItemProvider *provider)
Add a provider implementation. Takes ownership of the object.
virtual QgsDataItem * createDataItem(const QString &path, QgsDataItem *parentItem) override
Create a new instance of QgsDataItem (or null) for given path and parent item.
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:38
void(*)() cast_to_fptr(void *p)
Definition: qgis.h:272
QStringList providerList() const
Return list of available providers by their keys.
int dataCapabilities_t()
This is the interface for those who want to add custom data items to the browser tree.