comparison layer/ScrollableMagRangeCache.h @ 1216:dc2af6616c83

Merge from branch 3.0-integration
author Chris Cannam
date Fri, 13 Jan 2017 10:29:50 +0000
parents c53ed1a6fcbd
children a34a2a25907c
comparison
equal deleted inserted replaced
1048:e8102ff5573b 1216:dc2af6616c83
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 SCROLLABLE_MAG_RANGE_CACHE_H
16 #define SCROLLABLE_MAG_RANGE_CACHE_H
17
18 #include "base/BaseTypes.h"
19 #include "base/MagnitudeRange.h"
20
21 #include "LayerGeometryProvider.h"
22
23 /**
24 * A cached set of magnitude range records for a view that scrolls
25 * horizontally, such as a spectrogram. The cache object holds a
26 * magnitude range per column of the view, can report width (likely
27 * the same as the underlying view, but it's the caller's
28 * responsibility to set the size appropriately), can scroll the set
29 * of ranges, and can report and update which columns have had a range
30 * specified.
31 *
32 * The only way to *update* the valid area in a cache is to update the
33 * magnitude range for a column using the sampleColumn call.
34 */
35 class ScrollableMagRangeCache
36 {
37 public:
38 ScrollableMagRangeCache() :
39 m_startFrame(0),
40 m_zoomLevel(0)
41 {}
42
43 void invalidate() {
44 m_ranges = std::vector<MagnitudeRange>(m_ranges.size());
45 }
46
47 int getWidth() const {
48 return int(m_ranges.size());
49 }
50
51 /**
52 * Set the width of the cache in columns. If the new size differs
53 * from the current size, the cache is invalidated.
54 */
55 void resize(int newWidth) {
56 if (getWidth() != newWidth) {
57 m_ranges = std::vector<MagnitudeRange>(newWidth);
58 }
59 }
60
61 int getZoomLevel() const {
62 return m_zoomLevel;
63 }
64
65 /**
66 * Set the zoom level. If the new zoom level differs from the
67 * current one, the cache is invalidated. (Determining whether to
68 * invalidate the cache here is the only thing the zoom level is
69 * used for.)
70 */
71 void setZoomLevel(int zoom) {
72 if (m_zoomLevel != zoom) {
73 m_zoomLevel = zoom;
74 invalidate();
75 }
76 }
77
78 sv_frame_t getStartFrame() const {
79 return m_startFrame;
80 }
81
82 /**
83 * Set the start frame. If the new start frame differs from the
84 * current one, the cache is invalidated. To scroll, i.e. to set
85 * the start frame while retaining cache validity where possible,
86 * use scrollTo() instead.
87 */
88 void setStartFrame(sv_frame_t frame) {
89 if (m_startFrame != frame) {
90 m_startFrame = frame;
91 invalidate();
92 }
93 }
94
95 bool isColumnSet(int column) const {
96 return in_range_for(m_ranges, column) && m_ranges.at(column).isSet();
97 }
98
99 bool areColumnsSet(int x, int count) const {
100 for (int i = 0; i < count; ++i) {
101 if (!isColumnSet(x + i)) return false;
102 }
103 return true;
104 }
105
106 /**
107 * Get the magnitude range for a single column.
108 */
109 MagnitudeRange getRange(int column) const {
110 return m_ranges.at(column);
111 }
112
113 /**
114 * Get the magnitude range for a range of columns.
115 */
116 MagnitudeRange getRange(int x, int count) const;
117
118 /**
119 * Set the new start frame for the cache, according to the
120 * geometry of the supplied LayerGeometryProvider, if possible
121 * also moving along any existing valid data within the cache so
122 * that it continues to be valid for the new start frame.
123 */
124 void scrollTo(const LayerGeometryProvider *v, sv_frame_t newStartFrame);
125
126 /**
127 * Update a column in the cache, by column index. (Column zero is
128 * the first column in the cache, it has nothing to do with any
129 * underlying model that the cache may be used with.)
130 */
131 void sampleColumn(int column, const MagnitudeRange &r);
132
133 private:
134 std::vector<MagnitudeRange> m_ranges;
135 sv_frame_t m_startFrame;
136 int m_zoomLevel;
137 };
138
139 #endif