File: circular_list_ng.py

package info (click to toggle)
pigment-python 0.3.12-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,820 kB
  • ctags: 2,955
  • sloc: python: 11,664; sh: 10,131; makefile: 251; ansic: 97
file content (286 lines) | stat: -rw-r--r-- 8,922 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- mode: python; coding: utf-8 -*-
#
# Pigment Python tools
#
# Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import pgm
from pgm.graph.group import Group
from pgm.graph.image import Image
from pgm.timing import implicit

import math

from pgm.widgets.list_ng import List

class CircularList(List):

    def compute_width(self, index):
        return self._width / self._visible_range_size

    def compute_height(self, index):
        return self._height / self._visible_range_size

    def compute_x(self, index):
        offset = math.pi/2.0
        a = index/self._visible_range_size*math.pi*2.0 + offset
        x = (1.0+math.cos(a))/2.0*(self._width-self._widget_width)
        return x

    def compute_y(self, index):
        offset = math.pi/2.0
        a = index/self._visible_range_size*math.pi*2.0 + offset
        y = (1.0+math.sin(a))/2.0*(self._height-self._widget_height)

        epsilon = 1.0
        max_y = 1.0
        middle = self._visible_range_size/2.0

        if index > middle-epsilon and index < middle+epsilon:
            if index > middle:
                y += 1.0-(index-middle)/epsilon*max_y
            elif index <= middle:
                y += 1.0+(index-middle)/epsilon*max_y

        return y

    def compute_z(self, index):
        epsilon = 0.5
        middle = self._visible_range_size/2.0
        if index > middle-epsilon and index < middle+epsilon:
            return 0.1
        else:
            return 0.0

    def compute_opacity(self, index):
        if index < -1 or index > self._visible_range_size:
            return 0

        middle = self._visible_range_size/2.0
        max_opacity = 255
        step = max_opacity / middle

        if index > middle:
            index = -index + 2.0*middle

        opacity = index * step

        opacity = min(opacity, 255)
        opacity = max(opacity, 0)
        return opacity

    def compute_zoom(self, index):
        epsilon = 1.0
        max_zoom = 4.0
        min_zoom = 1.0
        middle = self._visible_range_size/2.0

        if index > middle-epsilon and index < middle+epsilon:
            if index > middle:
                zoom = (epsilon-(index-middle))/epsilon*(max_zoom-min_zoom) + min_zoom
            elif index <= middle:
                zoom = (epsilon+(index-middle))/epsilon*(max_zoom-min_zoom) + min_zoom
        else:
            zoom = 1.0

        zoom = max(zoom, 1.0)
        return zoom

    def layout_widget(self, widget, position):
        zoom = self.compute_zoom(position)
        width = self._widget_width*zoom
        height = self._widget_height*zoom
        x = self.compute_x(position)-(width-self._widget_width)/2.0
        y = self.compute_y(position)-(height-self._widget_height)/2.0
        z = self.compute_z(position)
        opacity = self.compute_opacity(position)

        # update widget properties
        widget.position = (x, y, z)
        widget.size = (width, height)
        widget.opacity = opacity

    def selected_item__set(self, index):
        if index < 0 or index >= len(self.widgets):
            return

        half_size = (self.visible_range_size-1.0)/2.0
        self._selected_item = index

        if self.animated:
            self._animated.visible_range_start = index
        else:
            self.visible_range_start = index

if __name__ == "__main__":
    import pgm
    import gobject
    import gst
    import glob, sys
    from pgm.graph.text import Text
    from pgm.graph.image import Image

    def create_text(label):
        txt = Text()
        txt.label = label
        txt.font_family = "Bitstream DejaVu"
        txt.font_height = 0.225
        txt.fg_color = (255, 255, 255, 255)
        txt.bg_color = (255, 0, 0, 255)
        txt.ellipsize = pgm.TEXT_ELLIPSIZE_END
        txt.visible = True
        return txt

    def create_img(img_file):
        img = Image()
        img.set_from_file(img_file, 512)
        img.fg_color = (255, 255, 255, 255)
        img.bg_color = (100, 200, 100, 155)
        img.bg_color = (0, 0, 0, 0)
        img.visible = True
        return img

    def create_reflection(master_img):
        img = Image()
        img.set_from_image(master_img)
        img.fg_color = (255, 255, 255, 255)
        img.bg_color = (100, 100, 200, 155)
        img.bg_color = (0, 0, 0, 0)
