annotate layer/TimeValueLayer.cpp @ 1487:c3cc36c014b8

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