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@370: //#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@271: int tick = getMajorTickSpacing(v, q); Chris@271: RealTime rtick = RealTime::fromMilliseconds(tick); 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@918: TimeRulerLayer::getMajorTickSpacing(LayerGeometryProvider *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@908: sv_samplerate_t sampleRate = m_model->getSampleRate(); Chris@271: if (!sampleRate) return 1000; Chris@271: Chris@908: sv_frame_t startFrame = v->getStartFrame(); Chris@908: sv_frame_t 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@918: int count = v->getPaintWidth() / 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@1266: incms = 1000; Chris@1266: int s = rtGap.sec; Chris@1266: if (s > 0) { incms *= 5; s /= 5; } Chris@1266: if (s > 0) { incms *= 2; s /= 2; } Chris@1266: if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; } Chris@1266: if (s > 0) { incms *= 5; s /= 5; quarterTicks = false; } Chris@1266: if (s > 0) { incms *= 2; s /= 2; } Chris@1266: if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; } Chris@1266: while (s > 0) { Chris@1266: incms *= 10; Chris@1266: s /= 10; Chris@1266: quarterTicks = false; Chris@1266: } Chris@271: } else { Chris@1266: incms = 1; Chris@1266: int ms = rtGap.msec(); Chris@1021: // cerr << "rtGap.msec = " << ms << ", rtGap = " << rtGap << ", count = " << count << endl; Chris@1021: // cerr << "startFrame = " << startFrame << ", endFrame = " << endFrame << " rtStart = " << rtStart << ", rtEnd = " << rtEnd << endl; Chris@1266: if (ms > 0) { incms *= 10; ms /= 10; } Chris@1266: if (ms > 0) { incms *= 10; ms /= 10; } Chris@1266: if (ms > 0) { incms *= 5; ms /= 5; } Chris@1266: if (ms > 0) { incms *= 2; ms /= 2; } Chris@271: } Chris@271: Chris@271: return incms; 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@271: int incms = getMajorTickSpacing(v, quarter); Chris@0: Chris@908: int ms = int(lrint(1000.0 * (double(startFrame) / double(sampleRate)))); Chris@0: ms = (ms / incms) * incms - incms; Chris@0: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@682: cerr << "start ms = " << ms << " at step " << incms << 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@370: // We always use the exact incms in our calculations for where to Chris@370: // draw the actual ticks or lines. Chris@370: Chris@370: int minPixelSpacing = 50; Chris@908: sv_frame_t incFrame = lrint((incms * sampleRate) / 1000); Chris@908: int incX = int(incFrame / v->getZoomLevel()); 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@832: int minlabel = 1; // ms Chris@832: Chris@1021: // used for a sanity check Chris@1021: sv_frame_t prevframe = 0; 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@370: double dms = ms; Chris@908: sv_frame_t frame = lrint((dms * sampleRate) / 1000.0); Chris@370: frame /= v->getZoomLevel(); Chris@370: frame *= v->getZoomLevel(); // so frame corresponds to an exact pixel Chris@370: Chris@1021: if (frame == prevframe && prevframe != 0) { Chris@1021: cerr << "ERROR: frame == prevframe (== " << frame Chris@1021: << ") in TimeRulerLayer::paint" << endl; Chris@1021: throw std::logic_error("frame == prevframe in TimeRulerLayer::paint"); Chris@1021: } Chris@1021: prevframe = frame; Chris@1021: Chris@370: int x = v->getXForFrame(frame); Chris@0: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@1021: cerr << "Considering frame = " << frame << ", x = " << x << endl; Chris@370: #endif 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@1266: if (x >= rect.x() - 50 && ms >= minlabel) { Chris@0: Chris@375: RealTime rt = RealTime::fromMilliseconds(ms); Chris@375: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@682: cerr << "X in range, drawing line here for time " << rt.toText() << 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@380: dms = ms + (i * double(incms)) / ticks; Chris@370: frame = lrint((dms * sampleRate) / 1000.0); Chris@370: frame /= v->getZoomLevel(); Chris@370: frame *= v->getZoomLevel(); // exact pixel as above Chris@370: Chris@370: x = v->getXForFrame(frame); 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@1266: ms += incms; 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: