enigma2 20130114 (master) -> 20130216 (master)
[enigma2.git] / usr / lib / enigma2 / 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, MultiContentEntryProgressPixmap
9                 l = locals()
10                 del l["self"] # cleanup locals a bit
11                 del l["args"]
12
13                 self.active_style = None
14                 self.active_buildfunc = None
15                 self.selectionEnabled = True
16                 self.template = eval(args, {}, l)
17
18                 assert "template" in self.template or "templates" in self.template
19                 assert "template" in self.template or "default" in self.template["templates"] # we need to have a default template
20                 assert "fonts" in self.template
21
22                 if not "template" in self.template: # default template can be ["template"] or ["templates"]["default"]
23                         self.template["template"] = self.template["templates"]["default"][1]
24                         self.template["itemHeight"] = self.template["template"][0]
25
26                 assert "itemHeight" in self.template
27
28         def changed(self, what):
29                 if not self.content:
30                         from enigma import eListboxPythonMultiContent
31                         self.content = eListboxPythonMultiContent()
32
33                         # also setup fonts (also given by source)
34                         index = 0
35                         for f in self.template["fonts"]:
36                                 self.content.setFont(index, f)
37                                 index += 1
38
39                 # if only template changed, don't reload list
40                 if what[0] == self.CHANGED_SPECIFIC and what[1] in ("style", "buildfunc", "selection_enabled"):
41                         pass
42                 elif self.source is not None:
43                         self.content.setList(self.source.list)
44
45                 self.setTemplate()
46                 self.downstream_elements.changed(what)
47
48         def setTemplate(self):
49                 if self.source is not None:
50                         buildfunc = self.source.buildfunc
51                         if buildfunc != self.active_buildfunc:
52                                 self.active_buildfunc = buildfunc
53                                 self.content.setBuildFunc(buildfunc)
54
55                         selection_enabled = self.source.selection_enabled
56                         if selection_enabled != self.selectionEnabled:
57                                 self.selectionEnabled = selection_enabled
58                                 self.content.setSelectionEnable(selection_enabled)
59
60                         style = self.source.style
61
62                         if style == self.active_style:
63                                 return
64
65                         # if skin defined "templates", that means that it defines multiple styles in a dict. template should still be a default
66                         templates = self.template.get("templates")
67                         template = self.template.get("template")
68                         itemheight = self.template["itemHeight"]
69                         selectionEnabled = self.template.get("selectionEnabled", True)
70                         scrollbarMode = self.template.get("scrollbarMode", None)
71
72                         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
73                                 template = templates[style][1]
74                                 itemheight = templates[style][0]
75                                 if len(templates[style]) > 2:
76                                         selectionEnabled = templates[style][2]
77                                 if len(templates[style]) > 3:
78                                         scrollbarMode = templates[style][3]
79
80                         self.content.setTemplate(template)
81                         self.content.setItemHeight(itemheight)
82                         self.selectionEnabled = selectionEnabled
83                         self.content.setSelectionEnable(selectionEnabled)
84                         if scrollbarMode is not None:
85                                 self.scrollbarMode = scrollbarMode
86                         self.active_style = style