#        img.width = -master_img.width
        img.height = master_img.height
        img.opacity = 30
#        img.x += master_img.width
        img.layout = pgm.IMAGE_SCALED
        img.y += master_img.height
        img.alignment = pgm.IMAGE_TOP
        img.visible = True
        return img

    def create_video(video_uri):
        img = Image()
        img.fg_color = (255, 255, 255, 255)
        img.bg_color = (0, 0, 0, 0)
        img.alignment = pgm.IMAGE_LEFT
        img.visible = True

        # GStreamer pipeline setup
        pipeline = gst.element_factory_make('playbin')
        sink = gst.element_factory_make('pgmimagesink')
        pipeline.set_property('uri', video_uri)
        pipeline.set_property('video-sink', sink)
        sink.set_property('image', img)
        pipeline.set_state(gst.STATE_PLAYING)

        return img


    def on_key_press(viewport, event, gl, widget):
        if event.type == pgm.KEY_PRESS:
            # quit on q or ESC
            if event.keyval == pgm.keysyms.q or \
               event.keyval == pgm.keysyms.Escape:
                pgm.main_quit()
            
            elif event.keyval == pgm.keysyms.f:
                viewport.fullscreen = not viewport.fullscreen

            elif event.keyval == pgm.keysyms.b:
                widget.selected_item = 0

            elif event.keyval == pgm.keysyms.g:
                widget.selected_item = len(widget) - 1

            elif event.keyval == pgm.keysyms.h:
                widget._animated.visible_range_size += 1

            elif event.keyval == pgm.keysyms.n:
                widget._animated.visible_range_size -= 1

            elif event.keyval == pgm.keysyms.Down or \
                 event.keyval == pgm.keysyms.Right:
                widget.selected_item += 1

            elif event.keyval == pgm.keysyms.Up or \
                 event.keyval == pgm.keysyms.Left:
                widget.selected_item -= 1

            elif event.keyval == pgm.keysyms.space:
                #widget.insert(0, create_text("T"))
                def test():
#                    img = create_img("/home/kaleo/dev/pigment/examples/pictures/fluendo.png")
                    """
                    list_widget.insert(0, img)
                    list_widget.pop(len(list_widget)-1)
                    """
                    img = list_widget.pop(0)
                    list_widget.append(img)
                    return True
                gobject.timeout_add(1000, test)
#                list_widget.append(img)

            # remove the currently selected item
            elif event.keyval == pgm.keysyms.Return:
                widget.pop(widget.selected_item)

    def on_delete(viewport, event):
        pgm.main_quit()


    # OpenGL viewport creation
    factory = pgm.ViewportFactory('opengl')
    gl = factory.create()
    gl.title = 'Circular list widget'
    gl.size = (640, 480)

    # Canvas and image drawable creation
    canvas = pgm.Canvas()

    # Bind the canvas to the OpenGL viewport
    gl.set_canvas(canvas)
    gl.show()

    list_widget = CircularList()
    list_widget.position = (0.5, 0.0, 0.0)
    list_widget.width = 3.0
    list_widget.height = 3.0
    list_widget.visible_range_size = 8
    list_widget.visible = True
    list_widget.canvas = canvas

    files = sys.argv[1:]
    for file in files:
        image = create_img(file)
        list_widget.append(image)
        """
        # reflection code
        image.alignment = pgm.IMAGE_BOTTOM
        reflection = create_reflection(image)
        g = Group(canvas, pgm.DRAWABLE_MIDDLE)
        g.add(image)
        g.add(reflection)
        g.visible = True
        list_widget.append(g)
        """
    
    # Let's start a mainloop
    gl.connect('key-press-event',
               on_key_press,
               gl,
               list_widget)
    gl.connect('delete-event', on_delete)
    pgm.main()