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

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

  1  # -*- mode: python; coding: utf-8 -*- 
  2  # 
  3  # Pigment Python tools 
  4  # 
  5  # Copyright © 2006-2008 Fluendo Embedded S.L. 
  6  # 
  7  # This library is free software; you can redistribute it and/or 
  8  # modify it under the terms of the GNU Lesser General Public 
  9  # License as published by the Free Software Foundation; either 
 10  # version 2 of the License, or (at your option) any later version. 
 11  # 
 12  # This library is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 15  # Lesser General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU Lesser General Public 
 18  # License along with this library; if not, write to the 
 19  # Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 20  # Boston, MA 02111-1307, USA. 
 21   
 22  from pgm.timing import implicit 
 23  from pgm.widgets.grid_ng import Grid 
 24   
25 -class GridWithSelector(Grid):
26
27 - def __init__(self):
28 # selector support 29 self._selector = None 30 31 Grid.__init__(self)
32
33 - def lines__set(self, lines):
34 Grid.lines__set(self, lines) 35 self._layout_selector()
36
37 - def visible_range_size__set(self, visible_range_size):
40
41 - def selected_item__set(self, index):
42 lines = int(self._lines) 43 44 if index < 0: 45 index = self.selected_item % lines 46 elif index >= len(self.widgets): 47 column_index = None 48 if index >= len(self.widgets): 49 column_index = self.selected_item % lines 50 else: 51 column_index = index % lines 52 last_column = len(self.widgets) / lines 53 index = lines * last_column + column_index 54 if index >= len(self.widgets): 55 index = lines * (last_column - 1) + column_index 56 57 Grid.selected_item__set(self, index) 58 self._layout_selector()
59
60 - def orientation__set(self, orientation):
61 Grid.orientation__set(self, orientation) 62 self._layout_selector()
63
64 - def selector__set(self, selector):
65 if self._selector != None: 66 self.remove(self._selector) 67 68 self.add(selector) 69 70 self._selector = selector 71 72 self._animated_selector = implicit.AnimatedObject(selector) 73 settings = {'duration': 200, 74 'transformation': implicit.DECELERATE} 75 self._animated_selector.setup_next_animations(**settings) 76 self._animated_selector.mode = implicit.REPLACE 77 78 self._layout_selector() 79 self._selector.visible = True
80
81 - def selector__get(self):
82 return self._selector
83
84 - def _layout_selector(self):
85 if self._selector == None: 86 return 87 88 half_size = (self.visible_range_size - 1.0) / 2.0 89 position = -self._animated.visible_range_start + \ 90 int(self.selected_item/self._lines) 91 line = self.selected_item % self.lines 92 93 # FIXME: this is *not* awsum !1!1 94 width = self._widget_width * 1.003 95 height = self._widget_height * 1.003 96 self._selector.size = (width, height) 97 98 lower_limit = half_size 99 upper_limit = -self._animated.visible_range_start + \ 100 int(len(self)/self._lines) - half_size 101 102 if (position <= lower_limit) or (position >= upper_limit): 103 x = self.compute_x(position, line) - 0.01 104 else: 105 x = self._animated_selector.x 106 107 y = self.compute_y(position, line) - 0.01 108 z = 1.0 109 110 if self._selector.visible and self._animated_selector.opacity != 0: 111 self._animated_selector.position = (x, y, z) 112 else: 113 self._selector.position = (x, y, z)
114
115 - def do_drag_begin(self, x, y, z, button, time):
116 super(GridWithSelector, self).do_drag_begin(x, y, z, button, time) 117 self._animated_selector.opacity = 0
118
119 - def _stop_deceleration(self):
120 super(GridWithSelector, self)._stop_deceleration() 121 self._animated_selector.opacity = 255
122 123 124 if __name__ == "__main__": 125 import pgm 126 import gobject 127 import gst 128 import glob, sys 129 from pgm.graph.text import Text 130 from pgm.graph.image import Image 131 from pgm.graph.group import Group 132 133 from pgm.widgets import const 134
135 - def create_text(label):
136 txt = Text() 137 txt.label = label 138 txt.font_family = "Bitstream DejaVu" 139 txt.font_height = 0.225 140 txt.fg_color = (255, 255, 255, 255) 141 txt.bg_color = (255, 0, 0, 255) 142 txt.ellipsize = pgm.TEXT_ELLIPSIZE_END 143 txt.visible = True 144 return txt
145
146 - def create_img(img_file):
147 img = Image() 148 img.set_from_file(img_file, 512) 149 img.fg_color = (255, 255, 255, 255) 150 img.bg_color = (100, 200, 100, 155) 151 img.bg_color = (0, 0, 0, 0) 152 img.layout = pgm.IMAGE_ZOOMED 153 img.visible = True 154 return img
155
156 - def create_reflection(master_img):
157 img = Image() 158 img.set_from_image(master_img) 159 img.fg_color = (255, 255, 255, 255) 160 img.bg_color = (100, 100, 200, 155) 161 img.bg_color = (0, 0, 0, 0) 162 # img.width = -master_img.width 163 img.height = master_img.height/8.0 164 img.opacity = 30 165 # img.x += master_img.width 166 img.layout = pgm.IMAGE_FILLED 167 img.y += master_img.height 168 img.alignment = pgm.IMAGE_TOP 169 img.visible = True 170 return img
171
172 - def create_video(video_uri):
173 img = Image() 174 img.fg_color = (255, 255, 255, 255) 175 img.bg_color = (0, 0, 0, 0) 176 img.alignment = pgm.IMAGE_LEFT 177 img.visible = True 178 179 # GStreamer pipeline setup 180 pipeline = gst.element_factory_make('playbin') 181 sink = gst.element_factory_make('pgmimagesink') 182 pipeline.set_property('uri', video_uri) 183 pipeline.set_property('video-sink', sink) 184 sink.set_property('image', img) 185 pipeline.set_state(gst.STATE_PLAYING) 186 187 return img
188 189
190 - def on_key_press(viewport, event, gl, widget):
191 if event.type == pgm.KEY_PRESS: 192 # quit on q or ESC 193 if event.keyval == pgm.keysyms.q or \ 194 event.keyval == pgm.keysyms.Escape: 195 pgm.main_quit() 196 197 elif event.keyval == pgm.keysyms.f: 198 viewport.fullscreen = not viewport.fullscreen 199 200 elif event.keyval == pgm.keysyms.Page_Up: 201 widget.selected_item = 0 202 203 elif event.keyval == pgm.keysyms.Page_Down: 204 widget.selected_item = len(widget) - 1 205 206 elif event.keyval == pgm.keysyms.h: 207 widget._animated.visible_range_size += 2 208 209 elif event.keyval == pgm.keysyms.n: 210 widget._animated.visible_range_size -= 2 211 212 elif event.keyval == pgm.keysyms.g: 213 widget._animated.lines += 1.0 214 215 elif event.keyval == pgm.keysyms.b: 216 widget._animated.lines -= 1.0 217 218 elif event.keyval == pgm.keysyms.s: 219 if widget.orientation == const.VERTICAL: 220 widget.orientation = const.HORIZONTAL 221 elif widget.orientation == const.HORIZONTAL: 222 widget.orientation = const.VERTICAL 223 224 elif event.keyval == pgm.keysyms.Right: 225 if widget.orientation == const.VERTICAL: 226 widget.selected_item += 1 227 elif widget.orientation == const.HORIZONTAL: 228 widget.selected_item += widget.lines 229 230 elif event.keyval == pgm.keysyms.Left: 231 if widget.orientation == const.VERTICAL: 232 widget.selected_item -= 1 233 elif widget.orientation == const.HORIZONTAL: 234 widget.selected_item -= widget.lines 235 236 elif event.keyval == pgm.keysyms.Up: 237 if widget.orientation == const.VERTICAL: 238 widget.selected_item -= widget.lines 239 elif widget.orientation == const.HORIZONTAL: 240 widget.selected_item -= 1 241 242 elif event.keyval == pgm.keysyms.Down: 243 if widget.orientation == const.VERTICAL: 244 widget.selected_item += widget.lines 245 elif widget.orientation == const.HORIZONTAL: 246 widget.selected_item += 1 247 248 249 elif event.keyval == pgm.keysyms.space: 250 #widget.insert(0, create_text("T")) 251 def test(): 252 # img = create_img("/home/kaleo/dev/pigment/examples/pictures/fluendo.png") 253 """ 254 widget.insert(0, img) 255 widget.pop(len(widget)-1) 256 """ 257 img = widget.pop(0) 258 widget.append(img) 259 return True
260 gobject.timeout_add(1000, test) 261 # grid.append(img) 262 263 # remove the currently selected item 264 elif event.keyval == pgm.keysyms.Return: 265 widget.pop(widget.selected_item) 266
267 - def on_delete(viewport, event):
268 pgm.main_quit()
269 270 271 # OpenGL viewport creation 272 factory = pgm.ViewportFactory('opengl') 273 gl = factory.create() 274 gl.title = 'Grid with selector widget' 275 276 # Canvas and image drawable creation 277 canvas = pgm.Canvas() 278 279 # Bind the canvas to the OpenGL viewport 280 gl.set_canvas(canvas) 281 gl.show() 282 283 grid = GridSelector() 284 grid.position = (0.5, 0.5, 0.0) 285 grid.orientation = const.HORIZONTAL 286 grid.width = 3.0 287 grid.height = 2.0 288 grid.visible_range_size = 5 289 grid.visible = True 290 grid.canvas = canvas 291 292 """ 293 from cairo_selector import Selector 294 # color = (0.4, 0.4, 0.6) 295 color = (0.9, 0.9, 0.9) 296 selector = Selector(200, 200, color, 15) 297 selector.position = (0.0, 0.0, 1.0) 298 selector.visible = True 299 grid.selector = selector 300 """ 301 302 files = sys.argv[1:] 303 for file in files: 304 image = create_img(file) 305 grid.append(image) 306 """ 307 bg = Image() 308 bg.size = image.size 309 g = Group(canvas, pgm.DRAWABLE_MIDDLE) 310 hmargin = 0.2 311 vmargin = 0.2 312 g.add(image) 313 g.add(bg) 314 image.width *= 1.0 - hmargin 315 image.height *= 1.0 - vmargin 316 image.x += (bg.width-image.width)/2.0 317 image.y += (bg.height-image.height)/2.0 318 g.visible = True 319 320 grid.append(g) 321 """ 322 323 # Let's start a mainloop 324 gl.connect('key-press-event', 325 on_key_press, 326 gl, 327 grid) 328 gl.connect('delete-event', on_delete) 329 pgm.main() 330