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