[pitivi: 4/36] Major rework on code; feaures complete, but still some bugs to fix



commit 702d5816f9cff58c00ee5c277d0bd264b2c600ad
Author: Pier Carteri <pier carteri gmail com>
Date:   Sat May 22 18:09:38 2010 +0200

    Major rework on code; feaures complete, but still some bugs to fix

 pitivi/ui/filechooserpreview.py |  151 +++++++++++++++++++++++++++++----------
 1 files changed, 114 insertions(+), 37 deletions(-)
---
diff --git a/pitivi/ui/filechooserpreview.py b/pitivi/ui/filechooserpreview.py
index f67d7cb..cbd252c 100644
--- a/pitivi/ui/filechooserpreview.py
+++ b/pitivi/ui/filechooserpreview.py
@@ -1,9 +1,12 @@
-import os
 
 import gobject
+gobject.threads_init()
+import gst
 import gtk
+gtk.gdk.threads_init()
 import pango
-import gst
+
+import os
 
 from pitivi.discoverer import Discoverer
 from pitivi.ui.common import factory_name, beautify_stream
@@ -16,6 +19,12 @@ DEFAULT_AUDIO_IMAGE = os.path.join(get_pixmap_dir(), "pitivi-sound.png")
 PREVIEW_WIDTH = 250
 PREVIEW_HEIGHT = 100
 
+def get_playbin():
+    try:
+        return gst.element_factory_make("playbin2", "preview-player")
+    except:
+        return gst.element_factory_make("playbin", "preview-player")
+
 
 class PreviewWidget(gtk.VBox):
 
@@ -30,19 +39,23 @@ class PreviewWidget(gtk.VBox):
         self.discoverer.connect('discovery-done', self._update_preview)
 
         #playbin for play pics
-        self.player = gst.element_factory_make("playbin", "preview-player")
+        self.player = get_playbin()
         bus = self.player.get_bus()
         bus.add_signal_watch()
         bus.connect('message', self._on_bus_message)
         bus.enable_sync_message_emission()
         bus.connect('sync-message::element', self._on_sync_message)
+        bus.connect('message::tag', self._on_tag_found)
         self.__videosink = self.player.get_property("video-sink")
         self.__fakesink = gst.element_factory_make("fakesink", "fakesink")
 
         #some global variables for preview handling
         self.is_playng = False 
         self.time_format = gst.Format(gst.FORMAT_TIME)
-
+        self.original_dims = None
+        self.countinuous_seek = False
+        self.tag_text = ""
+        
         #gui elements:
         #a title label
         self.title = gtk.Label('')
@@ -53,6 +66,7 @@ class PreviewWidget(gtk.VBox):
         
         # a drawing area for video output
         self.preview_video = gtk.DrawingArea()
+        self.preview_video.modify_bg(gtk.STATE_NORMAL, self.preview_video.style.black)
         self.preview_video.set_size_request(PREVIEW_WIDTH, PREVIEW_HEIGHT)
         self.preview_video.hide()
         self.pack_start(self.preview_video, expand=False)
@@ -61,33 +75,50 @@ class PreviewWidget(gtk.VBox):
         self.preview_image = gtk.Image()
         self.preview_image.set_size_request(PREVIEW_WIDTH, PREVIEW_HEIGHT)
         self.preview_image.show()
-        self.pack_start(self.preview_image)
+        self.pack_start(self.preview_image, expand=False)
+
 
+        #button play
+        self.bbox = gtk.HBox()
+        self.b_action = gtk.ToolButton(gtk.STOCK_MEDIA_PLAY)
+        self.b_action.connect("clicked", self._on_start_stop_clicked)
+        self.bbox.pack_start(self.b_action, expand=False)
+        
         #Scale for position handling
         adj = gtk.Adjustment(0.0, 0.00, 100.0, 0.1, 10.0, 10.0)
         self.seeker = gtk.HScale(adj)
         self.seeker.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
+        self.seeker.connect('button-press-event', self._on_seeker_press)
         self.seeker.connect('button-release-event', self._on_seeker_press)
+        self.seeker.connect('motion-notify-event', self._on_motion_notify)
         self.seeker.set_draw_value(False)
         self.seeker.show()
