annotate layer/TimeValueLayer.cpp @ 615:156a120345ae

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