annotate layer/TimeValueLayer.cpp @ 1470:696e569ff21b by-id

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