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@0: Chris Cannam, Queen Mary University of London, 2005 Chris@0: Chris@0: This is experimental software. Not for distribution. Chris@0: */ Chris@0: Chris@0: #include "TimeRulerLayer.h" Chris@0: Chris@0: #include "base/Model.h" Chris@0: #include "base/RealTime.h" Chris@0: #include "base/View.h" Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: using std::cerr; Chris@0: using std::endl; Chris@0: Chris@0: TimeRulerLayer::TimeRulerLayer(View *w) : Chris@0: Layer(w), Chris@0: m_model(0), Chris@0: m_colour(Qt::black), Chris@0: m_labelHeight(LabelTop) Chris@0: { Chris@0: m_view->addLayer(this); 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@0: list.push_back(tr("Colour")); Chris@0: return list; Chris@0: } Chris@0: Chris@0: Layer::PropertyType Chris@0: TimeRulerLayer::getPropertyType(const PropertyName &name) const Chris@0: { Chris@0: return ValueProperty; Chris@0: } Chris@0: Chris@0: int Chris@0: TimeRulerLayer::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@0: *min = 0; Chris@0: *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: TimeRulerLayer::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: TimeRulerLayer::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: TimeRulerLayer::paint(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@0: long startFrame = m_view->getStartFrame(); Chris@0: long endFrame = m_view->getEndFrame(); Chris@0: Chris@0: int zoomLevel = m_view->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: if (rectStart < startFrame) rectStart = startFrame; Chris@0: if (rectEnd > endFrame) rectEnd = endFrame; Chris@0: Chris@0: // std::cerr << "TimeRulerLayer::paint: calling paint.save()" << std::endl; Chris@0: paint.save(); Chris@0: //!!! paint.setClipRect(m_view->rect()); Chris@0: Chris@0: int minPixelSpacing = 50; Chris@0: Chris@0: RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate); Chris@0: RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate); Chris@0: // cerr << "startFrame " << startFrame << ", endFrame " << m_view->getEndFrame() << ", rtStart " << rtStart << ", rtEnd " << rtEnd << endl; Chris@0: int count = m_view->width() / minPixelSpacing; Chris@0: if (count < 1) count = 1; Chris@0: RealTime rtGap = (rtEnd - rtStart) / count; Chris@0: // cerr << "rtGap is " << rtGap << endl; Chris@0: Chris@0: int incms; Chris@0: bool quarter = false; Chris@0: Chris@0: if (rtGap.sec > 0) { Chris@0: incms = 1000; Chris@0: int s = rtGap.sec; Chris@0: if (s > 0) { incms *= 5; s /= 5; } Chris@0: if (s > 0) { incms *= 2; s /= 2; } Chris@0: if (s > 0) { incms *= 6; s /= 6; quarter = true; } Chris@0: if (s > 0) { incms *= 5; s /= 5; quarter = false; } Chris@0: if (s > 0) { incms *= 2; s /= 2; } Chris@0: if (s > 0) { incms *= 6; s /= 6; quarter = true; } Chris@0: while (s > 0) { Chris@0: incms *= 10; Chris@0: s /= 10; Chris@0: quarter = false; Chris@0: } Chris@0: } else { Chris@0: incms = 1; Chris@0: int ms = rtGap.msec(); Chris@0: if (ms > 0) { incms *= 10; ms /= 10; } Chris@0: if (ms > 0) { incms *= 10; ms /= 10; } Chris@0: if (ms > 0) { incms *= 5; ms /= 5; } Chris@0: if (ms > 0) { incms *= 2; ms /= 2; } Chris@0: } Chris@0: // cerr << "incms is " << incms << endl; 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@0: RealTime incRt = RealTime(incms / 1000, (incms % 1000) * 1000000); 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@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@0: rt = RealTime(ms / 1000, (ms % 1000) * 1000000); 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@0: paint.drawLine(x, 0, x, m_view->height()); Chris@0: Chris@0: paint.setPen(m_colour); Chris@0: paint.drawLine(x, 0, x, 5); Chris@0: paint.drawLine(x, m_view->height() - 6, x, m_view->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@0: y = m_view->height() / 2 - metrics.height() / 2 + metrics.ascent(); Chris@0: break; Chris@0: case LabelBottom: Chris@0: y = m_view->height() - metrics.height() + metrics.ascent() - 6; Chris@0: } Chris@0: Chris@0: int tw = metrics.width(text); Chris@0: Chris@0: paint.setPen(m_view->palette().background().color()); Chris@0: Chris@0: //!!! simple drawing function for this please Chris@0: //!!! and need getContrastingColour() in widget, or use the Chris@0: //palette properly -- get the base class able to draw text Chris@0: //using the proper colour (or this technique) automatically Chris@0: for (int dx = -1; dx <= 1; ++dx) { Chris@0: for (int dy = -1; dy <= 1; ++dy) { Chris@0: if ((dx && dy) || !(dx || dy)) continue; Chris@0: paint.drawText(x + 2 - tw / 2 + dx, y + dy, text); Chris@0: } Chris@0: } Chris@0: Chris@0: paint.setPen(m_colour); Chris@0: paint.drawText(x + 2 - tw / 2, y, text); 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@0: paint.drawLine(x, 0, x, m_view->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@0: paint.drawLine(x, m_view->height() - sz - 1, x, m_view->height() - 1); Chris@0: } Chris@0: } Chris@0: Chris@0: paint.restore(); Chris@0: } Chris@0: Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "TimeRulerLayer.moc.cpp" Chris@0: #endif Chris@0: