(no commit message)
[drlaunch] / icon.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 from hildondesktop import *
29 from gtk import gdk
30 from math import pi
31 import cairo
32 import time
33
34 from portrait import FremantleRotation
35 import launcher
36 from xdg.IconTheme import getIconPath
37
38
39 import config
40 import apps
41
42 def getIcon(name):
43     ico=getIconPath(name, config.iconsize)
44     ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, config.iconsize,
45         config.iconsize)
46
47     return(ret)
48
49 class Icon(gobject.GObject):
50     def __init__(self, isconfig):
51         self.__gobject_init__()
52
53         self.isconfig=isconfig
54
55         self.name=None
56         self.icon=None
57         self.lastpress=0
58         self.ispressed=False
59
60         self.x=0
61         self.y=0
62
63         self.presstime=0.25
64
65         self.window=None
66
67         self.clickcount=0
68
69     def timePressed(self):
70         """ return how much time a button is pressed """
71         dt=time.time() - self.lastpress
72
73         return(dt)
74
75     def setApp(self, dt):
76         self.name=dt['id']
77         self.icon=dt['icon2']
78         self.invalidate()
79
80     def getSize(self):
81         return(config.iconsize+config.iconspace)
82
83     def draw(self, cr, x, y, mode):
84         #print "draw", x, y, mode
85         self.x=x
86         self.y=y
87
88         if self.icon==None and not self.isconfig:
89             return
90
91         cr.save()
92         cr.set_source_rgba(0.1, 0.1, 0.1, 1)
93         cr.set_line_width(5)
94
95         if self.ispressed:
96             t=1.0 * min(self.timePressed(), self.presstime) / self.presstime
97             g=0.3+0.5*t
98             b=0.3+0.7*t
99             cr.set_source_rgba(0, g, b, 0.7)
100         else:
101             cr.set_source_rgba(0.3, 0.3, 0.3, 0.7)
102
103         x3=x + (config.iconspace/6)
104         y3=y + (config.iconspace/6)
105
106         r=10    # Radius
107         w=config.iconsize+(config.iconspace*2/3)
108
109         cr.move_to(x3+r, y3)
110         cr.arc(x3+w-r,  y3+r,   r,          pi*1.5, pi*2)
111         cr.arc(x3+w-r,  y3+w-r, r,          0,      pi*0.5)
112         cr.arc(x3+r,    y3+w-r, r,          pi*0.5, pi)
113         cr.arc(x3+r,    y3+r,   r,          pi,     pi*1.5)
114
115         cr.stroke_preserve()
116         cr.fill()
117         cr.clip()
118         cr.paint()
119         cr.restore()
120
121         if self.icon==None:
122             return
123
124         icon=self.icon
125
126         if mode=='l':
127             icon2=icon
128         else:
129             icon2=icon.rotate_simple(gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE)
130
131         cr.save()
132         x3=x + (config.iconspace/2)
133         y3=y + (config.iconspace/2)
134         cr.set_source_pixbuf(icon2, x3, y3)
135         cr.paint()
136         cr.restore()
137
138         return(False)
139
140     def timerPressed(self):
141         #print "timer"
142
143         if not self.ispressed:
144             return(False)
145
146         self.invalidate()
147
148         if self.timePressed()>self.presstime:
149             ret=False
150         else:
151             ret=True
152
153         return(ret)
154
155     def doPress(self):
156         #print "doPress()"
157         # Double-time: time for pressed and time for not-pressed
158         if time.time() - self.lastpress > self.presstime*2:
159             self.clickcount=0
160
161         self.lastpress=time.time()
162         self.ispressed=True
163         gobject.timeout_add(20, self.timerPressed)
164         #print "doPress() end"
165
166     def doRelease(self):
167         print "doRelease()"
168         dt=time.time() - self.lastpress
169         self.ispressed=False
170         self.invalidate()
171         if dt<=self.presstime:
172             self.clickcount+=1
173             if self.clickcount==1:
174                 print "click"
175                 self.emit('click')
176             elif self.clickcount==2:
177                 print "double-click"
178                 self.emit('double-click')
179             if self.clickcount==3:
180                 print "tripple-click"
181                 self.emit('tripple-click')
182                 self.clickcount=0
183         elif dt>self.presstime and dt<2:
184             print "long-press"
185             self.emit('long-press')
186
187     def doCancel(self):
188         print "doCancel()"
189         self.ispressed=False
190
191     def invalidate(self, window=None):
192         if window==None:
193             window=self.window
194         else:
195             self.window=window
196         
197         if window==None:
198             return
199
200         w=config.iconsize + config.iconspace
201         rect=gdk.Rectangle(self.x, self.y, w, w)
202         #print "rect", self.x, self.y, w, w
203         gdk.Window.invalidate_rect(window, rect, True)
204
205 gobject.type_register(Icon)
206 signals=['click', 'double-click', 'tripple-click', 'long-press']
207 for s in signals:
208     gobject.signal_new(s, Icon, gobject.SIGNAL_RUN_FIRST,
209         gobject.TYPE_NONE, ())
210
211 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
212