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