Skip to content
Permalink
Browse files

registerProvider now returns a bool

  • Loading branch information
elpaso committed Jun 4, 2018
1 parent 0430a8d commit 6ece1aa5432d83dff3859f8288953d82fb954c10
@@ -69,6 +69,7 @@ no library is involved.
provider = reinterpret_cast<QgsDataProvider *>( sipConvertToType( sipResObj, sipType_QgsDataProvider, NULL, SIP_NOT_NONE, &state0, &sipIsErr ) );
if ( sipIsErr != 0 )
{
sipReleaseType( provider, sipType_QgsDataProvider, state0 );
provider = nullptr;
}
}
@@ -180,10 +180,12 @@ Returns a string containing the available protocol drivers

void registerGuis( QWidget *widget );

void registerProvider( QgsProviderMetadata *providerMetadata /Transfer/ );
bool registerProvider( QgsProviderMetadata *providerMetadata /Transfer/ );
%Docstring
register a new vector data provider from its ``providerMetadata``

:return: true on success, false if a provider with the same key was already registered

.. note::

ownership of the QgsProviderMetadata instance is transferred to the registry
@@ -90,6 +90,7 @@ class CORE_EXPORT QgsProviderMetadata
provider = reinterpret_cast<QgsDataProvider *>( sipConvertToType( sipResObj, sipType_QgsDataProvider, NULL, SIP_NOT_NONE, &state0, &sipIsErr ) );
if ( sipIsErr != 0 )
{
sipReleaseType( provider, sipType_QgsDataProvider, state0 );
provider = nullptr;
}
}
@@ -525,10 +525,25 @@ void QgsProviderRegistry::registerGuis( QWidget *parent )
}
}

void QgsProviderRegistry::registerProvider( QgsProviderMetadata *providerMetadata )
bool QgsProviderRegistry::registerProvider( QgsProviderMetadata *providerMetadata )
{
if ( providerMetadata )
mProviders[ providerMetadata->key() ] = providerMetadata;
{
if ( mProviders.find( providerMetadata->key() ) == mProviders.end() )
{
mProviders[ providerMetadata->key() ] = providerMetadata;
return true;
}
else
{
QgsDebugMsgLevel( QStringLiteral( "Cannot register provider metadata: a provider with the same key (%1) was already registered!" ).arg( providerMetadata->key() ), 2 );
}
}
else
{
QgsDebugMsgLevel( QStringLiteral( "Trying to register a null metadata provider!" ), 2 );
}
return false;
}

QString QgsProviderRegistry::fileVectorFilters() const
@@ -181,10 +181,11 @@ class CORE_EXPORT QgsProviderRegistry

/**
* \brief register a new vector data provider from its \a providerMetadata
* \return true on success, false if a provider with the same key was already registered
* \note ownership of the QgsProviderMetadata instance is transferred to the registry
* \since QGIS 3.2
*/
void registerProvider( QgsProviderMetadata *providerMetadata SIP_TRANSFER );
bool registerProvider( QgsProviderMetadata *providerMetadata SIP_TRANSFER );

/**
* Open the given vector data source
@@ -105,7 +105,7 @@ def setUpClass(cls):
# Register the provider
r = QgsProviderRegistry.instance()
metadata = QgsProviderMetadata(PyProvider.providerKey(), PyProvider.description(), PyProvider.createProvider)
r.registerProvider(metadata)
assert r.registerProvider(metadata)
assert r.providerMetadata(PyProvider.providerKey()) == metadata

# Create test layer
@@ -399,6 +399,12 @@ def testThreadSafetyWithIndex(self):
request = QgsFeatureRequest().setFilterRect(extent)
self.assertTrue(QgsTestUtils.testProviderIteratorThreadSafety(self.source, request))

def tesRegisterSameProviderTwice(self):
"""Test that a provider cannot be registered twice"""
r = QgsProviderRegistry.instance()
metadata = QgsProviderMetadata(PyProvider.providerKey(), PyProvider.description(), PyProvider.createProvider)
self.assertFalse(r.registerProvider(metadata))


if __name__ == '__main__':
unittest.main()

0 comments on commit 6ece1aa

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