Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add signals to QgsProviderMetadata for connection created, changed an…
…d deleted
  • Loading branch information
nyalldawson committed Mar 9, 2020
1 parent 0f22920 commit 2217378
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
50 changes: 46 additions & 4 deletions python/core/auto_generated/qgsprovidermetadata.sip.in
Expand Up @@ -75,7 +75,7 @@ Returns the description for this driver.
QFlags<QgsMeshDriverMetadata::MeshDriverCapability> operator|(QgsMeshDriverMetadata::MeshDriverCapability f1, QFlags<QgsMeshDriverMetadata::MeshDriverCapability> f2);


class QgsProviderMetadata
class QgsProviderMetadata : QObject
{
%Docstring
Holds data provider key, description, and associated shared library file or function pointer information.
Expand Down Expand Up @@ -355,23 +355,27 @@ Raises a QgsProviderConnectionException if any errors are encountered.
%End


virtual QgsAbstractProviderConnection *createConnection( const QString &uri, const QVariantMap &configuration ) /Factory/;
virtual QgsAbstractProviderConnection *createConnection( const QString &uri, const QVariantMap &configuration ) throw( QgsProviderConnectionException ) /Factory/;
%Docstring
Creates a new connection from ``uri`` and ``configuration``,
the newly created connection is not automatically stored in the settings, call
saveConnection() to save it.
Ownership is transferred to the caller.

:raises :: py:class:`QgsProviderConnectionException`

.. seealso:: :py:func:`saveConnection`

.. versionadded:: 3.10
%End

virtual QgsAbstractProviderConnection *createConnection( const QString &name );
virtual QgsAbstractProviderConnection *createConnection( const QString &name ) throw( QgsProviderConnectionException );
%Docstring
Creates a new connection by loading the connection with the given ``name`` from the settings.
Ownership is transferred to the caller.

:raises :: py:class:`QgsProviderConnectionException`

.. seealso:: :py:func:`findConnection`
%End

Expand All @@ -385,14 +389,52 @@ Raises a QgsProviderConnectionException if any errors are encountered.
.. versionadded:: 3.10
%End

virtual void saveConnection( const QgsAbstractProviderConnection *connection, const QString &name );
virtual void saveConnection( const QgsAbstractProviderConnection *connection, const QString &name ) throw( QgsProviderConnectionException );
%Docstring
Stores the connection in the settings

:param connection: the connection to be stored in the settings
:param name: the name under which the connection will be stored

:raises :: py:class:`QgsProviderConnectionException`

.. versionadded:: 3.10
%End

signals:

void connectionCreated( const QString &name );
%Docstring
Emitted when a connection with the specified ``name`` is created.

.. note::

Only providers which implement the connection handling API will emit this signal.

.. versionadded:: 3.14
%End

void connectionDeleted( const QString &name );
%Docstring
Emitted when the connection with the specified ``name`` was deleted.

.. note::

Only providers which implement the connection handling API will emit this signal.

.. versionadded:: 3.14
%End

void connectionChanged( const QString &name );
%Docstring
Emitted when the connection with the specified ``name`` is changed, e.g. the settings
relating to the connection have been updated.

.. note::

Only providers which implement the connection handling API will emit this signal.

.. versionadded:: 3.14
%End

protected:
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsprovidermetadata.cpp
Expand Up @@ -227,8 +227,14 @@ void QgsProviderMetadata::saveConnection( const QgsAbstractProviderConnection *c
///@cond PRIVATE
void QgsProviderMetadata::saveConnectionProtected( const QgsAbstractProviderConnection *conn, const QString &name )
{
const bool isNewConnection = !connections().contains( name );
conn->store( name );
mProviderConnections.clear();

if ( !isNewConnection )
emit connectionChanged( name );
else
emit connectionCreated( name );
}
///@endcond

Expand Down
41 changes: 37 additions & 4 deletions src/core/qgsprovidermetadata.h
Expand Up @@ -123,8 +123,10 @@ Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMeshDriverMetadata::MeshDriverCapabilities )
* library object.
*
*/
class CORE_EXPORT QgsProviderMetadata
class CORE_EXPORT QgsProviderMetadata : public QObject
{
Q_OBJECT

public:

/**
Expand Down Expand Up @@ -401,17 +403,19 @@ class CORE_EXPORT QgsProviderMetadata
* the newly created connection is not automatically stored in the settings, call
* saveConnection() to save it.
* Ownership is transferred to the caller.
* \throws QgsProviderConnectionException
* \see saveConnection()
* \since QGIS 3.10
*/
virtual QgsAbstractProviderConnection *createConnection( const QString &uri, const QVariantMap &configuration ) SIP_FACTORY;
virtual QgsAbstractProviderConnection *createConnection( const QString &uri, const QVariantMap &configuration ) SIP_THROW( QgsProviderConnectionException ) SIP_FACTORY;

/**
* Creates a new connection by loading the connection with the given \a name from the settings.
* Ownership is transferred to the caller.
* \throws QgsProviderConnectionException
* \see findConnection()
*/
virtual QgsAbstractProviderConnection *createConnection( const QString &name );
virtual QgsAbstractProviderConnection *createConnection( const QString &name ) SIP_THROW( QgsProviderConnectionException );

/**
* Removes the connection with the given \a name from the settings.
Expand All @@ -425,9 +429,37 @@ class CORE_EXPORT QgsProviderMetadata
* Stores the connection in the settings
* \param connection the connection to be stored in the settings
* \param name the name under which the connection will be stored
* \throws QgsProviderConnectionException
* \since QGIS 3.10
*/
virtual void saveConnection( const QgsAbstractProviderConnection *connection, const QString &name );
virtual void saveConnection( const QgsAbstractProviderConnection *connection, const QString &name ) SIP_THROW( QgsProviderConnectionException );

signals:

/**
* Emitted when a connection with the specified \a name is created.
*
* \note Only providers which implement the connection handling API will emit this signal.
* \since QGIS 3.14
*/
void connectionCreated( const QString &name );

/**
* Emitted when the connection with the specified \a name was deleted.
*
* \note Only providers which implement the connection handling API will emit this signal.
* \since QGIS 3.14
*/
void connectionDeleted( const QString &name );

/**
* Emitted when the connection with the specified \a name is changed, e.g. the settings
* relating to the connection have been updated.
*
* \note Only providers which implement the connection handling API will emit this signal.
* \since QGIS 3.14
*/
void connectionChanged( const QString &name );

protected:

Expand Down Expand Up @@ -459,6 +491,7 @@ class CORE_EXPORT QgsProviderMetadata
T_provider_conn conn( name );
conn.remove( name );
mProviderConnections.clear();
emit connectionDeleted( name );
}
virtual void saveConnectionProtected( const QgsAbstractProviderConnection *connection, const QString &name );
//! Provider connections cache
Expand Down
13 changes: 13 additions & 0 deletions tests/src/python/test_qgsproviderconnection_base.py
Expand Up @@ -31,6 +31,7 @@
QgsProviderConnectionException,
)
from qgis.PyQt import QtCore
from qgis.PyQt.QtTest import QSignalSpy


