Mercurial > fife-parpg
comparison engine/extensions/pychan/widgets/listbox.py @ 248:a2d5e2721489
widgets.py split up.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Thu, 26 Mar 2009 16:20:16 +0000 |
parents | |
children | 10b5f7f36dd4 |
comparison
equal
deleted
inserted
replaced
247:040387b7167f | 248:a2d5e2721489 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 from common import * | |
4 from widget import Widget | |
5 | |
6 class GenericListmodel(fife.ListModel,list): | |
7 """ | |
8 A wrapper for the exported list model to behave more like a Python list. | |
9 Don't use directly. | |
10 """ | |
11 def __init__(self,*args): | |
12 super(GenericListmodel,self).__init__() | |
13 map(self.append,args) | |
14 def clear(self): | |
15 while len(self): | |
16 self.pop() | |
17 def getNumberOfElements(self): | |
18 return len(self) | |
19 | |
20 def getElementAt(self, i): | |
21 i = max(0,min(i,len(self) - 1)) | |
22 return str(self[i]) | |
23 | |
24 class ListBox(Widget): | |
25 """ | |
26 A basic list box widget for displaying lists of strings. It makes most sense to wrap | |
27 this into a L{ScrollArea}. | |
28 | |
29 New Attributes | |
30 ============== | |
31 | |
32 - items: A List of strings. This can be treated like an ordinary python list. | |
33 but only strings are allowed. | |
34 - selected: The index of the selected item in the list. Starting from C{0} to C{len(items)-1}. | |
35 A negative value indicates, that no item is selected. | |
36 - selected_item: The selected string itself, or C{None} - if no string is selected. | |
37 | |
38 Data | |
39 ==== | |
40 The selected attribute can be read and set via L{distributeData} and L{collectData}. | |
41 The list items can be set via L{distributeInitialData}. | |
42 """ | |
43 def __init__(self,items=[],**kwargs): | |
44 self._items = GenericListmodel(*items) | |
45 self.real_widget = fife.ListBox(self._items) | |
46 super(ListBox,self).__init__(**kwargs) | |
47 | |
48 # Prepare Data collection framework | |
49 self.accepts_initial_data = True | |
50 self._realSetInitialData = self._setItems | |
51 | |
52 self.accepts_data = True | |
53 self._realSetData = self._setSelected | |
54 self._realGetData = self._getSelected | |
55 | |
56 def resizeToContent(self,recurse=True): | |
57 # We append a minimum value, so max() does not bail out, | |
58 # if no items are in the list | |
59 _item_widths = map(self.real_font.getWidth,map(str,self._items)) + [0] | |
60 max_w = max(_item_widths) | |
61 self.width = max_w | |
62 self.height = (self.real_font.getHeight() + 2) * len(self._items) | |
63 | |
64 def _getItems(self): return self._items | |
65 def _setItems(self,items): | |
66 # Note we cannot use real_widget.setListModel | |
67 # for some reason ??? | |
68 | |
69 # Also self assignment can kill you | |
70 if id(items) != id(self._items): | |
71 self._items.clear() | |
72 self._items.extend(items) | |
73 | |
74 items = property(_getItems,_setItems) | |
75 | |
76 def _getSelected(self): return self.real_widget.getSelected() | |
77 def _setSelected(self,index): self.real_widget.setSelected(index) | |
78 selected = property(_getSelected,_setSelected) | |
79 def _getSelectedItem(self): | |
80 if 0 <= self.selected < len(self._items): | |
81 return self._items[self.selected] | |
82 return None | |
83 selected_item = property(_getSelectedItem) |