Chris@1033
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1033
|
2
|
Chris@1033
|
3 /*
|
Chris@1033
|
4 Sonic Visualiser
|
Chris@1033
|
5 An audio file viewer and annotation editor.
|
Chris@1033
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1033
|
7
|
Chris@1033
|
8 This program is free software; you can redistribute it and/or
|
Chris@1033
|
9 modify it under the terms of the GNU General Public License as
|
Chris@1033
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@1033
|
11 License, or (at your option) any later version. See the file
|
Chris@1033
|
12 COPYING included with this distribution for more information.
|
Chris@1033
|
13 */
|
Chris@1033
|
14
|
Chris@1033
|
15 #include "ScrollableImageCache.h"
|
Chris@1033
|
16
|
Chris@1033
|
17 #include <iostream>
|
Chris@1033
|
18 using namespace std;
|
Chris@1033
|
19
|
Chris@1090
|
20 #define DEBUG_SCROLLABLE_IMAGE_CACHE 1
|
Chris@1033
|
21
|
Chris@1033
|
22 void
|
Chris@1090
|
23 ScrollableImageCache::scrollTo(LayerGeometryProvider *v, sv_frame_t newStartFrame)
|
Chris@1033
|
24 {
|
Chris@1079
|
25 if (m_startFrame == newStartFrame) {
|
Chris@1079
|
26 // haven't moved
|
Chris@1079
|
27 return;
|
Chris@1079
|
28 }
|
Chris@1033
|
29
|
Chris@1090
|
30 int dx = (v->getXForFrame(m_startFrame) -
|
Chris@1090
|
31 v->getXForFrame(newStartFrame));
|
Chris@1033
|
32
|
Chris@1033
|
33 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
|
Chris@1033
|
34 cerr << "ScrollableImageCache::scrollTo: start frame " << m_startFrame
|
Chris@1033
|
35 << " -> " << newStartFrame << ", dx = " << dx << endl;
|
Chris@1033
|
36 #endif
|
Chris@1033
|
37
|
Chris@1033
|
38 m_startFrame = newStartFrame;
|
Chris@1033
|
39
|
Chris@1033
|
40 if (!isValid()) {
|
Chris@1033
|
41 return;
|
Chris@1033
|
42 }
|
Chris@1033
|
43
|
Chris@1033
|
44 int w = m_image.width();
|
Chris@1033
|
45
|
Chris@1033
|
46 if (dx == 0) {
|
Chris@1079
|
47 // haven't moved visibly
|
Chris@1033
|
48 return;
|
Chris@1033
|
49 }
|
Chris@1033
|
50
|
Chris@1033
|
51 if (dx <= -w || dx >= w) {
|
Chris@1033
|
52 // scrolled entirely off
|
Chris@1033
|
53 invalidate();
|
Chris@1033
|
54 return;
|
Chris@1033
|
55 }
|
Chris@1033
|
56
|
Chris@1033
|
57 // dx is in range, cache is scrollable
|
Chris@1033
|
58
|
Chris@1033
|
59 int dxp = dx;
|
Chris@1033
|
60 if (dxp < 0) dxp = -dxp;
|
Chris@1033
|
61
|
Chris@1033
|
62 int copylen = (w - dxp) * int(sizeof(QRgb));
|
Chris@1033
|
63 for (int y = 0; y < m_image.height(); ++y) {
|
Chris@1033
|
64 QRgb *line = (QRgb *)m_image.scanLine(y);
|
Chris@1033
|
65 if (dx < 0) {
|
Chris@1033
|
66 memmove(line, line + dxp, copylen);
|
Chris@1033
|
67 } else {
|
Chris@1033
|
68 memmove(line + dxp, line, copylen);
|
Chris@1033
|
69 }
|
Chris@1033
|
70 }
|
Chris@1033
|
71
|
Chris@1033
|
72 // update valid area
|
Chris@1033
|
73
|
Chris@1033
|
74 int px = m_left;
|
Chris@1033
|
75 int pw = m_width;
|
Chris@1033
|
76
|
Chris@1033
|
77 px += dx;
|
Chris@1033
|
78
|
Chris@1033
|
79 if (dx < 0) {
|
Chris@1033
|
80 // we scrolled left
|
Chris@1033
|
81 if (px < 0) {
|
Chris@1033
|
82 pw += px;
|
Chris@1033
|
83 px = 0;
|
Chris@1033
|
84 if (pw < 0) {
|
Chris@1033
|
85 pw = 0;
|
Chris@1033
|
86 }
|
Chris@1033
|
87 }
|
Chris@1033
|
88 } else {
|
Chris@1033
|
89 // we scrolled right
|
Chris@1033
|
90 if (px + pw > w) {
|
Chris@1033
|
91 pw = w - px;
|
Chris@1033
|
92 if (pw < 0) {
|
Chris@1033
|
93 pw = 0;
|
Chris@1033
|
94 }
|
Chris@1033
|
95 }
|
Chris@1033
|
96 }
|
Chris@1033
|
97
|
Chris@1033
|
98 m_left = px;
|
Chris@1033
|
99 m_width = pw;
|
Chris@1033
|
100 }
|
Chris@1033
|
101
|
Chris@1033
|
102 void
|
Chris@1033
|
103 ScrollableImageCache::adjustToTouchValidArea(int &left, int &width,
|
Chris@1033
|
104 bool &isLeftOfValidArea) const
|
Chris@1033
|
105 {
|
Chris@1036
|
106 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
|
Chris@1036
|
107 cerr << "ScrollableImageCache::adjustToTouchValidArea: left " << left
|
Chris@1036
|
108 << ", width " << width << endl;
|
Chris@1036
|
109 cerr << "ScrollableImageCache: my left " << m_left
|
Chris@1036
|
110 << ", width " << m_width << " so right " << (m_left + m_width) << endl;
|
Chris@1036
|
111 #endif
|
Chris@1033
|
112 if (left < m_left) {
|
Chris@1033
|
113 isLeftOfValidArea = true;
|
Chris@1036
|
114 if (left + width <= m_left + m_width) {
|
Chris@1033
|
115 width = m_left - left;
|
Chris@1033
|
116 }
|
Chris@1036
|
117 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
|
Chris@1036
|
118 cerr << "ScrollableImageCache: we're left of valid area, adjusted width to " << width << endl;
|
Chris@1036
|
119 #endif
|
Chris@1033
|
120 } else {
|
Chris@1033
|
121 isLeftOfValidArea = false;
|
Chris@1033
|
122 width = left + width - (m_left + m_width);
|
Chris@1033
|
123 left = m_left + m_width;
|
Chris@1033
|
124 if (width < 0) width = 0;
|
Chris@1036
|
125 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
|
Chris@1036
|
126 cerr << "ScrollableImageCache: we're right of valid area, adjusted left to " << left << ", width to " << width << endl;
|
Chris@1036
|
127 #endif
|
Chris@1033
|
128 }
|
Chris@1033
|
129 }
|
Chris@1033
|
130
|
Chris@1033
|
131 void
|
Chris@1033
|
132 ScrollableImageCache::drawImage(int left,
|
Chris@1033
|
133 int width,
|
Chris@1033
|
134 QImage image,
|
Chris@1033
|
135 int imageLeft,
|
Chris@1033
|
136 int imageWidth)
|
Chris@1033
|
137 {
|
Chris@1033
|
138 if (image.height() != m_image.height()) {
|
Chris@1033
|
139 cerr << "ScrollableImageCache::drawImage: ERROR: Supplied image height "
|
Chris@1033
|
140 << image.height() << " does not match cache height "
|
Chris@1033
|
141 << m_image.height() << endl;
|
Chris@1033
|
142 throw std::logic_error("Image height must match cache height in ScrollableImageCache::drawImage");
|
Chris@1033
|
143 }
|
Chris@1033
|
144 if (left < 0 || width < 0 || left + width > m_image.width()) {
|
Chris@1033
|
145 cerr << "ScrollableImageCache::drawImage: ERROR: Target area (left = "
|
Chris@1040
|
146 << left << ", width = " << width << ", so right = " << left + width
|
Chris@1040
|
147 << ") out of bounds for cache of width " << m_image.width() << endl;
|
Chris@1033
|
148 throw std::logic_error("Target area out of bounds in ScrollableImageCache::drawImage");
|
Chris@1033
|
149 }
|
Chris@1033
|
150 if (imageLeft < 0 || imageWidth < 0 ||
|
Chris@1033
|
151 imageLeft + imageWidth > image.width()) {
|
Chris@1033
|
152 cerr << "ScrollableImageCache::drawImage: ERROR: Source area (left = "
|
Chris@1040
|
153 << imageLeft << ", width = " << imageWidth << ", so right = "
|
Chris@1040
|
154 << imageLeft + imageWidth << ") out of bounds for image of "
|
Chris@1033
|
155 << "width " << image.width() << endl;
|
Chris@1033
|
156 throw std::logic_error("Source area out of bounds in ScrollableImageCache::drawImage");
|
Chris@1033
|
157 }
|
Chris@1033
|
158
|
Chris@1033
|
159 QPainter painter(&m_image);
|
Chris@1033
|
160 painter.drawImage(QRect(left, 0, width, m_image.height()),
|
Chris@1033
|
161 image,
|
Chris@1033
|
162 QRect(imageLeft, 0, imageWidth, image.height()));
|
Chris@1033
|
163 painter.end();
|
Chris@1033
|
164
|
Chris@1033
|
165 if (!isValid()) {
|
Chris@1033
|
166 m_left = left;
|
Chris@1033
|
167 m_width = width;
|
Chris@1033
|
168 return;
|
Chris@1033
|
169 }
|
Chris@1033
|
170
|
Chris@1033
|
171 if (left < m_left) {
|
Chris@1033
|
172 if (left + width > m_left + m_width) {
|
Chris@1033
|
173 // new image completely contains the old valid area --
|
Chris@1033
|
174 // use the new area as is
|
Chris@1033
|
175 m_left = left;
|
Chris@1033
|
176 m_width = width;
|
Chris@1033
|
177 } else if (left + width < m_left) {
|
Chris@1033
|
178 // new image completely off left of old valid area --
|
Chris@1033
|
179 // we can't extend the valid area because the bit in
|
Chris@1033
|
180 // between is not valid, so must use the new area only
|
Chris@1033
|
181 m_left = left;
|
Chris@1033
|
182 m_width = width;
|
Chris@1033
|
183 } else {
|
Chris@1033
|
184 // new image overlaps old valid area on left side --
|
Chris@1033
|
185 // use new left edge, and extend width to existing
|
Chris@1033
|
186 // right edge
|
Chris@1033
|
187 m_width = (m_left + m_width) - left;
|
Chris@1033
|
188 m_left = left;
|
Chris@1033
|
189 }
|
Chris@1033
|
190 } else {
|
Chris@1033
|
191 if (left > m_left + m_width) {
|
Chris@1033
|
192 // new image completely off right of old valid area --
|
Chris@1033
|
193 // we can't extend the valid area because the bit in
|
Chris@1033
|
194 // between is not valid, so must use the new area only
|
Chris@1033
|
195 m_left = left;
|
Chris@1033
|
196 m_width = width;
|
Chris@1033
|
197 } else if (left + width > m_left + m_width) {
|
Chris@1033
|
198 // new image overlaps old valid area on right side --
|
Chris@1033
|
199 // use existing left edge, and extend width to new
|
Chris@1033
|
200 // right edge
|
Chris@1033
|
201 m_width = (left + width) - m_left;
|
Chris@1033
|
202 // (m_left unchanged)
|
Chris@1033
|
203 } else {
|
Chris@1033
|
204 // new image completely contained within old valid
|
Chris@1033
|
205 // area -- leave the old area unchanged
|
Chris@1033
|
206 }
|
Chris@1033
|
207 }
|
Chris@1033
|
208 }
|
Chris@1033
|
209
|