annotate layer/TimeValueLayer.cpp @ 630:c3593bb2de6b

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