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

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

  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 pgm 
 18  from pgm.timing import implicit 
 19  from pgm.graph.image import Image 
 20  from pgm.utils import image as image_utils 
 21   
 22  from pgm.widgets import list_ng, const 
 23  import scrolled_list 
 24   
 25   
26 -class ShadedScrolledList(scrolled_list.ScrolledList):
27
28 - def __init__(self, 29 width=1.0, height=2.0, visible_range_size=7, 30 top_shade=None, bottom_shade=None,orientation=const.VERTICAL):
31 self._bottom_shade = Image() 32 self._bottom_shade.connect("pixbuf-loaded", self._bottom_shade_loaded) 33 34 self._top_shade = Image() 35 self._top_shade.connect("pixbuf-loaded", self._top_shade_loaded) 36 37 settings = {'duration': 300, 38 'transformation': implicit.DECELERATE, 39 } 40 self._animated_top_shade = implicit.AnimatedObject(self._top_shade, 41 ('opacity',)) 42 self._animated_bottom_shade = implicit.AnimatedObject(self._bottom_shade, 43 ('opacity',)) 44 self._animated_top_shade.setup_next_animations(**settings) 45 self._animated_bottom_shade.setup_next_animations(**settings) 46 47 48 super(ShadedScrolledList, self).__init__(width=width, height=height, 49 visible_range_size=visible_range_size, 50 orientation=orientation) 51 self.add(self._bottom_shade) 52 self.add(self._top_shade) 53 self.top_shade = top_shade 54 self.bottom_shade = bottom_shade
55
56 - def top_shade__set(self, top_shade):
57 if top_shade: 58 self._top_shade.set_from_file(top_shade)
59
60 - def top_shade__get(self):
61 return self._top_shade
62
63 - def bottom_shade__set(self, bottom_shade):
64 if bottom_shade: 65 self._bottom_shade.set_from_file(bottom_shade)
66
67 - def bottom_shade__get(self):
68 return self._bottom_shade
69
70 - def _top_shade_loaded(self, top):
71 self._top_shade.bg_color = (0, 0, 0, 0) 72 self._top_shade.visible = True 73 self._layout_shades() 74 self._hide_show_shades()
75
76 - def _bottom_shade_loaded(self, bottom):
77 self._bottom_shade.bg_color = (0, 0, 0, 0) 78 self._bottom_shade.visible = True 79 self._layout_shades() 80 self._hide_show_shades()
81
82 - def pop(self, index):
83 widget = scrolled_list.ScrolledList.pop(self, index) 84 self._hide_show_shades() 85 return widget
86
87 - def insert(self, index, widget):
90
91 - def selected_item__set(self, index):
94
95 - def size__set(self, size):
98
99 - def position__set(self, position):
102
103 - def _hide_show_shades(self):
104 index = self.selected_item 105 middle = self.visible_range_size / 2 106 max_opacity = 255 107 min_opacity = 1 108 109 bar_visible = False 110 if self._vbar or self._hbar: 111 bar_visible = self._hbar and self._hbar.visible 112 bar_visible = bar_visible or (self._vbar and self._vbar.visible) 113 114 if bar_visible: 115 self._top_shade.visible = True 116 self._bottom_shade.visible = True 117 if index > middle: 118 self._animated_top_shade.opacity = max_opacity 119 else: 120 self._animated_top_shade.opacity = min_opacity 121 122 if index < len(self.widgets)-middle-1: 123 self._animated_bottom_shade.opacity = max_opacity 124 else: 125 self._animated_bottom_shade.opacity = min_opacity 126 else: 127 self._top_shade.visible = False 128 self._bottom_shade.visible = False
129
130 - def _layout_shades(self):
131 z = self.z + 1 132 133 try: 134 current_item = self[self.selected_item] 135 item_size = current_item.size 136 except IndexError: 137 item_size = self.size 138 139 if self._orientation == const.VERTICAL: 140 self._top_shade.width = item_size[0] 141 self._top_shade.height = image_utils.get_height_from_par(self._top_shade) 142 143 top_y = -(self._top_shade.height / 1.8) 144 145 if self._vbar: 146 if self._vbar_pos == const.LEFT: 147 x = self._vbar_spacing * 1.5 148 else: 149 x = self.width - self._vbar_spacing 150 else: 151 x = 0 152 self._top_shade.position = (x, top_y, z) 153 154 self._bottom_shade.width = item_size[0] 155 height = image_utils.get_height_from_par(self._bottom_shade) 156 self._bottom_shade.height = height 157 158 bottom_y = self.height - (self._bottom_shade.height / 2.5) 159 self._bottom_shade.position = (x, bottom_y, z) 160 161 elif self._orientation == const.HORIZONTAL: 162 self._top_shade.height = item_size[1] 163 self._top_shade.width = image_utils.get_width_from_par(self._top_shade) 164 165 top_x = -(self._top_shade.width / 1.5) 166 if self._hbar: 167 if self._hbar_pos == const.TOP: 168 y = self._hbar_spacing 169 else: 170 y = self.height - self._hbar_spacing 171 else: 172 y = 0 173 self._top_shade.position = (top_x, y, z) 174 175 self._bottom_shade.height = self.height 176 bottom_width = image_utils.get_width_from_par(self._bottom_shade) 177 self._bottom_shade.width = bottom_width 178 179 bottom_x = self.width - (self._bottom_shade.width / 2.5) 180 self._bottom_shade.position = (bottom_x, y, z)
181 182 183 if __name__ == "__main__": 184 import pgm 185 import gobject 186 import operator 187 from pgm.graph.group import Group 188 from pgm.graph.image import Image 189 from pgm.graph.text import Text 190 from pgm.timing import implicit 191 from pgm.widgets import sliced_image, scrollbar, const 192 import selector 193 194
195 - class ListItem(Group):
196
197 - def __init__(self, thumbnail=None, description=None, arrow=None):
198 Group.__init__(self) 199 200 self.thumbnail = Image() 201 #self.thumbnail.bg_color = (0, 255, 0, 255) 202 self.thumbnail.bg_color = (0, 0, 0, 0) 203 self.thumbnail.layout = pgm.IMAGE_SCALED 204 self.thumbnail.visible = True 205 206 self._description = self._create_text() 207 self._description.bg_color = (255, 0, 0, 255) 208 209 self.add(self.thumbnail) 210 self.add(self._description) 211 212 if thumbnail is not None: 213 self.thumbnail.set_from_file(thumbnail) 214 215 self.description = description 216 217 ## if self.canvas: 218 ## self._layout() 219 220 # Arrow support disabled until some decision is taken. 221 """ 222 arrow_x = text_x + text_width 223 arrow_y = 0.4 224 arrow_width = 0.25 225 arrow_height = 0.8 226 227 self.arrow = Image() 228 self.arrow.bg_color = (0, 0, 0, 0) 229 #self.arrow.bg_color = (0, 0, 255, 255) 230 self.arrow.layout = pgm.IMAGE_SCALED 231 self.arrow.size = (arrow_width, arrow_height) 232 self.add(self.arrow) 233 self.arrow.position = (arrow_x, arrow_y, 0) 234 235 if arrow: 236 self.arrow.set_from_file(arrow) 237 """
238
239 - def _layout(self):
240 canvas = self.canvas 241 vspacing = canvas.width / 35. 242 243 thumbnail_width = canvas.width / 13. 244 thumbnail_height = canvas.height / 8. 245 thumbnail_x = canvas.width / 70. 246 thumbnail_y = canvas.height / 60. 247 248 self.thumbnail.position = (thumbnail_x, thumbnail_y, 0) 249 self.thumbnail.size = (thumbnail_width, thumbnail_height) 250 251 text_x = thumbnail_width + vspacing 252 text_y = canvas.height / 45. 253 text_width = canvas.width / 1.9 254 text_height = self.height 255 font_height = text_height * 0.35 256 #font_height = 0.13 257 258 self._description.font_height = font_height 259 self._description.position = (text_x, text_y, 0) 260 self._description.size = (text_width, text_height)
261
262 - def canvas__set(self, canvas):
263 Group.canvas__set(self, canvas) 264 if canvas is not None: 265 self._layout()
266
267 - def _create_text(self):
268 txt = Text() 269 txt.font_family = "Nimbus Sans L" 270 txt.bg_color = (0, 0, 0, 0) 271 txt.alignment = pgm.TEXT_ALIGN_LEFT 272 txt.ellipsize = pgm.TEXT_ELLIPSIZE_MIDDLE 273 txt.opacity = 255 274 return txt
275
276 - def description__set(self, text):
277 self._description.markup = u'<b>%s</b>' % text 278 self._description.visible = True
279 # len(text) > 0 280
281 - def description__get(self):
282 return self._description
283 284 285 286 text_idx = 0 287
288 - def create_item(label):
289 global text_idx 290 text = "%s %s" % (label, text_idx) 291 item = ListItem(description=text, arrow="theme/arrow.png") 292 text_idx += 1 293 return item
294
295 - def update_lists(lists, function):
296 for list_widget in lists: 297 try: 298 function(list_widget) 299 except Exception, exc: 300 print exc
301
302 - def on_key_press(viewport, event, lists):
303 # quit on q or ESC 304 if event.keyval in (pgm.keysyms.q, pgm.keysyms.Escape): 305 pgm.main_quit() 306 307 if event.keyval in (pgm.keysyms.Down, pgm.keysyms.Right): 308 prop = 'selected_item' 309 update_lists(lists, 310 lambda w: setattr(w, prop, getattr(w, prop) + 1)) 311 elif event.keyval in (pgm.keysyms.Up, pgm.keysyms.Left): 312 prop = 'selected_item' 313 update_lists(lists, 314 lambda w: setattr(w, prop, getattr(w, prop) - 1)) 315 elif event.keyval == pgm.keysyms.a: 316 update_lists(lists, 317 lambda w: w.append(create_item("pictures/fluendo.png"))) 318 elif event.keyval == pgm.keysyms.r: 319 update_lists(lists, 320 lambda list_widget: list_widget.pop())
321
322 - def on_delete(viewport, event):
323 pgm.main_quit()
324 325 # OpenGL viewport creation 326 factory = pgm.ViewportFactory('opengl') 327 gl = factory.create() 328 gl.title = 'TextList widget-test' 329 330 # Canvas and image drawable creation 331 canvas = pgm.Canvas() 332 333 # Bind the canvas to the OpenGL viewport 334 gl.set_canvas(canvas) 335 gl.show() 336 337 visible_items = 9 338 items_nb = 20 339 340 ## canvas_bg = Image() 341 ## canvas_bg.size = canvas.size 342 ## canvas_bg.fg_color = (255, 0, 0, 0) 343 ## canvas_bg.visible = True 344 ## canvas.add(pgm.DRAWABLE_MIDDLE, canvas_bg) 345 ## canvas_bg.position = (0, 0, 0) 346 347 top_left = ShadedScrolledList(3, 1, orientation=const.VERTICAL) 348 top_left.top_shade = "theme/gradient-top.png" 349 top_left.bottom_shade = "theme/gradient-bottom.png" 350 351 top_left.canvas = canvas 352 top_left.visible_range_size = visible_items 353 top_left.position = (0.04, 0.65, 0) 354 top_left.width = canvas.width * 0.55 355 top_left.height = canvas.height * 0.72 356 top_left.visible = True 357 358 bg = sliced_image.SlicedImage(top_file="theme/scrollbar-top.png", 359 bottom_file="theme/scrollbar-bottom.png", 360 body_file="theme/scrollbar-body.png") 361 362 cursor = sliced_image.SlicedImage(top_file="theme/cursor-top.png", 363 bottom_file="theme/cursor-bottom.png", 364 body_file="theme/cursor-body.png") 365 366 bar = scrollbar.Scrollbar(items_number=visible_items, 367 background=bg, cursor=cursor, 368 spacing=0.01) 369 #bar.visible = False 370 371 bar.thickness=0.07 372 373 top_left.set_vertical_scrollbar(bar, const.LEFT, spacing=0.02) 374 375 pictos = ("theme/selector-left.png", 376 "theme/selector-right.png", 377 "theme/selector-body.png", 378 "theme/back-selector-left.png", 379 "theme/back-selector-right.png", 380 "theme/back-selector-body.png", 381 382 ) 383 384 list_selector = selector.Selector(orientation=const.HORIZONTAL, *pictos) 385 #list_selector.opacity = 100 386 top_left.set_selector(list_selector) 387 388 lists = [ top_left, 389 ] 390 391 # very very very very 392 label = "this is a long long text" 393 update_lists(lists, lambda w: [ w.append(create_item(label)) 394 for i in xrange(items_nb) ]) 395 update_lists(lists, lambda w: setattr(w, 'selected_item', 0)) 396 397 398 # Let's start a mainloop 399 gl.connect('key-press-event', on_key_press, lists) 400 gl.connect('delete-event', on_delete) 401 pgm.main() 402