c1a8096be96a2cd65d79778ec0d76b22a35e6127
[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, iconsize):
43     ico=getIconPath(name, iconsize)
44     ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, iconsize, iconsize)
45
46     return(ret)
47
48 class Icon(gobject.GObject):
49     def __init__(self, isconfig, config):
50         self.__gobject_init__()
51
52         self.isconfig=isconfig
53         self.config=config
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(self.config.iconsize+self.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         if self.timePressed() <= self.presstime or self.ispressed:
100             t=1.0 * min(self.timePressed(), self.presstime) / self.presstime
101             g=0.3+0.5*t
102             b=0.3+0.7*t
103             cr.set_source_rgba(0, g, b, 0.7)
104             print "t:", t
105
106         else:
107             cr.set_source_rgba(0.3, 0.3, 0.3, 0.7)
108
109         x3=x + (self.config.iconspace/6)
110         y3=y + (self.config.iconspace/6)
111
112         r=10    # Radius
113         w=self.config.iconsize+(self.config.iconspace*2/3)
114
115         cr.move_to(x3+r, y3)
116         cr.arc(x3+w-r,  y3+r,   r,          pi*1.5, pi*2)
117         cr.arc(x3+w-r,  y3+w-r, r,          0,      pi*0.5)
118         cr.arc(x3+r,    y3+w-r, r,          pi*0.5, pi)
119         cr.arc(x3+r,    y3+r,   r,          pi,     pi*1.5)
120
121         cr.stroke_preserve()
122         cr.fill()
123         cr.clip()
124         cr.paint()
125         cr.restore()
126
127         if self.icon==None:
128             return
129
130         icon=self.icon
131
132         if mode=='l':
133             icon2=icon
134         else:
135             icon2=icon.rotate_simple(gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE)
136
137         cr.save()
138         x3=x + (self.config.iconspace/2)
139         y3=y + (self.config.iconspace/2)
140         cr.set_source_pixbuf(icon2, x3, y3)
141         cr.paint()
142         cr.restore()
143
144         return(False)
145
146     def timerPressed(self):
147 #       if not self.ispressed:
148 #           return(False)
149
150         print "timerPressed"
151
152         self.invalidate()
153
154         if self.timePressed()>self.presstime:
155             ret=False
156         else:
157             ret=True
158
159         return(ret)
160
161     def doPress(self):
162         # Double-time: time for pressed and time for not-pressed
163         if time.time() - self.lastpress > self.presstime*2:
164             self.clickcount=0
165
166         self.lastpress=time.time()
167         self.ispressed=True
168         gobject.timeout_add(20, self.timerPressed)
169
170     def doRelease(self):
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                 self.emit('click')
178             elif self.clickcount==2:
179                 self.emit('double-click')
180             if self.clickcount==3:
181                 self.emit('tripple-click')
182                 self.clickcount=0
183         elif dt>self.presstime and dt<2:
184             self.emit('long-press')
185
186     def doCancel(self):
187         self.ispressed=False
188
189     def invalidate(self, window=None):
190         if window==None:
191             window=self.window
192         else:
193             self.window=window
194         
195         if window==None:
196             return
197
198         w=self.config.iconsize + self.config.iconspace
199         rect=gdk.Rectangle(self.x, self.y, w, w)
200         gdk.Window.invalidate_rect(window, rect, True)
201
202 gobject.type_register(Icon)
203 signals=['click', 'double-click', 'tripple-click', 'long-press']
204 for s in signals:
205     gobject.signal_new(s, Icon, gobject.SIGNAL_RUN_FIRST,
206         gobject.TYPE_NONE, ())
207
208 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
209