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@335: #include "LayerFactory.h" Chris@335: Chris@128: #include "data/model/Model.h" Chris@0: #include "base/RealTime.h" Chris@1078: #include "view/View.h" Chris@1078: Chris@376: #include "ColourDatabase.h" Chris@1078: #include "PaintAssistant.h" Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@271: #include Chris@1021: #include Chris@0: Chris@1342: #define DEBUG_TIME_RULER_LAYER 1 Chris@370: Chris@682: Chris@44: TimeRulerLayer::TimeRulerLayer() : Chris@287: SingleColourLayer(), Chris@0: m_model(0), 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@271: bool Chris@918: TimeRulerLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, Chris@805: int &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@1341: int tickUSec = getMajorTickUSec(v, q); Chris@1341: RealTime rtick = RealTime(0, tickUSec * 1000); Chris@908: sv_samplerate_t 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@908: sv_frame_t left = RealTime::realTime2Frame(rdrt, rate); Chris@908: resolution = int(RealTime::realTime2Frame(rtick, rate)); Chris@908: sv_frame_t right = left + resolution; Chris@271: Chris@587: // SVDEBUG << "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@989: if (llabs(frame - left) > llabs(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@587: // SVDEBUG << " -> " << frame << " (resolution = " << resolution << ")" << endl; Chris@272: Chris@271: return true; Chris@271: } Chris@271: Chris@271: int Chris@1341: TimeRulerLayer::getMajorTickUSec(LayerGeometryProvider *v, Chris@1341: bool &quarterTicks) const Chris@271: { Chris@1341: // return value is in microseconds Chris@1341: if (!m_model || !v) return 1000 * 1000; Chris@271: Chris@908: sv_samplerate_t sampleRate = m_model->getSampleRate(); Chris@1341: if (!sampleRate) return 1000 * 1000; Chris@271: Chris@908: sv_frame_t startFrame = v->getStartFrame(); Chris@908: sv_frame_t endFrame = v->getEndFrame(); Chris@271: Chris@1341: int minPixelSpacing = ViewManager::scalePixelSize(50); Chris@271: Chris@271: RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate); Chris@271: RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate); Chris@271: Chris@918: int count = v->getPaintWidth() / minPixelSpacing; Chris@271: if (count < 1) count = 1; Chris@271: RealTime rtGap = (rtEnd - rtStart) / count; Chris@271: Chris@1341: int incus; Chris@271: quarterTicks = false; Chris@271: Chris@271: if (rtGap.sec > 0) { Chris@1341: incus = 1000 * 1000; Chris@1266: int s = rtGap.sec; Chris@1341: if (s > 0) { incus *= 5; s /= 5; } Chris@1341: if (s > 0) { incus *= 2; s /= 2; } Chris@1341: if (s > 0) { incus *= 6; s /= 6; quarterTicks = true; } Chris@1341: if (s > 0) { incus *= 5; s /= 5; quarterTicks = false; } Chris@1341: if (s > 0) { incus *= 2; s /= 2; } Chris@1341: if (s > 0) { incus *= 6; s /= 6; quarterTicks = true; } Chris@1266: while (s > 0) { Chris@1341: incus *= 10; Chris@1266: s /= 10; Chris@1266: quarterTicks = false; Chris@1266: } Chris@1341: } else if (rtGap.msec() > 0) { Chris@1341: incus = 1000; Chris@1341: int ms = rtGap.msec(); Chris@1341: if (ms > 0) { incus *= 10; ms /= 10; } Chris@1341: if (ms > 0) { incus *= 10; ms /= 10; } Chris@1341: if (ms > 0) { incus *= 5; ms /= 5; } Chris@1341: if (ms > 0) { incus *= 2; ms /= 2; } Chris@271: } else { Chris@1341: incus = 1; Chris@1341: int us = rtGap.usec(); Chris@1341: if (us > 0) { incus *= 10; us /= 10; } Chris@1341: if (us > 0) { incus *= 10; us /= 10; } Chris@1341: if (us > 0) { incus *= 5; us /= 5; } Chris@1341: if (us > 0) { incus *= 2; us /= 2; } Chris@271: } Chris@271: Chris@1341: return incus; Chris@1341: } Chris@1341: Chris@1341: int Chris@1341: TimeRulerLayer::getXForUSec(LayerGeometryProvider *v, double us) const Chris@1341: { Chris@1341: sv_samplerate_t sampleRate = m_model->getSampleRate(); Chris@1341: double dframe = (us * sampleRate) / 1000000.0; Chris@1341: double eps = 1e-7; Chris@1341: sv_frame_t frame = sv_frame_t(floor(dframe + eps)); Chris@1341: int x; Chris@1341: Chris@1341: ZoomLevel zoom = v->getZoomLevel(); Chris@1341: Chris@1341: if (zoom.zone == ZoomLevel::FramesPerPixel) { Chris@1341: Chris@1341: frame /= zoom.level; Chris@1341: frame *= zoom.level; // so frame corresponds to an exact pixel Chris@1341: Chris@1341: x = v->getXForFrame(frame); Chris@1341: Chris@1341: } else { Chris@1341: Chris@1341: double off = dframe - double(frame); Chris@1341: int x0 = v->getXForFrame(frame); Chris@1341: int x1 = v->getXForFrame(frame + 1); Chris@1341: Chris@1341: x = int(x0 + off * (x1 - x0)); Chris@1341: } Chris@1341: Chris@1341: #ifdef DEBUG_TIME_RULER_LAYER Chris@1341: cerr << "Considering frame = " << frame << ", x = " << x << endl; Chris@1341: #endif Chris@1341: Chris@1341: return x; Chris@271: } Chris@271: Chris@0: void Chris@916: TimeRulerLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@0: { Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@587: SVDEBUG << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y() Chris@1266: << ") [" << rect.width() << "x" << rect.height() << "]" << endl; Chris@370: #endif Chris@0: Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@908: sv_samplerate_t sampleRate = m_model->getSampleRate(); Chris@0: if (!sampleRate) return; Chris@0: Chris@908: sv_frame_t startFrame = v->getFrameForX(rect.x() - 50); Chris@0: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@682: cerr << "start frame = " << startFrame << endl; Chris@370: #endif Chris@0: Chris@0: bool quarter = false; Chris@1341: int incus = getMajorTickUSec(v, quarter); Chris@0: Chris@1341: int us = int(lrint(1000.0 * 1000.0 * (double(startFrame) / Chris@1341: double(sampleRate)))); Chris@1341: us = (us / incus) * incus - incus; Chris@0: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@1341: cerr << "start us = " << us << " at step " << incus << endl; Chris@370: #endif Chris@370: Chris@370: // Calculate the number of ticks per increment -- approximate Chris@370: // values for x and frame counts here will do, no rounding issue. Chris@1341: // We always use the exact incus in our calculations for where to Chris@370: // draw the actual ticks or lines. Chris@370: Chris@370: int minPixelSpacing = 50; Chris@1341: sv_frame_t incFrame = lrint((incus * sampleRate) / 1000000); Chris@1326: int incX = int(round(v->getZoomLevel().framesToPixels(double(incFrame)))); Chris@0: int ticks = 10; Chris@0: if (incX < minPixelSpacing * 2) { Chris@1266: ticks = quarter ? 4 : 5; Chris@0: } Chris@0: Chris@370: QColor greyColour = getPartialShades(v)[1]; Chris@0: Chris@370: paint.save(); Chris@0: Chris@832: // Do not label time zero - we now overlay an opaque area over Chris@832: // time < 0 which would cut it in half Chris@1341: int minlabel = 1; // us Chris@1021: Chris@0: while (1) { Chris@0: Chris@370: // frame is used to determine where to draw the lines, so it Chris@370: // needs to correspond to an exact pixel (so that we don't get Chris@370: // a different pixel when scrolling a small amount and Chris@370: // re-drawing with a different start frame). Chris@370: Chris@1341: double dus = us; Chris@370: Chris@1341: int x = getXForUSec(v, dus); Chris@0: Chris@370: if (x >= rect.x() + rect.width() + 50) { Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@682: cerr << "X well out of range, ending here" << endl; Chris@370: #endif Chris@370: break; Chris@370: } Chris@0: Chris@1341: if (x >= rect.x() - 50 && us >= minlabel) { Chris@0: Chris@1342: RealTime rt = RealTime::fromMicroseconds(us); Chris@375: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@1342: cerr << "X in range, drawing line here for time " << rt.toText() << " (usec = " << us << ")" << endl; Chris@370: #endif Chris@0: Chris@370: QString text(QString::fromStdString(rt.toText())); Chris@370: QFontMetrics metrics = paint.fontMetrics(); Chris@370: int tw = metrics.width(text); Chris@0: Chris@370: if (tw < 50 && Chris@370: (x < rect.x() - tw/2 || Chris@370: x >= rect.x() + rect.width() + tw/2)) { Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@682: cerr << "hm, maybe X isn't in range after all (x = " << x << ", tw = " << tw << ", rect.x() = " << rect.x() << ", rect.width() = " << rect.width() << ")" << endl; Chris@370: #endif Chris@370: } Chris@70: Chris@370: paint.setPen(greyColour); Chris@918: paint.drawLine(x, 0, x, v->getPaintHeight()); Chris@370: Chris@370: paint.setPen(getBaseQColor()); Chris@370: paint.drawLine(x, 0, x, 5); Chris@918: paint.drawLine(x, v->getPaintHeight() - 6, x, v->getPaintHeight() - 1); Chris@370: Chris@370: int y; Chris@370: switch (m_labelHeight) { Chris@370: default: Chris@370: case LabelTop: Chris@370: y = 6 + metrics.ascent(); Chris@370: break; Chris@370: case LabelMiddle: Chris@918: y = v->getPaintHeight() / 2 - metrics.height() / 2 + metrics.ascent(); Chris@370: break; Chris@370: case LabelBottom: Chris@918: y = v->getPaintHeight() - metrics.height() + metrics.ascent() - 6; Chris@370: } Chris@370: Chris@370: if (v->getViewManager() && v->getViewManager()->getOverlayMode() != Chris@370: ViewManager::NoOverlays) { Chris@370: Chris@919: if (v->getView()->getLayer(0) == this) { Chris@370: // backmost layer, don't worry about outlining the text Chris@370: paint.drawText(x+2 - tw/2, y, text); Chris@370: } else { Chris@1078: PaintAssistant::drawVisibleText(v, paint, x+2 - tw/2, y, text, PaintAssistant::OutlinedText); Chris@370: } Chris@70: } Chris@70: } Chris@0: Chris@1266: paint.setPen(greyColour); Chris@0: Chris@1266: for (int i = 1; i < ticks; ++i) { Chris@370: Chris@1341: dus = us + (i * double(incus)) / ticks; Chris@370: Chris@1341: x = getXForUSec(v, dus); Chris@370: Chris@370: if (x < rect.x() || x >= rect.x() + rect.width()) { Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@682: // cerr << "tick " << i << ": X out of range, going on to next tick" << endl; Chris@370: #endif Chris@370: continue; Chris@370: } Chris@370: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@682: cerr << "tick " << i << " in range, drawing at " << x << endl; Chris@370: #endif Chris@370: Chris@1266: int sz = 5; Chris@1266: if (ticks == 10) { Chris@1266: if ((i % 2) == 1) { Chris@1266: if (i == 5) { Chris@1266: paint.drawLine(x, 0, x, v->getPaintHeight()); Chris@1266: } else sz = 3; Chris@1266: } else { Chris@1266: sz = 7; Chris@1266: } Chris@1266: } Chris@1266: paint.drawLine(x, 0, x, sz); Chris@1266: paint.drawLine(x, v->getPaintHeight() - sz - 1, x, v->getPaintHeight() - 1); Chris@1266: } Chris@375: Chris@1341: us += incus; Chris@0: } Chris@0: Chris@0: paint.restore(); Chris@0: } Chris@287: Chris@287: int Chris@287: TimeRulerLayer::getDefaultColourHint(bool darkbg, bool &impose) Chris@287: { Chris@287: impose = true; Chris@287: return ColourDatabase::getInstance()->getColourIndex Chris@287: (QString(darkbg ? "White" : "Black")); Chris@287: } Chris@287: Chris@335: QString TimeRulerLayer::getLayerPresentationName() const Chris@335: { Chris@335: LayerFactory *factory = LayerFactory::getInstance(); Chris@335: QString layerName = factory->getLayerPresentationName Chris@335: (factory->getLayerType(this)); Chris@335: return layerName; Chris@335: } Chris@335: Chris@316: void Chris@316: TimeRulerLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@6: { Chris@316: SingleColourLayer::toXml(stream, indent, extraAttributes); Chris@6: } Chris@6: Chris@11: void Chris@11: TimeRulerLayer::setProperties(const QXmlAttributes &attributes) Chris@11: { Chris@287: SingleColourLayer::setProperties(attributes); Chris@11: } Chris@11: