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