-        self.pack_start(self.seeker, expand=False)
-
-        #buttons for play, seeks ecc
-        self.bbox = gtk.HBox()
-        self.b_action = gtk.ToolButton(gtk.STOCK_MEDIA_PLAY)
-        self.b_action.connect("clicked", self._on_start_stop_clicked)
-        self.bbox.pack_start(self.b_action)
+        self.bbox.pack_start(self.seeker)
+
+        #button zoom in
+        self.b_zoom_in = gtk.ToolButton(gtk.STOCK_ZOOM_IN)
+        self.b_zoom_in.connect("clicked", self._on_zoom_clicked, 1)
+        self.bbox.pack_start(self.b_zoom_in, expand=False)
+        #button zoom out
+        self.b_zoom_out = gtk.ToolButton(gtk.STOCK_ZOOM_OUT)
+        self.b_zoom_out.connect("clicked", self._on_zoom_clicked, -1)
+        self.bbox.pack_start(self.b_zoom_out, expand=False)
+        
         self.bbox.show_all()
+
         self.pack_start(self.bbox, expand=False)
 
         #another label for general info on file
         self.description = gtk.Label('') 
         self.description.set_use_markup(True)
-        self.description.set_justify(gtk.JUSTIFY_CENTER)
+        self.description.set_justify(gtk.JUSTIFY_LEFT)
         self.description.show()
         self.pack_start(self.description, expand=False)
+        #a filler
+        self.pack_start( gtk.Label(''))
 
-
+    def test_event(self, *arg):
+        print arg
 
     def add_preview_request(self, dialogbox):
         """add a preview request """ 
@@ -139,14 +170,18 @@ class PreviewWidget(gtk.VBox):
                 self.preview_image.hide()
                 self.player.set_property("video-sink", self.__videosink)
                 self.player.set_property("uri", self.current_selected_uri) 
+                self.player.set_state(gst.STATE_PAUSED)
                 self.clip_duration = factory.duration
                 w, h = self.__get_best_size(video.par*video.width, video.height)
+                self.original_dims = (w, h)
                 self.preview_video.set_size_request(w, h)
                 self.preview_video.show()
                 self.seeker.show()                
                 self.bbox.show()
-                desc = "<b>Video</b> <i>%dx%d</i>\n%s"
-                desc = desc % (video.par*video.width, video.height, duration)
+                self.b_zoom_in.set_sensitive(True)
+                self.b_zoom_out.set_sensitive(True)
+                desc = "<b>Width/Height</b> <i>%dx%d</i>\n" + "<b>Duration</b> %s \n"
+                self.tag_text = desc % (video.par*video.width, video.height, duration) 
                 self.description.set_markup(desc) 
         else:
             self.preview_video.hide()
@@ -157,17 +192,21 @@ class PreviewWidget(gtk.VBox):
             self.seeker.set_adjustment(adj)
             self.preview_image.set_from_file(DEFAULT_AUDIO_IMAGE)
             self.preview_image.show()
-            desc = "<b>Audio:</b> %d channels at %d <i>Hz</i> \n%s"
-            desc = desc % (audio.channels, audio.rate, duration)
+            desc = "<b>Channels:</b> %d  at %d <i>Hz</i> \n" + "<b>Duration</b> %s \n" 
+            self.tag_text = desc % (audio.channels, audio.rate, duration) 
             self.description.set_markup(desc)
             self.player.set_state(gst.STATE_NULL)
             self.player.set_property("uri", self.current_selected_uri) 
             self.player.set_property("video-sink", self.__fakesink)
-            self.seeker.show()                
+            self.player.set_state(gst.STATE_PAUSED)
+            self.seeker.show()
+            self.b_zoom_in.set_sensitive(False)
+            self.b_zoom_out.set_sensitive(False)
             self.bbox.show()
 
             
     def clear_preview(self):
+        self.seeker.set_value(0)
         self.seeker.hide()
         self.bbox.hide()
         self.title.set_markup("<i>No preview</i>")
@@ -175,18 +214,32 @@ class PreviewWidget(gtk.VBox):
         self.b_action.set_stock_id(gtk.STOCK_MEDIA_PLAY)
         self.player.set_state(gst.STATE_NULL)
         self.is_playing = False
+        self.tag_text = ""
         self.preview_image.set_from_stock(gtk.STOCK_MISSING_IMAGE,
                                         gtk.ICON_SIZE_DIALOG)
         self.preview_image.show()
         self.preview_video.hide()
 
     def _on_seeker_press(self, widget, event): 
-        value = widget.get_value() 
-        if self.is_playing == True:
+        if event.type == gtk.gdk.BUTTON_PRESS:
+            self.countinuous_seek = True
+            if self.is_playing:
+                self.player.set_state(gst.STATE_PAUSED)
+                
+        elif event.type == gtk.gdk.BUTTON_RELEASE:
+            self.countinuous_seek = False
+            value = widget.get_value() 
             time = value * (self.clip_duration / 100) 
             self.player.seek_simple(self.time_format, gst.SEEK_FLAG_FLUSH, time)
+            if self.is_playing:
+                self.player.set_state(gst.STATE_PLAYING)
 
-
+    def _on_motion_notify(self, widget, event):
+        if self.countinuous_seek:
+            value = widget.get_value() 
+            time = value * (self.clip_duration / 100) 
+            self.player.seek_simple(self.time_format, gst.SEEK_FLAG_FLUSH, time)
+            
 
     def _on_bus_message(self, bus, message):
         if message.type == gst.MESSAGE_EOS:
@@ -201,6 +254,7 @@ class PreviewWidget(gtk.VBox):
             err, dbg = message.parse_error()
             print "Error: %s " % err, dbg
 
+
     def _update_position(self, *args):
         if self.is_playing:
             curr_pos = self.player.query_position(self.time_format, None)[0]
@@ -211,7 +265,6 @@ class PreviewWidget(gtk.VBox):
 
 
     def _on_start_stop_clicked(self, button):
-        
         if button.get_stock_id() == gtk.STOCK_MEDIA_PLAY:
             self.player.set_state(gst.STATE_PLAYING)
             gobject.timeout_add(1000, self._update_position)
@@ -222,26 +275,54 @@ class PreviewWidget(gtk.VBox):
             self.is_playing = False
             button.set_stock_id(gtk.STOCK_MEDIA_PLAY)
 
-    def _on_sync_message(self, bus, mess):
-        if mess.structure is None:
-            return
 
-        if mess.structure.get_name() == 'prepare-xwindow-id':
-            imagesink = mess.src
-            imagesink.set_property('force-aspect-ratio', True)
-            gtk.gdk.threads_enter()
-            imagesink.set_xwindow_id(self.preview_video.window.xid)
-            gtk.gdk.threads_leave()
+    def _on_zoom_clicked(self, button, increment):
+        if increment > 0 :
+            w, h = self.preview_video.get_size_request()
+            w *= 1.2
+            h *= 1.2
+        else:
+            w, h = self.preview_video.get_size_request()
+            w *= 0.8
+            h *= 0.8
+            if (w, h) < self.original_dims:
+                (w, h) = self.original_dims
+        self.preview_video.set_size_request(int(w), int(h))
 
 
+    def _on_sync_message(self, bus, mess):
+        if mess.type == gst.MESSAGE_ELEMENT:
+            if mess.structure.get_name() == 'prepare-xwindow-id':
+                sink = mess.src
+                sink.set_property('force-aspect-ratio', True)
+                sink.set_property("handle-expose", True)
+                gtk.gdk.threads_enter()
+                sink.set_xwindow_id(self.preview_video.window.xid)
+                sink.expose()
+                gtk.gdk.threads_leave()
+        return gst.BUS_PASS
+
+
+    def _on_tag_found(self, abus, mess):
+        tag_list = mess.parse_tag()
+        keys = tag_list.keys()
+        keys.sort()
+        for tag in keys:
+            value = unicode(tag_list[tag]).replace('<', ' ').replace('>', ' ')
+            self.tag_text = self.tag_text + '<b>' + tag + '</b> ' \
+                                + value \
+                                + '\n'
+        self.description.set_markup(self.tag_text)
+
 
     def _free_all(self, widget):
         self.player.set_state(gst.STATE_NULL)
         self.is_playing = False
-        #FIXME: the followig line are really needed?
+        #FIXME: the followig lines are really needed?
         del self.player
         del self.preview_cache
 
+
     def __get_best_size(self, width_in, height_in):
         if width_in > height_in:
             if PREVIEW_WIDTH < width_in :
@@ -255,7 +336,3 @@ class PreviewWidget(gtk.VBox):
                 return (w, h)
         return (width_in, height_in)
 
-                
-    
-
-gtk.gdk.threads_init()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]