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