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@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: TimeValueLayer::TimeValueLayer(View *w) : Chris@0: Layer(w), Chris@0: m_model(0), Chris@0: m_colour(Qt::black), Chris@6: m_plotStyle(PlotConnectedPoints) Chris@0: { Chris@0: m_view->addLayer(this); 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@10: if (max) *max = 4; 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@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@0: TimeValueLayer::isLayerScrollable() 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@0: return !m_view->shouldIlluminateLocalFeatures(this, discard); Chris@0: } Chris@0: Chris@0: QRect Chris@0: TimeValueLayer::getFeatureDescriptionRect(QPainter &paint, QPoint pos) const Chris@0: { Chris@0: return QRect(0, 0, Chris@0: std::max(100, paint.fontMetrics().width(tr("No local points"))), Chris@0: 70); //!!! Chris@0: } Chris@0: Chris@0: //!!! too much in common with TimeInstantLayer Chris@0: Chris@0: SparseTimeValueModel::PointList Chris@0: TimeValueLayer::getLocalPoints(int x) const Chris@0: { Chris@0: if (!m_model) return SparseTimeValueModel::PointList(); Chris@0: Chris@20: long frame = 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@20: } else if (prevPoints.begin()->frame < m_view->getStartFrame() && Chris@20: !(nextPoints.begin()->frame > m_view->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@0: return usePoints; Chris@0: } Chris@0: Chris@0: void Chris@0: TimeValueLayer::paintLocalFeatureDescription(QPainter &paint, QRect rect, Chris@0: QPoint pos) const Chris@0: { Chris@0: //!!! bleagh Chris@0: Chris@0: int x = pos.x(); Chris@0: Chris@0: if (!m_model || !m_model->getSampleRate()) return; Chris@0: Chris@0: SparseTimeValueModel::PointList points = getLocalPoints(x); Chris@0: Chris@0: QFontMetrics metrics = paint.fontMetrics(); Chris@0: int xbase = rect.x() + 5; Chris@0: int ybase = rect.y() + 5; Chris@0: Chris@0: if (points.empty()) { Chris@0: QString label = tr("No local points"); Chris@0: if (!m_model->isReady()) { Chris@0: label = tr("In progress"); Chris@0: } Chris@0: paint.drawText(xbase + 5, ybase + 5 + metrics.ascent(), label); Chris@0: return; Chris@0: } Chris@0: Chris@0: long useFrame = points.begin()->frame; Chris@0: Chris@0: RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); Chris@0: QString timeText = QString("%1").arg(rt.toText(true).c_str()); Chris@0: QString valueText = QString("%1").arg(points.begin()->value); Chris@0: Chris@0: int timewidth = metrics.width(timeText); Chris@0: int valuewidth = metrics.width(valueText); Chris@0: int labelwidth = metrics.width(points.begin()->label); Chris@0: Chris@0: int boxheight = metrics.height() * 3 + 4; Chris@0: int boxwidth = std::max(std::max(timewidth, labelwidth), valuewidth); Chris@0: Chris@0: paint.drawRect(xbase, ybase, boxwidth + 10, Chris@0: boxheight + 10 - metrics.descent() + 1); Chris@0: Chris@0: paint.drawText(xbase + 5, ybase + 5 + metrics.ascent(), timeText); Chris@0: paint.drawText(xbase + 5, ybase + 7 + metrics.ascent() + metrics.height(), Chris@0: valueText); Chris@0: paint.drawText(xbase + 5, ybase + 9 + metrics.ascent() + 2*metrics.height(), Chris@0: points.begin()->label); Chris@0: } Chris@0: Chris@13: int Chris@13: TimeValueLayer::getNearestFeatureFrame(int frame, Chris@13: size_t &resolution, Chris@13: bool snapRight) const Chris@13: { Chris@13: if (!m_model) { Chris@13: return Layer::getNearestFeatureFrame(frame, resolution, snapRight); Chris@13: } Chris@13: Chris@13: resolution = m_model->getResolution(); Chris@13: SparseTimeValueModel::PointList points(m_model->getPoints(frame, frame)); Chris@13: Chris@13: int returnFrame = frame; Chris@13: Chris@13: for (SparseTimeValueModel::PointList::const_iterator i = points.begin(); Chris@13: i != points.end(); ++i) { Chris@13: Chris@13: if (snapRight) { Chris@13: if (i->frame > frame) { Chris@13: returnFrame = i->frame; Chris@13: break; Chris@13: } Chris@13: } else { Chris@13: if (i->frame <= frame) { Chris@13: returnFrame = i->frame; Chris@13: } Chris@13: } Chris@13: } Chris@13: Chris@13: return returnFrame; Chris@13: } Chris@13: Chris@0: void Chris@0: TimeValueLayer::paint(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@20: long frame0 = getFrameForX(x0); Chris@20: long frame1 = 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@0: int origin = int(nearbyint(m_view->height() - Chris@0: (-min * m_view->height()) / (max - min))); Chris@0: Chris@0: QPoint localPos; Chris@0: long illuminateFrame = -1; Chris@0: Chris@0: if (m_view->shouldIlluminateLocalFeatures(this, localPos)) { Chris@0: SparseTimeValueModel::PointList localPoints = Chris@0: getLocalPoints(localPos.x()); Chris@0: if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame; Chris@0: } Chris@6: Chris@20: int w = Chris@20: getXForFrame(frame0 + m_model->getResolution()) - Chris@20: getXForFrame(frame0); Chris@7: Chris@6: paint.save(); Chris@6: Chris@7: if (w > 1 && Chris@7: (m_plotStyle == PlotLines || Chris@7: m_plotStyle == PlotCurve)) { Chris@6: paint.setRenderHint(QPainter::Antialiasing, true); Chris@6: } Chris@6: QPainterPath path; 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@20: int x = getXForFrame(p.frame); Chris@0: int y = int(nearbyint(m_view->height() - Chris@0: ((p.value - min) * m_view->height()) / Chris@0: (max - min))); Chris@0: Chris@0: if (w < 1) w = 1; Chris@0: Chris@6: if (m_plotStyle == PlotLines || Chris@6: m_plotStyle == PlotCurve) { Chris@6: paint.setPen(m_colour); Chris@6: paint.setBrush(Qt::NoBrush); Chris@3: } else { Chris@3: paint.setPen(m_colour); 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@6: paint.setBrush(Qt::black);//!!! Chris@6: } Chris@0: } Chris@0: Chris@6: if (m_plotStyle != PlotLines && Chris@6: m_plotStyle != PlotCurve) { 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@0: SparseTimeValueModel::PointList::const_iterator j = i; Chris@0: ++j; Chris@3: Chris@0: if (j != points.end()) { Chris@3: Chris@0: const SparseTimeValueModel::Point &q(*j); Chris@20: int nx = getXForFrame(q.frame); Chris@0: int ny = int(nearbyint(m_view->height() - Chris@0: ((q.value - min) * m_view->height()) / Chris@0: (max - min))); Chris@3: Chris@6: if (m_plotStyle == PlotConnectedPoints) { Chris@6: 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@6: if (path.isEmpty()) { Chris@6: path.moveTo(x + w/2, y); Chris@6: } Chris@6: Chris@6: if (nx - x > 5) { Chris@6: path.cubicTo(x + w, y, nx, ny, nx + w/2, ny); Chris@6: } else { Chris@6: path.lineTo(nx + w/2, ny); Chris@6: } Chris@3: } Chris@0: } Chris@0: } Chris@0: Chris@0: /// if (p.label != "") { Chris@0: /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label); Chris@0: /// } Chris@0: } Chris@6: Chris@6: if (m_plotStyle == PlotCurve && !path.isEmpty()) { 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@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: