comparison layer/ScrollableImageCache.cpp @ 1148:c0d841cb8ab9 tony-2.0-integration

Merge latest SV 3.0 branch code
author Chris Cannam
date Fri, 19 Aug 2016 15:58:57 +0100
parents c53ed1a6fcbd
children f2f43802718b
comparison
equal deleted inserted replaced
1009:96cf499fad62 1148:c0d841cb8ab9
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 #include "ScrollableImageCache.h"
16
17 #include <iostream>
18 using namespace std;
19
20 //#define DEBUG_SCROLLABLE_IMAGE_CACHE 1
21
22 void
23 ScrollableImageCache::scrollTo(const LayerGeometryProvider *v,
24 sv_frame_t newStartFrame)
25 {
26 int dx = (v->getXForFrame(m_startFrame) -
27 v->getXForFrame(newStartFrame));
28
29 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
30 cerr << "ScrollableImageCache::scrollTo: start frame " << m_startFrame
31 << " -> " << newStartFrame << ", dx = " << dx << endl;
32 #endif
33
34 if (m_startFrame == newStartFrame) {
35 // haven't moved
36 return;
37 }
38
39 m_startFrame = newStartFrame;
40
41 if (!isValid()) {
42 return;
43 }
44
45 int w = m_image.width();
46
47 if (dx == 0) {
48 // haven't moved visibly (even though start frame may have changed)
49 return;
50 }
51
52 if (dx <= -w || dx >= w) {
53 // scrolled entirely off
54 invalidate();
55 return;
56 }
57
58 // dx is in range, cache is scrollable
59
60 int dxp = dx;
61 if (dxp < 0) dxp = -dxp;
62
63 int copylen = (w - dxp) * int(sizeof(QRgb));
64 for (int y = 0; y < m_image.height(); ++y) {
65 QRgb *line = (QRgb *)m_image.scanLine(y);
66 if (dx < 0) {
67 memmove(line, line + dxp, copylen);
68 } else {
69 memmove(line + dxp, line, copylen);
70 }
71 }
72
73 // update valid area
74
75 int px = m_validLeft;
76 int pw = m_validWidth;
77
78 px += dx;
79
80 if (dx < 0) {
81 // we scrolled left
82 if (px < 0) {
83 pw += px;
84 px = 0;
85 if (pw < 0) {
86 pw = 0;
87 }
88 }
89 } else {
90 // we scrolled right
91 if (px + pw > w) {
92 pw = w - px;
93 if (pw < 0) {
94 pw = 0;
95 }
96 }
97 }
98
99 m_validLeft = px;
100 m_validWidth = pw;
101 }
102
103 void
104 ScrollableImageCache::adjustToTouchValidArea(int &left, int &width,
105 bool &isLeftOfValidArea) const
106 {
107 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
108 cerr << "ScrollableImageCache::adjustToTouchValidArea: left " << left
109 << ", width " << width << endl;
110 cerr << "ScrollableImageCache: my left " << m_validLeft
111 << ", width " << m_validWidth << " so right " << (m_validLeft + m_validWidth) << endl;
112 #endif
113 if (left < m_validLeft) {
114 isLeftOfValidArea = true;
115 if (left + width <= m_validLeft + m_validWidth) {
116 width = m_validLeft - left;
117 }
118 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
119 cerr << "ScrollableImageCache: we're left of valid area, adjusted width to " << width << endl;
120 #endif
121 } else {
122 isLeftOfValidArea = false;
123 width = left + width - (m_validLeft + m_validWidth);
124 left = m_validLeft + m_validWidth;
125 if (width < 0) width = 0;
126 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
127 cerr << "ScrollableImageCache: we're right of valid area, adjusted left to " << left << ", width to " << width << endl;
128 #endif
129 }
130 }
131
132 void
133 ScrollableImageCache::drawImage(int left,
134 int width,
135 QImage image,
136 int imageLeft,
137 int imageWidth)
138 {
139 if (image.height() != m_image.height()) {
140 cerr << "ScrollableImageCache::drawImage: ERROR: Supplied image height "
141 << image.height() << " does not match cache height "
142 << m_image.height() << endl;
143 throw std::logic_error("Image height must match cache height in ScrollableImageCache::drawImage");
144 }
145 if (left < 0 || width < 0 || left + width > m_image.width()) {
146 cerr << "ScrollableImageCache::drawImage: ERROR: Target area (left = "
147 << left << ", width = " << width << ", so right = " << left + width
148 << ") out of bounds for cache of width " << m_image.width() << endl;
149 throw std::logic_error("Target area out of bounds in ScrollableImageCache::drawImage");
150 }
151 if (imageLeft < 0 || imageWidth < 0 ||
152 imageLeft + imageWidth > image.width()) {
153 cerr << "ScrollableImageCache::drawImage: ERROR: Source area (left = "
154 << imageLeft << ", width = " << imageWidth << ", so right = "
155 << imageLeft + imageWidth << ") out of bounds for image of "
156 << "width " << image.width() << endl;
157 throw std::logic_error("Source area out of bounds in ScrollableImageCache::drawImage");
158 }
159
160 QPainter painter(&m_image);
161 painter.drawImage(QRect(left, 0, width, m_image.height()),
162 image,
163 QRect(imageLeft, 0, imageWidth, image.height()));
164 painter.end();
165
166 if (!isValid()) {
167 m_validLeft = left;
168 m_validWidth = width;
169 return;
170 }
171
172 if (left < m_validLeft) {
173 if (left + width > m_validLeft + m_validWidth) {
174 // new image completely contains the old valid area --
175 // use the new area as is
176 m_validLeft = left;
177 m_validWidth = width;
178 } else if (left + width < m_validLeft) {
179 // new image completely off left of old valid area --
180 // we can't extend the valid area because the bit in
181 // between is not valid, so must use the new area only
182 m_validLeft = left;
183 m_validWidth = width;
184 } else {
185 // new image overlaps old valid area on left side --
186 // use new left edge, and extend width to existing
187 // right edge
188 m_validWidth = (m_validLeft + m_validWidth) - left;
189 m_validLeft = left;
190 }
191 } else {
192 if (left > m_validLeft + m_validWidth) {
193 // new image completely off right of old valid area --
194 // we can't extend the valid area because the bit in
195 // between is not valid, so must use the new area only
196 m_validLeft = left;
197 m_validWidth = width;
198 } else if (left + width > m_validLeft + m_validWidth) {
199 // new image overlaps old valid area on right side --
200 // use existing left edge, and extend width to new
201 // right edge
202 m_validWidth = (left + width) - m_validLeft;
203 // (m_validLeft unchanged)
204 } else {
205 // new image completely contained within old valid
206 // area -- leave the old area unchanged
207 }
208 }
209 }
210