Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@182: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "Colour3DPlotLayer.h" Chris@0: Chris@128: #include "view/View.h" Chris@0: #include "base/Profiler.h" Chris@197: #include "base/LogRange.h" Chris@197: #include "ColourMapper.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@125: //#define DEBUG_COLOUR_3D_PLOT_LAYER_PAINT 1 Chris@125: Chris@0: Chris@44: Colour3DPlotLayer::Colour3DPlotLayer() : Chris@0: m_model(0), Chris@159: m_cache(0), Chris@197: m_cacheStart(0), Chris@197: m_colourScale(LinearScale), Chris@197: m_colourMap(0), Chris@197: m_normalizeColumns(false), Chris@197: m_normalizeVisibleArea(false) Chris@0: { Chris@44: Chris@0: } Chris@0: Chris@0: Colour3DPlotLayer::~Colour3DPlotLayer() Chris@0: { Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::setModel(const DenseThreeDimensionalModel *model) Chris@0: { Chris@193: if (m_model == model) return; Chris@193: const DenseThreeDimensionalModel *oldModel = m_model; Chris@0: m_model = model; Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@0: connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged())); Chris@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SIGNAL(modelChanged(size_t, size_t))); Chris@0: Chris@0: connect(m_model, SIGNAL(completionChanged()), Chris@0: this, SIGNAL(modelCompletionChanged())); Chris@0: Chris@0: connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid())); Chris@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SLOT(cacheInvalid(size_t, size_t))); Chris@0: Chris@0: emit modelReplaced(); Chris@193: emit sliceableModelReplaced(oldModel, model); Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::cacheInvalid() Chris@0: { Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::cacheInvalid(size_t, size_t) Chris@0: { Chris@0: cacheInvalid(); Chris@0: } Chris@0: Chris@159: Layer::PropertyList Chris@159: Colour3DPlotLayer::getProperties() const Chris@159: { Chris@159: PropertyList list; Chris@197: list.push_back("Colour"); Chris@159: list.push_back("Colour Scale"); Chris@197: list.push_back("Normalize Columns"); Chris@197: list.push_back("Normalize Visible Area"); Chris@159: return list; Chris@159: } Chris@159: Chris@159: QString Chris@159: Colour3DPlotLayer::getPropertyLabel(const PropertyName &name) const Chris@159: { Chris@197: if (name == "Colour") return tr("Colour"); Chris@197: if (name == "Colour Scale") return tr("Scale"); Chris@197: if (name == "Normalize Columns") return tr("Normalize Columns"); Chris@197: if (name == "Normalize Visible Area") return tr("Normalize Visible Area"); Chris@159: return ""; Chris@159: } Chris@159: Chris@159: Layer::PropertyType Chris@159: Colour3DPlotLayer::getPropertyType(const PropertyName &name) const Chris@159: { Chris@197: if (name == "Normalize Columns") return ToggleProperty; Chris@197: if (name == "Normalize Visible Area") return ToggleProperty; Chris@159: return ValueProperty; Chris@159: } Chris@159: Chris@159: QString Chris@159: Colour3DPlotLayer::getPropertyGroupName(const PropertyName &name) const Chris@159: { Chris@197: if (name == "Normalize Columns" || Chris@197: name == "Normalize Visible Area" || Chris@197: name == "Colour Scale") return tr("Scale"); Chris@159: return QString(); Chris@159: } Chris@159: Chris@159: int Chris@159: Colour3DPlotLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: int *min, int *max, int *deflt) const Chris@159: { Chris@216: int val = 0; Chris@159: Chris@216: int garbage0, garbage1, garbage2; Chris@159: if (!min) min = &garbage0; Chris@159: if (!max) max = &garbage1; Chris@216: if (!deflt) deflt = &garbage2; Chris@159: Chris@159: if (name == "Colour Scale") { Chris@159: Chris@159: *min = 0; Chris@197: *max = 2; Chris@216: *deflt = (int)LinearScale; Chris@159: Chris@216: val = (int)m_colourScale; Chris@159: Chris@197: } else if (name == "Colour") { Chris@197: Chris@197: *min = 0; Chris@197: *max = ColourMapper::getColourMapCount() - 1; Chris@216: *deflt = 0; Chris@197: Chris@216: val = m_colourMap; Chris@197: Chris@197: } else if (name == "Normalize Columns") { Chris@197: Chris@216: *deflt = 0; Chris@216: val = (m_normalizeColumns ? 1 : 0); Chris@197: Chris@197: } else if (name == "Normalize Visible Area") { Chris@197: Chris@216: *deflt = 0; Chris@216: val = (m_normalizeVisibleArea ? 1 : 0); Chris@197: Chris@159: } else { Chris@216: val = Layer::getPropertyRangeAndValue(name, min, max, deflt); Chris@159: } Chris@159: Chris@216: return val; Chris@159: } Chris@159: Chris@159: QString Chris@159: Colour3DPlotLayer::getPropertyValueLabel(const PropertyName &name, Chris@159: int value) const Chris@159: { Chris@197: if (name == "Colour") { Chris@197: return ColourMapper::getColourMapName(value); Chris@197: } Chris@159: if (name == "Colour Scale") { Chris@159: switch (value) { Chris@159: default: Chris@198: case 0: return tr("Linear"); Chris@198: case 1: return tr("Log"); Chris@198: case 2: return tr("+/-1"); Chris@159: } Chris@159: } Chris@159: return tr(""); Chris@159: } Chris@159: Chris@159: void Chris@159: Colour3DPlotLayer::setProperty(const PropertyName &name, int value) Chris@159: { Chris@159: if (name == "Colour Scale") { Chris@159: switch (value) { Chris@159: default: Chris@159: case 0: setColourScale(LinearScale); break; Chris@197: case 1: setColourScale(LogScale); break; Chris@197: case 2: setColourScale(PlusMinusOneScale); break; Chris@159: } Chris@197: } else if (name == "Colour") { Chris@197: setColourMap(value); Chris@197: } else if (name == "Normalize Columns") { Chris@197: setNormalizeColumns(value ? true : false); Chris@197: } else if (name == "Normalize Visible Area") { Chris@197: setNormalizeVisibleArea(value ? true : false); Chris@159: } Chris@159: } Chris@159: Chris@159: void Chris@159: Colour3DPlotLayer::setColourScale(ColourScale scale) Chris@159: { Chris@159: if (m_colourScale == scale) return; Chris@159: m_colourScale = scale; Chris@159: cacheInvalid(); Chris@159: emit layerParametersChanged(); Chris@159: } Chris@159: Chris@197: void Chris@197: Colour3DPlotLayer::setColourMap(int map) Chris@197: { Chris@197: if (m_colourMap == map) return; Chris@197: m_colourMap = map; Chris@197: cacheInvalid(); Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: void Chris@197: Colour3DPlotLayer::setNormalizeColumns(bool n) Chris@197: { Chris@197: if (m_normalizeColumns == n) return; Chris@197: m_normalizeColumns = n; Chris@197: cacheInvalid(); Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: bool Chris@197: Colour3DPlotLayer::getNormalizeColumns() const Chris@197: { Chris@197: return m_normalizeColumns; Chris@197: } Chris@197: Chris@197: void Chris@197: Colour3DPlotLayer::setNormalizeVisibleArea(bool n) Chris@197: { Chris@197: if (m_normalizeVisibleArea == n) return; Chris@197: m_normalizeVisibleArea = n; Chris@197: cacheInvalid(); Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: bool Chris@197: Colour3DPlotLayer::getNormalizeVisibleArea() const Chris@197: { Chris@197: return m_normalizeVisibleArea; Chris@197: } Chris@197: Chris@25: bool Chris@44: Colour3DPlotLayer::isLayerScrollable(const View *v) const Chris@25: { Chris@197: if (m_normalizeVisibleArea) return false; Chris@25: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@25: } Chris@25: Chris@25: QString Chris@44: Colour3DPlotLayer::getFeatureDescription(View *v, QPoint &pos) const Chris@25: { Chris@25: if (!m_model) return ""; Chris@25: Chris@25: int x = pos.x(); Chris@25: int y = pos.y(); Chris@25: Chris@25: size_t modelStart = m_model->getStartFrame(); Chris@130: size_t modelResolution = m_model->getResolution(); Chris@25: Chris@159: float srRatio = Chris@159: float(v->getViewManager()->getMainModelSampleRate()) / Chris@159: float(m_model->getSampleRate()); Chris@159: Chris@160: int sx0 = int((v->getFrameForX(x) / srRatio - long(modelStart)) / Chris@160: long(modelResolution)); Chris@25: Chris@160: int f0 = sx0 * modelResolution; Chris@160: int f1 = f0 + modelResolution; Chris@160: Chris@160: float binHeight = float(v->height()) / m_model->getHeight(); Chris@44: int sy = (v->height() - y) / binHeight; Chris@25: Chris@160: float value = m_model->getValueAt(sx0, sy); Chris@159: Chris@159: // std::cerr << "bin value (" << sx0 << "," << sy << ") is " << value << std::endl; Chris@25: Chris@25: QString binName = m_model->getBinName(sy); Chris@25: if (binName == "") binName = QString("[%1]").arg(sy + 1); Chris@25: else binName = QString("%1 [%2]").arg(binName).arg(sy + 1); Chris@25: Chris@25: QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4") Chris@160: .arg(RealTime::frame2RealTime(f0, m_model->getSampleRate()) Chris@25: .toText(true).c_str()) Chris@160: .arg(RealTime::frame2RealTime(f1, m_model->getSampleRate()) Chris@25: .toText(true).c_str()) Chris@25: .arg(binName) Chris@25: .arg(value); Chris@25: Chris@25: return text; Chris@25: } Chris@25: Chris@25: int Chris@159: Colour3DPlotLayer::getColourScaleWidth(QPainter &paint) const Chris@159: { Chris@159: int cw = 20; Chris@159: return cw; Chris@159: } Chris@159: Chris@159: int Chris@44: Colour3DPlotLayer::getVerticalScaleWidth(View *v, QPainter &paint) const Chris@25: { Chris@25: if (!m_model) return 0; Chris@25: Chris@160: QString sampleText = QString("[%1]").arg(m_model->getHeight()); Chris@25: int tw = paint.fontMetrics().width(sampleText); Chris@98: bool another = false; Chris@25: Chris@160: for (size_t i = 0; i < m_model->getHeight(); ++i) { Chris@25: if (m_model->getBinName(i).length() > sampleText.length()) { Chris@25: sampleText = m_model->getBinName(i); Chris@98: another = true; Chris@25: } Chris@25: } Chris@98: if (another) { Chris@25: tw = std::max(tw, paint.fontMetrics().width(sampleText)); Chris@25: } Chris@25: Chris@159: return tw + 13 + getColourScaleWidth(paint); Chris@25: } Chris@25: Chris@25: void Chris@44: Colour3DPlotLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const Chris@25: { Chris@25: if (!m_model) return; Chris@25: Chris@25: int h = rect.height(), w = rect.width(); Chris@160: float binHeight = float(v->height()) / m_model->getHeight(); Chris@25: Chris@159: int cw = getColourScaleWidth(paint); Chris@159: Chris@159: int ch = h - 20; Chris@159: if (ch > 20 && m_cache) { Chris@159: Chris@159: paint.setPen(Qt::black); Chris@159: paint.drawRect(4, 10, cw - 8, ch - 19); Chris@159: Chris@159: for (int y = 0; y < ch - 20; ++y) { Chris@159: QRgb c = m_cache->color(((ch - 20 - y) * 255) / (ch - 20)); Chris@159: // std::cerr << "y = " << y << ": rgb " << qRed(c) << "," << qGreen(c) << "," << qBlue(c) << std::endl; Chris@159: paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c))); Chris@159: paint.drawLine(5, 11 + y, cw - 5, 11 + y); Chris@159: } Chris@159: } Chris@159: Chris@159: paint.setPen(Qt::black); Chris@159: Chris@98: int count = v->height() / paint.fontMetrics().height(); Chris@160: int step = m_model->getHeight() / count; Chris@98: if (step == 0) step = 1; Chris@25: Chris@160: for (size_t i = 0; i < m_model->getHeight(); ++i) { Chris@25: Chris@98: if ((i % step) != 0) continue; Chris@98: Chris@44: int y0 = v->height() - (i * binHeight) - 1; Chris@25: Chris@25: QString text = m_model->getBinName(i); Chris@25: if (text == "") text = QString("[%1]").arg(i + 1); Chris@25: Chris@159: paint.drawLine(cw, y0, w, y0); Chris@25: Chris@98: int cy = y0 - (step * binHeight)/2; Chris@25: int ty = cy + paint.fontMetrics().ascent()/2; Chris@25: Chris@159: paint.drawText(cw + 5, ty, text); Chris@25: } Chris@25: } Chris@25: Chris@0: void Chris@197: Colour3DPlotLayer::getColumn(size_t col, Chris@197: DenseThreeDimensionalModel::Column &values) const Chris@197: { Chris@197: m_model->getColumn(col, values); Chris@197: Chris@224: float colMax = 0.f, colMin = 0.f; Chris@197: Chris@197: float min = 0.f, max = 0.f; Chris@197: if (m_normalizeColumns) { Chris@197: min = m_model->getMinimumLevel(); Chris@197: max = m_model->getMaximumLevel(); Chris@197: } Chris@197: Chris@197: if (m_normalizeColumns) { Chris@197: for (size_t y = 0; y < values.size(); ++y) { Chris@224: if (y == 0 || values[y] > colMax) colMax = values[y]; Chris@224: if (y == 0 || values[y] < colMin) colMin = values[y]; Chris@197: } Chris@224: if (colMin == colMax) colMax = colMin + 1; Chris@197: } Chris@197: Chris@197: for (size_t y = 0; y < values.size(); ++y) { Chris@197: Chris@197: float value = min; Chris@197: Chris@197: value = values[y]; Chris@197: Chris@197: if (m_normalizeColumns) { Chris@224: float norm = (value - colMin) / (colMax - colMin); Chris@224: value = min + (max - min) * norm; Chris@197: } Chris@197: Chris@197: values[y] = value; Chris@197: } Chris@197: } Chris@197: Chris@197: void Chris@197: Colour3DPlotLayer::fillCache(size_t firstBin, size_t lastBin) const Chris@197: { Chris@197: size_t modelStart = m_model->getStartFrame(); Chris@197: size_t modelEnd = m_model->getEndFrame(); Chris@197: size_t modelResolution = m_model->getResolution(); Chris@197: Chris@224: // std::cerr << "Colour3DPlotLayer::fillCache: " << firstBin << " -> " << lastBin << std::endl; Chris@197: Chris@197: if (!m_normalizeVisibleArea || m_normalizeColumns) { Chris@197: firstBin = modelStart / modelResolution; Chris@197: lastBin = modelEnd / modelResolution; Chris@197: } Chris@197: Chris@197: size_t cacheWidth = lastBin - firstBin + 1; Chris@197: size_t cacheHeight = m_model->getHeight(); Chris@197: Chris@197: if (m_cache && Chris@197: (m_cacheStart != firstBin || Chris@197: m_cache->width() != cacheWidth || Chris@197: m_cache->height() != cacheHeight)) { Chris@197: Chris@197: delete m_cache; Chris@197: m_cache = 0; Chris@197: } Chris@197: Chris@197: if (m_cache) return; Chris@197: Chris@197: m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8); Chris@197: m_cacheStart = firstBin; Chris@197: Chris@224: // std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " starting " << m_cacheStart << std::endl; Chris@197: Chris@197: m_cache->setNumColors(256); Chris@197: DenseThreeDimensionalModel::Column values; Chris@197: Chris@197: float min = m_model->getMinimumLevel(); Chris@197: float max = m_model->getMaximumLevel(); Chris@197: Chris@197: if (m_colourScale == LogScale) { Chris@197: LogRange::mapRange(min, max); Chris@197: } else if (m_colourScale == PlusMinusOneScale) { Chris@197: min = -1.f; Chris@197: max = 1.f; Chris@197: } Chris@197: Chris@197: if (max == min) max = min + 1.0; Chris@197: Chris@197: ColourMapper mapper(m_colourMap, 0.f, 255.f); Chris@197: Chris@197: for (int index = 0; index < 256; ++index) { Chris@197: Chris@197: QColor colour = mapper.map(index); Chris@197: m_cache->setColor(index, qRgb(colour.red(), colour.green(), colour.blue())); Chris@197: } Chris@197: Chris@197: m_cache->fill(0); Chris@197: Chris@224: float visibleMax = 0.f, visibleMin = 0.f; Chris@197: Chris@197: if (m_normalizeVisibleArea && !m_normalizeColumns) { Chris@197: Chris@197: for (size_t c = firstBin; c <= lastBin; ++c) { Chris@197: Chris@197: values.clear(); Chris@197: getColumn(c, values); Chris@197: Chris@224: float colMax = 0.f, colMin = 0.f; Chris@197: Chris@197: for (size_t y = 0; y < m_model->getHeight(); ++y) { Chris@197: if (y >= values.size()) break; Chris@197: if (y == 0 || values[y] > colMax) colMax = values[y]; Chris@224: if (y == 0 || values[y] < colMin) colMin = values[y]; Chris@197: } Chris@197: Chris@197: if (c == firstBin || colMax > visibleMax) visibleMax = colMax; Chris@224: if (c == firstBin || colMin < visibleMin) visibleMin = colMin; Chris@197: } Chris@197: } Chris@197: Chris@224: if (visibleMin == visibleMax) visibleMax = visibleMin + 1; Chris@224: Chris@197: for (size_t c = firstBin; c <= lastBin; ++c) { Chris@197: Chris@197: values.clear(); Chris@197: getColumn(c, values); Chris@197: Chris@197: for (size_t y = 0; y < m_model->getHeight(); ++y) { Chris@197: Chris@197: float value = min; Chris@197: if (y < values.size()) { Chris@197: value = values[y]; Chris@197: } Chris@197: Chris@197: if (m_normalizeVisibleArea && !m_normalizeColumns) { Chris@224: float norm = (value - visibleMin) / (visibleMax - visibleMin); Chris@224: value = min + (max - min) * norm; Chris@224: } Chris@224: Chris@224: if (m_colourScale == LogScale) { Chris@224: value = LogRange::map(value); Chris@197: } Chris@197: Chris@197: int pixel = int(((value - min) * 256) / (max - min)); Chris@197: if (pixel < 0) pixel = 0; Chris@197: if (pixel > 255) pixel = 255; Chris@197: Chris@197: m_cache->setPixel(c - firstBin, y, pixel); Chris@197: } Chris@197: } Chris@197: } Chris@197: Chris@197: void Chris@44: Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@0: // Profiler profiler("Colour3DPlotLayer::paint"); Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@125: std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl; Chris@125: #endif Chris@0: Chris@0: int completion = 0; Chris@0: if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) { Chris@0: if (completion > 0) { Chris@44: paint.fillRect(0, 10, v->width() * completion / 100, Chris@0: 10, QColor(120, 120, 120)); Chris@0: } Chris@0: return; Chris@0: } Chris@0: Chris@197: if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect(); Chris@197: Chris@0: size_t modelStart = m_model->getStartFrame(); Chris@0: size_t modelEnd = m_model->getEndFrame(); Chris@130: size_t modelResolution = m_model->getResolution(); Chris@0: Chris@197: // The cache is from the model's start frame to the model's end Chris@197: // frame at the model's window increment frames per pixel. We Chris@197: // want to draw from our start frame + x0 * zoomLevel to our start Chris@197: // frame + x1 * zoomLevel at zoomLevel frames per pixel. Chris@12: Chris@197: // We have quite different paint mechanisms for rendering "large" Chris@197: // bins (more than one bin per pixel in both directions) and Chris@197: // "small". This is "large"; see paintDense below for "small". Chris@12: Chris@197: int x0 = rect.left(); Chris@197: int x1 = rect.right() + 1; Chris@12: Chris@197: int h = v->height(); Chris@0: Chris@197: float srRatio = Chris@197: float(v->getViewManager()->getMainModelSampleRate()) / Chris@197: float(m_model->getSampleRate()); Chris@0: Chris@197: int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart)) Chris@197: / long(modelResolution)); Chris@197: int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart)) Chris@197: / long(modelResolution)); Chris@197: int sh = m_model->getHeight(); Chris@159: Chris@197: if (sx0 > 0) --sx0; Chris@197: fillCache(sx0 < 0 ? 0 : sx0, Chris@197: sx1 < 0 ? 0 : sx1); Chris@0: Chris@160: if (m_model->getHeight() >= v->height() || Chris@159: modelResolution < v->getZoomLevel() / 2) { Chris@98: paintDense(v, paint, rect); Chris@98: return; Chris@98: } Chris@98: Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@0: std::cerr << "Colour3DPlotLayer::paint: w " << w << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sw << ", sh " << sh << std::endl; Chris@130: std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl; Chris@125: #endif Chris@0: Chris@25: QPoint illuminatePos; Chris@44: bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos); Chris@54: char labelbuf[10]; Chris@25: Chris@197: for (int sx = sx0; sx <= sx1; ++sx) { Chris@0: Chris@197: int scx = 0; Chris@197: if (sx > m_cacheStart) scx = sx - m_cacheStart; Chris@197: Chris@130: int fx = sx * int(modelResolution); Chris@0: Chris@130: if (fx + modelResolution < int(modelStart) || Chris@0: fx > int(modelEnd)) continue; Chris@0: Chris@159: int rx0 = v->getXForFrame((fx + int(modelStart)) * srRatio); Chris@159: int rx1 = v->getXForFrame((fx + int(modelStart) + int(modelResolution) + 1) * srRatio); Chris@54: Chris@159: int rw = rx1 - rx0; Chris@159: if (rw < 1) rw = 1; Chris@54: Chris@159: bool showLabel = (rw > 10 && Chris@159: paint.fontMetrics().width("0.000000") < rw - 3 && Chris@54: paint.fontMetrics().height() < (h / sh)); Chris@98: Chris@0: for (int sy = 0; sy < sh; ++sy) { Chris@0: Chris@0: int ry0 = h - (sy * h) / sh - 1; Chris@0: QRgb pixel = qRgb(255, 255, 255); Chris@197: if (scx >= 0 && scx < m_cache->width() && Chris@0: sy >= 0 && sy < m_cache->height()) { Chris@197: pixel = m_cache->pixel(scx, sy); Chris@0: } Chris@0: Chris@159: QRect r(rx0, ry0 - h / sh - 1, rw, h / sh + 1); Chris@159: Chris@159: if (rw == 1) { Chris@159: paint.setPen(pixel); Chris@159: paint.setBrush(Qt::NoBrush); Chris@159: paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1); Chris@159: continue; Chris@159: } Chris@159: Chris@0: QColor pen(255, 255, 255, 80); Chris@0: QColor brush(pixel); Chris@159: Chris@159: if (rw > 3 && r.height() > 3) { Chris@159: brush.setAlpha(160); Chris@159: } Chris@159: Chris@0: paint.setPen(Qt::NoPen); Chris@0: paint.setBrush(brush); Chris@0: Chris@25: if (illuminate) { Chris@25: if (r.contains(illuminatePos)) { Chris@25: paint.setPen(Qt::black);//!!! Chris@25: } Chris@25: } Chris@76: Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@159: std::cerr << "rect " << r.x() << "," << r.y() << " " Chris@159: << r.width() << "x" << r.height() << std::endl; Chris@125: #endif Chris@25: Chris@25: paint.drawRect(r); Chris@0: Chris@54: if (showLabel) { Chris@197: if (scx >= 0 && scx < m_cache->width() && Chris@54: sy >= 0 && sy < m_cache->height()) { Chris@197: float value = m_model->getValueAt(scx, sy); Chris@54: sprintf(labelbuf, "%06f", value); Chris@54: QString text(labelbuf); Chris@54: paint.setPen(Qt::white); Chris@54: paint.drawText(rx0 + 2, Chris@54: ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(), Chris@54: text); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@98: } Chris@0: Chris@98: void Chris@98: Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const Chris@98: { Chris@98: long startFrame = v->getStartFrame(); Chris@98: int zoomLevel = v->getZoomLevel(); Chris@0: Chris@98: size_t modelStart = m_model->getStartFrame(); Chris@98: size_t modelEnd = m_model->getEndFrame(); Chris@130: size_t modelResolution = m_model->getResolution(); Chris@0: Chris@159: float srRatio = Chris@159: float(v->getViewManager()->getMainModelSampleRate()) / Chris@159: float(m_model->getSampleRate()); Chris@159: Chris@98: int x0 = rect.left(); Chris@98: int x1 = rect.right() + 1; Chris@0: Chris@98: int w = x1 - x0; Chris@98: int h = v->height(); Chris@160: int sh = m_model->getHeight(); Chris@98: Chris@98: QImage img(w, h, QImage::Format_RGB32); Chris@98: Chris@98: for (int x = x0; x < x1; ++x) { Chris@98: Chris@159: long xf = v->getFrameForX(x) / srRatio; Chris@105: if (xf < 0) { Chris@105: for (int y = 0; y < h; ++y) { Chris@105: img.setPixel(x - x0, y, m_cache->color(0)); Chris@105: } Chris@105: continue; Chris@105: } Chris@105: Chris@130: float sx0 = (float(xf) - modelStart) / modelResolution; Chris@159: float sx1 = (float(v->getFrameForX(x+1) / srRatio) - modelStart) / modelResolution; Chris@98: Chris@98: int sx0i = int(sx0 + 0.001); Chris@98: int sx1i = int(sx1); Chris@98: Chris@98: for (int y = 0; y < h; ++y) { Chris@98: Chris@98: float sy0 = (float(h - y - 1) * sh) / h; Chris@98: float sy1 = (float(h - y) * sh) / h; Chris@98: Chris@98: int sy0i = int(sy0 + 0.001); Chris@98: int sy1i = int(sy1); Chris@98: Chris@98: float mag = 0.0, div = 0.0; Chris@159: int max = 0; Chris@98: Chris@98: for (int sx = sx0i; sx <= sx1i; ++sx) { Chris@98: Chris@197: int scx = 0; Chris@197: if (sx > m_cacheStart) scx = sx - m_cacheStart; Chris@197: Chris@197: if (scx < 0 || scx >= m_cache->width()) continue; Chris@98: Chris@98: for (int sy = sy0i; sy <= sy1i; ++sy) { Chris@98: Chris@98: if (sy < 0 || sy >= m_cache->height()) continue; Chris@98: Chris@98: float prop = 1.0; Chris@98: if (sx == sx0i) prop *= (sx + 1) - sx0; Chris@98: if (sx == sx1i) prop *= sx1 - sx; Chris@98: if (sy == sy0i) prop *= (sy + 1) - sy0; Chris@98: if (sy == sy1i) prop *= sy1 - sy; Chris@98: Chris@197: mag += prop * m_cache->pixelIndex(scx, sy); Chris@197: max = std::max(max, m_cache->pixelIndex(scx, sy)); Chris@98: div += prop; Chris@98: } Chris@98: } Chris@98: Chris@98: if (div != 0) mag /= div; Chris@98: if (mag < 0) mag = 0; Chris@98: if (mag > 255) mag = 255; Chris@159: if (max < 0) max = 0; Chris@159: if (max > 255) max = 255; Chris@98: Chris@98: img.setPixel(x - x0, y, m_cache->color(int(mag + 0.001))); Chris@159: // img.setPixel(x - x0, y, m_cache->color(max)); Chris@98: } Chris@0: } Chris@0: Chris@98: paint.drawImage(x0, 0, img); Chris@0: } Chris@0: Chris@28: bool Chris@44: Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame, Chris@28: size_t &resolution, Chris@28: SnapType snap) const Chris@24: { Chris@24: if (!m_model) { Chris@44: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@24: } Chris@24: Chris@130: resolution = m_model->getResolution(); Chris@28: int left = (frame / resolution) * resolution; Chris@28: int right = left + resolution; Chris@28: Chris@28: switch (snap) { Chris@28: case SnapLeft: frame = left; break; Chris@28: case SnapRight: frame = right; break; Chris@28: case SnapNearest: Chris@28: case SnapNeighbouring: Chris@28: if (frame - left > right - frame) frame = right; Chris@28: else frame = left; Chris@28: break; Chris@28: } Chris@24: Chris@28: return true; Chris@24: } Chris@24: Chris@197: QString Chris@197: Colour3DPlotLayer::toXmlString(QString indent, QString extraAttributes) const Chris@197: { Chris@197: QString s; Chris@197: Chris@197: s += QString("scale=\"%1\" " Chris@197: "colourScheme=\"%2\" " Chris@197: "normalizeColumns=\"%3\" " Chris@197: "normalizeVisibleArea=\"%4\"") Chris@197: .arg((int)m_colourScale) Chris@197: .arg(m_colourMap) Chris@197: .arg(m_normalizeColumns ? "true" : "false") Chris@197: .arg(m_normalizeVisibleArea ? "true" : "false"); Chris@197: Chris@197: return Layer::toXmlString(indent, extraAttributes + " " + s); Chris@197: } Chris@197: Chris@197: void Chris@197: Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes) Chris@197: { Chris@197: bool ok = false; Chris@197: Chris@197: ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok); Chris@197: if (ok) setColourScale(scale); Chris@197: Chris@197: int colourMap = attributes.value("colourScheme").toInt(&ok); Chris@197: if (ok) setColourMap(colourMap); Chris@197: Chris@197: bool normalizeColumns = Chris@197: (attributes.value("normalizeColumns").trimmed() == "true"); Chris@197: setNormalizeColumns(normalizeColumns); Chris@197: Chris@197: bool normalizeVisibleArea = Chris@197: (attributes.value("normalizeVisibleArea").trimmed() == "true"); Chris@197: setNormalizeVisibleArea(normalizeVisibleArea); Chris@197: } Chris@197: