using session.instantiateDialog to show the antiscrollbar, Extensionmenuentry shows...
[vuplus_dvbapp-plugin] / antiscrollbar / src / plugin.py
1 # (c) 2006 3c5x9, dream@3c5x9.de
2 # This Software is Free, use it where you want, when you want for whatever you want and modify it if you want. but don't remove my copyright!
3
4 from Screens.Screen import Screen
5 from Screens.InputBox import InputBox
6 from Screens.ChoiceBox import ChoiceBox
7 from Components.ActionMap import ActionMap
8 from Components.Label import Label
9 from Components.MenuList import MenuList
10 from Components.Input import Input
11 from Plugins.Plugin import PluginDescriptor
12 #############
13 from enigma import ePoint, eSize
14 #############
15 from ConfigParser import ConfigParser, DEFAULTSECT, DuplicateSectionError
16
17 ###############################################################################        
18 myname = "AntiScrollbar"     
19 myversion = "0.1"
20 ###############################################################################        
21 class AntiScrollMain(Screen):
22     step = 5    
23     def __init__(self, session, args = 0):
24         config = AntiScrollConfig()
25         try:
26           profil = config.getLastProfile()
27           self.size = [profil["sizex"],profil["sizey"]]
28           self.position = [profil["posx"],profil["posy"]]             
29         except Exception:
30           config.setProfile("standard",[200,200],[200,200])
31           config = AntiScrollConfig()          
32           profil = config.getProfile("standard")
33           
34         self.size = [profil["sizex"],profil["sizey"]]
35         self.position = [profil["posx"],profil["posy"]]           
36         ss  ="<screen position=\"%i,%i\" size=\"%i,%i\" title=\"%s\"  flags=\"wfNoBorder\" >" %(profil["posx"],profil["posy"],profil["sizex"],profil["sizey"],myname)
37         ss +="<widget name=\"label\" position=\"0,0\" size=\"%i,%i\"  backgroundColor=\"black\"  />" %(profil["sizex"],profil["sizey"])
38         ss +="</screen>"
39         self.skin = ss
40         self.session = session
41         Screen.__init__(self, session)
42         self.menu = args
43         self["label"] = Label()
44         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","NumberActions"], 
45             {
46              "ok":     self.go,
47              "back":     self.close,
48              "menu":     self.openmenu,
49              "down":     self.down,
50              "up":     self.up,
51              "left":    self.left,
52              "right":    self.right,
53              "2":    self.key2,
54              "8":    self.key8,
55              "4":    self.key4,
56              "6":    self.key6,
57                  }, -1)
58         
59
60     def go(self):
61       pass
62   
63     def openmenu(self):
64       self.session.open(AntiScrollMenu,callback=self.menuCallback,size=self.size,position=self.position)
65     
66     def menuCallback(self,size,position):
67       self.size = size
68       self.position = position
69       self.move(self.position[0],self.position[1])
70       self.resize(self.size[0],self.size[1])
71        
72     def key2(self):
73       self.size= [self.size[0],self.size[1]-self.step]
74       self.resize(self.size[0],self.size[1])
75     
76     def key8(self):
77       self.size= [self.size[0],self.size[1]+self.step]
78       self.resize(self.size[0],self.size[1])
79     
80     def key4(self):
81       self.size= [self.size[0]-self.step,self.size[1]]
82       self.resize(self.size[0],self.size[1])
83     
84     def key6(self):
85       self.size= [self.size[0]+self.step,self.size[1]]
86       self.resize(self.size[0],self.size[1])
87     
88     def down(self):
89       self.position = [self.position[0],self.position[1]+self.step]
90       self.move(self.position[0],self.position[1])
91     
92     def up(self):
93       self.position = [self.position[0],self.position[1]-self.step]
94       self.move(self.position[0],self.position[1])
95     
96     def left(self):
97       self.position = [self.position[0]-self.step,self.position[1]]
98       self.move(self.position[0],self.position[1])
99     
100     def right(self):
101       self.position = [self.position[0]+self.step,self.position[1]]
102       self.move(self.position[0],self.position[1])
103     
104     def move(self, x, y):
105       print "["+myname+"] moving to", str(x) + ":" + str(y)
106       self.instance.move(ePoint(x, y))
107       
108     def resize(self, w, h):
109       print "["+myname+"] resizing to", str(w) + "x" + str(h)
110       self.instance.resize(eSize(*(w, h)))
111       self["label"].instance.resize(eSize(*(w, h)))
112   
113 #############################
114 class  AntiScrollMenu(Screen):
115   def __init__(self,session,callback=None,size=None,position=None,arg=0):
116     self.session = session
117     self.callBack = callback
118     self.size= size
119     self.position = position
120     ss  ="<screen position=\"200,200\" size=\"300,200\" title=\"%s Menu\" >" % myname
121     ss +="<widget name=\"menu\" position=\"0,0\" size=\"300,150\" scrollbarMode=\"showOnDemand\" />" 
122     ss +="<widget name=\"label\" position=\"0,150\" size=\"300,50\"  font=\"Regular;18\" valign=\"center\" halign=\"center\" />"
123     ss +="</screen>"
124     self.skin = ss
125     Screen.__init__(self,session)
126     list = []
127     list.append(("load Profile",self.load))
128     list.append(("save Profile",self.save))
129     list.append(("save new Profile",self.savenew))
130     self["menu"] = MenuList(list)
131     self["label"] = Label("written by 3c5x9, V"+myversion)
132     self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","NumberActions"],
133             {
134              "ok": self.go,
135              "back": self.close,
136              }, -1)
137   def go(self):
138     selection = self["menu"].getCurrent()
139     selection[1]()
140   
141   def load(self):
142     config = AntiScrollConfig()
143     liste = []
144     for i in config.getProfiles():
145       liste.append((i,i))
146     self.session.openWithCallback(self.loadProfile,ChoiceBox,_("select Profile to load"),liste)
147     
148   def loadProfile(self,value):
149     if value is not None:
150       config = AntiScrollConfig()
151       profil = config.getProfile(value[1])
152       if profil is not False:
153         self.callBack([profil["sizex"],profil["sizey"]],[profil["posx"],profil["posy"]])
154   
155   def savenew(self):
156     self.session.openWithCallback(self.profilnameEntered,InputBox, title=_("Please enter a name for the Profile"), text="Profil", maxSize=False, type=Input.TEXT)
157     
158   def profilnameEntered(self,value):
159     if value is not None:
160       config = AntiScrollConfig()
161       config.setProfile(value,self.size,self.position)   
162   
163   def save(self):
164     config = AntiScrollConfig()
165     liste = []
166     for i in config.getProfiles():
167       liste.append((i,i))
168     self.session.openWithCallback(self.saveProfile,ChoiceBox,_("select Profile to save"),liste)
169   def saveProfile(self,value):
170     if value is not None:
171       config = AntiScrollConfig()
172       config.setProfile(value[1],self.size,self.position)
173                        
174                           
175 ##############################
176 class AntiScrollConfig:
177     configfile = "/etc/enigma2/AntiScrollbar.conf"
178     
179     def __init__(self):
180         self.configparser = ConfigParser()
181         self.configparser.read(self.configfile)
182     def setLastProfile(self,name):
183         self.configparser.set(DEFAULTSECT,"lastprofile",name)
184         self.writeConfig()
185     def getLastProfile(self):
186         last = self.configparser.get(DEFAULTSECT,"lastprofile")    
187         return self.getProfile(last)
188     def getProfiles(self):
189         profiles=[]
190         sections = self.configparser.sections()
191         for section in sections:
192           profiles.append(section)
193         return profiles
194
195     def getProfile(self,name):
196       if self.configparser.has_section(name) is True:
197         print "loading profile ",name
198         l={}
199         l["sizex"] = int(self.configparser.get(name, "size_x"))
200         l["sizey"] = int(self.configparser.get(name, "size_y"))
201         l["posx"] = int(self.configparser.get(name, "position_x"))
202         l["posy"] = int(self.configparser.get(name, "position_y"))
203         self.setLastProfile(name)
204         return l
205       else:
206         print "couldnt find profil", name
207         return False
208     def setProfile(self,name,size,position):
209         try:
210           self.configparser.add_section(name)
211           self.configparser.set(name, "size_x",size[0])
212           self.configparser.set(name, "size_y",size[1])
213           self.configparser.set(name, "position_x",position[0])
214           self.configparser.set(name, "position_y",position[1])
215           self.writeConfig()
216           return True
217         except DuplicateSectionError:
218           self.deleteProfile(name)
219           self.setProfile(name,size,position)
220                                                                                                     
221     def deleteProfile(self,name):
222         self.configparser.remove_section(name)
223         self.writeConfig()
224
225     def writeConfig(self):
226         fp = open(self.configfile,"w")
227         self.configparser.write(fp)
228         fp.close()
229                             
230     
231 activebar = None 
232 #############################
233 def showhide(session, **kwargs):
234     global activebar
235     if activebar is None:
236         activebar = session.instantiateDialog(AntiScrollMain)
237     
238     if activebar.shown:
239         activebar.hide()
240     else:
241         activebar.show()
242     print dir(activebar)
243     
244 def main(session, **kwargs):
245   session.open(AntiScrollMain)
246   
247 def Plugins(**kwargs):
248   return [PluginDescriptor(name=myname,description="overlay for scrolling bars",where = PluginDescriptor.WHERE_PLUGINMENU,fnc = main, icon="plugin.png"),
249           PluginDescriptor(name=myname+" show/hide",where = PluginDescriptor.WHERE_EXTENSIONSMENU,fnc = showhide)]