Merge branch 'master' of fraxinas@git.opendreambox.org:/git/enigma2
[vuplus_dvbapp] / 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
8                 from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmap, MultiContentEntryPixmapAlphaTest
9                 l = locals()
10                 del l["self"] # cleanup locals a bit
11                 del l["args"]
12
13                 self.template = eval(args, {}, l)
14                 self.active_style = None
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"]
22
23         def changed(self, what):
24                 if not self.content:
25                         from enigma import eListboxPythonMultiContent
26                         self.content = eListboxPythonMultiContent()
27                         self.content.setItemHeight(self.template["itemHeight"])
28                         self.setTemplate()
29
30                         # also setup fonts (also given by source)
31                         index = 0
32                         for f in self.template["fonts"]:
33                                 self.content.setFont(index, f)
34                                 index += 1
35
36                 # if only template changed, don't reload list
37                 if what[0] == self.CHANGED_SPECIFIC and what[1] == "style":
38                         self.setTemplate()
39                         return
40                         
41                 if self.source:
42                         self.content.setList(self.source.list)
43                         self.setTemplate()
44
45                 self.downstream_elements.changed(what)
46
47         def setTemplate(self):
48                 if self.source:
49                         style = self.source.style
50                         if style == self.active_style:
51                                 return # style did not change
52
53                         # if skin defined "templates", that means that it defines multiple styles in a dict. template should still be a default 
54                         templates = self.template.get("templates")
55                         template = self.template.get("template")
56
57                         if templates and style: # if we have a custom style defined in the source, and different templates in the skin, look it up
58                                 template = templates.get(self.source.style, template) # default to default template
59
60                         self.content.setTemplate(template)