QGIS API Documentation  2.14.11-Essen
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 
24 
33 {
34  public:
35  QgsDataItemProviderFromPlugin( const QString& name, dataCapabilities_t* capabilitiesFunc, dataItem_t* dataItemFunc )
36  : mName( name )
37  , mCapabilitiesFunc( capabilitiesFunc )
38  , mDataItemFunc( dataItemFunc )
39  {
40  }
41 
42  virtual QString name() override { return mName; }
43 
44  virtual int capabilities() override { return mCapabilitiesFunc(); }
45 
46  virtual QgsDataItem* createDataItem( const QString& path, QgsDataItem* parentItem ) override { return mDataItemFunc( path, parentItem ); }
47 
48  protected:
52 };
53 
54 
55 QgsDataItemProviderRegistry::QgsDataItemProviderRegistry()
56 {
58 
59  Q_FOREACH ( const QString& key, providersList )
60  {
62  if ( !library )
63  continue;
64 
65  dataCapabilities_t * dataCapabilities = reinterpret_cast< dataCapabilities_t * >( cast_to_fptr( library->resolve( "dataCapabilities" ) ) );
66  if ( !dataCapabilities )
67  {
68  QgsDebugMsg( library->fileName() + " does not have dataCapabilities" );
69  continue;
70  }
71 
72  dataItem_t *dataItem = reinterpret_cast< dataItem_t * >( cast_to_fptr( library->resolve( "dataItem" ) ) );
73  if ( !dataItem )
74  {
75  QgsDebugMsg( library->fileName() + " does not have dataItem" );
76  continue;
77  }
78 
79  mProviders.append( new QgsDataItemProviderFromPlugin( library->fileName(), dataCapabilities, dataItem ) );
80  }
81 }
82 
84 {
85  static QgsDataItemProviderRegistry sInstance;
86  return &sInstance;
87 }
88 
90 {
91  qDeleteAll( mProviders );
92 }
93 
95 {
96  mProviders.append( provider );
97 }
98 
100 {
101  int index = mProviders.indexOf( provider );
102  if ( index >= 0 )
103  delete mProviders.takeAt( index );
104 }
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
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:75
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:258
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.