Skip to content
Permalink
Browse files
Equality operator for QgsProviderSublayerModel.NonLayerItem
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent 94b7ef9 commit c04140443b2ef3646761a13bef177717aecd8679
@@ -138,6 +138,15 @@ Sets the item's ``icon``.
.. seealso:: :py:func:`setIcon`
%End

bool operator==( const QgsProviderSublayerModel::NonLayerItem &other ) const;
bool operator!=( const QgsProviderSublayerModel::NonLayerItem &other ) const;

SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsProviderSublayerModel.NonLayerItem: %1 - %2>" ).arg( sipCpp->type(), sipCpp->name() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

};

QgsProviderSublayerModel( QObject *parent /TransferThis/ = 0 );
@@ -349,6 +349,19 @@ void QgsProviderSublayerModel::NonLayerItem::setIcon( const QIcon &icon )
mIcon = icon;
}

bool QgsProviderSublayerModel::NonLayerItem::operator==( const QgsProviderSublayerModel::NonLayerItem &other ) const
{
return mType == other.mType
&& mName == other.mName
&& mDescription == other.mDescription
&& mUri == other.mUri;
}

bool QgsProviderSublayerModel::NonLayerItem::operator!=( const QgsProviderSublayerModel::NonLayerItem &other ) const
{
return !( *this == other );
}

//
// QgsProviderSublayerProxyModel
//
@@ -140,6 +140,17 @@ class CORE_EXPORT QgsProviderSublayerModel: public QAbstractItemModel
*/
void setIcon( const QIcon &icon );

bool operator==( const QgsProviderSublayerModel::NonLayerItem &other ) const;
bool operator!=( const QgsProviderSublayerModel::NonLayerItem &other ) const;

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsProviderSublayerModel.NonLayerItem: %1 - %2>" ).arg( sipCpp->type(), sipCpp->name() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif

private:

QString mType;
@@ -149,6 +149,36 @@ def test_model(self):
self.assertEqual(model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 3')
self.assertEqual(model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), None)

def test_non_layer_item(self):
item1 = QgsProviderSublayerModel.NonLayerItem()
item1.setUri('item uri 1')
item1.setName('item name 1')
item1.setType('item type 1')
item1.setDescription('item desc 1')

item2 = QgsProviderSublayerModel.NonLayerItem(item1)
self.assertEqual(item1, item2)
self.assertFalse(item1 != item2)

item2.setUri('uu')
self.assertNotEqual(item1, item2)
self.assertTrue(item1 != item2)

item2 = QgsProviderSublayerModel.NonLayerItem(item1)
item2.setName('item name 2')
self.assertNotEqual(item1, item2)
self.assertTrue(item1 != item2)

item2 = QgsProviderSublayerModel.NonLayerItem(item1)
item2.setType('item type 2')
self.assertNotEqual(item1, item2)
self.assertTrue(item1 != item2)

item2 = QgsProviderSublayerModel.NonLayerItem(item1)
item2.setDescription('item description 2')
self.assertNotEqual(item1, item2)
self.assertTrue(item1 != item2)

def test_model_with_non_layer_items(self):
model = QgsProviderSublayerModel()
self.assertEqual(model.rowCount(QModelIndex()), 0)

0 comments on commit c041404

Please sign in to comment.