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@376: #include "ColourDatabase.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@70: Chris@376: #include "ColourMapper.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@44: TimeValueLayer::TimeValueLayer() : Chris@287: SingleColourLayer(), Chris@0: m_model(0), 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@22: m_editingCommand(0), Chris@197: m_colourMap(0), Chris@66: m_plotStyle(PlotConnectedPoints), Chris@437: m_verticalScale(AutoAlignScale), Chris@513: m_drawSegmentDivisions(true), Chris@437: m_scaleMinimum(0), Chris@437: m_scaleMaximum(0) Chris@0: { Chris@44: Chris@0: } Chris@0: Chris@0: void Chris@0: TimeValueLayer::setModel(SparseTimeValueModel *model) Chris@0: { Chris@0: if (m_model == model) return; Chris@0: m_model = model; Chris@0: Chris@320: connectSignals(m_model); Chris@0: Chris@438: m_scaleMinimum = 0; Chris@438: m_scaleMaximum = 0; Chris@438: Chris@494: if (m_model && m_model->getRDFTypeURI().endsWith("Segment")) { Chris@494: setPlotStyle(PlotSegmentation); Chris@494: } Chris@494: if (m_model && m_model->getRDFTypeURI().endsWith("Change")) { Chris@494: setPlotStyle(PlotSegmentation); Chris@494: } Chris@494: Chris@101: // std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl; Chris@0: 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@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@287: return SingleColourLayer::getPropertyLabel(name); Chris@87: } Chris@87: 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@287: if (name == "Colour" && m_plotStyle == PlotSegmentation) return ValueProperty; Chris@513: if (name == "Draw Segment Division Lines") 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@513: if (name == "Plot Type" || name == "Draw Segment Division Lines") { Chris@513: return tr("Plot Type"); Chris@513: } Chris@287: return SingleColourLayer::getPropertyGroupName(name); Chris@198: } Chris@198: Chris@0: int Chris@0: TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: 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@0: Chris@10: if (min) *min = 0; Chris@26: if (max) *max = 5; Chris@216: if (deflt) *deflt = int(PlotConnectedPoints); Chris@0: Chris@216: val = int(m_plotStyle); Chris@0: Chris@87: } else if (name == "Vertical Scale") { Chris@66: Chris@66: if (min) *min = 0; Chris@66: if (max) *max = 3; Chris@216: if (deflt) *deflt = int(AutoAlignScale); Chris@66: Chris@216: val = int(m_verticalScale); Chris@66: Chris@100: } else if (name == "Scale Units") { Chris@100: Chris@216: if (deflt) *deflt = 0; Chris@100: if (m_model) { Chris@216: val = UnitDatabase::getInstance()->getUnitId Chris@100: (m_model->getScaleUnits()); Chris@100: } Chris@100: Chris@513: } else if (name == "Draw Segment Division Lines") { Chris@513: Chris@513: if (min) *min = 0; Chris@513: if (max) *max = 0; Chris@513: if (deflt) *deflt = 1; Chris@513: val = (m_drawSegmentDivisions ? 1.0 : 0.0); Chris@513: Chris@0: } else { Chris@0: Chris@287: 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@0: int value) const Chris@0: { Chris@287: if (name == "Colour" && m_plotStyle == PlotSegmentation) { Chris@287: return ColourMapper::getColourMapName(value); Chris@87: } else if (name == "Plot Type") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("Points"); Chris@0: case 1: return tr("Stems"); Chris@6: case 2: return tr("Connected Points"); Chris@6: case 3: return tr("Lines"); Chris@6: case 4: return tr("Curve"); Chris@26: case 5: return tr("Segmentation"); Chris@0: } Chris@87: } else if (name == "Vertical Scale") { Chris@66: switch (value) { Chris@66: default: Chris@101: case 0: return tr("Auto-Align"); Chris@198: case 1: return tr("Linear"); Chris@198: case 2: return tr("Log"); Chris@198: case 3: return tr("+/-1"); Chris@66: } 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@0: setPlotStyle(PlotStyle(value)); Chris@87: } else if (name == "Vertical Scale") { Chris@66: setVerticalScale(VerticalScale(value)); Chris@100: } else if (name == "Scale Units") { Chris@100: if (m_model) { Chris@100: m_model->setScaleUnits Chris@100: (UnitDatabase::getInstance()->getUnitById(value)); Chris@100: emit modelChanged(); Chris@100: } Chris@513: } else if (name == "Draw Segment Division Lines") { Chris@513: setDrawSegmentDivisions(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@0: bool Chris@44: TimeValueLayer::isLayerScrollable(const View *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@6: m_plotStyle == PlotCurve) return true; Chris@6: Chris@0: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@0: } Chris@0: Chris@79: bool Chris@101: TimeValueLayer::getValueExtents(float &min, float &max, Chris@101: bool &logarithmic, QString &unit) const Chris@79: { Chris@101: if (!m_model) return false; Chris@79: min = m_model->getValueMinimum(); Chris@79: max = m_model->getValueMaximum(); Chris@101: logarithmic = (m_verticalScale == LogScale); Chris@79: unit = m_model->getScaleUnits(); Chris@79: return true; Chris@79: } Chris@79: Chris@101: bool Chris@101: TimeValueLayer::getDisplayExtents(float &min, float &max) const Chris@101: { Chris@296: if (!m_model || shouldAutoAlign()) return false; Chris@101: Chris@437: if (m_scaleMinimum == m_scaleMaximum) { Chris@455: min = m_model->getValueMinimum(); Chris@455: max = m_model->getValueMaximum(); Chris@455: return true; Chris@437: } Chris@437: Chris@437: min = m_scaleMinimum; Chris@437: max = m_scaleMaximum; Chris@437: Chris@437: // std::cerr << "TimeValueLayer::getDisplayExtents: min = " << min << ", max = " << max << std::endl; Chris@437: Chris@101: return true; Chris@101: } Chris@101: Chris@437: bool Chris@437: TimeValueLayer::setDisplayExtents(float min, float max) Chris@437: { Chris@437: if (!m_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@438: // std::cerr << "TimeValueLayer::setDisplayExtents: min = " << min << ", max = " << max << std::endl; 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@437: if (!m_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@437: if (!m_model) return 0; Chris@437: Chris@437: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@437: if (!mapper) return 0; Chris@437: Chris@437: float dmin, dmax; Chris@437: getDisplayExtents(dmin, dmax); Chris@437: Chris@437: int nr = mapper->getPositionForValue(dmax - dmin); Chris@437: /* Chris@437: int n0 = mapper->getPositionForValue(dmax); Chris@437: int n1 = mapper->getPositionForValue(dmin); Chris@437: int nr = n1 - n0; Chris@437: if (nr < 0) nr = -nr; Chris@437: Chris@437: std::cerr << "TimeValueLayer::getCurrentVerticalZoomStep: dmin = " << dmin << ", dmax = " << dmax << ", n0 = " << n0 << ", n1 = " << n1 << ", nr = " << nr << std::endl; Chris@437: */ 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@437: if (!m_model) return; Chris@437: Chris@437: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@437: if (!mapper) return; Chris@437: Chris@437: float min, max; Chris@437: bool logarithmic; Chris@437: QString unit; Chris@437: getValueExtents(min, max, logarithmic, unit); Chris@437: Chris@437: float dmin, dmax; Chris@437: getDisplayExtents(dmin, dmax); Chris@437: Chris@437: float newdist = mapper->getValueForPosition(100 - step); Chris@437: Chris@437: float newmin, newmax; Chris@437: Chris@437: if (logarithmic) { Chris@437: Chris@437: // see SpectrogramLayer::setVerticalZoomStep Chris@437: Chris@437: newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2; Chris@437: newmin = newmax - newdist; Chris@437: Chris@437: // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl; Chris@437: Chris@437: } else { Chris@437: float 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@437: std::cerr << "TimeValueLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl; Chris@437: Chris@437: setDisplayExtents(newmin, newmax); Chris@437: } Chris@437: Chris@437: RangeMapper * Chris@437: TimeValueLayer::getNewVerticalZoomRangeMapper() const Chris@437: { Chris@437: if (!m_model) return 0; Chris@437: Chris@437: RangeMapper *mapper; Chris@437: Chris@437: float min, max; Chris@437: bool logarithmic; Chris@437: QString unit; Chris@437: getValueExtents(min, max, logarithmic, unit); Chris@437: Chris@437: if (min == max) return 0; 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@0: SparseTimeValueModel::PointList Chris@44: TimeValueLayer::getLocalPoints(View *v, int x) const Chris@0: { Chris@0: if (!m_model) return SparseTimeValueModel::PointList(); Chris@0: Chris@44: long frame = v->getFrameForX(x); Chris@0: Chris@0: SparseTimeValueModel::PointList onPoints = Chris@0: m_model->getPoints(frame); Chris@0: Chris@0: if (!onPoints.empty()) { Chris@0: return onPoints; Chris@0: } Chris@0: Chris@0: SparseTimeValueModel::PointList prevPoints = Chris@0: m_model->getPreviousPoints(frame); Chris@0: SparseTimeValueModel::PointList nextPoints = Chris@0: m_model->getNextPoints(frame); Chris@0: Chris@0: SparseTimeValueModel::PointList usePoints = prevPoints; Chris@0: Chris@0: if (prevPoints.empty()) { Chris@0: usePoints = nextPoints; Chris@248: } else if (long(prevPoints.begin()->frame) < v->getStartFrame() && Chris@44: !(nextPoints.begin()->frame > v->getEndFrame())) { Chris@0: usePoints = nextPoints; Chris@0: } else if (nextPoints.begin()->frame - frame < Chris@0: frame - prevPoints.begin()->frame) { Chris@0: usePoints = nextPoints; Chris@0: } Chris@0: Chris@28: if (!usePoints.empty()) { Chris@28: int fuzz = 2; Chris@44: int px = v->getXForFrame(usePoints.begin()->frame); Chris@28: if ((px > x && px - x > fuzz) || Chris@28: (px < x && x - px > fuzz + 1)) { Chris@28: usePoints.clear(); Chris@28: } Chris@28: } Chris@28: Chris@0: return usePoints; Chris@0: } Chris@0: Chris@25: QString Chris@44: TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const Chris@0: { Chris@25: int x = pos.x(); Chris@0: Chris@25: if (!m_model || !m_model->getSampleRate()) return ""; Chris@0: Chris@44: SparseTimeValueModel::PointList points = getLocalPoints(v, x); Chris@0: Chris@0: if (points.empty()) { Chris@0: if (!m_model->isReady()) { Chris@25: return tr("In progress"); Chris@25: } else { Chris@25: return tr("No local points"); Chris@0: } Chris@0: } Chris@0: Chris@0: long useFrame = points.begin()->frame; Chris@0: Chris@0: RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); Chris@25: Chris@25: QString text; Chris@101: QString unit = m_model->getScaleUnits(); Chris@101: if (unit != "") unit = " " + unit; Chris@0: Chris@25: if (points.begin()->label == "") { Chris@101: text = QString(tr("Time:\t%1\nValue:\t%2%3\nNo label")) Chris@25: .arg(rt.toText(true).c_str()) Chris@25: .arg(points.begin()->value) Chris@101: .arg(unit); Chris@101: } else { Chris@101: text = QString(tr("Time:\t%1\nValue:\t%2%3\nLabel:\t%4")) Chris@101: .arg(rt.toText(true).c_str()) Chris@101: .arg(points.begin()->value) Chris@101: .arg(unit) Chris@25: .arg(points.begin()->label); Chris@25: } Chris@0: Chris@44: pos = QPoint(v->getXForFrame(useFrame), Chris@44: getYForValue(v, points.begin()->value)); Chris@25: return text; Chris@0: } Chris@0: Chris@28: bool Chris@44: TimeValueLayer::snapToFeatureFrame(View *v, int &frame, Chris@28: size_t &resolution, Chris@28: SnapType snap) const Chris@13: { Chris@13: if (!m_model) { Chris@44: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@13: } Chris@13: Chris@13: resolution = m_model->getResolution(); Chris@28: SparseTimeValueModel::PointList points; Chris@13: Chris@28: if (snap == SnapNeighbouring) { Chris@28: Chris@44: points = getLocalPoints(v, v->getXForFrame(frame)); Chris@28: if (points.empty()) return false; Chris@28: frame = points.begin()->frame; Chris@28: return true; Chris@28: } Chris@28: Chris@28: points = m_model->getPoints(frame, frame); Chris@28: int snapped = frame; Chris@28: bool found = false; Chris@13: Chris@13: for (SparseTimeValueModel::PointList::const_iterator i = points.begin(); Chris@13: i != points.end(); ++i) { Chris@13: Chris@28: if (snap == SnapRight) { Chris@28: Chris@13: if (i->frame > frame) { Chris@28: snapped = i->frame; Chris@28: found = true; Chris@13: break; Chris@13: } Chris@28: Chris@28: } else if (snap == SnapLeft) { Chris@28: Chris@13: if (i->frame <= frame) { Chris@28: snapped = i->frame; Chris@28: found = true; // don't break, as the next may be better Chris@28: } else { Chris@28: break; Chris@28: } Chris@28: Chris@28: } else { // nearest Chris@28: Chris@28: SparseTimeValueModel::PointList::const_iterator j = i; Chris@28: ++j; Chris@28: Chris@28: if (j == points.end()) { Chris@28: Chris@28: snapped = i->frame; Chris@28: found = true; Chris@28: break; Chris@28: Chris@28: } else if (j->frame >= frame) { Chris@28: Chris@28: if (j->frame - frame < frame - i->frame) { Chris@28: snapped = j->frame; Chris@28: } else { Chris@28: snapped = i->frame; Chris@28: } Chris@28: found = true; Chris@28: break; Chris@13: } Chris@13: } Chris@13: } Chris@13: Chris@28: frame = snapped; Chris@28: return found; Chris@13: } Chris@13: Chris@101: void Chris@101: TimeValueLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const Chris@101: { Chris@101: min = 0.0; Chris@101: max = 0.0; Chris@101: log = false; Chris@101: Chris@296: if (shouldAutoAlign()) { Chris@101: Chris@101: if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) { Chris@101: min = m_model->getValueMinimum(); Chris@101: max = m_model->getValueMaximum(); Chris@101: } else if (log) { Chris@197: LogRange::mapRange(min, max); 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@101: Chris@101: if (m_verticalScale == LogScale) { Chris@197: LogRange::mapRange(min, max); Chris@101: log = true; Chris@101: } Chris@101: } Chris@101: Chris@101: if (max == min) max = min + 1.0; Chris@101: } Chris@101: Chris@21: int Chris@66: TimeValueLayer::getYForValue(View *v, float val) const Chris@21: { Chris@79: float min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@79: int h = v->height(); Chris@79: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@101: // std::cerr << "getYForValue(" << val << "): min " << min << ", max " Chris@101: // << max << ", log " << logarithmic << std::endl; Chris@101: Chris@101: if (logarithmic) { Chris@197: val = LogRange::map(val); Chris@79: } Chris@79: Chris@66: return int(h - ((val - min) * h) / (max - min)); Chris@21: } Chris@21: Chris@21: float Chris@44: TimeValueLayer::getValueForY(View *v, int y) const Chris@21: { Chris@101: float min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@44: int h = v->height(); Chris@21: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@101: float val = min + (float(h - y) * float(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@296: if (!m_model) return false; Chris@296: QString unit = m_model->getScaleUnits(); Chris@296: return (m_verticalScale == AutoAlignScale && unit != ""); Chris@296: } Chris@296: Chris@68: QColor Chris@101: TimeValueLayer::getColourForValue(View *v, float val) const Chris@68: { Chris@101: float 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@197: // std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max " Chris@197: // << max << ", log " << log << ", value " << val << std::endl; Chris@68: Chris@197: QColor solid = ColourMapper(m_colourMap, 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@44: TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@0: int sampleRate = m_model->getSampleRate(); Chris@0: if (!sampleRate) return; Chris@0: 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@44: long frame0 = v->getFrameForX(x0); Chris@44: long frame1 = v->getFrameForX(x1); Chris@0: Chris@0: SparseTimeValueModel::PointList points(m_model->getPoints Chris@0: (frame0, frame1)); 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@0: // std::cerr << "TimeValueLayer::paint: resolution is " Chris@0: // << m_model->getResolution() << " frames" << std::endl; Chris@0: Chris@0: float min = m_model->getValueMinimum(); Chris@0: float max = m_model->getValueMaximum(); Chris@0: if (max == min) max = min + 1.0; Chris@0: Chris@44: int origin = int(nearbyint(v->height() - Chris@44: (-min * v->height()) / (max - min))); Chris@0: Chris@0: QPoint localPos; Chris@0: long illuminateFrame = -1; Chris@0: Chris@44: if (v->shouldIlluminateLocalFeatures(this, localPos)) { Chris@0: SparseTimeValueModel::PointList localPoints = Chris@44: getLocalPoints(v, localPos.x()); Chris@296: // std::cerr << "TimeValueLayer: " << localPoints.size() << " local points" << std::endl; Chris@0: if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame; Chris@0: } Chris@6: Chris@20: int w = Chris@44: v->getXForFrame(frame0 + m_model->getResolution()) - Chris@44: v->getXForFrame(frame0); Chris@7: 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@79: textY = v->getTextLabelHeight(this, paint); Chris@79: } Chris@6: Chris@0: for (SparseTimeValueModel::PointList::const_iterator i = points.begin(); Chris@0: i != points.end(); ++i) { Chris@0: Chris@0: const SparseTimeValueModel::Point &p(*i); Chris@0: Chris@44: int x = v->getXForFrame(p.frame); Chris@44: int y = getYForValue(v, p.value); Chris@0: Chris@79: if (m_plotStyle != PlotSegmentation) { Chris@79: textY = y - paint.fontMetrics().height() Chris@79: + paint.fontMetrics().ascent(); Chris@372: if (textY < paint.fontMetrics().ascent() + 1) { Chris@372: textY = paint.fontMetrics().ascent() + 1; Chris@372: } Chris@79: } Chris@79: Chris@34: bool haveNext = false; Chris@76: int nx = v->getXForFrame(v->getModelsEndFrame()); Chris@34: int ny = y; Chris@34: Chris@34: SparseTimeValueModel::PointList::const_iterator j = i; Chris@34: ++j; Chris@34: Chris@34: if (j != points.end()) { Chris@34: const SparseTimeValueModel::Point &q(*j); Chris@44: nx = v->getXForFrame(q.frame); Chris@44: ny = getYForValue(v, q.value); Chris@34: haveNext = true; Chris@76: } Chris@76: Chris@76: // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext Chris@76: // << ", nx = " << nx << std::endl; Chris@34: Chris@0: if (w < 1) w = 1; Chris@287: paint.setPen(getBaseQColor()); Chris@0: Chris@26: if (m_plotStyle == PlotSegmentation) { Chris@287: paint.setPen(getForegroundQColor(v)); Chris@101: paint.setBrush(getColourForValue(v, p.value)); Chris@26: } else if (m_plotStyle == PlotLines || Chris@26: m_plotStyle == PlotCurve) { Chris@6: paint.setBrush(Qt::NoBrush); Chris@3: } else { Chris@6: paint.setBrush(brushColour); Chris@3: } Chris@0: Chris@0: if (m_plotStyle == PlotStems) { Chris@430: /* Chris@0: paint.setPen(brushColour); Chris@0: if (y < origin - 1) { Chris@0: paint.drawRect(x + w/2, y + 1, 1, origin - y); Chris@0: } else if (y > origin + 1) { Chris@0: paint.drawRect(x + w/2, origin, 1, y - origin - 1); Chris@0: } Chris@430: */ Chris@287: paint.setPen(getBaseQColor()); Chris@430: if (y < origin - 1) { Chris@430: paint.drawLine(x + w/2, y + 1, x + w/2, origin); Chris@430: } else if (y > origin + 1) { Chris@430: paint.drawLine(x + w/2, origin, x + w/2, y - 1); Chris@430: } Chris@0: } Chris@0: Chris@0: if (illuminateFrame == p.frame) { Chris@6: Chris@0: //!!! aside from the problem of choosing a colour, it'd be Chris@0: //better to save the highlighted rects and draw them at Chris@0: //the end perhaps Chris@6: Chris@6: //!!! not equipped to illuminate the right section in line Chris@6: //or curve mode Chris@6: Chris@6: if (m_plotStyle != PlotCurve && Chris@6: m_plotStyle != PlotLines) { Chris@287: paint.setPen(getForegroundQColor(v)); Chris@6: } Chris@0: } Chris@0: Chris@6: if (m_plotStyle != PlotLines && Chris@26: m_plotStyle != PlotCurve && Chris@26: m_plotStyle != PlotSegmentation) { Chris@326: if (m_plotStyle != PlotStems || Chris@326: w > 1) { Chris@326: paint.drawRect(x, y - 1, w, 2); Chris@326: } Chris@3: } Chris@0: Chris@6: if (m_plotStyle == PlotConnectedPoints || Chris@6: m_plotStyle == PlotLines || Chris@6: m_plotStyle == PlotCurve) { Chris@0: Chris@34: if (haveNext) { Chris@3: Chris@6: if (m_plotStyle == PlotConnectedPoints) { Chris@34: Chris@79: paint.save(); Chris@3: paint.setPen(brushColour); Chris@3: paint.drawLine(x + w, y, nx, ny); Chris@79: paint.restore(); Chris@6: Chris@6: } else if (m_plotStyle == PlotLines) { Chris@430: Chris@430: if (pointCount == 0) { Chris@430: path.moveTo(x + w/2, y); Chris@430: } Chris@430: ++pointCount; Chris@6: Chris@430: // paint.drawLine(x + w/2, y, nx + w/2, ny); Chris@430: path.lineTo(nx + w/2, ny); Chris@6: Chris@3: } else { Chris@6: Chris@55: float x0 = x + float(w)/2; Chris@55: float x1 = nx + float(w)/2; Chris@55: Chris@55: float y0 = y; Chris@55: float y1 = ny; Chris@55: Chris@55: if (pointCount == 0) { Chris@55: path.moveTo((x0 + x1) / 2, (y0 + y1) / 2); Chris@6: } Chris@55: ++pointCount; Chris@6: Chris@6: if (nx - x > 5) { Chris@55: path.cubicTo(x0, y0, Chris@55: x0, y0, Chris@55: (x0 + x1) / 2, (y0 + y1) / 2); Chris@55: Chris@55: // // or Chris@55: // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2); Chris@55: Chris@6: } else { Chris@55: path.lineTo((x0 + x1) / 2, (y0 + y1) / 2); Chris@6: } Chris@3: } Chris@0: } Chris@0: } Chris@0: Chris@26: if (m_plotStyle == PlotSegmentation) { Chris@76: Chris@76: // std::cerr << "drawing rect" << std::endl; Chris@26: Chris@27: if (nx <= x) continue; Chris@26: Chris@513: if (illuminateFrame != p.frame) { Chris@513: if (!m_drawSegmentDivisions || Chris@513: nx < x + 5 || Chris@513: x >= v->width() - 1) { Chris@513: paint.setPen(Qt::NoPen); Chris@513: } Chris@27: } Chris@26: Chris@44: paint.drawRect(x, -1, nx - x, v->height() + 1); Chris@26: } Chris@26: Chris@55: if (p.label != "") { Chris@79: if (!haveNext || nx > x + 6 + paint.fontMetrics().width(p.label)) { Chris@79: paint.drawText(x + 5, textY, p.label); Chris@79: } Chris@55: } Chris@0: } Chris@6: Chris@430: if ((m_plotStyle == PlotCurve || m_plotStyle == PlotLines) Chris@430: && !path.isEmpty()) { Chris@55: paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width()); Chris@6: 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@248: TimeValueLayer::getVerticalScaleWidth(View *, QPainter &paint) const Chris@42: { Chris@68: int w = paint.fontMetrics().width("-000.000"); Chris@68: if (m_plotStyle == PlotSegmentation) return w + 20; Chris@68: else return w + 10; Chris@42: } Chris@42: Chris@42: void Chris@248: TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect) const Chris@42: { Chris@42: if (!m_model) return; Chris@42: Chris@68: int h = v->height(); Chris@68: Chris@68: int n = 10; Chris@68: Chris@437: float min, max; Chris@437: bool logarithmic; Chris@437: getScaleExtents(v, min, max, logarithmic); Chris@437: Chris@437: if (m_plotStyle == PlotSegmentation) { Chris@437: QString unit; Chris@437: getValueExtents(min, max, logarithmic, unit); Chris@437: if (logarithmic) { Chris@437: LogRange::mapRange(min, max); Chris@437: } Chris@437: } Chris@437: Chris@68: float val = min; Chris@68: float inc = (max - val) / n; Chris@42: Chris@55: char buffer[40]; Chris@55: Chris@55: int w = getVerticalScaleWidth(v, paint); Chris@55: Chris@68: int tx = 5; Chris@68: Chris@68: int boxx = 5, boxy = 5; Chris@68: if (m_model->getScaleUnits() != "") { Chris@68: boxy += paint.fontMetrics().height(); Chris@68: } Chris@68: int boxw = 10, boxh = h - boxy - 5; Chris@68: Chris@68: if (m_plotStyle == PlotSegmentation) { Chris@68: tx += boxx + boxw; Chris@68: paint.drawRect(boxx, boxy, boxw, boxh); Chris@68: } Chris@68: Chris@68: if (m_plotStyle == PlotSegmentation) { Chris@68: paint.save(); Chris@68: for (int y = 0; y < boxh; ++y) { Chris@68: float val = ((boxh - y) * (max - min)) / boxh + min; Chris@437: if (logarithmic) { Chris@437: paint.setPen(getColourForValue(v, LogRange::unmap(val))); Chris@437: } else { Chris@437: paint.setPen(getColourForValue(v, val)); Chris@437: } Chris@68: paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1); Chris@68: } Chris@68: paint.restore(); Chris@68: } Chris@434: Chris@434: float round = 1.f; Chris@434: int dp = 0; Chris@434: if (inc > 0) { Chris@434: int prec = trunc(log10f(inc)); Chris@434: prec -= 1; Chris@434: if (prec < 0) dp = -prec; Chris@434: round = powf(10.f, prec); Chris@434: // std::cerr << "inc = " << inc << ", round = " << round << std::endl; Chris@434: } Chris@68: Chris@434: int prevy = -1; Chris@434: Chris@68: for (int i = 0; i < n; ++i) { Chris@68: Chris@68: int y, ty; Chris@68: bool drawText = true; Chris@68: Chris@434: float dispval = val; Chris@434: Chris@68: if (m_plotStyle == PlotSegmentation) { Chris@68: y = boxy + int(boxh - ((val - min) * boxh) / (max - min)); Chris@68: ty = y; Chris@68: } else { Chris@437: if (i == n-1 && Chris@437: v->height() < paint.fontMetrics().height() * (n*2)) { Chris@68: if (m_model->getScaleUnits() != "") drawText = false; Chris@68: } Chris@434: dispval = lrintf(val / round) * round; Chris@434: // std::cerr << "val = " << val << ", dispval = " << dispval << std::endl; Chris@437: if (logarithmic) { Chris@437: y = getYForValue(v, LogRange::unmap(dispval)); Chris@437: } else { Chris@437: y = getYForValue(v, dispval); Chris@437: } Chris@68: ty = y - paint.fontMetrics().height() + Chris@434: paint.fontMetrics().ascent() + 2; Chris@434: Chris@434: if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) { Chris@434: val += inc; Chris@434: continue; Chris@434: } Chris@68: } Chris@68: Chris@437: if (logarithmic) { Chris@437: sprintf(buffer, "%.*g", dp < 2 ? 2 : dp, LogRange::unmap(dispval)); Chris@437: } else { Chris@437: sprintf(buffer, "%.*f", dp, dispval); Chris@437: } Chris@55: QString label = QString(buffer); Chris@68: Chris@68: if (m_plotStyle != PlotSegmentation) { Chris@68: paint.drawLine(w - 5, y, w, y); Chris@68: } else { Chris@68: paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y); Chris@68: } Chris@68: Chris@434: if (drawText) { Chris@435: if (m_plotStyle != PlotSegmentation) { Chris@435: paint.drawText(tx + w - paint.fontMetrics().width(label) - 8, Chris@435: ty, label); Chris@435: } else { Chris@435: paint.drawText(tx, ty, label); Chris@435: } Chris@434: } Chris@434: Chris@434: prevy = y; Chris@44: val += inc; Chris@42: } Chris@68: Chris@67: if (m_model->getScaleUnits() != "") { Chris@67: paint.drawText(5, 5 + paint.fontMetrics().ascent(), Chris@67: m_model->getScaleUnits()); Chris@67: } Chris@42: } Chris@42: Chris@21: void Chris@44: TimeValueLayer::drawStart(View *v, QMouseEvent *e) Chris@21: { Chris@101: // std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl; Chris@21: Chris@21: if (!m_model) return; Chris@21: Chris@44: long frame = v->getFrameForX(e->x()); Chris@76: long resolution = m_model->getResolution(); Chris@21: if (frame < 0) frame = 0; Chris@76: frame = (frame / resolution) * resolution; Chris@21: Chris@44: float value = getValueForY(v, e->y()); Chris@21: Chris@76: bool havePoint = false; Chris@76: Chris@76: SparseTimeValueModel::PointList points = getLocalPoints(v, e->x()); Chris@76: if (!points.empty()) { Chris@76: for (SparseTimeValueModel::PointList::iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: if (((i->frame / resolution) * resolution) != frame) { Chris@101: // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl; 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@76: m_editingPoint = SparseTimeValueModel::Point Chris@76: (frame, 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@22: m_editingCommand = new SparseTimeValueModel::EditCommand(m_model, Chris@22: tr("Draw Point")); Chris@76: if (!havePoint) { Chris@76: m_editingCommand->addPoint(m_editingPoint); Chris@76: } Chris@22: Chris@21: m_editing = true; Chris@21: } Chris@21: Chris@21: void Chris@44: TimeValueLayer::drawDrag(View *v, QMouseEvent *e) Chris@21: { Chris@101: // std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl; Chris@21: Chris@21: if (!m_model || !m_editing) return; Chris@21: Chris@44: long frame = v->getFrameForX(e->x()); Chris@76: long resolution = m_model->getResolution(); Chris@21: if (frame < 0) frame = 0; Chris@76: frame = (frame / resolution) * resolution; Chris@21: Chris@44: float value = getValueForY(v, e->y()); Chris@21: Chris@76: SparseTimeValueModel::PointList points = getLocalPoints(v, e->x()); Chris@76: Chris@101: // std::cerr << points.size() << " points" << std::endl; Chris@76: Chris@76: bool havePoint = false; Chris@76: Chris@76: if (!points.empty()) { Chris@76: for (SparseTimeValueModel::PointList::iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: if (i->frame == m_editingPoint.frame && Chris@76: i->value == m_editingPoint.value) { Chris@101: // std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl; Chris@76: continue; Chris@76: } Chris@76: if (((i->frame / resolution) * resolution) != frame) { Chris@101: // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl; Chris@76: continue; Chris@76: } Chris@101: // std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl; Chris@76: m_editingPoint = *i; Chris@76: m_originalPoint = m_editingPoint; Chris@76: m_editingCommand->deletePoint(m_editingPoint); Chris@76: havePoint = true; Chris@76: } Chris@76: } Chris@76: Chris@76: if (!havePoint) { Chris@76: if (frame == m_editingPoint.frame) { Chris@76: m_editingCommand->deletePoint(m_editingPoint); Chris@76: } Chris@76: } Chris@76: Chris@76: // m_editingCommand->deletePoint(m_editingPoint); Chris@21: m_editingPoint.frame = frame; Chris@21: m_editingPoint.value = value; Chris@22: m_editingCommand->addPoint(m_editingPoint); Chris@21: } Chris@21: Chris@21: void Chris@248: TimeValueLayer::drawEnd(View *, QMouseEvent *) Chris@21: { Chris@101: // std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl; Chris@21: if (!m_model || !m_editing) return; Chris@376: finish(m_editingCommand); Chris@22: m_editingCommand = 0; Chris@21: m_editing = false; Chris@21: } Chris@21: Chris@21: void Chris@335: TimeValueLayer::eraseStart(View *v, QMouseEvent *e) Chris@335: { Chris@335: if (!m_model) return; Chris@335: Chris@335: SparseTimeValueModel::PointList 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@376: finish(m_editingCommand); Chris@335: m_editingCommand = 0; Chris@335: } Chris@335: Chris@335: m_editing = true; Chris@335: } Chris@335: Chris@335: void Chris@335: TimeValueLayer::eraseDrag(View *v, QMouseEvent *e) Chris@335: { Chris@335: } Chris@335: Chris@335: void Chris@335: TimeValueLayer::eraseEnd(View *v, QMouseEvent *e) Chris@335: { Chris@335: if (!m_model || !m_editing) return; Chris@335: Chris@335: m_editing = false; Chris@335: Chris@335: SparseTimeValueModel::PointList points = getLocalPoints(v, e->x()); Chris@335: if (points.empty()) return; Chris@335: if (points.begin()->frame != m_editingPoint.frame || Chris@335: points.begin()->value != m_editingPoint.value) return; Chris@335: Chris@335: m_editingCommand = new SparseTimeValueModel::EditCommand Chris@335: (m_model, tr("Erase Point")); Chris@335: Chris@335: m_editingCommand->deletePoint(m_editingPoint); Chris@335: Chris@376: finish(m_editingCommand); Chris@335: m_editingCommand = 0; Chris@335: m_editing = false; Chris@335: } Chris@335: Chris@335: void Chris@44: TimeValueLayer::editStart(View *v, QMouseEvent *e) Chris@21: { Chris@101: // std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl; Chris@21: Chris@21: if (!m_model) return; Chris@21: Chris@44: SparseTimeValueModel::PointList 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@376: finish(m_editingCommand); Chris@22: m_editingCommand = 0; Chris@22: } Chris@22: Chris@21: m_editing = true; Chris@21: } Chris@21: Chris@21: void Chris@44: TimeValueLayer::editDrag(View *v, QMouseEvent *e) Chris@21: { Chris@101: // std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl; Chris@21: Chris@21: if (!m_model || !m_editing) return; Chris@21: Chris@44: long frame = v->getFrameForX(e->x()); Chris@21: if (frame < 0) frame = 0; Chris@21: frame = frame / m_model->getResolution() * m_model->getResolution(); Chris@21: Chris@44: float value = getValueForY(v, e->y()); Chris@21: Chris@22: if (!m_editingCommand) { Chris@22: m_editingCommand = new SparseTimeValueModel::EditCommand(m_model, Chris@22: tr("Drag Point")); Chris@22: } Chris@22: Chris@22: m_editingCommand->deletePoint(m_editingPoint); Chris@21: m_editingPoint.frame = frame; Chris@21: m_editingPoint.value = value; Chris@22: m_editingCommand->addPoint(m_editingPoint); Chris@21: } Chris@21: Chris@21: void Chris@248: TimeValueLayer::editEnd(View *, QMouseEvent *) Chris@21: { Chris@101: // std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl; Chris@21: if (!m_model || !m_editing) return; Chris@23: Chris@23: if (m_editingCommand) { Chris@23: Chris@23: QString newName = m_editingCommand->getName(); Chris@23: Chris@23: if (m_editingPoint.frame != m_originalPoint.frame) { Chris@23: if (m_editingPoint.value != m_originalPoint.value) { Chris@23: newName = tr("Edit Point"); Chris@23: } else { Chris@23: newName = tr("Relocate Point"); Chris@23: } Chris@23: } else { Chris@23: newName = tr("Change Point Value"); Chris@23: } Chris@23: Chris@23: m_editingCommand->setName(newName); Chris@376: finish(m_editingCommand); Chris@23: } Chris@23: Chris@22: m_editingCommand = 0; Chris@21: m_editing = false; Chris@21: } Chris@21: Chris@255: bool Chris@70: TimeValueLayer::editOpen(View *v, QMouseEvent *e) Chris@70: { Chris@255: if (!m_model) return false; Chris@70: Chris@70: SparseTimeValueModel::PointList points = getLocalPoints(v, e->x()); Chris@255: if (points.empty()) return false; Chris@70: Chris@70: SparseTimeValueModel::Point point = *points.begin(); Chris@70: Chris@70: ItemEditDialog *dialog = new ItemEditDialog Chris@70: (m_model->getSampleRate(), Chris@70: ItemEditDialog::ShowTime | Chris@70: ItemEditDialog::ShowValue | Chris@73: ItemEditDialog::ShowText, Chris@73: m_model->getScaleUnits()); Chris@70: Chris@70: dialog->setFrameTime(point.frame); Chris@70: dialog->setValue(point.value); Chris@70: dialog->setText(point.label); Chris@70: Chris@70: if (dialog->exec() == QDialog::Accepted) { Chris@70: Chris@70: SparseTimeValueModel::Point newPoint = point; Chris@70: newPoint.frame = dialog->getFrameTime(); Chris@70: newPoint.value = dialog->getValue(); Chris@70: newPoint.label = dialog->getText(); Chris@70: Chris@70: SparseTimeValueModel::EditCommand *command = Chris@70: new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point")); Chris@70: command->deletePoint(point); Chris@70: command->addPoint(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@43: TimeValueLayer::moveSelection(Selection s, size_t newStartFrame) Chris@43: { Chris@99: if (!m_model) return; Chris@99: Chris@43: SparseTimeValueModel::EditCommand *command = Chris@43: new SparseTimeValueModel::EditCommand(m_model, Chris@43: tr("Drag Selection")); Chris@43: Chris@43: SparseTimeValueModel::PointList points = Chris@43: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@43: Chris@43: for (SparseTimeValueModel::PointList::iterator i = points.begin(); Chris@43: i != points.end(); ++i) { Chris@43: Chris@43: if (s.contains(i->frame)) { Chris@43: SparseTimeValueModel::Point newPoint(*i); Chris@43: newPoint.frame = i->frame + newStartFrame - s.getStartFrame(); Chris@43: command->deletePoint(*i); Chris@43: command->addPoint(newPoint); Chris@43: } 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@99: if (!m_model) return; Chris@99: Chris@43: SparseTimeValueModel::EditCommand *command = Chris@43: new SparseTimeValueModel::EditCommand(m_model, Chris@43: tr("Resize Selection")); Chris@43: Chris@43: SparseTimeValueModel::PointList points = Chris@43: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@43: Chris@43: double ratio = Chris@43: double(newSize.getEndFrame() - newSize.getStartFrame()) / Chris@43: double(s.getEndFrame() - s.getStartFrame()); Chris@43: Chris@43: for (SparseTimeValueModel::PointList::iterator i = points.begin(); Chris@43: i != points.end(); ++i) { Chris@43: Chris@43: if (s.contains(i->frame)) { Chris@43: Chris@43: double target = i->frame; Chris@43: target = newSize.getStartFrame() + Chris@43: double(target - s.getStartFrame()) * ratio; Chris@43: Chris@43: SparseTimeValueModel::Point newPoint(*i); Chris@43: newPoint.frame = lrint(target); Chris@43: command->deletePoint(*i); Chris@43: command->addPoint(newPoint); Chris@43: } Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@76: void Chris@76: TimeValueLayer::deleteSelection(Selection s) Chris@76: { Chris@99: if (!m_model) return; Chris@99: Chris@76: SparseTimeValueModel::EditCommand *command = Chris@76: new SparseTimeValueModel::EditCommand(m_model, Chris@76: tr("Delete Selected Points")); Chris@76: Chris@76: SparseTimeValueModel::PointList points = Chris@76: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@76: Chris@76: for (SparseTimeValueModel::PointList::iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: Chris@76: if (s.contains(i->frame)) { Chris@76: command->deletePoint(*i); Chris@76: } Chris@76: } Chris@76: Chris@376: finish(command); Chris@76: } Chris@76: Chris@76: void Chris@359: TimeValueLayer::copy(View *v, Selection s, Clipboard &to) Chris@76: { Chris@99: if (!m_model) return; Chris@99: Chris@76: SparseTimeValueModel::PointList points = Chris@76: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@76: Chris@76: for (SparseTimeValueModel::PointList::iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: if (s.contains(i->frame)) { Chris@83: Clipboard::Point point(i->frame, i->value, i->label); Chris@360: point.setReferenceFrame(alignToReference(v, i->frame)); Chris@76: to.addPoint(point); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@125: bool Chris@359: TimeValueLayer::paste(View *v, const Clipboard &from, int frameOffset, Chris@125: bool interactive) Chris@76: { Chris@125: if (!m_model) return false; Chris@99: Chris@76: const Clipboard::PointList &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@360: QMessageBox::question(v, 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@76: SparseTimeValueModel::EditCommand *command = Chris@76: new SparseTimeValueModel::EditCommand(m_model, 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@125: bool haveExistingItems = !(m_model->isEmpty()); Chris@340: Labeller labeller; Chris@340: labeller.setSampleRate(m_model->getSampleRate()); Chris@125: Chris@125: if (interactive) { Chris@125: Chris@125: ValueAvailability availability = UnknownAvailability; Chris@125: Chris@125: for (Clipboard::PointList::const_iterator i = points.begin(); Chris@125: i != points.end(); ++i) { Chris@125: Chris@125: if (!i->haveFrame()) continue; Chris@125: Chris@125: if (availability == UnknownAvailability) { Chris@125: if (i->haveValue()) availability = AllValues; Chris@125: else availability = NoValues; Chris@125: continue; Chris@125: } Chris@125: Chris@125: if (i->haveValue()) { 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@125: if (i->haveLabel()) { 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@125: (0, tr("Choose value calculation"), Chris@125: text, options, prevSelection, &ok); Chris@125: Chris@125: if (!ok) return false; 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@340: int cycleSize = QInputDialog::getInteger Chris@340: (0, 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@372: SparseTimeValueModel::Point prevPoint(0); Chris@125: Chris@76: for (Clipboard::PointList::const_iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: Chris@76: if (!i->haveFrame()) continue; Chris@360: Chris@76: size_t frame = 0; Chris@360: Chris@360: if (!realign) { Chris@360: Chris@360: frame = i->getFrame(); Chris@360: Chris@360: } else { Chris@360: Chris@360: if (i->haveReferenceFrame()) { 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@76: SparseTimeValueModel::Point newPoint(frame); Chris@76: Chris@125: if (i->haveLabel()) { Chris@125: newPoint.label = i->getLabel(); Chris@125: } else if (i->haveValue()) { Chris@125: newPoint.label = QString("%1").arg(i->getValue()); Chris@125: } Chris@125: Chris@372: bool usePrev = false; Chris@372: SparseTimeValueModel::Point formerPrevPoint = prevPoint; Chris@372: Chris@125: if (i->haveValue()) { Chris@125: newPoint.value = i->getValue(); Chris@125: } else { Chris@372: // std::cerr << "Setting value on point at " << newPoint.frame << " from labeller"; Chris@372: // if (i == points.begin()) { Chris@372: // std::cerr << ", no prev point" << std::endl; Chris@372: // } else { Chris@372: // std::cerr << ", prev point is at " << prevPoint.frame << std::endl; Chris@372: // } Chris@340: labeller.setValue Chris@371: (newPoint, (i == points.begin()) ? 0 : &prevPoint); Chris@372: // std::cerr << "New point value = " << newPoint.value << std::endl; Chris@372: if (labeller.actingOnPrevPoint() && i != points.begin()) { Chris@372: usePrev = true; Chris@372: } Chris@372: } Chris@372: Chris@372: if (usePrev) { Chris@372: command->deletePoint(formerPrevPoint); Chris@372: command->addPoint(prevPoint); Chris@340: } Chris@125: Chris@340: prevPoint = newPoint; Chris@76: command->addPoint(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@316: SingleColourLayer::toXml(stream, indent, Chris@316: extraAttributes + Chris@513: QString(" colourMap=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\" scaleMinimum=\"%4\" scaleMaximum=\"%5\" drawDivisions=\"%6\" ") Chris@316: .arg(m_colourMap) Chris@316: .arg(m_plotStyle) Chris@445: .arg(m_verticalScale) Chris@445: .arg(m_scaleMinimum) Chris@513: .arg(m_scaleMaximum) Chris@513: .arg(m_drawSegmentDivisions ? "true" : "false")); 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@287: int cmap = attributes.value("colourMap").toInt(&ok); Chris@287: if (ok) setFillColourMap(cmap); Chris@287: Chris@11: PlotStyle style = (PlotStyle) Chris@11: attributes.value("plotStyle").toInt(&ok); Chris@11: if (ok) setPlotStyle(style); Chris@286: Chris@286: VerticalScale scale = (VerticalScale) Chris@286: 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@445: float min = attributes.value("scaleMinimum").toFloat(&ok); Chris@445: float max = attributes.value("scaleMaximum").toFloat(&alsoOk); Chris@445: if (ok && alsoOk) setDisplayExtents(min, max); Chris@11: } Chris@11: