annotate layer/TimeRulerLayer.cpp @ 374:64e84e5efb76 spectrogram-cache-rejig

* Merge from trunk
author Chris Cannam
date Wed, 27 Feb 2008 11:59:42 +0000
parents c0b9eec70639
children
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@59 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "TimeRulerLayer.h"
Chris@0 17
Chris@374 18 #include "LayerFactory.h"
Chris@374 19
Chris@128 20 #include "data/model/Model.h"
Chris@0 21 #include "base/RealTime.h"
Chris@287 22 #include "base/ColourDatabase.h"
Chris@128 23 #include "view/View.h"
Chris@0 24
Chris@0 25 #include <QPainter>
Chris@0 26
Chris@0 27 #include <iostream>
Chris@271 28 #include <cmath>
Chris@0 29
Chris@374 30 //#define DEBUG_TIME_RULER_LAYER 1
Chris@374 31
Chris@0 32 using std::cerr;
Chris@0 33 using std::endl;
Chris@0 34
Chris@44 35 TimeRulerLayer::TimeRulerLayer() :
Chris@287 36 SingleColourLayer(),
Chris@0 37 m_model(0),
Chris@0 38 m_labelHeight(LabelTop)
Chris@0 39 {
Chris@44 40
Chris@0 41 }
Chris@0 42
Chris@0 43 void
Chris@0 44 TimeRulerLayer::setModel(Model *model)
Chris@0 45 {
Chris@0 46 if (m_model == model) return;
Chris@0 47 m_model = model;
Chris@0 48 emit modelReplaced();
Chris@0 49 }
Chris@0 50
Chris@271 51 bool
Chris@271 52 TimeRulerLayer::snapToFeatureFrame(View *v, int &frame,
Chris@271 53 size_t &resolution, SnapType snap) const
Chris@271 54 {
Chris@271 55 if (!m_model) {
Chris@271 56 resolution = 1;
Chris@271 57 return false;
Chris@271 58 }
Chris@271 59
Chris@271 60 bool q;
Chris@271 61 int tick = getMajorTickSpacing(v, q);
Chris@271 62 RealTime rtick = RealTime::fromMilliseconds(tick);
Chris@271 63 int rate = m_model->getSampleRate();
Chris@271 64
Chris@271 65 RealTime rt = RealTime::frame2RealTime(frame, rate);
Chris@271 66 double ratio = rt / rtick;
Chris@271 67
Chris@272 68 int rounded = int(ratio);
Chris@271 69 RealTime rdrt = rtick * rounded;
Chris@271 70
Chris@271 71 int left = RealTime::realTime2Frame(rdrt, rate);
Chris@271 72 resolution = RealTime::realTime2Frame(rtick, rate);
Chris@271 73 int right = left + resolution;
Chris@271 74
Chris@272 75 // std::cerr << "TimeRulerLayer::snapToFeatureFrame: type "
Chris@272 76 // << int(snap) << ", frame " << frame << " (time "
Chris@272 77 // << rt << ", tick " << rtick << ", rounded " << rdrt << ") ";
Chris@272 78
Chris@271 79 switch (snap) {
Chris@271 80
Chris@271 81 case SnapLeft:
Chris@271 82 frame = left;
Chris@271 83 break;
Chris@271 84
Chris@271 85 case SnapRight:
Chris@271 86 frame = right;
Chris@271 87 break;
Chris@271 88
Chris@271 89 case SnapNearest:
Chris@271 90 {
Chris@271 91 if (abs(frame - left) > abs(right - frame)) {
Chris@271 92 frame = right;
Chris@271 93 } else {
Chris@271 94 frame = left;
Chris@271 95 }
Chris@271 96 break;
Chris@271 97 }
Chris@271 98
Chris@271 99 case SnapNeighbouring:
Chris@271 100 {
Chris@271 101 int dl = -1, dr = -1;
Chris@271 102 int x = v->getXForFrame(frame);
Chris@271 103
Chris@271 104 if (left > v->getStartFrame() &&
Chris@271 105 left < v->getEndFrame()) {
Chris@271 106 dl = abs(v->getXForFrame(left) - x);
Chris@271 107 }
Chris@271 108
Chris@271 109 if (right > v->getStartFrame() &&
Chris@271 110 right < v->getEndFrame()) {
Chris@271 111 dr = abs(v->getXForFrame(right) - x);
Chris@271 112 }
Chris@271 113
Chris@271 114 int fuzz = 2;
Chris@271 115
Chris@271 116 if (dl >= 0 && dr >= 0) {
Chris@271 117 if (dl < dr) {
Chris@271 118 if (dl <= fuzz) {
Chris@271 119 frame = left;
Chris@271 120 }
Chris@271 121 } else {
Chris@271 122 if (dr < fuzz) {
Chris@271 123 frame = right;
Chris@271 124 }
Chris@271 125 }
Chris@271 126 } else if (dl >= 0) {
Chris@271 127 if (dl <= fuzz) {
Chris@271 128 frame = left;
Chris@271 129 }
Chris@271 130 } else if (dr >= 0) {
Chris@271 131 if (dr <= fuzz) {
Chris@271 132 frame = right;
Chris@271 133 }
Chris@271 134 }
Chris@271 135 }
Chris@271 136 }
Chris@271 137
Chris@272 138 // std::cerr << " -> " << frame << " (resolution = " << resolution << ")" << std::endl;
Chris@272 139
Chris@271 140 return true;
Chris@271 141 }
Chris@271 142
Chris@271 143 int
Chris@271 144 TimeRulerLayer::getMajorTickSpacing(View *v, bool &quarterTicks) const
Chris@271 145 {
Chris@271 146 // return value is in milliseconds
Chris@271 147
Chris@271 148 if (!m_model || !v) return 1000;
Chris@271 149
Chris@271 150 int sampleRate = m_model->getSampleRate();
Chris@271 151 if (!sampleRate) return 1000;
Chris@271 152
Chris@271 153 long startFrame = v->getStartFrame();
Chris@271 154 long endFrame = v->getEndFrame();
Chris@271 155
Chris@271 156 int minPixelSpacing = 50;
Chris@271 157
Chris@271 158 RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate);
Chris@271 159 RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate);
Chris@271 160
Chris@271 161 int count = v->width() / minPixelSpacing;
Chris@271 162 if (count < 1) count = 1;
Chris@271 163 RealTime rtGap = (rtEnd - rtStart) / count;
Chris@271 164
Chris@271 165 int incms;
Chris@271 166 quarterTicks = false;
Chris@271 167
Chris@271 168 if (rtGap.sec > 0) {
Chris@271 169 incms = 1000;
Chris@271 170 int s = rtGap.sec;
Chris@271 171 if (s > 0) { incms *= 5; s /= 5; }
Chris@271 172 if (s > 0) { incms *= 2; s /= 2; }
Chris@271 173 if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
Chris@271 174 if (s > 0) { incms *= 5; s /= 5; quarterTicks = false; }
Chris@271 175 if (s > 0) { incms *= 2; s /= 2; }
Chris@271 176 if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
Chris@271 177 while (s > 0) {
Chris@271 178 incms *= 10;
Chris@271 179 s /= 10;
Chris@271 180 quarterTicks = false;
Chris@271 181 }
Chris@271 182 } else {
Chris@271 183 incms = 1;
Chris@271 184 int ms = rtGap.msec();
Chris@271 185 if (ms > 0) { incms *= 10; ms /= 10; }
Chris@271 186 if (ms > 0) { incms *= 10; ms /= 10; }
Chris@271 187 if (ms > 0) { incms *= 5; ms /= 5; }
Chris@271 188 if (ms > 0) { incms *= 2; ms /= 2; }
Chris@271 189 }
Chris@271 190
Chris@271 191 return incms;
Chris@271 192 }
Chris@271 193
Chris@0 194 void
Chris@44 195 TimeRulerLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 196 {
Chris@374 197 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 198 std::cerr << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y()
Chris@374 199 << ") [" << rect.width() << "x" << rect.height() << "]" << std::endl;
Chris@374 200 #endif
Chris@0 201
Chris@0 202 if (!m_model || !m_model->isOK()) return;
Chris@0 203
Chris@0 204 int sampleRate = m_model->getSampleRate();
Chris@0 205 if (!sampleRate) return;
Chris@0 206
Chris@374 207 long startFrame = v->getFrameForX(rect.x() - 50);
Chris@0 208
Chris@374 209 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 210 std::cerr << "start frame = " << startFrame << std::endl;
Chris@374 211 #endif
Chris@0 212
Chris@0 213 bool quarter = false;
Chris@271 214 int incms = getMajorTickSpacing(v, quarter);
Chris@0 215
Chris@374 216 int ms = lrint(1000.0 * (double(startFrame) / double(sampleRate)));
Chris@0 217 ms = (ms / incms) * incms - incms;
Chris@0 218
Chris@374 219 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 220 std::cerr << "start ms = " << ms << " at step " << incms << std::endl;
Chris@374 221 #endif
Chris@374 222
Chris@374 223 // Calculate the number of ticks per increment -- approximate
Chris@374 224 // values for x and frame counts here will do, no rounding issue.
Chris@374 225 // We always use the exact incms in our calculations for where to
Chris@374 226 // draw the actual ticks or lines.
Chris@374 227
Chris@374 228 int minPixelSpacing = 50;
Chris@374 229 long incFrame = (incms * sampleRate) / 1000;
Chris@374 230 int incX = incFrame / v->getZoomLevel();
Chris@0 231 int ticks = 10;
Chris@0 232 if (incX < minPixelSpacing * 2) {
Chris@0 233 ticks = quarter ? 4 : 5;
Chris@0 234 }
Chris@0 235
Chris@374 236 QColor greyColour = getPartialShades(v)[1];
Chris@0 237
Chris@374 238 paint.save();
Chris@0 239
Chris@0 240 while (1) {
Chris@0 241
Chris@374 242 // frame is used to determine where to draw the lines, so it
Chris@374 243 // needs to correspond to an exact pixel (so that we don't get
Chris@374 244 // a different pixel when scrolling a small amount and
Chris@374 245 // re-drawing with a different start frame).
Chris@374 246
Chris@374 247 double dms = ms;
Chris@374 248 long frame = lrint((dms * sampleRate) / 1000.0);
Chris@374 249 frame /= v->getZoomLevel();
Chris@374 250 frame *= v->getZoomLevel(); // so frame corresponds to an exact pixel
Chris@374 251
Chris@0 252 ms += incms;
Chris@0 253
Chris@374 254 int x = v->getXForFrame(frame);
Chris@0 255
Chris@374 256 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 257 std::cerr << "Considering frame = " << frame << ", x = " << x << std::endl;
Chris@374 258 #endif
Chris@0 259
Chris@374 260 if (x >= rect.x() + rect.width() + 50) {
Chris@374 261 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 262 std::cerr << "X well out of range, ending here" << std::endl;
Chris@374 263 #endif
Chris@374 264 break;
Chris@374 265 }
Chris@0 266
Chris@374 267 if (x >= rect.x() - 50) {
Chris@0 268
Chris@374 269 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 270 std::cerr << "X in range, drawing line here" << std::endl;
Chris@374 271 #endif
Chris@0 272
Chris@374 273 RealTime rt = RealTime::fromMilliseconds(ms);
Chris@0 274
Chris@374 275 QString text(QString::fromStdString(rt.toText()));
Chris@374 276 QFontMetrics metrics = paint.fontMetrics();
Chris@374 277 int tw = metrics.width(text);
Chris@0 278
Chris@374 279 if (tw < 50 &&
Chris@374 280 (x < rect.x() - tw/2 ||
Chris@374 281 x >= rect.x() + rect.width() + tw/2)) {
Chris@374 282 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 283 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@374 284 #endif
Chris@374 285 }
Chris@70 286
Chris@374 287 paint.setPen(greyColour);
Chris@374 288 paint.drawLine(x, 0, x, v->height());
Chris@374 289
Chris@374 290 paint.setPen(getBaseQColor());
Chris@374 291 paint.drawLine(x, 0, x, 5);
Chris@374 292 paint.drawLine(x, v->height() - 6, x, v->height() - 1);
Chris@374 293
Chris@374 294 int y;
Chris@374 295 switch (m_labelHeight) {
Chris@374 296 default:
Chris@374 297 case LabelTop:
Chris@374 298 y = 6 + metrics.ascent();
Chris@374 299 break;
Chris@374 300 case LabelMiddle:
Chris@374 301 y = v->height() / 2 - metrics.height() / 2 + metrics.ascent();
Chris@374 302 break;
Chris@374 303 case LabelBottom:
Chris@374 304 y = v->height() - metrics.height() + metrics.ascent() - 6;
Chris@374 305 }
Chris@374 306
Chris@374 307 if (v->getViewManager() && v->getViewManager()->getOverlayMode() !=
Chris@374 308 ViewManager::NoOverlays) {
Chris@374 309
Chris@374 310 if (v->getLayer(0) == this) {
Chris@374 311 // backmost layer, don't worry about outlining the text
Chris@374 312 paint.drawText(x+2 - tw/2, y, text);
Chris@374 313 } else {
Chris@374 314 v->drawVisibleText(paint, x+2 - tw/2, y, text, View::OutlinedText);
Chris@374 315 }
Chris@70 316 }
Chris@70 317 }
Chris@0 318
Chris@0 319 paint.setPen(greyColour);
Chris@0 320
Chris@0 321 for (int i = 1; i < ticks; ++i) {
Chris@374 322
Chris@374 323 dms = ms - incms + (i * double(incms)) / ticks;
Chris@374 324 frame = lrint((dms * sampleRate) / 1000.0);
Chris@374 325 frame /= v->getZoomLevel();
Chris@374 326 frame *= v->getZoomLevel(); // exact pixel as above
Chris@374 327
Chris@374 328 x = v->getXForFrame(frame);
Chris@374 329
Chris@374 330 if (x < rect.x() || x >= rect.x() + rect.width()) {
Chris@374 331 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 332 // std::cerr << "tick " << i << ": X out of range, going on to next tick" << std::endl;
Chris@374 333 #endif
Chris@374 334 continue;
Chris@374 335 }
Chris@374 336
Chris@374 337 #ifdef DEBUG_TIME_RULER_LAYER
Chris@374 338 std::cerr << "tick " << i << " in range, drawing at " << x << std::endl;
Chris@374 339 #endif
Chris@374 340
Chris@0 341 int sz = 5;
Chris@0 342 if (ticks == 10) {
Chris@0 343 if ((i % 2) == 1) {
Chris@0 344 if (i == 5) {
Chris@44 345 paint.drawLine(x, 0, x, v->height());
Chris@0 346 } else sz = 3;
Chris@0 347 } else {
Chris@0 348 sz = 7;
Chris@0 349 }
Chris@0 350 }
Chris@0 351 paint.drawLine(x, 0, x, sz);
Chris@44 352 paint.drawLine(x, v->height() - sz - 1, x, v->height() - 1);
Chris@0 353 }
Chris@0 354 }
Chris@0 355
Chris@0 356 paint.restore();
Chris@0 357 }
Chris@287 358
Chris@287 359 int
Chris@287 360 TimeRulerLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 361 {
Chris@287 362 impose = true;
Chris@287 363 return ColourDatabase::getInstance()->getColourIndex
Chris@287 364 (QString(darkbg ? "White" : "Black"));
Chris@287 365 }
Chris@287 366
Chris@374 367 QString TimeRulerLayer::getLayerPresentationName() const
Chris@374 368 {
Chris@374 369 LayerFactory *factory = LayerFactory::getInstance();
Chris@374 370 QString layerName = factory->getLayerPresentationName
Chris@374 371 (factory->getLayerType(this));
Chris@374 372 return layerName;
Chris@374 373 }
Chris@374 374
Chris@316 375 void
Chris@316 376 TimeRulerLayer::toXml(QTextStream &stream,
Chris@316 377 QString indent, QString extraAttributes) const
Chris@6 378 {
Chris@316 379 SingleColourLayer::toXml(stream, indent, extraAttributes);
Chris@6 380 }
Chris@6 381
Chris@11 382 void
Chris@11 383 TimeRulerLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 384 {
Chris@287 385 SingleColourLayer::setProperties(attributes);
Chris@11 386 }
Chris@11 387