(no commit message)
[drlaunch] / drlaunch / src / icons.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 config
26 import apps
27 import icon
28 from icon import getIcon, Icon
29 from sig import Disconnector
30
31 import gobject
32 import gtk
33
34 class IconIter:
35     def __init__(self, items):
36         self.iter=items.__iter__()
37
38     def __iter__(self):
39         ret=self.iter.__iter__()
40         return(ret)
41
42     def next(self):
43         ret=self.iter.next()
44         return(ret)
45
46 class Icons(gobject.GObject, Disconnector):
47 #class Icons(gtk.Widget, Disconnector):
48
49     #__gsignals__=Icon.gsignals
50     def __init__(self, isconfig, config):
51         self.__gobject_init__()
52         #gobject.GObject.__init__(self)
53         #gtk.Widget.__init__(self)
54         Disconnector.__init__(self)
55
56         self.icons={}
57         self.allicons={}
58         self.size=0
59         self.isconfig=isconfig
60         self.config=config
61
62         # signal handlers
63         self.handlers={}
64
65         self.maxsz=(0,0)
66
67         # setup allicons
68         self.resizeMax()
69
70 #       print "icons-init"
71
72     def finish(self):
73         self.dis_finish()
74         return
75 #       self.icons=None
76 #       self.allicons=None
77 #       print "icons-finish"
78
79 #    def __del__(self):
80 #       print "icons-del"
81
82     def resizeMax(self):
83         sz=self.maxsz
84         maxsz=self.config.getMaxSize()
85
86         # Create new entries in x
87         if maxsz[0]>sz[0]:
88             for x in xrange(maxsz[0]-sz[0]):
89                 for y in xrange(sz[1]):
90                     k=(x+sz[0],y)
91                     ico=Icon(self.isconfig, self.config)
92                     self.allicons[k]=ico
93                     self.connect_one(ico)
94             sz=(maxsz[0], sz[1])
95         elif maxsz[0]<sz[0]:
96             for x in xrange(sz[0]-maxsz[0]):
97                 for y in xrange(sz[1]):
98                     k=(maxsz[0]+x,y)
99                     t=self.allicons.pop(k)
100                     self.disconnect_one(t)
101             sz=(maxsz[0], sz[1])
102
103         # Create new entries in y
104         if maxsz[1]>sz[1]:
105             for y in xrange(maxsz[1]-sz[1]):
106                 for x in xrange(sz[0]):
107                     k=(x,y+sz[1])
108                     ico=Icon(self.isconfig, self.config)
109                     self.allicons[k]=ico
110                     self.connect_one(ico)
111             sz=(sz[0], maxsz[1])
112         elif maxsz[1]<sz[1]:
113             for y in xrange(sz[1]-maxsz[1]):
114                 for x in xrange(sz[0]):
115                     k=(x,y+maxsz[1])
116                     t=self.allicons.pop(k)
117                     self.disconnect_one(t)
118             sz=(sz[0], maxsz[1])
119
120         self.maxsz=sz
121
122     @classmethod
123     def register_signals(cls):
124         signals=Icon.gsignals
125         for s in signals:
126             ss=signals[s]
127             gobject.signal_new(s, cls, ss[0], ss[1], (Icon,))
128 #           gobject.SIGNAL_RUN_FIRST,
129 #               gobject.TYPE_NONE, (Icon,))
130
131     def __iter__(self):
132         return(IconIter(self.icons))
133
134     def connect_one(self, which):
135         self.handlers[which]={
136             'longpress':    self.c(which, 'long-press', self.signalLongpress),
137             'click':        self.c(which, 'click', self.signalClick),
138             'tripple':      self.c(which, 'tripple-click',
139                                 self.signalTrippleClick)
140             }
141
142     def disconnect_one(self, which):
143         for i in self.handlers[which]:
144             which.disconnect(self.handlers[which][i])
145         self.handlers.pop(which)
146 #       which.disconnect(self.h[which]_longpress)
147 #       which.disconnect(self.h_click)
148 #       which.disconnect(self.h_tripple)
149
150     def setSize(self, sz):
151 #       print "sz:", sz, self.size
152
153         if sz==self.size:
154             return
155
156         old=self.icons
157         self.icons={}
158
159         for x in xrange(sz[0]):
160             for y in xrange(sz[1]):
161                 k=(x,y)
162                 ico=self.allicons[k]
163                 self.icons[k]=ico
164 #               if old.has_key(k):
165 #                   old.pop(k)  # Re-used
166 #               else:
167 #                   self.connect_one(ico)
168
169         # Disconnect signals
170 #       for i in old:
171 #           self.disconnect_one(old[i])
172
173         self.size=sz
174
175     def getSize(self):
176         return(self.size)
177
178     def setWindow(self, win):
179         """ Set the window for all icons """
180
181         for i in self.icons:
182             ic=self.icons[i]
183             ic.setWindow(win)
184
185     def signalLongpress(self, icon):
186         #print "signalLongpress()", icon
187         self.emit('long-press', icon)
188
189     def signalClick(self, icon):
190         #print "signalClick()", icon
191         self.emit('click', icon)
192
193     def signalTrippleClick(self, icon):
194         #print "signalTrippleClick()", icon
195         self.emit('tripple-click', icon)
196
197     def get(self, x, y):
198         k=(x,y)
199         if self.icons.has_key(k):
200             ret=self.icons[k]
201         else:
202             ret=None
203
204         return(ret)
205
206     def load(self):
207 #       x=0
208 #       y=0
209 #       fn=["maegirls", "wifieye", 'battery-eye', 'image-viewer',
210 #           'tecnoballz', 'ncalc', 'rtcom-call-ui', 'rtcom-messaging-ui',
211 #           'extcalllog', 'browser', 'modest', 'osso-addressbook']
212
213         wapps=self.config.getApps()
214         #sz=self.config.getSize()
215         sz=self.getSize()
216
217         for k in wapps:
218             x,y=k
219             if x>=sz or y>=sz:
220                 continue
221
222             appname=wapps[k]
223             if appname!=None:
224                 app=apps.readOne(appname)
225                 if app!=None:
226                     app['icon2']=getIcon(app['icon'], self.config.getIconSize())
227                     self.get(x,y).setApp(app)
228             else:
229                 ic=self.get(x,y)
230                 ic.setApp(None)
231
232         # Reload icons to make sure backgrounds, etc are valid
233         for x in xrange(sz[0]):
234             for y in xrange(sz[1]):
235                 ic=self.get(x,y)
236                 ic.reload()
237
238
239 #       for f in fn:
240 #           dt=apps.readOne(f)
241 #           dt['icon2']=getIcon(dt['icon'])
242 #           print x, y, dt
243 #           self.get(x,y).setApp(dt)
244 #           x+=1
245 #           if x>=config.getSize(getSize()):
246 #               x=0
247 #               y+=1
248 ##          self.icons.append(p)
249
250 gobject.type_register(Icons)
251 Icons.register_signals()
252
253 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
254