annotate layer/TimeValueLayer.cpp @ 664:62330d542e1d

Do not pad edges of scale extent when auto-aligning to another layer
author Chris Cannam
date Tue, 16 Jul 2013 13:22:31 +0100
parents c6d705bf1672
children ab9fefdaa103
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@59 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "TimeValueLayer.h"
Chris@0 17
Chris@128 18 #include "data/model/Model.h"
Chris@0 19 #include "base/RealTime.h"
Chris@0 20 #include "base/Profiler.h"
Chris@197 21 #include "base/LogRange.h"
Chris@437 22 #include "base/RangeMapper.h"
Chris@376 23 #include "ColourDatabase.h"
Chris@128 24 #include "view/View.h"
Chris@0 25
Chris@128 26 #include "data/model/SparseTimeValueModel.h"
Chris@340 27 #include "data/model/Labeller.h"
Chris@0 28
Chris@70 29 #include "widgets/ItemEditDialog.h"
Chris@125 30 #include "widgets/ListInputDialog.h"
Chris@70 31
Chris@376 32 #include "ColourMapper.h"
Chris@66 33
Chris@0 34 #include <QPainter>
Chris@6 35 #include <QPainterPath>
Chris@21 36 #include <QMouseEvent>
Chris@125 37 #include <QRegExp>
Chris@316 38 #include <QTextStream>
Chris@360 39 #include <QMessageBox>
Chris@340 40 #include <QInputDialog>
Chris@0 41
Chris@0 42 #include <iostream>
Chris@0 43 #include <cmath>
Chris@0 44
Chris@526 45 //#define DEBUG_TIME_VALUE_LAYER 1
Chris@526 46
Chris@44 47 TimeValueLayer::TimeValueLayer() :
Chris@287 48 SingleColourLayer(),
Chris@0 49 m_model(0),
Chris@21 50 m_editing(false),
Chris@23 51 m_originalPoint(0, 0.0, tr("New Point")),
Chris@21 52 m_editingPoint(0, 0.0, tr("New Point")),
Chris@22 53 m_editingCommand(0),
Chris@197 54 m_colourMap(0),
Chris@66 55 m_plotStyle(PlotConnectedPoints),
Chris@437 56 m_verticalScale(AutoAlignScale),
Chris@513 57 m_drawSegmentDivisions(true),
Chris@553 58 m_derivative(false),
Chris@437 59 m_scaleMinimum(0),
Chris@437 60 m_scaleMaximum(0)
Chris@0 61 {
Chris@44 62
Chris@0 63 }
Chris@0 64
Chris@0 65 void
Chris@0 66 TimeValueLayer::setModel(SparseTimeValueModel *model)
Chris@0 67 {
Chris@0 68 if (m_model == model) return;
Chris@0 69 m_model = model;
Chris@0 70
Chris@320 71 connectSignals(m_model);
Chris@0 72
Chris@438 73 m_scaleMinimum = 0;
Chris@438 74 m_scaleMaximum = 0;
Chris@438 75
Chris@494 76 if (m_model && m_model->getRDFTypeURI().endsWith("Segment")) {
Chris@494 77 setPlotStyle(PlotSegmentation);
Chris@494 78 }
Chris@494 79 if (m_model && m_model->getRDFTypeURI().endsWith("Change")) {
Chris@494 80 setPlotStyle(PlotSegmentation);
Chris@494 81 }
Chris@494 82
Chris@526 83 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@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@664 749 bool haveAutoAlignment = false;
Chris@664 750
Chris@296 751 if (shouldAutoAlign()) {
Chris@101 752
Chris@101 753 if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) {
Chris@101 754 min = m_model->getValueMinimum();
Chris@101 755 max = m_model->getValueMaximum();
Chris@664 756 } else {
Chris@664 757 haveAutoAlignment = true;
Chris@664 758 if (log) {
Chris@664 759 LogRange::mapRange(min, max);
Chris@664 760 }
Chris@101 761 }
Chris@101 762
Chris@101 763 } else if (m_verticalScale == PlusMinusOneScale) {
Chris@101 764
Chris@101 765 min = -1.0;
Chris@101 766 max = 1.0;
Chris@101 767
Chris@101 768 } else {
Chris@101 769
Chris@437 770 getDisplayExtents(min, max);
Chris@632 771
Chris@101 772 if (m_verticalScale == LogScale) {
Chris@197 773 LogRange::mapRange(min, max);
Chris@101 774 log = true;
Chris@101 775 }
Chris@101 776 }
Chris@101 777
Chris@629 778 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 779 std::cerr << "TimeValueLayer::getScaleExtents: min = " << min << ", max = " << max << std::endl;
Chris@629 780 #endif
Chris@629 781
Chris@664 782 if (!haveAutoAlignment) {
Chris@664 783
Chris@664 784 if (max == min) {
Chris@664 785 max = max + 0.5;
Chris@664 786 min = min - 0.5;
Chris@664 787 } else {
Chris@664 788 float margin = (max - min) / 10.0;
Chris@664 789 max = max + margin;
Chris@664 790 min = min - margin;
Chris@664 791 }
Chris@629 792
Chris@629 793 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@664 794 std::cerr << "TimeValueLayer::getScaleExtents: min = " << min << ", max = " << max << " (after adjustment)" << std::endl;
Chris@629 795 #endif
Chris@664 796 }
Chris@101 797 }
Chris@101 798
Chris@21 799 int
Chris@66 800 TimeValueLayer::getYForValue(View *v, float val) const
Chris@21 801 {
Chris@79 802 float min = 0.0, max = 0.0;
Chris@101 803 bool logarithmic = false;
Chris@79 804 int h = v->height();
Chris@79 805
Chris@101 806 getScaleExtents(v, min, max, logarithmic);
Chris@101 807
Chris@526 808 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@526 809 std::cerr << "getYForValue(" << val << "): min " << min << ", max "
Chris@526 810 << max << ", log " << logarithmic << std::endl;
Chris@526 811 #endif
Chris@101 812
Chris@101 813 if (logarithmic) {
Chris@197 814 val = LogRange::map(val);
Chris@79 815 }
Chris@79 816
Chris@66 817 return int(h - ((val - min) * h) / (max - min));
Chris@21 818 }
Chris@21 819
Chris@21 820 float
Chris@44 821 TimeValueLayer::getValueForY(View *v, int y) const
Chris@21 822 {
Chris@101 823 float min = 0.0, max = 0.0;
Chris@101 824 bool logarithmic = false;
Chris@44 825 int h = v->height();
Chris@21 826
Chris@101 827 getScaleExtents(v, min, max, logarithmic);
Chris@101 828
Chris@101 829 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 830
Chris@101 831 if (logarithmic) {
Chris@437 832 val = LogRange::map(val);
Chris@101 833 }
Chris@101 834
Chris@101 835 return val;
Chris@21 836 }
Chris@21 837
Chris@296 838 bool
Chris@296 839 TimeValueLayer::shouldAutoAlign() const
Chris@296 840 {
Chris@296 841 if (!m_model) return false;
Chris@296 842 QString unit = m_model->getScaleUnits();
Chris@296 843 return (m_verticalScale == AutoAlignScale && unit != "");
Chris@296 844 }
Chris@296 845
Chris@68 846 QColor
Chris@101 847 TimeValueLayer::getColourForValue(View *v, float val) const
Chris@68 848 {
Chris@101 849 float min, max;
Chris@101 850 bool log;
Chris@101 851 getScaleExtents(v, min, max, log);
Chris@68 852
Chris@197 853 if (min > max) std::swap(min, max);
Chris@197 854 if (max == min) max = min + 1;
Chris@197 855
Chris@101 856 if (log) {
Chris@197 857 val = LogRange::map(val);
Chris@68 858 }
Chris@68 859
Chris@526 860 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 861 std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
Chris@629 862 << max << ", log " << log << ", value " << val << std::endl;
Chris@526 863 #endif
Chris@68 864
Chris@197 865 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
Chris@197 866 return QColor(solid.red(), solid.green(), solid.blue(), 120);
Chris@68 867 }
Chris@68 868
Chris@287 869 int
Chris@287 870 TimeValueLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 871 {
Chris@287 872 impose = false;
Chris@287 873 return ColourDatabase::getInstance()->getColourIndex
Chris@287 874 (QString(darkbg ? "Bright Green" : "Green"));
Chris@287 875 }
Chris@287 876
Chris@0 877 void
Chris@44 878 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 879 {
Chris@0 880 if (!m_model || !m_model->isOK()) return;
Chris@0 881
Chris@0 882 int sampleRate = m_model->getSampleRate();
Chris@0 883 if (!sampleRate) return;
Chris@0 884
Chris@353 885 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@353 886
Chris@0 887 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 888
Chris@0 889 int x0 = rect.left(), x1 = rect.right();
Chris@44 890 long frame0 = v->getFrameForX(x0);
Chris@44 891 long frame1 = v->getFrameForX(x1);
Chris@553 892 if (m_derivative) --frame0;
Chris@0 893
Chris@0 894 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 895 (frame0, frame1));
Chris@11 896 if (points.empty()) return;
Chris@0 897
Chris@287 898 paint.setPen(getBaseQColor());
Chris@0 899
Chris@287 900 QColor brushColour(getBaseQColor());
Chris@0 901 brushColour.setAlpha(80);
Chris@0 902 paint.setBrush(brushColour);
Chris@0 903
Chris@526 904 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 905 std::cerr << "TimeValueLayer::paint: resolution is "
Chris@629 906 << m_model->getResolution() << " frames" << std::endl;
Chris@526 907 #endif
Chris@0 908
Chris@0 909 float min = m_model->getValueMinimum();
Chris@0 910 float max = m_model->getValueMaximum();
Chris@0 911 if (max == min) max = min + 1.0;
Chris@0 912
Chris@44 913 int origin = int(nearbyint(v->height() -
Chris@44 914 (-min * v->height()) / (max - min)));
Chris@0 915
Chris@0 916 QPoint localPos;
Chris@0 917 long illuminateFrame = -1;
Chris@0 918
Chris@44 919 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 920 SparseTimeValueModel::PointList localPoints =
Chris@44 921 getLocalPoints(v, localPos.x());
Chris@526 922 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@526 923 std::cerr << "TimeValueLayer: " << localPoints.size() << " local points" << std::endl;
Chris@526 924 #endif
Chris@0 925 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 926 }
Chris@6 927
Chris@20 928 int w =
Chris@44 929 v->getXForFrame(frame0 + m_model->getResolution()) -
Chris@44 930 v->getXForFrame(frame0);
Chris@7 931
Chris@629 932 if (m_plotStyle == PlotStems) {
Chris@629 933 if (w < 2) w = 2;
Chris@629 934 } else {
Chris@629 935 if (w < 1) w = 1;
Chris@629 936 }
Chris@629 937
Chris@6 938 paint.save();
Chris@6 939
Chris@6 940 QPainterPath path;
Chris@55 941 int pointCount = 0;
Chris@79 942
Chris@79 943 int textY = 0;
Chris@79 944 if (m_plotStyle == PlotSegmentation) {
Chris@79 945 textY = v->getTextLabelHeight(this, paint);
Chris@554 946 } else {
Chris@554 947 int originY = getYForValue(v, 0.f);
Chris@554 948 if (originY > 0 && originY < v->height()) {
Chris@554 949 paint.save();
Chris@554 950 paint.setPen(getPartialShades(v)[1]);
Chris@554 951 paint.drawLine(x0, originY, x1, originY);
Chris@554 952 paint.restore();
Chris@554 953 }
Chris@79 954 }
Chris@6 955
Chris@615 956 int prevFrame = 0;
Chris@615 957
Chris@0 958 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 959 i != points.end(); ++i) {
Chris@0 960
Chris@553 961 if (m_derivative && i == points.begin()) continue;
Chris@553 962
Chris@0 963 const SparseTimeValueModel::Point &p(*i);
Chris@0 964
Chris@553 965 float value = p.value;
Chris@553 966 if (m_derivative) {
Chris@553 967 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@553 968 --j;
Chris@553 969 value -= j->value;
Chris@553 970 }
Chris@553 971
Chris@44 972 int x = v->getXForFrame(p.frame);
Chris@553 973 int y = getYForValue(v, value);
Chris@0 974
Chris@615 975 bool gap = false;
Chris@615 976 if (m_plotStyle == PlotDiscreteCurves) {
Chris@615 977 gap = (p.frame > prevFrame &&
Chris@615 978 (p.frame - prevFrame >= m_model->getResolution() * 2));
Chris@615 979 }
Chris@615 980
Chris@79 981 if (m_plotStyle != PlotSegmentation) {
Chris@79 982 textY = y - paint.fontMetrics().height()
Chris@631 983 + paint.fontMetrics().ascent() - 1;
Chris@372 984 if (textY < paint.fontMetrics().ascent() + 1) {
Chris@372 985 textY = paint.fontMetrics().ascent() + 1;
Chris@372 986 }
Chris@79 987 }
Chris@79 988
Chris@34 989 bool haveNext = false;
Chris@615 990 int nf = v->getModelsEndFrame();
Chris@615 991 int nx = v->getXForFrame(nf);
Chris@34 992 int ny = y;
Chris@34 993
Chris@34 994 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 995 ++j;
Chris@34 996
Chris@34 997 if (j != points.end()) {
Chris@34 998 const SparseTimeValueModel::Point &q(*j);
Chris@553 999 float nvalue = q.value;
Chris@553 1000 if (m_derivative) nvalue -= p.value;
Chris@615 1001 nf = q.frame;
Chris@615 1002 nx = v->getXForFrame(nf);
Chris@553 1003 ny = getYForValue(v, nvalue);
Chris@34 1004 haveNext = true;
Chris@76 1005 }
Chris@76 1006
Chris@76 1007 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
Chris@76 1008 // << ", nx = " << nx << std::endl;
Chris@34 1009
Chris@615 1010 if (m_plotStyle == PlotDiscreteCurves) {
Chris@615 1011 paint.setPen(QPen(getBaseQColor(), 3));
Chris@615 1012 paint.setBrush(Qt::NoBrush);
Chris@615 1013 } else if (m_plotStyle == PlotSegmentation) {
Chris@287 1014 paint.setPen(getForegroundQColor(v));
Chris@553 1015 paint.setBrush(getColourForValue(v, value));
Chris@26 1016 } else if (m_plotStyle == PlotLines ||
Chris@26 1017 m_plotStyle == PlotCurve) {
Chris@615 1018 paint.setPen(getBaseQColor());
Chris@6 1019 paint.setBrush(Qt::NoBrush);
Chris@3 1020 } else {
Chris@615 1021 paint.setPen(getBaseQColor());
Chris@6 1022 paint.setBrush(brushColour);
Chris@3 1023 }
Chris@0 1024
Chris@0 1025 if (m_plotStyle == PlotStems) {
Chris@430 1026 /*
Chris@0 1027 paint.setPen(brushColour);
Chris@0 1028 if (y < origin - 1) {
Chris@0 1029 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 1030 } else if (y > origin + 1) {
Chris@0 1031 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 1032 }
Chris@430 1033 */
Chris@287 1034 paint.setPen(getBaseQColor());
Chris@430 1035 if (y < origin - 1) {
Chris@430 1036 paint.drawLine(x + w/2, y + 1, x + w/2, origin);
Chris@430 1037 } else if (y > origin + 1) {
Chris@430 1038 paint.drawLine(x + w/2, origin, x + w/2, y - 1);
Chris@430 1039 }
Chris@0 1040 }
Chris@0 1041
Chris@631 1042 bool illuminate = false;
Chris@631 1043
Chris@0 1044 if (illuminateFrame == p.frame) {
Chris@6 1045
Chris@631 1046 // not equipped to illuminate the right section in line
Chris@631 1047 // or curve mode
Chris@6 1048
Chris@6 1049 if (m_plotStyle != PlotCurve &&
Chris@615 1050 m_plotStyle != PlotDiscreteCurves &&
Chris@6 1051 m_plotStyle != PlotLines) {
Chris@631 1052 illuminate = true;
Chris@631 1053 }
Chris@631 1054 }
Chris@0 1055
Chris@6 1056 if (m_plotStyle != PlotLines &&
Chris@26 1057 m_plotStyle != PlotCurve &&
Chris@615 1058 m_plotStyle != PlotDiscreteCurves &&
Chris@26 1059 m_plotStyle != PlotSegmentation) {
Chris@631 1060 if (illuminate) {
Chris@631 1061 paint.save();
Chris@631 1062 paint.setPen(getForegroundQColor(v));
Chris@631 1063 paint.setBrush(getForegroundQColor(v));
Chris@631 1064 }
Chris@326 1065 if (m_plotStyle != PlotStems ||
Chris@326 1066 w > 1) {
Chris@326 1067 paint.drawRect(x, y - 1, w, 2);
Chris@326 1068 }
Chris@631 1069 if (illuminate) {
Chris@631 1070 paint.restore();
Chris@631 1071 }
Chris@3 1072 }
Chris@0 1073
Chris@6 1074 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 1075 m_plotStyle == PlotLines ||
Chris@615 1076 m_plotStyle == PlotDiscreteCurves ||
Chris@6 1077 m_plotStyle == PlotCurve) {
Chris@0 1078
Chris@34 1079 if (haveNext) {
Chris@3 1080
Chris@6 1081 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 1082
Chris@79 1083 paint.save();
Chris@3 1084 paint.setPen(brushColour);
Chris@3 1085 paint.drawLine(x + w, y, nx, ny);
Chris@79 1086 paint.restore();
Chris@6 1087
Chris@6 1088 } else if (m_plotStyle == PlotLines) {
Chris@430 1089
Chris@430 1090 if (pointCount == 0) {
Chris@430 1091 path.moveTo(x + w/2, y);
Chris@430 1092 }
Chris@6 1093
Chris@430 1094 // paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@430 1095 path.lineTo(nx + w/2, ny);
Chris@6 1096
Chris@3 1097 } else {
Chris@6 1098
Chris@55 1099 float x0 = x + float(w)/2;
Chris@55 1100 float x1 = nx + float(w)/2;
Chris@55 1101
Chris@55 1102 float y0 = y;
Chris@55 1103 float y1 = ny;
Chris@55 1104
Chris@615 1105 if (m_plotStyle == PlotDiscreteCurves) {
Chris@615 1106 bool nextGap = nf - p.frame >= m_model->getResolution() * 2;
Chris@615 1107 if (nextGap) {
Chris@615 1108 x1 = x0;
Chris@615 1109 y1 = y0;
Chris@615 1110 }
Chris@615 1111 }
Chris@615 1112
Chris@615 1113 if (pointCount == 0 || gap) {
Chris@55 1114 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 1115 }
Chris@6 1116
Chris@6 1117 if (nx - x > 5) {
Chris@55 1118 path.cubicTo(x0, y0,
Chris@55 1119 x0, y0,
Chris@55 1120 (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 1121
Chris@55 1122 // // or
Chris@55 1123 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 1124
Chris@6 1125 } else {
Chris@55 1126 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 1127 }
Chris@3 1128 }
Chris@0 1129 }
Chris@0 1130 }
Chris@0 1131
Chris@26 1132 if (m_plotStyle == PlotSegmentation) {
Chris@76 1133
Chris@526 1134 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1135 std::cerr << "drawing rect" << std::endl;
Chris@526 1136 #endif
Chris@26 1137
Chris@27 1138 if (nx <= x) continue;
Chris@26 1139
Chris@559 1140 paint.setPen(QPen(getForegroundQColor(v), 2));
Chris@559 1141
Chris@631 1142 if (!illuminate) {
Chris@513 1143 if (!m_drawSegmentDivisions ||
Chris@513 1144 nx < x + 5 ||
Chris@513 1145 x >= v->width() - 1) {
Chris@513 1146 paint.setPen(Qt::NoPen);
Chris@513 1147 }
Chris@27 1148 }
Chris@26 1149
Chris@44 1150 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 1151 }
Chris@26 1152
Chris@629 1153 QString label = p.label;
Chris@630 1154 bool italic = false;
Chris@629 1155
Chris@629 1156 if (label == "" &&
Chris@629 1157 (m_plotStyle == PlotPoints ||
Chris@631 1158 m_plotStyle == PlotSegmentation ||
Chris@631 1159 m_plotStyle == PlotConnectedPoints)) {
Chris@632 1160 char lc[20];
Chris@632 1161 snprintf(lc, 20, "%.3g", p.value);
Chris@632 1162 label = lc;
Chris@630 1163 italic = true;
Chris@629 1164 }
Chris@629 1165
Chris@629 1166 if (label != "") {
Chris@632 1167 bool haveRoom = nx > x + 6 + paint.fontMetrics().width(label);
Chris@632 1168 if (haveRoom ||
Chris@632 1169 (!haveNext &&
Chris@632 1170 (pointCount == 0 || !italic))) {
Chris@630 1171 v->drawVisibleText(paint, x + 5, textY, label,
Chris@630 1172 italic ?
Chris@630 1173 View::OutlinedItalicText :
Chris@630 1174 View::OutlinedText);
Chris@79 1175 }
Chris@55 1176 }
Chris@615 1177
Chris@615 1178 prevFrame = p.frame;
Chris@632 1179 ++pointCount;
Chris@0 1180 }
Chris@6 1181
Chris@615 1182 if ((m_plotStyle == PlotCurve || m_plotStyle == PlotDiscreteCurves ||
Chris@615 1183 m_plotStyle == PlotLines)
Chris@430 1184 && !path.isEmpty()) {
Chris@55 1185 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
Chris@6 1186 paint.drawPath(path);
Chris@6 1187 }
Chris@6 1188
Chris@6 1189 paint.restore();
Chris@6 1190
Chris@6 1191 // looks like save/restore doesn't deal with this:
Chris@6 1192 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 1193 }
Chris@6 1194
Chris@42 1195 int
Chris@607 1196 TimeValueLayer::getVerticalScaleWidth(View *, bool, QPainter &paint) const
Chris@42 1197 {
Chris@68 1198 int w = paint.fontMetrics().width("-000.000");
Chris@68 1199 if (m_plotStyle == PlotSegmentation) return w + 20;
Chris@68 1200 else return w + 10;
Chris@42 1201 }
Chris@42 1202
Chris@42 1203 void
Chris@607 1204 TimeValueLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
Chris@42 1205 {
Chris@42 1206 if (!m_model) return;
Chris@42 1207
Chris@68 1208 int h = v->height();
Chris@68 1209
Chris@68 1210 int n = 10;
Chris@68 1211
Chris@437 1212 float min, max;
Chris@437 1213 bool logarithmic;
Chris@437 1214 getScaleExtents(v, min, max, logarithmic);
Chris@437 1215
Chris@437 1216 if (m_plotStyle == PlotSegmentation) {
Chris@437 1217 QString unit;
Chris@437 1218 getValueExtents(min, max, logarithmic, unit);
Chris@437 1219 if (logarithmic) {
Chris@437 1220 LogRange::mapRange(min, max);
Chris@437 1221 }
Chris@437 1222 }
Chris@437 1223
Chris@68 1224 float val = min;
Chris@68 1225 float inc = (max - val) / n;
Chris@42 1226
Chris@55 1227 char buffer[40];
Chris@55 1228
Chris@607 1229 int w = getVerticalScaleWidth(v, false, paint);
Chris@55 1230
Chris@68 1231 int tx = 5;
Chris@68 1232
Chris@68 1233 int boxx = 5, boxy = 5;
Chris@68 1234 if (m_model->getScaleUnits() != "") {
Chris@68 1235 boxy += paint.fontMetrics().height();
Chris@68 1236 }
Chris@68 1237 int boxw = 10, boxh = h - boxy - 5;
Chris@68 1238
Chris@68 1239 if (m_plotStyle == PlotSegmentation) {
Chris@68 1240 tx += boxx + boxw;
Chris@68 1241 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@68 1242 }
Chris@68 1243
Chris@68 1244 if (m_plotStyle == PlotSegmentation) {
Chris@68 1245 paint.save();
Chris@68 1246 for (int y = 0; y < boxh; ++y) {
Chris@68 1247 float val = ((boxh - y) * (max - min)) / boxh + min;
Chris@437 1248 if (logarithmic) {
Chris@437 1249 paint.setPen(getColourForValue(v, LogRange::unmap(val)));
Chris@437 1250 } else {
Chris@437 1251 paint.setPen(getColourForValue(v, val));
Chris@437 1252 }
Chris@68 1253 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@68 1254 }
Chris@68 1255 paint.restore();
Chris@68 1256 }
Chris@434 1257
Chris@434 1258 float round = 1.f;
Chris@434 1259 int dp = 0;
Chris@434 1260 if (inc > 0) {
Chris@434 1261 int prec = trunc(log10f(inc));
Chris@434 1262 prec -= 1;
Chris@434 1263 if (prec < 0) dp = -prec;
Chris@434 1264 round = powf(10.f, prec);
Chris@526 1265 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@526 1266 std::cerr << "inc = " << inc << ", round = " << round << std::endl;
Chris@526 1267 #endif
Chris@434 1268 }
Chris@68 1269
Chris@434 1270 int prevy = -1;
Chris@434 1271
Chris@68 1272 for (int i = 0; i < n; ++i) {
Chris@68 1273
Chris@68 1274 int y, ty;
Chris@68 1275 bool drawText = true;
Chris@68 1276
Chris@434 1277 float dispval = val;
Chris@434 1278
Chris@68 1279 if (m_plotStyle == PlotSegmentation) {
Chris@68 1280 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@68 1281 ty = y;
Chris@68 1282 } else {
Chris@437 1283 if (i == n-1 &&
Chris@437 1284 v->height() < paint.fontMetrics().height() * (n*2)) {
Chris@68 1285 if (m_model->getScaleUnits() != "") drawText = false;
Chris@68 1286 }
Chris@434 1287 dispval = lrintf(val / round) * round;
Chris@526 1288 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@526 1289 std::cerr << "val = " << val << ", dispval = " << dispval << std::endl;
Chris@526 1290 #endif
Chris@437 1291 if (logarithmic) {
Chris@437 1292 y = getYForValue(v, LogRange::unmap(dispval));
Chris@437 1293 } else {
Chris@437 1294 y = getYForValue(v, dispval);
Chris@437 1295 }
Chris@68 1296 ty = y - paint.fontMetrics().height() +
Chris@434 1297 paint.fontMetrics().ascent() + 2;
Chris@434 1298
Chris@434 1299 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@434 1300 val += inc;
Chris@434 1301 continue;
Chris@434 1302 }
Chris@68 1303 }
Chris@68 1304
Chris@437 1305 if (logarithmic) {
Chris@437 1306 sprintf(buffer, "%.*g", dp < 2 ? 2 : dp, LogRange::unmap(dispval));
Chris@437 1307 } else {
Chris@437 1308 sprintf(buffer, "%.*f", dp, dispval);
Chris@437 1309 }
Chris@55 1310 QString label = QString(buffer);
Chris@68 1311
Chris@68 1312 if (m_plotStyle != PlotSegmentation) {
Chris@68 1313 paint.drawLine(w - 5, y, w, y);
Chris@68 1314 } else {
Chris@68 1315 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@68 1316 }
Chris@68 1317
Chris@434 1318 if (drawText) {
Chris@435 1319 if (m_plotStyle != PlotSegmentation) {
Chris@435 1320 paint.drawText(tx + w - paint.fontMetrics().width(label) - 8,
Chris@435 1321 ty, label);
Chris@435 1322 } else {
Chris@435 1323 paint.drawText(tx, ty, label);
Chris@435 1324 }
Chris@434 1325 }
Chris@434 1326
Chris@434 1327 prevy = y;
Chris@44 1328 val += inc;
Chris@42 1329 }
Chris@68 1330
Chris@67 1331 if (m_model->getScaleUnits() != "") {
Chris@67 1332 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
Chris@67 1333 m_model->getScaleUnits());
Chris@67 1334 }
Chris@42 1335 }
Chris@42 1336
Chris@21 1337 void
Chris@44 1338 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 1339 {
Chris@526 1340 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1341 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@526 1342 #endif
Chris@21 1343
Chris@21 1344 if (!m_model) return;
Chris@21 1345
Chris@44 1346 long frame = v->getFrameForX(e->x());
Chris@76 1347 long resolution = m_model->getResolution();
Chris@21 1348 if (frame < 0) frame = 0;
Chris@76 1349 frame = (frame / resolution) * resolution;
Chris@21 1350
Chris@44 1351 float value = getValueForY(v, e->y());
Chris@21 1352
Chris@76 1353 bool havePoint = false;
Chris@76 1354
Chris@76 1355 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 1356 if (!points.empty()) {
Chris@76 1357 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1358 i != points.end(); ++i) {
Chris@76 1359 if (((i->frame / resolution) * resolution) != frame) {
Chris@526 1360 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1361 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@526 1362 #endif
Chris@76 1363 continue;
Chris@76 1364 }
Chris@76 1365 m_editingPoint = *i;
Chris@76 1366 havePoint = true;
Chris@76 1367 }
Chris@76 1368 }
Chris@76 1369
Chris@76 1370 if (!havePoint) {
Chris@76 1371 m_editingPoint = SparseTimeValueModel::Point
Chris@76 1372 (frame, value, tr("New Point"));
Chris@76 1373 }
Chris@76 1374
Chris@23 1375 m_originalPoint = m_editingPoint;
Chris@22 1376
Chris@376 1377 if (m_editingCommand) finish(m_editingCommand);
Chris@22 1378 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 1379 tr("Draw Point"));
Chris@76 1380 if (!havePoint) {
Chris@76 1381 m_editingCommand->addPoint(m_editingPoint);
Chris@76 1382 }
Chris@22 1383
Chris@21 1384 m_editing = true;
Chris@21 1385 }
Chris@21 1386
Chris@21 1387 void
Chris@44 1388 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 1389 {
Chris@526 1390 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1391 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@526 1392 #endif
Chris@21 1393
Chris@21 1394 if (!m_model || !m_editing) return;
Chris@21 1395
Chris@44 1396 long frame = v->getFrameForX(e->x());
Chris@76 1397 long resolution = m_model->getResolution();
Chris@21 1398 if (frame < 0) frame = 0;
Chris@76 1399 frame = (frame / resolution) * resolution;
Chris@21 1400
Chris@44 1401 float value = getValueForY(v, e->y());
Chris@21 1402
Chris@76 1403 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 1404
Chris@526 1405 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@526 1406 std::cerr << points.size() << " points" << std::endl;
Chris@526 1407 #endif
Chris@76 1408
Chris@76 1409 bool havePoint = false;
Chris@76 1410
Chris@76 1411 if (!points.empty()) {
Chris@76 1412 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1413 i != points.end(); ++i) {
Chris@76 1414 if (i->frame == m_editingPoint.frame &&
Chris@76 1415 i->value == m_editingPoint.value) {
Chris@526 1416 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1417 std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
Chris@526 1418 #endif
Chris@76 1419 continue;
Chris@76 1420 }
Chris@76 1421 if (((i->frame / resolution) * resolution) != frame) {
Chris@526 1422 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1423 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@526 1424 #endif
Chris@76 1425 continue;
Chris@76 1426 }
Chris@526 1427 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1428 std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
Chris@526 1429 #endif
Chris@76 1430 m_editingPoint = *i;
Chris@76 1431 m_originalPoint = m_editingPoint;
Chris@76 1432 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 1433 havePoint = true;
Chris@76 1434 }
Chris@76 1435 }
Chris@76 1436
Chris@76 1437 if (!havePoint) {
Chris@76 1438 if (frame == m_editingPoint.frame) {
Chris@76 1439 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 1440 }
Chris@76 1441 }
Chris@76 1442
Chris@76 1443 // m_editingCommand->deletePoint(m_editingPoint);
Chris@21 1444 m_editingPoint.frame = frame;
Chris@21 1445 m_editingPoint.value = value;
Chris@22 1446 m_editingCommand->addPoint(m_editingPoint);
Chris@21 1447 }
Chris@21 1448
Chris@21 1449 void
Chris@248 1450 TimeValueLayer::drawEnd(View *, QMouseEvent *)
Chris@21 1451 {
Chris@526 1452 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1453 std::cerr << "TimeValueLayer::drawEnd" << std::endl;
Chris@526 1454 #endif
Chris@21 1455 if (!m_model || !m_editing) return;
Chris@376 1456 finish(m_editingCommand);
Chris@22 1457 m_editingCommand = 0;
Chris@21 1458 m_editing = false;
Chris@21 1459 }
Chris@21 1460
Chris@21 1461 void
Chris@335 1462 TimeValueLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 1463 {
Chris@335 1464 if (!m_model) return;
Chris@335 1465
Chris@335 1466 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@335 1467 if (points.empty()) return;
Chris@335 1468
Chris@335 1469 m_editingPoint = *points.begin();
Chris@335 1470
Chris@335 1471 if (m_editingCommand) {
Chris@376 1472 finish(m_editingCommand);
Chris@335 1473 m_editingCommand = 0;
Chris@335 1474 }
Chris@335 1475
Chris@335 1476 m_editing = true;
Chris@335 1477 }
Chris@335 1478
Chris@335 1479 void
Chris@335 1480 TimeValueLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 1481 {
Chris@335 1482 }
Chris@335 1483
Chris@335 1484 void
Chris@335 1485 TimeValueLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 1486 {
Chris@335 1487 if (!m_model || !m_editing) return;
Chris@335 1488
Chris@335 1489 m_editing = false;
Chris@335 1490
Chris@335 1491 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@335 1492 if (points.empty()) return;
Chris@335 1493 if (points.begin()->frame != m_editingPoint.frame ||
Chris@335 1494 points.begin()->value != m_editingPoint.value) return;
Chris@335 1495
Chris@335 1496 m_editingCommand = new SparseTimeValueModel::EditCommand
Chris@335 1497 (m_model, tr("Erase Point"));
Chris@335 1498
Chris@335 1499 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 1500
Chris@376 1501 finish(m_editingCommand);
Chris@335 1502 m_editingCommand = 0;
Chris@335 1503 m_editing = false;
Chris@335 1504 }
Chris@335 1505
Chris@335 1506 void
Chris@44 1507 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 1508 {
Chris@526 1509 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1510 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@526 1511 #endif
Chris@21 1512
Chris@21 1513 if (!m_model) return;
Chris@21 1514
Chris@44 1515 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 1516 if (points.empty()) return;
Chris@21 1517
Chris@21 1518 m_editingPoint = *points.begin();
Chris@23 1519 m_originalPoint = m_editingPoint;
Chris@22 1520
Chris@22 1521 if (m_editingCommand) {
Chris@376 1522 finish(m_editingCommand);
Chris@22 1523 m_editingCommand = 0;
Chris@22 1524 }
Chris@22 1525
Chris@21 1526 m_editing = true;
Chris@21 1527 }
Chris@21 1528
Chris@21 1529 void
Chris@44 1530 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 1531 {
Chris@526 1532 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1533 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@526 1534 #endif
Chris@21 1535
Chris@21 1536 if (!m_model || !m_editing) return;
Chris@21 1537
Chris@44 1538 long frame = v->getFrameForX(e->x());
Chris@21 1539 if (frame < 0) frame = 0;
Chris@21 1540 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 1541
Chris@44 1542 float value = getValueForY(v, e->y());
Chris@21 1543
Chris@22 1544 if (!m_editingCommand) {
Chris@22 1545 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 1546 tr("Drag Point"));
Chris@22 1547 }
Chris@22 1548
Chris@22 1549 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 1550 m_editingPoint.frame = frame;
Chris@21 1551 m_editingPoint.value = value;
Chris@22 1552 m_editingCommand->addPoint(m_editingPoint);
Chris@21 1553 }
Chris@21 1554
Chris@21 1555 void
Chris@248 1556 TimeValueLayer::editEnd(View *, QMouseEvent *)
Chris@21 1557 {
Chris@526 1558 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1559 std::cerr << "TimeValueLayer::editEnd" << std::endl;
Chris@526 1560 #endif
Chris@21 1561 if (!m_model || !m_editing) return;
Chris@23 1562
Chris@23 1563 if (m_editingCommand) {
Chris@23 1564
Chris@23 1565 QString newName = m_editingCommand->getName();
Chris@23 1566
Chris@23 1567 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 1568 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 1569 newName = tr("Edit Point");
Chris@23 1570 } else {
Chris@23 1571 newName = tr("Relocate Point");
Chris@23 1572 }
Chris@23 1573 } else {
Chris@23 1574 newName = tr("Change Point Value");
Chris@23 1575 }
Chris@23 1576
Chris@23 1577 m_editingCommand->setName(newName);
Chris@376 1578 finish(m_editingCommand);
Chris@23 1579 }
Chris@23 1580
Chris@22 1581 m_editingCommand = 0;
Chris@21 1582 m_editing = false;
Chris@21 1583 }
Chris@21 1584
Chris@255 1585 bool
Chris@70 1586 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1587 {
Chris@255 1588 if (!m_model) return false;
Chris@70 1589
Chris@70 1590 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@255 1591 if (points.empty()) return false;
Chris@70 1592
Chris@70 1593 SparseTimeValueModel::Point point = *points.begin();
Chris@70 1594
Chris@70 1595 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1596 (m_model->getSampleRate(),
Chris@70 1597 ItemEditDialog::ShowTime |
Chris@70 1598 ItemEditDialog::ShowValue |
Chris@73 1599 ItemEditDialog::ShowText,
Chris@73 1600 m_model->getScaleUnits());
Chris@70 1601
Chris@70 1602 dialog->setFrameTime(point.frame);
Chris@70 1603 dialog->setValue(point.value);
Chris@70 1604 dialog->setText(point.label);
Chris@70 1605
Chris@70 1606 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1607
Chris@70 1608 SparseTimeValueModel::Point newPoint = point;
Chris@70 1609 newPoint.frame = dialog->getFrameTime();
Chris@70 1610 newPoint.value = dialog->getValue();
Chris@70 1611 newPoint.label = dialog->getText();
Chris@70 1612
Chris@70 1613 SparseTimeValueModel::EditCommand *command =
Chris@70 1614 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
Chris@70 1615 command->deletePoint(point);
Chris@70 1616 command->addPoint(newPoint);
Chris@376 1617 finish(command);
Chris@70 1618 }
Chris@70 1619
Chris@70 1620 delete dialog;
Chris@255 1621 return true;
Chris@70 1622 }
Chris@70 1623
Chris@70 1624 void
Chris@43 1625 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
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("Drag 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 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1637 i != points.end(); ++i) {
Chris@43 1638
Chris@43 1639 if (s.contains(i->frame)) {
Chris@43 1640 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1641 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1642 command->deletePoint(*i);
Chris@43 1643 command->addPoint(newPoint);
Chris@43 1644 }
Chris@43 1645 }
Chris@43 1646
Chris@376 1647 finish(command);
Chris@43 1648 }
Chris@43 1649
Chris@43 1650 void
Chris@43 1651 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1652 {
Chris@99 1653 if (!m_model) return;
Chris@99 1654
Chris@43 1655 SparseTimeValueModel::EditCommand *command =
Chris@43 1656 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1657 tr("Resize Selection"));
Chris@43 1658
Chris@43 1659 SparseTimeValueModel::PointList points =
Chris@43 1660 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1661
Chris@43 1662 double ratio =
Chris@43 1663 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1664 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1665
Chris@43 1666 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1667 i != points.end(); ++i) {
Chris@43 1668
Chris@43 1669 if (s.contains(i->frame)) {
Chris@43 1670
Chris@43 1671 double target = i->frame;
Chris@43 1672 target = newSize.getStartFrame() +
Chris@43 1673 double(target - s.getStartFrame()) * ratio;
Chris@43 1674
Chris@43 1675 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1676 newPoint.frame = lrint(target);
Chris@43 1677 command->deletePoint(*i);
Chris@43 1678 command->addPoint(newPoint);
Chris@43 1679 }
Chris@43 1680 }
Chris@43 1681
Chris@376 1682 finish(command);
Chris@43 1683 }
Chris@43 1684
Chris@76 1685 void
Chris@76 1686 TimeValueLayer::deleteSelection(Selection s)
Chris@76 1687 {
Chris@99 1688 if (!m_model) return;
Chris@99 1689
Chris@76 1690 SparseTimeValueModel::EditCommand *command =
Chris@76 1691 new SparseTimeValueModel::EditCommand(m_model,
Chris@76 1692 tr("Delete Selected Points"));
Chris@76 1693
Chris@76 1694 SparseTimeValueModel::PointList points =
Chris@76 1695 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1696
Chris@76 1697 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1698 i != points.end(); ++i) {
Chris@76 1699
Chris@76 1700 if (s.contains(i->frame)) {
Chris@76 1701 command->deletePoint(*i);
Chris@76 1702 }
Chris@76 1703 }
Chris@76 1704
Chris@376 1705 finish(command);
Chris@76 1706 }
Chris@76 1707
Chris@76 1708 void
Chris@359 1709 TimeValueLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1710 {
Chris@99 1711 if (!m_model) return;
Chris@99 1712
Chris@76 1713 SparseTimeValueModel::PointList points =
Chris@76 1714 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1715
Chris@76 1716 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1717 i != points.end(); ++i) {
Chris@76 1718 if (s.contains(i->frame)) {
Chris@83 1719 Clipboard::Point point(i->frame, i->value, i->label);
Chris@360 1720 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1721 to.addPoint(point);
Chris@76 1722 }
Chris@76 1723 }
Chris@76 1724 }
Chris@76 1725
Chris@125 1726 bool
Chris@359 1727 TimeValueLayer::paste(View *v, const Clipboard &from, int frameOffset,
Chris@125 1728 bool interactive)
Chris@76 1729 {
Chris@125 1730 if (!m_model) return false;
Chris@99 1731
Chris@76 1732 const Clipboard::PointList &points = from.getPoints();
Chris@76 1733
Chris@360 1734 bool realign = false;
Chris@360 1735
Chris@360 1736 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1737
Chris@360 1738 QMessageBox::StandardButton button =
Chris@360 1739 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1740 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 1741 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1742 QMessageBox::Yes);
Chris@360 1743
Chris@360 1744 if (button == QMessageBox::Cancel) {
Chris@360 1745 return false;
Chris@360 1746 }
Chris@360 1747
Chris@360 1748 if (button == QMessageBox::Yes) {
Chris@360 1749 realign = true;
Chris@360 1750 }
Chris@360 1751 }
Chris@360 1752
Chris@76 1753 SparseTimeValueModel::EditCommand *command =
Chris@76 1754 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
Chris@76 1755
Chris@125 1756 enum ValueAvailability {
Chris@125 1757 UnknownAvailability,
Chris@125 1758 NoValues,
Chris@125 1759 SomeValues,
Chris@125 1760 AllValues
Chris@125 1761 };
Chris@125 1762
Chris@340 1763 Labeller::ValueType generation = Labeller::ValueNone;
Chris@125 1764
Chris@125 1765 bool haveUsableLabels = false;
Chris@125 1766 bool haveExistingItems = !(m_model->isEmpty());
Chris@340 1767 Labeller labeller;
Chris@340 1768 labeller.setSampleRate(m_model->getSampleRate());
Chris@125 1769
Chris@125 1770 if (interactive) {
Chris@125 1771
Chris@125 1772 ValueAvailability availability = UnknownAvailability;
Chris@125 1773
Chris@125 1774 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@125 1775 i != points.end(); ++i) {
Chris@125 1776
Chris@125 1777 if (!i->haveFrame()) continue;
Chris@125 1778
Chris@125 1779 if (availability == UnknownAvailability) {
Chris@125 1780 if (i->haveValue()) availability = AllValues;
Chris@125 1781 else availability = NoValues;
Chris@125 1782 continue;
Chris@125 1783 }
Chris@125 1784
Chris@125 1785 if (i->haveValue()) {
Chris@125 1786 if (availability == NoValues) {
Chris@125 1787 availability = SomeValues;
Chris@125 1788 }
Chris@125 1789 } else {
Chris@125 1790 if (availability == AllValues) {
Chris@125 1791 availability = SomeValues;
Chris@125 1792 }
Chris@125 1793 }
Chris@125 1794
Chris@125 1795 if (!haveUsableLabels) {
Chris@125 1796 if (i->haveLabel()) {
Chris@125 1797 if (i->getLabel().contains(QRegExp("[0-9]"))) {
Chris@125 1798 haveUsableLabels = true;
Chris@125 1799 }
Chris@125 1800 }
Chris@125 1801 }
Chris@125 1802
Chris@125 1803 if (availability == SomeValues && haveUsableLabels) break;
Chris@125 1804 }
Chris@125 1805
Chris@125 1806 if (availability == NoValues || availability == SomeValues) {
Chris@125 1807
Chris@125 1808 QString text;
Chris@125 1809 if (availability == NoValues) {
Chris@125 1810 text = tr("The items you are pasting do not have values.\nWhat values do you want to use for these items?");
Chris@125 1811 } else {
Chris@125 1812 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 1813 }
Chris@125 1814
Chris@340 1815 Labeller::TypeNameMap names = labeller.getTypeNames();
Chris@340 1816
Chris@125 1817 QStringList options;
Chris@340 1818 std::vector<Labeller::ValueType> genopts;
Chris@125 1819
Chris@340 1820 for (Labeller::TypeNameMap::const_iterator i = names.begin();
Chris@340 1821 i != names.end(); ++i) {
Chris@340 1822 if (i->first == Labeller::ValueNone) options << tr("Zero for all items");
Chris@340 1823 else options << i->second;
Chris@340 1824 genopts.push_back(i->first);
Chris@125 1825 }
Chris@125 1826
Chris@125 1827 static int prevSelection = 0;
Chris@125 1828
Chris@125 1829 bool ok = false;
Chris@125 1830 QString selected = ListInputDialog::getItem
Chris@125 1831 (0, tr("Choose value calculation"),
Chris@125 1832 text, options, prevSelection, &ok);
Chris@125 1833
Chris@125 1834 if (!ok) return false;
Chris@125 1835 int selection = 0;
Chris@340 1836 generation = Labeller::ValueNone;
Chris@125 1837
Chris@125 1838 for (QStringList::const_iterator i = options.begin();
Chris@125 1839 i != options.end(); ++i) {
Chris@125 1840 if (selected == *i) {
Chris@340 1841 generation = genopts[selection];
Chris@125 1842 break;
Chris@125 1843 }
Chris@125 1844 ++selection;
Chris@125 1845 }
Chris@340 1846
Chris@340 1847 labeller.setType(generation);
Chris@340 1848
Chris@340 1849 if (generation == Labeller::ValueFromCyclicalCounter ||
Chris@340 1850 generation == Labeller::ValueFromTwoLevelCounter) {
Chris@616 1851 int cycleSize = QInputDialog::getInt
Chris@340 1852 (0, tr("Select cycle size"),
Chris@340 1853 tr("Cycle size:"), 4, 2, 16, 1);
Chris@340 1854 labeller.setCounterCycleSize(cycleSize);
Chris@340 1855 }
Chris@125 1856
Chris@125 1857 prevSelection = selection;
Chris@125 1858 }
Chris@125 1859 }
Chris@125 1860
Chris@372 1861 SparseTimeValueModel::Point prevPoint(0);
Chris@125 1862
Chris@76 1863 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1864 i != points.end(); ++i) {
Chris@76 1865
Chris@76 1866 if (!i->haveFrame()) continue;
Chris@360 1867
Chris@76 1868 size_t frame = 0;
Chris@360 1869
Chris@360 1870 if (!realign) {
Chris@360 1871
Chris@360 1872 frame = i->getFrame();
Chris@360 1873
Chris@360 1874 } else {
Chris@360 1875
Chris@360 1876 if (i->haveReferenceFrame()) {
Chris@360 1877 frame = i->getReferenceFrame();
Chris@360 1878 frame = alignFromReference(v, frame);
Chris@360 1879 } else {
Chris@360 1880 frame = i->getFrame();
Chris@360 1881 }
Chris@76 1882 }
Chris@360 1883
Chris@76 1884 SparseTimeValueModel::Point newPoint(frame);
Chris@76 1885
Chris@125 1886 if (i->haveLabel()) {
Chris@125 1887 newPoint.label = i->getLabel();
Chris@125 1888 } else if (i->haveValue()) {
Chris@125 1889 newPoint.label = QString("%1").arg(i->getValue());
Chris@125 1890 }
Chris@125 1891
Chris@372 1892 bool usePrev = false;
Chris@372 1893 SparseTimeValueModel::Point formerPrevPoint = prevPoint;
Chris@372 1894
Chris@125 1895 if (i->haveValue()) {
Chris@125 1896 newPoint.value = i->getValue();
Chris@125 1897 } else {
Chris@526 1898 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@629 1899 std::cerr << "Setting value on point at " << newPoint.frame << " from labeller";
Chris@526 1900 if (i == points.begin()) {
Chris@629 1901 std::cerr << ", no prev point" << std::endl;
Chris@526 1902 } else {
Chris@526 1903 std::cerr << ", prev point is at " << prevPoint.frame << std::endl;
Chris@526 1904 }
Chris@526 1905 #endif
Chris@340 1906 labeller.setValue<SparseTimeValueModel::Point>
Chris@371 1907 (newPoint, (i == points.begin()) ? 0 : &prevPoint);
Chris@526 1908 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@526 1909 std::cerr << "New point value = " << newPoint.value << std::endl;
Chris@526 1910 #endif
Chris@372 1911 if (labeller.actingOnPrevPoint() && i != points.begin()) {
Chris@372 1912 usePrev = true;
Chris@372 1913 }
Chris@372 1914 }
Chris@372 1915
Chris@372 1916 if (usePrev) {
Chris@372 1917 command->deletePoint(formerPrevPoint);
Chris@372 1918 command->addPoint(prevPoint);
Chris@340 1919 }
Chris@125 1920
Chris@340 1921 prevPoint = newPoint;
Chris@76 1922 command->addPoint(newPoint);
Chris@76 1923 }
Chris@76 1924
Chris@376 1925 finish(command);
Chris@125 1926 return true;
Chris@360 1927 }
Chris@76 1928
Chris@316 1929 void
Chris@316 1930 TimeValueLayer::toXml(QTextStream &stream,
Chris@316 1931 QString indent, QString extraAttributes) const
Chris@6 1932 {
Chris@316 1933 SingleColourLayer::toXml(stream, indent,
Chris@316 1934 extraAttributes +
Chris@553 1935 QString(" colourMap=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\" scaleMinimum=\"%4\" scaleMaximum=\"%5\" drawDivisions=\"%6\" derivative=\"%7\" ")
Chris@316 1936 .arg(m_colourMap)
Chris@316 1937 .arg(m_plotStyle)
Chris@445 1938 .arg(m_verticalScale)
Chris@445 1939 .arg(m_scaleMinimum)
Chris@513 1940 .arg(m_scaleMaximum)
Chris@553 1941 .arg(m_drawSegmentDivisions ? "true" : "false")
Chris@553 1942 .arg(m_derivative ? "true" : "false"));
Chris@0 1943 }
Chris@0 1944
Chris@11 1945 void
Chris@11 1946 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1947 {
Chris@287 1948 SingleColourLayer::setProperties(attributes);
Chris@11 1949
Chris@445 1950 bool ok, alsoOk;
Chris@287 1951
Chris@287 1952 int cmap = attributes.value("colourMap").toInt(&ok);
Chris@287 1953 if (ok) setFillColourMap(cmap);
Chris@287 1954
Chris@11 1955 PlotStyle style = (PlotStyle)
Chris@11 1956 attributes.value("plotStyle").toInt(&ok);
Chris@11 1957 if (ok) setPlotStyle(style);
Chris@286 1958
Chris@286 1959 VerticalScale scale = (VerticalScale)
Chris@286 1960 attributes.value("verticalScale").toInt(&ok);
Chris@286 1961 if (ok) setVerticalScale(scale);
Chris@445 1962
Chris@513 1963 bool draw = (attributes.value("drawDivisions").trimmed() == "true");
Chris@513 1964 setDrawSegmentDivisions(draw);
Chris@513 1965
Chris@553 1966 bool derivative = (attributes.value("derivative").trimmed() == "true");
Chris@553 1967 setShowDerivative(derivative);
Chris@553 1968
Chris@445 1969 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1970 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@526 1971 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@526 1972 std::cerr << "from properties: min = " << min << ", max = " << max << std::endl;
Chris@526 1973 #endif
Chris@526 1974 if (ok && alsoOk && min != max) setDisplayExtents(min, max);
Chris@11 1975 }
Chris@11 1976