annotate layer/TimeValueLayer.cpp @ 528:44f391ec2172

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