commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r8491 - in trunk: gnue-common/src/logic gnue-forms/src/uidrivers/


From: reinhard
Subject: [gnue] r8491 - in trunk: gnue-common/src/logic gnue-forms/src/uidrivers/wx26/widgets
Date: Tue, 13 Jun 2006 09:54:28 -0500 (CDT)

Author: reinhard
Date: 2006-06-13 09:54:27 -0500 (Tue, 13 Jun 2006)
New Revision: 8491

Modified:
   trunk/gnue-common/src/logic/usercode.py
   trunk/gnue-forms/src/uidrivers/wx26/widgets/menuitem.py
Log:
Implemented toggleable menu items.

issue79 in-progress


Modified: trunk/gnue-common/src/logic/usercode.py
===================================================================
--- trunk/gnue-common/src/logic/usercode.py     2006-06-12 14:07:22 UTC (rev 
8490)
+++ trunk/gnue-common/src/logic/usercode.py     2006-06-13 14:54:27 UTC (rev 
8491)
@@ -239,6 +239,15 @@
 class GCommander(GObjects.GObj):
     """
     Any object that is bound to an action.
+
+    A commander can either be linked to a single action, in which case the
+    action is executed whenever the commander is fired, or it can be linked to
+    an L{action} and an L{action_off}, in which case the commander is a toggle
+    and the action is executed when the toggle is switched on and the
+    action_off is fired when the toggle is switched off.
+
+    For toggles, the action determines icon, label, and description, rather
+    than the action_off.
     """
 
     # -------------------------------------------------------------------------
@@ -250,6 +259,8 @@
     label       = None
     description = None
     action      = None
+    action_off  = None
+    state       = False
     enabled     = True
 
 
@@ -266,6 +277,9 @@
         #: L{GAction} object linked to this commander.
         self.__action = None
 
+        #: L{GAction} object linked to the "off" operation of the commander.
+        self.__action_off = None
+
         #: Whether the UI widget is enabled or not. The UIxxx implementations
         #: read this variable on initialisation; if at some point the UI
         #: widgets are initialized with parameters, this variable can be made
@@ -274,6 +288,9 @@
 
         # Trigger support
         _triggerProperties = {
+                'state': {
+                    'get': self.__trigger_get_state,
+                    'set': self.__trigger_set_state},
                 'enabled': {
                     'get': self.__trigger_get_enabled,
                     'set': self.__trigger_set_enabled}}
@@ -287,13 +304,14 @@
 
     def __initialize(self):
 
+        # Find the next GRootObj
+        parent = self.getParent()
+        while not isinstance(parent, GRootObj.GRootObj) \
+                and parent.getParent() is not None:
+            parent = parent.getParent()
+
         # Link to action object
         if self.action is not None:
-            # Find the next GRootObj
-            parent = self.getParent()
-            while not isinstance(parent, GRootObj.GRootObj) \
-                    and parent.getParent() is not None:
-                parent = parent.getParent()
             self.__action = parent._actions[self.action]
 
             # Register ourselves to the action so we get notified of action
@@ -312,13 +330,38 @@
 
             # Set a variable to determine whether the UI widget should be
             # enabled or not.
-            self._ui_enabled = self.enabled and self.__action.enabled
+            if self.action_off is None or not self.state:
+                self._ui_enabled = self.enabled and self.__action.enabled
 
+        # Link to action_off object
+        if self.action_off is not None:
+            self.__action_off = parent._actions[self.action_off]
 
+            # Register ourselves to the action so we get notified of action
+            # enables/disables
+            self.__action_off.register_commander(self)
+
+            # Set a variable to determine whether the UI widget should be
+            # enabled or not.
+            if self.state:
+                self._ui_enabled = self.enabled and self.__action_off.enabled
+
+
     # -------------------------------------------------------------------------
     # Trigger functions
     # -------------------------------------------------------------------------
 
+    def __trigger_get_state(self):
+        return self.state
+
+    # -------------------------------------------------------------------------
+
+    def __trigger_set_state(self, value):
+        if value != self.state:
+            self.fire()
+
+    # -------------------------------------------------------------------------
+
     def __trigger_get_enabled(self):
         return self.enabled
 
