Commit 3f712b56 authored by Jose Rostagno's avatar Jose Rostagno Committed by Martin Pitt
Browse files

Gtk overrides: Implement __delitem__ on TreeModel

https://bugzilla.gnome.org/show_bug.cgi?id=675892

Signed-off-by: default avatarMartin Pitt <martinpitt@gnome.org>
parent 9a1a0774
......@@ -750,9 +750,9 @@ class TreeModel(Gtk.TreeModel):
# alias for Python 2.x object protocol
__nonzero__ = __bool__
def __getitem__(self, key):
def _getiter(self, key):
if isinstance(key, Gtk.TreeIter):
return TreeModelRow(self, key)
return key
elif isinstance(key, int) and key < 0:
index = len(self) + key
if index < 0:
......@@ -761,18 +761,26 @@ class TreeModel(Gtk.TreeModel):
aiter = self.get_iter(index)
except ValueError:
raise IndexError("could not find tree path '%s'" % key)
return TreeModelRow(self, aiter)
return aiter
else:
try:
aiter = self.get_iter(key)
except ValueError:
raise IndexError("could not find tree path '%s'" % key)
return TreeModelRow(self, aiter)
return aiter
def __getitem__(self, key):
aiter = self._getiter(key)
return TreeModelRow(self, aiter)
def __setitem__(self, key, value):
row = self[key]
self.set_row(row.iter, value)
def __delitem__(self, key):
aiter = self._getiter(key)
self.remove(aiter)
def __iter__(self):
return TreeModelRowIter(self, self.get_iter_first())
......
......@@ -1516,6 +1516,15 @@ class TestGtk(unittest.TestCase):
self.assertRaises(ValueError, tree_store.get, aiter, 1, 100)
self.assertEqual(tree_store.get(aiter, 0, 1), (10, 'this is row #10'))
# check __delitem__
self.assertEqual(len(tree_store), 100)
aiter = tree_store.get_iter(10)
del tree_store[aiter]
self.assertEqual(len(tree_store), 99)
self.assertRaises(TypeError, tree_store.__delitem__, None)
self.assertRaises(IndexError, tree_store.__delitem__, -101)
self.assertRaises(IndexError, tree_store.__delitem__, 101)
def test_tree_model_edit(self):
model = Gtk.ListStore(int, str, float)
model.append([1, "one", -0.1])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment