[pygobject] overrides: Make value argument to Widget.style_get_property optional



commit 45a5fb2b0d6c7f46d355c83c73d829532e5a72ce
Author: Simon Feltman <sfeltman src gnome org>
Date:   Sun May 25 22:07:07 2014 -0700

    overrides: Make value argument to Widget.style_get_property optional
    
    Override Gtk.Widget.style_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 |   29 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 060798b..2a55040 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -86,6 +86,17 @@ class Widget(Gtk.Widget):
             target_list = Gtk.TargetList.new(_construct_target_list(target_list))
         super(Widget, self).drag_source_set_target_list(target_list)
 
+    def style_get_property(self, property_name, value=None):
+        if value is None:
+            prop = self.find_style_property(property_name)
+            if prop is None:
+                raise ValueError('Class "%s" does not contain style property "%s"' %
+                                 (self, property_name))
+            value = GObject.Value(prop.value_type)
+
+        Gtk.Widget.style_get_property(self, property_name, value)
+        return value.get_value()
+
 
 Widget = override(Widget)
 __all__.append('Widget')
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index 33aeae4..b7fc5ee 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -632,6 +632,35 @@ class TestGtk(unittest.TestCase):
 
 
 @unittest.skipUnless(Gtk, 'Gtk not available')
+class TestWidget(unittest.TestCase):
+    def test_style_get_property_gvalue(self):
+        button = Gtk.Button()
+        value = GObject.Value(int, -42)
+        button.style_get_property('focus-padding', value)
+        # Test only that the style property changed since we can't actuall
+        # set it.
+        self.assertNotEqual(value.get_int(), -42)
+
+    def test_style_get_property_return_with_explicit_gvalue(self):
+        button = Gtk.Button()
+        value = GObject.Value(int, -42)
+        result = button.style_get_property('focus-padding', value)
+        self.assertIsInstance(result, int)
+        self.assertNotEqual(result, -42)
+
+    def test_style_get_property_return_with_implicit_gvalue(self):
+        button = Gtk.Button()
+        result = button.style_get_property('focus-padding')
+        self.assertIsInstance(result, int)
+        self.assertNotEqual(result, -42)
+
+    def test_style_get_property_error(self):
+        button = Gtk.Button()
+        with self.assertRaises(ValueError):
+            button.style_get_property('not-a-valid-style-property')
+
+
+ unittest skipUnless(Gtk, 'Gtk not available')
 class TestSignals(unittest.TestCase):
     def test_class_closure_override_with_aliased_type(self):
         class WindowWithSizeAllocOverride(Gtk.ScrolledWindow):


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