annotate layer/TimeValueLayer.cpp @ 631:f69e08be3379

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