annotate layer/TimeValueLayer.cpp @ 515:ff1dc4f302bd sv-v1.5pre1

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