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

Source Code for Module elisa.plugins.bad.raval_frontend.raval_widgets.status_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 pgm 
 18  from pgm.graph.group import Group 
 19  from pgm.graph.image import Image 
 20  from pgm.timing import implicit 
 21  import threading 
 22   
23 -class AnimationInProgress(Exception):
24 pass
25
26 -class StatusBar(Group):
27
28 - def __init__(self, width=1.0, height=2.0):
29 Group.__init__(self) 30 31 # FIXME: Group.size is not overriden 32 self._width = width 33 self._height = height 34 35 self._attributes = [] 36 self._widgets = [] 37 self._animation_lock = threading.Lock() 38 # FIXME: there can be more than one animated widget 39 self._current_animated_widget = None 40 41 # mouse support 42 self._drag_zone = Image() 43 Group.add(self, self._drag_zone) 44 self._drag_zone.bg_color = (255, 0, 0, 0) 45 self._drag_zone.size = (width, height) 46 self._drag_zone.visible = True 47 48 # background 49 self._background = Image() 50 Group.add(self, self._background) 51 self._background.bg_color = (0, 255, 0, 0) 52 self._background.layout = pgm.IMAGE_FILLED 53 self._background.interp = pgm.IMAGE_NEAREST 54 self._background.size = (width, height) 55 self._background.visible = True
56
57 - def _compute_position(self, index):
58 x = 0.0 59 y = 0.0 60 z = 0.2 61 62 i = 0 63 for widget in self._widgets[:-1]: 64 if i >= index: 65 break 66 x += widget.static_object.width*0.88 67 z -= 0.02 68 i += 1 69 70 position = (x, y, z) 71 return position
72
73 - def _compute_opacity(self, index):
74 return 255
75
76 - def _compute_attributes(self, index):
77 return (self._compute_position(index), 78 self._compute_opacity(index))
79
80 - def _layout(self):
81 self._attributes = [ self._compute_attributes(i) 82 for i in xrange(len(self)) ]
83
84 - def _update(self):
85 i = 0 86 for widget in self._widgets: 87 widget.opacity = self._attributes[i][1] 88 widget.position = self._attributes[i][0] 89 i += 1
90
91 - def width__get(self):
92 return self._width
93
94 - def width__set(self, width):
95 self._width = width 96 self._update() 97 self._drag_zone.width = width 98 self._background.width = width
99
100 - def height__get(self):
101 return self._height
102
103 - def height__set(self, height):
104 self._height = height 105 self._update() 106 self._drag_zone.height = height 107 self._background.height = height 108 for widget in self._widgets: 109 widget.height = height*0.95 110 widget.regenerate()
111
112 - def size__set(self, size):
113 self.width = size[0] 114 self.height = size[1]
115
116 - def size__get(self):
117 return (self.width, self.height)
118
119 - def __len__(self):
120 return len(self._widgets)
121
122 - def __getitem__(self, index):
123 return self._widgets[index].static_object
124
125 - def _is_visible(self, index, widget):
126 x, y, z = self._compute_position(index) 127 widget_right_corner = x + widget.width 128 list_right_corner = self.x + self.width 129 visible = widget_right_corner <= list_right_corner 130 return visible
131
132 - def insert(self, index, widget):
133 # proportional resize otherwise it's awful 134 k = self._height*0.95 / widget.height 135 widget.height *= k 136 widget.width *= k 137 widget.regenerate() 138 139 if self._animation_lock.locked(): 140 settings = {'duration': 50} 141 self._current_animated_widget.update_animation_settings(**settings) 142 self._animation_lock.release() 143 144 self._animation_lock.acquire(False) 145 146 def done(ctrl): 147 if self._animation_lock.locked(): 148 self._animation_lock.release() 149 self._current_animated_widget = None
150 151 152 animated_widget = implicit.AnimatedObject(widget) 153 animated_widget.setup_next_animations(duration=300, 154 transformation=implicit.DECELERATE, 155 end_callback=done) 156 self._current_animated_widget = animated_widget 157 158 self._widgets.insert(index, animated_widget) 159 160 # FIXME: evil HACK 161 if self._is_visible(index, widget) or True: 162 # FIXME: why adding only here? 163 # more efficient is to add it to the Group and then check that its 164 # bottom right corner x-coordinate is positive and below self.width 165 Group.add(self, widget) 166 167 # FIXME: can be factorised 168 prev_position = self._compute_position(index-1) 169 if len(self._widgets) == 1: 170 prev_position = (-widget.width, prev_position[1], 171 prev_position[2]) 172 173 widget.position = prev_position 174 175 # FIXME: can be factorised 176 if len(self._widgets) == 1: 177 widget.opacity = 0 178 179 self._attributes.insert(index, self._compute_attributes(index)) 180 181 animated_widget.opacity = self._attributes[index][1] 182 183 if len(self._widgets) == 1: 184 # do not animate the first one 185 widget.position = self._attributes[index][0] 186 else: 187 widget.y = self._attributes[index][0][1] 188 widget.z = self._attributes[index][0][2] 189 animated_widget.x = self._attributes[index][0][0] 190 else: 191 self._animation_lock.release() 192 193 widget.visible = True
194
195 - def append(self, widget):
196 self.insert(len(self), widget)
197
198 - def remove(self, widget):
199 static_widgets = [ w.static_object for w in self._widgets ] 200 index = static_widgets.index(widget) 201 self.pop(index)
202 203 # FIXME: default should be -1
204 - def pop(self, index=None):
205 if self._animation_lock.locked(): 206 settings = {'duration': 50} 207 self._current_animated_widget.update_animation_settings(**settings) 208 self._animation_lock.release() 209 210 self._animation_lock.acquire(False) 211 212 if index is None: 213 # pop last item by default 214 index = len(self._widgets) - 1 215 216 def done(ctrl): 217 static = animated_widget.static_object 218 Group.remove(self, static) 219 if self._animation_lock.locked(): 220 self._animation_lock.release() 221 self._current_animated_widget = None
222 223 animated_widget = self._widgets.pop(index) 224 self._current_animated_widget = animated_widget 225 226 settings = {'duration': 300, 227 'transformation': implicit.ACCELERATE, 228 'end_callback': done} 229 animated_widget.setup_next_animations(**settings) 230 231 try: 232 x_offset = self._widgets[index-1].width 233 except IndexError: 234 x_offset = animated_widget.width 235 236 if len(self._widgets) == 0: 237 animated_widget.opacity = 0 238 else: 239 animated_widget.x -= x_offset 240 241 return animated_widget.static_object 242
243 - def head(self):
244 try: 245 widget = self._widgets[-1].static_object 246 except IndexError: 247 widget = None 248 return widget
249
250 - def background__set(self, background_file):
251 self._background.set_from_file(background_file)
252 253 if __name__ == "__main__": 254 import os 255 import gobject 256 import gst 257 from pgm.timing import implicit 258 259
260 - def on_key_press(viewport, event, widget):
261 if event.type == pgm.KEY_PRESS: 262 # quit on q or ESC 263 if event.keyval in (pgm.keysyms.q, pgm.keysyms.Escape): 264 pgm.main_quit() 265 266 elif event.keyval in (pgm.keysyms.Down, pgm.keysyms.Right): 267 path = Image() 268 path.visible = True 269 widget.append(path) 270 271 elif event.keyval in (pgm.keysyms.Up, pgm.keysyms.Left): 272 widget.pop()
273
274 - def on_delete(viewport, event):
275 pgm.main_quit()
276 277 # OpenGL viewport creation 278 factory = pgm.ViewportFactory('opengl') 279 gl = factory.create() 280 gl.title = 'Status bar widget' 281 282 # Canvas and image drawable creation 283 canvas = pgm.Canvas() 284 285 # Bind the canvas to the OpenGL viewport 286 gl.set_canvas(canvas) 287 gl.show() 288 289 status = StatusBar() 290 status.canvas = canvas 291 status.position = (0.0, 0.0, 0.0) 292 status.width = 3.0 293 status.height = 0.2 294 status.visible = True 295 296 # Let's start a mainloop 297 gl.connect('key-press-event', on_key_press, status) 298 gl.connect('delete-event', on_delete) 299 pgm.main() 300