annotate layer/ScrollableImageCache.cpp @ 1079:7ebfb61b1701 spectrogram-minor-refactor

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