dabo Commit
Revision 2527
Date: 2006-12-11 10:19:18 -0800 (Mon, 11 Dec 2006)
Author: Ed

Changed:
U   trunk/dabo/ui/uiwx/dControlItemMixin.py

Log:
Added two properties to list controls: 'Sorted' and 'SortFunction'. If you set 
Sorted=True, the Choices list is automatically sorted. It uses the SortFunction 
to control sorting; the default of None results in a default sorting by Python.


Diff:
Modified: trunk/dabo/ui/uiwx/dControlItemMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dControlItemMixin.py     2006-12-11 14:05:25 UTC (rev 
2526)
+++ trunk/dabo/ui/uiwx/dControlItemMixin.py     2006-12-11 18:19:18 UTC (rev 
2527)
@@ -13,6 +13,8 @@
                self._keys = []
                self._invertedKeys = []
                self._valueMode = "s"
+               self._sorted = False
+               self._sortFunction = None
                super(dControlItemMixin, self).__init__(*args, **kwargs)
 
                
@@ -95,6 +97,8 @@
                        oldVal = self.Value
                        self.Clear()
                        self._choices = list(choices)
+                       if self._sorted:
+                               self._choices.sort(self._sortFunction)
                        self.AppendItems(self._choices)
                        if oldVal is not None:
                                # Try to get back to the same row:
@@ -241,6 +245,35 @@
                        self._properties["PositionValue"] = value
 
        
+       def _getSorted(self):
+               return self._sorted
+
+       def _setSorted(self, val):
+               if self._constructed():
+                       if self._sorted != val:
+                               self._sorted = val
+                               if val:
+                                       # Force a re-ordering
+                                       self._setChoices(self._choices)
+               else:
+                       self._properties["Sorted"] = val
+
+
+       def _getSortFunction(self):
+               return self._sortFunction
+
+       def _setSortFunction(self, val):
+               if self._constructed():
+                       if callable(val):
+                               self._sortFunction = val
+                               # Force a re-ordering
+                               self._setChoices(self._choices)
+                       else:
+                               raise TypeError, _("SortFunction must be 
callable")
+               else:
+                       self._properties["SortFunction"] = val
+
+
        def _getStringValue(self):
                selections = self.PositionValue
                if not self._isMultiSelect():
@@ -363,6 +396,16 @@
                        -> Integer or tuple of integers. Read-write at runtime.
                        Returns the current position(s), or sets the current 
position(s).""") )
 
+       Sorted = property(_getSorted, _setSorted, None,
+                       _("""Are the items in the control automatically sorted? 
Default=False.
+                       If True, whenever the Choices property is changed, the 
resulting list
+                       will be first sorted using the SortFunction property, 
which defaults to 
+                       None, giving a default sort order.  (bool)"""))
+       
+       SortFunction = property(_getSortFunction, _setSortFunction, None,
+                       _("""Function used to sort list items when Sorted=True. 
Default=None, 
+                       which gives the default sorting  (callable or None)"""))
+       
        StringValue = property(_getStringValue, _setStringValue, None,
                        _("""Specifies the text of the selected item.
                        -> String or tuple of strings. Read-write at runtime.




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev

Reply via email to