/GUI/Generic/GRadioGroups.py
Python | 79 lines | 73 code | 2 blank | 4 comment | 0 complexity | b1de5b96b21c0b25bd7d1eee019db89d MD5 | raw file
1#
2# Python GUI - Radio groups - Generic
3#
4
5from Properties import Properties, overridable_property
6from Actions import Action
7
8class RadioGroup(Properties, Action):
9 """A RadioGroup coordinates a group of RadioButtons.
10 It has a 'value' property which is equal to the value
11 of the currently selected RadioButton. It may be given
12 an action procedure to execute when its value changes.
13
14 Operations:
15 iter(group)
16 Returns an iterator over the items of the group.
17 """
18
19 value = overridable_property('value', """The value of the currently
20 selected radio button.""")
21
22 _items = None
23 _value = None
24
25 def __init__(self, items = [], **kwds):
26 Properties.__init__(self, **kwds)
27 self._items = []
28 self.add(*items)
29
30 #
31 # Operations
32 #
33
34 def __iter__(self):
35 return iter(self._items)
36
37 #
38 # Properties
39 #
40
41 def get_value(self):
42 return self._value
43
44 def set_value(self, x):
45 if self._value <> x:
46 self._value = x
47 self._value_changed()
48 self.do_action()
49
50 #
51 # Adding and removing items
52 #
53
54 def add(self, *items):
55 "Add one or more RadioButtons to this group."
56 for item in items:
57 item.group = self
58
59 def remove(self, *items):
60 "Remove one or more RadioButtons from this group."
61 for item in items:
62 item.group = None
63
64 def _add(self, item):
65 self._items.append(item)
66 self._item_added(item)
67
68 def _remove(self, item):
69 self._items.remove(item)
70 self._item_removed(item)
71
72 def _item_added(self, item):
73 raise NotImplementedError
74
75 def _item_removed(self, item):
76 raise NotImplementedError
77
78 def _value_changed(self):
79 raise NotImplementedError