clean-ups
[drlaunch] / src / icongrid.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 from xdg.IconTheme import getIconPath
36
37 import config
38 import apps
39 import icon
40 from icon import Icon
41 from icons import Icons
42
43 def getIcon(name):
44     ico=getIconPath(name, config.iconsize)
45     ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, config.iconsize,
46         config.iconsize)
47
48     return(ret)
49
50 #class IconGrid(gtk.Widget, FremantleRotation):
51 class IconGrid(object): #(gobject.GObject):
52     def __init__(self, isconfig=False):
53 #       self.__gobject_init__()
54
55         self.size=0
56
57         self.isconfig=isconfig
58
59         self.icons=Icons(isconfig)
60
61         self.setMode('l')
62
63         # Maybe fix those:
64 #       w=(config.getSize() * config.iconsize) + \
65 #           (config.getSize()) * config.iconspace
66         #self.set_size_request(w, w)
67
68 #       self.setSize(config.getSize())
69         self.setSize(4)
70
71         self.lasticon=None  # The last icon that got selected
72
73         self.reloadIcons()
74
75     def connect(self, what, *args):
76         if what in icon.signals:
77             self.icons.connect(what, *args)
78         else:
79             super(IconGrid, self).connect(what, *args)
80
81     def setSize(self, size):
82         self.size=size
83         self.icons.setSize(size)
84
85     def setMode(self, mode):
86         self.mode=mode
87         if isinstance(self, gtk.Widget):
88             self.queue_draw()
89
90     def iconAt(self, x, y):
91         """ Get icon at coordinates x,y. X and Y are in pixels """
92
93         w=config.iconsize + config.iconspace
94
95         if self.mode=='l':
96             x2=int(x / w)
97             y2=int(y / w)
98         else:
99             x2=self.size - int(y/w) - 1
100             y2=int(x/w)
101
102         ret=self.get(x2,y2)
103
104         return(ret)
105
106     def get(self, x, y):
107         ret=self.icons.get(x,y)
108
109         return(ret)
110
111     def _draw(self, cr, event):
112         w=config.iconsize + config.iconspace
113         for x,y in self.icons:
114             if self.mode=='l':
115                 x2=x * (config.iconsize + config.iconspace)
116                 y2=y * (config.iconsize + config.iconspace)
117             else:
118                 x2=y * (config.iconsize + config.iconspace)
119                 y2=(self.size-x-1) * (config.iconsize + config.iconspace)
120
121             # Only repaint the needed icons
122             rect=gdk.Rectangle(x2, y2, w, w)
123             t=rect.intersect(event.area)
124             if t.width==0 and t.height==0:
125                 continue
126
127             ico=self.icons.get(x,y)
128             ico.draw(cr, x2, y2, self.mode)
129
130     def do_expose_event(self, event):
131         cr=self.window.cairo_create()
132
133         cr.rectangle(event.area.x, event.area.y,
134             event.area.width, event.area.height)
135
136         cr.clip()
137
138         self._draw(cr, event)
139
140     def setLastIcon(self, icon):
141         if icon==self.lasticon:
142             return
143
144         if self.lasticon!=None:
145             self.lasticon.doCancel()
146             self.lasticon.invalidate(self.window)
147         self.lasticon=icon
148
149     def do_button_press_event(self, event):
150         icon=self.iconAt(event.x, event.y)
151         if icon==None:
152             return
153 #       rect=gdk.Rectangle(event.x,event.y,1,1)
154 #       rect=gdk.Rectangle(0, 0, 100, 100)
155         icon.doPress()
156         icon.invalidate(self.window)
157         self.setLastIcon(icon)
158
159 #       gdk.Window.invalidate_rect(self.window, rect, True)
160
161         return(True)
162
163     def do_button_release_event(self, event):
164         if self.lasticon!=None:
165             self.lasticon.invalidate(self.window)
166             self.lasticon.doRelease()
167
168         self.setLastIcon(None)
169
170         return(True)
171
172     def do_leave_notify_event(self, event):
173         self.setLastIcon(None)
174         return(True)
175
176     def do_pproperty_notify_event(self, event):
177         icon=self.iconAt(event.x, event.y)
178         if icon==None:
179             return
180         icon.doCancel()
181         icon.invalidate(self.window)
182         return(True)
183
184     def do_motion_notify_event(self, event):
185         icon=self.iconAt(event.x, event.y)
186         if self.lasticon==icon:
187             return(True)
188
189         self.setLastIcon(None)
190         icon.doCancel()
191         icon.invalidate(self.window)
192         return(True)
193
194     def do_button_press_event_old(self, event):
195         if event.type==gdk.BUTTON_PRESS:
196             if self.mode=='p':
197                 self.setMode('l')
198             else:
199                 self.setMode('p')
200             self.queue_draw()
201         return True
202
203     # For debugging
204     def do_event1(self, event):
205         print "event:", event, event.type
206
207     def reloadIcons(self):
208         self.icons.load()
209
210 #    def on_orientation_changed(self, orientation):
211 #       print "orch:", orientation
212 #       o=orientation[0]
213 #       self.setMode(o)
214
215 class IconGridWidget(IconGrid, gtk.Widget):
216     def __init__(self, isconfig):
217         IconGrid.__init__(self, isconfig)
218         gtk.Widget.__init__(self)
219
220         if isconfig:
221             w=4 * (config.iconsize + config.iconspace)
222         else:
223             w=self.size * (config.iconsize + config.iconspace)
224
225         self.set_size_request(w, w)
226
227     def do_realize(self):
228         screen=self.get_screen()
229         self.set_colormap(screen.get_rgba_colormap())
230         self.set_app_paintable(True)
231
232         self.set_flags(self.flags() | gtk.REALIZED)
233
234         self.window=gdk.Window(
235             self.get_parent_window(),
236             width=self.allocation.width,
237             height=self.allocation.height,
238             window_type=gdk.WINDOW_CHILD,
239             wclass=gdk.INPUT_OUTPUT,
240             event_mask=self.get_events() | gdk.EXPOSURE_MASK
241                 | gdk.BUTTON_PRESS_MASK 
242                 | gdk.BUTTON_RELEASE_MASK 
243                 | gdk.BUTTON_MOTION_MASK
244                 | gdk.POINTER_MOTION_MASK
245                 | gdk.POINTER_MOTION_HINT_MASK 
246                 | gdk.ENTER_NOTIFY_MASK
247                 | gdk.LEAVE_NOTIFY_MASK )
248
249         self.window.set_user_data(self)
250         self.style.attach(self.window)
251
252 #       self.style.set_background(self.window, gtk.STATE_NORMAL)
253         self.window.move_resize(*self.allocation)
254
255 #       self.pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(
256 #             self.window, self.style.bg[gtk.STATE_NORMAL], STAR_PIXMAP)
257         
258 #       self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
259
260         #gtk.Widget.do_realize(self)
261         #HomePluginItem.do_realize(self)
262
263 #       screen=self.get_screen()
264 #       self.set_colormap(screen.get_rgba_colormap())
265 #       self.set_app_paintable(True)
266
267     def do_unrealize(self):
268         #self.window.set_user_data(None)
269         self.window.destroy()
270
271 #gobject.type_register(IconGrid)
272 gobject.type_register(IconGridWidget)
273
274
275 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
276