dabo Commit
Revision 6105
Date: 2010-10-16 08:53:50 -0700 (Sat, 16 Oct 2010)
Author: Ed
Trac: http://trac.dabodev.com/changeset/6105
Changed:
U trunk/dabo/lib/propertyHelperMixin.py
U trunk/dabo/ui/uiwx/dControlItemMixin.py
Log:
Made the Choices property of list controls dynamic, in that changes to the
value returned by that property will modify the list options of the control.
Formerly, you would have to get the Choices, modify them, and then re-apply
them to the property. Now you can modify them directly, and have the change
reflected in the control.
Diff:
Modified: trunk/dabo/lib/propertyHelperMixin.py
===================================================================
--- trunk/dabo/lib/propertyHelperMixin.py 2010-10-14 01:42:04 UTC (rev
6104)
+++ trunk/dabo/lib/propertyHelperMixin.py 2010-10-16 15:53:50 UTC (rev
6105)
@@ -333,3 +333,111 @@
getPropertyInfo = classmethod(getPropertyInfo)
+
+class _DynamicList(list):
+ """Helper class for list-based properties, such as the 'Choices'
property of
+ list-type controls. This wraps the regular Python list object, but in
addition
+ also propagates changes to the control, so that code like this:
+
+ control.Choices.append("Something")
+ - or -
+ control.Choices.sort()
+
+ will modify the choices in the control, as most users would expect.
+ """
+ def __init__(self, seq, obj, propname=None):
+ self._object = obj
+ if propname is None:
+ propname = "Choices"
+ self._setter = getattr(obj.__class__, propname).fset
+ super(_DynamicList, self).__init__(seq)
+
+
+ def _updateControl(self):
+ self._setter(self._object, self)
+
+
+ def __add__(self, val):
+ ret = super(_DynamicList, self).__add__(val)
+ self._updateControl()
+ return ret
+
+
+ def __delitem__(self, val):
+ ret = super(_DynamicList, self).__delitem__(val)
+ self._updateControl()
+ return ret
+
+
+ def __delslice__(self, i, j):
+ ret = super(_DynamicList, self).__delslice__(i, j)
+ self._updateControl()
+ return ret
+
+
+ def __iadd__(self, val):
+ ret = super(_DynamicList, self).__iadd__(val)
+ self._updateControl()
+ return ret
+
+
+ def __imul__(self, val):
+ ret = super(_DynamicList, self).__imul__(val)
+ self._updateControl()
+ return ret
+
+
+ def __setitem__(self, i, val):
+ ret = super(_DynamicList, self).__setitem__(i, val)
+ self._updateControl()
+ return ret
+
+
+ def __setslice__(self, i, j, val):
+ ret = super(_DynamicList, self).__setslice__(i, j, val)
+ self._updateControl()
+ return ret
+
+
+ def append(self, val):
+ ret = super(_DynamicList, self).append(val)
+ self._updateControl()
+ return ret
+
+
+ def extend(self, iterable):
+ ret = super(_DynamicList, self).extend(iterable)
+ self._updateControl()
+ return ret
+
+
+ def insert(self, i, val):
+ ret = super(_DynamicList, self).insert(i, val)
+ self._updateControl()
+ return ret
+
+
+ def pop(self, i=None):
+ if i is None:
+ i = len(self) - 1
+ ret = super(_DynamicList, self).pop(i)
+ self._updateControl()
+ return ret
+
+
+ def remove(self, val):
+ ret = super(_DynamicList, self).remove(val)
+ self._updateControl()
+ return ret
+
+
+ def reverse(self):
+ ret = super(_DynamicList, self).reverse()
+ self._updateControl()
+ return ret
+
+
+ def sort(self, cmp=None, key=None, reverse=False):
+ ret = super(_DynamicList, self).sort(cmp, key, reverse)
+ self._updateControl()
+ return ret
Modified: trunk/dabo/ui/uiwx/dControlItemMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dControlItemMixin.py 2010-10-14 01:42:04 UTC (rev
6104)
+++ trunk/dabo/ui/uiwx/dControlItemMixin.py 2010-10-16 15:53:50 UTC (rev
6105)
@@ -5,9 +5,11 @@
from dDataControlMixin import dDataControlMixin
from dabo.dLocalize import _
from dabo.lib.utils import ustr
+from dabo.lib.propertyHelperMixin import _DynamicList
from dabo.ui import makeDynamicProperty
+
class dControlItemMixin(dDataControlMixin):
""" This mixin class factors out the common code among all of the
controls that contain lists of items.
@@ -121,7 +123,7 @@
_choices = self._choices
except AttributeError:
_choices = self._choices = []
- return _choices
+ return _DynamicList(_choices, self)
def _setChoices(self, choices):
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message:
http://leafe.com/archives/byMID/[email protected]