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