annotate layer/TimeValueLayer.cpp @ 697:26b1ffe412f9

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