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@5: 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 "TimeInstantLayer.h" Chris@0: Chris@0: #include "base/Model.h" Chris@0: #include "base/RealTime.h" Chris@0: #include "base/View.h" Chris@0: #include "base/Profiler.h" Chris@0: Chris@0: #include "model/SparseOneDimensionalModel.h" Chris@0: Chris@0: #include Chris@17: #include Chris@0: Chris@0: #include Chris@0: Chris@0: TimeInstantLayer::TimeInstantLayer(View *w) : Chris@0: Layer(w), Chris@0: m_model(0), Chris@18: m_editing(false), Chris@17: m_editingPoint(0, tr("New Point")), Chris@0: m_colour(QColor(200, 50, 255)) Chris@0: { Chris@0: m_view->addLayer(this); Chris@0: } Chris@0: Chris@0: void Chris@0: TimeInstantLayer::setModel(SparseOneDimensionalModel *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 << "TimeInstantLayer::setModel(" << model << ")" << std::endl; Chris@0: Chris@0: emit modelReplaced(); Chris@0: } Chris@0: Chris@0: Layer::PropertyList Chris@0: TimeInstantLayer::getProperties() const Chris@0: { Chris@0: PropertyList list; Chris@0: list.push_back(tr("Colour")); Chris@0: return list; Chris@0: } Chris@0: Chris@0: Layer::PropertyType Chris@20: TimeInstantLayer::getPropertyType(const PropertyName &) const Chris@0: { Chris@0: return ValueProperty; Chris@0: } Chris@0: Chris@0: int Chris@0: TimeInstantLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@0: int *min, int *max) const 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 { 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: TimeInstantLayer::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: } Chris@0: return tr(""); Chris@0: } Chris@0: Chris@0: void Chris@0: TimeInstantLayer::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: } Chris@0: } Chris@0: Chris@0: void Chris@0: TimeInstantLayer::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: bool Chris@0: TimeInstantLayer::isLayerScrollable() const Chris@0: { Chris@0: QPoint discard; Chris@0: return !m_view->shouldIlluminateLocalFeatures(this, discard); Chris@0: } Chris@0: Chris@0: QRect Chris@0: TimeInstantLayer::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: 50); //!!! cruddy Chris@0: } Chris@0: Chris@0: SparseOneDimensionalModel::PointList Chris@0: TimeInstantLayer::getLocalPoints(int x) const Chris@0: { Chris@0: if (!m_model) return SparseOneDimensionalModel::PointList(); Chris@0: Chris@20: long frame = getFrameForX(x); Chris@0: Chris@0: SparseOneDimensionalModel::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: SparseOneDimensionalModel::PointList prevPoints = Chris@0: m_model->getPreviousPoints(frame); Chris@0: SparseOneDimensionalModel::PointList nextPoints = Chris@0: m_model->getNextPoints(frame); Chris@0: Chris@0: SparseOneDimensionalModel::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: TimeInstantLayer::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: SparseOneDimensionalModel::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@2: QString timeText = QString(tr("Time %1")).arg(rt.toText(true).c_str()); Chris@0: Chris@0: int timewidth = metrics.width(timeText); Chris@0: int labelwidth = metrics.width(points.begin()->label); Chris@0: Chris@0: int boxheight = metrics.height() * 2 + 3; Chris@0: int boxwidth = std::max(timewidth, labelwidth); 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: points.begin()->label); Chris@0: } Chris@0: Chris@13: int Chris@13: TimeInstantLayer::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: SparseOneDimensionalModel::PointList points(m_model->getPoints(frame, frame)); Chris@13: Chris@13: int returnFrame = frame; Chris@13: Chris@13: for (SparseOneDimensionalModel::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: TimeInstantLayer::paint(QPainter &paint, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@0: // Profiler profiler("TimeInstantLayer::paint", true); Chris@0: Chris@20: int x0 = rect.left(), x1 = rect.right(); Chris@0: Chris@20: long frame0 = getFrameForX(x0); Chris@20: long frame1 = getFrameForX(x1); Chris@0: Chris@0: SparseOneDimensionalModel::PointList points(m_model->getPoints Chris@0: (frame0, frame1)); Chris@0: Chris@0: paint.setPen(m_colour); Chris@0: Chris@0: QColor brushColour(m_colour); Chris@0: brushColour.setAlpha(100); Chris@0: paint.setBrush(brushColour); Chris@0: Chris@0: // std::cerr << "TimeInstantLayer::paint: resolution is " Chris@0: // << m_model->getResolution() << " frames" << std::endl; Chris@0: Chris@0: QPoint localPos; Chris@0: long illuminateFrame = -1; Chris@0: Chris@0: if (m_view->shouldIlluminateLocalFeatures(this, localPos)) { Chris@0: SparseOneDimensionalModel::PointList localPoints = Chris@0: getLocalPoints(localPos.x()); Chris@0: if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame; Chris@0: } Chris@0: Chris@0: for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin(); Chris@0: i != points.end(); ++i) { Chris@0: Chris@0: const SparseOneDimensionalModel::Point &p(*i); Chris@17: SparseOneDimensionalModel::PointList::const_iterator j = i; Chris@17: ++j; Chris@0: Chris@20: int x = getXForFrame(p.frame); Chris@20: int iw = getXForFrame(p.frame + m_model->getResolution()) - x; Chris@16: if (iw < 2) { Chris@17: if (iw < 1) { Chris@17: iw = 2; Chris@17: if (j != points.end()) { Chris@20: int nx = getXForFrame(j->frame); Chris@17: if (nx < x + 3) iw = 1; Chris@17: } Chris@17: } else { Chris@17: iw = 2; Chris@17: } Chris@16: } Chris@20: Chris@0: if (p.frame == illuminateFrame) { Chris@0: paint.setPen(Qt::black); //!!! Chris@0: } else { Chris@0: paint.setPen(brushColour); Chris@0: } Chris@16: paint.drawRect(x, 0, iw - 1, m_view->height() - 1); Chris@0: paint.setPen(m_colour); Chris@0: Chris@0: if (p.label != "") { Chris@0: Chris@0: // only draw if there's enough room from here to the next point Chris@0: Chris@0: int lw = paint.fontMetrics().width(p.label); Chris@0: bool good = true; Chris@0: Chris@17: if (j != points.end()) { Chris@20: int nx = getXForFrame(j->frame); Chris@20: if (nx >= x && nx - x - iw - 3 <= lw) good = false; Chris@0: } Chris@0: Chris@0: if (good) { Chris@20: paint.drawText(x + iw + 2, Chris@0: m_view->height() - paint.fontMetrics().height(), Chris@0: p.label); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@17: void Chris@17: TimeInstantLayer::drawStart(QMouseEvent *e) Chris@17: { Chris@17: std::cerr << "TimeInstantLayer::drawStart(" << e->x() << ")" << std::endl; Chris@17: Chris@17: if (!m_model) return; Chris@17: Chris@20: long frame = getFrameForX(e->x()); Chris@17: if (frame < 0) frame = 0; Chris@17: m_editingPoint = SparseOneDimensionalModel::Point(frame, tr("New Point")); Chris@17: m_model->addPoint(m_editingPoint); Chris@18: m_editing = true; Chris@17: } Chris@17: Chris@17: void Chris@17: TimeInstantLayer::drawDrag(QMouseEvent *e) Chris@17: { Chris@17: std::cerr << "TimeInstantLayer::drawDrag(" << e->x() << ")" << std::endl; Chris@17: Chris@18: if (!m_model || !m_editing) return; Chris@17: Chris@20: long frame = getFrameForX(e->x()); Chris@17: if (frame < 0) frame = 0; Chris@17: m_model->deletePoint(m_editingPoint); Chris@17: m_editingPoint.frame = frame; Chris@17: m_model->addPoint(m_editingPoint); Chris@17: } Chris@17: Chris@17: void Chris@17: TimeInstantLayer::drawEnd(QMouseEvent *e) Chris@17: { Chris@17: std::cerr << "TimeInstantLayer::drawEnd(" << e->x() << ")" << std::endl; Chris@18: if (!m_model || !m_editing) return; Chris@18: m_editing = false; Chris@18: } Chris@18: Chris@18: void Chris@18: TimeInstantLayer::editStart(QMouseEvent *e) Chris@18: { Chris@18: std::cerr << "TimeInstantLayer::editStart(" << e->x() << ")" << std::endl; Chris@18: Chris@17: if (!m_model) return; Chris@18: Chris@18: SparseOneDimensionalModel::PointList points = getLocalPoints(e->x()); Chris@18: if (points.empty()) return; Chris@18: Chris@18: m_editingPoint = *points.begin(); Chris@18: m_editing = true; Chris@18: } Chris@18: Chris@18: void Chris@18: TimeInstantLayer::editDrag(QMouseEvent *e) Chris@18: { Chris@18: std::cerr << "TimeInstantLayer::editDrag(" << e->x() << ")" << std::endl; Chris@18: Chris@18: if (!m_model || !m_editing) return; Chris@18: Chris@20: long frame = getFrameForX(e->x()); Chris@18: if (frame < 0) frame = 0; Chris@18: m_model->deletePoint(m_editingPoint); Chris@18: m_editingPoint.frame = frame; Chris@18: m_model->addPoint(m_editingPoint); Chris@18: } Chris@18: Chris@18: void Chris@18: TimeInstantLayer::editEnd(QMouseEvent *e) Chris@18: { Chris@18: std::cerr << "TimeInstantLayer::editEnd(" << e->x() << ")" << std::endl; Chris@18: if (!m_model || !m_editing) return; Chris@18: m_editing = false; Chris@17: } Chris@17: Chris@6: QString Chris@6: TimeInstantLayer::toXmlString(QString indent, QString extraAttributes) const Chris@6: { Chris@6: return Layer::toXmlString(indent, extraAttributes + Chris@6: QString(" colour=\"%1\"").arg(encodeColour(m_colour))); Chris@6: } Chris@0: Chris@11: void Chris@11: TimeInstantLayer::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: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "TimeInstantLayer.moc.cpp" Chris@0: #endif Chris@0: