[pygobject/pygobject-3-30] gtk: Fix rows getting inserted on the wrong level if parent=None. Fixes #281



commit 0e286990251423974c7fad0d04ad2721c9c2682e
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Fri Nov 30 01:19:33 2018 +0100

    gtk: Fix rows getting inserted on the wrong level if parent=None. Fixes #281
    
    TreeStore.insert_before allows passing parent as None in which case the parent
    is derived from the sibling. The new code using insert_with_values() didn't take
    this into account and simply passed the None along resulting in the row always getting
    added at the toplevel.
    
    Fix this by always passing a parent iter to insert_with_values() except when sibling is also
    None and we want to add to the toplevel anyway.

 gi/overrides/Gtk.py         |  4 ++++
 tests/test_overrides_gtk.py | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 0a0714d7..9cb92c0a 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -1282,6 +1282,8 @@ class TreeStore(Gtk.TreeStore, TreeModel, TreeSortable):
             if sibling is None:
                 position = -1
             else:
+                if parent is None:
+                    parent = self.iter_parent(sibling)
                 position = self.get_path(sibling).get_indices()[-1]
             return self._do_insert(parent, position, row)
 
@@ -1292,6 +1294,8 @@ class TreeStore(Gtk.TreeStore, TreeModel, TreeSortable):
             if sibling is None:
                 position = 0
             else:
+                if parent is None:
+                    parent = self.iter_parent(sibling)
                 position = self.get_path(sibling).get_indices()[-1] + 1
             return self._do_insert(parent, position, row)
 
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index c515e513..643be5c4 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -1596,6 +1596,26 @@ class TestTreeModel(unittest.TestCase):
             ([0], [-1]), ([0, 0], [0]), ([0, 1], [None]), ([0, 2], [None]),
             ([0, 3], [4321]), ([0, 4], [1234])]
 
+    def test_tree_store_insert_before_none(self):
+        store = Gtk.TreeStore(object)
+        root = store.append(None)
+        sub = store.append(root)
+
+        iter_ = store.insert_before(None, None, [1])
+        assert store.get_path(iter_).get_indices() == [1]
+
+        iter_ = store.insert_before(root, None, [1])
+        assert store.get_path(iter_).get_indices() == [0, 1]
+
+        iter_ = store.insert_before(sub, None, [1])
+        assert store.get_path(iter_).get_indices() == [0, 0, 0]
+
+        iter_ = store.insert_before(None, root, [1])
+        assert store.get_path(iter_).get_indices() == [0]
+
+        iter_ = store.insert_before(None, sub, [1])
+        assert store.get_path(iter_).get_indices() == [1, 0]
+
     def test_tree_store_insert_after(self):
         store = Gtk.TreeStore(object)
         signals = []
@@ -1657,6 +1677,26 @@ class TestTreeModel(unittest.TestCase):
             ([0], [-1]), ([0, 0], [1234]), ([0, 1], [4321]),
             ([0, 2], [None]), ([0, 3], [None]), ([0, 4], [0])]
 
+    def test_tree_store_insert_after_none(self):
+        store = Gtk.TreeStore(object)
+        root = store.append(None)
+        sub = store.append(root)
+
+        iter_ = store.insert_after(None, None, [1])
+        assert store.get_path(iter_).get_indices() == [0]
+
+        iter_ = store.insert_after(root, None, [1])
+        assert store.get_path(iter_).get_indices() == [1, 0]
+
+        iter_ = store.insert_after(sub, None, [1])
+        assert store.get_path(iter_).get_indices() == [1, 1, 0]
+
+        iter_ = store.insert_after(None, root, [1])
+        assert store.get_path(iter_).get_indices() == [2]
+
+        iter_ = store.insert_after(None, sub, [1])
+        assert store.get_path(iter_).get_indices() == [1, 2]
+
     def test_tree_path(self):
         p1 = Gtk.TreePath()
         p2 = Gtk.TreePath.new_first()


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