use local listbox styles for servicelist
[enigma2.git] / lib / python / Components / ServiceList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3 from skin import parseColor
4
5 from enigma import loadPNG, eListboxServiceContent, eListbox, eServiceCenter, eServiceReference, gFont, eRect
6
7 from string import upper
8
9 from Tools.Directories import resolveFilename, SCOPE_SKIN_IMAGE
10
11 class ServiceList(HTMLComponent, GUIComponent):
12         MODE_NORMAL = 0
13         MODE_FAVOURITES = 1
14
15         def __init__(self):
16                 GUIComponent.__init__(self)
17                 self.l = eListboxServiceContent()
18
19                 pic = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "folder.png"))
20                 if pic:
21                         self.l.setPixmap(self.l.picFolder, pic)
22
23                 pic = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "marker-fs8.png"))
24                 if pic:
25                         self.l.setPixmap(self.l.picMarker, pic)
26
27                 pic = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_dvb_s-fs8.png"))
28                 if pic:
29                         self.l.setPixmap(self.l.picDVB_S, pic)
30
31                 pic = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_dvb_c-fs8.png"))
32                 if pic:
33                         self.l.setPixmap(self.l.picDVB_C, pic)
34
35                 pic = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_dvb_t-fs8.png"))
36                 if pic:
37                         self.l.setPixmap(self.l.picDVB_T, pic)
38
39                 pic = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_service_group-fs8.png"))
40                 if pic:
41                         self.l.setPixmap(self.l.picServiceGroup, pic)
42
43                 self.root = None
44                 self.mode = self.MODE_NORMAL
45                 self.onSelectionChanged = [ ]
46
47         def applySkin(self, desktop):
48                 attribs = [ ]
49                 if self.skinAttributes is not None:
50                         attribs = [ ]
51                         for (attrib, value) in self.skinAttributes:
52                                 if attrib == "foregroundColorMarked":
53                                         self.l.setColor(eListboxServiceContent.markedForeground, parseColor(value))
54                                 elif attrib == "foregroundColorMarkedSelected":
55                                         self.l.setColor(eListboxServiceContent.markedForegroundSelected, parseColor(value))
56                                 elif attrib == "backgroundColorMarked":
57                                         self.l.setColor(eListboxServiceContent.markedBackground, parseColor(value))
58                                 elif attrib == "backgroundColorMarkedSelected":
59                                         self.l.setColor(eListboxServiceContent.markedBackgroundSelected, parseColor(value))
60                                 elif attrib == "foregroundColorServiceNotAvail":
61                                         self.l.setColor(eListboxServiceContent.serviceNotAvail, parseColor(value))
62                                 else:
63                                         attribs.append((attrib, value))
64                 self.skinAttributes = attribs
65                 return GUIComponent.applySkin(self, desktop)
66
67         def connectSelChanged(self, fnc):
68                 if not fnc in self.onSelectionChanged:
69                         self.onSelectionChanged.append(fnc)
70
71         def disconnectSelChanged(self, fnc):
72                 if fnc in self.onSelectionChanged:
73                         self.onSelectionChanged.remove(fnc)
74
75         def selectionChanged(self):
76                 for x in self.onSelectionChanged:
77                         x()
78
79         def setCurrent(self, ref):
80                 self.l.setCurrent(ref)
81
82         def getCurrent(self):
83                 r = eServiceReference()
84                 self.l.getCurrent(r)
85                 return r
86
87         def atBegin(self):
88                 return self.instance.atBegin()
89
90         def atEnd(self):
91                 return self.instance.atEnd()
92
93         def moveUp(self):
94                 self.instance.moveSelection(self.instance.moveUp)
95
96         def moveDown(self):
97                 self.instance.moveSelection(self.instance.moveDown)
98
99         def moveToChar(self, char):
100                 # TODO fill with life
101                 print "Next char: "
102                 index = self.l.getNextBeginningWithChar(char)
103                 indexup = self.l.getNextBeginningWithChar(upper(char))
104                 if indexup != 0:
105                         if (index > indexup or index == 0):
106                                 index = indexup
107
108                 self.instance.moveSelectionTo(index)
109                 print "Moving to character " + str(char)
110
111         def moveToNextMarker(self):
112                 idx = self.l.getNextMarkerPos()
113                 self.instance.moveSelectionTo(idx)
114
115         def moveToPrevMarker(self):
116                 idx = self.l.getPrevMarkerPos()
117                 self.instance.moveSelectionTo(idx)
118
119         def moveToIndex(self, index):
120                 self.instance.moveSelectionTo(index)
121
122         def getCurrentIndex(self):
123                 return self.instance.getCurrentIndex()
124
125         GUI_WIDGET = eListbox
126         
127         def postWidgetCreate(self, instance):
128                 instance.setWrapAround(True)
129                 instance.setContent(self.l)
130                 instance.selectionChanged.get().append(self.selectionChanged)
131                 self.setMode(self.mode)
132
133         def getRoot(self):
134                 return self.root
135
136         def getRootServices(self):
137                 serviceHandler = eServiceCenter.getInstance()
138                 list = serviceHandler.list(self.root)
139                 dest = [ ]
140                 if list is not None:
141                         while 1:
142                                 s = list.getNext()
143                                 if s.valid():
144                                         dest.append(s.toString())
145                                 else:
146                                         break
147                 return dest
148
149         def setNumberOffset(self, offset):
150                 self.l.setNumberOffset(offset)
151
152         def setPlayableIgnoreService(self, ref):
153                 self.l.setIgnoreService(ref)
154
155         def setRoot(self, root, justSet=False):
156                 self.root = root
157                 self.l.setRoot(root, justSet)
158                 if not justSet:
159                         self.l.sort()
160                 self.selectionChanged()
161
162         def removeCurrent(self):
163                 self.l.removeCurrent()
164
165         def addService(self, service, beforeCurrent=False):
166                 self.l.addService(service, beforeCurrent)
167
168         def finishFill(self):
169                 self.l.FillFinished()
170                 self.l.sort()
171
172 # stuff for multiple marks (edit mode / later multiepg)
173         def clearMarks(self):
174                 self.l.initMarked()
175
176         def isMarked(self, ref):
177                 return self.l.isMarked(ref)
178
179         def addMarked(self, ref):
180                 self.l.addMarked(ref)
181
182         def removeMarked(self, ref):
183                 self.l.removeMarked(ref)
184
185         def getMarked(self):
186                 i = self.l
187                 i.markedQueryStart()
188                 ref = eServiceReference()
189                 marked = [ ]
190                 while i.markedQueryNext(ref) == 0:
191                         marked.append(ref.toString())
192                         ref = eServiceReference()
193                 return marked
194
195 #just for movemode.. only one marked entry..
196         def setCurrentMarked(self, state):
197                 self.l.setCurrentMarked(state)
198
199         def setMode(self, mode):
200                 self.mode = mode
201
202                 if mode == self.MODE_NORMAL:
203                         self.l.setItemHeight(28)
204                         self.l.setVisualMode(eListboxServiceContent.visModeComplex)
205                         self.l.setElementFont(self.l.celServiceName, gFont("Regular", 22))
206                         self.l.setElementPosition(self.l.celServiceName, eRect(0, 0, self.instance.size().width(), 28))
207                         self.l.setElementFont(self.l.celServiceInfo, gFont("Regular", 18))
208                 else:
209                         self.l.setItemHeight(28)
210                         self.l.setVisualMode(eListboxServiceContent.visModeComplex)
211                         self.l.setElementFont(self.l.celServiceNumber, gFont("Regular", 20))
212                         self.l.setElementPosition(self.l.celServiceNumber, eRect(0, 0, 50, 28))
213                         self.l.setElementFont(self.l.celServiceName, gFont("Regular", 22))
214                         self.l.setElementPosition(self.l.celServiceName, eRect(60, 0, self.instance.size().width()-60, 28))
215                         self.l.setElementFont(self.l.celServiceInfo, gFont("Regular", 18))