[pitivi] viewer: Fix the ratio setting



commit 6c6a9340e0bc0da384910ed8e44f8acf070ec546
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Sun Jan 13 23:30:13 2019 +0100

    viewer: Fix the ratio setting
    
    The `ratio` property was not being set correctly.
    The calculated percent was incorrect for vertical videos.

 pitivi/editorperspective.py |  2 +-
 pitivi/viewer/viewer.py     | 55 +++++++++++++++++++++++----------------------
 2 files changed, 29 insertions(+), 28 deletions(-)
---
diff --git a/pitivi/editorperspective.py b/pitivi/editorperspective.py
index ba5f64c4..a92748fe 100644
--- a/pitivi/editorperspective.py
+++ b/pitivi/editorperspective.py
@@ -679,7 +679,7 @@ class EditorPerspective(Perspective, Loggable):
 
     def _reset_viewer_aspect_ratio(self, project):
         """Resets the viewer aspect ratio."""
-        self.viewer.update_aspect_ratio(project)
+        self.viewer.target.update_aspect_ratio(project)
         self.viewer.timecode_entry.setFramerate(project.videorate)
 
     def _timelineDurationChangedCb(self, timeline, unused_duration):
diff --git a/pitivi/viewer/viewer.py b/pitivi/viewer/viewer.py
index 41ce6f40..5d1e1fe6 100644
--- a/pitivi/viewer/viewer.py
+++ b/pitivi/viewer/viewer.py
@@ -138,7 +138,7 @@ class ViewerContainer(Gtk.Box, Loggable):
         self.pipeline.create_sink()
 
         self.overlay_stack = OverlayStack(self.app, self.pipeline.sink_widget)
-        self.target = ViewerWidget(self.overlay_stack, project=self.app.project_manager.current_project)
+        self.target = ViewerWidget(self.overlay_stack)
 
         if self.docked:
             self.pack_start(self.target, expand=True, fill=True, padding=0)
@@ -344,12 +344,6 @@ class ViewerContainer(Gtk.Box, Loggable):
         self.forward_button.hide()
         self._compactMode = True  # Prevent set_size_request later
 
-    def update_aspect_ratio(self, project):
-        ratio = project.getDAR()
-        self.debug("Updating aspect ratio to %f [%r]", float(ratio), ratio)
-        self.target.ratio = ratio
-        self.target.project = project
-
     def _entryActivateCb(self, unused_entry):
         nanoseconds = self.timecode_entry.getWidgetValue()
         self.app.project_manager.current_project.pipeline.simple_seek(nanoseconds)
@@ -551,34 +545,41 @@ class ViewerContainer(Gtk.Box, Loggable):
 
 
 class ViewerWidget(Gtk.AspectFrame, Loggable):
-    """Widget for displaying a video sink.
+    """Container responsible with enforcing the aspect ratio.
 
     Args:
-        widget (Gtk.Widget): The child widget doing the real work.
-        project (Optional[pitivi.project.Project]): The project providing
-            a video width and height used for snapping the viewer size.
+        video_widget (Gtk.Widget): The child widget doing the real work.
+            Can be an OverlayStack or a sink widget.
     """
 
-    def __init__(self, widget, project=None):
-        # Prevent black frames and flickering while resizing or changing focus:
+    def __init__(self, video_widget):
         Gtk.AspectFrame.__init__(self, xalign=0.5, yalign=0.5, ratio=4 / 3,
                                  border_width=SPACING, obey_child=False)
         Loggable.__init__(self)
 
+        # The width and height used when snapping the child widget size.
+        self.videowidth = 0
+        self.videoheight = 0
+
         # Set the shadow to None, otherwise it will take space and the
         # child widget size snapping will be a bit off.
         self.set_shadow_type(Gtk.ShadowType.NONE)
 
-        # The width and height used when snapping the child widget size.
-        self.videowidth = project.videowidth if project else 0
-        self.videoheight = project.videoheight if project else 0
-
-        self.add(widget)
+        self.add(video_widget)
 
         # We keep the ViewerWidget hidden initially, or the desktop wallpaper
         # would show through the non-double-buffered widget!
         self.hide()
 
+    def update_aspect_ratio(self, project):
+        """Forces the DAR of the project on the child widget."""
+        ratio_fraction = project.getDAR()
+        self.debug("Updating aspect ratio to %r", ratio_fraction)
+        self.props.ratio = float(ratio_fraction)
+
+        self.videowidth = project.videowidth
+        self.videoheight = project.videoheight
+
     def do_get_preferred_width(self):
         minimum, unused_natural = Gtk.AspectFrame.do_get_preferred_width(self)
         # Do not let a chance for Gtk to choose video natural size
@@ -599,20 +600,20 @@ class ViewerWidget(Gtk.AspectFrame, Loggable):
         if not self.videowidth:
             return
 
-        # Calculate the percent of the project size using w/h,
-        # whichever gives a higher precision.
-        if self.props.ratio > 1:
-            percent = allocation.width / self.project.videowidth
+        # Calculate the current scale of the displayed video
+        # using width or height, whichever gives a higher precision.
+        if self.videowidth > self.videoheight:
+            current_scale = allocation.width / self.videowidth
         else:
-            percent = allocation.height / self.height
+            current_scale = allocation.height / self.videoheight
 
         # See if we want to snap the size of the child widget.
         snap = 0
-        for mark in (0.25, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
-            if mark < percent < mark + 0.05:
-                snap = mark
+        for scale in (0.25, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
+            if scale < current_scale < scale + 0.05:
+                snap = scale
                 break
-            if percent < mark:
+            if current_scale < scale:
                 break
         if snap:
             allocation.width = self.videowidth * snap


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