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@376: #include "ColourDatabase.h" Chris@128: #include "view/View.h" Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@271: #include Chris@0: Chris@370: //#define DEBUG_TIME_RULER_LAYER 1 Chris@370: Chris@0: using std::cerr; Chris@0: using std::endl; Chris@0: 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@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@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@370: std::cerr << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y() Chris@370: << ") [" << rect.width() << "x" << rect.height() << "]" << std::endl; Chris@370: #endif 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@370: long startFrame = v->getFrameForX(rect.x() - 50); Chris@0: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@370: std::cerr << "start frame = " << startFrame << std::endl; Chris@370: #endif Chris@0: Chris@0: bool quarter = false; Chris@271: int incms = getMajorTickSpacing(v, quarter); Chris@0: Chris@370: int ms = lrint(1000.0 * (double(startFrame) / double(sampleRate))); Chris@0: ms = (ms / incms) * incms - incms; Chris@0: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@370: std::cerr << "start ms = " << ms << " at step " << incms << std::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@370: long incFrame = (incms * sampleRate) / 1000; Chris@370: int incX = incFrame / v->getZoomLevel(); Chris@0: int ticks = 10; Chris@0: if (incX < minPixelSpacing * 2) { Chris@0: ticks = quarter ? 4 : 5; Chris@0: } Chris@0: Chris@370: QColor greyColour = getPartialShades(v)[1]; Chris@0: Chris@370: paint.save(); Chris@0: 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@370: long 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@370: int x = v->getXForFrame(frame); Chris@0: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@370: std::cerr << "Considering frame = " << frame << ", x = " << x << std::endl; Chris@370: #endif Chris@0: Chris@370: if (x >= rect.x() + rect.width() + 50) { Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@370: std::cerr << "X well out of range, ending here" << std::endl; Chris@370: #endif Chris@370: break; Chris@370: } Chris@0: Chris@370: if (x >= rect.x() - 50) { Chris@0: Chris@375: RealTime rt = RealTime::fromMilliseconds(ms); Chris@375: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@375: std::cerr << "X in range, drawing line here for time " << rt.toText() << std::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@370: std::cerr << "hm, maybe X isn't in range after all (x = " << x << ", tw = " << tw << ", rect.x() = " << rect.x() << ", rect.width() = " << rect.width() << ")" << std::endl; Chris@370: #endif Chris@370: } Chris@70: Chris@370: paint.setPen(greyColour); Chris@370: paint.drawLine(x, 0, x, v->height()); Chris@370: Chris@370: paint.setPen(getBaseQColor()); Chris@370: paint.drawLine(x, 0, x, 5); Chris@370: paint.drawLine(x, v->height() - 6, x, v->height() - 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@370: y = v->height() / 2 - metrics.height() / 2 + metrics.ascent(); Chris@370: break; Chris@370: case LabelBottom: Chris@370: y = v->height() - metrics.height() + metrics.ascent() - 6; Chris@370: } Chris@370: Chris@370: if (v->getViewManager() && v->getViewManager()->getOverlayMode() != Chris@370: ViewManager::NoOverlays) { Chris@370: Chris@370: if (v->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@370: v->drawVisibleText(paint, x+2 - tw/2, y, text, View::OutlinedText); Chris@370: } Chris@70: } Chris@70: } Chris@0: Chris@0: paint.setPen(greyColour); Chris@0: Chris@0: for (int i = 1; i < ticks; ++i) { Chris@370: Chris@370: dms = ms - incms + (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@370: // std::cerr << "tick " << i << ": X out of range, going on to next tick" << std::endl; Chris@370: #endif Chris@370: continue; Chris@370: } Chris@370: Chris@370: #ifdef DEBUG_TIME_RULER_LAYER Chris@370: std::cerr << "tick " << i << " in range, drawing at " << x << std::endl; Chris@370: #endif Chris@370: 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@375: Chris@375: 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: