annotate layer/TimeValueLayer.cpp @ 1473:886c1cd48f9d by-id

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