ZoomLevel.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #ifndef SV_ZOOM_LEVEL_H
16 #define SV_ZOOM_LEVEL_H
17 
18 #include "BaseTypes.h"
19 
20 #include <ostream>
21 #include <cmath>
22 
26 struct ZoomLevel {
27 
28  enum Zone {
29  FramesPerPixel, // zoomed out (as in classic SV)
30  PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform)
31  };
32 
34  int level;
35 
36  ZoomLevel() : zone(FramesPerPixel), level(1) { }
37  ZoomLevel(Zone z, int lev) : zone(z), level(lev) { }
38 
39  bool operator<(const ZoomLevel &other) const {
40  if (zone == FramesPerPixel) {
41  if (other.zone == zone) {
42  return level < other.level;
43  } else {
44  return false;
45  }
46  } else {
47  if (other.zone == zone) {
48  return level > other.level;
49  } else {
50  return false;
51  }
52  }
53  }
54 
55  bool operator==(const ZoomLevel &other) const {
56  return (zone == other.zone && level == other.level);
57  }
58 
60  if (zone == FramesPerPixel) {
61  return { zone, level + 1 };
62  } else if (level == 1) {
63  return { FramesPerPixel, 2 };
64  } else if (level == 2) {
65  return { FramesPerPixel, 1 };
66  } else {
67  return { zone, level - 1 };
68  }
69  }
70 
72  if (zone == PixelsPerFrame) {
73  return { zone, level + 1 };
74  } else if (level == 1) {
75  return { PixelsPerFrame, 2 };
76  } else {
77  return { zone, level - 1 };
78  }
79  }
80 
85  double framesToPixels(double frames) const {
86  if (zone == PixelsPerFrame) {
87  return frames * level;
88  } else {
89  return frames / level;
90  }
91  }
92 
97  double pixelsToFrames(double pixels) const {
98  if (zone == PixelsPerFrame) {
99  return pixels / level;
100  } else {
101  return pixels * level;
102  }
103  }
104 
108  static ZoomLevel fromRatio(int pixels, sv_frame_t frames) {
109  if (pixels < frames) {
110  return { FramesPerPixel, int(round(double(frames)/pixels)) };
111  } else {
112  int r = int(round(pixels/double(frames)));
113  if (r > 1) {
114  return { PixelsPerFrame, r };
115  } else {
116  return { FramesPerPixel, 1 };
117  }
118  }
119  }
120 };
121 
122 std::ostream &operator<<(std::ostream &s, const ZoomLevel &z);
123 
124 #endif
static ZoomLevel fromRatio(int pixels, sv_frame_t frames)
Return a ZoomLevel that approximates the given ratio of pixels to frames.
Definition: ZoomLevel.h:108
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
double framesToPixels(double frames) const
Inexact conversion.
Definition: ZoomLevel.h:85
bool operator<(const ZoomLevel &other) const
Definition: ZoomLevel.h:39
ZoomLevel decremented() const
Definition: ZoomLevel.h:71
double pixelsToFrames(double pixels) const
Inexact conversion.
Definition: ZoomLevel.h:97
bool operator==(const ZoomLevel &other) const
Definition: ZoomLevel.h:55
ZoomLevel(Zone z, int lev)
Definition: ZoomLevel.h:37
std::ostream & operator<<(std::ostream &s, const ZoomLevel &z)
Definition: ZoomLevel.cpp:17
Zone zone
Definition: ZoomLevel.h:33
Display zoom level.
Definition: ZoomLevel.h:26
int level
Definition: ZoomLevel.h:34
ZoomLevel()
Definition: ZoomLevel.h:36
ZoomLevel incremented() const
Definition: ZoomLevel.h:59