Avoid problems with orientation
[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=1)
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         vbox2=gtk.VBox()
111
112         al=gtk.Alignment(xalign=0, yalign=1, xscale=1)
113         al.add(vbox2)
114         self.col11=al
115
116         vbox.add(al)
117         vbox=vbox2
118
119         but=hildon.CheckButton(
120                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
121         but.set_label("Rotate icons individually")
122         but.connect('toggled', self.slotButtonRotateIndividually)
123         self.buttonRotateIndividually=but
124         vbox.add(but)
125
126         but=hildon.CheckButton(
127                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
128         but.set_label("Require long press")
129 #       but.connect('toggled', self.slotButtonLongpress)
130         self.buttonRequireLongpress=but
131         vbox.add(but)
132
133         but=hildon.CheckButton(
134                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
135         but.set_label("Animate rotation")
136         self.buttonAnimateRotation=but
137         vbox.add(but)
138
139         # -----------------------------------------------
140         # Second column of options
141         vbox=gtk.VBox()
142
143         al=gtk.Alignment(xalign=0, yalign=1, xscale=1)
144         al.add(vbox)
145         self.col2=al
146
147         but=hildon.CheckButton(
148                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
149         but.set_label("Theme background")
150         #but.connect('toggled', self.slotButtonNoBackground)
151         self.buttonThemeBackground=but
152         vbox.add(but)
153
154         but=hildon.CheckButton(
155                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
156         but.set_label("No background")
157         #but.connect('toggled', self.slotButtonNoBackground)
158         self.buttonNoBackground=but
159         vbox.add(but)
160
161 #1      hbox.add(al)
162         but=hildon.Button(
163                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT,
164                 hildon.BUTTON_ARRANGEMENT_VERTICAL)
165         but.set_title("About")
166         but.connect("clicked", self.slotButtonAbout)
167         vbox.add(but)
168         self.buttonAbout=but
169
170         # -----------------------------------------------
171         # Add the icongrid
172         self.igw=IconGridWidget(True, self.config, False)
173 #       self.igw.do_realize()
174 #       self.igw.setSize(config.getSize())
175         al=gtk.Alignment(xalign=0, xscale=0)
176         al.add(self.igw)
177         al.set_padding(0, 0, 20, 0)
178         self.w_igw=al
179 #1      hbox.add(al)
180
181         self.igw.connect('long-press', self.slotLongpress)
182         self.igw.connect('click', self.slotLongpress)
183
184         self.ignore_toggle=False
185
186         self.setSize(self.config.getSize())
187         self.setIndiv(self.config.getIndiv())
188         self.setLongpress(self.config.getLongpress())
189         self.setAnimate(self.config.getAnimate())
190         self.setNoBg(self.config.getNoBg())
191         self.setThemeBg(self.config.getThemeBg())
192
193         hbox=gtk.HBox()
194         hbox.add(self.col1)
195         hbox.add(self.col2)
196         hbox.add(self.w_igw)
197         self.pa.add_with_viewport(hbox)
198
199         self.add(self.pa)
200
201 #    def setupUi(self, orientation):
202 #
203 #       self.setupUi0()
204 #
205 #       hbox=gtk.HBox()
206 #
207 #       if orientation=='l':
208 #           hbox.add(self.col1)
209 #           hbox.add(self.col2)
210 #           hbox.add(self.w_igw)
211 #       else:
212 #           vbox=gtk.VBox()
213 #           hbox.add(vbox)
214 #           vbox.add(self.col1)
215 #           vbox.add(self.col2)
216 #           hbox.add(self.w_igw)
217 #
218 #       self.pa.add_with_viewport(hbox)
219 #
220 #       if self.get_child()!=None:
221 #           self.remove(self.get_child())
222 #       self.add(self.pa)
223 #       self.pa.show_all()
224
225     def setLayoutPortrait(self):
226         print "lo: p"
227         hbox=gtk.HBox()
228         
229         vbox=gtk.VBox()
230         hbox.add(vbox)
231         self.col1.reparent(vbox)
232         self.col2.reparent(vbox)
233         self.w_igw.reparent(hbox)
234
235         r=self.pa.get_children()[0]
236         self.pa.remove(r)
237         r.destroy()
238         self.pa.add_with_viewport(hbox)
239
240         self.pa.show_all()
241
242     def setLayoutLandscape(self):
243         print "lo: l"
244         hbox=gtk.HBox()
245         
246         self.col1.reparent(hbox)
247         self.col2.reparent(hbox)
248         self.w_igw.reparent(hbox)
249
250         r=self.pa.get_children()[0]
251         self.pa.remove(r)
252         r.destroy()
253         self.pa.add_with_viewport(hbox)
254
255         self.pa.show_all()
256
257     def on_orientation_changed(self, orientation):
258         # This is disabled for now since I've not found any reliable
259         # way for supporting orientation changes (#$#%$#*&% GTK)
260         return
261
262         print "orch:", orientation
263         if orientation=='portrait':
264             self.setLayoutPortrait()
265         else:
266             self.setLayoutLandscape()
267
268     def slotLongpress(self, sender, icon):
269         self.doConfig(icon)
270
271     def slotButtonSizeX(self, sender, id):
272         if self.getIndiv():
273             old=self.getSize()
274             sz=(id+1, old[1])
275         else:
276             sz=(id+1, id+1)
277
278         self.setSize(sz)
279         
280     def slotButtonSizeY(self, sender, id):
281         old=self.getSize()
282         sz=(old[0], id+1)
283         self.setSize(sz)
284         
285     def slotButtonRotateIndividually(self, sender):
286         but=self.buttonRotateIndividually
287         self.setIndiv(but.get_active())
288
289     def slotButtonAbout(self, sender):
290         DlgAbout.present2(self)
291
292 #    def slotButtonLongpress(self, sender):
293 #       but=self.buttonRequireLongpress
294 #       self.set
295
296     def setSize(self, sz):
297         if self.ignore_toggle:
298             return
299
300         self.ignore_toggle=True
301
302         maxsz=self.config.getMaxSize()
303
304         id=sz[0]-1
305
306         for i in xrange(maxsz[0]):
307             but=self.butsSizeX[i]
308             but.set_active(i==id)
309
310         id=sz[1]-1
311
312         for i in xrange(maxsz[1]):
313             but=self.butsSizeY[i]
314             but.set_active(i==id)
315
316         self.ignore_toggle=False
317
318         self.igw.setSize(sz)
319
320         self.igw.queue_draw()
321
322     def getSize(self):
323         return(self.igw.getSize())
324
325     def getIndiv(self):
326         ret=self.buttonRotateIndividually.get_active()
327
328         return(ret)
329
330     def setIndiv(self, indiv):
331         if indiv:
332             for i in self.butsSizeY:
333                 i.set_sensitive(True)
334             for i in self.butsSizeX:
335                 i.set_sensitive(True)
336
337         else:
338             for i in self.butsSizeY:
339                 i.set_sensitive(False)
340
341             cnt=0
342             for i in self.butsSizeX:
343                 cnt+=1
344                 if cnt>4:
345                     i.set_sensitive(False)
346
347             sz=self.getSize()
348             szx=sz[0]
349             if szx>4:
350                 szx=4
351             self.setSize((szx, szx))
352
353         self.buttonRotateIndividually.set_active(indiv)
354
355     def setLongpress(self, lp):
356         self.buttonRequireLongpress.set_active(lp)
357
358     def setAnimate(self, ar):
359         self.buttonAnimateRotation.set_active(ar)
360
361     def setNoBg(self, nobg):
362         self.buttonNoBackground.set_active(nobg)
363
364     def setThemeBg(self, themebg):
365         self.buttonThemeBackground.set_active(themebg)
366
367     def doConfig(self, icon):
368         aps=apps.scan()
369
370         lst=[aps[x]['name'] for x in aps]
371         lst.sort()
372
373         dialog=gtk.Dialog('App select', None,
374             gtk.DIALOG_DESTROY_WITH_PARENT, buttons=())
375
376         selector=hildon.TouchSelectorEntry(text=True)
377         selector.set_column_selection_mode(
378             hildon.TOUCH_SELECTOR_SELECTION_MODE_SINGLE)
379
380         dialog.vbox.pack_start(selector, True, True, 0)
381         dialog.set_size_request(0,900)
382         dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
383         dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
384
385         selector.append_text('None')
386
387         idx=0
388         cnt=1
389         for app in lst:
390             if app==None:
391                 continue
392             selector.append_text(app)
393             if icon.name!=None and aps[icon.name]['name']==app:
394                 idx=cnt
395             cnt+=1
396
397         selector.set_active(0, idx)
398
399         dialog.show_all()
400
401         app=None
402
403         r=dialog.run()
404
405         if r==gtk.RESPONSE_OK:
406             idx2=selector.get_active(0)
407             if idx2<1:
408                 app=None
409             else:
410                 cur=lst[idx2-1]
411                 for i in aps:
412                     if aps[i]['name']==cur:
413                         app=aps[i]
414                         break
415             if app!=None:
416                 app['icon2']=getIcon(app['icon'], self.config.getIconSize())
417             else:
418                 app={
419                     'id':       None,
420                     'icon2':    None,
421                     }
422             icon.setApp(app)
423
424         dialog.destroy()
425
426     def getData(self):
427         szx=0
428         szy=0
429         for but in self.butsSizeX:
430             szx+=1
431             if but.get_active()==True:
432                 break
433
434         for but in self.butsSizeY:
435             szy+=1
436             if but.get_active()==True:
437                 break
438
439         if self.getIndiv():
440             sz=(szx, szy)
441         else:
442             sz=(szx, szx)
443
444         wapps={}
445
446         for x in xrange(sz[0]):
447             for y in xrange(sz[1]):
448                 ico=self.igw.get(x,y)
449                 k=(x,y)
450                 wapps[k]=ico.name
451
452         indiv=self.buttonRotateIndividually.get_active()
453         lp=self.buttonRequireLongpress.get_active()
454         ar=self.buttonAnimateRotation.get_active()
455         nobg=self.buttonNoBackground.get_active()
456         themebg=self.buttonThemeBackground.get_active()
457
458         ret={
459             'size':         sz,
460             'apps':         wapps,
461             'indiv':        indiv,
462             'longpress':    lp,
463             'animate':      ar,
464             'nobg':         nobg,
465             'themebg':      themebg,
466             }
467
468         return(ret)
469
470 if __name__=="__main__":
471     win=WinConfig()
472     win.connect('delete-event', gtk.main_quit)
473
474     win.show_all()
475     gtk.main()
476
477
478
479 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
480