QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  const auto constProvidersList = providersList;
82  for ( const QString &key : constProvidersList )
83  {
84  std::unique_ptr< QLibrary > library( QgsProviderRegistry::instance()->createProviderLibrary( key ) );
85  if ( !library )
86  continue;
87 
88  // new / better way of returning data items from providers
89 
90  dataItemProviders_t *dataItemProvidersFn = reinterpret_cast< dataItemProviders_t * >( cast_to_fptr( library->resolve( "dataItemProviders" ) ) );
91  if ( dataItemProvidersFn )
92  {
93  QList<QgsDataItemProvider *> *providerList = dataItemProvidersFn();
94  // the function is a factory - we keep ownership of the returned providers
95  mProviders << *providerList;
96  delete providerList;
97  }
98 
99  // legacy support - using dataItem() and dataCapabilities() methods
100 
101  dataCapabilities_t *dataCapabilities = reinterpret_cast< dataCapabilities_t * >( cast_to_fptr( library->resolve( "dataCapabilities" ) ) );
102  if ( !dataCapabilities )
103  {
104  QgsDebugMsg( library->fileName() + " does not have dataCapabilities" );
105  continue;
106  }
107 
108  dataItem_t *dataItem = reinterpret_cast< dataItem_t * >( cast_to_fptr( library->resolve( "dataItem" ) ) );
109  if ( !dataItem )
110  {
111  QgsDebugMsg( library->fileName() + " does not have dataItem" );
112  continue;
113  }
114 
115  handlesDirectoryPath_t *handlesDirectoryPath = reinterpret_cast< handlesDirectoryPath_t * >( cast_to_fptr( library->resolve( "handlesDirectoryPath" ) ) );
116 
117  mProviders.append( new QgsDataItemProviderFromPlugin( library->fileName(), dataCapabilities, dataItem, handlesDirectoryPath ) );
118  }
119 }
120 
122 {
123  qDeleteAll( mProviders );
124 }
125 
127 {
128  mProviders.append( provider );
129 }
130 
132 {
133  int index = mProviders.indexOf( provider );
134  if ( index >= 0 )
135  delete mProviders.takeAt( index );
136 }
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 nullptr) 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.