QGIS API Documentation  2.12.0-Lyon
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 
32 {
33  public:
34  QgsDataItemProviderFromPlugin( const QString& name, dataCapabilities_t* capabilitiesFunc, dataItem_t* dataItemFunc )
35  : mName( name )
36  , mCapabilitiesFunc( capabilitiesFunc )
37  , mDataItemFunc( dataItemFunc )
38  {
39  }
40 
41  virtual QString name() override { return mName; }
42 
43  virtual int capabilities() override { return mCapabilitiesFunc(); }
44 
45  virtual QgsDataItem* createDataItem( const QString& path, QgsDataItem* parentItem ) override { return mDataItemFunc( path, parentItem ); }
46 
47  protected:
51 };
52 
53 
54 QgsDataItemProviderRegistry::QgsDataItemProviderRegistry()
55 {
57 
58  Q_FOREACH ( const QString& key, providersList )
59  {
61  if ( !library )
62  continue;
63 
64  dataCapabilities_t * dataCapabilities = ( dataCapabilities_t * ) cast_to_fptr( library->resolve( "dataCapabilities" ) );
65  if ( !dataCapabilities )
66  {
67  QgsDebugMsg( library->fileName() + " does not have dataCapabilities" );
68  continue;
69  }
70 
71  dataItem_t *dataItem = ( dataItem_t * ) cast_to_fptr( library->resolve( "dataItem" ) );
72  if ( !dataItem )
73  {
74  QgsDebugMsg( library->fileName() + " does not have dataItem" );
75  continue;
76  }
77 
78  mProviders.append( new QgsDataItemProviderFromPlugin( library->fileName(), dataCapabilities, dataItem ) );
79  }
80 }
81 
83 {
84  static QgsDataItemProviderRegistry sInstance;
85  return &sInstance;
86 }
87 
89 {
90  qDeleteAll( mProviders );
91 }
92 
94 {
95  mProviders.append( provider );
96 }
97 
99 {
100  int index = mProviders.indexOf( provider );
101  if ( index >= 0 )
102  delete mProviders.takeAt( index );
103 }
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
T takeAt(int i)
int indexOf(const T &value, int from) const
QStringList providerList() const
Return list of available providers by their keys.
void append(const T &value)
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...
QLibrary * providerLibrary(const QString &providerKey) const
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:241
int dataCapabilities_t()
This is the interface for those who want to add custom data items to the browser tree.