Skip to content
Permalink
Browse files

Add QgsSourceSelectProviderRegistry with tests

  • Loading branch information
elpaso committed Sep 1, 2017
1 parent cd1c9b1 commit 4c46f6464742bee648a301213a10224db0b20bb2
@@ -298,4 +298,5 @@
%Include locator/qgslocatorwidget.sip
%Include qgsadvanceddigitizingcanvasitem.sip
%Include qgssourceselectprovider.sip
%Include qgssourceselectproviderregistry.sip

@@ -25,25 +25,44 @@ class QgsSourceSelectProvider

virtual QString providerKey() const = 0;
%Docstring
Provider key
Data Provider key
:rtype: str
%End

virtual QString name() const;
%Docstring
Source select provider name, this is useful to retrieve
a particular source select in case the provider has more
than one, it should be unique among all providers.

The default implementation returns the providerKey()
:rtype: str
%End

virtual QString text() const = 0;
%Docstring
Text for the menu item entry
Text for the menu item entry, it will be visible to the user so make sure it's translatable
:rtype: str
%End

virtual QIcon icon() const = 0 /Factory/;
%Docstring
Caller takes responsibility of deleting created.
Creates a new instance of an QIcon for the menu item entry
Caller takes responsibility of deleting created.
:rtype: QIcon
%End

virtual QgsAbstractDataSourceWidget *createDataSourceWidget( ) = 0 /Factory/;
virtual int ordering( ) const;
%Docstring
Ordering: the source select provider registry will be able to sort
the source selects (ascending) using this integer value
:rtype: int
%End

virtual QgsAbstractDataSourceWidget *createDataSourceWidget( QWidget *parent = 0 ) const = 0 /Factory/;
%Docstring
Caller takes responsibility of deleting created.
Create a new instance of QgsAbstractDataSourceWidget (or null).
Caller takes responsibility of deleting created.
:rtype: QgsAbstractDataSourceWidget
%End

