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@59: This file copyright 2006 Chris Cannam. 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 "TimeValueLayer.h" Chris@0: Chris@128: #include "data/model/Model.h" Chris@0: #include "base/RealTime.h" Chris@0: #include "base/Profiler.h" Chris@197: #include "base/LogRange.h" Chris@437: #include "base/RangeMapper.h" Chris@960: #include "base/Pitch.h" Chris@128: #include "view/View.h" Chris@0: Chris@128: #include "data/model/SparseTimeValueModel.h" Chris@340: #include "data/model/Labeller.h" Chris@0: Chris@70: #include "widgets/ItemEditDialog.h" Chris@125: #include "widgets/ListInputDialog.h" Chris@701: #include "widgets/TextAbbrev.h" Chris@70: Chris@1078: #include "ColourDatabase.h" Chris@376: #include "ColourMapper.h" Chris@691: #include "PianoScale.h" Chris@698: #include "LinearNumericalScale.h" Chris@698: #include "LogNumericalScale.h" Chris@699: #include "LinearColourScale.h" Chris@699: #include "LogColourScale.h" Chris@1078: #include "PaintAssistant.h" Chris@66: Chris@0: #include Chris@6: #include Chris@21: #include Chris@125: #include Chris@316: #include Chris@360: #include Chris@340: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@526: //#define DEBUG_TIME_VALUE_LAYER 1 Chris@526: Chris@44: TimeValueLayer::TimeValueLayer() : Chris@287: SingleColourLayer(), Chris@21: m_editing(false), Chris@23: m_originalPoint(0, 0.0, tr("New Point")), Chris@21: m_editingPoint(0, 0.0, tr("New Point")), Chris@1408: m_editingCommand(nullptr), Chris@197: m_colourMap(0), Chris@1362: m_colourInverted(false), Chris@66: m_plotStyle(PlotConnectedPoints), Chris@437: m_verticalScale(AutoAlignScale), Chris@513: m_drawSegmentDivisions(true), Chris@553: m_derivative(false), Chris@437: m_scaleMinimum(0), Chris@437: m_scaleMaximum(0) Chris@0: { Chris@44: Chris@0: } Chris@0: Chris@1470: int Chris@1470: TimeValueLayer::getCompletion(LayerGeometryProvider *) const Chris@1470: { Chris@1470: auto model = ModelById::get(m_model); Chris@1470: if (model) return model->getCompletion(); Chris@1470: else return 0; Chris@1470: } Chris@1470: Chris@0: void Chris@1470: TimeValueLayer::setModel(ModelId modelId) Chris@0: { Chris@1471: auto newModel = ModelById::getAs(modelId); Chris@1471: Chris@1471: if (!modelId.isNone() && !newModel) { Chris@1471: throw std::logic_error("Not a SparseTimeValueModel"); Chris@1471: } Chris@1471: Chris@1470: if (m_model == modelId) return; Chris@1470: m_model = modelId; Chris@1470: Chris@1471: if (newModel) { Chris@1471: Chris@1471: connectSignals(m_model); Chris@0: Chris@1471: m_scaleMinimum = 0; Chris@1471: m_scaleMaximum = 0; Chris@0: Chris@1471: if (newModel->getRDFTypeURI().endsWith("Segment")) { Chris@1471: setPlotStyle(PlotSegmentation); Chris@1471: } Chris@1471: if (newModel->getRDFTypeURI().endsWith("Change")) { Chris@1471: setPlotStyle(PlotSegmentation); Chris@1471: } Chris@494: } Chris@1471: Chris@0: emit modelReplaced(); Chris@0: } Chris@0: Chris@0: Layer::PropertyList Chris@0: TimeValueLayer::getProperties() const Chris@0: { Chris@287: PropertyList list = SingleColourLayer::getProperties(); Chris@87: list.push_back("Plot Type"); Chris@87: list.push_back("Vertical Scale"); Chris@100: list.push_back("Scale Units"); Chris@513: list.push_back("Draw Segment Division Lines"); Chris@553: list.push_back("Show Derivative"); Chris@0: return list; Chris@0: } Chris@0: Chris@87: QString Chris@87: TimeValueLayer::getPropertyLabel(const PropertyName &name) const Chris@87: { Chris@87: if (name == "Plot Type") return tr("Plot Type"); Chris@87: if (name == "Vertical Scale") return tr("Vertical Scale"); Chris@100: if (name == "Scale Units") return tr("Scale Units"); Chris@513: if (name == "Draw Segment Division Lines") return tr("Draw Segment Division Lines"); Chris@553: if (name == "Show Derivative") return tr("Show Derivative"); Chris@287: return SingleColourLayer::getPropertyLabel(name); Chris@87: } Chris@87: Chris@515: QString Chris@515: TimeValueLayer::getPropertyIconName(const PropertyName &name) const Chris@515: { Chris@515: if (name == "Draw Segment Division Lines") return "lines"; Chris@553: if (name == "Show Derivative") return "derivative"; Chris@515: return ""; Chris@515: } Chris@515: Chris@0: Layer::PropertyType Chris@0: TimeValueLayer::getPropertyType(const PropertyName &name) const Chris@0: { Chris@287: if (name == "Plot Type") return ValueProperty; Chris@287: if (name == "Vertical Scale") return ValueProperty; Chris@100: if (name == "Scale Units") return UnitsProperty; Chris@1198: if (name == "Colour" && m_plotStyle == PlotSegmentation) return ColourMapProperty; Chris@513: if (name == "Draw Segment Division Lines") return ToggleProperty; Chris@553: if (name == "Show Derivative") return ToggleProperty; Chris@287: return SingleColourLayer::getPropertyType(name); Chris@0: } Chris@0: Chris@198: QString Chris@198: TimeValueLayer::getPropertyGroupName(const PropertyName &name) const Chris@198: { Chris@198: if (name == "Vertical Scale" || name == "Scale Units") { Chris@198: return tr("Scale"); Chris@198: } Chris@553: if (name == "Plot Type" || name == "Draw Segment Division Lines" || Chris@553: name == "Show Derivative") { Chris@513: return tr("Plot Type"); Chris@513: } Chris@287: return SingleColourLayer::getPropertyGroupName(name); Chris@198: } Chris@198: Chris@1470: bool Chris@1470: TimeValueLayer::needsTextLabelHeight() const Chris@1470: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1470: if (!model) return false; Chris@1470: return m_plotStyle == PlotSegmentation && model->hasTextLabels(); Chris@1470: } Chris@1470: Chris@698: QString Chris@698: TimeValueLayer::getScaleUnits() const Chris@698: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (model) return model->getScaleUnits(); Chris@698: else return ""; Chris@698: } Chris@698: Chris@0: int Chris@0: TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@1266: int *min, int *max, int *deflt) const Chris@0: { Chris@216: int val = 0; Chris@0: Chris@287: if (name == "Colour" && m_plotStyle == PlotSegmentation) { Chris@197: Chris@287: if (min) *min = 0; Chris@287: if (max) *max = ColourMapper::getColourMapCount() - 1; Chris@287: if (deflt) *deflt = 0; Chris@197: Chris@287: val = m_colourMap; Chris@0: Chris@87: } else if (name == "Plot Type") { Chris@1266: Chris@1266: if (min) *min = 0; Chris@1266: if (max) *max = 6; Chris@216: if (deflt) *deflt = int(PlotConnectedPoints); Chris@1266: Chris@1266: val = int(m_plotStyle); Chris@0: Chris@87: } else if (name == "Vertical Scale") { Chris@1266: Chris@1266: if (min) *min = 0; Chris@1266: if (max) *max = 3; Chris@216: if (deflt) *deflt = int(AutoAlignScale); Chris@1266: Chris@1266: val = int(m_verticalScale); Chris@66: Chris@100: } else if (name == "Scale Units") { Chris@100: Chris@216: if (deflt) *deflt = 0; Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (model) { Chris@216: val = UnitDatabase::getInstance()->getUnitId Chris@698: (getScaleUnits()); Chris@100: } Chris@100: Chris@513: } else if (name == "Draw Segment Division Lines") { Chris@513: Chris@513: if (min) *min = 0; Chris@1365: if (max) *max = 1; Chris@513: if (deflt) *deflt = 1; Chris@513: val = (m_drawSegmentDivisions ? 1.0 : 0.0); Chris@513: Chris@553: } else if (name == "Show Derivative") { Chris@553: Chris@553: if (min) *min = 0; Chris@1365: if (max) *max = 1; Chris@553: if (deflt) *deflt = 0; Chris@553: val = (m_derivative ? 1.0 : 0.0); Chris@553: Chris@0: } else { Chris@1266: Chris@1266: val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); Chris@0: } Chris@0: Chris@216: return val; Chris@0: } Chris@0: Chris@0: QString Chris@0: TimeValueLayer::getPropertyValueLabel(const PropertyName &name, Chris@1266: int value) const Chris@0: { Chris@287: if (name == "Colour" && m_plotStyle == PlotSegmentation) { Chris@1362: return ColourMapper::getColourMapLabel(value); Chris@87: } else if (name == "Plot Type") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Points"); Chris@1266: case 1: return tr("Stems"); Chris@1266: case 2: return tr("Connected Points"); Chris@1266: case 3: return tr("Lines"); Chris@1266: case 4: return tr("Curve"); Chris@1266: case 5: return tr("Segmentation"); Chris@1266: case 6: return tr("Discrete Curves"); Chris@1266: } Chris@87: } else if (name == "Vertical Scale") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Auto-Align"); Chris@1266: case 1: return tr("Linear"); Chris@1266: case 2: return tr("Log"); Chris@1266: case 3: return tr("+/-1"); Chris@1266: } Chris@0: } Chris@287: return SingleColourLayer::getPropertyValueLabel(name, value); Chris@0: } Chris@0: Chris@0: void Chris@0: TimeValueLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@287: if (name == "Colour" && m_plotStyle == PlotSegmentation) { Chris@287: setFillColourMap(value); Chris@87: } else if (name == "Plot Type") { Chris@1266: setPlotStyle(PlotStyle(value)); Chris@87: } else if (name == "Vertical Scale") { Chris@1266: setVerticalScale(VerticalScale(value)); Chris@100: } else if (name == "Scale Units") { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (model) { Chris@1474: model->setScaleUnits Chris@100: (UnitDatabase::getInstance()->getUnitById(value)); Chris@1481: emit modelChanged(m_model); Chris@100: } Chris@513: } else if (name == "Draw Segment Division Lines") { Chris@513: setDrawSegmentDivisions(value > 0.5); Chris@553: } else if (name == "Show Derivative") { Chris@553: setShowDerivative(value > 0.5); Chris@287: } else { Chris@287: SingleColourLayer::setProperty(name, value); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@197: TimeValueLayer::setFillColourMap(int map) Chris@197: { Chris@197: if (m_colourMap == map) return; Chris@197: m_colourMap = map; Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: void Chris@0: TimeValueLayer::setPlotStyle(PlotStyle style) Chris@0: { Chris@0: if (m_plotStyle == style) return; Chris@197: bool colourTypeChanged = (style == PlotSegmentation || Chris@197: m_plotStyle == PlotSegmentation); Chris@0: m_plotStyle = style; Chris@197: if (colourTypeChanged) { Chris@197: emit layerParameterRangesChanged(); Chris@197: } Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@66: void Chris@66: TimeValueLayer::setVerticalScale(VerticalScale scale) Chris@66: { Chris@66: if (m_verticalScale == scale) return; Chris@66: m_verticalScale = scale; Chris@66: emit layerParametersChanged(); Chris@66: } Chris@66: Chris@513: void Chris@513: TimeValueLayer::setDrawSegmentDivisions(bool draw) Chris@513: { Chris@513: if (m_drawSegmentDivisions == draw) return; Chris@513: m_drawSegmentDivisions = draw; Chris@513: emit layerParametersChanged(); Chris@513: } Chris@513: Chris@553: void Chris@553: TimeValueLayer::setShowDerivative(bool show) Chris@553: { Chris@553: if (m_derivative == show) return; Chris@553: m_derivative = show; Chris@553: emit layerParametersChanged(); Chris@553: } Chris@553: Chris@0: bool Chris@918: TimeValueLayer::isLayerScrollable(const LayerGeometryProvider *v) const Chris@0: { Chris@6: // We don't illuminate sections in the line or curve modes, so Chris@6: // they're always scrollable Chris@6: Chris@6: if (m_plotStyle == PlotLines || Chris@1266: m_plotStyle == PlotCurve || Chris@615: m_plotStyle == PlotDiscreteCurves) return true; Chris@6: Chris@0: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@0: } Chris@0: Chris@79: bool Chris@908: TimeValueLayer::getValueExtents(double &min, double &max, Chris@101: bool &logarithmic, QString &unit) const Chris@79: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return false; Chris@668: Chris@1474: min = model->getValueMinimum(); Chris@1474: max = model->getValueMaximum(); Chris@668: Chris@101: logarithmic = (m_verticalScale == LogScale); Chris@668: Chris@698: unit = getScaleUnits(); Chris@668: Chris@553: if (m_derivative) { Chris@908: max = std::max(fabs(min), fabs(max)); Chris@553: min = -max; Chris@553: } Chris@629: Chris@629: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::getValueExtents: min = " << min << ", max = " << max << endl; Chris@629: #endif Chris@629: Chris@668: if (!shouldAutoAlign() && !logarithmic && !m_derivative) { Chris@668: Chris@668: if (max == min) { Chris@668: max = max + 0.5; Chris@668: min = min - 0.5; Chris@668: } else { Chris@908: double margin = (max - min) / 10.0; Chris@668: max = max + margin; Chris@668: min = min - margin; Chris@668: } Chris@668: Chris@668: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::getValueExtents: min = " << min << ", max = " << max << " (after adjustment)" << endl; Chris@668: #endif Chris@668: } Chris@668: Chris@79: return true; Chris@79: } Chris@79: Chris@101: bool Chris@908: TimeValueLayer::getDisplayExtents(double &min, double &max) const Chris@101: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || shouldAutoAlign()) return false; Chris@101: Chris@437: if (m_scaleMinimum == m_scaleMaximum) { Chris@668: bool log; Chris@668: QString unit; Chris@668: getValueExtents(min, max, log, unit); Chris@553: } else { Chris@553: min = m_scaleMinimum; Chris@553: max = m_scaleMaximum; Chris@437: } Chris@437: Chris@553: if (m_derivative) { Chris@908: max = std::max(fabs(min), fabs(max)); Chris@553: min = -max; Chris@553: } Chris@437: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::getDisplayExtents: min = " << min << ", max = " << max << endl; Chris@526: #endif Chris@437: Chris@101: return true; Chris@101: } Chris@101: Chris@437: bool Chris@908: TimeValueLayer::setDisplayExtents(double min, double max) Chris@437: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return false; Chris@437: Chris@437: if (min == max) { Chris@437: if (min == 0.f) { Chris@437: max = 1.f; Chris@437: } else { Chris@437: max = min * 1.0001; Chris@437: } Chris@437: } Chris@437: Chris@437: m_scaleMinimum = min; Chris@437: m_scaleMaximum = max; Chris@437: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::setDisplayExtents: min = " << min << ", max = " << max << endl; Chris@526: #endif Chris@437: Chris@437: emit layerParametersChanged(); Chris@437: return true; Chris@437: } Chris@437: Chris@437: int Chris@437: TimeValueLayer::getVerticalZoomSteps(int &defaultStep) const Chris@437: { Chris@437: if (shouldAutoAlign()) return 0; Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return 0; Chris@437: Chris@439: defaultStep = 0; Chris@437: return 100; Chris@437: } Chris@437: Chris@437: int Chris@437: TimeValueLayer::getCurrentVerticalZoomStep() const Chris@437: { Chris@437: if (shouldAutoAlign()) return 0; Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return 0; Chris@437: Chris@437: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@437: if (!mapper) return 0; Chris@437: Chris@908: double dmin, dmax; Chris@437: getDisplayExtents(dmin, dmax); Chris@437: Chris@437: int nr = mapper->getPositionForValue(dmax - dmin); Chris@526: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::getCurrentVerticalZoomStep: dmin = " << dmin << ", dmax = " << dmax << ", nr = " << nr << endl; Chris@526: #endif Chris@526: Chris@437: delete mapper; Chris@437: Chris@437: return 100 - nr; Chris@437: } Chris@437: Chris@437: void Chris@437: TimeValueLayer::setVerticalZoomStep(int step) Chris@437: { Chris@437: if (shouldAutoAlign()) return; Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@437: Chris@437: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@437: if (!mapper) return; Chris@437: Chris@908: double min, max; Chris@437: bool logarithmic; Chris@437: QString unit; Chris@437: getValueExtents(min, max, logarithmic, unit); Chris@437: Chris@908: double dmin, dmax; Chris@437: getDisplayExtents(dmin, dmax); Chris@437: Chris@908: double newdist = mapper->getValueForPosition(100 - step); Chris@437: Chris@908: double newmin, newmax; Chris@437: Chris@437: if (logarithmic) { Chris@437: Chris@437: // see SpectrogramLayer::setVerticalZoomStep Chris@437: Chris@908: newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2; Chris@437: newmin = newmax - newdist; Chris@437: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "newmin = " << newmin << ", newmax = " << newmax << endl; Chris@526: #endif Chris@437: Chris@437: } else { Chris@908: double dmid = (dmax + dmin) / 2; Chris@437: newmin = dmid - newdist / 2; Chris@437: newmax = dmid + newdist / 2; Chris@437: } Chris@437: Chris@437: if (newmin < min) { Chris@437: newmax += (min - newmin); Chris@437: newmin = min; Chris@437: } Chris@437: if (newmax > max) { Chris@437: newmax = max; Chris@437: } Chris@437: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl; Chris@526: #endif Chris@437: Chris@437: setDisplayExtents(newmin, newmax); Chris@437: } Chris@437: Chris@437: RangeMapper * Chris@437: TimeValueLayer::getNewVerticalZoomRangeMapper() const Chris@437: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return nullptr; Chris@437: Chris@437: RangeMapper *mapper; Chris@437: Chris@908: double min, max; Chris@437: bool logarithmic; Chris@437: QString unit; Chris@437: getValueExtents(min, max, logarithmic, unit); Chris@437: Chris@1408: if (min == max) return nullptr; Chris@437: Chris@437: if (logarithmic) { Chris@437: mapper = new LogRangeMapper(0, 100, min, max, unit); Chris@437: } else { Chris@437: mapper = new LinearRangeMapper(0, 100, min, max, unit); Chris@437: } Chris@437: Chris@437: return mapper; Chris@437: } Chris@437: Chris@1429: EventVector Chris@918: TimeValueLayer::getLocalPoints(LayerGeometryProvider *v, int x) const Chris@0: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return {}; Chris@0: Chris@1431: // Return all points at a frame f, where f is the closest frame to Chris@1431: // pixel coordinate x whose pixel coordinate is both within a Chris@1431: // small (but somewhat arbitrary) fuzz distance from x and within Chris@1431: // the current view. If there is no such frame, return an empty Chris@1431: // vector. Chris@1431: Chris@908: sv_frame_t frame = v->getFrameForX(x); Chris@1431: Chris@1474: EventVector exact = model->getEventsStartingAt(frame); Chris@1431: if (!exact.empty()) return exact; Chris@0: Chris@1431: // overspill == 1, so one event either side of the given span Chris@1474: EventVector neighbouring = model->getEventsWithin Chris@1474: (frame, model->getResolution(), 1); Chris@1431: Chris@1431: double fuzz = v->scaleSize(2); Chris@1431: sv_frame_t suitable = 0; Chris@1431: bool have = false; Chris@1429: Chris@1431: for (Event e: neighbouring) { Chris@1431: sv_frame_t f = e.getFrame(); Chris@1431: if (f < v->getStartFrame() || f > v->getEndFrame()) { Chris@1431: continue; Chris@1431: } Chris@1431: int px = v->getXForFrame(f); Chris@1431: if ((px > x && px - x > fuzz) || (px < x && x - px > fuzz + 3)) { Chris@1431: continue; Chris@1431: } Chris@1431: if (!have) { Chris@1431: suitable = f; Chris@1431: have = true; Chris@1431: } else if (llabs(frame - f) < llabs(suitable - f)) { Chris@1431: suitable = f; Chris@1266: } Chris@28: } Chris@28: Chris@1431: if (have) { Chris@1474: return model->getEventsStartingAt(suitable); Chris@1431: } else { Chris@1431: return {}; Chris@1431: } Chris@0: } Chris@0: Chris@25: QString Chris@1431: TimeValueLayer::getLabelPreceding(sv_frame_t frame) const Chris@552: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !model->hasTextLabels()) return ""; Chris@1431: Chris@1431: Event e; Chris@1474: if (model->getNearestEventMatching Chris@1431: (frame, Chris@1431: [](Event e) { return e.hasLabel() && e.getLabel() != ""; }, Chris@1431: EventSeries::Backward, Chris@1431: e)) { Chris@1431: return e.getLabel(); Chris@552: } Chris@1431: Chris@552: return ""; Chris@552: } Chris@552: Chris@552: QString Chris@918: TimeValueLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const Chris@0: { Chris@25: int x = pos.x(); Chris@0: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !model->getSampleRate()) return ""; Chris@0: Chris@1429: EventVector points = getLocalPoints(v, x); Chris@0: Chris@0: if (points.empty()) { Chris@1474: if (!model->isReady()) { Chris@1266: return tr("In progress"); Chris@1266: } else { Chris@1266: return tr("No local points"); Chris@1266: } Chris@0: } Chris@0: Chris@1429: sv_frame_t useFrame = points.begin()->getFrame(); Chris@0: Chris@1474: RealTime rt = RealTime::frame2RealTime(useFrame, model->getSampleRate()); Chris@25: Chris@960: QString valueText; Chris@1429: float value = points.begin()->getValue(); Chris@960: QString unit = getScaleUnits(); Chris@960: Chris@960: if (unit == "Hz") { Chris@960: valueText = tr("%1 Hz (%2, %3)") Chris@960: .arg(value) Chris@960: .arg(Pitch::getPitchLabelForFrequency(value)) Chris@960: .arg(Pitch::getPitchForFrequency(value)); Chris@960: } else if (unit != "") { Chris@960: valueText = tr("%1 %2").arg(value).arg(unit); Chris@960: } else { Chris@960: valueText = tr("%1").arg(value); Chris@960: } Chris@960: Chris@25: QString text; Chris@0: Chris@1429: if (points.begin()->getLabel() == "") { Chris@1266: text = QString(tr("Time:\t%1\nValue:\t%2\nNo label")) Chris@1266: .arg(rt.toText(true).c_str()) Chris@1266: .arg(valueText); Chris@101: } else { Chris@1266: text = QString(tr("Time:\t%1\nValue:\t%2\nLabel:\t%4")) Chris@1266: .arg(rt.toText(true).c_str()) Chris@1266: .arg(valueText) Chris@1429: .arg(points.begin()->getLabel()); Chris@25: } Chris@0: Chris@44: pos = QPoint(v->getXForFrame(useFrame), Chris@1429: getYForValue(v, points.begin()->getValue())); Chris@25: return text; Chris@0: } Chris@0: Chris@28: bool Chris@1429: TimeValueLayer::snapToFeatureFrame(LayerGeometryProvider *v, Chris@1429: sv_frame_t &frame, Chris@1266: int &resolution, Chris@1266: SnapType snap) const Chris@13: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) { Chris@1266: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@13: } Chris@13: Chris@1431: // SnapLeft / SnapRight: return frame of nearest feature in that Chris@1431: // direction no matter how far away Chris@1431: // Chris@1431: // SnapNeighbouring: return frame of feature that would be used in Chris@1431: // an editing operation, i.e. closest feature in either direction Chris@1431: // but only if it is "close enough" Chris@1431: Chris@1474: resolution = model->getResolution(); Chris@13: Chris@28: if (snap == SnapNeighbouring) { Chris@1431: EventVector points = getLocalPoints(v, v->getXForFrame(frame)); Chris@1266: if (points.empty()) return false; Chris@1429: frame = points.begin()->getFrame(); Chris@1266: return true; Chris@13: } Chris@13: Chris@1431: Event e; Chris@1474: if (model->getNearestEventMatching Chris@1431: (frame, Chris@1431: [](Event) { return true; }, Chris@1431: snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward, Chris@1431: e)) { Chris@1431: frame = e.getFrame(); Chris@1431: return true; Chris@1431: } Chris@1431: Chris@1431: return false; Chris@13: } Chris@13: Chris@517: bool Chris@1431: TimeValueLayer::snapToSimilarFeature(LayerGeometryProvider *v, Chris@1431: sv_frame_t &frame, Chris@805: int &resolution, Chris@517: SnapType snap) const Chris@517: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) { Chris@1266: return Layer::snapToSimilarFeature(v, frame, resolution, snap); Chris@517: } Chris@517: Chris@1432: // snap is only permitted to be SnapLeft or SnapRight here. Chris@1432: Chris@1474: resolution = model->getResolution(); Chris@517: Chris@1431: Event ref; Chris@1431: Event e; Chris@1431: float matchvalue; Chris@1431: bool found; Chris@517: Chris@1474: found = model->getNearestEventMatching Chris@1431: (frame, [](Event) { return true; }, EventSeries::Backward, ref); Chris@1429: Chris@1431: if (!found) { Chris@1431: return false; Chris@517: } Chris@517: Chris@1431: matchvalue = ref.getValue(); Chris@1431: Chris@1474: found = model->getNearestEventMatching Chris@1431: (frame, Chris@1431: [matchvalue](Event e) { Chris@1431: double epsilon = 0.0001; Chris@1431: return fabs(e.getValue() - matchvalue) < epsilon; Chris@1431: }, Chris@1431: snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward, Chris@1431: e); Chris@517: Chris@1431: if (!found) { Chris@1431: return false; Chris@517: } Chris@517: Chris@1431: frame = e.getFrame(); Chris@1431: return true; Chris@517: } Chris@517: Chris@101: void Chris@918: TimeValueLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const Chris@101: { Chris@101: min = 0.0; Chris@101: max = 0.0; Chris@101: log = false; Chris@101: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@1474: Chris@296: if (shouldAutoAlign()) { Chris@101: Chris@1537: if (!v->getVisibleExtentsForUnit(getScaleUnits(), min, max, log)) { Chris@1474: min = model->getValueMinimum(); Chris@1474: max = model->getValueMaximum(); Chris@1540: } else { Chris@1540: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1540: SVCERR << "getScaleExtents: view returned min = " << min Chris@1540: << ", max = " << max << ", log = " << log << endl; Chris@1540: #endif Chris@1540: if (log) { Chris@1540: LogRange::mapRange(min, max); Chris@1540: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1540: SVCERR << "getScaleExtents: mapped to min = " << min Chris@1540: << ", max = " << max << endl; Chris@1540: #endif Chris@1540: } Chris@101: } Chris@101: Chris@101: } else if (m_verticalScale == PlusMinusOneScale) { Chris@101: Chris@101: min = -1.0; Chris@101: max = 1.0; Chris@101: Chris@101: } else { Chris@101: Chris@437: getDisplayExtents(min, max); Chris@632: Chris@101: if (m_verticalScale == LogScale) { Chris@197: LogRange::mapRange(min, max); Chris@101: log = true; Chris@101: } Chris@101: } Chris@101: Chris@629: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::getScaleExtents: min = " << min << ", max = " << max << endl; Chris@629: #endif Chris@101: } Chris@101: Chris@21: int Chris@918: TimeValueLayer::getYForValue(LayerGeometryProvider *v, double val) const Chris@21: { Chris@908: double min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@918: int h = v->getPaintHeight(); Chris@79: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1540: SVCERR << "getYForValue(" << val << "): min " << min << ", max " Chris@1540: << max << ", log " << logarithmic << endl; Chris@526: #endif Chris@101: Chris@101: if (logarithmic) { Chris@197: val = LogRange::map(val); Chris@1540: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1540: SVCERR << "-> " << val << endl; Chris@1540: #endif Chris@79: } Chris@79: Chris@66: return int(h - ((val - min) * h) / (max - min)); Chris@21: } Chris@21: Chris@908: double Chris@918: TimeValueLayer::getValueForY(LayerGeometryProvider *v, int y) const Chris@21: { Chris@908: double min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@918: int h = v->getPaintHeight(); Chris@21: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@908: double val = min + (double(h - y) * double(max - min)) / h; Chris@101: Chris@101: if (logarithmic) { Chris@437: val = LogRange::map(val); Chris@101: } Chris@101: Chris@101: return val; Chris@21: } Chris@21: Chris@296: bool Chris@296: TimeValueLayer::shouldAutoAlign() const Chris@296: { Chris@698: QString unit = getScaleUnits(); Chris@296: return (m_verticalScale == AutoAlignScale && unit != ""); Chris@296: } Chris@296: Chris@68: QColor Chris@918: TimeValueLayer::getColourForValue(LayerGeometryProvider *v, double val) const Chris@68: { Chris@908: double min, max; Chris@101: bool log; Chris@101: getScaleExtents(v, min, max, log); Chris@68: Chris@197: if (min > max) std::swap(min, max); Chris@197: if (max == min) max = min + 1; Chris@197: Chris@101: if (log) { Chris@197: val = LogRange::map(val); Chris@68: } Chris@68: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::getColourForValue: min " << min << ", max " Chris@682: << max << ", log " << log << ", value " << val << endl; Chris@526: #endif Chris@68: Chris@1362: QColor solid = ColourMapper(m_colourMap, m_colourInverted, min, max).map(val); Chris@197: return QColor(solid.red(), solid.green(), solid.blue(), 120); Chris@68: } Chris@68: Chris@287: int Chris@287: TimeValueLayer::getDefaultColourHint(bool darkbg, bool &impose) Chris@287: { Chris@287: impose = false; Chris@287: return ColourDatabase::getInstance()->getColourIndex Chris@287: (QString(darkbg ? "Bright Green" : "Green")); Chris@287: } Chris@287: Chris@0: void Chris@916: TimeValueLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@0: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !model->isOK()) return; Chris@0: Chris@1474: sv_samplerate_t sampleRate = model->getSampleRate(); Chris@0: if (!sampleRate) return; Chris@0: Chris@1492: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1492: SVCERR << "TimeValueLayer[" << this << ", model " << getModel() << "]::paint in " << v->getId() << endl; Chris@1492: #endif Chris@1492: Chris@353: paint.setRenderHint(QPainter::Antialiasing, false); Chris@353: Chris@0: // Profiler profiler("TimeValueLayer::paint", true); Chris@0: Chris@0: int x0 = rect.left(), x1 = rect.right(); Chris@908: sv_frame_t frame0 = v->getFrameForX(x0); Chris@908: sv_frame_t frame1 = v->getFrameForX(x1); Chris@553: if (m_derivative) --frame0; Chris@0: Chris@1474: EventVector points(model->getEventsWithin(frame0, frame1 - frame0, 1)); Chris@1492: Chris@1492: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1492: SVCERR << "TimeValueLayer[" << this << "]::paint in " << v->getId() Chris@1492: << ": pixel extents " << x0 << " to " << x1 << ", frame extents " Chris@1492: << frame0 << " to " << frame1 << " yielding " << points.size() Chris@1492: << " points (of " << model->getAllEvents().size() Chris@1492: << " from frames " << model->getStartFrame() << " to " Chris@1492: << model->getEndFrame() << ")" << endl; Chris@1492: #endif Chris@1492: Chris@11: if (points.empty()) return; Chris@0: Chris@287: paint.setPen(getBaseQColor()); Chris@0: Chris@287: QColor brushColour(getBaseQColor()); Chris@0: brushColour.setAlpha(80); Chris@0: paint.setBrush(brushColour); Chris@0: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::paint: resolution is " Chris@1474: << model->getResolution() << " frames" << endl; Chris@526: #endif Chris@0: Chris@1474: double min = model->getValueMinimum(); Chris@1474: double max = model->getValueMaximum(); Chris@0: if (max == min) max = min + 1.0; Chris@0: Chris@918: int origin = int(nearbyint(v->getPaintHeight() - Chris@1266: (-min * v->getPaintHeight()) / (max - min))); Chris@0: Chris@0: QPoint localPos; Chris@908: sv_frame_t illuminateFrame = -1; Chris@0: Chris@44: if (v->shouldIlluminateLocalFeatures(this, localPos)) { Chris@1429: EventVector localPoints = getLocalPoints(v, localPos.x()); Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer: " << localPoints.size() << " local points" << endl; Chris@526: #endif Chris@1429: if (!localPoints.empty()) { Chris@1429: illuminateFrame = localPoints.begin()->getFrame(); Chris@1429: } Chris@0: } Chris@6: Chris@20: int w = Chris@1474: v->getXForFrame(frame0 + model->getResolution()) - Chris@1266: v->getXForFrame(frame0); Chris@7: Chris@629: if (m_plotStyle == PlotStems) { Chris@629: if (w < 2) w = 2; Chris@629: } else { Chris@629: if (w < 1) w = 1; Chris@629: } Chris@629: Chris@6: paint.save(); Chris@6: Chris@6: QPainterPath path; Chris@55: int pointCount = 0; Chris@79: Chris@79: int textY = 0; Chris@79: if (m_plotStyle == PlotSegmentation) { Chris@1537: textY = v->getTextLabelYCoord(this, paint); Chris@554: } else { Chris@554: int originY = getYForValue(v, 0.f); Chris@918: if (originY > 0 && originY < v->getPaintHeight()) { Chris@554: paint.save(); Chris@554: paint.setPen(getPartialShades(v)[1]); Chris@554: paint.drawLine(x0, originY, x1, originY); Chris@554: paint.restore(); Chris@554: } Chris@79: } Chris@6: Chris@908: sv_frame_t prevFrame = 0; Chris@615: Chris@1429: for (EventVector::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@0: Chris@553: if (m_derivative && i == points.begin()) continue; Chris@721: Chris@1429: Event p(*i); Chris@0: Chris@1429: double value = p.getValue(); Chris@553: if (m_derivative) { Chris@1429: EventVector::const_iterator j = i; Chris@553: --j; Chris@1429: value -= j->getValue(); Chris@553: } Chris@553: Chris@1429: int x = v->getXForFrame(p.getFrame()); Chris@1266: int y = getYForValue(v, value); Chris@0: Chris@615: bool gap = false; Chris@615: if (m_plotStyle == PlotDiscreteCurves) { Chris@721: if (value == 0.0) { Chris@721: // Treat zeros as gaps Chris@721: continue; Chris@721: } Chris@1429: gap = (p.getFrame() > prevFrame && Chris@1474: (p.getFrame() - prevFrame >= model->getResolution() * 2)); Chris@615: } Chris@615: Chris@79: if (m_plotStyle != PlotSegmentation) { Chris@79: textY = y - paint.fontMetrics().height() Chris@631: + paint.fontMetrics().ascent() - 1; Chris@372: if (textY < paint.fontMetrics().ascent() + 1) { Chris@372: textY = paint.fontMetrics().ascent() + 1; Chris@372: } Chris@79: } Chris@79: Chris@1266: bool haveNext = false; Chris@908: double nvalue = 0.f; Chris@908: sv_frame_t nf = v->getModelsEndFrame(); Chris@1266: int nx = v->getXForFrame(nf); Chris@1266: int ny = y; Chris@34: Chris@1429: EventVector::const_iterator j = i; Chris@1266: ++j; Chris@34: Chris@1266: if (j != points.end()) { Chris@1429: Event q(*j); Chris@1429: nvalue = q.getValue(); Chris@1429: if (m_derivative) nvalue -= p.getValue(); Chris@1429: nf = q.getFrame(); Chris@1266: nx = v->getXForFrame(nf); Chris@1266: ny = getYForValue(v, nvalue); Chris@1266: haveNext = true; Chris@76: } Chris@76: Chris@1429: // cout << "frame = " << p.getFrame() << ", x = " << x << ", haveNext = " << haveNext Chris@682: // << ", nx = " << nx << endl; Chris@34: Chris@1229: QPen pen(getBaseQColor()); Chris@1229: QBrush brush(brushColour); Chris@1229: Chris@615: if (m_plotStyle == PlotDiscreteCurves) { Chris@1229: pen = QPen(getBaseQColor(), 3); Chris@1229: brush = QBrush(Qt::NoBrush); Chris@615: } else if (m_plotStyle == PlotSegmentation) { Chris@1229: pen = QPen(getForegroundQColor(v)); Chris@1229: brush = QBrush(getColourForValue(v, value)); Chris@1266: } else if (m_plotStyle == PlotLines || Chris@1266: m_plotStyle == PlotCurve) { Chris@1229: brush = QBrush(Qt::NoBrush); Chris@1266: } Chris@1229: Chris@1401: paint.setPen(v->scalePen(pen)); Chris@1229: paint.setBrush(brush); Chris@1229: Chris@1266: if (m_plotStyle == PlotStems) { Chris@1266: if (y < origin - 1) { Chris@1266: paint.drawLine(x + w/2, y + 1, x + w/2, origin); Chris@1266: } else if (y > origin + 1) { Chris@1266: paint.drawLine(x + w/2, origin, x + w/2, y - 1); Chris@1266: } Chris@1266: } Chris@0: Chris@631: bool illuminate = false; Chris@631: Chris@1429: if (illuminateFrame == p.getFrame()) { Chris@6: Chris@1266: // not equipped to illuminate the right section in line Chris@1266: // or curve mode Chris@6: Chris@1266: if (m_plotStyle != PlotCurve && Chris@615: m_plotStyle != PlotDiscreteCurves && Chris@1266: m_plotStyle != PlotLines) { Chris@631: illuminate = true; Chris@631: } Chris@631: } Chris@0: Chris@1266: if (m_plotStyle != PlotLines && Chris@1266: m_plotStyle != PlotCurve && Chris@615: m_plotStyle != PlotDiscreteCurves && Chris@1266: m_plotStyle != PlotSegmentation) { Chris@631: if (illuminate) { Chris@631: paint.save(); Chris@1401: paint.setPen(v->scalePen(getForegroundQColor(v))); Chris@631: paint.setBrush(getForegroundQColor(v)); Chris@631: } Chris@326: if (m_plotStyle != PlotStems || Chris@326: w > 1) { Chris@326: paint.drawRect(x, y - 1, w, 2); Chris@326: } Chris@631: if (illuminate) { Chris@631: paint.restore(); Chris@631: } Chris@1266: } Chris@0: Chris@1266: if (m_plotStyle == PlotConnectedPoints || Chris@1266: m_plotStyle == PlotLines || Chris@615: m_plotStyle == PlotDiscreteCurves || Chris@1266: m_plotStyle == PlotCurve) { Chris@0: Chris@1266: if (haveNext) { Chris@3: Chris@1266: if (m_plotStyle == PlotConnectedPoints) { Chris@1266: Chris@79: paint.save(); Chris@1401: paint.setPen(v->scalePen(brushColour)); Chris@1266: paint.drawLine(x + w, y, nx, ny); Chris@79: paint.restore(); Chris@6: Chris@1266: } else if (m_plotStyle == PlotLines) { Chris@430: Chris@430: if (pointCount == 0) { Chris@430: path.moveTo(x + w/2, y); Chris@430: } Chris@6: Chris@1266: // paint.drawLine(x + w/2, y, nx + w/2, ny); Chris@430: path.lineTo(nx + w/2, ny); Chris@6: Chris@1266: } else { Chris@6: Chris@1266: double x0 = x + double(w)/2; Chris@1266: double x1 = nx + double(w)/2; Chris@1266: Chris@1266: double y0 = y; Chris@1266: double y1 = ny; Chris@55: Chris@615: if (m_plotStyle == PlotDiscreteCurves) { Chris@721: bool nextGap = Chris@721: (nvalue == 0.0) || Chris@1474: (nf - p.getFrame() >= model->getResolution() * 2); Chris@615: if (nextGap) { Chris@615: x1 = x0; Chris@615: y1 = y0; Chris@615: } Chris@615: } Chris@615: Chris@1266: if (pointCount == 0 || gap) { Chris@1266: path.moveTo((x0 + x1) / 2, (y0 + y1) / 2); Chris@1266: } Chris@6: Chris@1266: if (nx - x > 5) { Chris@1266: path.cubicTo(x0, y0, Chris@1266: x0, y0, Chris@1266: (x0 + x1) / 2, (y0 + y1) / 2); Chris@55: Chris@1266: // // or Chris@1266: // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2); Chris@55: Chris@1266: } else { Chris@883: path.lineTo(x0, y0); Chris@1266: path.lineTo((x0 + x1) / 2, (y0 + y1) / 2); Chris@1266: } Chris@1266: } Chris@1266: } Chris@1266: } Chris@0: Chris@1266: if (m_plotStyle == PlotSegmentation) { Chris@76: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "drawing rect" << endl; Chris@526: #endif Chris@1266: Chris@1266: if (nx <= x) continue; Chris@26: Chris@1401: paint.setPen(v->scalePen(QPen(getForegroundQColor(v), 2))); Chris@559: Chris@631: if (!illuminate) { Chris@513: if (!m_drawSegmentDivisions || Chris@513: nx < x + 5 || Chris@918: x >= v->getPaintWidth() - 1) { Chris@513: paint.setPen(Qt::NoPen); Chris@513: } Chris@1266: } Chris@26: Chris@1266: paint.drawRect(x, -1, nx - x, v->getPaintHeight() + 1); Chris@1266: } Chris@26: Chris@741: if (v->shouldShowFeatureLabels()) { Chris@629: Chris@1429: QString label = p.getLabel(); Chris@741: bool italic = false; Chris@741: Chris@741: if (label == "" && Chris@741: (m_plotStyle == PlotPoints || Chris@741: m_plotStyle == PlotSegmentation || Chris@741: m_plotStyle == PlotConnectedPoints)) { Chris@741: char lc[20]; Chris@1429: snprintf(lc, 20, "%.3g", p.getValue()); Chris@741: label = lc; Chris@741: italic = true; Chris@741: } Chris@741: Chris@1474: // Qt 5.13 deprecates QFontMetrics::width(), but its suggested Chris@1474: // replacement (horizontalAdvance) was only added in Qt 5.11 Chris@1474: // which is too new for us Chris@1474: #pragma GCC diagnostic ignored "-Wdeprecated-declarations" Chris@1474: Chris@741: if (label != "") { Chris@741: // Quick test for 20px before we do the slower test using metrics Chris@741: bool haveRoom = (nx > x + 20); Chris@741: haveRoom = (haveRoom && Chris@741: (nx > x + 6 + paint.fontMetrics().width(label))); Chris@741: if (haveRoom || Chris@741: (!haveNext && Chris@741: (pointCount == 0 || !italic))) { Chris@1429: PaintAssistant::drawVisibleText Chris@1429: (v, paint, x + 5, textY, label, Chris@1429: italic ? Chris@1429: PaintAssistant::OutlinedItalicText : Chris@1429: PaintAssistant::OutlinedText); Chris@741: } Chris@741: } Chris@629: } Chris@629: Chris@1429: prevFrame = p.getFrame(); Chris@632: ++pointCount; Chris@0: } Chris@6: Chris@691: if (m_plotStyle == PlotDiscreteCurves) { Chris@691: paint.setRenderHint(QPainter::Antialiasing, true); Chris@1266: paint.drawPath(path); Chris@691: } else if ((m_plotStyle == PlotCurve || m_plotStyle == PlotLines) Chris@691: && !path.isEmpty()) { Chris@1266: paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->getPaintWidth()); Chris@1266: paint.drawPath(path); Chris@6: } Chris@6: Chris@6: paint.restore(); Chris@6: Chris@6: // looks like save/restore doesn't deal with this: Chris@6: paint.setRenderHint(QPainter::Antialiasing, false); Chris@6: } Chris@6: Chris@42: int Chris@918: TimeValueLayer::getVerticalScaleWidth(LayerGeometryProvider *v, bool, QPainter &paint) const Chris@42: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) { Chris@1315: return 0; Chris@1315: } else if (shouldAutoAlign() && !valueExtentsMatchMine(v)) { Chris@701: return 0; Chris@701: } else if (m_plotStyle == PlotSegmentation) { Chris@699: if (m_verticalScale == LogScale) { Chris@699: return LogColourScale().getWidth(v, paint); Chris@699: } else { Chris@699: return LinearColourScale().getWidth(v, paint); Chris@699: } Chris@698: } else { Chris@699: if (m_verticalScale == LogScale) { Chris@699: return LogNumericalScale().getWidth(v, paint) + 10; // for piano Chris@699: } else { Chris@699: return LinearNumericalScale().getWidth(v, paint); Chris@699: } Chris@698: } Chris@42: } Chris@42: Chris@42: void Chris@918: TimeValueLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect) const Chris@42: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || model->isEmpty()) return; Chris@42: Chris@699: QString unit; Chris@908: double min, max; Chris@698: bool logarithmic; Chris@698: Chris@698: int w = getVerticalScaleWidth(v, false, paint); Chris@918: int h = v->getPaintHeight(); Chris@698: Chris@698: if (m_plotStyle == PlotSegmentation) { Chris@698: Chris@699: getValueExtents(min, max, logarithmic, unit); Chris@699: Chris@699: if (logarithmic) { Chris@699: LogRange::mapRange(min, max); Chris@699: LogColourScale().paintVertical(v, this, paint, 0, min, max); Chris@699: } else { Chris@699: LinearColourScale().paintVertical(v, this, paint, 0, min, max); Chris@699: } Chris@698: Chris@698: } else { Chris@698: Chris@699: getScaleExtents(v, min, max, logarithmic); Chris@699: Chris@698: if (logarithmic) { Chris@698: LogNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@698: } else { Chris@698: LinearNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@698: } Chris@698: Chris@698: if (logarithmic && (getScaleUnits() == "Hz")) { Chris@698: PianoScale().paintPianoVertical Chris@698: (v, paint, QRect(w - 10, 0, 10, h), Chris@698: LogRange::unmap(min), Chris@698: LogRange::unmap(max)); Chris@698: paint.drawLine(w, 0, w, h); Chris@698: } Chris@698: } Chris@698: Chris@698: if (getScaleUnits() != "") { Chris@701: int mw = w - 5; Chris@701: paint.drawText(5, Chris@701: 5 + paint.fontMetrics().ascent(), Chris@701: TextAbbrev::abbreviate(getScaleUnits(), Chris@701: paint.fontMetrics(), Chris@701: mw)); Chris@691: } Chris@42: } Chris@42: Chris@21: void Chris@918: TimeValueLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@21: { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl; Chris@526: #endif Chris@21: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@21: Chris@908: sv_frame_t frame = v->getFrameForX(e->x()); Chris@1474: int resolution = model->getResolution(); Chris@21: if (frame < 0) frame = 0; Chris@76: frame = (frame / resolution) * resolution; Chris@21: Chris@908: double value = getValueForY(v, e->y()); Chris@21: Chris@76: bool havePoint = false; Chris@76: Chris@1429: EventVector points = getLocalPoints(v, e->x()); Chris@76: if (!points.empty()) { Chris@1429: for (EventVector::iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@1429: if (((i->getFrame() / resolution) * resolution) != frame) { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1429: cerr << "ignoring out-of-range frame at " << i->getFrame() << endl; Chris@526: #endif Chris@76: continue; Chris@76: } Chris@76: m_editingPoint = *i; Chris@76: havePoint = true; Chris@76: } Chris@76: } Chris@76: Chris@76: if (!havePoint) { Chris@1429: m_editingPoint = Event(frame, float(value), tr("New Point")); Chris@76: } Chris@76: Chris@23: m_originalPoint = m_editingPoint; Chris@22: Chris@376: if (m_editingCommand) finish(m_editingCommand); Chris@1474: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Draw Point")); Chris@76: if (!havePoint) { Chris@1429: m_editingCommand->add(m_editingPoint); Chris@76: } Chris@22: Chris@21: m_editing = true; Chris@21: } Chris@21: Chris@21: void Chris@918: TimeValueLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@21: { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl; Chris@526: #endif Chris@21: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@21: Chris@908: sv_frame_t frame = v->getFrameForX(e->x()); Chris@1474: int resolution = model->getResolution(); Chris@21: if (frame < 0) frame = 0; Chris@76: frame = (frame / resolution) * resolution; Chris@21: Chris@908: double value = getValueForY(v, e->y()); Chris@21: Chris@1429: EventVector points = getLocalPoints(v, e->x()); Chris@76: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << points.size() << " points" << endl; Chris@526: #endif Chris@76: Chris@76: bool havePoint = false; Chris@76: Chris@76: if (!points.empty()) { Chris@1429: for (EventVector::iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@1429: if (i->getFrame() == m_editingPoint.getFrame() && Chris@1429: i->getValue() == m_editingPoint.getValue()) { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1429: cerr << "ignoring current editing point at " << i->getFrame() << ", " << i->getValue() << endl; Chris@526: #endif Chris@76: continue; Chris@76: } Chris@1429: if (((i->getFrame() / resolution) * resolution) != frame) { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1429: cerr << "ignoring out-of-range frame at " << i->getFrame() << endl; Chris@526: #endif Chris@76: continue; Chris@76: } Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1429: cerr << "adjusting to new point at " << i->getFrame() << ", " << i->getValue() << endl; Chris@526: #endif Chris@76: m_editingPoint = *i; Chris@76: m_originalPoint = m_editingPoint; Chris@1429: m_editingCommand->remove(m_editingPoint); Chris@76: havePoint = true; Chris@76: } Chris@76: } Chris@76: Chris@76: if (!havePoint) { Chris@1429: if (frame == m_editingPoint.getFrame()) { Chris@1429: m_editingCommand->remove(m_editingPoint); Chris@76: } Chris@76: } Chris@76: Chris@1429: m_editingPoint = m_editingPoint Chris@1429: .withFrame(frame) Chris@1429: .withValue(float(value)); Chris@1429: m_editingCommand->add(m_editingPoint); Chris@21: } Chris@21: Chris@21: void Chris@918: TimeValueLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *) Chris@21: { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::drawEnd" << endl; Chris@526: #endif Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@376: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@21: m_editing = false; Chris@21: } Chris@21: Chris@21: void Chris@918: TimeValueLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@335: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@335: Chris@1429: EventVector points = getLocalPoints(v, e->x()); Chris@335: if (points.empty()) return; Chris@335: Chris@335: m_editingPoint = *points.begin(); Chris@335: Chris@335: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@335: } Chris@335: Chris@335: m_editing = true; Chris@335: } Chris@335: Chris@335: void Chris@918: TimeValueLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *) Chris@335: { Chris@335: } Chris@335: Chris@335: void Chris@918: TimeValueLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e) Chris@335: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@335: Chris@335: m_editing = false; Chris@335: Chris@1429: EventVector points = getLocalPoints(v, e->x()); Chris@335: if (points.empty()) return; Chris@1429: if (points.begin()->getFrame() != m_editingPoint.getFrame() || Chris@1429: points.begin()->getValue() != m_editingPoint.getValue()) return; Chris@335: Chris@1474: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Erase Point")); Chris@1429: m_editingCommand->remove(m_editingPoint); Chris@376: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@335: m_editing = false; Chris@335: } Chris@335: Chris@335: void Chris@918: TimeValueLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@21: { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << endl; Chris@526: #endif Chris@21: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@21: Chris@1429: EventVector points = getLocalPoints(v, e->x()); Chris@21: if (points.empty()) return; Chris@21: Chris@21: m_editingPoint = *points.begin(); Chris@23: m_originalPoint = m_editingPoint; Chris@22: Chris@22: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@22: } Chris@22: Chris@21: m_editing = true; Chris@21: } Chris@21: Chris@21: void Chris@918: TimeValueLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@21: { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl; Chris@526: #endif Chris@21: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@21: Chris@908: sv_frame_t frame = v->getFrameForX(e->x()); Chris@21: if (frame < 0) frame = 0; Chris@1474: frame = frame / model->getResolution() * model->getResolution(); Chris@21: Chris@908: double value = getValueForY(v, e->y()); Chris@21: Chris@22: if (!m_editingCommand) { Chris@1474: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Drag Point")); Chris@22: } Chris@22: Chris@1429: m_editingCommand->remove(m_editingPoint); Chris@1429: m_editingPoint = m_editingPoint Chris@1429: .withFrame(frame) Chris@1429: .withValue(float(value)); Chris@1429: m_editingCommand->add(m_editingPoint); Chris@21: } Chris@21: Chris@21: void Chris@918: TimeValueLayer::editEnd(LayerGeometryProvider *, QMouseEvent *) Chris@21: { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "TimeValueLayer::editEnd" << endl; Chris@526: #endif Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@23: Chris@23: if (m_editingCommand) { Chris@23: Chris@1266: QString newName = m_editingCommand->getName(); Chris@23: Chris@1429: if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) { Chris@1429: if (m_editingPoint.getValue() != m_originalPoint.getValue()) { Chris@1266: newName = tr("Edit Point"); Chris@1266: } else { Chris@1266: newName = tr("Relocate Point"); Chris@1266: } Chris@1266: } else { Chris@1266: newName = tr("Change Point Value"); Chris@1266: } Chris@23: Chris@1266: m_editingCommand->setName(newName); Chris@1266: finish(m_editingCommand); Chris@23: } Chris@23: Chris@1408: m_editingCommand = nullptr; Chris@21: m_editing = false; Chris@21: } Chris@21: Chris@255: bool Chris@918: TimeValueLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e) Chris@70: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return false; Chris@70: Chris@1429: EventVector points = getLocalPoints(v, e->x()); Chris@255: if (points.empty()) return false; Chris@70: Chris@1429: Event point = *points.begin(); Chris@70: Chris@70: ItemEditDialog *dialog = new ItemEditDialog Chris@1474: (model->getSampleRate(), Chris@70: ItemEditDialog::ShowTime | Chris@70: ItemEditDialog::ShowValue | Chris@73: ItemEditDialog::ShowText, Chris@698: getScaleUnits()); Chris@70: Chris@1429: dialog->setFrameTime(point.getFrame()); Chris@1429: dialog->setValue(point.getValue()); Chris@1429: dialog->setText(point.getLabel()); Chris@70: Chris@70: if (dialog->exec() == QDialog::Accepted) { Chris@70: Chris@1429: Event newPoint = point Chris@1429: .withFrame(dialog->getFrameTime()) Chris@1429: .withValue(dialog->getValue()) Chris@1429: .withLabel(dialog->getText()); Chris@70: Chris@1429: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Edit Point")); Chris@1429: command->remove(point); Chris@1429: command->add(newPoint); Chris@376: finish(command); Chris@70: } Chris@70: Chris@70: delete dialog; Chris@255: return true; Chris@70: } Chris@70: Chris@70: void Chris@908: TimeValueLayer::moveSelection(Selection s, sv_frame_t newStartFrame) Chris@43: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@99: Chris@1429: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Drag Selection")); Chris@43: Chris@1429: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1429: for (Event p: points) { Chris@43: Chris@1429: Event newPoint = p.withFrame Chris@1429: (p.getFrame() + newStartFrame - s.getStartFrame()); Chris@1429: command->remove(p); Chris@1429: command->add(newPoint); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@43: void Chris@43: TimeValueLayer::resizeSelection(Selection s, Selection newSize) Chris@43: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !s.getDuration()) return; Chris@99: Chris@1429: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Resize Selection")); Chris@43: Chris@1429: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1429: double ratio = double(newSize.getDuration()) / double(s.getDuration()); Chris@1429: double oldStart = double(s.getStartFrame()); Chris@1429: double newStart = double(newSize.getStartFrame()); Chris@43: Chris@1429: for (Event p: points) { Chris@1429: Chris@1429: double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart; Chris@43: Chris@1429: Event newPoint = p Chris@1429: .withFrame(lrint(newFrame)); Chris@1429: command->remove(p); Chris@1429: command->add(newPoint); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@76: void Chris@76: TimeValueLayer::deleteSelection(Selection s) Chris@76: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@99: Chris@1429: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points")); Chris@76: Chris@1429: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@76: Chris@1429: for (Event p: points) { Chris@1429: command->remove(p); Chris@76: } Chris@76: Chris@376: finish(command); Chris@76: } Chris@76: Chris@76: void Chris@918: TimeValueLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to) Chris@76: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@99: Chris@1429: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@76: Chris@1429: for (Event p: points) { Chris@1429: to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame()))); Chris@76: } Chris@76: } Chris@76: Chris@125: bool Chris@1533: TimeValueLayer::paste(LayerGeometryProvider *v, const Clipboard &from, Chris@1533: sv_frame_t /* frameOffset */, bool interactive) Chris@76: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return false; Chris@99: Chris@1433: EventVector points = from.getPoints(); Chris@76: Chris@360: bool realign = false; Chris@360: Chris@360: if (clipboardHasDifferentAlignment(v, from)) { Chris@360: Chris@360: QMessageBox::StandardButton button = Chris@919: QMessageBox::question(v->getView(), tr("Re-align pasted items?"), Chris@360: tr("The items you are pasting came from a layer with different source material from this one. Do you want to re-align them in time, to match the source material for this layer?"), Chris@360: QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, Chris@360: QMessageBox::Yes); Chris@360: Chris@360: if (button == QMessageBox::Cancel) { Chris@360: return false; Chris@360: } Chris@360: Chris@360: if (button == QMessageBox::Yes) { Chris@360: realign = true; Chris@360: } Chris@360: } Chris@360: Chris@1429: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Paste")); Chris@76: Chris@125: enum ValueAvailability { Chris@125: UnknownAvailability, Chris@125: NoValues, Chris@125: SomeValues, Chris@125: AllValues Chris@125: }; Chris@125: Chris@340: Labeller::ValueType generation = Labeller::ValueNone; Chris@125: Chris@125: bool haveUsableLabels = false; Chris@340: Labeller labeller; Chris@1474: labeller.setSampleRate(model->getSampleRate()); Chris@125: Chris@125: if (interactive) { Chris@125: Chris@125: ValueAvailability availability = UnknownAvailability; Chris@125: Chris@1423: for (EventVector::const_iterator i = points.begin(); Chris@125: i != points.end(); ++i) { Chris@125: Chris@125: if (availability == UnknownAvailability) { Chris@1423: if (i->hasValue()) availability = AllValues; Chris@125: else availability = NoValues; Chris@125: continue; Chris@125: } Chris@125: Chris@1423: if (i->hasValue()) { Chris@125: if (availability == NoValues) { Chris@125: availability = SomeValues; Chris@125: } Chris@125: } else { Chris@125: if (availability == AllValues) { Chris@125: availability = SomeValues; Chris@125: } Chris@125: } Chris@125: Chris@125: if (!haveUsableLabels) { Chris@1423: if (i->hasLabel()) { Chris@125: if (i->getLabel().contains(QRegExp("[0-9]"))) { Chris@125: haveUsableLabels = true; Chris@125: } Chris@125: } Chris@125: } Chris@125: Chris@125: if (availability == SomeValues && haveUsableLabels) break; Chris@125: } Chris@125: Chris@125: if (availability == NoValues || availability == SomeValues) { Chris@125: Chris@125: QString text; Chris@125: if (availability == NoValues) { Chris@125: text = tr("The items you are pasting do not have values.\nWhat values do you want to use for these items?"); Chris@125: } else { Chris@125: text = tr("Some of the items you are pasting do not have values.\nWhat values do you want to use for these items?"); Chris@125: } Chris@125: Chris@340: Labeller::TypeNameMap names = labeller.getTypeNames(); Chris@340: Chris@125: QStringList options; Chris@340: std::vector genopts; Chris@125: Chris@340: for (Labeller::TypeNameMap::const_iterator i = names.begin(); Chris@340: i != names.end(); ++i) { Chris@340: if (i->first == Labeller::ValueNone) options << tr("Zero for all items"); Chris@340: else options << i->second; Chris@340: genopts.push_back(i->first); Chris@125: } Chris@125: Chris@125: static int prevSelection = 0; Chris@125: Chris@125: bool ok = false; Chris@125: QString selected = ListInputDialog::getItem Chris@1408: (nullptr, tr("Choose value calculation"), Chris@125: text, options, prevSelection, &ok); Chris@125: Chris@852: if (!ok) { Chris@852: delete command; Chris@852: return false; Chris@852: } Chris@125: int selection = 0; Chris@340: generation = Labeller::ValueNone; Chris@125: Chris@125: for (QStringList::const_iterator i = options.begin(); Chris@125: i != options.end(); ++i) { Chris@125: if (selected == *i) { Chris@340: generation = genopts[selection]; Chris@125: break; Chris@125: } Chris@125: ++selection; Chris@125: } Chris@340: Chris@340: labeller.setType(generation); Chris@340: Chris@340: if (generation == Labeller::ValueFromCyclicalCounter || Chris@340: generation == Labeller::ValueFromTwoLevelCounter) { Chris@616: int cycleSize = QInputDialog::getInt Chris@1408: (nullptr, tr("Select cycle size"), Chris@340: tr("Cycle size:"), 4, 2, 16, 1); Chris@340: labeller.setCounterCycleSize(cycleSize); Chris@340: } Chris@125: Chris@125: prevSelection = selection; Chris@125: } Chris@125: } Chris@125: Chris@1429: Event prevPoint; Chris@125: Chris@1423: for (EventVector::const_iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: Chris@908: sv_frame_t frame = 0; Chris@360: Chris@360: if (!realign) { Chris@360: Chris@360: frame = i->getFrame(); Chris@360: Chris@360: } else { Chris@360: Chris@1423: if (i->hasReferenceFrame()) { Chris@360: frame = i->getReferenceFrame(); Chris@360: frame = alignFromReference(v, frame); Chris@360: } else { Chris@360: frame = i->getFrame(); Chris@360: } Chris@76: } Chris@360: Chris@1533: Event newPoint = i->withFrame(frame); Chris@1533: Chris@1429: if (!i->hasLabel() && i->hasValue()) { Chris@1429: newPoint = newPoint.withLabel(QString("%1").arg(i->getValue())); Chris@125: } Chris@125: Chris@372: bool usePrev = false; Chris@1429: Event formerPrevPoint = prevPoint; Chris@372: Chris@1429: if (!i->hasValue()) { Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1429: cerr << "Setting value on point at " << newPoint.getFrame() << " from labeller"; Chris@526: if (i == points.begin()) { Chris@682: cerr << ", no prev point" << endl; Chris@526: } else { Chris@1429: cerr << ", prev point is at " << prevPoint.getFrame() << endl; Chris@526: } Chris@526: #endif Chris@1429: Chris@1429: Labeller::Revaluing valuing = Chris@1429: labeller.revalue Chris@1408: (newPoint, (i == points.begin()) ? nullptr : &prevPoint); Chris@1429: Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@1429: cerr << "New point value = " << newPoint.getValue() << endl; Chris@526: #endif Chris@1429: if (valuing.first == Labeller::AppliesToPreviousEvent) { Chris@372: usePrev = true; Chris@1429: prevPoint = valuing.second; Chris@1429: } else { Chris@1429: newPoint = valuing.second; Chris@372: } Chris@372: } Chris@372: Chris@372: if (usePrev) { Chris@1429: command->remove(formerPrevPoint); Chris@1429: command->add(prevPoint); Chris@340: } Chris@125: Chris@340: prevPoint = newPoint; Chris@1429: command->add(newPoint); Chris@76: } Chris@76: Chris@376: finish(command); Chris@125: return true; Chris@360: } Chris@76: Chris@316: void Chris@316: TimeValueLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@6: { Chris@1362: QString s; Chris@1362: Chris@1362: s += QString("plotStyle=\"%1\" " Chris@1362: "verticalScale=\"%2\" " Chris@1362: "scaleMinimum=\"%3\" " Chris@1362: "scaleMaximum=\"%4\" " Chris@1362: "drawDivisions=\"%5\" " Chris@1362: "derivative=\"%6\" ") Chris@1362: .arg(m_plotStyle) Chris@1362: .arg(m_verticalScale) Chris@1362: .arg(m_scaleMinimum) Chris@1362: .arg(m_scaleMaximum) Chris@1362: .arg(m_drawSegmentDivisions ? "true" : "false") Chris@1362: .arg(m_derivative ? "true" : "false"); Chris@1362: Chris@1362: // New-style colour map attribute, by string id rather than by Chris@1362: // number Chris@1362: Chris@1362: s += QString("fillColourMap=\"%1\" ") Chris@1362: .arg(ColourMapper::getColourMapId(m_colourMap)); Chris@1362: Chris@1362: // Old-style colour map attribute Chris@1362: Chris@1362: s += QString("colourMap=\"%1\" ") Chris@1362: .arg(ColourMapper::getBackwardCompatibilityColourMap(m_colourMap)); Chris@1362: Chris@1362: SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s); Chris@0: } Chris@0: Chris@11: void Chris@11: TimeValueLayer::setProperties(const QXmlAttributes &attributes) Chris@11: { Chris@287: SingleColourLayer::setProperties(attributes); Chris@11: Chris@445: bool ok, alsoOk; Chris@287: Chris@1362: QString colourMapId = attributes.value("fillColourMap"); Chris@1362: int colourMap = ColourMapper::getColourMapById(colourMapId); Chris@1362: if (colourMap >= 0) { Chris@1362: setFillColourMap(colourMap); Chris@1362: } else { Chris@1362: colourMap = attributes.value("colourMap").toInt(&ok); Chris@1362: if (ok && colourMap < ColourMapper::getColourMapCount()) { Chris@1362: setFillColourMap(colourMap); Chris@1362: } Chris@1362: } Chris@287: Chris@11: PlotStyle style = (PlotStyle) Chris@1266: attributes.value("plotStyle").toInt(&ok); Chris@11: if (ok) setPlotStyle(style); Chris@286: Chris@286: VerticalScale scale = (VerticalScale) Chris@1266: attributes.value("verticalScale").toInt(&ok); Chris@286: if (ok) setVerticalScale(scale); Chris@445: Chris@513: bool draw = (attributes.value("drawDivisions").trimmed() == "true"); Chris@513: setDrawSegmentDivisions(draw); Chris@513: Chris@553: bool derivative = (attributes.value("derivative").trimmed() == "true"); Chris@553: setShowDerivative(derivative); Chris@553: Chris@445: float min = attributes.value("scaleMinimum").toFloat(&ok); Chris@445: float max = attributes.value("scaleMaximum").toFloat(&alsoOk); Chris@526: #ifdef DEBUG_TIME_VALUE_LAYER Chris@682: cerr << "from properties: min = " << min << ", max = " << max << endl; Chris@526: #endif Chris@526: if (ok && alsoOk && min != max) setDisplayExtents(min, max); Chris@11: } Chris@11: