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 "TimeRulerLayer.h" Chris@0: Chris@128: #include "data/model/Model.h" Chris@0: #include "base/RealTime.h" Chris@128: #include "view/View.h" Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@271: #include Chris@0: Chris@0: using std::cerr; Chris@0: using std::endl; Chris@0: Chris@44: TimeRulerLayer::TimeRulerLayer() : Chris@44: Layer(), Chris@0: m_model(0), Chris@0: m_colour(Qt::black), Chris@0: m_labelHeight(LabelTop) Chris@0: { Chris@44: Chris@0: } Chris@0: Chris@0: void Chris@0: TimeRulerLayer::setModel(Model *model) Chris@0: { Chris@0: if (m_model == model) return; Chris@0: m_model = model; Chris@0: emit modelReplaced(); Chris@0: } Chris@0: Chris@0: void Chris@0: TimeRulerLayer::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: Layer::PropertyList Chris@0: TimeRulerLayer::getProperties() const Chris@0: { Chris@0: PropertyList list; Chris@87: list.push_back("Colour"); Chris@0: return list; Chris@0: } Chris@0: Chris@87: QString Chris@87: TimeRulerLayer::getPropertyLabel(const PropertyName &name) const Chris@87: { Chris@87: if (name == "Colour") return tr("Colour"); Chris@87: return ""; Chris@87: } Chris@87: Chris@0: Layer::PropertyType Chris@248: TimeRulerLayer::getPropertyType(const PropertyName &) const Chris@0: { Chris@0: return ValueProperty; Chris@0: } Chris@0: Chris@0: int Chris@0: TimeRulerLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: int *min, int *max, int *deflt) const Chris@0: { Chris@216: int val = 0; Chris@0: Chris@87: if (name == "Colour") { Chris@0: Chris@10: if (min) *min = 0; Chris@10: if (max) *max = 5; Chris@216: if (deflt) *deflt = 0; Chris@0: Chris@216: if (m_colour == Qt::black) val = 0; Chris@216: else if (m_colour == Qt::darkRed) val = 1; Chris@216: else if (m_colour == Qt::darkBlue) val = 2; Chris@216: else if (m_colour == Qt::darkGreen) val = 3; Chris@216: else if (m_colour == QColor(200, 50, 255)) val = 4; Chris@216: else if (m_colour == QColor(255, 150, 50)) val = 5; Chris@0: Chris@0: } else { Chris@0: Chris@216: val = Layer::getPropertyRangeAndValue(name, min, max, deflt); Chris@0: } Chris@0: Chris@216: return val; Chris@0: } Chris@0: Chris@0: QString Chris@0: TimeRulerLayer::getPropertyValueLabel(const PropertyName &name, Chris@0: int value) const Chris@0: { Chris@87: if (name == "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: TimeRulerLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@87: if (name == "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@271: bool Chris@271: TimeRulerLayer::snapToFeatureFrame(View *v, int &frame, Chris@271: size_t &resolution, SnapType snap) const Chris@271: { Chris@271: if (!m_model) { Chris@271: resolution = 1; Chris@271: return false; Chris@271: } Chris@271: Chris@271: bool q; Chris@271: int tick = getMajorTickSpacing(v, q); Chris@271: RealTime rtick = RealTime::fromMilliseconds(tick); Chris@271: int rate = m_model->getSampleRate(); Chris@271: Chris@271: RealTime rt = RealTime::frame2RealTime(frame, rate); Chris@271: double ratio = rt / rtick; Chris@271: Chris@272: int rounded = int(ratio); Chris@271: RealTime rdrt = rtick * rounded; Chris@271: Chris@271: int left = RealTime::realTime2Frame(rdrt, rate); Chris@271: resolution = RealTime::realTime2Frame(rtick, rate); Chris@271: int right = left + resolution; Chris@271: Chris@272: // std::cerr << "TimeRulerLayer::snapToFeatureFrame: type " Chris@272: // << int(snap) << ", frame " << frame << " (time " Chris@272: // << rt << ", tick " << rtick << ", rounded " << rdrt << ") "; Chris@272: Chris@271: switch (snap) { Chris@271: Chris@271: case SnapLeft: Chris@271: frame = left; Chris@271: break; Chris@271: Chris@271: case SnapRight: Chris@271: frame = right; Chris@271: break; Chris@271: Chris@271: case SnapNearest: Chris@271: { Chris@271: if (abs(frame - left) > abs(right - frame)) { Chris@271: frame = right; Chris@271: } else { Chris@271: frame = left; Chris@271: } Chris@271: break; Chris@271: } Chris@271: Chris@271: case SnapNeighbouring: Chris@271: { Chris@271: int dl = -1, dr = -1; Chris@271: int x = v->getXForFrame(frame); Chris@271: Chris@271: if (left > v->getStartFrame() && Chris@271: left < v->getEndFrame()) { Chris@271: dl = abs(v->getXForFrame(left) - x); Chris@271: } Chris@271: Chris@271: if (right > v->getStartFrame() && Chris@271: right < v->getEndFrame()) { Chris@271: dr = abs(v->getXForFrame(right) - x); Chris@271: } Chris@271: Chris@271: int fuzz = 2; Chris@271: Chris@271: if (dl >= 0 && dr >= 0) { Chris@271: if (dl < dr) { Chris@271: if (dl <= fuzz) { Chris@271: frame = left; Chris@271: } Chris@271: } else { Chris@271: if (dr < fuzz) { Chris@271: frame = right; Chris@271: } Chris@271: } Chris@271: } else if (dl >= 0) { Chris@271: if (dl <= fuzz) { Chris@271: frame = left; Chris@271: } Chris@271: } else if (dr >= 0) { Chris@271: if (dr <= fuzz) { Chris@271: frame = right; Chris@271: } Chris@271: } Chris@271: } Chris@271: } Chris@271: Chris@272: // std::cerr << " -> " << frame << " (resolution = " << resolution << ")" << std::endl; Chris@272: Chris@271: return true; Chris@271: } Chris@271: Chris@271: int Chris@271: TimeRulerLayer::getMajorTickSpacing(View *v, bool &quarterTicks) const Chris@271: { Chris@271: // return value is in milliseconds Chris@271: Chris@271: if (!m_model || !v) return 1000; Chris@271: Chris@271: int sampleRate = m_model->getSampleRate(); Chris@271: if (!sampleRate) return 1000; Chris@271: Chris@271: long startFrame = v->getStartFrame(); Chris@271: long endFrame = v->getEndFrame(); Chris@271: Chris@271: int minPixelSpacing = 50; Chris@271: Chris@271: RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate); Chris@271: RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate); Chris@271: Chris@271: int count = v->width() / minPixelSpacing; Chris@271: if (count < 1) count = 1; Chris@271: RealTime rtGap = (rtEnd - rtStart) / count; Chris@271: Chris@271: int incms; Chris@271: quarterTicks = false; Chris@271: Chris@271: if (rtGap.sec > 0) { Chris@271: incms = 1000; Chris@271: int s = rtGap.sec; Chris@271: if (s > 0) { incms *= 5; s /= 5; } Chris@271: if (s > 0) { incms *= 2; s /= 2; } Chris@271: if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; } Chris@271: if (s > 0) { incms *= 5; s /= 5; quarterTicks = false; } Chris@271: if (s > 0) { incms *= 2; s /= 2; } Chris@271: if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; } Chris@271: while (s > 0) { Chris@271: incms *= 10; Chris@271: s /= 10; Chris@271: quarterTicks = false; Chris@271: } Chris@271: } else { Chris@271: incms = 1; Chris@271: int ms = rtGap.msec(); Chris@271: if (ms > 0) { incms *= 10; ms /= 10; } Chris@271: if (ms > 0) { incms *= 10; ms /= 10; } Chris@271: if (ms > 0) { incms *= 5; ms /= 5; } Chris@271: if (ms > 0) { incms *= 2; ms /= 2; } Chris@271: } Chris@271: Chris@271: return incms; Chris@271: } Chris@271: Chris@0: void Chris@44: TimeRulerLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@0: // std::cerr << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y() Chris@0: // << ") [" << rect.width() << "x" << rect.height() << "]" << std::endl; 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@44: long startFrame = v->getStartFrame(); Chris@44: long endFrame = v->getEndFrame(); Chris@0: Chris@44: int zoomLevel = v->getZoomLevel(); Chris@0: Chris@0: long rectStart = startFrame + (rect.x() - 100) * zoomLevel; Chris@0: long rectEnd = startFrame + (rect.x() + rect.width() + 100) * zoomLevel; Chris@0: Chris@0: // std::cerr << "TimeRulerLayer::paint: calling paint.save()" << std::endl; Chris@0: paint.save(); Chris@44: //!!! paint.setClipRect(v->rect()); Chris@0: Chris@0: int minPixelSpacing = 50; Chris@0: Chris@0: bool quarter = false; Chris@271: int incms = getMajorTickSpacing(v, quarter); Chris@0: Chris@0: RealTime rt = RealTime::frame2RealTime(rectStart, sampleRate); Chris@0: long ms = rt.sec * 1000 + rt.msec(); Chris@0: ms = (ms / incms) * incms - incms; Chris@0: Chris@30: RealTime incRt = RealTime::fromMilliseconds(incms); Chris@0: long incFrame = RealTime::realTime2Frame(incRt, sampleRate); Chris@0: int incX = incFrame / zoomLevel; Chris@0: int ticks = 10; Chris@0: if (incX < minPixelSpacing * 2) { Chris@0: ticks = quarter ? 4 : 5; Chris@0: } Chris@0: Chris@0: QRect oldClipRect = rect; Chris@0: QRect newClipRect(oldClipRect.x() - 25, oldClipRect.y(), Chris@0: oldClipRect.width() + 50, oldClipRect.height()); Chris@0: paint.setClipRect(newClipRect); Chris@55: paint.setClipRect(rect); Chris@0: Chris@0: QColor greyColour(m_colour); Chris@0: if (m_colour == Qt::black) { Chris@0: greyColour = QColor(200,200,200); Chris@0: } else { Chris@0: greyColour = m_colour.light(150); Chris@0: } Chris@0: Chris@0: while (1) { Chris@0: Chris@30: rt = RealTime::fromMilliseconds(ms); Chris@0: ms += incms; Chris@0: Chris@0: long frame = RealTime::realTime2Frame(rt, sampleRate); Chris@0: if (frame >= rectEnd) break; Chris@0: Chris@0: int x = (frame - startFrame) / zoomLevel; Chris@0: if (x < rect.x() || x >= rect.x() + rect.width()) continue; Chris@0: Chris@0: paint.setPen(greyColour); Chris@44: paint.drawLine(x, 0, x, v->height()); Chris@0: Chris@0: paint.setPen(m_colour); Chris@0: paint.drawLine(x, 0, x, 5); Chris@44: paint.drawLine(x, v->height() - 6, x, v->height() - 1); Chris@0: Chris@0: QString text(QString::fromStdString(rt.toText())); Chris@0: Chris@0: int y; Chris@0: QFontMetrics metrics = paint.fontMetrics(); Chris@0: switch (m_labelHeight) { Chris@0: default: Chris@0: case LabelTop: Chris@0: y = 6 + metrics.ascent(); Chris@0: break; Chris@0: case LabelMiddle: Chris@44: y = v->height() / 2 - metrics.height() / 2 + metrics.ascent(); Chris@0: break; Chris@0: case LabelBottom: Chris@44: y = v->height() - metrics.height() + metrics.ascent() - 6; Chris@0: } Chris@0: Chris@0: int tw = metrics.width(text); Chris@0: Chris@70: if (v->getViewManager() && v->getViewManager()->getOverlayMode() != Chris@70: ViewManager::NoOverlays) { Chris@70: Chris@70: if (v->getLayer(0) == this) { Chris@70: // backmost layer, don't worry about outlining the text Chris@70: paint.drawText(x+2 - tw/2, y, text); Chris@70: } else { Chris@70: v->drawVisibleText(paint, x+2 - tw/2, y, text, View::OutlinedText); Chris@70: } Chris@70: } Chris@0: Chris@0: paint.setPen(greyColour); Chris@0: Chris@0: for (int i = 1; i < ticks; ++i) { Chris@0: rt = rt + (incRt / ticks); Chris@0: frame = RealTime::realTime2Frame(rt, sampleRate); Chris@0: x = (frame - startFrame) / zoomLevel; Chris@0: int sz = 5; Chris@0: if (ticks == 10) { Chris@0: if ((i % 2) == 1) { Chris@0: if (i == 5) { Chris@44: paint.drawLine(x, 0, x, v->height()); Chris@0: } else sz = 3; Chris@0: } else { Chris@0: sz = 7; Chris@0: } Chris@0: } Chris@0: paint.drawLine(x, 0, x, sz); Chris@44: paint.drawLine(x, v->height() - sz - 1, x, v->height() - 1); Chris@0: } Chris@0: } Chris@0: Chris@0: paint.restore(); Chris@0: } Chris@0: Chris@6: QString Chris@6: TimeRulerLayer::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@6: Chris@11: void Chris@11: TimeRulerLayer::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: