annotate layer/TimeValueLayer.cpp @ 629:355fa700ce70

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