[pygi] add GtkUIManager and GtkActionGroup overrides



commit 897420ed97cc4a7b8a806894df5e76ed72617614
Author: John (J5) Palmieri <johnp redhat com>
Date:   Wed May 12 14:25:32 2010 -0400

    add GtkUIManager and GtkActionGroup overrides
    
    * fixes bug https://bugzilla.gnome.org/show_bug.cgi?id=618476

 gi/overrides/Gtk.py     |  167 ++++++++++++++++++++++++++++++++++++++++++++++-
 tests/test_overrides.py |   45 +++++++++++++
 2 files changed, 211 insertions(+), 1 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 53a1dbe..dd8963c 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -27,6 +27,170 @@ from ..importer import modules
 
 Gtk = modules['Gtk']
 
+class ActionGroup(Gtk.ActionGroup):
+    def add_actions(self, entries):
+        """
+        The add_actions() method is a convenience method that creates a number
+        of gtk.Action  objects based on the information in the list of action
+        entry tuples contained in entries and adds them to the action group.
+        The entry tuples can vary in size from one to six items with the
+        following information:
+
+            * The name of the action. Must be specified.
+            * The stock id for the action. Optional with a default value of None
+              if a label is specified.
+            * The label for the action. This field should typically be marked
+              for translation, see the set_translation_domain() method. Optional
+              with a default value of None if a stock id is specified.
+            * The accelerator for the action, in the format understood by the
+              gtk.accelerator_parse() function. Optional with a default value of
+              None.
+            * The tooltip for the action. This field should typically be marked
+              for translation, see the set_translation_domain() method. Optional
+              with a default value of None.
+            * The callback function invoked when the action is activated.
+              Optional with a default value of None.
+
+        The "activate" signals of the actions are connected to the callbacks and
+        their accel paths are set to <Actions>/group-name/action-name.
+        """
+        try:
+            iter(entries)
+        except:
+            raise TypeError('entries must be iterable')
+
+        def _process_action(name, stock_id=None, label=None, accelerator=None, tooltip=None, callback=None):
+            action = Gtk.Action(name=name, label=label, tooltip=tooltip, stock_id=stock_id)
+            if callback is not None:
+                action.connect('activate', callback)
+
+            self.add_action_with_accel(action, accelerator)
+
+        for e in entries:
+            # using inner function above since entries can leave out optional arguments
+            _process_action(*e)
+
+    def add_toggle_actions(self, entries):
+        """
+        The add_toggle_actions() method is a convenience method that creates a
+        number of gtk.ToggleAction objects based on the information in the list
+        of action entry tuples contained in entries and adds them to the action
+        group. The toggle action entry tuples can vary in size from one to seven
+        items with the following information:
+
+            * The name of the action. Must be specified.
+            * The stock id for the action. Optional with a default value of None
+              if a label is specified.
+            * The label for the action. This field should typically be marked
+              for translation, see the set_translation_domain() method. Optional
+              with a default value of None if a stock id is specified.
+            * The accelerator for the action, in the format understood by the
+              gtk.accelerator_parse() function. Optional with a default value of
+              None.
+            * The tooltip for the action. This field should typically be marked
+              for translation, see the set_translation_domain() method. Optional
+              with a default value of None.
+            * The callback function invoked when the action is activated.
+              Optional with a default value of None.
+            * A flag indicating whether the toggle action is active. Optional
+              with a default value of False.
+
+        The "activate" signals of the actions are connected to the callbacks and
+        their accel paths are set to <Actions>/group-name/action-name.
+        """
+
+        try:
+            iter(entries)
+        except:
+            raise TypeError('entries must be iterable')
+
+        def _process_action(name, stock_id=None, label=None, accelerator=None, tooltip=None, callback=None, is_active=False):
+            action = Gtk.ToggleAction(name=name, label=label, tooltip=tooltip, stock_id=stock_id)
+            action.set_active(is_active)
+            if callback is not None:
+                action.connect('activate', callback)
+
+            self.add_action_with_accel(action, accelerator)
+
+        for e in entries:
+            # using inner function above since entries can leave out optional arguments
+            _process_action(*e)
+
+
+    def add_radio_actions(self, entries, value=None, on_change=None):
+        """
+        The add_radio_actions() method is a convenience method that creates a
+        number of gtk.RadioAction objects based on the information in the list
+        of action entry tuples contained in entries and adds them to the action
+        group. The entry tuples can vary in size from one to six items with the
+        following information:
+
+            * The name of the action. Must be specified.
+            * The stock id for the action. Optional with a default value of None
+              if a label is specified.
+            * The label for the action. This field should typically be marked
+              for translation, see the set_translation_domain() method. Optional
+              with a default value of None if a stock id is specified.
+            * The accelerator for the action, in the format understood by the
+              gtk.accelerator_parse() function. Optional with a default value of
+              None.
+            * The tooltip for the action. This field should typically be marked
+              for translation, see the set_translation_domain() method. Optional
+              with a default value of None.
+            * The value to set on the radio action. Optional with a default
+              value of 0. Should be specified in applications.
+
+        The value parameter specifies the radio action that should be set
+        active. The "changed" signal of the first radio action is connected to
+        the on_change callback (if specified and not None) and the accel paths
+        of the actions are set to <Actions>/group-name/action-name.
+        """
+        try:
+            iter(entries)
+        except:
+            raise TypeError('entries must be iterable')
+
+        first_action = None
+
+        def _process_action(group_source, name, stock_id=None, label=None, accelerator=None, tooltip=None, entry_value=0):
+            action = Gtk.RadioAction(name=name, label=label, tooltip=tooltip, stock_id=stock_id, value=entry_value)
+
+            # FIXME: join_group is a patch to Gtk+ 3.0
+            #        otherwise we can't effectively add radio actions to a
+            #        group.  Should we depend on 3.0 and error out here
+            #        or should we offer the functionality via a compat
+            #        C module?
+            if hasattr(action, 'join_group'):
+                action.join_group(group_source)
+
+            if value == entry_value:
+                action.set_active(True)
+
+            self.add_action_with_accel(action, accelerator)
+            return action
+
+        for e in entries:
+            # using inner function above since entries can leave out optional arguments
+            action = _process_action(first_action, *e)
+            if first_action is None:
+                first_action = action
+
+        if first_action is not None and on_change is not None:
+            first_action.connect('changed', on_change)
+
+ActionGroup = override(ActionGroup)
+
+class UIManager(Gtk.UIManager):
+    def add_ui_from_string(self, buffer):
+        if not isinstance(buffer, basestring):
+            raise TypeError('buffer must be a string')
+
+        length = len(buffer)
+
+        return Gtk.UIManager.add_ui_from_string(self, buffer, length)
+
+UIManager = override(UIManager)
+
 class Builder(Gtk.Builder):
 
     def connect_signals(self, obj_or_map):
