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@0: #include "base/Profiler.h" Chris@197: #include "base/LogRange.h" Chris@444: #include "base/RangeMapper.h" Chris@1077: Chris@376: #include "ColourMapper.h" Chris@1077: #include "LayerGeometryProvider.h" Chris@1078: #include "PaintAssistant.h" Chris@1077: Chris@1100: #include "data/model/Dense3DModelPeakCache.h" Chris@1100: Chris@1077: #include "view/ViewManager.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@316: #include Chris@1018: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@545: #ifndef __GNUC__ Chris@545: #include Chris@545: #endif Chris@545: Chris@903: using std::vector; Chris@903: Chris@353: //#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@469: m_peaksCache(0), Chris@461: m_cacheValidStart(0), Chris@461: m_cacheValidEnd(0), Chris@1105: m_colourScale(ColourScaleType::Linear), Chris@461: m_colourScaleSet(false), Chris@197: m_colourMap(0), Chris@534: m_gain(1.0), Chris@1103: m_binScale(BinScale::Linear), Chris@1104: m_normalization(ColumnNormalization::None), Chris@1104: m_normalizeVisibleArea(false), Chris@444: m_invertVertical(false), Chris@465: m_opaque(false), Chris@535: m_smooth(false), Chris@805: m_peakResolution(256), Chris@444: m_miny(0), Chris@1100: m_maxy(0), Chris@1101: m_synchronous(false), Chris@1100: m_peakCache(0), Chris@1100: m_peakCacheDivisor(8) Chris@0: { Chris@1018: QSettings settings; Chris@1018: settings.beginGroup("Preferences"); Chris@1018: setColourMap(settings.value("colour-3d-plot-colour", ColourMapper::Green).toInt()); Chris@1018: settings.endGroup(); Chris@0: } Chris@0: Chris@0: Colour3DPlotLayer::~Colour3DPlotLayer() Chris@0: { Chris@461: delete m_cache; Chris@1100: delete m_peaksCache; //!!! this one is to go... Chris@1100: delete m_peakCache; Chris@1102: Chris@1102: cacheInvalid(); //!!! dup with above? Chris@0: } Chris@0: Chris@1105: ColourScaleType Chris@1104: Colour3DPlotLayer::convertToColourScale(int value) Chris@1104: { Chris@1104: switch (value) { Chris@1104: default: Chris@1105: case 0: return ColourScaleType::Linear; Chris@1105: case 1: return ColourScaleType::Log; Chris@1105: case 2: return ColourScaleType::PlusMinusOne; Chris@1105: case 3: return ColourScaleType::Absolute; Chris@1104: } Chris@1104: } Chris@1104: Chris@1104: int Chris@1105: Colour3DPlotLayer::convertFromColourScale(ColourScaleType scale) Chris@1104: { Chris@1104: switch (scale) { Chris@1105: case ColourScaleType::Linear: return 0; Chris@1105: case ColourScaleType::Log: return 1; Chris@1105: case ColourScaleType::PlusMinusOne: return 2; Chris@1105: case ColourScaleType::Absolute: return 3; Chris@1104: Chris@1105: case ColourScaleType::Meter: Chris@1105: case ColourScaleType::Phase: Chris@1104: default: return 0; Chris@1104: } Chris@1104: } Chris@1104: Chris@1104: std::pair Chris@1104: Colour3DPlotLayer::convertToColumnNorm(int value) Chris@1104: { Chris@1104: switch (value) { Chris@1104: default: Chris@1104: case 0: return { ColumnNormalization::None, false }; Chris@1104: case 1: return { ColumnNormalization::Max1, false }; Chris@1104: case 2: return { ColumnNormalization::None, true }; // visible area Chris@1104: case 3: return { ColumnNormalization::Hybrid, false }; Chris@1104: } Chris@1104: } Chris@1104: Chris@1104: int Chris@1104: Colour3DPlotLayer::convertFromColumnNorm(ColumnNormalization norm, bool visible) Chris@1104: { Chris@1104: if (visible) return 2; Chris@1104: switch (norm) { Chris@1104: case ColumnNormalization::None: return 0; Chris@1104: case ColumnNormalization::Max1: return 1; Chris@1104: case ColumnNormalization::Hybrid: return 3; Chris@1104: Chris@1104: case ColumnNormalization::Sum1: Chris@1104: default: return 0; Chris@1104: } Chris@1104: } Chris@1104: Chris@0: void Chris@1101: Colour3DPlotLayer::setSynchronousPainting(bool synchronous) Chris@1101: { Chris@1101: m_synchronous = synchronous; Chris@1101: } Chris@1101: Chris@1101: 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@320: connectSignals(m_model); Chris@0: Chris@461: connect(m_model, SIGNAL(modelChanged()), this, SLOT(modelChanged())); Chris@908: connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), Chris@908: this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t))); Chris@0: Chris@474: m_peakResolution = 256; Chris@474: if (model->getResolution() > 512) { Chris@474: m_peakResolution = 16; Chris@474: } else if (model->getResolution() > 128) { Chris@474: m_peakResolution = 64; Chris@474: } else if (model->getResolution() > 2) { Chris@474: m_peakResolution = 128; Chris@474: } Chris@1100: Chris@1100: delete m_peakCache; Chris@1100: m_peakCache = 0; Chris@1100: Chris@474: cacheInvalid(); Chris@474: Chris@0: emit modelReplaced(); Chris@193: emit sliceableModelReplaced(oldModel, model); Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::cacheInvalid() Chris@0: { Chris@1100: //!!! to go Chris@1100: Chris@469: delete m_cache; Chris@469: delete m_peaksCache; Chris@0: m_cache = 0; Chris@469: m_peaksCache = 0; Chris@461: m_cacheValidStart = 0; Chris@461: m_cacheValidEnd = 0; Chris@1102: Chris@1102: //!!! Chris@1102: for (ViewRendererMap::iterator i = m_renderers.begin(); Chris@1102: i != m_renderers.end(); ++i) { Chris@1102: delete i->second; Chris@1102: } Chris@1102: m_renderers.clear(); Chris@0: } Chris@0: Chris@0: void Chris@908: Colour3DPlotLayer::cacheInvalid(sv_frame_t startFrame, sv_frame_t endFrame) Chris@0: { Chris@1100: //!!! to go Chris@1100: Chris@843: if (!m_cache || !m_model) return; Chris@461: Chris@805: int modelResolution = m_model->getResolution(); Chris@908: int start = int(startFrame / modelResolution); Chris@908: int end = int(endFrame / modelResolution + 1); Chris@461: if (m_cacheValidStart < end) m_cacheValidStart = end; Chris@461: if (m_cacheValidEnd > start) m_cacheValidEnd = start; Chris@461: if (m_cacheValidStart > m_cacheValidEnd) m_cacheValidEnd = m_cacheValidStart; Chris@461: } Chris@461: Chris@1100: Dense3DModelPeakCache * Chris@1100: Colour3DPlotLayer::getPeakCache() const Chris@1100: { Chris@1100: if (!m_peakCache) { Chris@1100: m_peakCache = new Dense3DModelPeakCache(m_model, m_peakCacheDivisor); Chris@1100: } Chris@1100: return m_peakCache; Chris@1100: } Chris@1100: Chris@461: void Chris@461: Colour3DPlotLayer::modelChanged() Chris@461: { Chris@1105: if (!m_colourScaleSet && m_colourScale == ColourScaleType::Linear) { Chris@461: if (m_model) { Chris@461: if (m_model->shouldUseLogValueScale()) { Chris@1105: setColourScale(ColourScaleType::Log); Chris@461: } else { Chris@461: m_colourScaleSet = true; Chris@461: } Chris@461: } Chris@461: } Chris@0: cacheInvalid(); Chris@0: } Chris@0: Chris@461: void Chris@908: Colour3DPlotLayer::modelChangedWithin(sv_frame_t startFrame, sv_frame_t endFrame) Chris@461: { Chris@1105: if (!m_colourScaleSet && m_colourScale == ColourScaleType::Linear) { Chris@461: if (m_model && m_model->getWidth() > 50) { Chris@461: if (m_model->shouldUseLogValueScale()) { Chris@1105: setColourScale(ColourScaleType::Log); Chris@461: } else { Chris@461: m_colourScaleSet = true; Chris@461: } Chris@461: } Chris@461: } Chris@461: cacheInvalid(startFrame, endFrame); Chris@461: } Chris@461: 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@1104: list.push_back("Normalization"); Chris@534: list.push_back("Gain"); Chris@531: list.push_back("Bin Scale"); Chris@357: list.push_back("Invert Vertical Scale"); Chris@465: list.push_back("Opaque"); Chris@535: list.push_back("Smooth"); 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@1104: if (name == "Normalization") return tr("Normalization"); Chris@357: if (name == "Invert Vertical Scale") return tr("Invert Vertical Scale"); Chris@534: if (name == "Gain") return tr("Gain"); Chris@465: if (name == "Opaque") return tr("Always Opaque"); Chris@535: if (name == "Smooth") return tr("Smooth"); Chris@531: if (name == "Bin Scale") return tr("Bin Scale"); Chris@159: return ""; Chris@159: } Chris@159: Chris@346: QString Chris@346: Colour3DPlotLayer::getPropertyIconName(const PropertyName &name) const Chris@346: { Chris@357: if (name == "Invert Vertical Scale") return "invert-vertical"; Chris@465: if (name == "Opaque") return "opaque"; Chris@535: if (name == "Smooth") return "smooth"; Chris@346: return ""; Chris@346: } Chris@346: Chris@159: Layer::PropertyType Chris@159: Colour3DPlotLayer::getPropertyType(const PropertyName &name) const Chris@159: { Chris@534: if (name == "Gain") return RangeProperty; Chris@357: if (name == "Invert Vertical Scale") return ToggleProperty; Chris@465: if (name == "Opaque") return ToggleProperty; Chris@535: if (name == "Smooth") return ToggleProperty; Chris@159: return ValueProperty; Chris@159: } Chris@159: Chris@159: QString Chris@159: Colour3DPlotLayer::getPropertyGroupName(const PropertyName &name) const Chris@159: { Chris@1104: if (name == "Normalization" || Chris@1104: name == "Colour Scale" || Chris@534: name == "Gain") return tr("Scale"); Chris@531: if (name == "Bin Scale" || Chris@531: name == "Invert Vertical Scale") return tr("Bins"); Chris@465: if (name == "Opaque" || Chris@535: name == "Smooth" || Chris@465: name == "Colour") return tr("Colour"); 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@534: if (name == "Gain") { Chris@534: Chris@534: *min = -50; Chris@534: *max = 50; Chris@534: Chris@902: *deflt = int(lrint(log10(1.0) * 20.0)); Chris@534: if (*deflt < *min) *deflt = *min; Chris@534: if (*deflt > *max) *deflt = *max; Chris@534: Chris@902: val = int(lrint(log10(m_gain) * 20.0)); Chris@534: if (val < *min) val = *min; Chris@534: if (val > *max) val = *max; Chris@534: Chris@534: } else if (name == "Colour Scale") { Chris@159: Chris@1099: // linear, log, +/-1, abs Chris@159: *min = 0; Chris@509: *max = 3; Chris@1099: *deflt = 0; Chris@159: Chris@1104: val = convertFromColourScale(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@1099: } else if (name == "Normalization") { Chris@197: Chris@1099: *min = 0; Chris@1099: *max = 3; Chris@1104: *deflt = 0; Chris@1104: Chris@1104: val = convertFromColumnNorm(m_normalization, m_normalizeVisibleArea); Chris@197: Chris@357: } else if (name == "Invert Vertical Scale") { Chris@357: Chris@357: *deflt = 0; Chris@357: val = (m_invertVertical ? 1 : 0); Chris@357: Chris@531: } else if (name == "Bin Scale") { Chris@531: Chris@531: *min = 0; Chris@531: *max = 1; Chris@1103: *deflt = int(BinScale::Linear); Chris@531: val = (int)m_binScale; Chris@531: Chris@465: } else if (name == "Opaque") { Chris@465: Chris@465: *deflt = 0; Chris@465: val = (m_opaque ? 1 : 0); Chris@465: Chris@535: } else if (name == "Smooth") { Chris@535: Chris@535: *deflt = 0; Chris@535: val = (m_smooth ? 1 : 0); Chris@535: 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@509: case 3: return tr("Absolute"); Chris@159: } Chris@159: } Chris@1099: if (name == "Normalization") { Chris@1099: return ""; // icon only Chris@1099: } Chris@531: if (name == "Bin Scale") { Chris@531: switch (value) { Chris@531: default: Chris@531: case 0: return tr("Linear"); Chris@531: case 1: return tr("Log"); Chris@531: } Chris@531: } Chris@159: return tr(""); Chris@159: } Chris@159: Chris@1099: QString Chris@1099: Colour3DPlotLayer::getPropertyValueIconName(const PropertyName &name, Chris@1099: int value) const Chris@1099: { Chris@1099: if (name == "Normalization") { Chris@1099: switch(value) { Chris@1099: default: Chris@1099: case 0: return "normalise-none"; Chris@1099: case 1: return "normalise-columns"; Chris@1099: case 2: return "normalise"; Chris@1099: case 3: return "normalise-hybrid"; Chris@1099: } Chris@1099: } Chris@1099: return ""; Chris@1099: } Chris@1099: Chris@534: RangeMapper * Chris@534: Colour3DPlotLayer::getNewPropertyRangeMapper(const PropertyName &name) const Chris@534: { Chris@534: if (name == "Gain") { Chris@534: return new LinearRangeMapper(-50, 50, -25, 25, tr("dB")); Chris@534: } Chris@534: return 0; Chris@534: } Chris@534: Chris@159: void Chris@159: Colour3DPlotLayer::setProperty(const PropertyName &name, int value) Chris@159: { Chris@534: if (name == "Gain") { Chris@902: setGain(float(pow(10, value/20.0))); Chris@534: } else if (name == "Colour Scale") { Chris@1104: setColourScale(convertToColourScale(value)); Chris@197: } else if (name == "Colour") { Chris@197: setColourMap(value); Chris@357: } else if (name == "Invert Vertical Scale") { Chris@357: setInvertVertical(value ? true : false); Chris@465: } else if (name == "Opaque") { Chris@465: setOpaque(value ? true : false); Chris@535: } else if (name == "Smooth") { Chris@535: setSmooth(value ? true : false); Chris@531: } else if (name == "Bin Scale") { Chris@531: switch (value) { Chris@531: default: Chris@1103: case 0: setBinScale(BinScale::Linear); break; Chris@1103: case 1: setBinScale(BinScale::Log); break; Chris@531: } Chris@1099: } else if (name == "Normalization") { Chris@1104: auto n = convertToColumnNorm(value); Chris@1104: setNormalization(n.first); Chris@1104: setNormalizeVisibleArea(n.second); Chris@159: } Chris@159: } Chris@159: Chris@159: void Chris@1105: Colour3DPlotLayer::setColourScale(ColourScaleType scale) Chris@159: { Chris@159: if (m_colourScale == scale) return; Chris@159: m_colourScale = scale; Chris@461: m_colourScaleSet = true; 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@534: Colour3DPlotLayer::setGain(float gain) Chris@534: { Chris@534: if (m_gain == gain) return; Chris@534: m_gain = gain; Chris@534: cacheInvalid(); Chris@534: emit layerParametersChanged(); Chris@534: } Chris@534: Chris@534: float Chris@534: Colour3DPlotLayer::getGain() const Chris@534: { Chris@534: return m_gain; Chris@534: } Chris@534: Chris@534: void Chris@1103: Colour3DPlotLayer::setBinScale(BinScale binScale) Chris@531: { Chris@531: if (m_binScale == binScale) return; Chris@531: m_binScale = binScale; Chris@531: cacheInvalid(); Chris@531: emit layerParametersChanged(); Chris@531: } Chris@531: Chris@1103: BinScale Chris@531: Colour3DPlotLayer::getBinScale() const Chris@531: { Chris@531: return m_binScale; Chris@531: } Chris@531: Chris@531: void Chris@1104: Colour3DPlotLayer::setNormalization(ColumnNormalization n) Chris@197: { Chris@1099: if (m_normalization == n) return; Chris@1099: Chris@1099: m_normalization = n; Chris@197: cacheInvalid(); Chris@1099: Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@1104: ColumnNormalization Chris@1099: Colour3DPlotLayer::getNormalization() const Chris@197: { Chris@1099: return m_normalization; Chris@197: } Chris@197: Chris@357: void Chris@1104: Colour3DPlotLayer::setNormalizeVisibleArea(bool n) Chris@1104: { Chris@1104: if (m_normalizeVisibleArea == n) return; Chris@1104: Chris@1104: m_normalizeVisibleArea = n; Chris@1104: cacheInvalid(); Chris@1104: Chris@1104: emit layerParametersChanged(); Chris@1104: } Chris@1104: Chris@1104: bool Chris@1104: Colour3DPlotLayer::getNormalizeVisibleArea() const Chris@1104: { Chris@1104: return m_normalizeVisibleArea; Chris@1104: } Chris@1104: Chris@1104: void Chris@357: Colour3DPlotLayer::setInvertVertical(bool n) Chris@357: { Chris@357: if (m_invertVertical == n) return; Chris@357: m_invertVertical = n; Chris@357: cacheInvalid(); Chris@357: emit layerParametersChanged(); Chris@357: } Chris@357: Chris@465: void Chris@465: Colour3DPlotLayer::setOpaque(bool n) Chris@465: { Chris@465: if (m_opaque == n) return; Chris@465: m_opaque = n; Chris@465: emit layerParametersChanged(); Chris@465: } Chris@465: Chris@535: void Chris@535: Colour3DPlotLayer::setSmooth(bool n) Chris@535: { Chris@535: if (m_smooth == n) return; Chris@535: m_smooth = n; Chris@535: emit layerParametersChanged(); Chris@535: } Chris@535: Chris@357: bool Chris@357: Colour3DPlotLayer::getInvertVertical() const Chris@357: { Chris@357: return m_invertVertical; Chris@357: } Chris@357: Chris@25: bool Chris@465: Colour3DPlotLayer::getOpaque() const Chris@465: { Chris@465: return m_opaque; Chris@465: } Chris@465: Chris@535: bool Chris@535: Colour3DPlotLayer::getSmooth() const Chris@535: { Chris@535: return m_smooth; Chris@535: } Chris@535: Chris@475: void Chris@916: Colour3DPlotLayer::setLayerDormant(const LayerGeometryProvider *v, bool dormant) Chris@475: { Chris@475: if (dormant) { Chris@475: Chris@475: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::setLayerDormant(" << dormant << ")" Chris@585: << endl; Chris@475: #endif Chris@475: Chris@475: if (isLayerDormant(v)) { Chris@475: return; Chris@475: } Chris@475: Chris@475: Layer::setLayerDormant(v, true); Chris@475: Chris@475: cacheInvalid(); Chris@475: Chris@475: } else { Chris@475: Chris@475: Layer::setLayerDormant(v, false); Chris@475: } Chris@475: } Chris@475: Chris@465: bool Chris@916: Colour3DPlotLayer::isLayerScrollable(const LayerGeometryProvider *v) const Chris@25: { Chris@1104: if (m_normalizeVisibleArea) { Chris@812: return false; Chris@812: } Chris@812: if (shouldPaintDenseIn(v)) { Chris@812: return true; Chris@812: } Chris@25: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@25: } Chris@25: Chris@444: bool Chris@904: Colour3DPlotLayer::getValueExtents(double &min, double &max, Chris@444: bool &logarithmic, QString &unit) const Chris@444: { Chris@444: if (!m_model) return false; Chris@444: Chris@444: min = 0; Chris@904: max = double(m_model->getHeight()); Chris@444: Chris@444: logarithmic = false; Chris@444: unit = ""; Chris@444: Chris@444: return true; Chris@444: } Chris@444: Chris@444: bool Chris@904: Colour3DPlotLayer::getDisplayExtents(double &min, double &max) const Chris@444: { Chris@444: if (!m_model) return false; Chris@444: Chris@904: double hmax = double(m_model->getHeight()); Chris@902: Chris@904: min = m_miny; Chris@904: max = m_maxy; Chris@444: if (max <= min) { Chris@444: min = 0; Chris@902: max = hmax; Chris@444: } Chris@444: if (min < 0) min = 0; Chris@902: if (max > hmax) max = hmax; Chris@444: Chris@444: return true; Chris@444: } Chris@444: Chris@444: bool Chris@904: Colour3DPlotLayer::setDisplayExtents(double min, double max) Chris@444: { Chris@444: if (!m_model) return false; Chris@444: Chris@904: m_miny = int(lrint(min)); Chris@904: m_maxy = int(lrint(max)); Chris@444: Chris@444: emit layerParametersChanged(); Chris@444: return true; Chris@444: } Chris@444: Chris@725: bool Chris@916: Colour3DPlotLayer::getYScaleValue(const LayerGeometryProvider *, int, Chris@904: double &, QString &) const Chris@725: { Chris@725: return false;//!!! Chris@725: } Chris@725: Chris@444: int Chris@444: Colour3DPlotLayer::getVerticalZoomSteps(int &defaultStep) const Chris@444: { Chris@444: if (!m_model) return 0; Chris@444: Chris@444: defaultStep = 0; Chris@444: int h = m_model->getHeight(); Chris@444: return h; Chris@444: } Chris@444: Chris@444: int Chris@444: Colour3DPlotLayer::getCurrentVerticalZoomStep() const Chris@444: { Chris@444: if (!m_model) return 0; Chris@444: Chris@904: double min, max; Chris@444: getDisplayExtents(min, max); Chris@904: return m_model->getHeight() - int(lrint(max - min)); Chris@444: } Chris@444: Chris@444: void Chris@444: Colour3DPlotLayer::setVerticalZoomStep(int step) Chris@444: { Chris@444: if (!m_model) return; Chris@444: Chris@587: // SVDEBUG << "Colour3DPlotLayer::setVerticalZoomStep(" <getHeight(); Chris@532: getDisplayExtents(mn, mx); Chris@916: double h = v->getPaintHeight(); Chris@1103: if (m_binScale == BinScale::Linear) { Chris@532: y = h - (((bin - mn) * h) / (mx - mn)); Chris@532: } else { Chris@904: double logmin = mn + 1, logmax = mx + 1; Chris@532: LogRange::mapRange(logmin, logmax); Chris@532: y = h - (((LogRange::map(bin + 1) - logmin) * h) / (logmax - logmin)); Chris@532: } Chris@532: return y; Chris@532: } Chris@532: Chris@904: double Chris@916: Colour3DPlotLayer::getBinForY(LayerGeometryProvider *v, double y) const Chris@532: { Chris@904: double bin = y; Chris@532: if (!m_model) return bin; Chris@904: double mn = 0, mx = m_model->getHeight(); Chris@532: getDisplayExtents(mn, mx); Chris@916: double h = v->getPaintHeight(); Chris@1103: if (m_binScale == BinScale::Linear) { Chris@532: bin = mn + ((h - y) * (mx - mn)) / h; Chris@532: } else { Chris@904: double logmin = mn + 1, logmax = mx + 1; Chris@532: LogRange::mapRange(logmin, logmax); Chris@532: bin = LogRange::unmap(logmin + ((h - y) * (logmax - logmin)) / h) - 1; Chris@532: } Chris@532: return bin; Chris@532: } Chris@532: Chris@25: QString Chris@916: Colour3DPlotLayer::getFeatureDescription(LayerGeometryProvider *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@902: sv_frame_t modelStart = m_model->getStartFrame(); Chris@805: int modelResolution = m_model->getResolution(); Chris@25: Chris@902: double srRatio = Chris@902: v->getViewManager()->getMainModelSampleRate() / Chris@902: m_model->getSampleRate(); Chris@159: Chris@902: int sx0 = int((double(v->getFrameForX(x)) / srRatio - double(modelStart)) / Chris@812: modelResolution); Chris@25: Chris@160: int f0 = sx0 * modelResolution; Chris@160: int f1 = f0 + modelResolution; Chris@160: Chris@447: int sh = m_model->getHeight(); Chris@447: Chris@447: int symin = m_miny; Chris@447: int symax = m_maxy; Chris@447: if (symax <= symin) { Chris@447: symin = 0; Chris@447: symax = sh; Chris@447: } Chris@447: if (symin < 0) symin = 0; Chris@447: if (symax > sh) symax = sh; Chris@447: Chris@916: // double binHeight = double(v->getPaintHeight()) / (symax - symin); Chris@916: // int sy = int((v->getPaintHeight() - y) / binHeight) + symin; Chris@534: Chris@903: int sy = getIBinForY(v, y); Chris@25: Chris@812: if (sy < 0 || sy >= m_model->getHeight()) { Chris@812: return ""; Chris@812: } Chris@812: Chris@357: if (m_invertVertical) sy = m_model->getHeight() - sy - 1; Chris@357: Chris@160: float value = m_model->getValueAt(sx0, sy); Chris@159: Chris@682: // cerr << "bin value (" << sx0 << "," << sy << ") is " << value << 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@969: Colour3DPlotLayer::getColourScaleWidth(QPainter &p) const Chris@159: { Chris@969: // Font is rotated Chris@969: int cw = p.fontMetrics().height(); Chris@159: return cw; Chris@159: } Chris@159: Chris@159: int Chris@916: Colour3DPlotLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool, 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@805: for (int 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@916: Colour3DPlotLayer::paintVerticalScale(LayerGeometryProvider *v, bool, 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@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@904: double min = m_model->getMinimumLevel(); Chris@904: double max = m_model->getMaximumLevel(); Chris@447: Chris@904: double mmin = min; Chris@904: double mmax = max; Chris@447: Chris@1105: if (m_colourScale == ColourScaleType::Log) { Chris@447: LogRange::mapRange(mmin, mmax); Chris@1105: } else if (m_colourScale == ColourScaleType::PlusMinusOne) { Chris@447: mmin = -1.f; Chris@447: mmax = 1.f; Chris@1105: } else if (m_colourScale == ColourScaleType::Absolute) { Chris@509: if (mmin < 0) { Chris@904: if (fabs(mmin) > fabs(mmax)) mmax = fabs(mmin); Chris@904: else mmax = fabs(mmax); Chris@509: mmin = 0; Chris@509: } else { Chris@904: mmin = fabs(mmin); Chris@904: mmax = fabs(mmax); Chris@509: } Chris@447: } Chris@447: Chris@902: if (max == min) max = min + 1.f; Chris@902: if (mmax == mmin) mmax = mmin + 1.f; Chris@447: Chris@287: paint.setPen(v->getForeground()); Chris@447: paint.drawRect(4, 10, cw - 8, ch+1); Chris@159: Chris@446: for (int y = 0; y < ch; ++y) { Chris@904: double value = ((max - min) * (double(ch-y) - 1.0)) / double(ch) + min; Chris@1105: if (m_colourScale == ColourScaleType::Log) { Chris@447: value = LogRange::map(value); Chris@447: } Chris@447: int pixel = int(((value - mmin) * 256) / (mmax - mmin)); Chris@465: if (pixel >= 0 && pixel < 256) { Chris@465: QRgb c = m_cache->color(pixel); Chris@465: paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c))); Chris@465: paint.drawLine(5, 11 + y, cw - 5, 11 + y); Chris@465: } else { Chris@682: cerr << "WARNING: Colour3DPlotLayer::paintVerticalScale: value " << value << ", mmin " << mmin << ", mmax " << mmax << " leads to invalid pixel " << pixel << endl; Chris@465: } Chris@159: } Chris@446: Chris@446: QString minstr = QString("%1").arg(min); Chris@446: QString maxstr = QString("%1").arg(max); Chris@446: Chris@446: paint.save(); Chris@446: Chris@446: QFont font = paint.font(); Chris@1053: if (font.pixelSize() > 0) { Chris@1053: int newSize = int(font.pixelSize() * 0.65); Chris@1053: if (newSize < 6) newSize = 6; Chris@1053: font.setPixelSize(newSize); Chris@1053: paint.setFont(font); Chris@1053: } Chris@446: Chris@446: int msw = paint.fontMetrics().width(maxstr); Chris@446: Chris@446: QMatrix m; Chris@446: m.translate(cw - 6, ch + 10); Chris@446: m.rotate(-90); Chris@446: Chris@446: paint.setWorldMatrix(m); Chris@446: Chris@1078: PaintAssistant::drawVisibleText(v, paint, 2, 0, minstr, PaintAssistant::OutlinedText); Chris@446: Chris@446: m.translate(ch - msw - 2, 0); Chris@446: paint.setWorldMatrix(m); Chris@446: Chris@1078: PaintAssistant::drawVisibleText(v, paint, 0, 0, maxstr, PaintAssistant::OutlinedText); Chris@446: Chris@446: paint.restore(); Chris@159: } Chris@159: Chris@287: paint.setPen(v->getForeground()); Chris@159: Chris@445: int sh = m_model->getHeight(); Chris@445: Chris@445: int symin = m_miny; Chris@445: int symax = m_maxy; Chris@445: if (symax <= symin) { Chris@445: symin = 0; Chris@445: symax = sh; Chris@445: } Chris@445: if (symin < 0) symin = 0; Chris@445: if (symax > sh) symax = sh; Chris@445: Chris@532: paint.save(); Chris@456: Chris@533: int py = h; Chris@25: Chris@969: int defaultFontHeight = paint.fontMetrics().height(); Chris@969: Chris@805: for (int i = symin; i <= symax; ++i) { Chris@98: Chris@532: int y0; Chris@534: Chris@903: y0 = getIYForBin(v, i); Chris@532: int h = py - y0; Chris@532: Chris@532: if (i > symin) { Chris@532: if (paint.fontMetrics().height() >= h) { Chris@969: if (h >= defaultFontHeight * 0.8) { Chris@532: QFont tf = paint.font(); Chris@973: tf.setPixelSize(int(h * 0.8)); Chris@532: paint.setFont(tf); Chris@532: } else { Chris@532: continue; Chris@532: } Chris@532: } Chris@532: } Chris@25: Chris@532: py = y0; Chris@532: Chris@534: if (i < symax) { Chris@534: paint.drawLine(cw, y0, w, y0); Chris@534: } Chris@25: Chris@532: if (i > symin) { Chris@534: Chris@805: int idx = i - 1; Chris@534: if (m_invertVertical) idx = m_model->getHeight() - idx - 1; Chris@534: Chris@534: QString text = m_model->getBinName(idx); Chris@534: if (text == "") text = QString("[%1]").arg(idx + 1); Chris@534: Chris@534: int ty = y0 + (h/2) - (paint.fontMetrics().height()/2) + Chris@534: paint.fontMetrics().ascent() + 1; Chris@534: Chris@534: paint.drawText(cw + 5, ty, text); Chris@457: } Chris@25: } Chris@456: Chris@456: paint.restore(); Chris@25: } Chris@25: Chris@467: DenseThreeDimensionalModel::Column Chris@805: Colour3DPlotLayer::getColumn(int col) const Chris@197: { Chris@812: Profiler profiler("Colour3DPlotLayer::getColumn"); Chris@812: Chris@467: DenseThreeDimensionalModel::Column values = m_model->getColumn(col); Chris@1019: values.resize(m_model->getHeight(), 0.f); Chris@1104: if (m_normalization != ColumnNormalization::Max1 && Chris@1104: m_normalization != ColumnNormalization::Hybrid) { Chris@1099: return values; Chris@1099: } Chris@461: Chris@904: double colMax = 0.f, colMin = 0.f; Chris@904: double min = 0.f, max = 0.f; Chris@197: Chris@1019: int nv = int(values.size()); Chris@1019: Chris@461: min = m_model->getMinimumLevel(); Chris@461: max = m_model->getMaximumLevel(); Chris@461: Chris@1019: for (int y = 0; y < nv; ++y) { Chris@467: if (y == 0 || values.at(y) > colMax) colMax = values.at(y); Chris@467: if (y == 0 || values.at(y) < colMin) colMin = values.at(y); Chris@197: } Chris@461: if (colMin == colMax) colMax = colMin + 1; Chris@197: Chris@1019: for (int y = 0; y < nv; ++y) { Chris@461: Chris@904: double value = values.at(y); Chris@904: double norm = (value - colMin) / (colMax - colMin); Chris@904: double newvalue = min + (max - min) * norm; Chris@197: Chris@904: if (value != newvalue) values[y] = float(newvalue); Chris@468: } Chris@468: Chris@1104: if (m_normalization == ColumnNormalization::Hybrid Chris@1099: && (colMax > 0.0)) { Chris@904: double logmax = log10(colMax); Chris@1019: for (int y = 0; y < nv; ++y) { Chris@904: values[y] = float(values[y] * logmax); Chris@719: } Chris@719: } Chris@719: Chris@468: return values; Chris@197: } Chris@197: Chris@197: void Chris@1046: Colour3DPlotLayer::fillCache(int firstColumn, int lastColumn) const Chris@197: { Chris@1046: // This call requests a (perhaps partial) fill of the cache Chris@1046: // between model columns firstColumn and lastColumn inclusive. Chris@1046: // The cache itself always has size sufficient to contain the Chris@1046: // whole model, but its validity may be less, depending on which Chris@1046: // regions have been requested via calls to this function. Note Chris@1046: // that firstColumn and lastColumn are *model* column numbers. If Chris@1046: // the model starts at a frame > 0, a firstColumn of zero still Chris@1046: // corresponds to the first column in the model, not the first Chris@1046: // column on the resulting rendered layer. Chris@1046: Chris@812: Profiler profiler("Colour3DPlotLayer::fillCache", true); Chris@476: Chris@1046: int cacheWidth = m_model->getWidth(); Chris@1046: int cacheHeight = m_model->getHeight(); Chris@461: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@1046: cerr << "Colour3DPlotLayer::fillCache: range " << firstColumn << " -> " << lastColumn << " (cache size will be " << cacheWidth << " x " << cacheHeight << ")" << endl; Chris@812: #endif Chris@812: Chris@812: if (m_cache && m_cache->height() != cacheHeight) { Chris@742: // height has changed: delete everything rather than resizing Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::fillCache: Cache height has changed, recreating" << endl; Chris@812: #endif Chris@461: delete m_cache; Chris@469: delete m_peaksCache; Chris@461: m_cache = 0; Chris@469: m_peaksCache = 0; Chris@461: } Chris@461: Chris@812: if (m_cache && m_cache->width() != cacheWidth) { Chris@742: // width has changed and we have an existing cache: resize it Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::fillCache: Cache width has changed, resizing existing cache" << endl; Chris@812: #endif Chris@469: QImage *newCache = Chris@469: new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight)); Chris@461: delete m_cache; Chris@461: m_cache = newCache; Chris@469: if (m_peaksCache) { Chris@469: QImage *newPeaksCache = Chris@469: new QImage(m_peaksCache->copy Chris@742: (0, 0, cacheWidth / m_peakResolution + 1, cacheHeight)); Chris@469: delete m_peaksCache; Chris@469: m_peaksCache = newPeaksCache; Chris@469: } Chris@197: } Chris@197: Chris@461: if (!m_cache) { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::fillCache: Have no cache, making one" << endl; Chris@812: #endif Chris@1046: m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8); Chris@812: m_cache->setColorCount(256); Chris@514: m_cache->fill(0); Chris@1104: if (!m_normalizeVisibleArea) { Chris@469: m_peaksCache = new QImage Chris@469: (cacheWidth / m_peakResolution + 1, cacheHeight, Chris@469: QImage::Format_Indexed8); Chris@812: m_peaksCache->setColorCount(256); Chris@514: m_peaksCache->fill(0); Chris@469: } else if (m_peaksCache) { Chris@469: delete m_peaksCache; Chris@469: m_peaksCache = 0; Chris@469: } Chris@461: m_cacheValidStart = 0; Chris@461: m_cacheValidEnd = 0; Chris@197: } Chris@197: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "cache size = " << m_cache->width() << "x" << m_cache->height() Chris@812: << " peaks cache size = " << m_peaksCache->width() << "x" << m_peaksCache->height() << endl; Chris@812: #endif Chris@742: Chris@1046: if (m_cacheValidStart <= firstColumn && m_cacheValidEnd >= lastColumn) { Chris@519: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@682: cerr << "Cache is valid in this region already" << endl; Chris@519: #endif Chris@461: return; Chris@461: } Chris@461: Chris@1046: int fillStart = firstColumn; Chris@1046: int fillEnd = lastColumn; Chris@197: Chris@1046: if (fillStart >= cacheWidth) fillStart = cacheWidth-1; Chris@1046: if (fillStart < 0) fillStart = 0; Chris@1046: if (fillEnd >= cacheWidth) fillEnd = cacheWidth-1; Chris@1046: if (fillEnd < 0) fillEnd = 0; Chris@1046: if (fillEnd < fillStart) fillEnd = fillStart; Chris@197: Chris@1104: bool normalizeVisible = (m_normalizeVisibleArea); Chris@197: Chris@461: if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) { Chris@461: Chris@461: if (m_cacheValidEnd < fillStart) { Chris@461: fillStart = m_cacheValidEnd + 1; Chris@461: } Chris@461: if (m_cacheValidStart > fillEnd) { Chris@461: fillEnd = m_cacheValidStart - 1; Chris@461: } Chris@461: Chris@461: m_cacheValidStart = std::min(fillStart, m_cacheValidStart); Chris@461: m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd); Chris@461: Chris@461: } else { Chris@461: Chris@812: // when normalising the visible area, the only valid area, Chris@812: // ever, is the currently visible one Chris@461: Chris@461: m_cacheValidStart = fillStart; Chris@461: m_cacheValidEnd = fillEnd; Chris@461: } Chris@461: Chris@519: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << " (fillStart = " << fillStart << ", fillEnd = " << fillEnd << ")" << endl; Chris@519: #endif Chris@461: Chris@197: DenseThreeDimensionalModel::Column values; Chris@197: Chris@904: double min = m_model->getMinimumLevel(); Chris@904: double max = m_model->getMaximumLevel(); Chris@197: Chris@1105: if (m_colourScale == ColourScaleType::Log) { Chris@197: LogRange::mapRange(min, max); Chris@1105: } else if (m_colourScale == ColourScaleType::PlusMinusOne) { Chris@197: min = -1.f; Chris@197: max = 1.f; Chris@1105: } else if (m_colourScale == ColourScaleType::Absolute) { Chris@509: if (min < 0) { Chris@904: if (fabs(min) > fabs(max)) max = fabs(min); Chris@904: else max = fabs(max); Chris@509: min = 0; Chris@509: } else { Chris@904: min = fabs(min); Chris@904: max = fabs(max); Chris@509: } Chris@197: } Chris@197: Chris@902: if (max == min) max = min + 1.f; 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: QColor colour = mapper.map(index); Chris@469: m_cache->setColor Chris@469: (index, qRgb(colour.red(), colour.green(), colour.blue())); Chris@469: if (m_peaksCache) { Chris@469: m_peaksCache->setColor Chris@469: (index, qRgb(colour.red(), colour.green(), colour.blue())); Chris@469: } Chris@197: } Chris@197: Chris@904: double visibleMax = 0.f, visibleMin = 0.f; Chris@197: Chris@461: if (normalizeVisible) { Chris@197: Chris@805: for (int c = fillStart; c <= fillEnd; ++c) { Chris@197: Chris@467: values = getColumn(c); Chris@197: Chris@904: double colMax = 0.f, colMin = 0.f; Chris@197: Chris@805: for (int y = 0; y < cacheHeight; ++y) { Chris@1019: if (!in_range_for(values, y)) 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@461: if (c == fillStart || colMax > visibleMax) visibleMax = colMax; Chris@461: if (c == fillStart || colMin < visibleMin) visibleMin = colMin; Chris@461: } Chris@461: Chris@1105: if (m_colourScale == ColourScaleType::Log) { Chris@461: visibleMin = LogRange::map(visibleMin); Chris@461: visibleMax = LogRange::map(visibleMax); Chris@461: if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax); Chris@1105: } else if (m_colourScale == ColourScaleType::Absolute) { Chris@509: if (visibleMin < 0) { Chris@904: if (fabs(visibleMin) > fabs(visibleMax)) visibleMax = fabs(visibleMin); Chris@904: else visibleMax = fabs(visibleMax); Chris@509: visibleMin = 0; Chris@509: } else { Chris@904: visibleMin = fabs(visibleMin); Chris@904: visibleMax = fabs(visibleMax); Chris@509: } Chris@197: } Chris@197: } Chris@197: Chris@224: if (visibleMin == visibleMax) visibleMax = visibleMin + 1; Chris@224: Chris@469: int *peaks = 0; Chris@469: if (m_peaksCache) { Chris@469: peaks = new int[cacheHeight]; Chris@469: for (int y = 0; y < cacheHeight; ++y) { Chris@469: peaks[y] = 0; Chris@469: } Chris@469: } Chris@469: Chris@812: Profiler profiler2("Colour3DPlotLayer::fillCache: filling", true); Chris@812: Chris@805: for (int c = fillStart; c <= fillEnd; ++c) { Chris@197: Chris@467: values = getColumn(c); Chris@197: Chris@742: if (c >= m_cache->width()) { Chris@742: cerr << "ERROR: column " << c << " >= cache width " Chris@742: << m_cache->width() << endl; Chris@742: continue; Chris@742: } Chris@742: Chris@805: for (int y = 0; y < cacheHeight; ++y) { Chris@197: Chris@904: double value = min; Chris@1019: if (in_range_for(values, y)) { Chris@469: value = values.at(y); Chris@197: } Chris@224: Chris@534: value = value * m_gain; Chris@534: Chris@1105: if (m_colourScale == ColourScaleType::Log) { Chris@224: value = LogRange::map(value); Chris@1105: } else if (m_colourScale == ColourScaleType::Absolute) { Chris@904: value = fabs(value); Chris@197: } Chris@461: Chris@461: if (normalizeVisible) { Chris@904: double norm = (value - visibleMin) / (visibleMax - visibleMin); Chris@461: value = min + (max - min) * norm; Chris@461: } 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@469: if (peaks && (pixel > peaks[y])) peaks[y] = pixel; Chris@197: Chris@357: if (m_invertVertical) { Chris@461: m_cache->setPixel(c, cacheHeight - y - 1, pixel); Chris@357: } else { Chris@742: if (y >= m_cache->height()) { Chris@742: cerr << "ERROR: row " << y << " >= cache height " << m_cache->height() << endl; Chris@742: } else { Chris@742: m_cache->setPixel(c, y, pixel); Chris@742: } Chris@357: } Chris@197: } Chris@469: Chris@469: if (peaks) { Chris@805: int notch = (c % m_peakResolution); Chris@469: if (notch == m_peakResolution-1 || c == fillEnd) { Chris@805: int pc = c / m_peakResolution; Chris@742: if (pc >= m_peaksCache->width()) { Chris@742: cerr << "ERROR: peak column " << pc Chris@742: << " (from col " << c << ") >= peaks cache width " Chris@742: << m_peaksCache->width() << endl; Chris@742: continue; Chris@742: } Chris@805: for (int y = 0; y < cacheHeight; ++y) { Chris@469: if (m_invertVertical) { Chris@469: m_peaksCache->setPixel(pc, cacheHeight - y - 1, peaks[y]); Chris@469: } else { Chris@742: if (y >= m_peaksCache->height()) { Chris@742: cerr << "ERROR: row " << y Chris@742: << " >= peaks cache height " Chris@742: << m_peaksCache->height() << endl; Chris@742: } else { Chris@742: m_peaksCache->setPixel(pc, y, peaks[y]); Chris@742: } Chris@469: } Chris@469: } Chris@469: for (int y = 0; y < cacheHeight; ++y) { Chris@469: peaks[y] = 0; Chris@469: } Chris@469: } Chris@469: } Chris@197: } Chris@469: Chris@469: delete[] peaks; Chris@197: } Chris@197: Chris@812: bool Chris@916: Colour3DPlotLayer::shouldPaintDenseIn(const LayerGeometryProvider *v) const Chris@812: { Chris@812: if (!m_model || !v || !(v->getViewManager())) { Chris@812: return false; Chris@812: } Chris@902: double srRatio = Chris@902: v->getViewManager()->getMainModelSampleRate() / m_model->getSampleRate(); Chris@812: if (m_opaque || Chris@812: m_smooth || Chris@916: m_model->getHeight() >= v->getPaintHeight() || Chris@812: ((m_model->getResolution() * srRatio) / v->getZoomLevel()) < 2) { Chris@812: return true; Chris@812: } Chris@812: return false; Chris@812: } Chris@812: Chris@1100: Colour3DPlotRenderer * Chris@1100: Colour3DPlotLayer::getRenderer(LayerGeometryProvider *v) const Chris@1100: { Chris@1100: if (m_renderers.find(v->getId()) == m_renderers.end()) { Chris@1100: Chris@1100: Colour3DPlotRenderer::Sources sources; Chris@1100: sources.verticalBinLayer = this; Chris@1100: sources.fft = 0; Chris@1100: sources.source = m_model; Chris@1100: sources.peaks = getPeakCache(); Chris@1100: Chris@1100: ColourScale::Parameters cparams; Chris@1100: cparams.colourMap = m_colourMap; Chris@1100: cparams.scale = m_colourScale; Chris@1100: cparams.gain = m_gain; Chris@1100: Chris@1100: Colour3DPlotRenderer::Parameters params; Chris@1100: params.colourScale = ColourScale(cparams); Chris@1100: params.normalization = m_normalization; Chris@1100: params.binScale = m_binScale; Chris@1100: params.alwaysOpaque = m_opaque; Chris@1100: params.invertVertical = m_invertVertical; Chris@1100: params.interpolate = m_smooth; Chris@1100: Chris@1100: m_renderers[v->getId()] = new Colour3DPlotRenderer(sources, params); Chris@1100: } Chris@1100: Chris@1100: return m_renderers[v->getId()]; Chris@1100: } Chris@1100: Chris@197: void Chris@1101: Colour3DPlotLayer::paintAlternative(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@1101: { Chris@1101: static int depth = 0; Chris@1101: Chris@1101: Colour3DPlotRenderer *renderer = getRenderer(v); Chris@1101: Chris@1101: if (m_synchronous) { Chris@1101: (void)renderer->render(v, paint, rect); Chris@1101: return; Chris@1101: } Chris@1101: Chris@1101: ++depth; Chris@1101: cerr << "paint depth " << depth << endl; Chris@1101: Chris@1101: (void)renderer->renderTimeConstrained(v, paint, rect); Chris@1101: Chris@1101: //!!! + mag range Chris@1101: Chris@1101: QRect uncached = renderer->getLargestUncachedRect(); Chris@1101: if (uncached.width() > 0) { Chris@1101: cerr << "updating rect at " << uncached.x() << " width " Chris@1101: << uncached.width() << endl; Chris@1101: v->updatePaintRect(uncached); Chris@1101: } Chris@1101: Chris@1101: cerr << "exiting paint depth " << depth << endl; Chris@1101: --depth; Chris@1101: } Chris@1101: Chris@1101: void Chris@916: Colour3DPlotLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@0: { Chris@514: /* Chris@443: if (m_model) { Chris@587: SVDEBUG << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << endl; Chris@443: } Chris@514: */ Chris@466: Profiler profiler("Colour3DPlotLayer::paint"); Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", rect is (" << rect.x() << "," << rect.y() << ") " << rect.width() << "x" << rect.height() << 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@916: paint.fillRect(0, 10, v->getPaintWidth() * completion / 100, Chris@0: 10, QColor(120, 120, 120)); Chris@0: } Chris@0: return; Chris@0: } Chris@0: Chris@1053: if (m_model->getWidth() == 0) { Chris@1053: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@1053: cerr << "Colour3DPlotLayer::paint(): model width == 0, " Chris@1053: << "nothing to paint (yet)" << endl; Chris@1053: #endif Chris@1053: return; Chris@1053: } Chris@1101: Chris@1101: //!!! why is the setLayerDormant(false) found here in Chris@1101: //!!! SpectrogramLayer not present in Colour3DPlotLayer? Chris@1101: //!!! unnecessary? vestigial? forgotten? Chris@1101: Chris@1101: paintAlternative(v, paint, rect); Chris@1101: return; Chris@1101: Chris@1101: //!!!??? Chris@1053: Chris@1104: if (m_normalizeVisibleArea) { Chris@1099: rect = v->getPaintRect(); Chris@1099: } Chris@197: Chris@902: sv_frame_t modelStart = m_model->getStartFrame(); Chris@902: sv_frame_t modelEnd = m_model->getEndFrame(); Chris@805: int 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@916: int h = v->getPaintHeight(); Chris@0: Chris@902: double srRatio = Chris@902: v->getViewManager()->getMainModelSampleRate() / m_model->getSampleRate(); Chris@0: Chris@1047: // the s-prefix values are source, i.e. model, column and bin numbers Chris@902: int sx0 = int((double(v->getFrameForX(x0)) / srRatio - double(modelStart)) Chris@812: / modelResolution); Chris@902: int sx1 = int((double(v->getFrameForX(x1)) / srRatio - double(modelStart)) Chris@812: / modelResolution); Chris@197: int sh = m_model->getHeight(); Chris@159: Chris@444: int symin = m_miny; Chris@444: int symax = m_maxy; Chris@444: if (symax <= symin) { Chris@444: symin = 0; Chris@444: symax = sh; Chris@444: } Chris@444: if (symin < 0) symin = 0; Chris@444: if (symax > sh) symax = sh; Chris@444: Chris@197: if (sx0 > 0) --sx0; Chris@197: fillCache(sx0 < 0 ? 0 : sx0, Chris@197: sx1 < 0 ? 0 : sx1); Chris@0: Chris@351: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::paint: height = "<< m_model->getHeight() << ", modelStart = " << modelStart << ", resolution = " << modelResolution << ", model rate = " << m_model->getSampleRate() << " (zoom level = " << v->getZoomLevel() << ", srRatio = " << srRatio << ")" << endl; Chris@351: #endif Chris@351: Chris@812: if (shouldPaintDenseIn(v)) { Chris@347: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "calling paintDense" << endl; Chris@347: #endif Chris@98: paintDense(v, paint, rect); Chris@98: return; Chris@98: } Chris@98: Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << endl; Chris@682: cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << endl; Chris@125: #endif Chris@0: Chris@25: QPoint illuminatePos; Chris@44: bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos); Chris@864: Chris@864: const int buflen = 40; Chris@864: char labelbuf[buflen]; Chris@25: Chris@197: for (int sx = sx0; sx <= sx1; ++sx) { Chris@0: Chris@1047: sv_frame_t fx = sx * modelResolution + modelStart; Chris@0: Chris@812: if (fx + modelResolution <= modelStart || fx > modelEnd) continue; Chris@0: Chris@1047: int rx0 = v->getXForFrame(int(double(fx) * srRatio)); Chris@1047: int rx1 = v->getXForFrame(int(double(fx + 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@444: for (int sy = symin; sy < symax; ++sy) { Chris@0: Chris@903: int ry0 = getIYForBin(v, sy); Chris@903: int ry1 = getIYForBin(v, sy + 1); Chris@534: QRect r(rx0, ry1, rw, ry0 - ry1); Chris@534: Chris@0: QRgb pixel = qRgb(255, 255, 255); Chris@461: if (sx >= 0 && sx < m_cache->width() && Chris@0: sy >= 0 && sy < m_cache->height()) { Chris@461: pixel = m_cache->pixel(sx, sy); Chris@0: } Chris@0: 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@287: paint.setPen(v->getForeground()); Chris@25: } Chris@25: } Chris@76: Chris@125: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@682: // cerr << "rect " << r.x() << "," << r.y() << " " Chris@682: // << r.width() << "x" << r.height() << endl; Chris@125: #endif Chris@25: Chris@25: paint.drawRect(r); Chris@0: Chris@54: if (showLabel) { Chris@461: if (sx >= 0 && sx < m_cache->width() && Chris@54: sy >= 0 && sy < m_cache->height()) { Chris@904: double value = m_model->getValueAt(sx, sy); Chris@864: snprintf(labelbuf, buflen, "%06f", value); Chris@54: QString text(labelbuf); Chris@1078: PaintAssistant::drawVisibleText Chris@1078: (v, Chris@1078: paint, Chris@1048: rx0 + 2, Chris@1048: ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(), Chris@1048: text, Chris@1078: PaintAssistant::OutlinedText); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@98: } Chris@0: Chris@98: void Chris@916: Colour3DPlotLayer::paintDense(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@98: { Chris@812: Profiler profiler("Colour3DPlotLayer::paintDense", true); Chris@463: if (!m_cache) return; Chris@463: Chris@903: double modelStart = double(m_model->getStartFrame()); Chris@903: double modelResolution = double(m_model->getResolution()); Chris@0: Chris@903: sv_samplerate_t mmsr = v->getViewManager()->getMainModelSampleRate(); Chris@903: sv_samplerate_t msr = m_model->getSampleRate(); Chris@903: double srRatio = mmsr / msr; Chris@159: Chris@98: int x0 = rect.left(); Chris@98: int x1 = rect.right() + 1; Chris@0: Chris@470: const int w = x1 - x0; // const so it can be used as array size below Chris@916: int h = v->getPaintHeight(); // we always paint full height Chris@160: int sh = m_model->getHeight(); Chris@98: Chris@444: int symin = m_miny; Chris@444: int symax = m_maxy; Chris@444: if (symax <= symin) { Chris@444: symin = 0; Chris@444: symax = sh; Chris@444: } Chris@444: if (symin < 0) symin = 0; Chris@444: if (symax > sh) symax = sh; Chris@444: Chris@466: QImage img(w, h, QImage::Format_Indexed8); Chris@466: img.setColorTable(m_cache->colorTable()); Chris@98: Chris@466: uchar *peaks = new uchar[w]; Chris@466: memset(peaks, 0, w); Chris@98: Chris@469: int zoomLevel = v->getZoomLevel(); Chris@469: Chris@469: QImage *source = m_cache; Chris@812: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "modelResolution " << modelResolution << ", srRatio " Chris@812: << srRatio << ", m_peakResolution " << m_peakResolution Chris@812: << ", zoomLevel " << zoomLevel << ", result " Chris@812: << ((modelResolution * srRatio * m_peakResolution) / zoomLevel) Chris@812: << endl; Chris@812: #endif Chris@474: Chris@474: if (m_peaksCache) { Chris@474: if (((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "using peaks cache" << endl; Chris@812: #endif Chris@474: source = m_peaksCache; Chris@474: modelResolution *= m_peakResolution; Chris@474: } else { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "not using peaks cache" << endl; Chris@812: #endif Chris@474: } Chris@469: } else { Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "have no peaks cache" << endl; Chris@812: #endif Chris@469: } Chris@469: Chris@469: int sw = source->width(); Chris@470: Chris@903: sv_frame_t xf = -1; Chris@903: sv_frame_t nxf = v->getFrameForX(x0); Chris@470: Chris@903: double epsilon = 0.000001; Chris@535: Chris@903: vector sxa(w*2); Chris@903: Chris@470: for (int x = 0; x < w; ++x) { Chris@470: Chris@470: xf = nxf; Chris@470: nxf = xf + zoomLevel; Chris@470: Chris@903: double sx0 = (double(xf) / srRatio - modelStart) / modelResolution; Chris@903: double sx1 = (double(nxf) / srRatio - modelStart) / modelResolution; Chris@470: Chris@535: sxa[x*2] = sx0; Chris@535: sxa[x*2 + 1] = sx1; Chris@470: } Chris@466: Chris@904: double logmin = symin+1, logmax = symax+1; Chris@532: LogRange::mapRange(logmin, logmax); Chris@532: Chris@812: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@812: cerr << "m_smooth = " << m_smooth << ", w = " << w << ", h = " << h << endl; Chris@812: #endif Chris@812: Chris@535: if (m_smooth) { Chris@535: Chris@535: for (int y = 0; y < h; ++y) { Chris@466: Chris@904: double sy = getBinForY(v, y) - 0.5; Chris@535: int syi = int(sy + epsilon); Chris@535: if (syi < 0 || syi >= source->height()) continue; Chris@531: Chris@535: uchar *targetLine = img.scanLine(y); Chris@535: uchar *sourceLine = source->scanLine(syi); Chris@535: uchar *nextSource; Chris@535: if (syi + 1 < source->height()) { Chris@535: nextSource = source->scanLine(syi + 1); Chris@535: } else { Chris@535: nextSource = sourceLine; Chris@535: } Chris@466: Chris@466: for (int x = 0; x < w; ++x) { Chris@98: Chris@535: targetLine[x] = 0; Chris@474: Chris@903: double sx0 = sxa[x*2]; Chris@537: if (sx0 < 0) continue; Chris@535: int sx0i = int(sx0 + epsilon); Chris@474: if (sx0i >= sw) break; Chris@98: Chris@903: double a = sourceLine[sx0i]; Chris@903: double b = a; Chris@903: double value; Chris@535: Chris@903: double sx1 = sxa[x*2+1]; Chris@535: if (sx1 > sx0 + 1.f) { Chris@535: int sx1i = int(sx1); Chris@535: bool have = false; Chris@535: for (int sx = sx0i; sx <= sx1i; ++sx) { Chris@535: if (sx < 0 || sx >= sw) continue; Chris@535: if (!have) { Chris@903: a = sourceLine[sx]; Chris@903: b = nextSource[sx]; Chris@535: have = true; Chris@535: } else { Chris@903: a = std::max(a, double(sourceLine[sx])); Chris@903: b = std::max(b, double(nextSource[sx])); Chris@535: } Chris@535: } Chris@903: double yprop = sy - syi; Chris@535: value = (a * (1.f - yprop) + b * yprop); Chris@535: } else { Chris@903: a = sourceLine[sx0i]; Chris@903: b = nextSource[sx0i]; Chris@903: double yprop = sy - syi; Chris@535: value = (a * (1.f - yprop) + b * yprop); Chris@535: int oi = sx0i + 1; Chris@903: double xprop = sx0 - sx0i; Chris@535: xprop -= 0.5; Chris@535: if (xprop < 0) { Chris@535: oi = sx0i - 1; Chris@535: xprop = -xprop; Chris@535: } Chris@535: if (oi < 0 || oi >= sw) oi = sx0i; Chris@903: a = sourceLine[oi]; Chris@903: b = nextSource[oi]; Chris@535: value = (value * (1.f - xprop) + Chris@535: (a * (1.f - yprop) + b * yprop) * xprop); Chris@98: } Chris@535: Chris@903: int vi = int(lrint(value)); Chris@535: if (vi > 255) vi = 255; Chris@535: if (vi < 0) vi = 0; Chris@535: targetLine[x] = uchar(vi); Chris@98: } Chris@466: } Chris@535: } else { Chris@535: Chris@904: double sy0 = getBinForY(v, 0); Chris@812: Chris@812: int psy0i = -1, psy1i = -1; Chris@812: Chris@535: for (int y = 0; y < h; ++y) { Chris@535: Chris@904: double sy1 = sy0; Chris@904: sy0 = getBinForY(v, double(y + 1)); Chris@535: Chris@535: int sy0i = int(sy0 + epsilon); Chris@535: int sy1i = int(sy1); Chris@535: Chris@535: uchar *targetLine = img.scanLine(y); Chris@535: Chris@812: if (sy0i == psy0i && sy1i == psy1i) { Chris@812: // same source scan line as just computed Chris@535: goto copy; Chris@535: } Chris@535: Chris@812: psy0i = sy0i; Chris@812: psy1i = sy1i; Chris@812: Chris@535: for (int x = 0; x < w; ++x) { Chris@535: peaks[x] = 0; Chris@535: } Chris@466: Chris@535: for (int sy = sy0i; sy <= sy1i; ++sy) { Chris@535: Chris@535: if (sy < 0 || sy >= source->height()) continue; Chris@535: Chris@535: uchar *sourceLine = source->scanLine(sy); Chris@535: Chris@535: for (int x = 0; x < w; ++x) { Chris@535: Chris@903: double sx1 = sxa[x*2 + 1]; Chris@537: if (sx1 < 0) continue; Chris@537: int sx1i = int(sx1); Chris@535: Chris@903: double sx0 = sxa[x*2]; Chris@537: if (sx0 < 0) continue; Chris@537: int sx0i = int(sx0 + epsilon); Chris@535: if (sx0i >= sw) break; Chris@535: Chris@535: uchar peak = 0; Chris@535: for (int sx = sx0i; sx <= sx1i; ++sx) { Chris@535: if (sx < 0 || sx >= sw) continue; Chris@535: if (sourceLine[sx] > peak) peak = sourceLine[sx]; Chris@535: } Chris@535: peaks[x] = peak; Chris@535: } Chris@535: } Chris@535: Chris@535: copy: Chris@535: for (int x = 0; x < w; ++x) { Chris@535: targetLine[x] = peaks[x]; Chris@535: } Chris@98: } Chris@0: } Chris@0: Chris@469: delete[] peaks; Chris@469: Chris@98: paint.drawImage(x0, 0, img); Chris@0: } Chris@0: Chris@28: bool Chris@916: Colour3DPlotLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, Chris@805: int &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@904: sv_frame_t left = (frame / resolution) * resolution; Chris@904: sv_frame_t 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@316: void Chris@316: Colour3DPlotLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@197: { Chris@316: QString s = QString("scale=\"%1\" " Chris@316: "colourScheme=\"%2\" " Chris@1099: "minY=\"%3\" " Chris@1099: "maxY=\"%4\" " Chris@1099: "invertVertical=\"%5\" " Chris@1099: "opaque=\"%6\" %7") Chris@1104: .arg(convertFromColourScale(m_colourScale)) Chris@197: .arg(m_colourMap) Chris@445: .arg(m_miny) Chris@465: .arg(m_maxy) Chris@465: .arg(m_invertVertical ? "true" : "false") Chris@534: .arg(m_opaque ? "true" : "false") Chris@536: .arg(QString("binScale=\"%1\" smooth=\"%2\" gain=\"%3\" ") Chris@1103: .arg(int(m_binScale)) Chris@536: .arg(m_smooth ? "true" : "false") Chris@536: .arg(m_gain)); Chris@535: Chris@1099: // New-style normalization attributes, allowing for more types of Chris@1099: // normalization in future: write out the column normalization Chris@1099: // type separately, and then whether we are normalizing visible Chris@1099: // area as well afterwards Chris@1099: Chris@1099: s += QString("columnNormalization=\"%1\" ") Chris@1104: .arg(m_normalization == ColumnNormalization::Max1 ? "peak" : Chris@1104: m_normalization == ColumnNormalization::Hybrid ? "hybrid" : "none"); Chris@1099: Chris@1099: // Old-style normalization attribute, for backward compatibility Chris@1099: Chris@1099: s += QString("normalizeColumns=\"%1\" ") Chris@1104: .arg(m_normalization == ColumnNormalization::Max1 ? "true" : "false"); Chris@1099: Chris@1099: // And this applies to both old- and new-style attributes Chris@1099: Chris@1099: s += QString("normalizeVisibleArea=\"%1\" ") Chris@1104: .arg(m_normalizeVisibleArea ? "true" : "false"); Chris@1099: Chris@316: Layer::toXml(stream, indent, extraAttributes + " " + s); Chris@197: } Chris@197: Chris@197: void Chris@197: Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes) Chris@197: { Chris@445: bool ok = false, alsoOk = false; Chris@197: Chris@1105: ColourScaleType colourScale = convertToColourScale Chris@1099: (attributes.value("colourScale").toInt(&ok)); Chris@1099: if (ok) setColourScale(colourScale); Chris@197: Chris@197: int colourMap = attributes.value("colourScheme").toInt(&ok); Chris@197: if (ok) setColourMap(colourMap); Chris@197: Chris@1103: BinScale binScale = (BinScale) Chris@1099: attributes.value("binScale").toInt(&ok); Chris@1099: if (ok) setBinScale(binScale); Chris@445: Chris@465: bool invertVertical = Chris@465: (attributes.value("invertVertical").trimmed() == "true"); Chris@465: setInvertVertical(invertVertical); Chris@465: Chris@465: bool opaque = Chris@465: (attributes.value("opaque").trimmed() == "true"); Chris@535: setOpaque(opaque); Chris@535: Chris@535: bool smooth = Chris@535: (attributes.value("smooth").trimmed() == "true"); Chris@535: setSmooth(smooth); Chris@465: Chris@536: float gain = attributes.value("gain").toFloat(&ok); Chris@536: if (ok) setGain(gain); Chris@536: Chris@445: float min = attributes.value("minY").toFloat(&ok); Chris@445: float max = attributes.value("maxY").toFloat(&alsoOk); Chris@445: if (ok && alsoOk) setDisplayExtents(min, max); Chris@1099: Chris@1099: bool haveNewStyleNormalization = false; Chris@1099: Chris@1099: QString columnNormalization = attributes.value("columnNormalization"); Chris@1099: Chris@1099: if (columnNormalization != "") { Chris@1099: Chris@1099: haveNewStyleNormalization = true; Chris@1099: Chris@1099: if (columnNormalization == "peak") { Chris@1104: setNormalization(ColumnNormalization::Max1); Chris@1099: } else if (columnNormalization == "hybrid") { Chris@1104: setNormalization(ColumnNormalization::Hybrid); Chris@1099: } else if (columnNormalization == "none") { Chris@1104: setNormalization(ColumnNormalization::None); Chris@1099: } else { Chris@1099: cerr << "NOTE: Unknown or unsupported columnNormalization attribute \"" Chris@1099: << columnNormalization << "\"" << endl; Chris@1099: } Chris@1099: } Chris@1099: Chris@1099: if (!haveNewStyleNormalization) { Chris@1099: Chris@1104: setNormalization(ColumnNormalization::None); Chris@1104: Chris@1099: bool normalizeColumns = Chris@1099: (attributes.value("normalizeColumns").trimmed() == "true"); Chris@1099: if (normalizeColumns) { Chris@1104: setNormalization(ColumnNormalization::Max1); Chris@1099: } Chris@1099: Chris@1099: bool normalizeHybrid = Chris@1099: (attributes.value("normalizeHybrid").trimmed() == "true"); Chris@1099: if (normalizeHybrid) { Chris@1104: setNormalization(ColumnNormalization::Hybrid); Chris@1099: } Chris@1099: } Chris@1099: Chris@1099: bool normalizeVisibleArea = Chris@1099: (attributes.value("normalizeVisibleArea").trimmed() == "true"); Chris@1104: setNormalizeVisibleArea(normalizeVisibleArea); Chris@1099: Chris@1099: //!!! todo: check save/reload scaling, compare with Chris@1099: //!!! SpectrogramLayer, compare with prior SV versions, compare Chris@1099: //!!! with Tony v1 and v2 and their save files Chris@197: } Chris@197: