a00372316abad4c81b6ac74ce1f2f97a69255209
[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
40 class WinConfig(StackableWindow):
41     def __init__(self, config, *args):
42         StackableWindow.__init__(self)
43
44         self.config=config
45
46         self.setupUi()
47
48     def setupUi(self):
49         self.set_title("DrLaunch Configuration")
50
51         self.pa=hildon.PannableArea()
52         self.add(self.pa)
53         self.pa.set_property('mov-mode', hildon.MOVEMENT_MODE_HORIZ)
54
55         hbox=gtk.HBox()
56         self.pa.add_with_viewport(hbox)
57
58         # Add the first column of options
59         al=gtk.Alignment(yscale=0)
60         hbox.add(al)
61
62         vbox=gtk.VBox()
63         al.add(vbox)
64
65         maxsz=self.config.getMaxSize()
66
67         # ----------------------------------------------
68         vbox.add(gtk.Label('Width:'))
69
70         hbox2=gtk.HBox()
71         vbox.add(hbox2)
72
73         self.butsSizeX=[]
74         self.butsSize=self.butsSizeX # For now
75         for i in xrange(maxsz[0]):
76             but=hildon.GtkToggleButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
77             but.set_label("%s" % (i+1,))
78             but.connect('toggled', self.slotButtonSizeX, i)
79
80             self.butsSizeX.append(but)
81
82             hbox2.add(but)
83
84         # ----------------------------------------------
85         vbox.add(gtk.Label('Height:'))
86
87         hbox2=gtk.HBox()
88         vbox.add(hbox2)
89
90         self.butsSizeY=[]
91         for i in xrange(maxsz[1]):
92             but=hildon.GtkToggleButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
93             but.set_label("%s" % (i+1,))
94             but.connect('toggled', self.slotButtonSizeY, i)
95
96             self.butsSizeY.append(but)
97
98             hbox2.add(but)
99
100         but=hildon.CheckButton(
101                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
102         but.set_label("Rotate icons individually")
103         but.connect('toggled', self.slotButtonRotateIndividually)
104         self.buttonRotateIndividually=but
105         vbox.add(but)
106
107         but=hildon.CheckButton(
108                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
109         but.set_label("Require long press")
110 #       but.connect('toggled', self.slotButtonLongpress)
111         self.buttonRequireLongpress=but
112         vbox.add(but)
113
114         but=hildon.CheckButton(
115                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
116         but.set_label("Animate rotation")
117         self.buttonAnimateRotation=but
118         vbox.add(but)
119
120         # -----------------------------------------------
121         # Second column of options
122         vbox=gtk.VBox()
123         al=gtk.Alignment(xalign=0, yalign=1, xscale=1)
124         al.add(vbox)
125         hbox.add(al)
126         but=hildon.Button(
127                 gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT,
128                 hildon.BUTTON_ARRANGEMENT_VERTICAL)
129         but.set_title("About")
130         but.connect("clicked", self.slotButtonAbout)
131         vbox.add(but)
132         self.buttonAbout=but
133
134         # -----------------------------------------------
135         # Add the icongrid
136         self.igw=IconGridWidget(True, self.config, False)
137 #       self.igw.do_realize()
138 #       self.igw.setSize(config.getSize())
139         al=gtk.Alignment(xalign=0, xscale=0)
140         al.add(self.igw)
141         al.set_padding(0, 0, 20, 0)
142         hbox.add(al)
143
144         self.igw.connect('long-press', self.slotLongpress)
145         self.igw.connect('click', self.slotLongpress)
146
147         self.ignore_toggle=False
148
149         self.setSize(self.config.getSize())
150         self.setIndiv(self.config.getIndiv())
151         self.setLongpress(self.config.getLongpress())
152         self.setAnimate(self.config.getAnimate())
153
154     def slotLongpress(self, sender, icon):
155         self.doConfig(icon)
156
157     def slotButtonSizeX(self, sender, id):
158         if self.getIndiv():
159             old=self.getSize()
160             sz=(id+1, old[1])
161         else:
162             sz=(id+1, id+1)
163
164         self.setSize(sz)
165         
166     def slotButtonSizeY(self, sender, id):
167         old=self.getSize()
168         sz=(old[0], id+1)
169         self.setSize(sz)
170         
171     def slotButtonRotateIndividually(self, sender):
172         but=self.buttonRotateIndividually
173         self.setIndiv(but.get_active())
174
175     def slotButtonAbout(self, sender):
176         print "about"
177         #dlg=DlgAbout()
178         #dlg.run()
179         DlgAbout.present2()
180
181 #    def slotButtonLongpress(self, sender):
182 #       but=self.buttonRequireLongpress
183 #       self.set
184
185     def setSize(self, sz):
186         if self.ignore_toggle:
187             return
188
189         self.ignore_toggle=True
190
191         maxsz=self.config.getMaxSize()
192
193         id=sz[0]-1
194
195         for i in xrange(maxsz[0]):
196             but=self.butsSizeX[i]
197             but.set_active(i==id)
198
199         id=sz[1]-1
200
201         for i in xrange(maxsz[1]):
202             but=self.butsSizeY[i]
203             but.set_active(i==id)
204
205         self.ignore_toggle=False
206
207         self.igw.setSize(sz)
208
209         self.igw.queue_draw()
210
211     def getSize(self):
212         return(self.igw.getSize())
213
214     def getIndiv(self):
215         ret=self.buttonRotateIndividually.get_active()
216
217         return(ret)
218
219     def setIndiv(self, indiv):
220         if indiv:
221             for i in self.butsSizeY:
222                 i.set_sensitive(True)
223             for i in self.butsSizeX:
224                 i.set_sensitive(True)
225
226         else:
227             for i in self.butsSizeY:
228                 i.set_sensitive(False)
229
230             cnt=0
231             for i in self.butsSizeX:
232                 cnt+=1
233                 if cnt>4:
234                     i.set_sensitive(False)
235
236             sz=self.getSize()
237             szx=sz[0]
238             if szx>4:
239                 szx=4
240             self.setSize((szx, szx))
241
242         self.buttonRotateIndividually.set_active(indiv)
243
244     def setLongpress(self, lp):
245         self.buttonRequireLongpress.set_active(lp)
246
247     def setAnimate(self, ar):
248         self.buttonAnimateRotation.set_active(ar)
249
250     def doConfig(self, icon):
251         aps=apps.scan()
252
253         lst=[aps[x]['name'] for x in aps]
254         lst.sort()
255
256         dialog=gtk.Dialog('App select', None,
257             gtk.DIALOG_DESTROY_WITH_PARENT, buttons=())
258
259         selector=hildon.TouchSelectorEntry(text=True)
260         selector.set_column_selection_mode(
261             hildon.TOUCH_SELECTOR_SELECTION_MODE_SINGLE)
262
263         dialog.vbox.pack_start(selector, True, True, 0)
264         dialog.set_size_request(0,900)
265         dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
266         dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
267
268         selector.append_text('None')
269
270         idx=0
271         cnt=1
272         for app in lst:
273             if app==None:
274                 continue
275             selector.append_text(app)
276             if icon.name!=None and aps[icon.name]['name']==app:
277                 idx=cnt
278             cnt+=1
279
280         selector.set_active(0, idx)
281
282         dialog.show_all()
283
284         app=None
285
286         r=dialog.run()
287
288         if r==gtk.RESPONSE_OK:
289             idx2=selector.get_active(0)
290             if idx2<1:
291                 app=None
292             else:
293                 cur=lst[idx2-1]
294                 for i in aps:
295                     if aps[i]['name']==cur:
296                         app=aps[i]
297                         break
298             if app!=None:
299                 app['icon2']=getIcon(app['icon'], self.config.getIconSize())
300             else:
301                 app={
302                     'id':       None,
303                     'icon2':    None,
304                     }
305             icon.setApp(app)
306
307         dialog.destroy()
308
309     def getData(self):
310         szx=0
311         szy=0
312         for but in self.butsSizeX:
313             szx+=1
314             if but.get_active()==True:
315                 break
316
317         for but in self.butsSizeY:
318             szy+=1
319             if but.get_active()==True:
320                 break
321
322         if self.getIndiv():
323             sz=(szx, szy)
324         else:
325             sz=(szx, szx)
326
327         wapps={}
328
329         for x in xrange(sz[0]):
330             for y in xrange(sz[1]):
331                 ico=self.igw.get(x,y)
332                 k=(x,y)
333                 wapps[k]=ico.name
334
335         indiv=self.buttonRotateIndividually.get_active()
336         lp=self.buttonRequireLongpress.get_active()
337         ar=self.buttonAnimateRotation.get_active()
338
339         ret={
340             'size':         sz,
341             'apps':         wapps,
342             'indiv':        indiv,
343             'longpress':    lp,
344             'animate':      ar,
345             }
346
347         return(ret)
348
349 if __name__=="__main__":
350     win=WinConfig()
351     win.connect('delete-event', gtk.main_quit)
352
353     win.show_all()
354     gtk.main()
355
356
357
358 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
359