@@ -60,7 +224,8 @@ class Builder(Gtk.Builder):
                                   obj_or_map);
 
 Builder = override(Builder)
-__all__ = ['Builder']
+
+__all__ = ['ActionGroup', 'Builder', 'UIManager']
 
 import sys
 
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 90a2426..7f70311 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -11,6 +11,8 @@ import sys
 sys.path.insert(0, "../")
 
 from gi.repository import Gdk
+from gi.repository import Gtk
+import gi.overrides as overrides
 
 class TestGdk(unittest.TestCase):
 
@@ -20,3 +22,46 @@ class TestGdk(unittest.TestCase):
         self.assertEquals(color.g, 200)
         self.assertEquals(color.b, 300)
 
+class TestGtk(unittest.TestCase):
+    def test_uimanager(self):
+        self.assertEquals(Gtk.UIManager, overrides.Gtk.UIManager)
+        ui = Gtk.UIManager()
+        ui.add_ui_from_string(
+"""
+<ui>
+    <menubar name="menubar1"></menubar>
+</ui>
+"""
+)
+        menubar = ui.get_widget("/menubar1")
+        self.assertEquals(type(menubar), Gtk.MenuBar) 
+        
+    def test_actiongroup(self):
+        self.assertEquals(Gtk.ActionGroup, overrides.Gtk.ActionGroup)
+        action_group = Gtk.ActionGroup (name = 'TestActionGroup')
+
+        action_group.add_actions ([
+            ('test-action1', None, 'Test Action 1',
+             None, None, None),
+            ('test-action2', Gtk.STOCK_COPY, 'Test Action 2',
+              None, None, None)])
+        action_group.add_toggle_actions([
+            ('test-toggle-action1', None, 'Test Toggle Action 1',
+             None, None, None, False),
+            ('test-toggle-action2', Gtk.STOCK_COPY, 'Test Toggle Action 2',
+              None, None, None, True)])
+        action_group.add_radio_actions([
+            ('test-radio-action1', None, 'Test Radio Action 1'),
+            ('test-radio-action2', Gtk.STOCK_COPY, 'Test Radio Action 2')], 1, None)
+        
+        expected_results = (('test-action1', Gtk.Action),
+                            ('test-action2', Gtk.Action),
+                            ('test-toggle-action1', Gtk.ToggleAction),
+                            ('test-toggle-action2', Gtk.ToggleAction),
+                            ('test-radio-action1', Gtk.RadioAction),
+                            ('test-radio-action2', Gtk.RadioAction))
+
+        for action, cmp in zip(action_group.list_actions(), expected_results):
+            a = (action.get_name(), type(action))
+            self.assertEquals(a,cmp)
+



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