comparison layer/ScrollableImageCache.h @ 1043:fccee028a522 3.0-integration

Merge from branch "spectrogram-minor-refactor"
author Chris Cannam
date Thu, 04 Feb 2016 11:18:08 +0000
parents 55ac6ac1982e
children 5144d7185fb5
comparison
equal deleted inserted replaced
1042:cd9e76e755bf 1043:fccee028a522
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_IMAGE_CACHE_H
16 #define SCROLLABLE_IMAGE_CACHE_H
17
18 #include "base/BaseTypes.h"
19
20 #include "view/LayerGeometryProvider.h"
21
22 #include <QImage>
23 #include <QRect>
24 #include <QPainter>
25
26 /**
27 * A cached image for a view that scrolls horizontally, primarily the
28 * spectrogram. The cache object holds an image, reports the size of
29 * the image (likely the same as the underlying view, but it's the
30 * caller's responsibility to set the size appropriately), can scroll
31 * the image, and can report and update which contiguous horizontal
32 * range of the image is valid.
33 *
34 * The only way to *update* the valid area in a cache is to draw to it
35 * using the drawImage call.
36 */
37 class ScrollableImageCache
38 {
39 public:
40 ScrollableImageCache(const LayerGeometryProvider *v = 0) :
41 m_v(v),
42 m_left(0),
43 m_width(0),
44 m_startFrame(0),
45 m_zoomLevel(0)
46 {}
47
48 void invalidate() {
49 m_width = 0;
50 }
51
52 bool isValid() const {
53 return m_width > 0;
54 }
55
56 QSize getSize() const {
57 return m_image.size();
58 }
59
60 void resize(QSize newSize) {
61 m_image = QImage(newSize, QImage::Format_ARGB32_Premultiplied);
62 invalidate();
63 }
64
65 int getValidLeft() const {
66 return m_left;
67 }
68
69 int getValidWidth() const {
70 return m_width;
71 }
72
73 int getValidRight() const {
74 return m_left + m_width;
75 }
76
77 QRect getValidArea() const {
78 return QRect(m_left, 0, m_width, m_image.height());
79 }
80
81 int getZoomLevel() const {
82 return m_zoomLevel;
83 }
84
85 void setZoomLevel(int zoom) {
86 m_zoomLevel = zoom;
87 invalidate();
88 }
89
90 sv_frame_t getStartFrame() const {
91 return m_startFrame;
92 }
93
94 /**
95 * Set the start frame and invalidate the cache. To scroll,
96 * i.e. to set the start frame while retaining cache validity
97 * where possible, use scrollTo() instead.
98 */
99 void setStartFrame(sv_frame_t frame) {
100 m_startFrame = frame;
101 invalidate();
102 }
103
104 const QImage &getImage() const {
105 return m_image;
106 }
107
108 /**
109 * Set the new start frame for the cache, if possible also moving
110 * along any existing valid data within the cache so that it
111 * continues to be valid for the new start frame.
112 */
113 void scrollTo(sv_frame_t newStartFrame);
114
115 /**
116 * Take a left coordinate and width describing a region, and
117 * adjust them so that they are contiguous with the cache valid
118 * region and so that the union of the adjusted region with the
119 * cache valid region contains the supplied region.
120 */
121 void adjustToTouchValidArea(int &left, int &width,
122 bool &isLeftOfValidArea) const;
123 /**
124 * Draw from an image onto the cache. The supplied image must have
125 * the same height as the cache and the full height is always
126 * drawn. The left and width parameters determine the target
127 * region of the cache, the imageLeft and imageWidth parameters
128 * the source region of the image.
129 */
130 void drawImage(int left,
131 int width,
132 QImage image,
133 int imageLeft,
134 int imageWidth);
135
136 private:
137 const LayerGeometryProvider *m_v;
138 QImage m_image;
139 int m_left; // of valid region
140 int m_width; // of valid region
141 sv_frame_t m_startFrame;
142 int m_zoomLevel;
143 };
144
145 #endif