Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@3: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. Chris@0: */ Chris@0: Chris@0: #include "TimeValueLayer.h" Chris@0: Chris@0: #include "base/Model.h" Chris@0: #include "base/RealTime.h" Chris@0: #include "base/Profiler.h" Chris@0: #include "base/View.h" Chris@0: Chris@0: #include "model/SparseTimeValueModel.h" Chris@0: Chris@0: #include Chris@6: #include Chris@21: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@44: TimeValueLayer::TimeValueLayer() : Chris@44: Layer(), 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@0: m_colour(Qt::black), Chris@6: m_plotStyle(PlotConnectedPoints) 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@0: connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged())); Chris@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SIGNAL(modelChanged(size_t, size_t))); Chris@0: Chris@0: connect(m_model, SIGNAL(completionChanged()), Chris@0: this, SIGNAL(modelCompletionChanged())); Chris@0: Chris@0: 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@0: PropertyList list; Chris@0: list.push_back(tr("Colour")); Chris@0: list.push_back(tr("Plot Type")); Chris@0: return list; Chris@0: } Chris@0: Chris@0: Layer::PropertyType Chris@0: TimeValueLayer::getPropertyType(const PropertyName &name) const Chris@0: { Chris@0: return ValueProperty; Chris@0: } Chris@0: Chris@0: int Chris@0: TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@0: int *min, int *max) const Chris@0: { Chris@0: //!!! factor this colour handling stuff out into a colour manager class Chris@0: Chris@0: int deft = 0; Chris@0: Chris@0: if (name == tr("Colour")) { Chris@0: Chris@10: if (min) *min = 0; Chris@10: if (max) *max = 5; Chris@0: Chris@0: if (m_colour == Qt::black) deft = 0; Chris@0: else if (m_colour == Qt::darkRed) deft = 1; Chris@0: else if (m_colour == Qt::darkBlue) deft = 2; Chris@0: else if (m_colour == Qt::darkGreen) deft = 3; Chris@0: else if (m_colour == QColor(200, 50, 255)) deft = 4; Chris@0: else if (m_colour == QColor(255, 150, 50)) deft = 5; Chris@0: Chris@0: } else if (name == tr("Plot Type")) { Chris@0: Chris@10: if (min) *min = 0; Chris@26: if (max) *max = 5; Chris@0: Chris@0: deft = int(m_plotStyle); Chris@0: Chris@0: } else { Chris@0: Chris@0: deft = Layer::getPropertyRangeAndValue(name, min, max); Chris@0: } Chris@0: Chris@0: return deft; Chris@0: } Chris@0: Chris@0: QString Chris@0: TimeValueLayer::getPropertyValueLabel(const PropertyName &name, Chris@0: int value) const Chris@0: { Chris@0: if (name == tr("Colour")) { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("Black"); Chris@0: case 1: return tr("Red"); Chris@0: case 2: return tr("Blue"); Chris@0: case 3: return tr("Green"); Chris@0: case 4: return tr("Purple"); Chris@0: case 5: return tr("Orange"); Chris@0: } Chris@0: } else if (name == tr("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@0: } Chris@0: return tr(""); Chris@0: } Chris@0: Chris@0: void Chris@0: TimeValueLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@0: if (name == tr("Colour")) { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: setBaseColour(Qt::black); break; Chris@0: case 1: setBaseColour(Qt::darkRed); break; Chris@0: case 2: setBaseColour(Qt::darkBlue); break; Chris@0: case 3: setBaseColour(Qt::darkGreen); break; Chris@0: case 4: setBaseColour(QColor(200, 50, 255)); break; Chris@0: case 5: setBaseColour(QColor(255, 150, 50)); break; Chris@0: } Chris@0: } else if (name == tr("Plot Type")) { Chris@0: setPlotStyle(PlotStyle(value)); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: TimeValueLayer::setBaseColour(QColor colour) Chris@0: { Chris@0: if (m_colour == colour) return; Chris@0: m_colour = colour; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: void Chris@0: TimeValueLayer::setPlotStyle(PlotStyle style) Chris@0: { Chris@0: if (m_plotStyle == style) return; Chris@0: m_plotStyle = style; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: 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@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@44: } else if (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@0: Chris@25: if (points.begin()->label == "") { Chris@25: text = QString(tr("Time:\t%1\nValue:\t%2\nNo label")) Chris@25: .arg(rt.toText(true).c_str()) Chris@25: .arg(points.begin()->value); Chris@25: } else { Chris@25: text = QString(tr("Time:\t%1\nValue:\t%2\nLabel:\t%3")) Chris@25: .arg(rt.toText(true).c_str()) Chris@25: .arg(points.begin()->value) 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@21: int Chris@44: TimeValueLayer::getYForValue(View *v, float value) const Chris@21: { Chris@21: float min = m_model->getValueMinimum(); Chris@21: float max = m_model->getValueMaximum(); Chris@21: if (max == min) max = min + 1.0; Chris@21: Chris@44: int h = v->height(); Chris@21: Chris@21: return int(h - ((value - min) * h) / (max - min)); Chris@21: } Chris@21: Chris@21: float Chris@44: TimeValueLayer::getValueForY(View *v, int y) const Chris@21: { Chris@21: float min = m_model->getValueMinimum(); Chris@21: float max = m_model->getValueMaximum(); Chris@21: if (max == min) max = min + 1.0; Chris@21: Chris@44: int h = v->height(); Chris@21: Chris@21: return min + (float(h - y) * float(max - min)) / h; Chris@21: } Chris@21: 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@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@0: paint.setPen(m_colour); Chris@0: Chris@0: QColor brushColour(m_colour); 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@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@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@34: bool haveNext = false; Chris@44: int nx = v->getXForFrame(m_model->getEndFrame()); 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@34: } Chris@34: Chris@34: int labelY = y; Chris@34: Chris@0: if (w < 1) w = 1; Chris@26: paint.setPen(m_colour); Chris@0: Chris@26: if (m_plotStyle == PlotSegmentation) { Chris@26: int value = ((p.value - min) / (max - min)) * 255.999; Chris@26: QColor colour = QColor::fromHsv(256 - value, value / 2 + 128, value); Chris@26: paint.setBrush(QColor(colour.red(), colour.green(), colour.blue(), Chris@26: 120)); Chris@44: labelY = v->height(); 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@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@0: paint.setPen(m_colour); 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@6: paint.setPen(Qt::black);//!!! Chris@26: if (m_plotStyle != PlotSegmentation) { Chris@26: paint.setBrush(Qt::black);//!!! Chris@26: } Chris@6: } Chris@0: } Chris@0: Chris@6: if (m_plotStyle != PlotLines && Chris@26: m_plotStyle != PlotCurve && Chris@26: m_plotStyle != PlotSegmentation) { Chris@3: paint.drawRect(x, y - 1, w, 2); 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@3: paint.setPen(brushColour); Chris@3: paint.drawLine(x + w, y, nx, ny); Chris@6: Chris@6: } else if (m_plotStyle == PlotLines) { Chris@6: Chris@6: paint.drawLine(x + w/2, y, 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@26: Chris@27: if (nx <= x) continue; Chris@26: Chris@28: if (illuminateFrame != p.frame && Chris@44: (nx < x + 5 || x >= v->width() - 1)) { Chris@27: paint.setPen(Qt::NoPen); 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@55: paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label); Chris@55: } Chris@0: } Chris@6: Chris@6: if (m_plotStyle == PlotCurve && !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@44: TimeValueLayer::getVerticalScaleWidth(View *v, QPainter &paint) const Chris@42: { Chris@55: if (m_plotStyle == PlotSegmentation) return 0; Chris@55: return paint.fontMetrics().width("+0.000e+00") + 15; Chris@42: } Chris@42: Chris@42: void Chris@44: TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const Chris@42: { Chris@42: if (!m_model) return; Chris@55: if (m_plotStyle == PlotSegmentation) return; Chris@42: Chris@44: float val = m_model->getValueMinimum(); Chris@44: float inc = (m_model->getValueMaximum() - val) / 10; Chris@42: Chris@55: char buffer[40]; Chris@55: Chris@55: int w = getVerticalScaleWidth(v, paint); Chris@55: Chris@44: while (val < m_model->getValueMaximum()) { Chris@44: int y = getYForValue(v, val); Chris@55: // QString label = QString("%1").arg(val); Chris@55: sprintf(buffer, "%+.3e", val); Chris@55: QString label = QString(buffer); Chris@55: paint.drawLine(w - 5, y, w, y);// 100 - 10, y, 100, y); Chris@55: paint.drawText(5, // 100 - 15 - paint.fontMetrics().width(label), Chris@55: y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), Chris@42: label); Chris@44: val += inc; Chris@42: } Chris@42: Chris@42: } Chris@42: Chris@21: void Chris@44: TimeValueLayer::drawStart(View *v, QMouseEvent *e) Chris@21: { Chris@21: 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@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@21: m_editingPoint = SparseTimeValueModel::Point(frame, value, tr("New Point")); Chris@23: m_originalPoint = m_editingPoint; Chris@22: Chris@22: if (m_editingCommand) m_editingCommand->finish(); Chris@22: m_editingCommand = new SparseTimeValueModel::EditCommand(m_model, Chris@22: tr("Draw Point")); Chris@22: m_editingCommand->addPoint(m_editingPoint); 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@21: 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@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: 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@44: TimeValueLayer::drawEnd(View *v, QMouseEvent *e) Chris@21: { Chris@21: std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl; Chris@21: if (!m_model || !m_editing) return; Chris@22: m_editingCommand->finish(); Chris@22: m_editingCommand = 0; Chris@21: m_editing = false; Chris@21: } Chris@21: Chris@21: void Chris@44: TimeValueLayer::editStart(View *v, QMouseEvent *e) Chris@21: { Chris@21: 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@22: m_editingCommand->finish(); 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@21: 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@44: TimeValueLayer::editEnd(View *v, QMouseEvent *e) Chris@21: { Chris@21: 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@23: m_editingCommand->finish(); Chris@23: } Chris@23: Chris@22: m_editingCommand = 0; Chris@21: m_editing = false; Chris@21: } Chris@21: Chris@43: void Chris@43: TimeValueLayer::moveSelection(Selection s, size_t newStartFrame) Chris@43: { 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@43: command->finish(); Chris@43: } Chris@43: Chris@43: void Chris@43: TimeValueLayer::resizeSelection(Selection s, Selection newSize) Chris@43: { 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@43: command->finish(); Chris@43: } Chris@43: Chris@6: QString Chris@6: TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const Chris@6: { Chris@6: return Layer::toXmlString(indent, extraAttributes + Chris@6: QString(" colour=\"%1\" plotStyle=\"%2\"") Chris@6: .arg(encodeColour(m_colour)).arg(m_plotStyle)); Chris@0: } Chris@0: Chris@11: void Chris@11: TimeValueLayer::setProperties(const QXmlAttributes &attributes) Chris@11: { Chris@11: QString colourSpec = attributes.value("colour"); Chris@11: if (colourSpec != "") { Chris@11: QColor colour(colourSpec); Chris@11: if (colour.isValid()) { Chris@11: setBaseColour(QColor(colourSpec)); Chris@11: } Chris@11: } Chris@11: Chris@11: bool ok; Chris@11: PlotStyle style = (PlotStyle) Chris@11: attributes.value("plotStyle").toInt(&ok); Chris@11: if (ok) setPlotStyle(style); Chris@11: } Chris@11: Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "TimeValueLayer.moc.cpp" Chris@0: #endif Chris@0: