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