Skip to content
Permalink
Browse files
Add methods to return QgsProviderSublayerDetails corresponding
to QModelIndex
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent c041404 commit e685ad00b37af71791c3c31d1e8c151c4b74a71d
@@ -166,6 +166,16 @@ Sets the sublayer ``details`` to show in the model.
Returns the sublayer details shown in the model.

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

QgsProviderSublayerDetails indexToSublayer( const QModelIndex &index ) const;
%Docstring
Returns the sublayer corresponding to the given ``index``.
%End

QgsProviderSublayerModel::NonLayerItem indexToNonLayerItem( const QModelIndex &index ) const;
%Docstring
Returns the non layer item corresponding to the given ``index``.
%End

void addNonLayerItem( const QgsProviderSublayerModel::NonLayerItem &item );
@@ -56,6 +56,26 @@ QList<QgsProviderSublayerDetails> QgsProviderSublayerModel::sublayerDetails() co
return mSublayers;
}

QgsProviderSublayerDetails QgsProviderSublayerModel::indexToSublayer( const QModelIndex &index ) const
{
if ( index.isValid() && index.row() < mSublayers.count() )
{
return mSublayers.at( index.row() );
}

return QgsProviderSublayerDetails();
}

QgsProviderSublayerModel::NonLayerItem QgsProviderSublayerModel::indexToNonLayerItem( const QModelIndex &index ) const
{
if ( index.isValid() && index.row() >= mSublayers.count() && index.row() < mSublayers.count() + mNonLayerItems.count() )
{
return mNonLayerItems.at( index.row() - mSublayers.count() );
}

return QgsProviderSublayerModel::NonLayerItem();
}

void QgsProviderSublayerModel::addNonLayerItem( const QgsProviderSublayerModel::NonLayerItem &item )
{
beginInsertRows( QModelIndex(), mSublayers.count() + mNonLayerItems.count(), mSublayers.count() + mNonLayerItems.count() );
@@ -180,6 +180,16 @@ class CORE_EXPORT QgsProviderSublayerModel: public QAbstractItemModel
*/
QList< QgsProviderSublayerDetails > sublayerDetails() const;

/**
* Returns the sublayer corresponding to the given \a index.
*/
QgsProviderSublayerDetails indexToSublayer( const QModelIndex &index ) const;

/**
* Returns the non layer item corresponding to the given \a index.
*/
QgsProviderSublayerModel::NonLayerItem indexToNonLayerItem( const QModelIndex &index ) const;

/**
* Adds a non-layer item (e.g. an embedded QGIS project item) to the model.
*/
@@ -41,6 +41,10 @@ def test_model(self):
self.assertEqual(model.headerData(1, Qt.Horizontal, Qt.DisplayRole), 'Description')
self.assertEqual(model.headerData(1, Qt.Horizontal, Qt.ToolTipRole), 'Description')

# no crash, should return invalid results
self.assertFalse(model.indexToSublayer(model.index(0, 0, QModelIndex())).name())
self.assertFalse(model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())

layer1 = QgsProviderSublayerDetails()
layer1.setType(QgsMapLayerType.RasterLayer)
layer1.setName('layer 1')
@@ -58,6 +62,10 @@ def test_model(self):
self.assertEqual(model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1')
self.assertEqual(model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1')

self.assertEqual(model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1)
self.assertFalse(model.indexToSublayer(model.index(1, 0, QModelIndex())).name())
self.assertFalse(model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())

layer2 = QgsProviderSublayerDetails()
layer2.setType(QgsMapLayerType.VectorLayer)
layer2.setName('layer 2')
@@ -85,6 +93,11 @@ def test_model(self):
self.assertEqual(model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Name), 'layer 2')
self.assertEqual(model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Description), 'description 2')

self.assertEqual(model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1)
self.assertEqual(model.indexToSublayer(model.index(1, 0, QModelIndex())), layer2)
self.assertFalse(model.indexToSublayer(model.index(2, 0, QModelIndex())).name())
self.assertFalse(model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())

layer3 = QgsProviderSublayerDetails()
layer3.setType(QgsMapLayerType.VectorLayer)
layer3.setName('layer 3')
@@ -119,6 +132,12 @@ def test_model(self):
self.assertEqual(model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Name), 'layer 3')
self.assertEqual(model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Description), None)

self.assertEqual(model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1)
self.assertEqual(model.indexToSublayer(model.index(1, 0, QModelIndex())), layer2)
self.assertEqual(model.indexToSublayer(model.index(2, 0, QModelIndex())), layer3)
self.assertFalse(model.indexToSublayer(model.index(3, 0, QModelIndex())).name())
self.assertFalse(model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())

# remove a layer
model.setSublayerDetails([layer3, layer1])
self.assertEqual(model.rowCount(QModelIndex()), 2)
@@ -206,6 +225,10 @@ def test_model_with_non_layer_items(self):
self.assertEqual(model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1')
self.assertEqual(model.data(model.index(0, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False)

self.assertEqual(model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1)
self.assertFalse(model.indexToSublayer(model.index(1, 0, QModelIndex())).name())
self.assertFalse(model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())

item1 = QgsProviderSublayerModel.NonLayerItem()
item1.setUri('item uri 1')
item1.setName('item name 1')
@@ -231,6 +254,12 @@ def test_model_with_non_layer_items(self):
self.assertEqual(model.data(model.index(1, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), True)
self.assertEqual(model.data(model.index(1, 0), QgsProviderSublayerModel.Role.NonLayerItemType), 'item type 1')

self.assertEqual(model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1)
self.assertFalse(model.indexToSublayer(model.index(1, 0, QModelIndex())).name())
self.assertFalse(model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())
self.assertEqual(model.indexToNonLayerItem(model.index(1, 0, QModelIndex())), item1)
self.assertFalse(model.indexToNonLayerItem(model.index(2, 0, QModelIndex())).name())

item2 = QgsProviderSublayerModel.NonLayerItem()
item2.setUri('item uri 2')
item2.setName('item name 2')
@@ -264,6 +293,13 @@ def test_model_with_non_layer_items(self):
self.assertEqual(model.data(model.index(2, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), True)
self.assertEqual(model.data(model.index(2, 0), QgsProviderSublayerModel.Role.NonLayerItemType), 'item type 2')

self.assertEqual(model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1)
self.assertFalse(model.indexToSublayer(model.index(1, 0, QModelIndex())).name())
self.assertFalse(model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())
self.assertEqual(model.indexToNonLayerItem(model.index(1, 0, QModelIndex())), item1)
self.assertEqual(model.indexToNonLayerItem(model.index(2, 0, QModelIndex())), item2)
self.assertFalse(model.indexToNonLayerItem(model.index(3, 0, QModelIndex())).name())

def test_proxy(self):
"""
Test QgsProviderSublayerProxyModel

0 comments on commit e685ad0

Please sign in to comment.