Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mediaview.py
blob: afc7ada090d667f9889d450bbef6c388bbe971e9 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import os
from gettext import gettext as _

from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from gi.repository import GdkX11

import constants
import utils

COLOR_BLACK = Gdk.color_parse('#000000')
COLOR_WHITE = Gdk.color_parse('#ffffff')
COLOR_GREY = Gdk.color_parse('#808080')

class XoIcon(Gtk.Image):
    
    def __init__(self):
        
        super(XoIcon, self).__init__()

    def set_colors(self, stroke, fill):
        
        pixbuf = utils.load_colored_svg('xo-guy.svg', stroke, fill)
        self.set_from_pixbuf(pixbuf)


class InfoView(Gtk.EventBox):
    """
    A metadata view/edit widget, that presents a primary view area in the top
    right and a secondary view area in the bottom left.
    """
    
    __gsignals__ = {
    'primary-allocated': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT,)),
    'secondary-allocated': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT,)),
    'tags-changed': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, (GObject.TYPE_OBJECT,)),
    }

    def __init__(self):
        
        super(InfoView, self).__init__()
        
        self.modify_bg(Gtk.StateType.NORMAL, COLOR_GREY)

        self.connect('size-allocate', self._size_allocate)

        self._outer_vbox = Gtk.Box(orientation = Gtk.Orientation.VERTICAL) # FIXME: spacing=7?
        self.add(self._outer_vbox)

        hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
        self._outer_vbox.pack_start(hbox, True, True, 0)

        inner_vbox = Gtk.Box(orientation = Gtk.Orientation.VERTICAL) # FIXME: spacing=5?
        hbox.pack_start(inner_vbox, True, True, 6)

        author_hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
        inner_vbox.pack_start(author_hbox, False, False, 0)

        label = Gtk.Label()
        label.set_markup('<b>' + _('Author:') + '</b>')
        author_hbox.pack_start(label, False, False, 0)

        self._xo_icon = XoIcon()
        author_hbox.pack_start(self._xo_icon, False, False, 0)

        self._author_label = Gtk.Label()
        author_hbox.pack_start(self._author_label, False, False, 0)

        self._date_label = Gtk.Label()
        self._date_label.set_line_wrap(True)
        alignment = Gtk.Alignment()
        alignment.set(0.0, 0.5, 0.0, 0.0)
        alignment.add(self._date_label)
        inner_vbox.pack_start(alignment, False, False, 0)

        label = Gtk.Label()
        label.set_markup('<b>' + _('Tags:') + '</b>')
        alignment = Gtk.Alignment()
        alignment.set(0.0, 0.5, 0.0, 0.0)
        alignment.add(label)
        inner_vbox.pack_start(alignment, False, False, 0)

        textview = Gtk.TextView()
        self._tags_buffer = textview.get_buffer()
        self._tags_buffer.connect('changed', self._tags_changed)
        inner_vbox.pack_start(textview, True, True, 0)

        # the main viewing widget will be painted exactly on top of this one
        alignment = Gtk.Alignment()
        alignment.set(1.0, 0.0, 0.0, 0.0)
        self._view_bg = Gtk.EventBox()
        self._view_bg.modify_bg(Gtk.StateType.NORMAL, COLOR_BLACK)
        alignment.add(self._view_bg)
        hbox.pack_start(alignment, False, False, 0)

        # the secondary viewing widget will be painted exactly on top of this one
        alignment = Gtk.Alignment()
        alignment.set(0.0, 1.0, 0.0, 0.0)
        self._live_bg = Gtk.EventBox()
        self._live_bg.modify_bg(Gtk.StateType.NORMAL, COLOR_BLACK)
        alignment.add(self._live_bg)
        self._outer_vbox.pack_start(alignment, False, False, 0)

        self.show_all()
        
    def fit_to_allocation(self, allocation):
        
        # main viewing area: 50% of each dimension
        scale = 0.5
        w = int(allocation.width * scale)
        h = int(allocation.height * scale)
        self._view_bg.set_size_request(w, h)

        # live area: 1/4 of each dimension
        scale = 0.25
        w = int(allocation.width * scale)
        h = int(allocation.height * scale)
        self._live_bg.set_size_request(w, h)

    def set_author(self, name, stroke, fill):
        
        self._xo_icon.set_colors(stroke, fill)
        self._author_label.set_text(name)

    def set_date(self, date):
        
        self._date_label.set_markup('<b>' + _('Date:') + '</b> ' + date)

    def set_tags(self, tags):
        
        self._tags_buffer.set_text(tags)

    def _size_allocate(self, widget, allocation):
        
        self.emit('primary-allocated', self._view_bg.get_allocation())
        self.emit('secondary-allocated', self._live_bg.get_allocation())

    def _tags_changed(self, widget):
        
        self.emit('tags-changed', widget)

