annotate layer/TimeRulerLayer.cpp @ 1343:5f3e374746b6 zoom

Fix crash when scrolling beyond end of waveform (+ fix compiler warnings)
author Chris Cannam
date Thu, 27 Sep 2018 16:20:58 +0100
parents ed6400d5b571
children 3fa8cdbf362c
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@335 18 #include "LayerFactory.h"
Chris@335 19
Chris@128 20 #include "data/model/Model.h"
Chris@0 21 #include "base/RealTime.h"
Chris@1078 22 #include "view/View.h"
Chris@1078 23
Chris@376 24 #include "ColourDatabase.h"
Chris@1078 25 #include "PaintAssistant.h"
Chris@0 26
Chris@0 27 #include <QPainter>
Chris@0 28
Chris@0 29 #include <iostream>
Chris@271 30 #include <cmath>
Chris@1021 31 #include <stdexcept>
Chris@0 32
Chris@1342 33 #define DEBUG_TIME_RULER_LAYER 1
Chris@370 34
Chris@682 35
Chris@44 36 TimeRulerLayer::TimeRulerLayer() :
Chris@287 37 SingleColourLayer(),
Chris@0 38 m_model(0),
Chris@0 39 m_labelHeight(LabelTop)
Chris@0 40 {
Chris@44 41
Chris@0 42 }
Chris@0 43
Chris@0 44 void
Chris@0 45 TimeRulerLayer::setModel(Model *model)
Chris@0 46 {
Chris@0 47 if (m_model == model) return;
Chris@0 48 m_model = model;
Chris@0 49 emit modelReplaced();
Chris@0 50 }
Chris@0 51
Chris@271 52 bool
Chris@918 53 TimeRulerLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 54 int &resolution, SnapType snap) const
Chris@271 55 {
Chris@271 56 if (!m_model) {
Chris@271 57 resolution = 1;
Chris@271 58 return false;
Chris@271 59 }
Chris@271 60
Chris@271 61 bool q;
Chris@1341 62 int tickUSec = getMajorTickUSec(v, q);
Chris@1341 63 RealTime rtick = RealTime(0, tickUSec * 1000);
Chris@908 64 sv_samplerate_t rate = m_model->getSampleRate();
Chris@271 65
Chris@271 66 RealTime rt = RealTime::frame2RealTime(frame, rate);
Chris@271 67 double ratio = rt / rtick;
Chris@271 68
Chris@272 69 int rounded = int(ratio);
Chris@271 70 RealTime rdrt = rtick * rounded;
Chris@271 71
Chris@908 72 sv_frame_t left = RealTime::realTime2Frame(rdrt, rate);
Chris@908 73 resolution = int(RealTime::realTime2Frame(rtick, rate));
Chris@908 74 sv_frame_t right = left + resolution;
Chris@271 75
Chris@587 76 // SVDEBUG << "TimeRulerLayer::snapToFeatureFrame: type "
Chris@272 77 // << int(snap) << ", frame " << frame << " (time "
Chris@272 78 // << rt << ", tick " << rtick << ", rounded " << rdrt << ") ";
Chris@272 79
Chris@271 80 switch (snap) {
Chris@271 81
Chris@271 82 case SnapLeft:
Chris@271 83 frame = left;
Chris@271 84 break;
Chris@271 85
Chris@271 86 case SnapRight:
Chris@271 87 frame = right;
Chris@271 88 break;
Chris@271 89
Chris@271 90 case SnapNearest:
Chris@271 91 {
Chris@989 92 if (llabs(frame - left) > llabs(right - frame)) {
Chris@271 93 frame = right;
Chris@271 94 } else {
Chris@271 95 frame = left;
Chris@271 96 }
Chris@271 97 break;
Chris@271 98 }
Chris@271 99
Chris@271 100 case SnapNeighbouring:
Chris@271 101 {
Chris@271 102 int dl = -1, dr = -1;
Chris@271 103 int x = v->getXForFrame(frame);
Chris@271 104
Chris@271 105 if (left > v->getStartFrame() &&
Chris@271 106 left < v->getEndFrame()) {
Chris@271 107 dl = abs(v->getXForFrame(left) - x);
Chris@271 108 }
Chris@271 109
Chris@271 110 if (right > v->getStartFrame() &&
Chris@271 111 right < v->getEndFrame()) {
Chris@271 112 dr = abs(v->getXForFrame(right) - x);
Chris@271 113 }
Chris@271 114
Chris@271 115 int fuzz = 2;
Chris@271 116
Chris@271 117 if (dl >= 0 && dr >= 0) {
Chris@271 118 if (dl < dr) {
Chris@271 119 if (dl <= fuzz) {
Chris@271 120 frame = left;
Chris@271 121 }
Chris@271 122 } else {
Chris@271 123 if (dr < fuzz) {
Chris@271 124 frame = right;
Chris@271 125 }
Chris@271 126 }
Chris@271 127 } else if (dl >= 0) {
Chris@271 128 if (dl <= fuzz) {
Chris@271 129 frame = left;
Chris@271 130 }
Chris@271 131 } else if (dr >= 0) {
Chris@271 132 if (dr <= fuzz) {
Chris@271 133 frame = right;
Chris@271 134 }
Chris@271 135 }
Chris@271 136 }
Chris@271 137 }
Chris@271 138
Chris@587 139 // SVDEBUG << " -> " << frame << " (resolution = " << resolution << ")" << endl;
Chris@272 140
Chris@271 141 return true;
Chris@271 142 }
Chris@271 143
Chris@271 144 int
Chris@1341 145 TimeRulerLayer::getMajorTickUSec(LayerGeometryProvider *v,
Chris@1341 146 bool &quarterTicks) const
Chris@271 147 {
Chris@1341 148 // return value is in microseconds
Chris@1341 149 if (!m_model || !v) return 1000 * 1000;
Chris@271 150
Chris@908 151 sv_samplerate_t sampleRate = m_model->getSampleRate();
Chris@1341 152 if (!sampleRate) return 1000 * 1000;
Chris@271 153
Chris@908 154 sv_frame_t startFrame = v->getStartFrame();
Chris@908 155 sv_frame_t endFrame = v->getEndFrame();
Chris@271 156
Chris@1341 157 int minPixelSpacing = ViewManager::scalePixelSize(50);
Chris@271 158
Chris@271 159 RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate);
Chris@271 160 RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate);
Chris@271 161
Chris@918 162 int count = v->getPaintWidth() / minPixelSpacing;
Chris@271 163 if (count < 1) count = 1;
Chris@271 164 RealTime rtGap = (rtEnd - rtStart) / count;
Chris@271 165
Chris@1341 166 int incus;
Chris@271 167 quarterTicks = false;
Chris@271 168
Chris@271 169 if (rtGap.sec > 0) {
Chris@1341 170 incus = 1000 * 1000;
Chris@1266 171 int s = rtGap.sec;
Chris@1341 172 if (s > 0) { incus *= 5; s /= 5; }
Chris@1341 173 if (s > 0) { incus *= 2; s /= 2; }
Chris@1341 174 if (s > 0) { incus *= 6; s /= 6; quarterTicks = true; }
Chris@1341 175 if (s > 0) { incus *= 5; s /= 5; quarterTicks = false; }
Chris@1341 176 if (s > 0) { incus *= 2; s /= 2; }
Chris@1341 177 if (s > 0) { incus *= 6; s /= 6; quarterTicks = true; }
Chris@1266 178 while (s > 0) {
Chris@1341 179 incus *= 10;
Chris@1266 180 s /= 10;
Chris@1266 181 quarterTicks = false;
Chris@1266 182 }
Chris@1341 183 } else if (rtGap.msec() > 0) {
Chris@1341 184 incus = 1000;
Chris@1341 185 int ms = rtGap.msec();
Chris@1341 186 if (ms > 0) { incus *= 10; ms /= 10; }
Chris@1341 187 if (ms > 0) { incus *= 10; ms /= 10; }
Chris@1341 188 if (ms > 0) { incus *= 5; ms /= 5; }
Chris@1341 189 if (ms > 0) { incus *= 2; ms /= 2; }
Chris@271 190 } else {
Chris@1341 191 incus = 1;
Chris@1341 192 int us = rtGap.usec();
Chris@1341 193 if (us > 0) { incus *= 10; us /= 10; }
Chris@1341 194 if (us > 0) { incus *= 10; us /= 10; }
Chris@1341 195 if (us > 0) { incus *= 5; us /= 5; }
Chris@1341 196 if (us > 0) { incus *= 2; us /= 2; }
Chris@271 197 }
Chris@271 198
Chris@1341 199 return incus;
Chris@1341 200 }
Chris@1341 201
Chris@1341 202 int
Chris@1341 203 TimeRulerLayer::getXForUSec(LayerGeometryProvider *v, double us) const
Chris@1341 204 {
Chris@1341 205 sv_samplerate_t sampleRate = m_model->getSampleRate();
Chris@1341 206 double dframe = (us * sampleRate) / 1000000.0;
Chris@1341 207 double eps = 1e-7;
Chris@1341 208 sv_frame_t frame = sv_frame_t(floor(dframe + eps));
Chris@1341 209 int x;
Chris@1341 210
Chris@1341 211 ZoomLevel zoom = v->getZoomLevel();
Chris@1341 212
Chris@1341 213 if (zoom.zone == ZoomLevel::FramesPerPixel) {
Chris@1341 214
Chris@1341 215 frame /= zoom.level;
Chris@1341 216 frame *= zoom.level; // so frame corresponds to an exact pixel
Chris@1341 217
Chris@1341 218 x = v->getXForFrame(frame);
Chris@1341 219
Chris@1341 220 } else {
Chris@1341 221
Chris@1341 222 double off = dframe - double(frame);
Chris@1341 223 int x0 = v->getXForFrame(frame);
Chris@1341 224 int x1 = v->getXForFrame(frame + 1);
Chris@1341 225
Chris@1341 226 x = int(x0 + off * (x1 - x0));
Chris@1341 227 }
Chris@1341 228
Chris@1341 229 #ifdef DEBUG_TIME_RULER_LAYER
Chris@1341 230 cerr << "Considering frame = " << frame << ", x = " << x << endl;
Chris@1341 231 #endif
Chris@1341 232
Chris@1341 233 return x;
Chris@271 234 }
Chris@271 235
Chris@0 236 void
Chris@916 237 TimeRulerLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@0 238 {
Chris@370 239 #ifdef DEBUG_TIME_RULER_LAYER
Chris@587 240 SVDEBUG << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y()
Chris@1266 241 << ") [" << rect.width() << "x" << rect.height() << "]" << endl;
Chris@370 242 #endif
Chris@0 243
Chris@0 244 if (!m_model || !m_model->isOK()) return;
Chris@0 245
Chris@908 246 sv_samplerate_t sampleRate = m_model->getSampleRate();
Chris@0 247 if (!sampleRate) return;
Chris@0 248
Chris@908 249 sv_frame_t startFrame = v->getFrameForX(rect.x() - 50);
Chris@0 250
Chris@370 251 #ifdef DEBUG_TIME_RULER_LAYER
Chris@682 252 cerr << "start frame = " << startFrame << endl;
Chris@370 253 #endif
Chris@0 254
Chris@0 255 bool quarter = false;
Chris@1341 256 int incus = getMajorTickUSec(v, quarter);
Chris@0 257
Chris@1341 258 int us = int(lrint(1000.0 * 1000.0 * (double(startFrame) /
Chris@1341 259 double(sampleRate))));
Chris@1341 260 us = (us / incus) * incus - incus;
Chris@0 261
Chris@370 262 #ifdef DEBUG_TIME_RULER_LAYER
Chris@1341 263 cerr << "start us = " << us << " at step " << incus << endl;
Chris@370 264 #endif
Chris@370 265
Chris@370 266 // Calculate the number of ticks per increment -- approximate
Chris@370 267 // values for x and frame counts here will do, no rounding issue.
Chris@1341 268 // We always use the exact incus in our calculations for where to
Chris@370 269 // draw the actual ticks or lines.
Chris@370 270
Chris@370 271 int minPixelSpacing = 50;
Chris@1341 272 sv_frame_t incFrame = lrint((incus * sampleRate) / 1000000);
Chris@1326 273 int incX = int(round(v->getZoomLevel().framesToPixels(double(incFrame))));
Chris@0 274 int ticks = 10;
Chris@0 275 if (incX < minPixelSpacing * 2) {
Chris@1266 276 ticks = quarter ? 4 : 5;
Chris@0 277 }
Chris@0 278
Chris@370 279 QColor greyColour = getPartialShades(v)[1];
Chris@0 280
Chris@370 281 paint.save();
Chris@0 282
Chris@832 283 // Do not label time zero - we now overlay an opaque area over
Chris@832 284 // time < 0 which would cut it in half
Chris@1341 285 int minlabel = 1; // us
Chris@1021 286
Chris@0 287 while (1) {
Chris@0 288
Chris@370 289 // frame is used to determine where to draw the lines, so it
Chris@370 290 // needs to correspond to an exact pixel (so that we don't get
Chris@370 291 // a different pixel when scrolling a small amount and
Chris@370 292 // re-drawing with a different start frame).
Chris@370 293
Chris@1341 294 double dus = us;
Chris@370 295
Chris@1341 296 int x = getXForUSec(v, dus);
Chris@0 297
Chris@370 298 if (x >= rect.x() + rect.width() + 50) {
Chris@370 299 #ifdef DEBUG_TIME_RULER_LAYER
Chris@682 300 cerr << "X well out of range, ending here" << endl;
Chris@370 301 #endif
Chris@370 302 break;
Chris@370 303 }
Chris@0 304
Chris@1341 305 if (x >= rect.x() - 50 && us >= minlabel) {
Chris@0 306
Chris@1342 307 RealTime rt = RealTime::fromMicroseconds(us);
Chris@375 308
Chris@370 309 #ifdef DEBUG_TIME_RULER_LAYER
Chris@1342 310 cerr << "X in range, drawing line here for time " << rt.toText() << " (usec = " << us << ")" << endl;
Chris@370 311 #endif
Chris@0 312
Chris@370 313 QString text(QString::fromStdString(rt.toText()));
Chris@370 314 QFontMetrics metrics = paint.fontMetrics();
Chris@370 315 int tw = metrics.width(text);
Chris@0 316
Chris@370 317 if (tw < 50 &&
Chris@370 318 (x < rect.x() - tw/2 ||
Chris@370 319 x >= rect.x() + rect.width() + tw/2)) {
Chris@370 320 #ifdef DEBUG_TIME_RULER_LAYER
Chris@682 321 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 322 #endif
Chris@370 323 }
Chris@70 324
Chris@370 325 paint.setPen(greyColour);
Chris@918 326 paint.drawLine(x, 0, x, v->getPaintHeight());
Chris@370 327
Chris@370 328 paint.setPen(getBaseQColor());
Chris@370 329 paint.drawLine(x, 0, x, 5);
Chris@918 330 paint.drawLine(x, v->getPaintHeight() - 6, x, v->getPaintHeight() - 1);
Chris@370 331
Chris@370 332 int y;
Chris@370 333 switch (m_labelHeight) {
Chris@370 334 default:
Chris@370 335 case LabelTop:
Chris@370 336 y = 6 + metrics.ascent();
Chris@370 337 break;
Chris@370 338 case LabelMiddle:
Chris@918 339 y = v->getPaintHeight() / 2 - metrics.height() / 2 + metrics.ascent();
Chris@370 340 break;
Chris@370 341 case LabelBottom:
Chris@918 342 y = v->getPaintHeight() - metrics.height() + metrics.ascent() - 6;
Chris@370 343 }
Chris@370 344
Chris@370 345 if (v->getViewManager() && v->getViewManager()->getOverlayMode() !=
Chris@370 346 ViewManager::NoOverlays) {
Chris@370 347
Chris@919 348 if (v->getView()->getLayer(0) == this) {
Chris@370 349 // backmost layer, don't worry about outlining the text
Chris@370 350 paint.drawText(x+2 - tw/2, y, text);
Chris@370 351 } else {
Chris@1078 352 PaintAssistant::drawVisibleText(v, paint, x+2 - tw/2, y, text, PaintAssistant::OutlinedText);
Chris@370 353 }
Chris@70 354 }
Chris@70 355 }
Chris@0 356
Chris@1266 357 paint.setPen(greyColour);
Chris@0 358
Chris@1266 359 for (int i = 1; i < ticks; ++i) {
Chris@370 360
Chris@1341 361 dus = us + (i * double(incus)) / ticks;
Chris@370 362
Chris@1341 363 x = getXForUSec(v, dus);
Chris@370 364
Chris@370 365 if (x < rect.x() || x >= rect.x() + rect.width()) {
Chris@370 366 #ifdef DEBUG_TIME_RULER_LAYER
Chris@682 367 // cerr << "tick " << i << ": X out of range, going on to next tick" << endl;
Chris@370 368 #endif
Chris@370 369 continue;
Chris@370 370 }
Chris@370 371
Chris@370 372 #ifdef DEBUG_TIME_RULER_LAYER
Chris@682 373 cerr << "tick " << i << " in range, drawing at " << x << endl;
Chris@370 374 #endif
Chris@370 375
Chris@1266 376 int sz = 5;
Chris@1266 377 if (ticks == 10) {
Chris@1266 378 if ((i % 2) == 1) {
Chris@1266 379 if (i == 5) {
Chris@1266 380 paint.drawLine(x, 0, x, v->getPaintHeight());
Chris@1266 381 } else sz = 3;
Chris@1266 382 } else {
Chris@1266 383 sz = 7;
Chris@1266 384 }
Chris@1266 385 }
Chris@1266 386 paint.drawLine(x, 0, x, sz);
Chris@1266 387 paint.drawLine(x, v->getPaintHeight() - sz - 1, x, v->getPaintHeight() - 1);
Chris@1266 388 }
Chris@375 389
Chris@1341 390 us += incus;
Chris@0 391 }
Chris@0 392
Chris@0 393 paint.restore();
Chris@0 394 }
Chris@287 395
Chris@287 396 int
Chris@287 397 TimeRulerLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 398 {
Chris@287 399 impose = true;
Chris@287 400 return ColourDatabase::getInstance()->getColourIndex
Chris@287 401 (QString(darkbg ? "White" : "Black"));
Chris@287 402 }
Chris@287 403
Chris@335 404 QString TimeRulerLayer::getLayerPresentationName() const
Chris@335 405 {
Chris@335 406 LayerFactory *factory = LayerFactory::getInstance();
Chris@335 407 QString layerName = factory->getLayerPresentationName
Chris@335 408 (factory->getLayerType(this));
Chris@335 409 return layerName;
Chris@335 410 }
Chris@335 411
Chris@316 412 void
Chris@316 413 TimeRulerLayer::toXml(QTextStream &stream,
Chris@316 414 QString indent, QString extraAttributes) const
Chris@6 415 {
Chris@316 416 SingleColourLayer::toXml(stream, indent, extraAttributes);
Chris@6 417 }
Chris@6 418
Chris@11 419 void
Chris@11 420 TimeRulerLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 421 {
Chris@287 422 SingleColourLayer::setProperties(attributes);
Chris@11 423 }
Chris@11 424