comparison layer/ScrollableImageCache.cpp @ 1045:f535f6e5dbb0 alignment-simple

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