typo fix
[enigma2-plugins.git] / antiscrollbar / src / plugin.py
1 from Screens.Screen import Screen
2 from Screens.InputBox import InputBox
3 from Screens.ChoiceBox import ChoiceBox
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from Components.Button import Button
7 from Components.MenuList import MenuList
8 from Components.Input import Input
9 from Plugins.Plugin import PluginDescriptor
10 from enigma import iPlayableService
11 from Components.ServiceEventTracker import ServiceEventTracker
12 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSubList, ConfigInteger, ConfigYesNo, ConfigText, ConfigSelection
13 from Components.ConfigList import ConfigListScreen
14 #############
15 from enigma import ePoint, eSize
16
17
18 ###############################################################################
19 myname = "AntiScrollbar"
20
21 config.plugins.antiscrollbar = ConfigSubsection()
22 config.plugins.antiscrollbar.autostart = ConfigYesNo(default=True)
23 config.plugins.antiscrollbar.modescount = ConfigInteger(0)
24 config.plugins.antiscrollbar.mode = ConfigSubList()
25
26 def initConfig():
27     modescount = config.plugins.antiscrollbar.modescount.value
28     if modescount == 0:
29         pass
30     else:
31         i = 0
32         while i < modescount:
33             print "["+myname+".initConfig] i is %s" % i
34             config.plugins.antiscrollbar.mode.append(ConfigSubsection())
35             config.plugins.antiscrollbar.mode[i].sref = ConfigText("")
36             config.plugins.antiscrollbar.mode[i].sizex = ConfigInteger(0)
37             config.plugins.antiscrollbar.mode[i].sizey = ConfigInteger(0)
38             config.plugins.antiscrollbar.mode[i].posx = ConfigInteger(0)
39             config.plugins.antiscrollbar.mode[i].posy = ConfigInteger(0)
40             config.plugins.antiscrollbar.mode[i].enabled = ConfigYesNo(default=True)
41
42             config.plugins.antiscrollbar.modescount.value = i + 1
43
44             i += 1
45
46 initConfig()
47
48 def readConfig():
49     srefs= {}
50     for mode in config.plugins.antiscrollbar.mode:
51         sref = mode.sref.value
52         sizex = mode.sizex.value
53         sizey = mode.sizey.value
54         posx = mode.posx.value
55         posy = mode.posy.value
56         enabled = mode.enabled.value
57         srefs[sref]=[sizex,sizey,posx,posy,enabled]
58     return srefs
59
60 ###############################################################################
61 class AntiScrollOverlay(Screen):
62     def __init__(self, session):
63         self.size = [0,0]
64         self.position = [0,0]
65         ss  ="<screen position=\"0,0\" size=\"0,0\" title=\"AntiScrollOverlay\"  flags=\"wfNoBorder\" zPosition=\"-1\" backgroundColor=\"#FF000000\">"
66         ss +="<widget name=\"label\" position=\"1,1\" size=\"0,0\"  backgroundColor=\"#00000000\" />"
67         ss +="</screen>"
68         self.skin = ss
69         self.session = session
70         Screen.__init__(self, session)
71         self["label"] = Label()
72         self.__event_tracker = ServiceEventTracker(screen=self,eventmap=
73             {
74                 iPlayableService.evUpdatedInfo: self.evStart,
75                 iPlayableService.evEOF: self.hide,
76             })
77         self.hide()
78
79     def evStart(self):
80         service = self.session.nav.getCurrentlyPlayingServiceReference()
81         if service is None:
82             return
83         srefs = readConfig()
84         if service.toString() in srefs:
85             data = srefs[service.toString()]
86             if data[4]:
87                 self.resize(data[0],data[1])
88                 self.move(data[2],data[3])
89                 self.show()
90             else:
91                 self.hide()
92         else:
93             self.hide()
94
95     def move(self, x, y):
96       self.instance.move(ePoint(x, y))
97
98     def resize(self, w, h):
99       self.instance.resize(eSize(*(w, h)))
100       self["label"].instance.resize(eSize(*(w-2, h-2)))
101
102
103 #############################
104 class AntiScrollConfig(ConfigListScreen, Screen):
105     skin = """
106         <screen position="100,100" size="550,400" title="%s">
107             <widget name="config" position="5,5" size="540,360" scrollbarMode="showOnDemand" zPosition="1"/>
108
109             <widget name="key_red" position="0,360" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
110             <widget name="key_green" position="140,360" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
111             <widget name="key_yellow" position="280,360" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
112
113             <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,360" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
114             <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,360" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
115             <ePixmap name="yellow" pixmap="skin_default/buttons/yellow.png" position="280,360" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
116         </screen>""" % _(myname+": Main Setup")
117
118     def __init__(self, session, args=0):
119         Screen.__init__(self, session)
120         l = [
121             getConfigListEntry(_("Start on Sessionstart"), config.plugins.antiscrollbar.autostart),
122         ]
123
124         ConfigListScreen.__init__(self, l)
125         self["key_red"] = Button(_("Cancel"))
126         self["key_green"] = Button(_("OK"))
127         self["key_yellow"] = Button(_("current Service"))
128         self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
129         {
130             "red": self.cancel,
131             "green": self.save,
132             "yellow": self.openCurrentSeviceConfig,
133             "save": self.save,
134             "cancel": self.cancel,
135             "ok": self.save,
136         }, -2)
137
138     def openCurrentSeviceConfig(self):
139         print "yellow"
140         smode = False
141         for mode in config.plugins.antiscrollbar.mode:
142             if mode.sref.value == self.session.nav.getCurrentlyPlayingServiceReference().toString():
143                 smode = mode
144         if smode is False:
145             print "new config "*40
146             i = config.plugins.antiscrollbar.modescount.value
147             config.plugins.antiscrollbar.mode.append(ConfigSubsection())
148             config.plugins.antiscrollbar.mode[i].sref = ConfigText("")
149             config.plugins.antiscrollbar.mode[i].sizex = ConfigInteger(200)
150             config.plugins.antiscrollbar.mode[i].sizey = ConfigInteger(200)
151             config.plugins.antiscrollbar.mode[i].posx = ConfigInteger(100)
152             config.plugins.antiscrollbar.mode[i].posy = ConfigInteger(50)
153             config.plugins.antiscrollbar.mode[i].enabled = ConfigYesNo(default=True)
154
155             config.plugins.antiscrollbar.mode[i].save()
156             config.plugins.antiscrollbar.mode[i].sref.value = self.session.nav.getCurrentlyPlayingServiceReference().toString()
157             config.plugins.antiscrollbar.mode[i].save()
158
159             config.plugins.antiscrollbar.modescount.value +=1
160             config.plugins.antiscrollbar.modescount.save()
161             smode = config.plugins.antiscrollbar.mode[i]
162         self.session.open(CurrentSeviceConfig,smode)
163
164     def save(self):
165         print "saving"
166         for x in self["config"].list:
167             x[1].save()
168         self.close(True, self.session)
169
170     def cancel(self):
171         print "cancel"
172         for x in self["config"].list:
173             x[1].cancel()
174         self.close(False, self.session)
175
176 class CurrentSeviceConfig(Screen):
177     step = 5
178     def __init__(self, session, mode):
179         print "editing "+mode.sref.value
180         self.mode = mode
181         self.size = [mode.sizex.value,mode.sizey.value]
182         self.enabled = mode.enabled.value
183         self.position = [mode.posx.value,mode.posy.value]
184         ss  ="<screen position=\"%i,%i\" size=\"%i,%i\" title=\"%s\"  flags=\"wfNoBorder\" >" %(mode.posx.value,mode.posy.value,mode.sizex.value,mode.sizey.value,myname)
185         ss +="<widget name=\"label\" position=\"0,0\" size=\"%i,%i\"  backgroundColor=\"black\"  />" %(mode.sizex.value,mode.sizey.value)
186         ss +="</screen>"
187         self.skin = ss
188         self.session = session
189         Screen.__init__(self, session)
190
191         self["label"] = Label()
192         if self.enabled is not True:
193             self["label"].setText("disabled")
194         else:
195             self["label"].setText("")
196
197         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","NumberActions"],
198             {
199              "ok":     self.go,
200              "back":     self.cancel,
201              "down":     self.down,
202              "up":     self.up,
203              "left":    self.left,
204              "right":    self.right,
205              "2":    self.key2,
206              "8":    self.key8,
207              "4":    self.key4,
208              "6":    self.key6,
209              "0":    self.key0,
210                  }, -1)
211
212     def go(self):
213       self.mode.posx.value = self.position[0]
214       self.mode.posy.value = self.position[1]
215       self.mode.sizex.value = self.size[0]
216       self.mode.sizey.value = self.size[1]
217       self.mode.enabled.value = self.enabled
218       self.mode.save()
219       self.close()
220
221     def cancel(self):
222        self.close()
223
224     def key0(self):
225         if self.enabled:
226             self.enabled = False
227             self["label"].setText("disabled")
228         else:
229             self.enabled = True
230             self["label"].setText("")
231
232     def key2(self):
233       self.size= [self.size[0],self.size[1]-self.step]
234       self.resize(self.size[0],self.size[1])
235
236     def key8(self):
237       self.size= [self.size[0],self.size[1]+self.step]
238       self.resize(self.size[0],self.size[1])
239
240     def key4(self):
241       self.size= [self.size[0]-self.step,self.size[1]]
242       self.resize(self.size[0],self.size[1])
243
244     def key6(self):
245       self.size= [self.size[0]+self.step,self.size[1]]
246       self.resize(self.size[0],self.size[1])
247
248     def down(self):
249       self.position = [self.position[0],self.position[1]+self.step]
250       self.move(self.position[0],self.position[1])
251
252     def up(self):
253       self.position = [self.position[0],self.position[1]-self.step]
254       self.move(self.position[0],self.position[1])
255
256     def left(self):
257       self.position = [self.position[0]-self.step,self.position[1]]
258       self.move(self.position[0],self.position[1])
259
260     def right(self):
261       self.position = [self.position[0]+self.step,self.position[1]]
262       self.move(self.position[0],self.position[1])
263
264     def move(self, x, y):
265       self.instance.move(ePoint(x, y))
266
267
268     def resize(self, w, h):
269       self.instance.resize(eSize(*(w, h)))
270       self["label"].instance.resize(eSize(*(w, h)))
271
272 #############################
273
274 activebar = None
275
276
277 def main(session, **kwargs):
278     global activebar
279     if activebar.show:
280         activebar.hide()
281     session.openWithCallback(mainCB,AntiScrollConfig)
282
283 def mainCB(saved,session):
284     global activebar,doshow
285     activebar.evStart()
286
287 def autostart(session, **kwargs):
288     global activebar
289     if config.plugins.antiscrollbar.autostart.value:
290         activebar = session.instantiateDialog(AntiScrollOverlay)
291
292 def Plugins(**kwargs):
293   return [PluginDescriptor(name=myname,description="overlay for scrolling bars",where = PluginDescriptor.WHERE_PLUGINMENU,fnc = main, icon="plugin.png"),
294           PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART,fnc = autostart)]