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