Skip to content
Permalink
Browse files

Add some more useful roles to model

  • Loading branch information
nyalldawson committed Mar 7, 2020
1 parent 1fba225 commit 2eb4eaff86bf8e063eda83396c7402d166b1ddfa
@@ -28,6 +28,13 @@ A model containing registered connection names for a specific data provider.
%End
public:

enum Role
{
RoleConnectionName,
RoleUri,
RoleConfiguration,
};

explicit QgsProviderConnectionModel( const QString &provider, QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsProviderConnectionModel, for the specified ``provider``.
@@ -78,11 +78,13 @@ QVariant QgsProviderConnectionModel::data( const QModelIndex &index, int role )
switch ( role )
{
case Qt::DisplayRole:
case RoleConnectionName:
{
return connectionName;
}

case Qt::ToolTipRole:
case RoleUri:
{
if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
{
@@ -93,6 +95,19 @@ QVariant QgsProviderConnectionModel::data( const QModelIndex &index, int role )
return QString();
}
}

case RoleConfiguration:
{
if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
{
return connection->configuration();
}
else
{
return QVariant();
}
}

}

return QVariant();
@@ -41,6 +41,14 @@ class CORE_EXPORT QgsProviderConnectionModel : public QAbstractItemModel

public:

//! Model roles
enum Role
{
RoleConnectionName = Qt::UserRole, //!< Connection name
RoleUri, //!< Connection URI string
RoleConfiguration, //!< Connection configuration variant map
};

/**
* Constructor for QgsProviderConnectionModel, for the specified \a provider.
*
@@ -73,6 +73,9 @@ def test_model(self):
self.assertEqual(model.columnCount(), 1)
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.ToolTipRole), self.gpkg_path)
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName), 'qgis_test1')
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri), self.gpkg_path)
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleConfiguration), {})

md.saveConnection(conn, 'qgis_test1')
self.assertEqual(model.rowCount(), 1)
@@ -83,8 +86,12 @@ def test_model(self):
self.assertEqual(model.rowCount(), 2)
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test1')
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), Qt.ToolTipRole), self.gpkg_path)
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName), 'qgis_test1')
self.assertEqual(model.data(model.index(0, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri), self.gpkg_path)
self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.DisplayRole), 'qgis_test2')
self.assertEqual(model.data(model.index(1, 0, QModelIndex()), Qt.ToolTipRole), self.gpkg_path2)
self.assertEqual(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleConnectionName), 'qgis_test2')
self.assertEqual(model.data(model.index(1, 0, QModelIndex()), QgsProviderConnectionModel.RoleUri), self.gpkg_path2)

md.deleteConnection('qgis_test1')
self.assertEqual(model.rowCount(), 1)

0 comments on commit 2eb4eaf

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