(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 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         if dt==None:
77             self.name=None
78             self.icon=None
79         else:
80             self.name=dt['id']
81             self.icon=dt['icon2']
82         self.invalidate()
83
84     def getSize(self):
85         return(config.iconsize+config.iconspace)
86
87     def draw(self, cr, 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         if not self.ispressed:
145             return(False)
146
147         self.invalidate()
148
149         if self.timePressed()>self.presstime:
150             ret=False
151         else:
152             ret=True
153
154         return(ret)
155
156     def doPress(self):
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
165     def doRelease(self):
166         dt=time.time() - self.lastpress
167         self.ispressed=False
168         self.invalidate()
169         if dt<=self.presstime:
170             self.clickcount+=1
171             if self.clickcount==1:
172                 self.emit('click')
173             elif self.clickcount==2:
174                 self.emit('double-click')
175             if self.clickcount==3:
176                 self.emit('tripple-click')
177                 self.clickcount=0
178         elif dt>self.presstime and dt<2:
179             self.emit('long-press')
180
181     def doCancel(self):
182         self.ispressed=False
183
184     def invalidate(self, window=None):
185         if window==None:
186             window=self.window
187         else:
188             self.window=window
189         
190         if window==None:
191             return
192
193         w=config.iconsize + config.iconspace
194         rect=gdk.Rectangle(self.x, self.y, w, w)
195         gdk.Window.invalidate_rect(window, rect, True)
196
197 gobject.type_register(Icon)
198 signals=['click', 'double-click', 'tripple-click', 'long-press']
199 for s in signals:
200     gobject.signal_new(s, Icon, gobject.SIGNAL_RUN_FIRST,
201         gobject.TYPE_NONE, ())
202
203 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
204