Merge commit 'origin/bug_478_Listbox_ScrollbarMode'
[enigma2.git] / lib / python / Components / Converter / TemplatedMultiContent.py
1 from Components.Converter.StringList import StringList
2
3 class TemplatedMultiContent(StringList):
4         """Turns a python tuple list into a multi-content list which can be used in a listbox renderer."""
5         def __init__(self, args):
6                 StringList.__init__(self, args)
7                 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, RT_HALIGN_CENTER, RT_HALIGN_RIGHT, RT_VALIGN_TOP, RT_VALIGN_CENTER, RT_VALIGN_BOTTOM, RT_WRAP
8                 from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmap, MultiContentEntryPixmapAlphaTest, MultiContentEntryPixmapAlphaBlend, MultiContentTemplateColor, MultiContentEntryProgress
9                 l = locals()
10                 del l["self"] # cleanup locals a bit
11                 del l["args"]
12
13                 self.active_style = None
14                 self.template = eval(args, {}, l)
15                 assert "fonts" in self.template
16                 assert "itemHeight" in self.template
17                 assert "template" in self.template or "templates" in self.template
18                 assert "template" in self.template or "default" in self.template["templates"] # we need to have a default template
19
20                 if not "template" in self.template: # default template can be ["template"] or ["templates"]["default"]
21                         self.template["template"] = self.template["templates"]["default"][1]
22                         self.template["itemHeight"] = self.template["template"][0]
23
24         def changed(self, what):
25                 if not self.content:
26                         from enigma import eListboxPythonMultiContent
27                         self.content = eListboxPythonMultiContent()
28
29                         # also setup fonts (also given by source)
30                         index = 0
31                         for f in self.template["fonts"]:
32                                 self.content.setFont(index, f)
33                                 index += 1
34
35                 # if only template changed, don't reload list
36                 if what[0] == self.CHANGED_SPECIFIC and what[1] == "style":
37                         pass
38                 elif self.source:
39                         self.content.setList(self.source.list)
40
41                 self.setTemplate()
42                 self.downstream_elements.changed(what)
43
44         def setTemplate(self):
45                 if self.source:
46                         style = self.source.style
47
48                         if style == self.active_style:
49                                 return
50
51                         # if skin defined "templates", that means that it defines multiple styles in a dict. template should still be a default
52                         templates = self.template.get("templates")
53                         template = self.template.get("template")
54                         itemheight = self.template["itemHeight"]
55                         selectionEnabled = self.template.get("selectionEnabled", True)
56                         scrollbarMode = self.template.get("scrollbarMode", "showOnDemand")
57
58                         if templates and style and style in templates: # if we have a custom style defined in the source, and different templates in the skin, look it up
59                                 template = templates[style][1]
60                                 itemheight = templates[style][0]
61                                 if len(templates[style]) > 2:
62                                         selectionEnabled = templates[style][2]
63                                 if len(templates[style]) > 3:
64                                         scrollbarMode = templates[style][3]
65
66                         self.content.setTemplate(template)
67                         self.content.setItemHeight(itemheight)
68                         self.selectionEnabled = selectionEnabled
69                         self.scrollbarMode = scrollbarMode
70                         self.active_style = style