annotate layer/TimeValueLayer.cpp @ 699:1a1448f7beb2

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