class VideoBox(Gtk.EventBox):
    """
    A widget with its own window for rendering a gstreamer video sink onto.
    """
    
    def __init__(self):
        
        super(VideoBox, self).__init__()
        
        self.set_double_buffered(False)
        self.set_app_paintable(True)
        self._sink = None
        self._xid = None
        self.connect('realize', self._realize)
        
        self.show_all()
        self.realize()
        
    def _realize(self, widget):
        
        self._xid = self.get_property('window').get_xid()
        
    def do_draw(self, context):
        
        if self._sink:
            self._sink.expose()
            return False
        
        else:
            return True
    
    # can be called from gstreamer thread, must not do any GTK+ stuff
    def set_sink(self, sink):
        
        self._sink = sink
        self._sink.set_window_handle(self._xid)

class FullscreenButton(Gtk.EventBox):
    
    def __init__(self):
        
        super(FullscreenButton, self).__init__()

        path = os.path.join(constants.GFX_PATH, 'max-reduce.svg')
        self._enlarge_pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
        self.width = self._enlarge_pixbuf.get_width()
        self.height = self._enlarge_pixbuf.get_height()

        path = os.path.join(constants.GFX_PATH, 'max-enlarge.svg')
        self._reduce_pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, self.width, self.height)

        self._image = Gtk.Image()
        self.set_enlarge()
        self._image.show()
        self.add(self._image)

    def set_enlarge(self):
        
        self._image.set_from_pixbuf(self._enlarge_pixbuf)

    def set_reduce(self):
        
        self._image.set_from_pixbuf(self._reduce_pixbuf)


class InfoButton(Gtk.EventBox):
    
    def __init__(self):
        
        super(InfoButton, self).__init__()

        path = os.path.join(constants.GFX_PATH, 'corner-info.svg')
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
        self.width = pixbuf.get_width()
        self.height = pixbuf.get_height()

        self._image = Gtk.Image.new_from_pixbuf(pixbuf)
        self._image.show()
        self.add(self._image)


class ImageBox(Gtk.EventBox):
    
    def __init__(self):
        
        super(ImageBox, self).__init__()
        
        self._pixbuf = None
        self._image = Gtk.Image()
        self.add(self._image)

    def show(self):
        
        self._image.show()
        super(ImageBox, self).show()

    def hide(self):
        
        self._image.hide()
        super(ImageBox, self).hide()

    def clear(self):
        
        self._image.clear()
        self._pixbuf = None

    def set_pixbuf(self, pixbuf):
        
        self._pixbuf = pixbuf

    def set_size(self, width, height):
        
        if self._pixbuf:
            if width == self._pixbuf.get_width() and height == self._pixbuf.get_height():
                pixbuf = self._pixbuf
                
            else:
                pixbuf = self._pixbuf.scale_simple(width, height, GdkPixbuf.InterpType.BILINEAR)

            self._image.set_from_pixbuf(pixbuf)

        self._image.set_size_request(width, height)
        self.set_size_request(width, height)

