[pygobject] Add signal emission methods to TreeModel which coerce the path argument



commit acef1d3266d11b2465d61185a55526df879a5c62
Author: Simon Feltman <sfeltman src gnome org>
Date:   Mon Dec 31 19:01:57 2012 -0800

    Add signal emission methods to TreeModel which coerce the path argument
    
    Override TreeModel row_changed, row_inserted, row_has_child_toggled,
    row_deleted, and rows_reordered methods to accept python iterables as
    the path parameter. This is for compatibility with pygtk and consistency
    with the rest of the TreeModel and TreePath overrides.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=682933

 gi/overrides/Gtk.py         |   31 ++++++++++++++++++++++++++++---
 tests/test_overrides_gtk.py |   27 +++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 3 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 9618110..337bed6 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -780,6 +780,12 @@ class TreeModel(Gtk.TreeModel):
                 raise IndexError("could not find tree path '%s'" % key)
             return aiter
 
+    def _coerce_path(self, path):
+        if isinstance(path, Gtk.TreePath):
+            return path
+        else:
+            return TreePath(path)
+
     def __getitem__(self, key):
         aiter = self._getiter(key)
         return TreeModelRow(self, aiter)
@@ -796,9 +802,7 @@ class TreeModel(Gtk.TreeModel):
         return TreeModelRowIter(self, self.get_iter_first())
 
     def get_iter(self, path):
-        if not isinstance(path, Gtk.TreePath):
-            path = TreePath(path)
-
+        path = self._coerce_path(path)
         success, aiter = super(TreeModel, self).get_iter(path)
         if not success:
             raise ValueError("invalid tree path '%s'" % path)
@@ -896,6 +900,27 @@ class TreeModel(Gtk.TreeModel):
     def filter_new(self, root=None):
         return super(TreeModel, self).filter_new(root)
 
+    #
+    # Signals supporting python iterables as tree paths
+    #
+    def row_changed(self, path, iter):
+        return super(TreeModel, self).row_changed(self._coerce_path(path), iter)
+
+    def row_inserted(self, path, iter):
+        return super(TreeModel, self).row_inserted(self._coerce_path(path), iter)
+
+    def row_has_child_toggled(self, path, iter):
+        return super(TreeModel, self).row_has_child_toggled(self._coerce_path(path),
+                                                            iter)
+
+    def row_deleted(self, path):
+        return super(TreeModel, self).row_deleted(self._coerce_path(path))
+
+    def rows_reordered(self, path, iter, new_order):
+        return super(TreeModel, self).rows_reordered(self._coerce_path(path),
+                                                     iter, new_order)
+
+
 TreeModel = override(TreeModel)
 __all__.append('TreeModel')
 
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index 7fa6fe1..3aced38 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -1317,6 +1317,33 @@ class TestTreeModel(unittest.TestCase):
         store.set_value(row, 0, None)
         self.assertSequenceEqual(store[0][:], [None])
 
+    def test_signal_emission_tree_path_coerce(self):
+        class Model(GObject.Object, Gtk.TreeModel):
+            pass
+
+        model = Model()
+        tree_paths = []
+
+        def on_any_signal(model, path, *args):
+            tree_paths.append(path.to_string())
+
+        model.connect('row-changed', on_any_signal)
+        model.connect('row-deleted', on_any_signal)
+        model.connect('row-has-child-toggled', on_any_signal)
+        model.connect('row-inserted', on_any_signal)
+
+        model.row_changed('0', Gtk.TreeIter())
+        self.assertEqual(tree_paths[-1], '0')
+
+        model.row_deleted('1')
+        self.assertEqual(tree_paths[-1], '1')
+
+        model.row_has_child_toggled('2', Gtk.TreeIter())
+        self.assertEqual(tree_paths[-1], '2')
+
+        model.row_inserted('3', Gtk.TreeIter())
+        self.assertEqual(tree_paths[-1], '3')
+
 
 @unittest.skipUnless(Gtk, 'Gtk not available')
 class TestTreeView(unittest.TestCase):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]