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 "TimeInstantLayer.h" Chris@0: Chris@128: #include "data/model/Model.h" Chris@0: #include "base/RealTime.h" Chris@128: #include "view/View.h" Chris@0: #include "base/Profiler.h" Chris@76: #include "base/Clipboard.h" Chris@1078: Chris@376: #include "ColourDatabase.h" Chris@1078: #include "PaintAssistant.h" Chris@0: Chris@128: #include "data/model/SparseOneDimensionalModel.h" Chris@0: Chris@70: #include "widgets/ItemEditDialog.h" Chris@358: #include "widgets/ListInputDialog.h" Chris@70: Chris@0: #include Chris@17: #include Chris@316: #include Chris@360: #include Chris@0: Chris@0: #include Martin@46: #include Chris@0: Chris@429: //#define DEBUG_TIME_INSTANT_LAYER 1 Chris@387: Chris@44: TimeInstantLayer::TimeInstantLayer() : Chris@287: SingleColourLayer(), Chris@18: m_editing(false), Chris@17: m_editingPoint(0, tr("New Point")), Chris@1408: m_editingCommand(nullptr), Chris@28: m_plotStyle(PlotInstants) Chris@0: { Chris@308: } Chris@308: Chris@308: TimeInstantLayer::~TimeInstantLayer() Chris@308: { Chris@0: } Chris@0: Chris@1470: int Chris@1470: TimeInstantLayer::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: TimeInstantLayer::setModel(ModelId modelId) Chris@0: { Chris@1471: auto newModel = ModelById::getAs(modelId); Chris@1471: if (!modelId.isNone() && !newModel) { Chris@1471: throw std::logic_error("Not a SparseOneDimensionalModel"); Chris@1471: } Chris@1471: Chris@1470: if (m_model == modelId) return; Chris@1470: m_model = modelId; Chris@0: Chris@1471: if (newModel) { Chris@1471: connectSignals(m_model); Chris@1471: if (newModel->getRDFTypeURI().endsWith("Segment")) { Chris@1471: setPlotStyle(PlotSegmentation); Chris@1471: } Chris@494: } Chris@494: Chris@0: emit modelReplaced(); Chris@0: } Chris@0: Chris@0: Layer::PropertyList Chris@0: TimeInstantLayer::getProperties() const Chris@0: { Chris@287: PropertyList list = SingleColourLayer::getProperties(); Chris@87: list.push_back("Plot Type"); Chris@0: return list; Chris@0: } Chris@0: Chris@87: QString Chris@87: TimeInstantLayer::getPropertyLabel(const PropertyName &name) const Chris@87: { Chris@87: if (name == "Plot Type") return tr("Plot Type"); Chris@287: return SingleColourLayer::getPropertyLabel(name); Chris@87: } Chris@87: Chris@0: Layer::PropertyType Chris@287: TimeInstantLayer::getPropertyType(const PropertyName &name) const Chris@0: { Chris@287: if (name == "Plot Type") return ValueProperty; Chris@287: return SingleColourLayer::getPropertyType(name); Chris@0: } Chris@0: Chris@0: int Chris@0: TimeInstantLayer::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 == "Plot Type") { Chris@1266: Chris@1266: if (min) *min = 0; Chris@1266: if (max) *max = 1; Chris@216: if (deflt) *deflt = 0; Chris@1266: Chris@1266: val = int(m_plotStyle); Chris@28: 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: TimeInstantLayer::getPropertyValueLabel(const PropertyName &name, Chris@287: int value) const Chris@0: { Chris@287: if (name == "Plot Type") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Instants"); Chris@1266: case 1: return tr("Segmentation"); Chris@1266: } Chris@0: } Chris@287: return SingleColourLayer::getPropertyValueLabel(name, value); Chris@0: } Chris@0: Chris@0: void Chris@0: TimeInstantLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@287: if (name == "Plot Type") { Chris@1266: setPlotStyle(PlotStyle(value)); Chris@287: } else { Chris@287: SingleColourLayer::setProperty(name, value); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@28: TimeInstantLayer::setPlotStyle(PlotStyle style) Chris@28: { Chris@28: if (m_plotStyle == style) return; Chris@28: m_plotStyle = style; Chris@28: emit layerParametersChanged(); Chris@28: } Chris@28: Chris@0: bool Chris@1470: TimeInstantLayer::needsTextLabelHeight() const Chris@1470: { Chris@1470: auto model = ModelById::getAs(m_model); Chris@1474: if (model) return model->hasTextLabels(); Chris@1470: else return false; Chris@1470: } Chris@1470: Chris@1470: bool Chris@918: TimeInstantLayer::isLayerScrollable(const LayerGeometryProvider *v) const Chris@0: { Chris@0: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@0: } Chris@0: Chris@1433: EventVector Chris@918: TimeInstantLayer::getLocalPoints(LayerGeometryProvider *v, int x) const Chris@0: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return {}; Chris@1433: Chris@28: // Return a set of points that all have the same frame number, the Chris@28: // nearest to the given x coordinate, and that are within a Chris@28: // certain fuzz distance of that x coordinate. Chris@28: Chris@989: sv_frame_t frame = v->getFrameForX(x); Chris@0: Chris@1474: EventVector exact = model->getEventsStartingAt(frame); Chris@1433: if (!exact.empty()) return exact; Chris@0: Chris@1433: // overspill == 1, so one event either side of the given span Chris@1474: EventVector neighbouring = model->getEventsWithin Chris@1474: (frame, model->getResolution(), 1); Chris@0: Chris@1433: double fuzz = v->scaleSize(2); Chris@1433: sv_frame_t suitable = 0; Chris@1433: bool have = false; Chris@1433: Chris@1433: for (Event e: neighbouring) { Chris@1433: sv_frame_t f = e.getFrame(); Chris@1433: if (f < v->getStartFrame() || f > v->getEndFrame()) { Chris@1433: continue; Chris@1433: } Chris@1433: int px = v->getXForFrame(f); Chris@1433: if ((px > x && px - x > fuzz) || (px < x && x - px > fuzz + 3)) { Chris@1433: continue; Chris@1433: } Chris@1433: if (!have) { Chris@1433: suitable = f; Chris@1433: have = true; Chris@1433: } else if (llabs(frame - f) < llabs(suitable - f)) { Chris@1433: suitable = f; Chris@1266: } Chris@28: } Chris@28: Chris@1433: if (have) { Chris@1474: return model->getEventsStartingAt(suitable); Chris@1433: } else { Chris@1433: return {}; Chris@1433: } Chris@0: } Chris@0: Chris@25: QString Chris@909: TimeInstantLayer::getLabelPreceding(sv_frame_t frame) const Chris@552: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !model->hasTextLabels()) return ""; Chris@1433: Chris@1433: Event e; Chris@1474: if (model->getNearestEventMatching Chris@1433: (frame, Chris@1433: [](Event e) { return e.hasLabel() && e.getLabel() != ""; }, Chris@1433: EventSeries::Backward, Chris@1433: e)) { Chris@1433: return e.getLabel(); Chris@552: } Chris@1433: Chris@552: return ""; Chris@552: } Chris@552: Chris@552: QString Chris@918: TimeInstantLayer::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@1433: 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@1433: sv_frame_t useFrame = points.begin()->getFrame(); Chris@0: Chris@1474: RealTime rt = RealTime::frame2RealTime(useFrame, model->getSampleRate()); Chris@25: Chris@25: QString text; Chris@0: Chris@1433: if (points.begin()->getLabel() == "") { Chris@1266: text = QString(tr("Time:\t%1\nNo label")) Chris@1266: .arg(rt.toText(true).c_str()); Chris@25: } else { Chris@1266: text = QString(tr("Time:\t%1\nLabel:\t%2")) Chris@1266: .arg(rt.toText(true).c_str()) Chris@1433: .arg(points.begin()->getLabel()); Chris@25: } Chris@0: Chris@44: pos = QPoint(v->getXForFrame(useFrame), pos.y()); Chris@25: return text; Chris@0: } Chris@0: Chris@28: bool Chris@918: TimeInstantLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, Chris@1266: int &resolution, Chris@1547: SnapType snap, int ycoord) const Chris@13: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) { Chris@1547: return Layer::snapToFeatureFrame(v, frame, resolution, snap, ycoord); Chris@13: } Chris@13: Chris@1433: // SnapLeft / SnapRight: return frame of nearest feature in that Chris@1433: // direction no matter how far away Chris@1433: // Chris@1433: // SnapNeighbouring: return frame of feature that would be used in Chris@1433: // an editing operation, i.e. closest feature in either direction Chris@1433: // but only if it is "close enough" Chris@1433: Chris@1474: resolution = model->getResolution(); Chris@13: Chris@28: if (snap == SnapNeighbouring) { Chris@1433: EventVector points = getLocalPoints(v, v->getXForFrame(frame)); Chris@1266: if (points.empty()) return false; Chris@1433: frame = points.begin()->getFrame(); Chris@1266: return true; Chris@13: } Chris@13: Chris@1433: Event e; Chris@1474: if (model->getNearestEventMatching Chris@1433: (frame, Chris@1433: [](Event) { return true; }, Chris@1433: snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward, Chris@1433: e)) { Chris@1433: frame = e.getFrame(); Chris@1433: return true; Chris@1433: } Chris@1433: Chris@1433: return false; Chris@13: } Chris@13: Chris@0: void Chris@916: TimeInstantLayer::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@0: // Profiler profiler("TimeInstantLayer::paint", true); Chris@0: Chris@1549: int x0 = rect.left(); Chris@1549: int x1 = x0 + rect.width(); Chris@0: Chris@1549: sv_frame_t resolution = model->getResolution(); Chris@1549: Chris@1549: sv_frame_t frame0 = v->getFrameForX(x0) - resolution; Chris@989: sv_frame_t frame1 = v->getFrameForX(x1); Chris@0: Chris@1549: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@1549: SVCERR << "TimeInstantLayer[" << this << "]::paint: x0 = " Chris@1549: << x0 << ", x1 = " << x1 << ", frame0 = " << frame0 Chris@1549: << ", frame1 = " << frame1 << endl; Chris@1549: #endif Chris@1549: Chris@1461: int overspill = 0; Chris@1461: if (m_plotStyle == PlotSegmentation) { Chris@1461: // We need to start painting at the prior point, so we can Chris@1461: // fill in the visible part of its segmentation area Chris@1461: overspill = 1; Chris@1461: } Chris@1461: Chris@1474: EventVector points(model->getEventsWithin(frame0, frame1 - frame0, Chris@1549: overspill)); Chris@1549: Chris@1549: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@1549: SVCERR << "TimeInstantLayer[" << this << "]::paint: have " << points.size() Chris@1549: << " point(s) with overspill = " << overspill << " from model " Chris@1549: << getModel() << endl; Chris@1549: #endif Chris@0: Chris@28: bool odd = false; Chris@28: if (m_plotStyle == PlotSegmentation && !points.empty()) { Chris@1474: int index = model->getRowForFrame(points.begin()->getFrame()); Chris@1266: odd = ((index % 2) == 1); Chris@28: } Chris@28: Chris@287: paint.setPen(getBaseQColor()); Chris@0: Chris@287: QColor brushColour(getBaseQColor()); Chris@0: brushColour.setAlpha(100); Chris@0: paint.setBrush(brushColour); Chris@0: Chris@28: QColor oddBrushColour(brushColour); Chris@28: if (m_plotStyle == PlotSegmentation) { Chris@1266: if (getBaseQColor() == Qt::black) { Chris@1266: oddBrushColour = Qt::gray; Chris@1266: } else if (getBaseQColor() == Qt::darkRed) { Chris@1266: oddBrushColour = Qt::red; Chris@1266: } else if (getBaseQColor() == Qt::darkBlue) { Chris@1266: oddBrushColour = Qt::blue; Chris@1266: } else if (getBaseQColor() == Qt::darkGreen) { Chris@1266: oddBrushColour = Qt::green; Chris@1266: } else { Chris@1474: oddBrushColour = oddBrushColour.lighter(150); Chris@1266: } Chris@1266: oddBrushColour.setAlpha(100); Chris@28: } Chris@28: Chris@0: QPoint localPos; Chris@989: sv_frame_t illuminateFrame = -1; Chris@0: Chris@44: if (v->shouldIlluminateLocalFeatures(this, localPos)) { Chris@1433: EventVector localPoints = getLocalPoints(v, localPos.x()); Chris@1433: if (!localPoints.empty()) { Chris@1433: illuminateFrame = localPoints.begin()->getFrame(); Chris@1433: } Chris@0: } Chris@1266: Chris@23: int prevX = -1; Chris@1537: int textY = v->getTextLabelYCoord(this, paint); Chris@79: Chris@1433: for (EventVector::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@0: Chris@1433: Event p(*i); Chris@1433: EventVector::const_iterator j = i; Chris@1266: ++j; Chris@0: Chris@1433: int x = v->getXForFrame(p.getFrame()); Chris@1549: Chris@1549: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@1549: SVCERR << "point frame = " << p.getFrame() << " -> x = " << x << endl; Chris@1549: #endif Chris@1549: Chris@576: if (x == prevX && m_plotStyle == PlotInstants && Chris@1549: p.getFrame() != illuminateFrame) { Chris@1549: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@1549: SVCERR << "(skipping)" << endl; Chris@1549: #endif Chris@1549: continue; Chris@1549: } Chris@23: Chris@1474: int iw = v->getXForFrame(p.getFrame() + model->getResolution()) - x; Chris@1266: if (iw < 2) { Chris@1266: if (iw < 1) { Chris@1266: iw = 2; Chris@1266: if (j != points.end()) { Chris@1433: int nx = v->getXForFrame(j->getFrame()); Chris@1266: if (nx < x + 3) iw = 1; Chris@1266: } Chris@1266: } else { Chris@1266: iw = 2; Chris@1266: } Chris@1266: } Chris@1266: Chris@1433: if (p.getFrame() == illuminateFrame) { Chris@1266: paint.setPen(getForegroundQColor(v->getView())); Chris@1266: } else { Chris@1266: paint.setPen(brushColour); Chris@1266: } Chris@23: Chris@1549: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@1549: SVCERR << "m_plotStyle = " << m_plotStyle << ", iw = " << iw << endl; Chris@1549: #endif Chris@1549: Chris@1266: if (m_plotStyle == PlotInstants) { Chris@1266: if (iw > 1) { Chris@1266: paint.drawRect(x, 0, iw - 1, v->getPaintHeight() - 1); Chris@1266: } else { Chris@1266: paint.drawLine(x, 0, x, v->getPaintHeight() - 1); Chris@1266: } Chris@1266: } else { Chris@28: Chris@1266: if (odd) paint.setBrush(oddBrushColour); Chris@1266: else paint.setBrush(brushColour); Chris@1266: Chris@1266: int nx; Chris@1266: Chris@1266: if (j != points.end()) { Chris@1433: Event q(*j); Chris@1433: nx = v->getXForFrame(q.getFrame()); Chris@1266: } else { Chris@1474: nx = v->getXForFrame(model->getEndFrame()); Chris@1266: } Chris@28: Chris@1266: if (nx >= x) { Chris@1266: Chris@1433: if (illuminateFrame != p.getFrame() && Chris@1266: (nx < x + 5 || x >= v->getPaintWidth() - 1)) { Chris@1266: paint.setPen(Qt::NoPen); Chris@1266: } Chris@28: Chris@918: paint.drawRect(x, -1, nx - x, v->getPaintHeight() + 1); Chris@1266: } Chris@28: Chris@1266: odd = !odd; Chris@1266: } Chris@28: Chris@1266: paint.setPen(getBaseQColor()); Chris@1266: Chris@1433: if (p.getLabel() != "") { Chris@1474: 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@0: Chris@1266: // only draw if there's enough room from here to the next point Chris@0: Chris@1433: int lw = paint.fontMetrics().width(p.getLabel()); Chris@1266: bool good = true; Chris@0: Chris@1266: if (j != points.end()) { Chris@1433: int nx = v->getXForFrame(j->getFrame()); Chris@1266: if (nx >= x && nx - x - iw - 3 <= lw) good = false; Chris@1266: } Chris@0: Chris@1266: if (good) { Chris@1273: PaintAssistant::drawVisibleText(v, paint, Chris@1273: x + iw + 2, textY, Chris@1433: p.getLabel(), Chris@1273: PaintAssistant::OutlinedText); Chris@1266: } Chris@1266: } Chris@23: Chris@1266: prevX = x; Chris@0: } Chris@0: } Chris@0: Chris@17: void Chris@918: TimeInstantLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@17: { Chris@387: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@682: cerr << "TimeInstantLayer::drawStart(" << e->x() << ")" << endl; Chris@387: #endif Chris@17: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@17: Chris@989: sv_frame_t frame = v->getFrameForX(e->x()); Chris@17: if (frame < 0) frame = 0; Chris@1474: frame = frame / model->getResolution() * model->getResolution(); Chris@22: Chris@1433: m_editingPoint = Event(frame, tr("New Point")); Chris@22: Chris@376: if (m_editingCommand) finish(m_editingCommand); Chris@1474: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Draw Point")); Chris@1433: m_editingCommand->add(m_editingPoint); Chris@22: Chris@18: m_editing = true; Chris@17: } Chris@17: Chris@17: void Chris@918: TimeInstantLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@17: { Chris@387: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@682: cerr << "TimeInstantLayer::drawDrag(" << e->x() << ")" << endl; Chris@387: #endif Chris@17: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@17: Chris@989: sv_frame_t frame = v->getFrameForX(e->x()); Chris@17: if (frame < 0) frame = 0; Chris@1474: frame = frame / model->getResolution() * model->getResolution(); Chris@1433: m_editingCommand->remove(m_editingPoint); Chris@1433: m_editingPoint = m_editingPoint.withFrame(frame); Chris@1433: m_editingCommand->add(m_editingPoint); Chris@17: } Chris@17: Chris@17: void Chris@1549: TimeInstantLayer::drawEnd(LayerGeometryProvider *, QMouseEvent * Chris@1549: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@1549: e Chris@1549: #endif Chris@1549: ) Chris@17: { Chris@387: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@682: cerr << "TimeInstantLayer::drawEnd(" << e->x() << ")" << endl; Chris@387: #endif Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@23: QString newName = tr("Add Point at %1 s") Chris@1433: .arg(RealTime::frame2RealTime(m_editingPoint.getFrame(), Chris@1474: model->getSampleRate()) Chris@1266: .toText(false).c_str()); Chris@23: m_editingCommand->setName(newName); Chris@376: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@18: m_editing = false; Chris@18: } Chris@18: Chris@18: void Chris@918: TimeInstantLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@335: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@335: Chris@1433: 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: TimeInstantLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *) Chris@335: { Chris@335: } Chris@335: Chris@335: void Chris@918: TimeInstantLayer::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@1433: EventVector points = getLocalPoints(v, e->x()); Chris@335: if (points.empty()) return; Chris@1433: if (points.begin()->getFrame() != m_editingPoint.getFrame()) return; Chris@335: Chris@1474: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Erase Point")); Chris@1433: 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: TimeInstantLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@18: { Chris@387: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@682: cerr << "TimeInstantLayer::editStart(" << e->x() << ")" << endl; Chris@387: #endif Chris@18: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@18: Chris@1433: EventVector points = getLocalPoints(v, e->x()); Chris@18: if (points.empty()) return; Chris@18: Chris@18: m_editingPoint = *points.begin(); Chris@22: Chris@22: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@22: } Chris@22: Chris@18: m_editing = true; Chris@18: } Chris@18: Chris@18: void Chris@918: TimeInstantLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@18: { Chris@387: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@682: cerr << "TimeInstantLayer::editDrag(" << e->x() << ")" << endl; Chris@387: #endif Chris@18: Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@18: Chris@989: sv_frame_t frame = v->getFrameForX(e->x()); Chris@18: if (frame < 0) frame = 0; Chris@1474: frame = frame / model->getResolution() * model->getResolution(); Chris@22: Chris@22: if (!m_editingCommand) { Chris@1474: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Drag Point")); Chris@22: } Chris@22: Chris@1433: m_editingCommand->remove(m_editingPoint); Chris@1433: m_editingPoint = m_editingPoint.withFrame(frame); Chris@1433: m_editingCommand->add(m_editingPoint); Chris@18: } Chris@18: Chris@18: void Chris@1549: TimeInstantLayer::editEnd(LayerGeometryProvider *, QMouseEvent * Chris@1549: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@1549: e Chris@1549: #endif Chris@1549: ) Chris@18: { Chris@387: #ifdef DEBUG_TIME_INSTANT_LAYER Chris@682: cerr << "TimeInstantLayer::editEnd(" << e->x() << ")" << endl; Chris@387: #endif Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model || !m_editing) return; Chris@23: if (m_editingCommand) { Chris@1266: QString newName = tr("Move Point to %1 s") Chris@1433: .arg(RealTime::frame2RealTime(m_editingPoint.getFrame(), Chris@1474: model->getSampleRate()) Chris@1266: .toText(false).c_str()); Chris@1266: m_editingCommand->setName(newName); Chris@1266: finish(m_editingCommand); Chris@23: } Chris@1408: m_editingCommand = nullptr; Chris@18: m_editing = false; Chris@17: } Chris@17: Chris@255: bool Chris@918: TimeInstantLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e) Chris@70: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return false; Chris@70: Chris@1433: EventVector points = getLocalPoints(v, e->x()); Chris@255: if (points.empty()) return false; Chris@70: Chris@1433: Event point = *points.begin(); Chris@70: Chris@70: ItemEditDialog *dialog = new ItemEditDialog Chris@1474: (model->getSampleRate(), Chris@70: ItemEditDialog::ShowTime | Chris@70: ItemEditDialog::ShowText); Chris@70: Chris@1433: dialog->setFrameTime(point.getFrame()); Chris@1433: dialog->setText(point.getLabel()); Chris@70: Chris@70: if (dialog->exec() == QDialog::Accepted) { Chris@70: Chris@1433: Event newPoint = point Chris@1433: .withFrame(dialog->getFrameTime()) Chris@1433: .withLabel(dialog->getText()); Chris@70: Chris@1433: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Edit Point")); Chris@1433: command->remove(point); Chris@1433: 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: TimeInstantLayer::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@1433: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Drag Selection")); Chris@43: Chris@1433: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1433: for (auto p: points) { Chris@1433: Event newPoint = p Chris@1433: .withFrame(p.getFrame() + newStartFrame - s.getStartFrame()); Chris@1433: command->remove(p); Chris@1433: command->add(newPoint); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@43: void Chris@43: TimeInstantLayer::resizeSelection(Selection s, Selection newSize) Chris@43: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@99: Chris@1433: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Resize Selection")); Chris@43: Chris@1433: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1433: double ratio = double(newSize.getDuration()) / double(s.getDuration()); Chris@1433: double oldStart = double(s.getStartFrame()); Chris@1433: double newStart = double(newSize.getStartFrame()); Chris@43: Chris@1433: for (auto p: points) { Chris@43: Chris@1433: double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart; Chris@43: Chris@1433: Event newPoint = p Chris@1433: .withFrame(lrint(newFrame)); Chris@1433: command->remove(p); Chris@1433: command->add(newPoint); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@43: void Chris@43: TimeInstantLayer::deleteSelection(Selection s) Chris@43: { Chris@1474: auto model = ModelById::getAs(m_model); Chris@1474: if (!model) return; Chris@99: Chris@1433: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Delete Selection")); Chris@43: Chris@1433: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1433: for (auto p: points) { Chris@1433: command->remove(p); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@76: Chris@76: void Chris@918: TimeInstantLayer::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@1433: EventVector points = Chris@1474: model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@76: Chris@1433: for (auto p: points) { Chris@1433: to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame()))); Chris@76: } Chris@76: } Chris@76: Chris@125: bool Chris@1533: TimeInstantLayer::paste(LayerGeometryProvider *v, const Clipboard &from, Chris@1533: sv_frame_t frameOffset, bool) 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@358: bool realign = false; Chris@358: Chris@360: if (clipboardHasDifferentAlignment(v, from)) { Chris@358: Chris@360: QMessageBox::StandardButton button = Chris@918: QMessageBox::question(v->getView(), tr("Re-align pasted instants?"), Chris@360: tr("The instants 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@358: Chris@360: if (button == QMessageBox::Cancel) { Chris@360: return false; Chris@360: } Chris@358: Chris@360: if (button == QMessageBox::Yes) { Chris@360: realign = true; Chris@360: } Chris@358: } Chris@358: Chris@1433: ChangeEventsCommand *command = Chris@1474: new ChangeEventsCommand(m_model.untyped, tr("Paste")); Chris@358: 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@359: Chris@359: if (!realign) { Chris@359: Chris@359: frame = i->getFrame(); Chris@359: Chris@359: } else { Chris@359: Chris@1423: if (i->hasReferenceFrame()) { Chris@359: frame = i->getReferenceFrame(); Chris@359: frame = alignFromReference(v, frame); Chris@359: } else { Chris@359: frame = i->getFrame(); Chris@359: } Chris@76: } Chris@359: Chris@359: if (frameOffset > 0) frame += frameOffset; Chris@359: else if (frameOffset < 0) { Chris@359: if (frame > -frameOffset) frame += frameOffset; Chris@359: else frame = 0; Chris@359: } Chris@359: Chris@1533: Event newPoint = i->withFrame(frame); Chris@1533: Chris@1433: if (!i->hasLabel() && i->hasValue()) { Chris@1433: newPoint = newPoint.withLabel(QString("%1").arg(i->getValue())); Chris@125: } Chris@76: Chris@1433: command->add(newPoint); Chris@76: } Chris@76: Chris@376: finish(command); Chris@125: return true; Chris@76: } Chris@43: Chris@287: int Chris@287: TimeInstantLayer::getDefaultColourHint(bool darkbg, bool &impose) Chris@287: { Chris@287: impose = false; Chris@287: return ColourDatabase::getInstance()->getColourIndex Chris@287: (QString(darkbg ? "Bright Purple" : "Purple")); Chris@287: } Chris@287: Chris@316: void Chris@316: TimeInstantLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@6: { Chris@316: SingleColourLayer::toXml(stream, indent, Chris@316: extraAttributes + Chris@316: QString(" plotStyle=\"%1\"") Chris@316: .arg(m_plotStyle)); Chris@6: } Chris@0: Chris@11: void Chris@11: TimeInstantLayer::setProperties(const QXmlAttributes &attributes) Chris@11: { Chris@287: SingleColourLayer::setProperties(attributes); Chris@28: Chris@28: bool ok; Chris@28: PlotStyle style = (PlotStyle) Chris@1266: attributes.value("plotStyle").toInt(&ok); Chris@28: if (ok) setPlotStyle(style); Chris@11: } Chris@11: