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