Converter/ValueToPixmap.py, Renderer/Pixmap.py: use cache for LoadPixmap inside Value...
[enigma2.git] / lib / python / Components / Converter / ValueToPixmap.py
1 from Components.Converter.Converter import Converter
2 from Components.Element import cached, ElementError
3 from Tools.Directories import fileExists, SCOPE_SKIN_IMAGE, SCOPE_CURRENT_SKIN, resolveFilename
4 from Tools.LoadPixmap import LoadPixmap
5
6
7 class ValueToPixmap(Converter, object):
8         LANGUAGE_CODE = 0
9         PATH = 1
10         
11         def __init__(self, type):
12                 Converter.__init__(self, type)
13                 if type == "LanguageCode":
14                         self.type = self.LANGUAGE_CODE
15                 elif type == "Path":
16                         self.type = self.PATH
17                 else:
18                         raise ElementError("'%s' is not <LanguageCode|Path> for ValueToPixmap converter" % type)
19
20         @cached
21         def getPixmap(self):
22                 if self.source:
23                         val = self.source.text
24                         if val in (None, ""):
25                                 return None
26                 if self.type == self.PATH:
27                         return LoadPixmap(cached=True, path=val)
28                 if self.type == self.LANGUAGE_CODE:
29                         png = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "countries/" + val[3:].lower() + ".png"))
30                         if png == None:
31                                 png = LoadPixmap(cached=True, path=resolveFilename(SCOPE_SKIN_IMAGE, "countries/missing.png"))
32                         return png
33                 return None                     
34         
35         pixmap = property(getPixmap)
36
37         def changed(self, what):
38                 if what[0] != self.CHANGED_SPECIFIC or what[1] == self.type:
39                         Converter.changed(self, what)
40