@@ -0,0 +1,68 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgssourceselectproviderregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsSourceSelectProviderRegistry
{
%Docstring
This class keeps a list of source select providers that may add items to the QgsDataSourceManagerDialog
When created, it automatically adds providers from data provider plugins (e.g. PostGIS, WMS, ...)

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgssourceselectproviderregistry.h"
%End
public:
QgsSourceSelectProviderRegistry();

~QgsSourceSelectProviderRegistry();


QList< QgsSourceSelectProvider*> providers() const;
%Docstring
Get list of available providers
:rtype: list of QgsSourceSelectProvider
%End

void addProvider( QgsSourceSelectProvider *provider /Transfer/ );
%Docstring
Add a provider implementation. Takes ownership of the object.
%End

void removeProvider( QgsSourceSelectProvider *provider );
%Docstring
Remove provider implementation from the list (provider object is deleted)
%End

QgsSourceSelectProvider *providerByName( const QString &name ) const;
%Docstring
Return a provider by name or None if not found
:rtype: QgsSourceSelectProvider
%End

QList<QgsSourceSelectProvider *> providersByKey( const QString &providerKey ) const;
%Docstring
Return a (possibly empty) list of providers by data provider's key
:rtype: list of QgsSourceSelectProvider
%End


private:
QgsSourceSelectProviderRegistry( const QgsSourceSelectProviderRegistry &rh );
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgssourceselectproviderregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -350,6 +350,7 @@ SET(QGIS_GUI_SRCS
qgsdatasourcemanagerdialog.cpp
qgsabstractdatasourcewidget.cpp
qgssourceselectprovider.cpp
qgssourceselectproviderregistry.cpp
)

SET(QGIS_GUI_MOC_HDRS
@@ -713,6 +714,7 @@ SET(QGIS_GUI_HDRS
qgsdatasourcemanagerdialog.h
qgsabstractdatasourcewidget.h
qgssourceselectprovider.h
qgssourceselectproviderregistry.h

ogr/qgsogrhelperfunctions.h
ogr/qgsnewogrconnection.h
@@ -22,6 +22,7 @@
#include "qgsabstractdatasourcewidget.h"

class QString;
class QWidget;

/** \ingroup gui
* This is the interface for those who want to add entries to the \see QgsDataSourceManagerDialog
@@ -33,19 +34,34 @@ class GUI_EXPORT QgsSourceSelectProvider
public:
virtual ~QgsSourceSelectProvider() = default;

//! Provider key
//! Data Provider key
virtual QString providerKey() const = 0;

//! Text for the menu item entry
/** Source select provider name, this is useful to retrieve
* a particular source select in case the provider has more
* than one, it should be unique among all providers.
*
* The default implementation returns the providerKey()
*/
virtual QString name() const { return providerKey(); }

//! Text for the menu item entry, it will be visible to the user so make sure it's translatable
virtual QString text() const = 0;

//! Creates a new instance of an QIcon for the menu item entry
//! Caller takes responsibility of deleting created.
/** Creates a new instance of an QIcon for the menu item entry
* Caller takes responsibility of deleting created.
*/
virtual QIcon icon() const = 0 SIP_FACTORY;

//! Create a new instance of QgsAbstractDataSourceWidget (or null).
//! Caller takes responsibility of deleting created.
virtual QgsAbstractDataSourceWidget *createDataSourceWidget( ) = 0 SIP_FACTORY;
/** Ordering: the source select provider registry will be able to sort
* the source selects (ascending) using this integer value
*/
virtual int ordering( ) const { return 0; }

/** Create a new instance of QgsAbstractDataSourceWidget (or null).
* Caller takes responsibility of deleting created.
*/
virtual QgsAbstractDataSourceWidget *createDataSourceWidget( QWidget *parent = nullptr ) const = 0 SIP_FACTORY;

};

@@ -0,0 +1,93 @@
/***************************************************************************
qgssourceselectproviderregistry.cpp - QgsSourceSelectProviderRegistry
---------------------
begin : 1.9.2017
copyright : (C) 2017 by Alessandro Pasotti
email : apasotti at boundlessgeo dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgssourceselectprovider.h"
#include "qgssourceselectproviderregistry.h"
#include "qgsproviderregistry.h"

#include <memory>

typedef QList<QgsSourceSelectProvider *> *sourceSelectProviders_t();


QgsSourceSelectProviderRegistry::QgsSourceSelectProviderRegistry()
{
QStringList providersList = QgsProviderRegistry::instance()->providerList();

Q_FOREACH ( const QString &key, providersList )
{
std::unique_ptr< QLibrary > library( QgsProviderRegistry::instance()->createProviderLibrary( key ) );
if ( !library )
continue;

sourceSelectProviders_t *sourceSelectProvidersFn = reinterpret_cast< sourceSelectProviders_t * >( cast_to_fptr( library->resolve( "sourceSelectProviders" ) ) );
if ( sourceSelectProvidersFn )
{
QList<QgsSourceSelectProvider *> *providerList = sourceSelectProvidersFn();
// the function is a factory - we keep ownership of the returned providers
for ( auto provider : qgsAsConst( *providerList ) )
{
addProvider( provider );
}
delete providerList;
}
}
}

QgsSourceSelectProviderRegistry::~QgsSourceSelectProviderRegistry()
{
qDeleteAll( mProviders );
}

void QgsSourceSelectProviderRegistry::addProvider( QgsSourceSelectProvider *provider )
{
mProviders.append( provider );
std::sort( mProviders.begin(), mProviders.end(), [ ]( const QgsSourceSelectProvider * first, const QgsSourceSelectProvider * second ) -> bool
{
return first->ordering() < second->ordering();
} );
}

void QgsSourceSelectProviderRegistry::removeProvider( QgsSourceSelectProvider *provider )
{
int index = mProviders.indexOf( provider );
if ( index >= 0 )
delete mProviders.takeAt( index );
}

QgsSourceSelectProvider *QgsSourceSelectProviderRegistry::providerByName( const QString &name ) const
{
for ( const auto provider : qgsAsConst( mProviders ) )
{
if ( provider->name() == name )
{
return provider;
}
}
return nullptr;
}

QList<QgsSourceSelectProvider *> QgsSourceSelectProviderRegistry::providersByKey( const QString &providerKey ) const
{
QList<QgsSourceSelectProvider *> providerList;
for ( const auto provider : qgsAsConst( mProviders ) )
{
if ( provider->providerKey() == providerKey )
{
providerList << provider;
}
}
return providerList;
}
@@ -0,0 +1,68 @@
/***************************************************************************
qgssourceselectproviderregistry.h - QgsSourceSelectProviderRegistry
---------------------
begin : 1.9.2017
copyright : (C) 2017 by Alessandro Pasotti
email : apasotti at boundlessgeo dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSSOURCESELECTPROVIDERREGISTRY_H
#define QGSSOURCESELECTPROVIDERREGISTRY_H

#include "qgis_gui.h"
#include "qgis.h"

class QgsSourceSelectProvider;

/** \ingroup gui
* This class keeps a list of source select providers that may add items to the QgsDataSourceManagerDialog
* When created, it automatically adds providers from data provider plugins (e.g. PostGIS, WMS, ...)
*
* \since QGIS 3.0
*/
class GUI_EXPORT QgsSourceSelectProviderRegistry
{
public:
QgsSourceSelectProviderRegistry();

~QgsSourceSelectProviderRegistry();

//! QgsDataItemProviderRegistry cannot be copied.
QgsSourceSelectProviderRegistry( const QgsSourceSelectProviderRegistry &rh ) = delete;
//! QgsDataItemProviderRegistry cannot be copied.
QgsSourceSelectProviderRegistry &operator=( const QgsSourceSelectProviderRegistry &rh ) = delete;

//! Get list of available providers
QList< QgsSourceSelectProvider *> providers() const { return mProviders; }

//! Add a provider implementation. Takes ownership of the object.
void addProvider( QgsSourceSelectProvider *provider SIP_TRANSFER );

//! Remove provider implementation from the list (provider object is deleted)
void removeProvider( QgsSourceSelectProvider *provider );

//! Return a provider by name or nullptr if not found
QgsSourceSelectProvider *providerByName( const QString &name ) const;

//! Return a (possibly empty) list of providers by data provider's key
QList<QgsSourceSelectProvider *> providersByKey( const QString &providerKey ) const;


private:
#ifdef SIP_RUN
QgsSourceSelectProviderRegistry( const QgsSourceSelectProviderRegistry &rh );
#endif

//! available providers. this class owns the pointers
QList<QgsSourceSelectProvider *> mProviders;

};

#endif // QGSSOURCESELECTPROVIDERREGISTRY_H

0 comments on commit 4c46f64

Please sign in to comment.
You can’t perform that action at this time.