annotate layer/TimeValueLayer.cpp @ 1429:8a7c82282fbc single-point

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