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