annotate layer/TimeValueLayer.cpp @ 698:ad7623c39396

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