[pitivi] ruler: Fix ticks disappearing



commit 5c7586a7ea3f7101e866e0575e4a53f33435de5c
Author: Thejas Kiran P S <thejaskiranps gmail com>
Date:   Sat Jun 4 15:11:59 2022 +0530

    ruler: Fix ticks disappearing
    
    We used to break the drawing of ticks when we encounter ticks which have
    space between them less than MIN_TICK_SPACING_PIXELS. But now we are
    iterating from most frequent to rare ticks. So the 'break' is now changed
    to 'continue' so that it won't completely break the drawing loop.
    
    Fixes #2622

 pitivi/timeline/ruler.py     | 12 +++++++-----
 tests/test_timeline_ruler.py | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 5 deletions(-)
---
diff --git a/pitivi/timeline/ruler.py b/pitivi/timeline/ruler.py
index d6fd4efca..4f90baacd 100644
--- a/pitivi/timeline/ruler.py
+++ b/pitivi/timeline/ruler.py
@@ -34,9 +34,9 @@ from pitivi.utils.ui import time_to_string
 
 
 # Tuples of:
-# - an interval lengths in seconds for which a timestamp will be displayed
+# - an interval duration in seconds for which a timestamp will be displayed
 # - how the ticks should be displayed for this interval:
-#   (count per interval, height ratio) tuples.
+#   (tick interval duration in seconds, height ratio) tuples.
 SCALES = (
     (0.1, ((0.1, 1.0), (0.05, .5), (0.01, .25))),
     (0.2, ((0.2, 1.0), (0.1, .5), (0.05, .25))),
@@ -116,6 +116,8 @@ class ScaleRuler(Gtk.DrawingArea, Loggable):
 
         self.position = 0  # In nanoseconds
 
+        self._scales = [(interval, list(reversed(ticks))) for interval, ticks in SCALES]
+
         # Update colors when theme or color preferences change.
         Gtk.Settings.get_default().connect("notify::gtk-theme-name", self._update_colors_cb)
         Gtk.Settings.get_default().connect("notify::gtk-application-prefer-dark-theme", 
self._update_colors_cb)
@@ -260,7 +262,7 @@ class ScaleRuler(Gtk.DrawingArea, Loggable):
         # when we display millis, they are displayed by themselves.
         min_interval_width = context.text_extents("0:00:00")[2] * 1.3
         zoomratio = self.zoom.zoomratio
-        for interval_seconds, ticks in SCALES:
+        for interval_seconds, ticks in self._scales:
             interval_width = interval_seconds * zoomratio
             if interval_width >= min_interval_width:
                 return interval_width, interval_seconds, ticks
@@ -269,11 +271,11 @@ class ScaleRuler(Gtk.DrawingArea, Loggable):
             (min_interval_width, zoomratio))
 
     def draw_ticks(self, context, offset, spacing, interval_seconds, ticks):
-        for tick_interval, height_ratio in reversed(ticks):
+        for tick_interval, height_ratio in ticks:
             count_per_interval = interval_seconds / tick_interval
             space = spacing / count_per_interval
             if space < MIN_TICK_SPACING_PIXELS:
-                break
+                continue
             paintpos = 0.5 - offset
 
             color = (self._color_normal if height_ratio == 1
diff --git a/tests/test_timeline_ruler.py b/tests/test_timeline_ruler.py
new file mode 100644
index 000000000..d891232a5
--- /dev/null
+++ b/tests/test_timeline_ruler.py
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+# Pitivi video editor
+# Copyright (c) 2022, Thejas Kiran P S <thejaskiranps gmail com>
+#
+# This program 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.1 of the License, or (at your option) any later version.
+#
+# This program 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 program; if not, see <http://www.gnu.org/licenses/>.
+# pylint: disable=protected-access
+"""Tests for the pitivi.timeline.ruler module."""
+from tests import common
+
+
+class TestRuler(common.TestCase):
+    """Tests for the ScaleRuler class."""
+
+    def test_ruler_scales_sorted(self):
+        scales = common.create_timeline_container().ruler._scales
+
+        # Check the scales are sorted correctly.
+        self.assertListEqual(sorted(scales), scales)
+
+        # Check the ticks are sorted correctly.
+        for unused_interval, ticks in scales:
+            self.assertListEqual(sorted(ticks), ticks)


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