changeset 1533:53073777e591 zoom

More ZoomLevel updates
author Chris Cannam
date Thu, 20 Sep 2018 10:45:48 +0100
parents 5360f7aba189
children 8f9e9cff141d
files base/ZoomLevel.h
diffstat 1 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/base/ZoomLevel.h	Wed Sep 19 15:41:44 2018 +0100
+++ b/base/ZoomLevel.h	Thu Sep 20 10:45:48 2018 +0100
@@ -16,6 +16,7 @@
 #define SV_ZOOM_LEVEL_H
 
 #include <ostream>
+#include <cmath>
 
 /** Display zoom level. Can be an integer number of samples per pixel,
  *  or an integer number of pixels per sample.
@@ -26,6 +27,7 @@
         FramesPerPixel, // zoomed out (as in classic SV)
         PixelsPerFrame  // zoomed in beyond 1-1 (interpolating the waveform)
     };
+
     Zone zone;
     int level;
 
@@ -74,7 +76,10 @@
         }
     }
 
-    /// Inexact
+    /** Inexact conversion. The result is a whole number if we are
+     *  zoomed in enough (in PixelsPerFrame zone), a fraction
+     *  otherwise.
+     */
     double framesToPixels(double frames) const {
         if (zone == PixelsPerFrame) {
             return frames * level;
@@ -83,7 +88,10 @@
         }
     }
 
-    /// Inexact
+    /** Inexact conversion. The result is a whole number if we are
+     *  zoomed out enough (in FramesPerPixel zone), a fraction
+     *  otherwise.
+     */
     double pixelsToFrames(double pixels) const {
         if (zone == PixelsPerFrame) {
             return pixels / level;
@@ -91,6 +99,22 @@
             return pixels * level;
         }
     }
+
+    /** Return a ZoomLevel that approximates the given ratio of pixels
+     *  to frames.
+     */
+    static ZoomLevel fromRatio(int pixels, int frames) {
+        if (pixels < frames) {
+            return { FramesPerPixel, int(round(double(pixels)/frames)) };
+        } else {
+            int r = int(round(double(frames)/pixels));
+            if (r > 1) {
+                return { PixelsPerFrame, r };
+            } else {
+                return { FramesPerPixel, 1 };
+            }
+        }
+    }
 };
 
 std::ostream &operator<<(std::ostream &s, const ZoomLevel &z);