QGIS API Documentation  3.6.0-Noosa (5873452)
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 typedef QList<QgsDataItemProvider *> *dataItemProviders_t();
25 
26 
37 {
38  public:
39 
47  QgsDataItemProviderFromPlugin( const QString &name, dataCapabilities_t *capabilitiesFunc, dataItem_t *dataItemFunc, handlesDirectoryPath_t *handlesDirectoryPathFunc )
48  : mName( name )
49  , mCapabilitiesFunc( capabilitiesFunc )
50  , mDataItemFunc( dataItemFunc )
51  , mHandlesDirectoryPathFunc( handlesDirectoryPathFunc )
52  {
53  }
54 
55  QString name() override { return mName; }
56 
57  int capabilities() override { return mCapabilitiesFunc(); }
58 
59  QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override { return mDataItemFunc( path, parentItem ); }
60 
61  bool handlesDirectoryPath( const QString &path ) override
62  {
64  return mHandlesDirectoryPathFunc( path );
65  else
66  return false;
67  }
68 
69  protected:
70  QString mName;
74 };
75 
76 
78 {
79  QStringList providersList = QgsProviderRegistry::instance()->providerList();
80 
81  Q_FOREACH ( const QString &key, providersList )
82  {
83  std::unique_ptr< QLibrary > library( QgsProviderRegistry::instance()->createProviderLibrary( key ) );
84  if ( !library )
85  continue;
86 
87  // new / better way of returning data items from providers
88 
89  dataItemProviders_t *dataItemProvidersFn = reinterpret_cast< dataItemProviders_t * >( cast_to_fptr( library->resolve( "dataItemProviders" ) ) );
90  if ( dataItemProvidersFn )
91  {
92  QList<QgsDataItemProvider *> *providerList = dataItemProvidersFn();
93  // the function is a factory - we keep ownership of the returned providers
94  mProviders << *providerList;
95  delete providerList;
96  }
97 
98  // legacy support - using dataItem() and dataCapabilities() methods
99 
100  dataCapabilities_t *dataCapabilities = reinterpret_cast< dataCapabilities_t * >( cast_to_fptr( library->resolve( "dataCapabilities" ) ) );
101  if ( !dataCapabilities )
102  {
103  QgsDebugMsg( library->fileName() + " does not have dataCapabilities" );
104  continue;
105  }
106 
107  dataItem_t *dataItem = reinterpret_cast< dataItem_t * >( cast_to_fptr( library->resolve( "dataItem" ) ) );
108  if ( !dataItem )
109  {
110  QgsDebugMsg( library->fileName() + " does not have dataItem" );
111  continue;
112  }
113 
114  handlesDirectoryPath_t *handlesDirectoryPath = reinterpret_cast< handlesDirectoryPath_t * >( cast_to_fptr( library->resolve( "handlesDirectoryPath" ) ) );
115 
116  mProviders.append( new QgsDataItemProviderFromPlugin( library->fileName(), dataCapabilities, dataItem, handlesDirectoryPath ) );
117  }
118 }
119 
121 {
122  qDeleteAll( mProviders );
123 }
124 
126 {
127  mProviders.append( provider );
128 }
129 
131 {
132  int index = mProviders.indexOf( provider );
133  if ( index >= 0 )
134  delete mProviders.takeAt( index );
135 }
QString name() override
Human-readable name of the provider name.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
bool handlesDirectoryPath_t(const QString &path)
handlesDirectoryPath function
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
handlesDirectoryPath_t * mHandlesDirectoryPathFunc
QgsDataItemProviderFromPlugin(const QString &name, dataCapabilities_t *capabilitiesFunc, dataItem_t *dataItemFunc, handlesDirectoryPath_t *handlesDirectoryPathFunc)
QgsDataItemProviderFromPlugin constructor.
void removeProvider(QgsDataItemProvider *provider)
Removes a provider implementation from the registry.
#define cast_to_fptr(f)
Definition: qgis.h:158
Base class for all items in the model.
Definition: qgsdataitem.h:49
Simple data item provider implementation that handles the support for provider plugins (which may con...
void addProvider(QgsDataItemProvider *provider)
Adds a provider implementation to the registry.
bool handlesDirectoryPath(const QString &path) override
Returns true if the provider will handle the directory at the specified path.
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:42
QStringList providerList() const
Returns list of available providers by their keys.
int capabilities() override
Returns combination of flags from QgsDataProvider::DataCapabilities.
int dataCapabilities_t()
QgsDataItem * createDataItem(const QString &path, QgsDataItem *parentItem) override
Create a new instance of QgsDataItem (or null) for given path and parent item.
QList< QgsDataItemProvider * > * dataItemProviders_t()
This is the interface for those who want to add custom data items to the browser tree.