class TestPyQgsProviderConnectionBase():
Expand Down Expand Up @@ -59,10 +60,20 @@ def setUp(self):
def _test_save_load(self, md, uri):
"""Common tests on connection save and load"""
conn = md.createConnection(self.uri, {})
created_spy = QSignalSpy(md.connectionCreated)
changed_spy = QSignalSpy(md.connectionChanged)
md.saveConnection(conn, 'qgis_test1')
# Check that we retrieve the new connection
self.assertTrue('qgis_test1' in md.connections().keys())
self.assertTrue('qgis_test1' in md.dbConnections().keys())
self.assertEqual(len(created_spy), 1)
self.assertEqual(len(changed_spy), 0)

# if we try to save again, the connectionChanged signal should be emitted instead of connectionCreated
md.saveConnection(conn, 'qgis_test1')
self.assertEqual(len(created_spy), 1)
self.assertEqual(len(changed_spy), 1)

return md.connections()['qgis_test1']

def _table_names(self, table_properties):
Expand Down Expand Up @@ -259,8 +270,10 @@ def _test_operations(self, md, conn):
self.assertTrue(isinstance(list(conns.values())[0], QgsAbstractDatabaseProviderConnection))

# Remove connection
spy_deleted = QSignalSpy(md.connectionDeleted)
md.deleteConnection('qgis_test1')
self.assertEqual(list(md.connections().values()), [])
self.assertEqual(len(spy_deleted), 1)

def test_errors(self):
"""Test SQL errors"""
Expand Down

0 comments on commit 2217378

Please sign in to comment.