[pygobject] overrides: Make value argument to Container.child_get_property optional



commit 6f5a9a37bcdec5074332b1066396321d40b15d99
Author: Simon Feltman <sfeltman src gnome org>
Date:   Sun May 25 21:08:47 2014 -0700

    overrides: Make value argument to Container.child_get_property optional
    
    Override Gtk.Container.child_get_property to optionally accept the "value"
    argument. If "value" is not supplied, the override will locate the child
    property value type and create the GValue. Additionally return the resulting
    GValue converted to a native Python value.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=685076

 gi/overrides/Gtk.py         |   11 ++++++++++
 tests/test_overrides_gtk.py |   47 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 0 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index bd3d42a..060798b 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -110,6 +110,17 @@ class Container(Gtk.Container, Widget):
 
     get_focus_chain = strip_boolean_result(Gtk.Container.get_focus_chain)
 
+    def child_get_property(self, child, property_name, value=None):
+        if value is None:
+            prop = self.find_child_property(property_name)
+            if prop is None:
+                raise ValueError('Class "%s" does not contain child property "%s"' %
+                                 (self, property_name))
+            value = GObject.Value(prop.value_type)
+
+        Gtk.Container.child_get_property(self, child, property_name, value)
+        return value.get_value()
+
 
 Container = override(Container)
 __all__.append('Container')
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index 2a0fd90..33aeae4 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -1796,3 +1796,50 @@ class TestTextBuffer(unittest.TestCase):
                                         None)
         self.assertEqual(start.get_offset(), 6)
         self.assertEqual(end.get_offset(), 11)
+
+
+ unittest skipUnless(Gtk, 'Gtk not available')
+class TestContainer(unittest.TestCase):
+    def test_child_set_property(self):
+        box = Gtk.Box()
+        child = Gtk.Button()
+        box.pack_start(child, expand=False, fill=True, padding=0)
+
+        box.child_set_property(child, 'padding', 42)
+
+        value = GObject.Value(int)
+        box.child_get_property(child, 'padding', value)
+        self.assertEqual(value.get_int(), 42)
+
+    def test_child_get_property_gvalue(self):
+        box = Gtk.Box()
+        child = Gtk.Button()
+        box.pack_start(child, expand=False, fill=True, padding=42)
+
+        value = GObject.Value(int)
+        box.child_get_property(child, 'padding', value)
+        self.assertEqual(value.get_int(), 42)
+
+    def test_child_get_property_return_with_explicit_gvalue(self):
+        box = Gtk.Box()
+        child = Gtk.Button()
+        box.pack_start(child, expand=False, fill=True, padding=42)
+
+        value = GObject.Value(int)
+        result = box.child_get_property(child, 'padding', value)
+        self.assertEqual(result, 42)
+
+    def test_child_get_property_return_with_implicit_gvalue(self):
+        box = Gtk.Box()
+        child = Gtk.Button()
+        box.pack_start(child, expand=False, fill=True, padding=42)
+
+        result = box.child_get_property(child, 'padding')
+        self.assertEqual(result, 42)
+
+    def test_child_get_property_error(self):
+        box = Gtk.Box()
+        child = Gtk.Button()
+        box.pack_start(child, expand=False, fill=True, padding=42)
+        with self.assertRaises(ValueError):
+            box.child_get_property(child, 'not-a-valid-child-property')


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