annotate layer/TimeValueLayer.cpp @ 696:e77b1673e17e tonioni

Pull out log and linear vertical scales into their own classes, make some improvements to log numbering
author Chris Cannam
date Wed, 04 Dec 2013 11:35:08 +0000
parents 5674950ed82a
children ceb9a2992d96
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 "TimeValueLayer.h"
Chris@0 17
Chris@128 18 #include "data/model/Model.h"
Chris@0 19 #include "base/RealTime.h"
Chris@0 20 #include "base/Profiler.h"
Chris@197 21 #include "base/LogRange.h"
Chris@437 22 #include "base/RangeMapper.h"
Chris@376 23 #include "ColourDatabase.h"
Chris@128 24 #include "view/View.h"
Chris@0 25
Chris@128 26 #include "data/model/SparseTimeValueModel.h"
Chris@340 27 #include "data/model/Labeller.h"
Chris@0 28
Chris@70 29 #include "widgets/ItemEditDialog.h"
Chris@125 30 #include "widgets/ListInputDialog.h"
Chris@70 31
Chris@376 32 #include "ColourMapper.h"
Chris@691 33 #include "PianoScale.h"
Chris@696 34 #include "LinearNumericalScale.h"
Chris@696 35 #include "LogNumericalScale.h"
Chris@66 36
Chris@0 37 #include <QPainter>
Chris@6 38 #include <QPainterPath>
Chris@21 39 #include <QMouseEvent>
Chris@125 40 #include <QRegExp>
Chris@316 41 #include <QTextStream>
Chris@360 42 #include <QMessageBox>
Chris@340 43 #include <QInputDialog>
Chris@0 44
Chris@0 45 #include <iostream>
Chris@0 46 #include <cmath>
Chris@0 47
Chris@526 48 //#define DEBUG_TIME_VALUE_LAYER 1
Chris@526 49
Chris@44 50 TimeValueLayer::TimeValueLayer() :
Chris@287 51 SingleColourLayer(),
Chris@0 52 m_model(0),
Chris@21 53 m_editing(false),
Chris@23 54 m_originalPoint(0, 0.0, tr("New Point")),
Chris@21 55 m_editingPoint(0, 0.0, tr("New Point")),
Chris@22 56 m_editingCommand(0),
Chris@197 57 m_colourMap(0),
Chris@66 58 m_plotStyle(PlotConnectedPoints),
Chris@437 59 m_verticalScale(AutoAlignScale),
Chris@513 60 m_drawSegmentDivisions(true),
Chris@553 61 m_derivative(false),
Chris@437 62 m_scaleMinimum(0),
Chris@437 63 m_scaleMaximum(0)
Chris@0 64 {
Chris@44 65
Chris@0 66 }
Chris@0 67
Chris@0 68 void
Chris@0 69 TimeValueLayer::setModel(SparseTimeValueModel *model)
Chris@0 70 {
Chris@0 71 if (m_model == model) return;
Chris@0 72 m_model = model;
Chris@0 73
Chris@320 74 connectSignals(m_model);
Chris@0 75
Chris@438 76 m_scaleMinimum = 0;
Chris@438 77 m_scaleMaximum = 0;
Chris@438 78
Chris@494 79 if (m_model && m_model->getRDFTypeURI().endsWith("Segment")) {
Chris@494 80 setPlotStyle(PlotSegmentation);
Chris@494 81 }
Chris@494 82 if (m_model && m_model->getRDFTypeURI().endsWith("Change")) {
Chris@494 83 setPlotStyle(PlotSegmentation);
Chris@494 84 }
Chris@494 85
Chris@526 86 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 87 cerr << "TimeValueLayer::setModel(" << model << ")" << endl;
Chris@526 88 #endif
Chris@0 89
Chris@0 90 emit modelReplaced();
Chris@0 91 }
Chris@0 92
Chris@0 93 Layer::PropertyList
Chris@0 94 TimeValueLayer::getProperties() const
Chris@0 95 {
Chris@287 96 PropertyList list = SingleColourLayer::getProperties();
Chris@87 97 list.push_back("Plot Type");
Chris@87 98 list.push_back("Vertical Scale");
Chris@100 99 list.push_back("Scale Units");
Chris@513 100 list.push_back("Draw Segment Division Lines");
Chris@553 101 list.push_back("Show Derivative");
Chris@0 102 return list;
Chris@0 103 }
Chris@0 104
Chris@87 105 QString
Chris@87 106 TimeValueLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 107 {
Chris@87 108 if (name == "Plot Type") return tr("Plot Type");
Chris@87 109 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@100 110 if (name == "Scale Units") return tr("Scale Units");
Chris@513 111 if (name == "Draw Segment Division Lines") return tr("Draw Segment Division Lines");
Chris@553 112 if (name == "Show Derivative") return tr("Show Derivative");
Chris@287 113 return SingleColourLayer::getPropertyLabel(name);
Chris@87 114 }
Chris@87 115
Chris@515 116 QString
Chris@515 117 TimeValueLayer::getPropertyIconName(const PropertyName &name) const
Chris@515 118 {
Chris@515 119 if (name == "Draw Segment Division Lines") return "lines";
Chris@553 120 if (name == "Show Derivative") return "derivative";
Chris@515 121 return "";
Chris@515 122 }
Chris@515 123
Chris@0 124 Layer::PropertyType
Chris@0 125 TimeValueLayer::getPropertyType(const PropertyName &name) const
Chris@0 126 {
Chris@287 127 if (name == "Plot Type") return ValueProperty;
Chris@287 128 if (name == "Vertical Scale") return ValueProperty;
Chris@100 129 if (name == "Scale Units") return UnitsProperty;
Chris@287 130 if (name == "Colour" && m_plotStyle == PlotSegmentation) return ValueProperty;
Chris@513 131 if (name == "Draw Segment Division Lines") return ToggleProperty;
Chris@553 132 if (name == "Show Derivative") return ToggleProperty;
Chris@287 133 return SingleColourLayer::getPropertyType(name);
Chris@0 134 }
Chris@0 135
Chris@198 136 QString
Chris@198 137 TimeValueLayer::getPropertyGroupName(const PropertyName &name) const
Chris@198 138 {
Chris@198 139 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@198 140 return tr("Scale");
Chris@198 141 }
Chris@553 142 if (name == "Plot Type" || name == "Draw Segment Division Lines" ||
Chris@553 143 name == "Show Derivative") {
Chris@513 144 return tr("Plot Type");
Chris@513 145 }
Chris@287 146 return SingleColourLayer::getPropertyGroupName(name);
Chris@198 147 }
Chris@198 148
Chris@696 149 QString
Chris@696 150 TimeValueLayer::getScaleUnits() const
Chris@696 151 {
Chris@696 152 if (m_model) return m_model->getScaleUnits();
Chris@696 153 else return "";
Chris@696 154 }
Chris@696 155
Chris@0 156 int
Chris@0 157 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 158 int *min, int *max, int *deflt) const
Chris@0 159 {
Chris@216 160 int val = 0;
Chris@0 161
Chris@287 162 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@197 163
Chris@287 164 if (min) *min = 0;
Chris@287 165 if (max) *max = ColourMapper::getColourMapCount() - 1;
Chris@287 166 if (deflt) *deflt = 0;
Chris@197 167
Chris@287 168 val = m_colourMap;
Chris@0 169
Chris@87 170 } else if (name == "Plot Type") {
Chris@0 171
Chris@10 172 if (min) *min = 0;
Chris@615 173 if (max) *max = 6;
Chris@216 174 if (deflt) *deflt = int(PlotConnectedPoints);
Chris@0 175
Chris@216 176 val = int(m_plotStyle);
Chris@0 177
Chris@87 178 } else if (name == "Vertical Scale") {
Chris@66 179
Chris@66 180 if (min) *min = 0;
Chris@66 181 if (max) *max = 3;
Chris@216 182 if (deflt) *deflt = int(AutoAlignScale);
Chris@66 183
Chris@216 184 val = int(m_verticalScale);
Chris@66 185
Chris@100 186 } else if (name == "Scale Units") {
Chris@100 187
Chris@216 188 if (deflt) *deflt = 0;
Chris@100 189 if (m_model) {
Chris@216 190 val = UnitDatabase::getInstance()->getUnitId
Chris@696 191 (getScaleUnits());
Chris@100 192 }
Chris@100 193
Chris@513 194 } else if (name == "Draw Segment Division Lines") {
Chris@513 195
Chris@513 196 if (min) *min = 0;
Chris@513 197 if (max) *max = 0;
Chris@513 198 if (deflt) *deflt = 1;
Chris@513 199 val = (m_drawSegmentDivisions ? 1.0 : 0.0);
Chris@513 200
Chris@553 201 } else if (name == "Show Derivative") {
Chris@553 202
Chris@553 203 if (min) *min = 0;
Chris@553 204 if (max) *max = 0;
Chris@553 205 if (deflt) *deflt = 0;
Chris@553 206 val = (m_derivative ? 1.0 : 0.0);
Chris@553 207
Chris@0 208 } else {
Chris@0 209
Chris@287 210 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 211 }
Chris@0 212
Chris@216 213 return val;
Chris@0 214 }
Chris@0 215
Chris@0 216 QString
Chris@0 217 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 218 int value) const
Chris@0 219 {
Chris@287 220 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@287 221 return ColourMapper::getColourMapName(value);
Chris@87 222 } else if (name == "Plot Type") {
Chris@0 223 switch (value) {
Chris@0 224 default:
Chris@0 225 case 0: return tr("Points");
Chris@0 226 case 1: return tr("Stems");
Chris@6 227 case 2: return tr("Connected Points");
Chris@6 228 case 3: return tr("Lines");
Chris@6 229 case 4: return tr("Curve");
Chris@26 230 case 5: return tr("Segmentation");
Chris@615 231 case 6: return tr("Discrete Curves");
Chris@0 232 }
Chris@87 233 } else if (name == "Vertical Scale") {
Chris@66 234 switch (value) {
Chris@66 235 default:
Chris@101 236 case 0: return tr("Auto-Align");
Chris@198 237 case 1: return tr("Linear");
Chris@198 238 case 2: return tr("Log");
Chris@198 239 case 3: return tr("+/-1");
Chris@66 240 }
Chris@0 241 }
Chris@287 242 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@0 243 }
Chris@0 244
Chris@0 245 void
Chris@0 246 TimeValueLayer::setProperty(const PropertyName &name, int value)
Chris@0 247 {
Chris@287 248 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@287 249 setFillColourMap(value);
Chris@87 250 } else if (name == "Plot Type") {
Chris@0 251 setPlotStyle(PlotStyle(value));
Chris@87 252 } else if (name == "Vertical Scale") {
Chris@66 253 setVerticalScale(VerticalScale(value));
Chris@100 254 } else if (name == "Scale Units") {
Chris@100 255 if (m_model) {
Chris@100 256 m_model->setScaleUnits
Chris@100 257 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 258 emit modelChanged();
Chris@100 259 }
Chris@513 260 } else if (name == "Draw Segment Division Lines") {
Chris@513 261 setDrawSegmentDivisions(value > 0.5);
Chris@553 262 } else if (name == "Show Derivative") {
Chris@553 263 setShowDerivative(value > 0.5);
Chris@287 264 } else {
Chris@287 265 SingleColourLayer::setProperty(name, value);
Chris@0 266 }
Chris@0 267 }
Chris@0 268
Chris@0 269 void
Chris@197 270 TimeValueLayer::setFillColourMap(int map)
Chris@197 271 {
Chris@197 272 if (m_colourMap == map) return;
Chris@197 273 m_colourMap = map;
Chris@197 274 emit layerParametersChanged();
Chris@197 275 }
Chris@197 276
Chris@197 277 void
Chris@0 278 TimeValueLayer::setPlotStyle(PlotStyle style)
Chris@0 279 {
Chris@0 280 if (m_plotStyle == style) return;
Chris@197 281 bool colourTypeChanged = (style == PlotSegmentation ||
Chris@197 282 m_plotStyle == PlotSegmentation);
Chris@0 283 m_plotStyle = style;
Chris@197 284 if (colourTypeChanged) {
Chris@197 285 emit layerParameterRangesChanged();
Chris@197 286 }
Chris@0 287 emit layerParametersChanged();
Chris@0 288 }
Chris@0 289
Chris@66 290 void
Chris@66 291 TimeValueLayer::setVerticalScale(VerticalScale scale)
Chris@66 292 {
Chris@66 293 if (m_verticalScale == scale) return;
Chris@66 294 m_verticalScale = scale;
Chris@66 295 emit layerParametersChanged();
Chris@66 296 }
Chris@66 297
Chris@513 298 void
Chris@513 299 TimeValueLayer::setDrawSegmentDivisions(bool draw)
Chris@513 300 {
Chris@513 301 if (m_drawSegmentDivisions == draw) return;
Chris@513 302 m_drawSegmentDivisions = draw;
Chris@513 303 emit layerParametersChanged();
Chris@513 304 }
Chris@513 305
Chris@553 306 void
Chris@553 307 TimeValueLayer::setShowDerivative(bool show)
Chris@553 308 {
Chris@553 309 if (m_derivative == show) return;
Chris@553 310 m_derivative = show;
Chris@553 311 emit layerParametersChanged();
Chris@553 312 }
Chris@553 313
Chris@0 314 bool
Chris@44 315 TimeValueLayer::isLayerScrollable(const View *v) const
Chris@0 316 {
Chris@6 317 // We don't illuminate sections in the line or curve modes, so
Chris@6 318 // they're always scrollable
Chris@6 319
Chris@6 320 if (m_plotStyle == PlotLines ||
Chris@615 321 m_plotStyle == PlotCurve ||
Chris@615 322 m_plotStyle == PlotDiscreteCurves) return true;
Chris@6 323
Chris@0 324 QPoint discard;
Chris@44 325 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@0 326 }
Chris@0 327
Chris@79 328 bool
Chris@101 329 TimeValueLayer::getValueExtents(float &min, float &max,
Chris@101 330 bool &logarithmic, QString &unit) const
Chris@79 331 {
Chris@101 332 if (!m_model) return false;
Chris@668 333
Chris@79 334 min = m_model->getValueMinimum();
Chris@79 335 max = m_model->getValueMaximum();
Chris@668 336
Chris@101 337 logarithmic = (m_verticalScale == LogScale);
Chris@668 338
Chris@696 339 unit = getScaleUnits();
Chris@668 340
Chris@553 341 if (m_derivative) {
Chris@553 342 max = std::max(fabsf(min), fabsf(max));
Chris@553 343 min = -max;
Chris@553 344 }
Chris@629 345
Chris@629 346 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 347 cerr << "TimeValueLayer::getValueExtents: min = " << min << ", max = " << max << endl;
Chris@629 348 #endif
Chris@629 349
Chris@668 350 if (!shouldAutoAlign() && !logarithmic && !m_derivative) {
Chris@668 351
Chris@668 352 if (max == min) {
Chris@668 353 max = max + 0.5;
Chris@668 354 min = min - 0.5;
Chris@668 355 } else {
Chris@668 356 float margin = (max - min) / 10.0;
Chris@668 357 max = max + margin;
Chris@668 358 min = min - margin;
Chris@668 359 }
Chris@668 360
Chris@668 361 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 362 cerr << "TimeValueLayer::getValueExtents: min = " << min << ", max = " << max << " (after adjustment)" << endl;
Chris@668 363 #endif
Chris@668 364 }
Chris@668 365
Chris@79 366 return true;
Chris@79 367 }
Chris@79 368
Chris@101 369 bool
Chris@101 370 TimeValueLayer::getDisplayExtents(float &min, float &max) const
Chris@101 371 {
Chris@296 372 if (!m_model || shouldAutoAlign()) return false;
Chris@101 373
Chris@437 374 if (m_scaleMinimum == m_scaleMaximum) {
Chris@668 375 bool log;
Chris@668 376 QString unit;
Chris@668 377 getValueExtents(min, max, log, unit);
Chris@553 378 } else {
Chris@553 379 min = m_scaleMinimum;
Chris@553 380 max = m_scaleMaximum;
Chris@437 381 }
Chris@437 382
Chris@553 383 if (m_derivative) {
Chris@553 384 max = std::max(fabsf(min), fabsf(max));
Chris@553 385 min = -max;
Chris@553 386 }
Chris@437 387
Chris@526 388 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 389 cerr << "TimeValueLayer::getDisplayExtents: min = " << min << ", max = " << max << endl;
Chris@526 390 #endif
Chris@437 391
Chris@101 392 return true;
Chris@101 393 }
Chris@101 394
Chris@437 395 bool
Chris@437 396 TimeValueLayer::setDisplayExtents(float min, float max)
Chris@437 397 {
Chris@437 398 if (!m_model) return false;
Chris@437 399
Chris@437 400 if (min == max) {
Chris@437 401 if (min == 0.f) {
Chris@437 402 max = 1.f;
Chris@437 403 } else {
Chris@437 404 max = min * 1.0001;
Chris@437 405 }
Chris@437 406 }
Chris@437 407
Chris@437 408 m_scaleMinimum = min;
Chris@437 409 m_scaleMaximum = max;
Chris@437 410
Chris@526 411 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 412 cerr << "TimeValueLayer::setDisplayExtents: min = " << min << ", max = " << max << endl;
Chris@526 413 #endif
Chris@437 414
Chris@437 415 emit layerParametersChanged();
Chris@437 416 return true;
Chris@437 417 }
Chris@437 418
Chris@437 419 int
Chris@437 420 TimeValueLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@437 421 {
Chris@437 422 if (shouldAutoAlign()) return 0;
Chris@437 423 if (!m_model) return 0;
Chris@437 424
Chris@439 425 defaultStep = 0;
Chris@437 426 return 100;
Chris@437 427 }
Chris@437 428
Chris@437 429 int
Chris@437 430 TimeValueLayer::getCurrentVerticalZoomStep() const
Chris@437 431 {
Chris@437 432 if (shouldAutoAlign()) return 0;
Chris@437 433 if (!m_model) return 0;
Chris@437 434
Chris@437 435 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@437 436 if (!mapper) return 0;
Chris@437 437
Chris@437 438 float dmin, dmax;
Chris@437 439 getDisplayExtents(dmin, dmax);
Chris@437 440
Chris@437 441 int nr = mapper->getPositionForValue(dmax - dmin);
Chris@526 442
Chris@526 443 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 444 cerr << "TimeValueLayer::getCurrentVerticalZoomStep: dmin = " << dmin << ", dmax = " << dmax << ", nr = " << nr << endl;
Chris@526 445 #endif
Chris@526 446
Chris@437 447 delete mapper;
Chris@437 448
Chris@437 449 return 100 - nr;
Chris@437 450 }
Chris@437 451
Chris@437 452 void
Chris@437 453 TimeValueLayer::setVerticalZoomStep(int step)
Chris@437 454 {
Chris@437 455 if (shouldAutoAlign()) return;
Chris@437 456 if (!m_model) return;
Chris@437 457
Chris@437 458 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@437 459 if (!mapper) return;
Chris@437 460
Chris@437 461 float min, max;
Chris@437 462 bool logarithmic;
Chris@437 463 QString unit;
Chris@437 464 getValueExtents(min, max, logarithmic, unit);
Chris@437 465
Chris@437 466 float dmin, dmax;
Chris@437 467 getDisplayExtents(dmin, dmax);
Chris@437 468
Chris@437 469 float newdist = mapper->getValueForPosition(100 - step);
Chris@437 470
Chris@437 471 float newmin, newmax;
Chris@437 472
Chris@437 473 if (logarithmic) {
Chris@437 474
Chris@437 475 // see SpectrogramLayer::setVerticalZoomStep
Chris@437 476
Chris@437 477 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@437 478 newmin = newmax - newdist;
Chris@437 479
Chris@526 480 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 481 cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
Chris@526 482 #endif
Chris@437 483
Chris@437 484 } else {
Chris@437 485 float dmid = (dmax + dmin) / 2;
Chris@437 486 newmin = dmid - newdist / 2;
Chris@437 487 newmax = dmid + newdist / 2;
Chris@437 488 }
Chris@437 489
Chris@437 490 if (newmin < min) {
Chris@437 491 newmax += (min - newmin);
Chris@437 492 newmin = min;
Chris@437 493 }
Chris@437 494 if (newmax > max) {
Chris@437 495 newmax = max;
Chris@437 496 }
Chris@437 497
Chris@526 498 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 499 cerr << "TimeValueLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@526 500 #endif
Chris@437 501
Chris@437 502 setDisplayExtents(newmin, newmax);
Chris@437 503 }
Chris@437 504
Chris@437 505 RangeMapper *
Chris@437 506 TimeValueLayer::getNewVerticalZoomRangeMapper() const
Chris@437 507 {
Chris@437 508 if (!m_model) return 0;
Chris@437 509
Chris@437 510 RangeMapper *mapper;
Chris@437 511
Chris@437 512 float min, max;
Chris@437 513 bool logarithmic;
Chris@437 514 QString unit;
Chris@437 515 getValueExtents(min, max, logarithmic, unit);
Chris@437 516
Chris@437 517 if (min == max) return 0;
Chris@437 518
Chris@437 519 if (logarithmic) {
Chris@437 520 mapper = new LogRangeMapper(0, 100, min, max, unit);
Chris@437 521 } else {
Chris@437 522 mapper = new LinearRangeMapper(0, 100, min, max, unit);
Chris@437 523 }
Chris@437 524
Chris@437 525 return mapper;
Chris@437 526 }
Chris@437 527
Chris@0 528 SparseTimeValueModel::PointList
Chris@44 529 TimeValueLayer::getLocalPoints(View *v, int x) const
Chris@0 530 {
Chris@0 531 if (!m_model) return SparseTimeValueModel::PointList();
Chris@0 532
Chris@44 533 long frame = v->getFrameForX(x);
Chris@0 534
Chris@0 535 SparseTimeValueModel::PointList onPoints =
Chris@0 536 m_model->getPoints(frame);
Chris@0 537
Chris@0 538 if (!onPoints.empty()) {
Chris@0 539 return onPoints;
Chris@0 540 }
Chris@0 541
Chris@0 542 SparseTimeValueModel::PointList prevPoints =
Chris@0 543 m_model->getPreviousPoints(frame);
Chris@0 544 SparseTimeValueModel::PointList nextPoints =
Chris@0 545 m_model->getNextPoints(frame);
Chris@0 546
Chris@0 547 SparseTimeValueModel::PointList usePoints = prevPoints;
Chris@0 548
Chris@0 549 if (prevPoints.empty()) {
Chris@0 550 usePoints = nextPoints;
Chris@631 551 } else if (nextPoints.empty()) {
Chris@631 552 // stick with prevPoints
Chris@248 553 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
Chris@44 554 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@0 555 usePoints = nextPoints;
Chris@0 556 } else if (nextPoints.begin()->frame - frame <
Chris@0 557 frame - prevPoints.begin()->frame) {
Chris@0 558 usePoints = nextPoints;
Chris@0 559 }
Chris@0 560
Chris@28 561 if (!usePoints.empty()) {
Chris@28 562 int fuzz = 2;
Chris@44 563 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@28 564 if ((px > x && px - x > fuzz) ||
Chris@631 565 (px < x && x - px > fuzz + 3)) {
Chris@28 566 usePoints.clear();
Chris@28 567 }
Chris@28 568 }
Chris@28 569
Chris@0 570 return usePoints;
Chris@0 571 }
Chris@0 572
Chris@25 573 QString
Chris@552 574 TimeValueLayer::getLabelPreceding(size_t frame) const
Chris@552 575 {
Chris@552 576 if (!m_model) return "";
Chris@552 577 SparseTimeValueModel::PointList points = m_model->getPreviousPoints(frame);
Chris@552 578 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@552 579 i != points.end(); ++i) {
Chris@552 580 if (i->label != "") return i->label;
Chris@552 581 }
Chris@552 582 return "";
Chris@552 583 }
Chris@552 584
Chris@552 585 QString
Chris@44 586 TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@0 587 {
Chris@25 588 int x = pos.x();
Chris@0 589
Chris@25 590 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 591
Chris@44 592 SparseTimeValueModel::PointList points = getLocalPoints(v, x);
Chris@0 593
Chris@0 594 if (points.empty()) {
Chris@0 595 if (!m_model->isReady()) {
Chris@25 596 return tr("In progress");
Chris@25 597 } else {
Chris@25 598 return tr("No local points");
Chris@0 599 }
Chris@0 600 }
Chris@0 601
Chris@0 602 long useFrame = points.begin()->frame;
Chris@0 603
Chris@0 604 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 605
Chris@25 606 QString text;
Chris@696 607 QString unit = getScaleUnits();
Chris@101 608 if (unit != "") unit = " " + unit;
Chris@0 609
Chris@25 610 if (points.begin()->label == "") {
Chris@101 611 text = QString(tr("Time:\t%1\nValue:\t%2%3\nNo label"))
Chris@25 612 .arg(rt.toText(true).c_str())
Chris@25 613 .arg(points.begin()->value)
Chris@101 614 .arg(unit);
Chris@101 615 } else {
Chris@101 616 text = QString(tr("Time:\t%1\nValue:\t%2%3\nLabel:\t%4"))
Chris@101 617 .arg(rt.toText(true).c_str())
Chris@101 618 .arg(points.begin()->value)
Chris@101 619 .arg(unit)
Chris@25 620 .arg(points.begin()->label);
Chris@25 621 }
Chris@0 622
Chris@44 623 pos = QPoint(v->getXForFrame(useFrame),
Chris@44 624 getYForValue(v, points.begin()->value));
Chris@25 625 return text;
Chris@0 626 }
Chris@0 627
Chris@28 628 bool
Chris@44 629 TimeValueLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 630 size_t &resolution,
Chris@28 631 SnapType snap) const
Chris@13 632 {
Chris@13 633 if (!m_model) {
Chris@44 634 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@13 635 }
Chris@13 636
Chris@13 637 resolution = m_model->getResolution();
Chris@28 638 SparseTimeValueModel::PointList points;
Chris@13 639
Chris@28 640 if (snap == SnapNeighbouring) {
Chris@28 641
Chris@44 642 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@28 643 if (points.empty()) return false;
Chris@28 644 frame = points.begin()->frame;
Chris@28 645 return true;
Chris@28 646 }
Chris@28 647
Chris@28 648 points = m_model->getPoints(frame, frame);
Chris@28 649 int snapped = frame;
Chris@28 650 bool found = false;
Chris@13 651
Chris@13 652 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@13 653 i != points.end(); ++i) {
Chris@13 654
Chris@28 655 if (snap == SnapRight) {
Chris@28 656
Chris@13 657 if (i->frame > frame) {
Chris@28 658 snapped = i->frame;
Chris@28 659 found = true;
Chris@13 660 break;
Chris@13 661 }
Chris@28 662
Chris@28 663 } else if (snap == SnapLeft) {
Chris@28 664
Chris@13 665 if (i->frame <= frame) {
Chris@28 666 snapped = i->frame;
Chris@28 667 found = true; // don't break, as the next may be better
Chris@28 668 } else {
Chris@28 669 break;
Chris@28 670 }
Chris@28 671
Chris@28 672 } else { // nearest
Chris@28 673
Chris@28 674 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@28 675 ++j;
Chris@28 676
Chris@28 677 if (j == points.end()) {
Chris@28 678
Chris@28 679 snapped = i->frame;
Chris@28 680 found = true;
Chris@28 681 break;
Chris@28 682
Chris@28 683 } else if (j->frame >= frame) {
Chris@28 684
Chris@28 685 if (j->frame - frame < frame - i->frame) {
Chris@28 686 snapped = j->frame;
Chris@28 687 } else {
Chris@28 688 snapped = i->frame;
Chris@28 689 }
Chris@28 690 found = true;
Chris@28 691 break;
Chris@13 692 }
Chris@13 693 }
Chris@13 694 }
Chris@13 695
Chris@28 696 frame = snapped;
Chris@28 697 return found;
Chris@13 698 }
Chris@13 699
Chris@517 700 bool
Chris@517 701 TimeValueLayer::snapToSimilarFeature(View *v, int &frame,
Chris@517 702 size_t &resolution,
Chris@517 703 SnapType snap) const
Chris@517 704 {
Chris@517 705 if (!m_model) {
Chris@517 706 return Layer::snapToSimilarFeature(v, frame, resolution, snap);
Chris@517 707 }
Chris@517 708
Chris@517 709 resolution = m_model->getResolution();
Chris@517 710
Chris@517 711 const SparseTimeValueModel::PointList &points = m_model->getPoints();
Chris@517 712 SparseTimeValueModel::PointList close = m_model->getPoints(frame, frame);
Chris@517 713
Chris@517 714 SparseTimeValueModel::PointList::const_iterator i;
Chris@517 715
Chris@517 716 int matchframe = frame;
Chris@517 717 float matchvalue = 0.f;
Chris@517 718
Chris@517 719 for (i = close.begin(); i != close.end(); ++i) {
Chris@517 720 if (i->frame > frame) break;
Chris@517 721 matchvalue = i->value;
Chris@517 722 matchframe = i->frame;
Chris@517 723 }
Chris@517 724
Chris@517 725 int snapped = frame;
Chris@517 726 bool found = false;
Chris@517 727 bool distant = false;
Chris@517 728 float epsilon = 0.0001;
Chris@517 729
Chris@517 730 i = close.begin();
Chris@517 731
Chris@517 732 // Scan through the close points first, then the more distant ones
Chris@517 733 // if no suitable close one is found
Chris@517 734
Chris@517 735 while (i != points.end()) {
Chris@517 736
Chris@517 737 if (i == close.end()) {
Chris@517 738 i = points.begin();
Chris@517 739 distant = true;
Chris@517 740 }
Chris@517 741
Chris@517 742 if (snap == SnapRight) {
Chris@517 743
Chris@517 744 if (i->frame > matchframe &&
Chris@517 745 fabsf(i->value - matchvalue) < epsilon) {
Chris@517 746 snapped = i->frame;
Chris@517 747 found = true;
Chris@517 748 break;
Chris@517 749 }
Chris@517 750
Chris@517 751 } else if (snap == SnapLeft) {
Chris@517 752
Chris@517 753 if (i->frame < matchframe) {
Chris@517 754 if (fabsf(i->value - matchvalue) < epsilon) {
Chris@517 755 snapped = i->frame;
Chris@517 756 found = true; // don't break, as the next may be better
Chris@517 757 }
Chris@517 758 } else if (found || distant) {
Chris@517 759 break;
Chris@517 760 }
Chris@517 761
Chris@517 762 } else {
Chris@517 763 // no other snap types supported
Chris@517 764 }
Chris@517 765
Chris@517 766 ++i;
Chris@517 767 }
Chris@517 768
Chris@517 769 frame = snapped;
Chris@517 770 return found;
Chris@517 771 }
Chris@517 772
Chris@101 773 void
Chris@101 774 TimeValueLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@101 775 {
Chris@101 776 min = 0.0;
Chris@101 777 max = 0.0;
Chris@101 778 log = false;
Chris@101 779
Chris@296 780 if (shouldAutoAlign()) {
Chris@101 781
Chris@696 782 if (!v->getValueExtents(getScaleUnits(), min, max, log)) {
Chris@101 783 min = m_model->getValueMinimum();
Chris@101 784 max = m_model->getValueMaximum();
Chris@668 785 } else if (log) {
Chris@668 786 LogRange::mapRange(min, max);
Chris@101 787 }
Chris@101 788
Chris@101 789 } else if (m_verticalScale == PlusMinusOneScale) {
Chris@101 790
Chris@101 791 min = -1.0;
Chris@101 792 max = 1.0;
Chris@101 793
Chris@101 794 } else {
Chris@101 795
Chris@437 796 getDisplayExtents(min, max);
Chris@632 797
Chris@101 798 if (m_verticalScale == LogScale) {
Chris@197 799 LogRange::mapRange(min, max);
Chris@101 800 log = true;
Chris@101 801 }
Chris@101 802 }
Chris@101 803
Chris@629 804 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 805 cerr << "TimeValueLayer::getScaleExtents: min = " << min << ", max = " << max << endl;
Chris@629 806 #endif
Chris@101 807 }
Chris@101 808
Chris@21 809 int
Chris@66 810 TimeValueLayer::getYForValue(View *v, float val) const
Chris@21 811 {
Chris@79 812 float min = 0.0, max = 0.0;
Chris@101 813 bool logarithmic = false;
Chris@79 814 int h = v->height();
Chris@79 815
Chris@101 816 getScaleExtents(v, min, max, logarithmic);
Chris@101 817
Chris@526 818 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 819 cerr << "getYForValue(" << val << "): min " << min << ", max "
Chris@682 820 << max << ", log " << logarithmic << endl;
Chris@526 821 #endif
Chris@101 822
Chris@101 823 if (logarithmic) {
Chris@197 824 val = LogRange::map(val);
Chris@79 825 }
Chris@79 826
Chris@66 827 return int(h - ((val - min) * h) / (max - min));
Chris@21 828 }
Chris@21 829
Chris@21 830 float
Chris@44 831 TimeValueLayer::getValueForY(View *v, int y) const
Chris@21 832 {
Chris@101 833 float min = 0.0, max = 0.0;
Chris@101 834 bool logarithmic = false;
Chris@44 835 int h = v->height();
Chris@21 836
Chris@101 837 getScaleExtents(v, min, max, logarithmic);
Chris@101 838
Chris@101 839 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 840
Chris@101 841 if (logarithmic) {
Chris@437 842 val = LogRange::map(val);
Chris@101 843 }
Chris@101 844
Chris@101 845 return val;
Chris@21 846 }
Chris@21 847
Chris@296 848 bool
Chris@296 849 TimeValueLayer::shouldAutoAlign() const
Chris@296 850 {
Chris@296 851 if (!m_model) return false;
Chris@696 852 QString unit = getScaleUnits();
Chris@296 853 return (m_verticalScale == AutoAlignScale && unit != "");
Chris@296 854 }
Chris@296 855
Chris@68 856 QColor
Chris@101 857 TimeValueLayer::getColourForValue(View *v, float val) const
Chris@68 858 {
Chris@101 859 float min, max;
Chris@101 860 bool log;
Chris@101 861 getScaleExtents(v, min, max, log);
Chris@68 862
Chris@197 863 if (min > max) std::swap(min, max);
Chris@197 864 if (max == min) max = min + 1;
Chris@197 865
Chris@101 866 if (log) {
Chris@197 867 val = LogRange::map(val);
Chris@68 868 }
Chris@68 869
Chris@526 870 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 871 cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
Chris@682 872 << max << ", log " << log << ", value " << val << endl;
Chris@526 873 #endif
Chris@68 874
Chris@197 875 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
Chris@197 876 return QColor(solid.red(), solid.green(), solid.blue(), 120);
Chris@68 877 }
Chris@68 878
Chris@287 879 int
Chris@287 880 TimeValueLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 881 {
Chris@287 882 impose = false;
Chris@287 883 return ColourDatabase::getInstance()->getColourIndex
Chris@287 884 (QString(darkbg ? "Bright Green" : "Green"));
Chris@287 885 }
Chris@287 886
Chris@0 887 void
Chris@44 888 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 889 {
Chris@0 890 if (!m_model || !m_model->isOK()) return;
Chris@0 891
Chris@0 892 int sampleRate = m_model->getSampleRate();
Chris@0 893 if (!sampleRate) return;
Chris@0 894
Chris@353 895 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@353 896
Chris@0 897 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 898
Chris@0 899 int x0 = rect.left(), x1 = rect.right();
Chris@44 900 long frame0 = v->getFrameForX(x0);
Chris@44 901 long frame1 = v->getFrameForX(x1);
Chris@553 902 if (m_derivative) --frame0;
Chris@0 903
Chris@0 904 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 905 (frame0, frame1));
Chris@11 906 if (points.empty()) return;
Chris@0 907
Chris@287 908 paint.setPen(getBaseQColor());
Chris@0 909
Chris@287 910 QColor brushColour(getBaseQColor());
Chris@0 911 brushColour.setAlpha(80);
Chris@0 912 paint.setBrush(brushColour);
Chris@0 913
Chris@526 914 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 915 cerr << "TimeValueLayer::paint: resolution is "
Chris@682 916 << m_model->getResolution() << " frames" << endl;
Chris@526 917 #endif
Chris@0 918
Chris@0 919 float min = m_model->getValueMinimum();
Chris@0 920 float max = m_model->getValueMaximum();
Chris@0 921 if (max == min) max = min + 1.0;
Chris@0 922
Chris@44 923 int origin = int(nearbyint(v->height() -
Chris@44 924 (-min * v->height()) / (max - min)));
Chris@0 925
Chris@0 926 QPoint localPos;
Chris@0 927 long illuminateFrame = -1;
Chris@0 928
Chris@44 929 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 930 SparseTimeValueModel::PointList localPoints =
Chris@44 931 getLocalPoints(v, localPos.x());
Chris@526 932 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 933 cerr << "TimeValueLayer: " << localPoints.size() << " local points" << endl;
Chris@526 934 #endif
Chris@0 935 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 936 }
Chris@6 937
Chris@20 938 int w =
Chris@44 939 v->getXForFrame(frame0 + m_model->getResolution()) -
Chris@44 940 v->getXForFrame(frame0);
Chris@7 941
Chris@629 942 if (m_plotStyle == PlotStems) {
Chris@629 943 if (w < 2) w = 2;
Chris@629 944 } else {
Chris@629 945 if (w < 1) w = 1;
Chris@629 946 }
Chris@629 947
Chris@6 948 paint.save();
Chris@6 949
Chris@6 950 QPainterPath path;
Chris@55 951 int pointCount = 0;
Chris@79 952
Chris@79 953 int textY = 0;
Chris@79 954 if (m_plotStyle == PlotSegmentation) {
Chris@79 955 textY = v->getTextLabelHeight(this, paint);
Chris@554 956 } else {
Chris@554 957 int originY = getYForValue(v, 0.f);
Chris@554 958 if (originY > 0 && originY < v->height()) {
Chris@554 959 paint.save();
Chris@554 960 paint.setPen(getPartialShades(v)[1]);
Chris@554 961 paint.drawLine(x0, originY, x1, originY);
Chris@554 962 paint.restore();
Chris@554 963 }
Chris@79 964 }
Chris@6 965
Chris@615 966 int prevFrame = 0;
Chris@615 967
Chris@0 968 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 969 i != points.end(); ++i) {
Chris@0 970
Chris@553 971 if (m_derivative && i == points.begin()) continue;
Chris@553 972
Chris@0 973 const SparseTimeValueModel::Point &p(*i);
Chris@0 974
Chris@553 975 float value = p.value;
Chris@553 976 if (m_derivative) {
Chris@553 977 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@553 978 --j;
Chris@553 979 value -= j->value;
Chris@553 980 }
Chris@553 981
Chris@44 982 int x = v->getXForFrame(p.frame);
Chris@553 983 int y = getYForValue(v, value);
Chris@0 984
Chris@615 985 bool gap = false;
Chris@615 986 if (m_plotStyle == PlotDiscreteCurves) {
Chris@615 987 gap = (p.frame > prevFrame &&
Chris@615 988 (p.frame - prevFrame >= m_model->getResolution() * 2));
Chris@615 989 }
Chris@615 990
Chris@79 991 if (m_plotStyle != PlotSegmentation) {
Chris@79 992 textY = y - paint.fontMetrics().height()
Chris@631 993 + paint.fontMetrics().ascent() - 1;
Chris@372 994 if (textY < paint.fontMetrics().ascent() + 1) {
Chris@372 995 textY = paint.fontMetrics().ascent() + 1;
Chris@372 996 }
Chris@79 997 }
Chris@79 998
Chris@34 999 bool haveNext = false;
Chris@615 1000 int nf = v->getModelsEndFrame();
Chris@615 1001 int nx = v->getXForFrame(nf);
Chris@34 1002 int ny = y;
Chris@34 1003
Chris@34 1004 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 1005 ++j;
Chris@34 1006
Chris@34 1007 if (j != points.end()) {
Chris@34 1008 const SparseTimeValueModel::Point &q(*j);
Chris@553 1009 float nvalue = q.value;
Chris@553 1010 if (m_derivative) nvalue -= p.value;
Chris@615 1011 nf = q.frame;
Chris@615 1012 nx = v->getXForFrame(nf);
Chris@553 1013 ny = getYForValue(v, nvalue);
Chris@34 1014 haveNext = true;
Chris@76 1015 }
Chris@76 1016
Chris@682 1017 // cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
Chris@682 1018 // << ", nx = " << nx << endl;
Chris@34 1019
Chris@615 1020 if (m_plotStyle == PlotDiscreteCurves) {
Chris@615 1021 paint.setPen(QPen(getBaseQColor(), 3));
Chris@615 1022 paint.setBrush(Qt::NoBrush);
Chris@615 1023 } else if (m_plotStyle == PlotSegmentation) {
Chris@287 1024 paint.setPen(getForegroundQColor(v));
Chris@553 1025 paint.setBrush(getColourForValue(v, value));
Chris@26 1026 } else if (m_plotStyle == PlotLines ||
Chris@26 1027 m_plotStyle == PlotCurve) {
Chris@615 1028 paint.setPen(getBaseQColor());
Chris@6 1029 paint.setBrush(Qt::NoBrush);
Chris@3 1030 } else {
Chris@615 1031 paint.setPen(getBaseQColor());
Chris@6 1032 paint.setBrush(brushColour);
Chris@3 1033 }
Chris@0 1034
Chris@0 1035 if (m_plotStyle == PlotStems) {
Chris@430 1036 /*
Chris@0 1037 paint.setPen(brushColour);
Chris@0 1038 if (y < origin - 1) {
Chris@0 1039 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 1040 } else if (y > origin + 1) {
Chris@0 1041 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 1042 }
Chris@430 1043 */
Chris@287 1044 paint.setPen(getBaseQColor());
Chris@430 1045 if (y < origin - 1) {
Chris@430 1046 paint.drawLine(x + w/2, y + 1, x + w/2, origin);
Chris@430 1047 } else if (y > origin + 1) {
Chris@430 1048 paint.drawLine(x + w/2, origin, x + w/2, y - 1);
Chris@430 1049 }
Chris@0 1050 }
Chris@0 1051
Chris@631 1052 bool illuminate = false;
Chris@631 1053
Chris@0 1054 if (illuminateFrame == p.frame) {
Chris@6 1055
Chris@631 1056 // not equipped to illuminate the right section in line
Chris@631 1057 // or curve mode
Chris@6 1058
Chris@6 1059 if (m_plotStyle != PlotCurve &&
Chris@615 1060 m_plotStyle != PlotDiscreteCurves &&
Chris@6 1061 m_plotStyle != PlotLines) {
Chris@631 1062 illuminate = true;
Chris@631 1063 }
Chris@631 1064 }
Chris@0 1065
Chris@6 1066 if (m_plotStyle != PlotLines &&
Chris@26 1067 m_plotStyle != PlotCurve &&
Chris@615 1068 m_plotStyle != PlotDiscreteCurves &&
Chris@26 1069 m_plotStyle != PlotSegmentation) {
Chris@631 1070 if (illuminate) {
Chris@631 1071 paint.save();
Chris@631 1072 paint.setPen(getForegroundQColor(v));
Chris@631 1073 paint.setBrush(getForegroundQColor(v));
Chris@631 1074 }
Chris@326 1075 if (m_plotStyle != PlotStems ||
Chris@326 1076 w > 1) {
Chris@326 1077 paint.drawRect(x, y - 1, w, 2);
Chris@326 1078 }
Chris@631 1079 if (illuminate) {
Chris@631 1080 paint.restore();
Chris@631 1081 }
Chris@3 1082 }
Chris@0 1083
Chris@6 1084 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 1085 m_plotStyle == PlotLines ||
Chris@615 1086 m_plotStyle == PlotDiscreteCurves ||
Chris@6 1087 m_plotStyle == PlotCurve) {
Chris@0 1088
Chris@34 1089 if (haveNext) {
Chris@3 1090
Chris@6 1091 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 1092
Chris@79 1093 paint.save();
Chris@3 1094 paint.setPen(brushColour);
Chris@3 1095 paint.drawLine(x + w, y, nx, ny);
Chris@79 1096 paint.restore();
Chris@6 1097
Chris@6 1098 } else if (m_plotStyle == PlotLines) {
Chris@430 1099
Chris@430 1100 if (pointCount == 0) {
Chris@430 1101 path.moveTo(x + w/2, y);
Chris@430 1102 }
Chris@6 1103
Chris@430 1104 // paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@430 1105 path.lineTo(nx + w/2, ny);
Chris@6 1106
Chris@3 1107 } else {
Chris@6 1108
Chris@55 1109 float x0 = x + float(w)/2;
Chris@55 1110 float x1 = nx + float(w)/2;
Chris@55 1111
Chris@55 1112 float y0 = y;
Chris@55 1113 float y1 = ny;
Chris@55 1114
Chris@615 1115 if (m_plotStyle == PlotDiscreteCurves) {
Chris@615 1116 bool nextGap = nf - p.frame >= m_model->getResolution() * 2;
Chris@615 1117 if (nextGap) {
Chris@615 1118 x1 = x0;
Chris@615 1119 y1 = y0;
Chris@615 1120 }
Chris@615 1121 }
Chris@615 1122
Chris@615 1123 if (pointCount == 0 || gap) {
Chris@55 1124 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 1125 }
Chris@6 1126
Chris@6 1127 if (nx - x > 5) {
Chris@55 1128 path.cubicTo(x0, y0,
Chris@55 1129 x0, y0,
Chris@55 1130 (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 1131
Chris@55 1132 // // or
Chris@55 1133 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 1134
Chris@6 1135 } else {
Chris@55 1136 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 1137 }
Chris@3 1138 }
Chris@0 1139 }
Chris@0 1140 }
Chris@0 1141
Chris@26 1142 if (m_plotStyle == PlotSegmentation) {
Chris@76 1143
Chris@526 1144 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1145 cerr << "drawing rect" << endl;
Chris@526 1146 #endif
Chris@26 1147
Chris@27 1148 if (nx <= x) continue;
Chris@26 1149
Chris@559 1150 paint.setPen(QPen(getForegroundQColor(v), 2));
Chris@559 1151
Chris@631 1152 if (!illuminate) {
Chris@513 1153 if (!m_drawSegmentDivisions ||
Chris@513 1154 nx < x + 5 ||
Chris@513 1155 x >= v->width() - 1) {
Chris@513 1156 paint.setPen(Qt::NoPen);
Chris@513 1157 }
Chris@27 1158 }
Chris@26 1159
Chris@44 1160 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 1161 }
Chris@26 1162
Chris@629 1163 QString label = p.label;
Chris@630 1164 bool italic = false;
Chris@629 1165
Chris@629 1166 if (label == "" &&
Chris@629 1167 (m_plotStyle == PlotPoints ||
Chris@631 1168 m_plotStyle == PlotSegmentation ||
Chris@631 1169 m_plotStyle == PlotConnectedPoints)) {
Chris@632 1170 char lc[20];
Chris@632 1171 snprintf(lc, 20, "%.3g", p.value);
Chris@632 1172 label = lc;
Chris@630 1173 italic = true;
Chris@629 1174 }
Chris@629 1175
Chris@629 1176 if (label != "") {
Chris@632 1177 bool haveRoom = nx > x + 6 + paint.fontMetrics().width(label);
Chris@632 1178 if (haveRoom ||
Chris@632 1179 (!haveNext &&
Chris@632 1180 (pointCount == 0 || !italic))) {
Chris@630 1181 v->drawVisibleText(paint, x + 5, textY, label,
Chris@630 1182 italic ?
Chris@630 1183 View::OutlinedItalicText :
Chris@630 1184 View::OutlinedText);
Chris@79 1185 }
Chris@55 1186 }
Chris@615 1187
Chris@615 1188 prevFrame = p.frame;
Chris@632 1189 ++pointCount;
Chris@0 1190 }
Chris@6 1191
Chris@691 1192 if (m_plotStyle == PlotDiscreteCurves) {
Chris@691 1193 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@691 1194 paint.drawPath(path);
Chris@691 1195 } else if ((m_plotStyle == PlotCurve || m_plotStyle == PlotLines)
Chris@691 1196 && !path.isEmpty()) {
Chris@55 1197 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
Chris@6 1198 paint.drawPath(path);
Chris@6 1199 }
Chris@6 1200
Chris@6 1201 paint.restore();
Chris@6 1202
Chris@6 1203 // looks like save/restore doesn't deal with this:
Chris@6 1204 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 1205 }
Chris@6 1206
Chris@42 1207 int
Chris@696 1208 TimeValueLayer::getVerticalScaleWidth(View *v, bool, QPainter &paint) const
Chris@42 1209 {
Chris@696 1210 int w = 0;
Chris@696 1211 if (m_verticalScale == LogScale) {
Chris@696 1212 w = LogNumericalScale().getWidth(v, paint);
Chris@696 1213 } else {
Chris@696 1214 w = LinearNumericalScale().getWidth(v, paint);
Chris@696 1215 }
Chris@68 1216 if (m_plotStyle == PlotSegmentation) return w + 20;
Chris@68 1217 else return w + 10;
Chris@42 1218 }
Chris@42 1219
Chris@42 1220 void
Chris@607 1221 TimeValueLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
Chris@42 1222 {
Chris@42 1223 if (!m_model) return;
Chris@42 1224
Chris@696 1225 /*
Chris@68 1226 int h = v->height();
Chris@68 1227
Chris@68 1228 int n = 10;
Chris@68 1229
Chris@437 1230 float min, max;
Chris@437 1231 bool logarithmic;
Chris@437 1232 getScaleExtents(v, min, max, logarithmic);
Chris@437 1233
Chris@437 1234 if (m_plotStyle == PlotSegmentation) {
Chris@437 1235 QString unit;
Chris@437 1236 getValueExtents(min, max, logarithmic, unit);
Chris@437 1237 if (logarithmic) {
Chris@437 1238 LogRange::mapRange(min, max);
Chris@437 1239 }
Chris@437 1240 }
Chris@437 1241
Chris@68 1242 float val = min;
Chris@68 1243 float inc = (max - val) / n;
Chris@42 1244
Chris@55 1245 char buffer[40];
Chris@55 1246
Chris@607 1247 int w = getVerticalScaleWidth(v, false, paint);
Chris@55 1248
Chris@68 1249 int tx = 5;
Chris@68 1250
Chris@68 1251 int boxx = 5, boxy = 5;
Chris@696 1252 if (getScaleUnits() != "") {
Chris@68 1253 boxy += paint.fontMetrics().height();
Chris@68 1254 }
Chris@68 1255 int boxw = 10, boxh = h - boxy - 5;
Chris@68 1256
Chris@68 1257 if (m_plotStyle == PlotSegmentation) {
Chris@68 1258 tx += boxx + boxw;
Chris@68 1259 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@68 1260 }
Chris@68 1261
Chris@68 1262 if (m_plotStyle == PlotSegmentation) {
Chris@68 1263 paint.save();
Chris@68 1264 for (int y = 0; y < boxh; ++y) {
Chris@68 1265 float val = ((boxh - y) * (max - min)) / boxh + min;
Chris@437 1266 if (logarithmic) {
Chris@437 1267 paint.setPen(getColourForValue(v, LogRange::unmap(val)));
Chris@437 1268 } else {
Chris@437 1269 paint.setPen(getColourForValue(v, val));
Chris@437 1270 }
Chris@68 1271 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@68 1272 }
Chris@68 1273 paint.restore();
Chris@68 1274 }
Chris@434 1275
Chris@434 1276 float round = 1.f;
Chris@434 1277 int dp = 0;
Chris@434 1278 if (inc > 0) {
Chris@434 1279 int prec = trunc(log10f(inc));
Chris@434 1280 prec -= 1;
Chris@434 1281 if (prec < 0) dp = -prec;
Chris@434 1282 round = powf(10.f, prec);
Chris@526 1283 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1284 cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
Chris@526 1285 #endif
Chris@434 1286 }
Chris@68 1287
Chris@434 1288 int prevy = -1;
Chris@434 1289
Chris@68 1290 for (int i = 0; i < n; ++i) {
Chris@68 1291
Chris@68 1292 int y, ty;
Chris@68 1293 bool drawText = true;
Chris@68 1294
Chris@434 1295 float dispval = val;
Chris@434 1296
Chris@68 1297 if (m_plotStyle == PlotSegmentation) {
Chris@68 1298 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@68 1299 ty = y;
Chris@68 1300 } else {
Chris@437 1301 if (i == n-1 &&
Chris@437 1302 v->height() < paint.fontMetrics().height() * (n*2)) {
Chris@696 1303 if (getScaleUnits() != "") drawText = false;
Chris@68 1304 }
Chris@434 1305 dispval = lrintf(val / round) * round;
Chris@526 1306 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1307 cerr << "val = " << val << ", dispval = " << dispval << endl;
Chris@526 1308 #endif
Chris@437 1309 if (logarithmic) {
Chris@437 1310 y = getYForValue(v, LogRange::unmap(dispval));
Chris@437 1311 } else {
Chris@437 1312 y = getYForValue(v, dispval);
Chris@437 1313 }
Chris@68 1314 ty = y - paint.fontMetrics().height() +
Chris@434 1315 paint.fontMetrics().ascent() + 2;
Chris@434 1316
Chris@434 1317 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@434 1318 val += inc;
Chris@434 1319 continue;
Chris@434 1320 }
Chris@68 1321 }
Chris@68 1322
Chris@437 1323 if (logarithmic) {
Chris@665 1324 double dv = LogRange::unmap(dispval);
Chris@665 1325 int digits = trunc(log10f(dv));
Chris@665 1326 int sf = dp + (digits > 0 ? digits : 0);
Chris@665 1327 if (sf < 2) sf = 2;
Chris@665 1328 sprintf(buffer, "%.*g", sf, dv);
Chris@437 1329 } else {
Chris@437 1330 sprintf(buffer, "%.*f", dp, dispval);
Chris@437 1331 }
Chris@55 1332 QString label = QString(buffer);
Chris@68 1333
Chris@68 1334 if (m_plotStyle != PlotSegmentation) {
Chris@68 1335 paint.drawLine(w - 5, y, w, y);
Chris@68 1336 } else {
Chris@68 1337 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@68 1338 }
Chris@68 1339
Chris@434 1340 if (drawText) {
Chris@435 1341 if (m_plotStyle != PlotSegmentation) {
Chris@435 1342 paint.drawText(tx + w - paint.fontMetrics().width(label) - 8,
Chris@435 1343 ty, label);
Chris@435 1344 } else {
Chris@435 1345 paint.drawText(tx, ty, label);
Chris@435 1346 }
Chris@434 1347 }
Chris@434 1348
Chris@434 1349 prevy = y;
Chris@44 1350 val += inc;
Chris@42 1351 }
Chris@696 1352 */
Chris@696 1353 float min, max;
Chris@696 1354 bool logarithmic;
Chris@696 1355 getScaleExtents(v, min, max, logarithmic);
Chris@696 1356
Chris@696 1357 int w = getVerticalScaleWidth(v, false, paint);
Chris@696 1358 int h = v->height();
Chris@696 1359
Chris@696 1360 if (m_plotStyle == PlotSegmentation) {
Chris@696 1361
Chris@696 1362 //!!! todo!
Chris@696 1363
Chris@696 1364 } else {
Chris@696 1365
Chris@696 1366 if (logarithmic) {
Chris@696 1367 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@696 1368 } else {
Chris@696 1369 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@696 1370 }
Chris@696 1371
Chris@696 1372 if (logarithmic && (getScaleUnits() == "Hz")) {
Chris@696 1373 PianoScale().paintPianoVertical
Chris@696 1374 (v, paint, QRect(w - 10, 0, 10, h),
Chris@696 1375 LogRange::unmap(min),
Chris@696 1376 LogRange::unmap(max));
Chris@696 1377 paint.drawLine(w, 0, w, h);
Chris@696 1378 }
Chris@696 1379 }
Chris@696 1380
Chris@696 1381 if (getScaleUnits() != "") {
Chris@67 1382 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
Chris@696 1383 getScaleUnits());
Chris@691 1384 }
Chris@42 1385 }
Chris@42 1386
Chris@21 1387 void
Chris@44 1388 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 1389 {
Chris@526 1390 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1391 cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@526 1392 #endif
Chris@21 1393
Chris@21 1394 if (!m_model) return;
Chris@21 1395
Chris@44 1396 long frame = v->getFrameForX(e->x());
Chris@76 1397 long resolution = m_model->getResolution();
Chris@21 1398 if (frame < 0) frame = 0;
Chris@76 1399 frame = (frame / resolution) * resolution;
Chris@21 1400
Chris@44 1401 float value = getValueForY(v, e->y());
Chris@21 1402
Chris@76 1403 bool havePoint = false;
Chris@76 1404
Chris@76 1405 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 1406 if (!points.empty()) {
Chris@76 1407 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1408 i != points.end(); ++i) {
Chris@76 1409 if (((i->frame / resolution) * resolution) != frame) {
Chris@526 1410 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1411 cerr << "ignoring out-of-range frame at " << i->frame << endl;
Chris@526 1412 #endif
Chris@76 1413 continue;
Chris@76 1414 }
Chris@76 1415 m_editingPoint = *i;
Chris@76 1416 havePoint = true;
Chris@76 1417 }
Chris@76 1418 }
Chris@76 1419
Chris@76 1420 if (!havePoint) {
Chris@76 1421 m_editingPoint = SparseTimeValueModel::Point
Chris@76 1422 (frame, value, tr("New Point"));
Chris@76 1423 }
Chris@76 1424
Chris@23 1425 m_originalPoint = m_editingPoint;
Chris@22 1426
Chris@376 1427 if (m_editingCommand) finish(m_editingCommand);
Chris@22 1428 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 1429 tr("Draw Point"));
Chris@76 1430 if (!havePoint) {
Chris@76 1431 m_editingCommand->addPoint(m_editingPoint);
Chris@76 1432 }
Chris@22 1433
Chris@21 1434 m_editing = true;
Chris@21 1435 }
Chris@21 1436
Chris@21 1437 void
Chris@44 1438 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 1439 {
Chris@526 1440 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1441 cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@526 1442 #endif
Chris@21 1443
Chris@21 1444 if (!m_model || !m_editing) return;
Chris@21 1445
Chris@44 1446 long frame = v->getFrameForX(e->x());
Chris@76 1447 long resolution = m_model->getResolution();
Chris@21 1448 if (frame < 0) frame = 0;
Chris@76 1449 frame = (frame / resolution) * resolution;
Chris@21 1450
Chris@44 1451 float value = getValueForY(v, e->y());
Chris@21 1452
Chris@76 1453 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 1454
Chris@526 1455 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1456 cerr << points.size() << " points" << endl;
Chris@526 1457 #endif
Chris@76 1458
Chris@76 1459 bool havePoint = false;
Chris@76 1460
Chris@76 1461 if (!points.empty()) {
Chris@76 1462 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1463 i != points.end(); ++i) {
Chris@76 1464 if (i->frame == m_editingPoint.frame &&
Chris@76 1465 i->value == m_editingPoint.value) {
Chris@526 1466 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1467 cerr << "ignoring current editing point at " << i->frame << ", " << i->value << endl;
Chris@526 1468 #endif
Chris@76 1469 continue;
Chris@76 1470 }
Chris@76 1471 if (((i->frame / resolution) * resolution) != frame) {
Chris@526 1472 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1473 cerr << "ignoring out-of-range frame at " << i->frame << endl;
Chris@526 1474 #endif
Chris@76 1475 continue;
Chris@76 1476 }
Chris@526 1477 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1478 cerr << "adjusting to new point at " << i->frame << ", " << i->value << endl;
Chris@526 1479 #endif
Chris@76 1480 m_editingPoint = *i;
Chris@76 1481 m_originalPoint = m_editingPoint;
Chris@76 1482 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 1483 havePoint = true;
Chris@76 1484 }
Chris@76 1485 }
Chris@76 1486
Chris@76 1487 if (!havePoint) {
Chris@76 1488 if (frame == m_editingPoint.frame) {
Chris@76 1489 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 1490 }
Chris@76 1491 }
Chris@76 1492
Chris@76 1493 // m_editingCommand->deletePoint(m_editingPoint);
Chris@21 1494 m_editingPoint.frame = frame;
Chris@21 1495 m_editingPoint.value = value;
Chris@22 1496 m_editingCommand->addPoint(m_editingPoint);
Chris@21 1497 }
Chris@21 1498
Chris@21 1499 void
Chris@248 1500 TimeValueLayer::drawEnd(View *, QMouseEvent *)
Chris@21 1501 {
Chris@526 1502 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1503 cerr << "TimeValueLayer::drawEnd" << endl;
Chris@526 1504 #endif
Chris@21 1505 if (!m_model || !m_editing) return;
Chris@376 1506 finish(m_editingCommand);
Chris@22 1507 m_editingCommand = 0;
Chris@21 1508 m_editing = false;
Chris@21 1509 }
Chris@21 1510
Chris@21 1511 void
Chris@335 1512 TimeValueLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 1513 {
Chris@335 1514 if (!m_model) return;
Chris@335 1515
Chris@335 1516 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@335 1517 if (points.empty()) return;
Chris@335 1518
Chris@335 1519 m_editingPoint = *points.begin();
Chris@335 1520
Chris@335 1521 if (m_editingCommand) {
Chris@376 1522 finish(m_editingCommand);
Chris@335 1523 m_editingCommand = 0;
Chris@335 1524 }
Chris@335 1525
Chris@335 1526 m_editing = true;
Chris@335 1527 }
Chris@335 1528
Chris@335 1529 void
Chris@335 1530 TimeValueLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 1531 {
Chris@335 1532 }
Chris@335 1533
Chris@335 1534 void
Chris@335 1535 TimeValueLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 1536 {
Chris@335 1537 if (!m_model || !m_editing) return;
Chris@335 1538
Chris@335 1539 m_editing = false;
Chris@335 1540
Chris@335 1541 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@335 1542 if (points.empty()) return;
Chris@335 1543 if (points.begin()->frame != m_editingPoint.frame ||
Chris@335 1544 points.begin()->value != m_editingPoint.value) return;
Chris@335 1545
Chris@335 1546 m_editingCommand = new SparseTimeValueModel::EditCommand
Chris@335 1547 (m_model, tr("Erase Point"));
Chris@335 1548
Chris@335 1549 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 1550
Chris@376 1551 finish(m_editingCommand);
Chris@335 1552 m_editingCommand = 0;
Chris@335 1553 m_editing = false;
Chris@335 1554 }
Chris@335 1555
Chris@335 1556 void
Chris@44 1557 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 1558 {
Chris@526 1559 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1560 cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@526 1561 #endif
Chris@21 1562
Chris@21 1563 if (!m_model) return;
Chris@21 1564
Chris@44 1565 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 1566 if (points.empty()) return;
Chris@21 1567
Chris@21 1568 m_editingPoint = *points.begin();
Chris@23 1569 m_originalPoint = m_editingPoint;
Chris@22 1570
Chris@22 1571 if (m_editingCommand) {
Chris@376 1572 finish(m_editingCommand);
Chris@22 1573 m_editingCommand = 0;
Chris@22 1574 }
Chris@22 1575
Chris@21 1576 m_editing = true;
Chris@21 1577 }
Chris@21 1578
Chris@21 1579 void
Chris@44 1580 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 1581 {
Chris@526 1582 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1583 cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@526 1584 #endif
Chris@21 1585
Chris@21 1586 if (!m_model || !m_editing) return;
Chris@21 1587
Chris@44 1588 long frame = v->getFrameForX(e->x());
Chris@21 1589 if (frame < 0) frame = 0;
Chris@21 1590 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 1591
Chris@44 1592 float value = getValueForY(v, e->y());
Chris@21 1593
Chris@22 1594 if (!m_editingCommand) {
Chris@22 1595 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 1596 tr("Drag Point"));
Chris@22 1597 }
Chris@22 1598
Chris@22 1599 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 1600 m_editingPoint.frame = frame;
Chris@21 1601 m_editingPoint.value = value;
Chris@22 1602 m_editingCommand->addPoint(m_editingPoint);
Chris@21 1603 }
Chris@21 1604
Chris@21 1605 void
Chris@248 1606 TimeValueLayer::editEnd(View *, QMouseEvent *)
Chris@21 1607 {
Chris@526 1608 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1609 cerr << "TimeValueLayer::editEnd" << endl;
Chris@526 1610 #endif
Chris@21 1611 if (!m_model || !m_editing) return;
Chris@23 1612
Chris@23 1613 if (m_editingCommand) {
Chris@23 1614
Chris@23 1615 QString newName = m_editingCommand->getName();
Chris@23 1616
Chris@23 1617 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 1618 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 1619 newName = tr("Edit Point");
Chris@23 1620 } else {
Chris@23 1621 newName = tr("Relocate Point");
Chris@23 1622 }
Chris@23 1623 } else {
Chris@23 1624 newName = tr("Change Point Value");
Chris@23 1625 }
Chris@23 1626
Chris@23 1627 m_editingCommand->setName(newName);
Chris@376 1628 finish(m_editingCommand);
Chris@23 1629 }
Chris@23 1630
Chris@22 1631 m_editingCommand = 0;
Chris@21 1632 m_editing = false;
Chris@21 1633 }
Chris@21 1634
Chris@255 1635 bool
Chris@70 1636 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1637 {
Chris@255 1638 if (!m_model) return false;
Chris@70 1639
Chris@70 1640 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@255 1641 if (points.empty()) return false;
Chris@70 1642
Chris@70 1643 SparseTimeValueModel::Point point = *points.begin();
Chris@70 1644
Chris@70 1645 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1646 (m_model->getSampleRate(),
Chris@70 1647 ItemEditDialog::ShowTime |
Chris@70 1648 ItemEditDialog::ShowValue |
Chris@73 1649 ItemEditDialog::ShowText,
Chris@696 1650 getScaleUnits());
Chris@70 1651
Chris@70 1652 dialog->setFrameTime(point.frame);
Chris@70 1653 dialog->setValue(point.value);
Chris@70 1654 dialog->setText(point.label);
Chris@70 1655
Chris@70 1656 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1657
Chris@70 1658 SparseTimeValueModel::Point newPoint = point;
Chris@70 1659 newPoint.frame = dialog->getFrameTime();
Chris@70 1660 newPoint.value = dialog->getValue();
Chris@70 1661 newPoint.label = dialog->getText();
Chris@70 1662
Chris@70 1663 SparseTimeValueModel::EditCommand *command =
Chris@70 1664 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
Chris@70 1665 command->deletePoint(point);
Chris@70 1666 command->addPoint(newPoint);
Chris@376 1667 finish(command);
Chris@70 1668 }
Chris@70 1669
Chris@70 1670 delete dialog;
Chris@255 1671 return true;
Chris@70 1672 }
Chris@70 1673
Chris@70 1674 void
Chris@43 1675 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1676 {
Chris@99 1677 if (!m_model) return;
Chris@99 1678
Chris@43 1679 SparseTimeValueModel::EditCommand *command =
Chris@43 1680 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1681 tr("Drag Selection"));
Chris@43 1682
Chris@43 1683 SparseTimeValueModel::PointList points =
Chris@43 1684 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1685
Chris@43 1686 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1687 i != points.end(); ++i) {
Chris@43 1688
Chris@43 1689 if (s.contains(i->frame)) {
Chris@43 1690 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1691 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1692 command->deletePoint(*i);
Chris@43 1693 command->addPoint(newPoint);
Chris@43 1694 }
Chris@43 1695 }
Chris@43 1696
Chris@376 1697 finish(command);
Chris@43 1698 }
Chris@43 1699
Chris@43 1700 void
Chris@43 1701 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1702 {
Chris@99 1703 if (!m_model) return;
Chris@99 1704
Chris@43 1705 SparseTimeValueModel::EditCommand *command =
Chris@43 1706 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1707 tr("Resize Selection"));
Chris@43 1708
Chris@43 1709 SparseTimeValueModel::PointList points =
Chris@43 1710 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1711
Chris@43 1712 double ratio =
Chris@43 1713 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1714 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1715
Chris@43 1716 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1717 i != points.end(); ++i) {
Chris@43 1718
Chris@43 1719 if (s.contains(i->frame)) {
Chris@43 1720
Chris@43 1721 double target = i->frame;
Chris@43 1722 target = newSize.getStartFrame() +
Chris@43 1723 double(target - s.getStartFrame()) * ratio;
Chris@43 1724
Chris@43 1725 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1726 newPoint.frame = lrint(target);
Chris@43 1727 command->deletePoint(*i);
Chris@43 1728 command->addPoint(newPoint);
Chris@43 1729 }
Chris@43 1730 }
Chris@43 1731
Chris@376 1732 finish(command);
Chris@43 1733 }
Chris@43 1734
Chris@76 1735 void
Chris@76 1736 TimeValueLayer::deleteSelection(Selection s)
Chris@76 1737 {
Chris@99 1738 if (!m_model) return;
Chris@99 1739
Chris@76 1740 SparseTimeValueModel::EditCommand *command =
Chris@76 1741 new SparseTimeValueModel::EditCommand(m_model,
Chris@76 1742 tr("Delete Selected Points"));
Chris@76 1743
Chris@76 1744 SparseTimeValueModel::PointList points =
Chris@76 1745 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1746
Chris@76 1747 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1748 i != points.end(); ++i) {
Chris@76 1749
Chris@76 1750 if (s.contains(i->frame)) {
Chris@76 1751 command->deletePoint(*i);
Chris@76 1752 }
Chris@76 1753 }
Chris@76 1754
Chris@376 1755 finish(command);
Chris@76 1756 }
Chris@76 1757
Chris@76 1758 void
Chris@359 1759 TimeValueLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1760 {
Chris@99 1761 if (!m_model) return;
Chris@99 1762
Chris@76 1763 SparseTimeValueModel::PointList points =
Chris@76 1764 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1765
Chris@76 1766 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1767 i != points.end(); ++i) {
Chris@76 1768 if (s.contains(i->frame)) {
Chris@83 1769 Clipboard::Point point(i->frame, i->value, i->label);
Chris@360 1770 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1771 to.addPoint(point);
Chris@76 1772 }
Chris@76 1773 }
Chris@76 1774 }
Chris@76 1775
Chris@125 1776 bool
Chris@359 1777 TimeValueLayer::paste(View *v, const Clipboard &from, int frameOffset,
Chris@125 1778 bool interactive)
Chris@76 1779 {
Chris@125 1780 if (!m_model) return false;
Chris@99 1781
Chris@76 1782 const Clipboard::PointList &points = from.getPoints();
Chris@76 1783
Chris@360 1784 bool realign = false;
Chris@360 1785
Chris@360 1786 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1787
Chris@360 1788 QMessageBox::StandardButton button =
Chris@360 1789 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1790 tr("The items you are pasting came from a layer with different source material from this one. Do you want to re-align them in time, to match the source material for this layer?"),
Chris@360 1791 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1792 QMessageBox::Yes);
Chris@360 1793
Chris@360 1794 if (button == QMessageBox::Cancel) {
Chris@360 1795 return false;
Chris@360 1796 }
Chris@360 1797
Chris@360 1798 if (button == QMessageBox::Yes) {
Chris@360 1799 realign = true;
Chris@360 1800 }
Chris@360 1801 }
Chris@360 1802
Chris@76 1803 SparseTimeValueModel::EditCommand *command =
Chris@76 1804 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
Chris@76 1805
Chris@125 1806 enum ValueAvailability {
Chris@125 1807 UnknownAvailability,
Chris@125 1808 NoValues,
Chris@125 1809 SomeValues,
Chris@125 1810 AllValues
Chris@125 1811 };
Chris@125 1812
Chris@340 1813 Labeller::ValueType generation = Labeller::ValueNone;
Chris@125 1814
Chris@125 1815 bool haveUsableLabels = false;
Chris@125 1816 bool haveExistingItems = !(m_model->isEmpty());
Chris@340 1817 Labeller labeller;
Chris@340 1818 labeller.setSampleRate(m_model->getSampleRate());
Chris@125 1819
Chris@125 1820 if (interactive) {
Chris@125 1821
Chris@125 1822 ValueAvailability availability = UnknownAvailability;
Chris@125 1823
Chris@125 1824 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@125 1825 i != points.end(); ++i) {
Chris@125 1826
Chris@125 1827 if (!i->haveFrame()) continue;
Chris@125 1828
Chris@125 1829 if (availability == UnknownAvailability) {
Chris@125 1830 if (i->haveValue()) availability = AllValues;
Chris@125 1831 else availability = NoValues;
Chris@125 1832 continue;
Chris@125 1833 }
Chris@125 1834
Chris@125 1835 if (i->haveValue()) {
Chris@125 1836 if (availability == NoValues) {
Chris@125 1837 availability = SomeValues;
Chris@125 1838 }
Chris@125 1839 } else {
Chris@125 1840 if (availability == AllValues) {
Chris@125 1841 availability = SomeValues;
Chris@125 1842 }
Chris@125 1843 }
Chris@125 1844
Chris@125 1845 if (!haveUsableLabels) {
Chris@125 1846 if (i->haveLabel()) {
Chris@125 1847 if (i->getLabel().contains(QRegExp("[0-9]"))) {
Chris@125 1848 haveUsableLabels = true;
Chris@125 1849 }
Chris@125 1850 }
Chris@125 1851 }
Chris@125 1852
Chris@125 1853 if (availability == SomeValues && haveUsableLabels) break;
Chris@125 1854 }
Chris@125 1855
Chris@125 1856 if (availability == NoValues || availability == SomeValues) {
Chris@125 1857
Chris@125 1858 QString text;
Chris@125 1859 if (availability == NoValues) {
Chris@125 1860 text = tr("The items you are pasting do not have values.\nWhat values do you want to use for these items?");
Chris@125 1861 } else {
Chris@125 1862 text = tr("Some of the items you are pasting do not have values.\nWhat values do you want to use for these items?");
Chris@125 1863 }
Chris@125 1864
Chris@340 1865 Labeller::TypeNameMap names = labeller.getTypeNames();
Chris@340 1866
Chris@125 1867 QStringList options;
Chris@340 1868 std::vector<Labeller::ValueType> genopts;
Chris@125 1869
Chris@340 1870 for (Labeller::TypeNameMap::const_iterator i = names.begin();
Chris@340 1871 i != names.end(); ++i) {
Chris@340 1872 if (i->first == Labeller::ValueNone) options << tr("Zero for all items");
Chris@340 1873 else options << i->second;
Chris@340 1874 genopts.push_back(i->first);
Chris@125 1875 }
Chris@125 1876
Chris@125 1877 static int prevSelection = 0;
Chris@125 1878
Chris@125 1879 bool ok = false;
Chris@125 1880 QString selected = ListInputDialog::getItem
Chris@125 1881 (0, tr("Choose value calculation"),
Chris@125 1882 text, options, prevSelection, &ok);
Chris@125 1883
Chris@125 1884 if (!ok) return false;
Chris@125 1885 int selection = 0;
Chris@340 1886 generation = Labeller::ValueNone;
Chris@125 1887
Chris@125 1888 for (QStringList::const_iterator i = options.begin();
Chris@125 1889 i != options.end(); ++i) {
Chris@125 1890 if (selected == *i) {
Chris@340 1891 generation = genopts[selection];
Chris@125 1892 break;
Chris@125 1893 }
Chris@125 1894 ++selection;
Chris@125 1895 }
Chris@340 1896
Chris@340 1897 labeller.setType(generation);
Chris@340 1898
Chris@340 1899 if (generation == Labeller::ValueFromCyclicalCounter ||
Chris@340 1900 generation == Labeller::ValueFromTwoLevelCounter) {
Chris@616 1901 int cycleSize = QInputDialog::getInt
Chris@340 1902 (0, tr("Select cycle size"),
Chris@340 1903 tr("Cycle size:"), 4, 2, 16, 1);
Chris@340 1904 labeller.setCounterCycleSize(cycleSize);
Chris@340 1905 }
Chris@125 1906
Chris@125 1907 prevSelection = selection;
Chris@125 1908 }
Chris@125 1909 }
Chris@125 1910
Chris@372 1911 SparseTimeValueModel::Point prevPoint(0);
Chris@125 1912
Chris@76 1913 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1914 i != points.end(); ++i) {
Chris@76 1915
Chris@76 1916 if (!i->haveFrame()) continue;
Chris@360 1917
Chris@76 1918 size_t frame = 0;
Chris@360 1919
Chris@360 1920 if (!realign) {
Chris@360 1921
Chris@360 1922 frame = i->getFrame();
Chris@360 1923
Chris@360 1924 } else {
Chris@360 1925
Chris@360 1926 if (i->haveReferenceFrame()) {
Chris@360 1927 frame = i->getReferenceFrame();
Chris@360 1928 frame = alignFromReference(v, frame);
Chris@360 1929 } else {
Chris@360 1930 frame = i->getFrame();
Chris@360 1931 }
Chris@76 1932 }
Chris@360 1933
Chris@76 1934 SparseTimeValueModel::Point newPoint(frame);
Chris@76 1935
Chris@125 1936 if (i->haveLabel()) {
Chris@125 1937 newPoint.label = i->getLabel();
Chris@125 1938 } else if (i->haveValue()) {
Chris@125 1939 newPoint.label = QString("%1").arg(i->getValue());
Chris@125 1940 }
Chris@125 1941
Chris@372 1942 bool usePrev = false;
Chris@372 1943 SparseTimeValueModel::Point formerPrevPoint = prevPoint;
Chris@372 1944
Chris@125 1945 if (i->haveValue()) {
Chris@125 1946 newPoint.value = i->getValue();
Chris@125 1947 } else {
Chris@526 1948 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1949 cerr << "Setting value on point at " << newPoint.frame << " from labeller";
Chris@526 1950 if (i == points.begin()) {
Chris@682 1951 cerr << ", no prev point" << endl;
Chris@526 1952 } else {
Chris@682 1953 cerr << ", prev point is at " << prevPoint.frame << endl;
Chris@526 1954 }
Chris@526 1955 #endif
Chris@340 1956 labeller.setValue<SparseTimeValueModel::Point>
Chris@371 1957 (newPoint, (i == points.begin()) ? 0 : &prevPoint);
Chris@526 1958 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 1959 cerr << "New point value = " << newPoint.value << endl;
Chris@526 1960 #endif
Chris@372 1961 if (labeller.actingOnPrevPoint() && i != points.begin()) {
Chris@372 1962 usePrev = true;
Chris@372 1963 }
Chris@372 1964 }
Chris@372 1965
Chris@372 1966 if (usePrev) {
Chris@372 1967 command->deletePoint(formerPrevPoint);
Chris@372 1968 command->addPoint(prevPoint);
Chris@340 1969 }
Chris@125 1970
Chris@340 1971 prevPoint = newPoint;
Chris@76 1972 command->addPoint(newPoint);
Chris@76 1973 }
Chris@76 1974
Chris@376 1975 finish(command);
Chris@125 1976 return true;
Chris@360 1977 }
Chris@76 1978
Chris@316 1979 void
Chris@316 1980 TimeValueLayer::toXml(QTextStream &stream,
Chris@316 1981 QString indent, QString extraAttributes) const
Chris@6 1982 {
Chris@316 1983 SingleColourLayer::toXml(stream, indent,
Chris@316 1984 extraAttributes +
Chris@553 1985 QString(" colourMap=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\" scaleMinimum=\"%4\" scaleMaximum=\"%5\" drawDivisions=\"%6\" derivative=\"%7\" ")
Chris@316 1986 .arg(m_colourMap)
Chris@316 1987 .arg(m_plotStyle)
Chris@445 1988 .arg(m_verticalScale)
Chris@445 1989 .arg(m_scaleMinimum)
Chris@513 1990 .arg(m_scaleMaximum)
Chris@553 1991 .arg(m_drawSegmentDivisions ? "true" : "false")
Chris@553 1992 .arg(m_derivative ? "true" : "false"));
Chris@0 1993 }
Chris@0 1994
Chris@11 1995 void
Chris@11 1996 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1997 {
Chris@287 1998 SingleColourLayer::setProperties(attributes);
Chris@11 1999
Chris@445 2000 bool ok, alsoOk;
Chris@287 2001
Chris@287 2002 int cmap = attributes.value("colourMap").toInt(&ok);
Chris@287 2003 if (ok) setFillColourMap(cmap);
Chris@287 2004
Chris@11 2005 PlotStyle style = (PlotStyle)
Chris@11 2006 attributes.value("plotStyle").toInt(&ok);
Chris@11 2007 if (ok) setPlotStyle(style);
Chris@286 2008
Chris@286 2009 VerticalScale scale = (VerticalScale)
Chris@286 2010 attributes.value("verticalScale").toInt(&ok);
Chris@286 2011 if (ok) setVerticalScale(scale);
Chris@445 2012
Chris@513 2013 bool draw = (attributes.value("drawDivisions").trimmed() == "true");
Chris@513 2014 setDrawSegmentDivisions(draw);
Chris@513 2015
Chris@553 2016 bool derivative = (attributes.value("derivative").trimmed() == "true");
Chris@553 2017 setShowDerivative(derivative);
Chris@553 2018
Chris@445 2019 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 2020 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@526 2021 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@682 2022 cerr << "from properties: min = " << min << ", max = " << max << endl;
Chris@526 2023 #endif
Chris@526 2024 if (ok && alsoOk && min != max) setDisplayExtents(min, max);
Chris@11 2025 }
Chris@11 2026