4 # Copyright (C) 2010 Stefanos Harhalakis
6 # This file is part of wifieye.
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.
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.
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/>.
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
28 from hildondesktop import *
34 from portrait import FremantleRotation
36 from xdg.IconTheme import getIconPath
43 # Fall-back to default/blue if not found or name==None
44 def getIcon(name, iconsize):
46 idef='tasklaunch_default_application'
48 # If name==None then use the default icon
49 if name==None or name=='':
54 ico=getIconPath(iname, iconsize)
56 # If not found then use the default icon
58 ico=getIconPath(idef, iconsize)
60 ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, iconsize, iconsize)
64 class Icon(gobject.GObject):
65 def __init__(self, isconfig, config):
66 self.__gobject_init__()
68 self.isconfig=isconfig
87 def timePressed(self):
88 """ return how much time a button is pressed """
89 dt=time.time() - self.lastpress
103 return(self.config.iconsize+self.config.iconspace)
105 def setAngle(self, angle):
106 if self.angle==angle:
112 # The caller should be responsible for redrawing.
113 # If we call invalidate() here there is the risk of having
114 # icons rotate individually using different angles
117 def draw(self, cr, x, y):
118 self.draw_queued=False
122 if self.icon==None and not self.isconfig:
127 cr.set_source_rgba(0.1, 0.1, 0.1, 1)
131 if self.timePressed() <= self.presstime or self.ispressed:
132 t=1.0 * min(self.timePressed(), self.presstime) / self.presstime
135 cr.set_source_rgba(0, g, b, 0.7)
137 cr.set_source_rgba(0.3, 0.3, 0.3, 0.7)
139 x3=x + (self.config.iconspace/6)
140 y3=y + (self.config.iconspace/6)
143 w=self.config.iconsize+(self.config.iconspace*2/3)
146 cr.arc(x3+w-r, y3+r, r, pi*1.5, pi*2)
147 cr.arc(x3+w-r, y3+w-r, r, 0, pi*0.5)
148 cr.arc(x3+r, y3+w-r, r, pi*0.5, pi)
149 cr.arc(x3+r, y3+r, r, pi, pi*1.5)
164 # Old method. Faster rotation but without support for rotation
170 # icon2=icon.rotate_simple(gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE)
173 # x3=x + (self.config.iconspace/2)
174 # y3=y + (self.config.iconspace/2)
175 # cr.set_source_pixbuf(icon2, x3, y3)
181 # Width is the iconsize plus the empty border around the icon
182 w=self.config.iconsize + self.config.iconspace
184 # This is used to locate the center of the surface
187 # This is the delta from the center where icons are drawn
188 dx2=int(self.config.iconsize/2)
190 # A surface to draw on
191 t_s=cairo.ImageSurface(cairo.FORMAT_ARGB32, w, w)
193 # And a context to draw
194 t_cr0=cairo.Context(t_s)
195 t_cr=gtk.gdk.CairoContext(t_cr0)
197 # A transformation matrix with dx/dy set to point to the center
198 m=cairo.Matrix(1, 0, 0, 1, dx, dx)
200 # Transform degrees to rads
201 rot=-1 * pi * 2 * self.angle / 360
204 t_cr.set_source_pixbuf(icon2, -dx2, -dx2)
207 # Draw the rotated icon on the main cairo context
209 cr.set_source_surface(t_s, x, y)
215 def timerPressed(self):
216 # if not self.ispressed:
221 if self.timePressed()>self.presstime:
229 # Double-time: time for pressed and time for not-pressed
230 if time.time() - self.lastpress > self.presstime*2:
233 self.lastpress=time.time()
235 gobject.timeout_add(20, self.timerPressed)
238 dt=time.time() - self.lastpress
241 if dt<=self.presstime:
243 if self.clickcount==1:
245 elif self.clickcount==2:
246 self.emit('double-click')
247 if self.clickcount==3:
248 self.emit('tripple-click')
250 elif dt>self.presstime and dt<2:
251 self.emit('long-press')
256 def setWindow(self, window):
259 def invalidate(self, window=None):
271 self.draw_queued=True
272 w=self.config.iconsize + self.config.iconspace
273 rect=gdk.Rectangle(self.x, self.y, w, w)
274 gdk.Window.invalidate_rect(window, rect, True)
276 gobject.type_register(Icon)
277 signals=['click', 'double-click', 'tripple-click', 'long-press']
279 gobject.signal_new(s, Icon, gobject.SIGNAL_RUN_FIRST,
280 gobject.TYPE_NONE, ())
282 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent: