Package elisa :: Package plugins :: Package bad :: Package poblenou_frontend :: Package poblenou_widgets :: Module slideshow
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.poblenou_frontend.poblenou_widgets.slideshow

  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  from pgm.graph.group import Group 
 18  from pgm.graph.image import Image 
 19  from pgm.timing.implicit import * 
 20  import pgm 
 21  import os 
 22   
 23   
24 -class Slideshow(Group):
25
26 - def __init__(self, canvas, layer):
27 Group.__init__(self, canvas, layer) 28 29 self._pictures_paths = [] 30 self._current_index = None 31 self._target_index = None 32 self._frame_size = 0.1 33 34 self._current = Image() 35 self._current.bg_color = (0, 0, 0, 0) 36 self._current.visible = True 37 self._animated_current = AnimatedObject(self._current) 38 self._animated_current.mode = REPLACE 39 40 self._hidden = Image() 41 self._hidden.bg_color = (0, 0, 0, 0) 42 self._hidden.visible = True 43 self._animated_hidden = AnimatedObject(self._hidden) 44 self._animated_hidden.mode = REPLACE 45 46 # invisible drawable used to capture mouse events 47 self._mouse_zone = Image() 48 self._mouse_zone.opacity = 0 49 self._mouse_zone.position = (0.0, 0.0, 0.0) 50 self._mouse_zone.visible = True 51 52 self.delay = 500 53 54 self.add(self._current) 55 self.add(self._hidden) 56 self.add(self._mouse_zone) 57 58 self._current_position = (0.0, 0.0, 0.0) 59 self.size = self._current.size 60 61 self._moved = False 62 self._moved_orig_dx = 0.0 63 self._loaded_paths = {self._hidden: "", self._current: ""}
64 65
66 - def delay__set(self, delay):
67 self._animated_current.setup_next_animations(transformation = DECELERATE, 68 duration = delay, 69 end_callback = self._current_ready) 70 71 self._animated_hidden.setup_next_animations(transformation = DECELERATE, 72 duration = delay)
73
74 - def pictures_paths__set(self, pictures_paths):
75 try: 76 if len(self._pictures_paths) > 0: 77 old_path = self._pictures_paths[self._current_index] 78 else: 79 old_path = "" 80 self._pictures_paths = pictures_paths 81 new_path = self._pictures_paths[self._current_index] 82 if new_path != old_path: 83 self._load_picture_from_index(self._current, self._current_index) 84 except: 85 pass 86 87 self._pictures_paths = pictures_paths
88
89 - def _load_picture_from_index(self, drawable, index):
90 try: 91 if index < 0 or index >= len(self._pictures_paths): 92 raise 93 path = self._pictures_paths[index] 94 if self._loaded_paths[drawable] != path: 95 self._loaded_paths[drawable] = path 96 drawable.set_from_file(path, 1024) 97 except: 98 drawable.clear() 99 self._loaded_paths[drawable] = ""
100
101 - def set_path_for_index(self, index, picture_path):
102 self._pictures_paths[index] = picture_path 103 104 if index == self._current_index: 105 self._load_picture_from_index(self._current, index)
106
107 - def length__get(self):
108 return len(self._pictures_paths)
109
110 - def current_index__set(self, index):
111 index = max(0, index) 112 index = min(len(self._pictures_paths)-1, index) 113 114 if len(self._pictures_paths) == 0: 115 return 116 117 self._target_index = index 118 119 if not self._animated_current.is_animated() and \ 120 not self._animated_hidden.is_animated(): 121 self._update_current_index()
122
123 - def current_index__get(self):
124 return self._target_index
125
126 - def _update_current_index(self):
127 if self._current_index == None: 128 self._current_index = self._target_index 129 self._current.opacity = 0 130 self._load_picture_from_index(self._current, self._target_index) 131 self._animated_current.opacity = 255 132 elif self._target_index - self._current_index < 0: 133 width = self._current.width 134 self._hidden.x = self._current.x - width - self._frame_size*width 135 136 self._current_index = self._target_index 137 self._switch_hidden_current(self._previous_position, 138 self._next_position) 139 140 elif self._target_index - self._current_index > 0: 141 width = self._current.width 142 self._hidden.x = self._current.x + width + self._frame_size*width 143 144 self._current_index = self._target_index 145 self._switch_hidden_current(self._next_position, 146 self._previous_position)
147 148
149 - def _switch_hidden_current(self, from_position, to_position):
150 if not self._moved: 151 self._current.position = self._current_position 152 self._hidden.position = from_position 153 else: 154 self._moved = False 155 156 self._current, self._hidden = self._hidden, self._current 157 self._animated_current, self._animated_hidden = self._animated_hidden, self._animated_current 158 159 self._load_picture_from_index(self._current, self._current_index) 160 161 self._animated_current.position = self._current_position 162 self._animated_hidden.position = to_position
163 #self._animated_current.opacity = 255 164 #self._animated_hidden.opacity = 1 165
166 - def _current_ready(self, controller):
168
169 - def size__set(self, size):
170 self._current.size = size 171 self._hidden.size = size 172 173 self._previous_position = (self._current_position[0] - \ 174 self._canvas.width - \ 175 self._frame_size*size[0], \ 176 self._current_position[1], 177 self._current_position[2]) 178 self._next_position = (self._current_position[0] + \ 179 self._canvas.width + \ 180 self._frame_size*size[0], \ 181 self._current_position[1], 182 self._current_position[2]) 183 184 self._mouse_zone.size = size
185
186 - def size__get(self):
187 return self._current.size
188
189 - def width__set(self, width):
190 self.size = (width, self.height)
191
192 - def width__get(self):
193 return self.size[0]
194
195 - def height__set(self, height):
196 self.size = (self.width, height)
197
198 - def height__get(self):
199 return self.size[1]
200
201 - def connect(self, signal, *args):
202 self._mouse_zone.connect(signal, *args)
203
204 - def move(self, dx):
205 # FIXME: this is hacky 206 if not self._moved: 207 self._moved = True 208 self._moved_orig_dx = self._current.x 209 self._animated_current.stop_animations() 210 self._animated_hidden.stop_animations() 211 212 self._current.x = self._moved_orig_dx + dx 213 if self._current.x > 0: 214 self._hidden.x = self._previous_position[0] + self._moved_orig_dx + dx 215 self._load_picture_from_index(self._hidden, self._current_index - 1) 216 elif self._current.x < 0: 217 self._hidden.x = self._next_position[0] + self._moved_orig_dx + dx 218 self._load_picture_from_index(self._hidden, self._current_index + 1)
219
220 - def release(self):
221 # FIXME: this is hacky 222 self._moved = False 223 self._animated_current.position = self._current_position 224 if abs(self._hidden.x - self._previous_position[0]) < \ 225 abs(self._hidden.x - self._next_position[0]): 226 self._animated_hidden.position = self._previous_position 227 else: 228 self._animated_hidden.position = self._next_position
229 230
231 -def main(args):
232 import pgm 233 import gobject 234 235 # Terminate the mainloop on a delete event 236 def on_delete(viewport, event): 237 pgm.main_quit()
238 239 # Handles the key presses 240 def on_key_press(viewport, event, slideshow): 241 if event.keyval == pgm.keysyms.q or event.keyval == pgm.keysyms.Escape: 242 pgm.main_quit() 243 244 elif event.keyval == pgm.keysyms.Right: 245 slideshow.current_index += 1 246 247 elif event.keyval == pgm.keysyms.Left: 248 slideshow.current_index -= 1 249 250 def on_drag_begin(drawable, x, y, z, button, time, slideshow): 251 global start_position 252 start_position = (x, y) 253 slideshow.move(0.0) 254 return True 255 256 def on_drag_motion(drawable, x, y, z, button, time, slideshow): 257 global start_position 258 if start_position != None: 259 dx = x - start_position[0] 260 slideshow.move(dx) 261 return True 262 263 return False 264 265 def on_drag_end(drawable, x, y, z, button, time, slideshow): 266 global start_position 267 if start_position != None: 268 dx = start_position[0] - x 269 dy = start_position[1] - y 270 271 if dx > 0.3: 272 if slideshow.current_index < slideshow.length-1: 273 slideshow.current_index += 1 274 else: 275 slideshow.release() 276 elif dx < -0.3: 277 if slideshow.current_index > 0: 278 slideshow.current_index -= 1 279 else: 280 slideshow.release() 281 else: 282 slideshow.release() 283 284 start_position = None 285 return True 286 287 return False 288 289 290 # OpenGL viewport creation 291 factory = pgm.ViewportFactory('opengl') 292 gl = factory.create() 293 gl.title = 'Implicit animation' 294 295 # Canvas creation 296 canvas = pgm.Canvas() 297 298 # Bind the canvas to the OpenGL viewport 299 gl.set_canvas(canvas) 300 301 # Create and setup the slideshow widget 302 #images = ["examples/pictures/fluendo.png" for i in range(5)] 303 images = args 304 slideshow = Slideshow(canvas, pgm.DRAWABLE_MIDDLE) 305 slideshow.pictures_paths = images 306 slideshow.current_index = 0 307 #slideshow.position = (canvas.width * 0.05, canvas.height * 0.05, 0.0) 308 #slideshow.size = (canvas.width * 0.9, canvas.height * 0.9) 309 slideshow.size = canvas.size 310 slideshow.visible = True 311 312 start_position = None 313 slideshow.connect('drag_begin', on_drag_begin, slideshow) 314 slideshow.connect('drag_motion', on_drag_motion, slideshow) 315 slideshow.connect('drag_end', on_drag_end, slideshow) 316 317 # Let's start the mainloop 318 gobject.timeout_add(15, gl.update) 319 gl.connect('delete-event', on_delete) 320 gl.connect('key-press-event', on_key_press, slideshow) 321 gl.show() 322 pgm.main() 323 324 325 if __name__ == '__main__': 326 import sys 327 sys.exit(main(sys.argv[1:])) 328