ba0eac2dccf8c14c01d48b10baa6a596c530ac01
[drlaunch] / src / win_config.py
1 #!/usr/bin/env python
2 # coding=UTF-8
3
4 # Copyright (C) 2010 Stefanos Harhalakis
5 #
6 # This file is part of wifieye.
7 #
8 # wifieye is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # wifieye is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with wifieye.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
22
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
24
25 import gtk
26 import gobject
27 import hildon
28 import time
29
30 from hildon import StackableWindow
31 #from portrait import FremantleRotation
32 #from xdg.IconTheme import getIconPath
33
34 import config
35 import apps
36 from icon import Icon, getIcon
37 from icongrid import IconGridWidget
38 from about import DlgAbout
39 from portrait import FremantleRotation
40
41 class WinConfig(StackableWindow): #, FremantleRotation):
42     def __init__(self, config, *args):
43         StackableWindow.__init__(self)
44 #       FremantleRotation.__init__(self, "DrlaunchPlugin",
45 #           mode=FremantleRotation.AUTOMATIC)
46
47         self.config=config
48
49         self.setupUi()
50
51     def setupUi(self):
52         """
53         self.pa         Main Pannable Area
54         self.col1       A VBox for the first column
55         self.col2       A VBox for the second column
56         self.w_igw      The IGW in an alignment
57         """
58         self.set_title("DrLaunch v" + config.version)
59
60         self.pa=hildon.PannableArea()
61 #       self.add(self.pa)
62         self.pa.set_property('mov-mode', hildon.MOVEMENT_MODE_HORIZ)
63
64 #1      hbox=gtk.HBox()
65 #1      self.pa.add_with_viewport(hbox)
66
67         # Add the first column of options
68         al=gtk.Alignment(yscale=0)
69 #1      hbox.add(al)
70
71         vbox=gtk.VBox()
72         al.add(vbox)
73         self.col1=al
74
75         maxsz=self.config.getMaxSize()
76
77         # ----------------------------------------------
78         vbox.add(gtk.Label('Width:'))
79
80         hbox2=gtk.HBox()
81         vbox.add(hbox2)
82
83         self.butsSizeX=[]
84         self.butsSize=self.butsSizeX # For now
85         for i in xrange(maxsz[0]):
86             but=hildon.GtkToggleButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
87             but.set_label("%s" % (i+1,))
88             but.connect('toggled', self.slotButtonSizeX, i)
89
90             self.butsSizeX.append(but)
91
92             hbox2.add(but)
93
94         # ----------------------------------------------
95         vbox.add(gtk.Label('Height:'))
96
97         hbox2=gtk.HBox()
98         vbox.add(hbox2)
99
100         self.butsSizeY=[]
101         for i in xrange(maxsz[1]):
102             but=hildon.GtkToggleButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
103             but.set_label("%s" % (i+1,))
104             but.connect('toggled', self.slotButtonSizeY, i)
105
106             self.butsSizeY.append(but)
107
108             hbox2.add(but)
109
110         but=hildon.CheckButton(
111                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
112         but.set_label("Rotate icons individually")
113         but.connect('toggled', self.slotButtonRotateIndividually)
114         self.buttonRotateIndividually=but
115         vbox.add(but)
116
117         but=hildon.CheckButton(
118                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
119         but.set_label("Require long press")
120 #       but.connect('toggled', self.slotButtonLongpress)
121         self.buttonRequireLongpress=but
122         vbox.add(but)
123
124         but=hildon.CheckButton(
125                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
126         but.set_label("Animate rotation")
127         self.buttonAnimateRotation=but
128         vbox.add(but)
129
130         # -----------------------------------------------
131         # Second column of options
132         vbox=gtk.VBox()
133
134         al=gtk.Alignment(xalign=0, yalign=1, xscale=1)
135         al.add(vbox)
136         self.col2=al
137
138         but=hildon.CheckButton(
139                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
140         but.set_label("No background")
141         #but.connect('toggled', self.slotButtonNoBackground)
142         self.buttonNoBackground=but
143         vbox.add(but)
144
145 #1      hbox.add(al)
146         but=hildon.Button(
147                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT,
148                 hildon.BUTTON_ARRANGEMENT_VERTICAL)
149         but.set_title("About")
150         but.connect("clicked", self.slotButtonAbout)
151         vbox.add(but)
152         self.buttonAbout=but
153
154         # -----------------------------------------------
155         # Add the icongrid
156         self.igw=IconGridWidget(True, self.config, False)
157 #       self.igw.do_realize()
158 #       self.igw.setSize(config.getSize())
159         al=gtk.Alignment(xalign=0, xscale=0)
160         al.add(self.igw)
161         al.set_padding(0, 0, 20, 0)
162         self.w_igw=al
163 #1      hbox.add(al)
164
165         self.igw.connect('long-press', self.slotLongpress)
166         self.igw.connect('click', self.slotLongpress)
167
168         self.ignore_toggle=False
169
170         self.setSize(self.config.getSize())
171         self.setIndiv(self.config.getIndiv())
172         self.setLongpress(self.config.getLongpress())
173         self.setAnimate(self.config.getAnimate())
174         self.setNoBg(self.config.getNoBg())
175
176         hbox=gtk.HBox()
177         hbox.add(self.col1)
178         hbox.add(self.col2)
179         hbox.add(self.w_igw)
180         self.pa.add_with_viewport(hbox)
181
182         self.add(self.pa)
183
184 #    def setupUi(self, orientation):
185 #
186 #       self.setupUi0()
187 #
188 #       hbox=gtk.HBox()
189 #
190 #       if orientation=='l':
191 #           hbox.add(self.col1)
192 #           hbox.add(self.col2)
193 #           hbox.add(self.w_igw)
194 #       else:
195 #           vbox=gtk.VBox()
196 #           hbox.add(vbox)
197 #           vbox.add(self.col1)
198 #           vbox.add(self.col2)
199 #           hbox.add(self.w_igw)
200 #
201 #       self.pa.add_with_viewport(hbox)
202 #
203 #       if self.get_child()!=None:
204 #           self.remove(self.get_child())
205 #       self.add(self.pa)
206 #       self.pa.show_all()
207
208     def setLayoutPortrait(self):
209         print "lo: p"
210         hbox=gtk.HBox()
211         
212         vbox=gtk.VBox()
213         hbox.add(vbox)
214         self.col1.reparent(vbox)
215         self.col2.reparent(vbox)
216         self.w_igw.reparent(hbox)
217
218         r=self.pa.get_children()[0]
219         self.pa.remove(r)
220         r.destroy()
221         self.pa.add_with_viewport(hbox)
222
223         self.pa.show_all()
224
225     def setLayoutLandscape(self):
226         print "lo: l"
227         hbox=gtk.HBox()
228         
229         self.col1.reparent(hbox)
230         self.col2.reparent(hbox)
231         self.w_igw.reparent(hbox)
232
233         r=self.pa.get_children()[0]
234         self.pa.remove(r)
235         r.destroy()
236         self.pa.add_with_viewport(hbox)
237
238         self.pa.show_all()
239
240     def on_orientation_changed(self, orientation):
241         # This is disabled for now since I've not found any reliable
242         # way for supporting orientation changes (#$#%$#*&% GTK)
243         return
244
245         print "orch:", orientation
246         if orientation=='portrait':
247             self.setLayoutPortrait()
248         else:
249             self.setLayoutLandscape()
250
251     def slotLongpress(self, sender, icon):
252         self.doConfig(icon)
253
254     def slotButtonSizeX(self, sender, id):
255         if self.getIndiv():
256             old=self.getSize()
257             sz=(id+1, old[1])
258         else:
259             sz=(id+1, id+1)
260
261         self.setSize(sz)
262         
263     def slotButtonSizeY(self, sender, id):
264         old=self.getSize()
265         sz=(old[0], id+1)
266         self.setSize(sz)
267         
268     def slotButtonRotateIndividually(self, sender):
269         but=self.buttonRotateIndividually
270         self.setIndiv(but.get_active())
271
272     def slotButtonAbout(self, sender):
273         DlgAbout.present2(self)
274
275 #    def slotButtonLongpress(self, sender):
276 #       but=self.buttonRequireLongpress
277 #       self.set
278
279     def setSize(self, sz):
280         if self.ignore_toggle:
281             return
282
283         self.ignore_toggle=True
284
285         maxsz=self.config.getMaxSize()
286
287         id=sz[0]-1
288
289         for i in xrange(maxsz[0]):
290             but=self.butsSizeX[i]
291             but.set_active(i==id)
292
293         id=sz[1]-1
294
295         for i in xrange(maxsz[1]):
296             but=self.butsSizeY[i]
297             but.set_active(i==id)
298
299         self.ignore_toggle=False
300
301         self.igw.setSize(sz)
302
303         self.igw.queue_draw()
304
305     def getSize(self):
306         return(self.igw.getSize())
307
308     def getIndiv(self):
309         ret=self.buttonRotateIndividually.get_active()
310
311         return(ret)
312
313     def setIndiv(self, indiv):
314         if indiv:
315             for i in self.butsSizeY:
316                 i.set_sensitive(True)
317             for i in self.butsSizeX:
318                 i.set_sensitive(True)
319
320         else:
321             for i in self.butsSizeY:
322                 i.set_sensitive(False)
323
324             cnt=0
325             for i in self.butsSizeX:
326                 cnt+=1
327                 if cnt>4:
328                     i.set_sensitive(False)
329
330             sz=self.getSize()
331             szx=sz[0]
332             if szx>4:
333                 szx=4
334             self.setSize((szx, szx))
335
336         self.buttonRotateIndividually.set_active(indiv)
337
338     def setLongpress(self, lp):
339         self.buttonRequireLongpress.set_active(lp)
340
341     def setAnimate(self, ar):
342         self.buttonAnimateRotation.set_active(ar)
343
344     def setNoBg(self, nobg):
345         self.buttonNoBackground.set_active(nobg)
346
347     def doConfig(self, icon):
348         aps=apps.scan()
349
350         lst=[aps[x]['name'] for x in aps]
351         lst.sort()
352
353         dialog=gtk.Dialog('App select', None,
354             gtk.DIALOG_DESTROY_WITH_PARENT, buttons=())
355
356         selector=hildon.TouchSelectorEntry(text=True)
357         selector.set_column_selection_mode(
358             hildon.TOUCH_SELECTOR_SELECTION_MODE_SINGLE)
359
360         dialog.vbox.pack_start(selector, True, True, 0)
361         dialog.set_size_request(0,900)
362         dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
363         dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
364
365         selector.append_text('None')
366
367         idx=0
368         cnt=1
369         for app in lst:
370             if app==None:
371                 continue
372             selector.append_text(app)
373             if icon.name!=None and aps[icon.name]['name']==app:
374                 idx=cnt
375             cnt+=1
376
377         selector.set_active(0, idx)
378
379         dialog.show_all()
380
381         app=None
382
383         r=dialog.run()
384
385         if r==gtk.RESPONSE_OK:
386             idx2=selector.get_active(0)
387             if idx2<1:
388                 app=None
389             else:
390                 cur=lst[idx2-1]
391                 for i in aps:
392                     if aps[i]['name']==cur:
393                         app=aps[i]
394                         break
395             if app!=None:
396                 app['icon2']=getIcon(app['icon'], self.config.getIconSize())
397             else:
398                 app={
399                     'id':       None,
400                     'icon2':    None,
401                     }
402             icon.setApp(app)
403
404         dialog.destroy()
405
406     def getData(self):
407         szx=0
408         szy=0
409         for but in self.butsSizeX:
410             szx+=1
411             if but.get_active()==True:
412                 break
413
414         for but in self.butsSizeY:
415             szy+=1
416             if but.get_active()==True:
417                 break
418
419         if self.getIndiv():
420             sz=(szx, szy)
421         else:
422             sz=(szx, szx)
423
424         wapps={}
425
426         for x in xrange(sz[0]):
427             for y in xrange(sz[1]):
428                 ico=self.igw.get(x,y)
429                 k=(x,y)
430                 wapps[k]=ico.name
431
432         indiv=self.buttonRotateIndividually.get_active()
433         lp=self.buttonRequireLongpress.get_active()
434         ar=self.buttonAnimateRotation.get_active()
435         nobg=self.buttonNoBackground.get_active()
436
437         ret={
438             'size':         sz,
439             'apps':         wapps,
440             'indiv':        indiv,
441             'longpress':    lp,
442             'animate':      ar,
443             'nobg':         nobg,
444             }
445
446         return(ret)
447
448 if __name__=="__main__":
449     win=WinConfig()
450     win.connect('delete-event', gtk.main_quit)
451
452     win.show_all()
453     gtk.main()
454
455
456
457 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
458