Chris@1033: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1033: Chris@1033: /* Chris@1033: Sonic Visualiser Chris@1033: An audio file viewer and annotation editor. Chris@1033: Centre for Digital Music, Queen Mary, University of London. Chris@1033: Chris@1033: This program is free software; you can redistribute it and/or Chris@1033: modify it under the terms of the GNU General Public License as Chris@1033: published by the Free Software Foundation; either version 2 of the Chris@1033: License, or (at your option) any later version. See the file Chris@1033: COPYING included with this distribution for more information. Chris@1033: */ Chris@1033: Chris@1033: #include "ScrollableImageCache.h" Chris@1033: Chris@1164: #include "base/HitCount.h" Chris@1164: Chris@1033: #include Chris@1033: using namespace std; Chris@1033: Chris@1143: //#define DEBUG_SCROLLABLE_IMAGE_CACHE 1 Chris@1033: Chris@1033: void Chris@1113: ScrollableImageCache::scrollTo(const LayerGeometryProvider *v, Chris@1113: sv_frame_t newStartFrame) Chris@1033: { Chris@1164: static HitCount count("ScrollableImageCache: scrolling"); Chris@1164: Chris@1122: int dx = (v->getXForFrame(m_startFrame) - Chris@1266: v->getXForFrame(newStartFrame)); Chris@1122: Chris@1122: #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE Chris@1122: cerr << "ScrollableImageCache::scrollTo: start frame " << m_startFrame Chris@1266: << " -> " << newStartFrame << ", dx = " << dx << endl; Chris@1122: #endif Chris@1122: Chris@1079: if (m_startFrame == newStartFrame) { Chris@1266: // haven't moved Chris@1164: count.hit(); Chris@1079: return; Chris@1079: } Chris@1266: Chris@1033: m_startFrame = newStartFrame; Chris@1266: Chris@1033: if (!isValid()) { Chris@1164: count.miss(); Chris@1266: return; Chris@1033: } Chris@1033: Chris@1033: int w = m_image.width(); Chris@1033: Chris@1033: if (dx == 0) { Chris@1266: // haven't moved visibly (even though start frame may have changed) Chris@1164: count.hit(); Chris@1266: return; Chris@1033: } Chris@1033: Chris@1033: if (dx <= -w || dx >= w) { Chris@1266: // scrolled entirely off Chris@1266: invalidate(); Chris@1164: count.miss(); Chris@1266: return; Chris@1033: } Chris@1164: Chris@1164: count.partial(); Chris@1266: Chris@1033: // dx is in range, cache is scrollable Chris@1033: Chris@1033: int dxp = dx; Chris@1033: if (dxp < 0) dxp = -dxp; Chris@1033: Chris@1033: int copylen = (w - dxp) * int(sizeof(QRgb)); Chris@1033: for (int y = 0; y < m_image.height(); ++y) { Chris@1266: QRgb *line = (QRgb *)m_image.scanLine(y); Chris@1266: if (dx < 0) { Chris@1266: memmove(line, line + dxp, copylen); Chris@1266: } else { Chris@1266: memmove(line + dxp, line, copylen); Chris@1266: } Chris@1033: } Chris@1266: Chris@1033: // update valid area Chris@1033: Chris@1118: int px = m_validLeft; Chris@1118: int pw = m_validWidth; Chris@1266: Chris@1033: px += dx; Chris@1266: Chris@1033: if (dx < 0) { Chris@1266: // we scrolled left Chris@1266: if (px < 0) { Chris@1266: pw += px; Chris@1266: px = 0; Chris@1266: if (pw < 0) { Chris@1266: pw = 0; Chris@1266: } Chris@1266: } Chris@1033: } else { Chris@1266: // we scrolled right Chris@1266: if (px + pw > w) { Chris@1266: pw = w - px; Chris@1266: if (pw < 0) { Chris@1266: pw = 0; Chris@1266: } Chris@1266: } Chris@1033: } Chris@1033: Chris@1118: m_validLeft = px; Chris@1118: m_validWidth = pw; Chris@1033: } Chris@1033: Chris@1033: void Chris@1033: ScrollableImageCache::adjustToTouchValidArea(int &left, int &width, Chris@1266: bool &isLeftOfValidArea) const Chris@1033: { Chris@1036: #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE Chris@1036: cerr << "ScrollableImageCache::adjustToTouchValidArea: left " << left Chris@1036: << ", width " << width << endl; Chris@1118: cerr << "ScrollableImageCache: my left " << m_validLeft Chris@1118: << ", width " << m_validWidth << " so right " << (m_validLeft + m_validWidth) << endl; Chris@1036: #endif Chris@1118: if (left < m_validLeft) { Chris@1266: isLeftOfValidArea = true; Chris@1266: if (left + width <= m_validLeft + m_validWidth) { Chris@1266: width = m_validLeft - left; Chris@1266: } Chris@1036: #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE Chris@1036: cerr << "ScrollableImageCache: we're left of valid area, adjusted width to " << width << endl; Chris@1036: #endif Chris@1033: } else { Chris@1266: isLeftOfValidArea = false; Chris@1266: width = left + width - (m_validLeft + m_validWidth); Chris@1266: left = m_validLeft + m_validWidth; Chris@1266: if (width < 0) width = 0; Chris@1036: #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE Chris@1036: cerr << "ScrollableImageCache: we're right of valid area, adjusted left to " << left << ", width to " << width << endl; Chris@1036: #endif Chris@1033: } Chris@1033: } Chris@1033: Chris@1033: void Chris@1033: ScrollableImageCache::drawImage(int left, Chris@1266: int width, Chris@1266: QImage image, Chris@1266: int imageLeft, Chris@1266: int imageWidth) Chris@1033: { Chris@1033: if (image.height() != m_image.height()) { Chris@1266: cerr << "ScrollableImageCache::drawImage: ERROR: Supplied image height " Chris@1266: << image.height() << " does not match cache height " Chris@1266: << m_image.height() << endl; Chris@1266: throw std::logic_error("Image height must match cache height in ScrollableImageCache::drawImage"); Chris@1033: } Chris@1033: if (left < 0 || width < 0 || left + width > m_image.width()) { Chris@1266: cerr << "ScrollableImageCache::drawImage: ERROR: Target area (left = " Chris@1266: << left << ", width = " << width << ", so right = " << left + width Chris@1040: << ") out of bounds for cache of width " << m_image.width() << endl; Chris@1266: throw std::logic_error("Target area out of bounds in ScrollableImageCache::drawImage"); Chris@1033: } Chris@1033: if (imageLeft < 0 || imageWidth < 0 || Chris@1266: imageLeft + imageWidth > image.width()) { Chris@1266: cerr << "ScrollableImageCache::drawImage: ERROR: Source area (left = " Chris@1266: << imageLeft << ", width = " << imageWidth << ", so right = " Chris@1040: << imageLeft + imageWidth << ") out of bounds for image of " Chris@1266: << "width " << image.width() << endl; Chris@1266: throw std::logic_error("Source area out of bounds in ScrollableImageCache::drawImage"); Chris@1033: } Chris@1266: Chris@1033: QPainter painter(&m_image); Chris@1033: painter.drawImage(QRect(left, 0, width, m_image.height()), Chris@1266: image, Chris@1266: QRect(imageLeft, 0, imageWidth, image.height())); Chris@1033: painter.end(); Chris@1033: Chris@1033: if (!isValid()) { Chris@1266: m_validLeft = left; Chris@1266: m_validWidth = width; Chris@1266: return; Chris@1033: } Chris@1266: Chris@1118: if (left < m_validLeft) { Chris@1266: if (left + width > m_validLeft + m_validWidth) { Chris@1266: // new image completely contains the old valid area -- Chris@1266: // use the new area as is Chris@1266: m_validLeft = left; Chris@1266: m_validWidth = width; Chris@1266: } else if (left + width < m_validLeft) { Chris@1266: // new image completely off left of old valid area -- Chris@1266: // we can't extend the valid area because the bit in Chris@1266: // between is not valid, so must use the new area only Chris@1266: m_validLeft = left; Chris@1266: m_validWidth = width; Chris@1266: } else { Chris@1266: // new image overlaps old valid area on left side -- Chris@1266: // use new left edge, and extend width to existing Chris@1266: // right edge Chris@1266: m_validWidth = (m_validLeft + m_validWidth) - left; Chris@1266: m_validLeft = left; Chris@1266: } Chris@1033: } else { Chris@1266: if (left > m_validLeft + m_validWidth) { Chris@1266: // new image completely off right of old valid area -- Chris@1266: // we can't extend the valid area because the bit in Chris@1266: // between is not valid, so must use the new area only Chris@1266: m_validLeft = left; Chris@1266: m_validWidth = width; Chris@1266: } else if (left + width > m_validLeft + m_validWidth) { Chris@1266: // new image overlaps old valid area on right side -- Chris@1266: // use existing left edge, and extend width to new Chris@1266: // right edge Chris@1266: m_validWidth = (left + width) - m_validLeft; Chris@1266: // (m_validLeft unchanged) Chris@1266: } else { Chris@1266: // new image completely contained within old valid Chris@1266: // area -- leave the old area unchanged Chris@1266: } Chris@1033: } Chris@1033: } Chris@1033: