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