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