@@ -342,7 +385,9 @@
         interface element.
         """
 
-        if self.__action is not None:
+        if self.state and self.__action_off is not None:
+            new_ui_enabled = self.enabled and self.__action_off.enabled
+        elif self.__action is not None:
             new_ui_enabled = self.enabled and self.__action.enabled
         else:
             new_ui_enabled = False
@@ -364,8 +409,26 @@
     # -------------------------------------------------------------------------
 
     def fire(self):
-        if self.__action is not None:
+        """
+        Fire the commander.
+
+        If the commander is bound to a single action, this action is executed.
+        
+        If the commander is bound to two actions (i.e. it is a toggle), the
+        state of the toggle is changed, and the corresponding action is
+        executed.
+        """
+
+        if self.state and self.__action_off is not None:
+            self.__action_off.run()
+            self.state = False
+            self.uiWidget.do_switch_off()
+        elif self.__action is not None:
             self.__action.run()
+            if self.__action_off is not None:
+                self.state = True
+                self.uiWidget.do_switch_on()
+        self.update_status()
 
 
 # =============================================================================
@@ -572,6 +635,20 @@
                             "item is selected"),
                         'Typecast'   : GTypecast.name,
                         'References' : 'trigger.name'},
+                    'action_off': {
+                        'Label'      : u_("Action Off"),
+                        'Description': u_(
+                            "Name of the trigger to run whenever this menu "
+                            "item is switched to off"),
+                        'Typecast'   : GTypecast.name,
+                        'References' : 'trigger.name'},
+                    'state': {
+                        'Label'      : u_("State"),
+                        'Description': u_(
+                            "Determines whether this menu item will be "
+                            "switched on by default"),
+                        'Typecast'   : GTypecast.boolean,
+                        'Default'    : False},
                     'enabled': {
                         'Label'      : u_("Enabled"),
                         'Description': u_(
@@ -621,6 +698,20 @@
                             "is clicked"),
                         'Typecast'   : GTypecast.name,
                         'References' : 'trigger.name'},
+                    'action_off': {
+                        'Label'      : u_("Action Off"),
+                        'Description': u_(
+                            "Name of the trigger to run whenever this button "
+                            "is switched to off"),
+                        'Typecast'   : GTypecast.name,
+                        'References' : 'trigger.name'},
+                    'state': {
+                        'Label'      : u_("State"),
+                        'Description': u_(
+                            "Determines whether this button will be switched "
+                            "on by default"),
+                        'Typecast'   : GTypecast.boolean,
+                        'Default'    : False},
                     'enabled': {
                         'Label'      : u_("Enabled"),
                         'Description': u_(

Modified: trunk/gnue-forms/src/uidrivers/wx26/widgets/menuitem.py
===================================================================
--- trunk/gnue-forms/src/uidrivers/wx26/widgets/menuitem.py     2006-06-12 
14:07:22 UTC (rev 8490)
+++ trunk/gnue-forms/src/uidrivers/wx26/widgets/menuitem.py     2006-06-13 
14:54:27 UTC (rev 8491)
@@ -44,9 +44,15 @@
         Creates a new MenuItem widget.
         """
 
+        if self._gfObject.action_off is not None:
+            kind = wx.ITEM_CHECK
+        else:
+            kind = wx.ITEM_NORMAL
+
         if self._gfObject.label is not None:
             widget = wx.MenuItem(event.container, wx.ID_ANY,
-                    self._gfObject.label, self._gfObject.description or u"")
+                    self._gfObject.label, self._gfObject.description or u"",
+                    kind)
     
             if self._gfObject.icon:
                 image = wx.Image(self._gfObject.icon, wx.BITMAP_TYPE_PNG)
@@ -77,6 +83,21 @@
 
 
     # -------------------------------------------------------------------------
+    # Check/uncheck menu item
+    # -------------------------------------------------------------------------
+
+    def do_switch_on(self):
+        if self.__widget is not None:
+            self.__widget.Check(True)
+
+    # -------------------------------------------------------------------------
+
+    def do_switch_off(self):
+        if self.__widget is not None:
+            self.__widget.Check(False)
+
+
+    # -------------------------------------------------------------------------
     # Enable/disable menu item
     # -------------------------------------------------------------------------
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]