Restore portrait.py's abilities to rotate windows.
[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         print "about"
264         #dlg=DlgAbout()
265         #dlg.run()
266         DlgAbout.present2(self)
267
268 #    def slotButtonLongpress(self, sender):
269 #       but=self.buttonRequireLongpress
270 #       self.set
271
272     def setSize(self, sz):
273         if self.ignore_toggle:
274             return
275
276         self.ignore_toggle=True
277
278         maxsz=self.config.getMaxSize()
279
280         id=sz[0]-1
281
282         for i in xrange(maxsz[0]):
283             but=self.butsSizeX[i]
284             but.set_active(i==id)
285
286         id=sz[1]-1
287
288         for i in xrange(maxsz[1]):
289             but=self.butsSizeY[i]
290             but.set_active(i==id)
291
292         self.ignore_toggle=False
293
294         self.igw.setSize(sz)
295
296         self.igw.queue_draw()
297
298     def getSize(self):
299         return(self.igw.getSize())
300
301     def getIndiv(self):
302         ret=self.buttonRotateIndividually.get_active()
303
304         return(ret)
305
306     def setIndiv(self, indiv):
307         if indiv:
308             for i in self.butsSizeY:
309                 i.set_sensitive(True)
310             for i in self.butsSizeX:
311                 i.set_sensitive(True)
312
313         else:
314             for i in self.butsSizeY:
315                 i.set_sensitive(False)
316
317             cnt=0
318             for i in self.butsSizeX:
319                 cnt+=1
320                 if cnt>4:
321                     i.set_sensitive(False)
322
323             sz=self.getSize()
324             szx=sz[0]
325             if szx>4:
326                 szx=4
327             self.setSize((szx, szx))
328
329         self.buttonRotateIndividually.set_active(indiv)
330
331     def setLongpress(self, lp):
332         self.buttonRequireLongpress.set_active(lp)
333
334     def setAnimate(self, ar):
335         self.buttonAnimateRotation.set_active(ar)
336
337     def doConfig(self, icon):
338         aps=apps.scan()
339
340         lst=[aps[x]['name'] for x in aps]
341         lst.sort()
342
343         dialog=gtk.Dialog('App select', None,
344             gtk.DIALOG_DESTROY_WITH_PARENT, buttons=())
345
346         selector=hildon.TouchSelectorEntry(text=True)
347         selector.set_column_selection_mode(
348             hildon.TOUCH_SELECTOR_SELECTION_MODE_SINGLE)
349
350         dialog.vbox.pack_start(selector, True, True, 0)
351         dialog.set_size_request(0,900)
352         dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
353         dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
354
355         selector.append_text('None')
356
357         idx=0
358         cnt=1
359         for app in lst:
360             if app==None:
361                 continue
362             selector.append_text(app)
363             if icon.name!=None and aps[icon.name]['name']==app:
364                 idx=cnt
365             cnt+=1
366
367         selector.set_active(0, idx)
368
369         dialog.show_all()
370
371         app=None
372
373         r=dialog.run()
374
375         if r==gtk.RESPONSE_OK:
376             idx2=selector.get_active(0)
377             if idx2<1:
378                 app=None
379             else:
380                 cur=lst[idx2-1]
381                 for i in aps:
382                     if aps[i]['name']==cur:
383                         app=aps[i]
384                         break
385             if app!=None:
386                 app['icon2']=getIcon(app['icon'], self.config.getIconSize())
387             else:
388                 app={
389                     'id':       None,
390                     'icon2':    None,
391                     }
392             icon.setApp(app)
393
394         dialog.destroy()
395
396     def getData(self):
397         szx=0
398         szy=0
399         for but in self.butsSizeX:
400             szx+=1
401             if but.get_active()==True:
402                 break
403
404         for but in self.butsSizeY:
405             szy+=1
406             if but.get_active()==True:
407                 break
408
409         if self.getIndiv():
410             sz=(szx, szy)
411         else:
412             sz=(szx, szx)
413
414         wapps={}
415
416         for x in xrange(sz[0]):
417             for y in xrange(sz[1]):
418                 ico=self.igw.get(x,y)
419                 k=(x,y)
420                 wapps[k]=ico.name
421
422         indiv=self.buttonRotateIndividually.get_active()
423         lp=self.buttonRequireLongpress.get_active()
424         ar=self.buttonAnimateRotation.get_active()
425
426         ret={
427             'size':         sz,
428             'apps':         wapps,
429             'indiv':        indiv,
430             'longpress':    lp,
431             'animate':      ar,
432             }
433
434         return(ret)
435
436 if __name__=="__main__":
437     win=WinConfig()
438     win.connect('delete-event', gtk.main_quit)
439
440     win.show_all()
441     gtk.main()
442
443
444
445 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
446