annotate layer/TimeValueLayer.cpp @ 513:1341e95eeae9

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