Package elisa :: Package plugins :: Package bad :: Package raval_frontend :: Package raval_widgets :: Module grid_bar
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.raval_frontend.raval_widgets.grid_bar

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  import gobject 
 18  import pgm 
 19  from pgm.timing import implicit 
 20  from pgm.graph.group import Group 
 21  from pgm.graph.text import Text 
 22  from pgm.graph.image import Image 
 23  from pgm.utils import image as image_utils 
 24   
 25  from pgm.widgets import const 
 26   
 27  NONE_FOCUSED=-1 
 28  BACK_FOCUSED=0 
 29  ZOOM_OUT_FOCUSED=1 
 30  ZOOM_IN_FOCUSED=2 
 31   
32 -class GridBar(Group):
33 attr_focus_map = {BACK_FOCUSED: 'back', 34 ZOOM_IN_FOCUSED: 'zoom_in', 35 ZOOM_OUT_FOCUSED: 'zoom_out'} 36 37 38 __gsignals__ = { 39 'focus-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, 40 (gobject.TYPE_INT,)) 41 } 42 43
44 - def __init__(self, canvas=None, layer=pgm.DRAWABLE_MIDDLE, 45 width=1.0, height=2.0, 46 background=None, main_text="", sub_text="", 47 back_button=None, back_button_focused=None, 48 zoom_in_button=None, zoom_in_button_focused=None, 49 zoom_out_button=None, zoom_out_button_focused=None):
68
69 - def canvas__set(self, canvas):
70 super(GridBar, self).canvas__set(canvas) 71 self._layout()
72
73 - def _setup_initial_widgets(self):
74 self._main_text = self._create_text() 75 self._sub_text = self._create_text() 76 self.add(self._main_text) 77 self.add(self._sub_text) 78 79 unfocused_button_attrs = {'_back_img': 'back', 80 '_zoom_in_img': 'zoom_in', 81 '_zoom_out_img': 'zoom_out'} 82 for attr in ['_background_img', 83 '_back_focused_img', 84 '_zoom_in_focused_img', 85 '_zoom_out_focused_img'] + unfocused_button_attrs.keys(): 86 img = Image() 87 img.set_name(attr) 88 img.bg_color = (0, 0, 0, 0) 89 img.layout = pgm.IMAGE_FILLED 90 setattr(self, attr, img) 91 self.add(img) 92 93 for attr, zone_name in unfocused_button_attrs.iteritems(): 94 mouse_zone = self._create_click_zone() 95 mouse_zone.set_name(zone_name) 96 97 img = getattr(self, attr) 98 img_zone_name = '%s_zone' % img.get_name() 99 setattr(self, img_zone_name, mouse_zone) 100 self.add(mouse_zone)
101
102 - def _create_text(self):
103 txt = Text() 104 txt.font_family = "Nimbus Sans L Bold" 105 txt.bg_color = (0, 0, 0, 0) 106 txt.alignment = pgm.TEXT_ALIGN_CENTER 107 txt.ellipsize = pgm.TEXT_ELLIPSIZE_MIDDLE 108 txt.opacity = 255 109 txt.visible = True 110 return txt
111
112 - def _create_click_zone(self):
113 mouse_zone = Image() 114 mouse_zone.bg_color = (0, 0, 0, 0) 115 mouse_zone.visible = True 116 mouse_zone.connect("clicked", self._img_clicked) 117 return mouse_zone
118
119 - def _layout(self):
120 self._focus_buttons() 121 canvas = self.canvas 122 if canvas: 123 self._layout_buttons() 124 125 # text are in front of buttons and background 126 # FIXME: ideally there would be an horizontal scrolling animation 127 # if any text overlaps the buttons 128 z = self.z + 2 129 130 self._background_img.size = self.size 131 132 text_x = self._back_img.x + self._back_img.width * 0.95 133 w = canvas.width - text_x - self._zoom_in_img.width - self._zoom_out_img.width 134 135 if w > 0: 136 self._main_text.width = w 137 138 self._main_text.font_height = canvas.height / 22. 139 self._main_text.position = (text_x, 0.01, z) 140 141 self._sub_text.width = canvas.width 142 self._sub_text.font_height = canvas.height / 25. 143 self._sub_text.fg_color = (150, 150, 150, 255) 144 self._sub_text.position = (0, canvas.height / 20., z)
145
146 - def _layout_buttons(self):
147 z = self.z + 1 148 149 canvas = self.canvas 150 151 y = canvas.height / 150. 152 button_height = canvas.height / 20. 153 zoom_in_x = canvas.width * 0.91 154 155 self._back_img.height = button_height 156 self._back_focused_img.height = button_height 157 self._zoom_in_img.height = button_height 158 self._zoom_in_focused_img.height = button_height 159 self._zoom_out_img.height = button_height 160 self._zoom_out_focused_img.height = button_height 161 162 get_width = image_utils.get_width_from_par 163 164 self._back_img.width = get_width(self._back_img) 165 self._back_focused_img.width = get_width(self._back_focused_img) 166 self._back_img_zone.size = self._back_img.size 167 168 self._zoom_in_img.width = get_width(self._zoom_in_img) 169 self._zoom_in_focused_img.width = get_width(self._zoom_in_focused_img) 170 self._zoom_in_img_zone.size = self._zoom_in_img.size 171 172 self._zoom_out_img.width = get_width(self._zoom_out_img) 173 self._zoom_out_focused_img.width = get_width(self._zoom_out_focused_img) 174 self._zoom_out_img_zone.size = self._zoom_out_img.size 175 176 zoom_in_x = canvas.width - (get_width(self._zoom_in_img)) 177 178 back_position = (0, y, z) 179 zoom_in_position = (zoom_in_x, y, z) 180 zoom_out_position = (zoom_in_position[0] - self._zoom_out_img.width, 181 y, z) 182 183 self._back_img.position = back_position 184 self._back_focused_img.position = back_position 185 self._back_img_zone.position = back_position 186 187 self._zoom_in_img.position = zoom_in_position 188 self._zoom_in_focused_img.position = zoom_in_position 189 self._zoom_in_img_zone.position = zoom_in_position 190 191 self._zoom_out_img.position = zoom_out_position 192 self._zoom_out_focused_img.position = zoom_out_position 193 self._zoom_out_img_zone.position = zoom_out_position
194
195 - def _unfocus(self, attr):
196 name = '_%s_img' % attr 197 focused_name = '_%s_focused_img' % attr 198 getattr(self, name).visible = True 199 getattr(self, focused_name).visible = False
200
201 - def _focus(self, attr):
202 name = '_%s_img' % attr 203 focused_name = '_%s_focused_img' % attr 204 getattr(self, name).visible = False 205 getattr(self, focused_name).visible = True
206
207 - def _focus_buttons(self, emit_signal=False):
208 focused = self._focused_button 209 210 if focused == NONE_FOCUSED: 211 self._unfocus('back') 212 self._unfocus('zoom_in') 213 self._unfocus('zoom_out') 214 else: 215 to_focus = self.attr_focus_map.get(focused) 216 217 self._focus(to_focus) 218 if emit_signal: 219 self.emit('focus-changed', focused) 220 221 for attr in self.attr_focus_map.values(): 222 if attr != to_focus: 223 self._unfocus(attr)
224
225 - def _set_image(self, img, image_file, visible=True):
226 if img is not None: 227 img.clear() 228 if image_file is not None: 229 img.visible = visible 230 img.connect("pixbuf-loaded", self._img_loaded) 231 img.set_from_file(image_file)
232
233 - def _img_loaded(self, img):
234 if img not in self.children: 235 self.add(img) 236 self._layout()
237
238 - def _img_clicked(self, mouse_zone, x, y, z, time, button_type):
239 img_name = mouse_zone.get_name() 240 for const, name in self.attr_focus_map.iteritems(): 241 if name == img_name: 242 self._focused_button = const 243 break 244 self._focus_buttons(emit_signal=True) 245 246 # Stop propagation of the signal 247 return True
248
249 - def focused_button__set(self, value):
250 self._focused_button = value 251 self._focus_buttons()
252
253 - def focused_button__get(self):
254 return self._focused_button
255
256 - def background__set(self, image_file):
257 self._background_img.interp = pgm.IMAGE_NEAREST 258 self._set_image(self._background_img, image_file)
259
260 - def background__get(self):
261 return self._background_img
262
263 - def main_text__set(self, text):
264 self._main_text.label = text 265 self._layout()
266
267 - def main_text__get(self):
268 return self._main_text
269
270 - def sub_text__set(self, text):
271 self._sub_text.label = text 272 self._layout()
273
274 - def sub_text__get(self):
275 return self._sub_text
276
277 - def back_button__set(self, image_file):
278 self._set_image(self._back_img, image_file)
279
280 - def back_button__get(self):
281 return self._back_img
282
283 - def back_button_focused__set(self, image_file):
284 self._set_image(self._back_focused_img, image_file, visible=False)
285
286 - def back_button_focused__get(self):
287 return self._back_focused_img
288
289 - def zoom_in_button__set(self, image_file):
290 self._set_image(self._zoom_in_img, image_file)
291
292 - def zoom_in_button__get(self):
293 return self._zoom_in_img
294
295 - def zoom_in_button_focused__set(self, image_file):
296 self._set_image(self._zoom_in_focused_img, image_file, visible=False)
297
299 return self._zoom_in_focused_img
300
301 - def zoom_out_button__set(self, image_file):
302 self._set_image(self._zoom_out_img, image_file)
303
304 - def zoom_out_button__get(self):
305 return self._zoom_out_img
306
307 - def zoom_out_button_focused__set(self, image_file):
308 self._set_image(self._zoom_out_focused_img, image_file, visible=False)
309
311 return self._zoom_out_focused_img
312 313 if __name__ == "__main__": 314 import pgm 315 from pgm.widgets.grid_ng import Grid 316 #from pgm.widgets.list_ng import List as Grid 317 from pgm.graph.group import Group 318
319 - def on_key_press(viewport, event, widget):
320 # quit on q or ESC 321 if event.keyval == pgm.keysyms.q or \ 322 event.keyval == pgm.keysyms.Escape: 323 pgm.main_quit() 324 325 elif event.keyval == pgm.keysyms.a: 326 canvas = viewport.get_canvas() 327 canvas.regenerate() 328 329 elif event.keyval == pgm.keysyms.f: 330 viewport.fullscreen = not viewport.fullscreen 331 332 elif event.keyval == pgm.keysyms.Right: 333 new_value = widget.focused_button + 1 334 if new_value < ZOOM_IN_FOCUSED + 1: 335 widget.focused_button = new_value 336 337 elif event.keyval == pgm.keysyms.Left: 338 new_value = widget.focused_button - 1 339 if new_value >= NONE_FOCUSED: 340 widget.focused_button = new_value
341
342 - def on_delete(viewport, event):
343 pgm.main_quit()
344 345 # OpenGL viewport creation 346 factory = pgm.ViewportFactory('opengl') 347 gl = factory.create() 348 gl.title = 'GridBar widget' 349 350 # Canvas and image drawable creation 351 canvas = pgm.Canvas() 352 353 # Bind the canvas to the OpenGL viewport 354 gl.set_canvas(canvas) 355 gl.show() 356 357 ## grp = Group() 358 ## grp.visible = True 359 ## grp.canvas = canvas 360 361 ## list_widget = Grid() 362 ## list_widget.orientation = const.HORIZONTAL 363 ## list_widget.width = 3.0 364 ## list_widget.height = 2.0 365 ## list_widget.visible_range_size = 5 366 ## list_widget.visible = True 367 ## list_widget.canvas = canvas 368 ## #grp.add(list_widget) 369 ## list_widget.position = (0.5, 0.5, 0.0) 370 371 bar_height = 0.35 372 bar_y = canvas.height - bar_height 373 #bar_x = -0.5 374 bar_x = 0. 375 376 bar = GridBar() 377 bar.visible = True 378 379 bar.canvas = canvas 380 #grp.add(bar) 381 #list_widget.add(bar) 382 bar.position = (bar_x, bar_y, 0.0) 383 bar.size = (canvas.width, bar_height) 384 385 bar.background = 'theme/bottom-grid-bar.png' 386 bar.zoom_in_button = 'theme/grid-morebutton.png' 387 bar.zoom_out_button = 'theme/grid-lessbutton.png' 388 bar.back_button = 'theme/grid-backbutton.png' 389 390 bar.zoom_in_button_focused = 'theme/grid-morebutton-focused.png' 391 bar.zoom_out_button_focused = 'theme/grid-lessbutton-focused.png' 392 bar.back_button_focused = 'theme/grid-backbutton-focused.png' 393 394 395 bar.main_text = "Quelqu'un m'a dit que tu m'aimes encore" 396 bar.sub_text = 'Carla Bruni' 397 398 # Let's start a mainloop 399 gl.connect('key-press-event', on_key_press, bar) 400 gl.connect('delete-event', on_delete) 401 pgm.main() 402