class MediaView(Gtk.EventBox):
    """
    A widget to show the main record UI with a video feed, but with some
    extra features: possibility to show images, information UI about media,
    etc.

    It is made complicated because under the UI design, some widgets need
    to be placed on top of others. In GTK+, this is not trivial. We achieve
    this here by relying on the fact that GDK+ specifically allows us to
    raise specific windows, and by packing all our Z-order-sensitive widgets
    into EventBoxes (which have their own windows).
    """
    
    __gtype_name__ = "MediaView"
    __gsignals__ = {
        'media-clicked': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
        'pip-clicked': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
        'full-clicked': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
        'info-clicked': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, ()),
        'tags-changed': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
        (GObject.TYPE_OBJECT,)),
    }

    MODE_LIVE = 0
    MODE_PHOTO = 1
    MODE_VIDEO = 2
    MODE_STILL = 3
    MODE_INFO_PHOTO = 4
    MODE_INFO_VIDEO = 5

    def __init__(self):
        
        super(MediaView, self).__init__()
        
        self._mode = None
        self._allocation = None
        self._hide_controls_timer = None
        
        self.connect('size-allocate', self._size_allocate)
        self.connect('motion-notify-event', self._motion_notify)
        
        self.set_events(
            Gdk.EventMask.POINTER_MOTION_MASK |
            Gdk.EventMask.POINTER_MOTION_HINT_MASK)

        self._fixed = Gtk.Fixed()
        self.add(self._fixed)

        self._info_view = InfoView()
        self._info_view.connect('primary-allocated', self._info_view_primary_allocated)
        self._info_view.connect('secondary-allocated', self._info_view_secondary_allocated)
        self._info_view.connect('tags-changed', self._info_view_tags_changed)
        self._fixed.put(self._info_view, 0, 0)

        self._image_box = ImageBox()
        self._image_box.connect('button-release-event', self._image_clicked)
        self._fixed.put(self._image_box, 0, 0)

        self._video = VideoBox()
        self._video.connect('button-release-event', self._video_clicked)
        self._fixed.put(self._video, 0, 0)

        self._video2 = VideoBox()
        self._video2.connect('button-release-event', self._video2_clicked)
        self._fixed.put(self._video2, 0, 0)

        self._info_button = InfoButton()
        self._info_button.connect('button-release-event', self._info_clicked)
        self._fixed.put(self._info_button, 0, 0)

        self._full_button = FullscreenButton()
        self._full_button.connect('button-release-event', self._full_clicked)
        self._fixed.put(self._full_button, 0, 0)

        self.show_all()
        self.realize()
        
        self._switch_mode(MediaView.MODE_LIVE)
    
    def _size_allocate(self, widget, allocation):
        # First check if we've already processed an allocation of this size.
        # This is necessary because the operations we do in response to the
        # size allocation cause another size allocation to happen.
        if self._allocation == allocation:
            return

        self._allocation = allocation
        self._place_widgets()

    def _motion_notify(self, widget, event):
        
        if self._hide_controls_timer:
            # remove timer, it will be reprogrammed right after
            GObject.source_remove(self._hide_controls_timer)
            
        else:
            self._show_controls()

        self._hide_controls_timer = GObject.timeout_add(2000, self._hide_controls)

    def _show_controls(self):
        
        if self._mode in (MediaView.MODE_LIVE, MediaView.MODE_VIDEO, MediaView.MODE_PHOTO, MediaView.MODE_STILL):
            self._full_button.show()

        if self._mode in (MediaView.MODE_VIDEO, MediaView.MODE_PHOTO):
            self._info_button.show()

        if self._mode in (MediaView.MODE_VIDEO, MediaView.MODE_PHOTO):
            self._video.show()

    def _hide_controls(self):
        
        if self._hide_controls_timer:
            GObject.source_remove(self._hide_controls_timer)
            self._hide_controls_timer = None

        self._full_button.hide()
        
        if self._mode not in (MediaView.MODE_INFO_PHOTO, MediaView.MODE_INFO_VIDEO):
            self._info_button.hide()

        if self._mode in (MediaView.MODE_VIDEO, MediaView.MODE_PHOTO):
            self._video.hide()

        return False
    
    def _place_widgets(self):
        
        allocation = self._allocation

        self._image_box.hide()
        self._video.hide()
        self._video2.hide()
        self._info_view.hide()
        self._info_button.hide()

        border = 5
        full_button_x = allocation.width - border - self._full_button.width
        full_button_y = border
        self._fixed.move(self._full_button, full_button_x, full_button_y)

        info_x = allocation.width - self._info_button.width
        info_y = allocation.height - self._info_button.height
        self._fixed.move(self._info_button, info_x, info_y)

        if self._mode == MediaView.MODE_LIVE:
            self._fixed.move(self._video, 0, 0)
            self._video.set_size_request(allocation.width, allocation.height)
            self._video.show()
            self._image_box.clear()
            
        elif self._mode == MediaView.MODE_VIDEO:
            self._fixed.move(self._video2, 0, 0)
            self._video2.set_size_request(allocation.width, allocation.height)
            self._video2.show()
            self._image_box.clear()

            vid_h = allocation.height / 6
            vid_w = allocation.width / 6
            self._video.set_size_request(vid_w, vid_h)

            border = 20
            vid_x = border
            vid_y = self.allocation.height - border - vid_h
            self._fixed.move(self._video, vid_x, vid_y)
            
        elif self._mode == MediaView.MODE_PHOTO:
            self._fixed.move(self._image_box, 0, 0)
            self._image_box.set_size(allocation.width, allocation.height)
            self._image_box.show()

            vid_h = allocation.height / 6
            vid_w = allocation.width / 6
            self._video.set_size_request(vid_w, vid_h)

            border = 20
            vid_x = border
            vid_y = self.allocation.height - border - vid_h
            self._fixed.move(self._video, vid_x, vid_y)
            
        elif self._mode == MediaView.MODE_STILL:
            self._fixed.move(self._image_box, 0, 0)
            self._image_box.set_size(allocation.width, allocation.height)
            self._image_box.show()
            
        elif self._mode in (MediaView.MODE_INFO_PHOTO, MediaView.MODE_INFO_VIDEO):
            self._full_button.hide()
            self._info_view.set_size_request(allocation.width, allocation.height)
            self._info_view.fit_to_allocation(allocation)
            self._info_view.show()
            self._raise_widget(self._info_button)
    
    def _info_view_primary_allocated(self, widget, allocation):
        
        if self._mode == MediaView.MODE_INFO_PHOTO:
            self._fixed.move(self._image_box, allocation.x, allocation.y)
            self._image_box.set_size(allocation.width, allocation.height)
            self._raise_widget(self._image_box)
            
        elif self._mode == MediaView.MODE_INFO_VIDEO:
            self._fixed.move(self._video2, allocation.x, allocation.y)
            self._video2.set_size_request(allocation.width, allocation.height)
            self._raise_widget(self._video2)

    def _info_view_secondary_allocated(self, widget, allocation):
        
        if self._mode in (MediaView.MODE_INFO_PHOTO, MediaView.MODE_INFO_VIDEO):
            self._fixed.move(self._video, allocation.x, allocation.y)
            self._video.set_size_request(allocation.width, allocation.height)
            self._raise_widget(self._video)

    def _info_view_tags_changed(self, widget, tbuffer):
        
        self.emit('tags-changed', tbuffer)

    def _switch_mode(self, new_mode):
        
        if self._mode == MediaView.MODE_LIVE and new_mode == MediaView.MODE_LIVE:
            return
        
        self._mode = new_mode

        if self._hide_controls_timer:
            GObject.source_remove(self._hide_controls_timer)
            self._hide_controls_timer = None

        if self._allocation:
            self._place_widgets()

    def _image_clicked(self, widget, event):
        
        self.emit('media-clicked')

    def _video_clicked(self, widget, event):
        
        if self._mode != MediaView.MODE_LIVE:
            self.emit('pip-clicked')

    def _video2_clicked(self, widget, event):
        
        self.emit('media-clicked')

    def _full_clicked(self, widget, event):
        
        self.emit('full-clicked')

    def _info_clicked(self, widget, event):
        
        self.emit('info-clicked')

    def _show_info(self, mode, author, stroke, fill, date, tags):
        
        self._info_view.set_author(author, stroke, fill)
        self._info_view.set_date(date)
        self._info_view.set_tags(tags)
        self._switch_mode(mode)

    def show_info_photo(self, author, stroke, fill, date, tags):
        
        self._show_info(MediaView.MODE_INFO_PHOTO, author, stroke, fill, date, tags)

    def show_info_video(self, author, stroke, fill, date, tags):
        
        self._show_info(MediaView.MODE_INFO_VIDEO, author, stroke, fill, date, tags)

    def set_fullscreen(self, fullscreen):
        
        if self._hide_controls_timer:
            GObject.source_remove(self._hide_controls_timer)
            self._hide_controls_timer = None

        if fullscreen:
            self._full_button.set_reduce()
            
        else:
            self._full_button.set_enlarge()

    def realize_video(self):
        
        self._video.realize()
        self._video2.realize()

    # can be called from gstreamer thread, must not do any GTK+ stuff
    def set_video_sink(self, sink):
        
        self._video.set_sink(sink)
    
    # can be called from gstreamer thread, must not do any GTK+ stuff
    def set_video2_sink(self, sink):
        
        self._video2.set_sink(sink)

    def show_still(self, pixbuf):
        
        # don't modify the original...
        pixbuf = pixbuf.copy()
        pixbuf.saturate_and_pixelate(pixbuf, 0, 0)
        self._image_box.set_pixbuf(pixbuf)
        self._switch_mode(MediaView.MODE_STILL)

    def show_photo(self, path):
        
        if path:
            pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
            self._image_box.set_pixbuf(pixbuf)
            
        self._switch_mode(MediaView.MODE_PHOTO)

    def show_video(self):
        
        self._switch_mode(MediaView.MODE_VIDEO)

    def show_live(self):
        
        self._switch_mode(MediaView.MODE_LIVE)
    
    def show(self):
        
        self._fixed.show()
        super(MediaView, self).show()

    def hide(self):
        
        self._fixed.hide()
        super(MediaView, self).hide()