annotate layer/TimeValueLayer.cpp @ 197:6b023411087b

* Work on harmonising colour and scale ranges between types of layer * Add normalize options to colour 3d plot layer
author Chris Cannam
date Thu, 01 Feb 2007 14:31:28 +0000
parents 33929e0c3c6b
children c2ed5014d4ff
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@128 22 #include "view/View.h"
Chris@0 23
Chris@128 24 #include "data/model/SparseTimeValueModel.h"
Chris@0 25
Chris@70 26 #include "widgets/ItemEditDialog.h"
Chris@125 27 #include "widgets/ListInputDialog.h"
Chris@70 28
Chris@66 29 #include "SpectrogramLayer.h" // for optional frequency alignment
Chris@197 30 #include "ColourMapper.h"
Chris@66 31
Chris@0 32 #include <QPainter>
Chris@6 33 #include <QPainterPath>
Chris@21 34 #include <QMouseEvent>
Chris@125 35 #include <QRegExp>
Chris@0 36
Chris@0 37 #include <iostream>
Chris@0 38 #include <cmath>
Chris@0 39
Chris@44 40 TimeValueLayer::TimeValueLayer() :
Chris@44 41 Layer(),
Chris@0 42 m_model(0),
Chris@21 43 m_editing(false),
Chris@23 44 m_originalPoint(0, 0.0, tr("New Point")),
Chris@21 45 m_editingPoint(0, 0.0, tr("New Point")),
Chris@22 46 m_editingCommand(0),
Chris@67 47 m_colour(Qt::darkGreen),
Chris@197 48 m_colourMap(0),
Chris@66 49 m_plotStyle(PlotConnectedPoints),
Chris@101 50 m_verticalScale(AutoAlignScale)
Chris@0 51 {
Chris@44 52
Chris@0 53 }
Chris@0 54
Chris@0 55 void
Chris@0 56 TimeValueLayer::setModel(SparseTimeValueModel *model)
Chris@0 57 {
Chris@0 58 if (m_model == model) return;
Chris@0 59 m_model = model;
Chris@0 60
Chris@0 61 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 62 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 63 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 64
Chris@0 65 connect(m_model, SIGNAL(completionChanged()),
Chris@0 66 this, SIGNAL(modelCompletionChanged()));
Chris@0 67
Chris@101 68 // std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
Chris@0 69
Chris@0 70 emit modelReplaced();
Chris@0 71 }
Chris@0 72
Chris@0 73 Layer::PropertyList
Chris@0 74 TimeValueLayer::getProperties() const
Chris@0 75 {
Chris@0 76 PropertyList list;
Chris@87 77 list.push_back("Colour");
Chris@87 78 list.push_back("Plot Type");
Chris@87 79 list.push_back("Vertical Scale");
Chris@100 80 list.push_back("Scale Units");
Chris@0 81 return list;
Chris@0 82 }
Chris@0 83
Chris@87 84 QString
Chris@87 85 TimeValueLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 86 {
Chris@87 87 if (name == "Colour") return tr("Colour");
Chris@87 88 if (name == "Plot Type") return tr("Plot Type");
Chris@87 89 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@100 90 if (name == "Scale Units") return tr("Scale Units");
Chris@87 91 return "";
Chris@87 92 }
Chris@87 93
Chris@0 94 Layer::PropertyType
Chris@0 95 TimeValueLayer::getPropertyType(const PropertyName &name) const
Chris@0 96 {
Chris@100 97 if (name == "Scale Units") return UnitsProperty;
Chris@100 98 else return ValueProperty;
Chris@0 99 }
Chris@0 100
Chris@0 101 int
Chris@0 102 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@0 103 int *min, int *max) const
Chris@0 104 {
Chris@0 105 //!!! factor this colour handling stuff out into a colour manager class
Chris@0 106
Chris@0 107 int deft = 0;
Chris@0 108
Chris@87 109 if (name == "Colour") {
Chris@0 110
Chris@197 111 if (m_plotStyle == PlotSegmentation) {
Chris@197 112
Chris@197 113 if (min) *min = 0;
Chris@197 114 if (max) *max = ColourMapper::getColourMapCount() - 1;
Chris@0 115
Chris@197 116 deft = m_colourMap;
Chris@197 117
Chris@197 118 } else {
Chris@197 119
Chris@197 120 if (min) *min = 0;
Chris@197 121 if (max) *max = 5;
Chris@197 122
Chris@197 123 if (m_colour == Qt::black) deft = 0;
Chris@197 124 else if (m_colour == Qt::darkRed) deft = 1;
Chris@197 125 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@197 126 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@197 127 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@197 128 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@197 129 }
Chris@0 130
Chris@87 131 } else if (name == "Plot Type") {
Chris@0 132
Chris@10 133 if (min) *min = 0;
Chris@26 134 if (max) *max = 5;
Chris@0 135
Chris@0 136 deft = int(m_plotStyle);
Chris@0 137
Chris@87 138 } else if (name == "Vertical Scale") {
Chris@66 139
Chris@66 140 if (min) *min = 0;
Chris@66 141 if (max) *max = 3;
Chris@66 142
Chris@66 143 deft = int(m_verticalScale);
Chris@66 144
Chris@100 145 } else if (name == "Scale Units") {
Chris@100 146
Chris@100 147 if (m_model) {
Chris@100 148 deft = UnitDatabase::getInstance()->getUnitId
Chris@100 149 (m_model->getScaleUnits());
Chris@100 150 }
Chris@100 151
Chris@0 152 } else {
Chris@0 153
Chris@0 154 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 155 }
Chris@0 156
Chris@0 157 return deft;
Chris@0 158 }
Chris@0 159
Chris@0 160 QString
Chris@0 161 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 162 int value) const
Chris@0 163 {
Chris@87 164 if (name == "Colour") {
Chris@197 165 if (m_plotStyle == PlotSegmentation) {
Chris@197 166 return ColourMapper::getColourMapName(value);
Chris@197 167 } else {
Chris@197 168 switch (value) {
Chris@197 169 default:
Chris@197 170 case 0: return tr("Black");
Chris@197 171 case 1: return tr("Red");
Chris@197 172 case 2: return tr("Blue");
Chris@197 173 case 3: return tr("Green");
Chris@197 174 case 4: return tr("Purple");
Chris@197 175 case 5: return tr("Orange");
Chris@197 176 }
Chris@197 177 }
Chris@87 178 } else if (name == "Plot Type") {
Chris@0 179 switch (value) {
Chris@0 180 default:
Chris@0 181 case 0: return tr("Points");
Chris@0 182 case 1: return tr("Stems");
Chris@6 183 case 2: return tr("Connected Points");
Chris@6 184 case 3: return tr("Lines");
Chris@6 185 case 4: return tr("Curve");
Chris@26 186 case 5: return tr("Segmentation");
Chris@0 187 }
Chris@87 188 } else if (name == "Vertical Scale") {
Chris@66 189 switch (value) {
Chris@66 190 default:
Chris@101 191 case 0: return tr("Auto-Align");
Chris@101 192 case 1: return tr("Linear Scale");
Chris@101 193 case 2: return tr("Log Scale");
Chris@101 194 case 3: return tr("+/-1 Scale");
Chris@66 195 }
Chris@0 196 }
Chris@0 197 return tr("<unknown>");
Chris@0 198 }
Chris@0 199
Chris@0 200 void
Chris@0 201 TimeValueLayer::setProperty(const PropertyName &name, int value)
Chris@0 202 {
Chris@87 203 if (name == "Colour") {
Chris@197 204 if (m_plotStyle == PlotSegmentation) {
Chris@197 205 setFillColourMap(value);
Chris@197 206 } else {
Chris@197 207 switch (value) {
Chris@197 208 default:
Chris@197 209 case 0: setBaseColour(Qt::black); break;
Chris@197 210 case 1: setBaseColour(Qt::darkRed); break;
Chris@197 211 case 2: setBaseColour(Qt::darkBlue); break;
Chris@197 212 case 3: setBaseColour(Qt::darkGreen); break;
Chris@197 213 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@197 214 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@197 215 }
Chris@197 216 }
Chris@87 217 } else if (name == "Plot Type") {
Chris@0 218 setPlotStyle(PlotStyle(value));
Chris@87 219 } else if (name == "Vertical Scale") {
Chris@66 220 setVerticalScale(VerticalScale(value));
Chris@100 221 } else if (name == "Scale Units") {
Chris@100 222 if (m_model) {
Chris@100 223 m_model->setScaleUnits
Chris@100 224 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 225 emit modelChanged();
Chris@100 226 }
Chris@0 227 }
Chris@0 228 }
Chris@0 229
Chris@0 230 void
Chris@0 231 TimeValueLayer::setBaseColour(QColor colour)
Chris@0 232 {
Chris@0 233 if (m_colour == colour) return;
Chris@0 234 m_colour = colour;
Chris@0 235 emit layerParametersChanged();
Chris@0 236 }
Chris@0 237
Chris@0 238 void
Chris@197 239 TimeValueLayer::setFillColourMap(int map)
Chris@197 240 {
Chris@197 241 if (m_colourMap == map) return;
Chris@197 242 m_colourMap = map;
Chris@197 243 emit layerParametersChanged();
Chris@197 244 }
Chris@197 245
Chris@197 246 void
Chris@0 247 TimeValueLayer::setPlotStyle(PlotStyle style)
Chris@0 248 {
Chris@0 249 if (m_plotStyle == style) return;
Chris@197 250 bool colourTypeChanged = (style == PlotSegmentation ||
Chris@197 251 m_plotStyle == PlotSegmentation);
Chris@0 252 m_plotStyle = style;
Chris@197 253 if (colourTypeChanged) {
Chris@197 254 emit layerParameterRangesChanged();
Chris@197 255 }
Chris@0 256 emit layerParametersChanged();
Chris@0 257 }
Chris@0 258
Chris@66 259 void
Chris@66 260 TimeValueLayer::setVerticalScale(VerticalScale scale)
Chris@66 261 {
Chris@66 262 if (m_verticalScale == scale) return;
Chris@66 263 m_verticalScale = scale;
Chris@66 264 emit layerParametersChanged();
Chris@66 265 }
Chris@66 266
Chris@0 267 bool
Chris@44 268 TimeValueLayer::isLayerScrollable(const View *v) const
Chris@0 269 {
Chris@6 270 // We don't illuminate sections in the line or curve modes, so
Chris@6 271 // they're always scrollable
Chris@6 272
Chris@6 273 if (m_plotStyle == PlotLines ||
Chris@6 274 m_plotStyle == PlotCurve) return true;
Chris@6 275
Chris@0 276 QPoint discard;
Chris@44 277 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@0 278 }
Chris@0 279
Chris@79 280 bool
Chris@101 281 TimeValueLayer::getValueExtents(float &min, float &max,
Chris@101 282 bool &logarithmic, QString &unit) const
Chris@79 283 {
Chris@101 284 if (!m_model) return false;
Chris@79 285 min = m_model->getValueMinimum();
Chris@79 286 max = m_model->getValueMaximum();
Chris@101 287 logarithmic = (m_verticalScale == LogScale);
Chris@79 288 unit = m_model->getScaleUnits();
Chris@79 289 return true;
Chris@79 290 }
Chris@79 291
Chris@101 292 bool
Chris@101 293 TimeValueLayer::getDisplayExtents(float &min, float &max) const
Chris@101 294 {
Chris@101 295 if (!m_model || m_verticalScale == AutoAlignScale) return false;
Chris@101 296
Chris@101 297 min = m_model->getValueMinimum();
Chris@101 298 max = m_model->getValueMaximum();
Chris@101 299 return true;
Chris@101 300 }
Chris@101 301
Chris@0 302 SparseTimeValueModel::PointList
Chris@44 303 TimeValueLayer::getLocalPoints(View *v, int x) const
Chris@0 304 {
Chris@0 305 if (!m_model) return SparseTimeValueModel::PointList();
Chris@0 306
Chris@44 307 long frame = v->getFrameForX(x);
Chris@0 308
Chris@0 309 SparseTimeValueModel::PointList onPoints =
Chris@0 310 m_model->getPoints(frame);
Chris@0 311
Chris@0 312 if (!onPoints.empty()) {
Chris@0 313 return onPoints;
Chris@0 314 }
Chris@0 315
Chris@0 316 SparseTimeValueModel::PointList prevPoints =
Chris@0 317 m_model->getPreviousPoints(frame);
Chris@0 318 SparseTimeValueModel::PointList nextPoints =
Chris@0 319 m_model->getNextPoints(frame);
Chris@0 320
Chris@0 321 SparseTimeValueModel::PointList usePoints = prevPoints;
Chris@0 322
Chris@0 323 if (prevPoints.empty()) {
Chris@0 324 usePoints = nextPoints;
Chris@44 325 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
Chris@44 326 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@0 327 usePoints = nextPoints;
Chris@0 328 } else if (nextPoints.begin()->frame - frame <
Chris@0 329 frame - prevPoints.begin()->frame) {
Chris@0 330 usePoints = nextPoints;
Chris@0 331 }
Chris@0 332
Chris@28 333 if (!usePoints.empty()) {
Chris@28 334 int fuzz = 2;
Chris@44 335 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@28 336 if ((px > x && px - x > fuzz) ||
Chris@28 337 (px < x && x - px > fuzz + 1)) {
Chris@28 338 usePoints.clear();
Chris@28 339 }
Chris@28 340 }
Chris@28 341
Chris@0 342 return usePoints;
Chris@0 343 }
Chris@0 344
Chris@25 345 QString
Chris@44 346 TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@0 347 {
Chris@25 348 int x = pos.x();
Chris@0 349
Chris@25 350 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 351
Chris@44 352 SparseTimeValueModel::PointList points = getLocalPoints(v, x);
Chris@0 353
Chris@0 354 if (points.empty()) {
Chris@0 355 if (!m_model->isReady()) {
Chris@25 356 return tr("In progress");
Chris@25 357 } else {
Chris@25 358 return tr("No local points");
Chris@0 359 }
Chris@0 360 }
Chris@0 361
Chris@0 362 long useFrame = points.begin()->frame;
Chris@0 363
Chris@0 364 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 365
Chris@25 366 QString text;
Chris@101 367 QString unit = m_model->getScaleUnits();
Chris@101 368 if (unit != "") unit = " " + unit;
Chris@0 369
Chris@25 370 if (points.begin()->label == "") {
Chris@101 371 text = QString(tr("Time:\t%1\nValue:\t%2%3\nNo label"))
Chris@25 372 .arg(rt.toText(true).c_str())
Chris@25 373 .arg(points.begin()->value)
Chris@101 374 .arg(unit);
Chris@101 375 } else {
Chris@101 376 text = QString(tr("Time:\t%1\nValue:\t%2%3\nLabel:\t%4"))
Chris@101 377 .arg(rt.toText(true).c_str())
Chris@101 378 .arg(points.begin()->value)
Chris@101 379 .arg(unit)
Chris@25 380 .arg(points.begin()->label);
Chris@25 381 }
Chris@0 382
Chris@44 383 pos = QPoint(v->getXForFrame(useFrame),
Chris@44 384 getYForValue(v, points.begin()->value));
Chris@25 385 return text;
Chris@0 386 }
Chris@0 387
Chris@28 388 bool
Chris@44 389 TimeValueLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 390 size_t &resolution,
Chris@28 391 SnapType snap) const
Chris@13 392 {
Chris@13 393 if (!m_model) {
Chris@44 394 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@13 395 }
Chris@13 396
Chris@13 397 resolution = m_model->getResolution();
Chris@28 398 SparseTimeValueModel::PointList points;
Chris@13 399
Chris@28 400 if (snap == SnapNeighbouring) {
Chris@28 401
Chris@44 402 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@28 403 if (points.empty()) return false;
Chris@28 404 frame = points.begin()->frame;
Chris@28 405 return true;
Chris@28 406 }
Chris@28 407
Chris@28 408 points = m_model->getPoints(frame, frame);
Chris@28 409 int snapped = frame;
Chris@28 410 bool found = false;
Chris@13 411
Chris@13 412 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@13 413 i != points.end(); ++i) {
Chris@13 414
Chris@28 415 if (snap == SnapRight) {
Chris@28 416
Chris@13 417 if (i->frame > frame) {
Chris@28 418 snapped = i->frame;
Chris@28 419 found = true;
Chris@13 420 break;
Chris@13 421 }
Chris@28 422
Chris@28 423 } else if (snap == SnapLeft) {
Chris@28 424
Chris@13 425 if (i->frame <= frame) {
Chris@28 426 snapped = i->frame;
Chris@28 427 found = true; // don't break, as the next may be better
Chris@28 428 } else {
Chris@28 429 break;
Chris@28 430 }
Chris@28 431
Chris@28 432 } else { // nearest
Chris@28 433
Chris@28 434 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@28 435 ++j;
Chris@28 436
Chris@28 437 if (j == points.end()) {
Chris@28 438
Chris@28 439 snapped = i->frame;
Chris@28 440 found = true;
Chris@28 441 break;
Chris@28 442
Chris@28 443 } else if (j->frame >= frame) {
Chris@28 444
Chris@28 445 if (j->frame - frame < frame - i->frame) {
Chris@28 446 snapped = j->frame;
Chris@28 447 } else {
Chris@28 448 snapped = i->frame;
Chris@28 449 }
Chris@28 450 found = true;
Chris@28 451 break;
Chris@13 452 }
Chris@13 453 }
Chris@13 454 }
Chris@13 455
Chris@28 456 frame = snapped;
Chris@28 457 return found;
Chris@13 458 }
Chris@13 459
Chris@101 460 void
Chris@101 461 TimeValueLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@101 462 {
Chris@101 463 min = 0.0;
Chris@101 464 max = 0.0;
Chris@101 465 log = false;
Chris@101 466
Chris@101 467 if (m_verticalScale == AutoAlignScale) {
Chris@101 468
Chris@101 469 if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) {
Chris@101 470 min = m_model->getValueMinimum();
Chris@101 471 max = m_model->getValueMaximum();
Chris@101 472 } else if (log) {
Chris@197 473 LogRange::mapRange(min, max);
Chris@101 474 }
Chris@101 475
Chris@101 476 } else if (m_verticalScale == PlusMinusOneScale) {
Chris@101 477
Chris@101 478 min = -1.0;
Chris@101 479 max = 1.0;
Chris@101 480
Chris@101 481 } else {
Chris@101 482
Chris@101 483 min = m_model->getValueMinimum();
Chris@101 484 max = m_model->getValueMaximum();
Chris@101 485
Chris@101 486 if (m_verticalScale == LogScale) {
Chris@197 487 LogRange::mapRange(min, max);
Chris@101 488 log = true;
Chris@101 489 }
Chris@101 490 }
Chris@101 491
Chris@101 492 if (max == min) max = min + 1.0;
Chris@101 493 }
Chris@101 494
Chris@21 495 int
Chris@66 496 TimeValueLayer::getYForValue(View *v, float val) const
Chris@21 497 {
Chris@79 498 float min = 0.0, max = 0.0;
Chris@101 499 bool logarithmic = false;
Chris@79 500 int h = v->height();
Chris@79 501
Chris@101 502 getScaleExtents(v, min, max, logarithmic);
Chris@101 503
Chris@101 504 // std::cerr << "getYForValue(" << val << "): min " << min << ", max "
Chris@101 505 // << max << ", log " << logarithmic << std::endl;
Chris@101 506
Chris@101 507 if (logarithmic) {
Chris@197 508 val = LogRange::map(val);
Chris@79 509 }
Chris@79 510
Chris@66 511 return int(h - ((val - min) * h) / (max - min));
Chris@21 512 }
Chris@21 513
Chris@21 514 float
Chris@44 515 TimeValueLayer::getValueForY(View *v, int y) const
Chris@21 516 {
Chris@101 517 float min = 0.0, max = 0.0;
Chris@101 518 bool logarithmic = false;
Chris@44 519 int h = v->height();
Chris@21 520
Chris@101 521 getScaleExtents(v, min, max, logarithmic);
Chris@101 522
Chris@101 523 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 524
Chris@101 525 if (logarithmic) {
Chris@197 526 val = powf(10.f, val);
Chris@101 527 }
Chris@101 528
Chris@101 529 return val;
Chris@21 530 }
Chris@21 531
Chris@68 532 QColor
Chris@101 533 TimeValueLayer::getColourForValue(View *v, float val) const
Chris@68 534 {
Chris@101 535 float min, max;
Chris@101 536 bool log;
Chris@101 537 getScaleExtents(v, min, max, log);
Chris@68 538
Chris@197 539 float logthresh = -80;
Chris@197 540
Chris@197 541 if (min > max) std::swap(min, max);
Chris@197 542 if (max == min) max = min + 1;
Chris@197 543
Chris@101 544 if (log) {
Chris@197 545 LogRange::mapRange(min, max);
Chris@197 546 val = LogRange::map(val);
Chris@68 547 }
Chris@68 548
Chris@197 549 // std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
Chris@197 550 // << max << ", log " << log << ", value " << val << std::endl;
Chris@68 551
Chris@197 552 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
Chris@197 553 return QColor(solid.red(), solid.green(), solid.blue(), 120);
Chris@68 554 }
Chris@68 555
Chris@0 556 void
Chris@44 557 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 558 {
Chris@0 559 if (!m_model || !m_model->isOK()) return;
Chris@0 560
Chris@0 561 int sampleRate = m_model->getSampleRate();
Chris@0 562 if (!sampleRate) return;
Chris@0 563
Chris@0 564 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 565
Chris@0 566 int x0 = rect.left(), x1 = rect.right();
Chris@44 567 long frame0 = v->getFrameForX(x0);
Chris@44 568 long frame1 = v->getFrameForX(x1);
Chris@0 569
Chris@0 570 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 571 (frame0, frame1));
Chris@11 572 if (points.empty()) return;
Chris@0 573
Chris@0 574 paint.setPen(m_colour);
Chris@0 575
Chris@0 576 QColor brushColour(m_colour);
Chris@0 577 brushColour.setAlpha(80);
Chris@0 578 paint.setBrush(brushColour);
Chris@0 579
Chris@0 580 // std::cerr << "TimeValueLayer::paint: resolution is "
Chris@0 581 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 582
Chris@0 583 float min = m_model->getValueMinimum();
Chris@0 584 float max = m_model->getValueMaximum();
Chris@0 585 if (max == min) max = min + 1.0;
Chris@0 586
Chris@44 587 int origin = int(nearbyint(v->height() -
Chris@44 588 (-min * v->height()) / (max - min)));
Chris@0 589
Chris@0 590 QPoint localPos;
Chris@0 591 long illuminateFrame = -1;
Chris@0 592
Chris@44 593 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 594 SparseTimeValueModel::PointList localPoints =
Chris@44 595 getLocalPoints(v, localPos.x());
Chris@0 596 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 597 }
Chris@6 598
Chris@20 599 int w =
Chris@44 600 v->getXForFrame(frame0 + m_model->getResolution()) -
Chris@44 601 v->getXForFrame(frame0);
Chris@7 602
Chris@6 603 paint.save();
Chris@6 604
Chris@6 605 QPainterPath path;
Chris@55 606 int pointCount = 0;
Chris@79 607
Chris@79 608 int textY = 0;
Chris@79 609 if (m_plotStyle == PlotSegmentation) {
Chris@79 610 textY = v->getTextLabelHeight(this, paint);
Chris@79 611 }
Chris@6 612
Chris@0 613 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 614 i != points.end(); ++i) {
Chris@0 615
Chris@0 616 const SparseTimeValueModel::Point &p(*i);
Chris@0 617
Chris@44 618 int x = v->getXForFrame(p.frame);
Chris@44 619 int y = getYForValue(v, p.value);
Chris@0 620
Chris@79 621 if (m_plotStyle != PlotSegmentation) {
Chris@79 622 textY = y - paint.fontMetrics().height()
Chris@79 623 + paint.fontMetrics().ascent();
Chris@79 624 }
Chris@79 625
Chris@34 626 bool haveNext = false;
Chris@76 627 int nx = v->getXForFrame(v->getModelsEndFrame());
Chris@76 628 // m_model->getEndFrame());
Chris@34 629 int ny = y;
Chris@34 630
Chris@34 631 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 632 ++j;
Chris@34 633
Chris@34 634 if (j != points.end()) {
Chris@34 635 const SparseTimeValueModel::Point &q(*j);
Chris@44 636 nx = v->getXForFrame(q.frame);
Chris@44 637 ny = getYForValue(v, q.value);
Chris@34 638 haveNext = true;
Chris@76 639 }
Chris@76 640
Chris@76 641 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
Chris@76 642 // << ", nx = " << nx << std::endl;
Chris@34 643
Chris@34 644 int labelY = y;
Chris@34 645
Chris@0 646 if (w < 1) w = 1;
Chris@26 647 paint.setPen(m_colour);
Chris@0 648
Chris@26 649 if (m_plotStyle == PlotSegmentation) {
Chris@197 650 paint.setPen(Qt::black);
Chris@101 651 paint.setBrush(getColourForValue(v, p.value));
Chris@44 652 labelY = v->height();
Chris@26 653 } else if (m_plotStyle == PlotLines ||
Chris@26 654 m_plotStyle == PlotCurve) {
Chris@6 655 paint.setBrush(Qt::NoBrush);
Chris@3 656 } else {
Chris@6 657 paint.setBrush(brushColour);
Chris@3 658 }
Chris@0 659
Chris@0 660 if (m_plotStyle == PlotStems) {
Chris@0 661 paint.setPen(brushColour);
Chris@0 662 if (y < origin - 1) {
Chris@0 663 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 664 } else if (y > origin + 1) {
Chris@0 665 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 666 }
Chris@0 667 paint.setPen(m_colour);
Chris@0 668 }
Chris@0 669
Chris@0 670 if (illuminateFrame == p.frame) {
Chris@6 671
Chris@0 672 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 673 //better to save the highlighted rects and draw them at
Chris@0 674 //the end perhaps
Chris@6 675
Chris@6 676 //!!! not equipped to illuminate the right section in line
Chris@6 677 //or curve mode
Chris@6 678
Chris@6 679 if (m_plotStyle != PlotCurve &&
Chris@6 680 m_plotStyle != PlotLines) {
Chris@6 681 paint.setPen(Qt::black);//!!!
Chris@26 682 if (m_plotStyle != PlotSegmentation) {
Chris@26 683 paint.setBrush(Qt::black);//!!!
Chris@26 684 }
Chris@6 685 }
Chris@0 686 }
Chris@0 687
Chris@6 688 if (m_plotStyle != PlotLines &&
Chris@26 689 m_plotStyle != PlotCurve &&
Chris@26 690 m_plotStyle != PlotSegmentation) {
Chris@3 691 paint.drawRect(x, y - 1, w, 2);
Chris@3 692 }
Chris@0 693
Chris@6 694 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 695 m_plotStyle == PlotLines ||
Chris@6 696 m_plotStyle == PlotCurve) {
Chris@0 697
Chris@34 698 if (haveNext) {
Chris@3 699
Chris@6 700 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 701
Chris@79 702 paint.save();
Chris@3 703 paint.setPen(brushColour);
Chris@3 704 paint.drawLine(x + w, y, nx, ny);
Chris@79 705 paint.restore();
Chris@6 706
Chris@6 707 } else if (m_plotStyle == PlotLines) {
Chris@6 708
Chris@6 709 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 710
Chris@3 711 } else {
Chris@6 712
Chris@55 713 float x0 = x + float(w)/2;
Chris@55 714 float x1 = nx + float(w)/2;
Chris@55 715
Chris@55 716 float y0 = y;
Chris@55 717 float y1 = ny;
Chris@55 718
Chris@55 719 if (pointCount == 0) {
Chris@55 720 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 721 }
Chris@55 722 ++pointCount;
Chris@6 723
Chris@6 724 if (nx - x > 5) {
Chris@55 725 path.cubicTo(x0, y0,
Chris@55 726 x0, y0,
Chris@55 727 (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 728
Chris@55 729 // // or
Chris@55 730 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 731
Chris@6 732 } else {
Chris@55 733 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 734 }
Chris@3 735 }
Chris@0 736 }
Chris@0 737 }
Chris@0 738
Chris@26 739 if (m_plotStyle == PlotSegmentation) {
Chris@76 740
Chris@76 741 // std::cerr << "drawing rect" << std::endl;
Chris@26 742
Chris@27 743 if (nx <= x) continue;
Chris@26 744
Chris@28 745 if (illuminateFrame != p.frame &&
Chris@44 746 (nx < x + 5 || x >= v->width() - 1)) {
Chris@27 747 paint.setPen(Qt::NoPen);
Chris@27 748 }
Chris@26 749
Chris@44 750 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 751 }
Chris@26 752
Chris@55 753 if (p.label != "") {
Chris@79 754 if (!haveNext || nx > x + 6 + paint.fontMetrics().width(p.label)) {
Chris@79 755 paint.drawText(x + 5, textY, p.label);
Chris@79 756 }
Chris@55 757 }
Chris@0 758 }
Chris@6 759
Chris@6 760 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@55 761 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
Chris@6 762 paint.drawPath(path);
Chris@6 763 }
Chris@6 764
Chris@6 765 paint.restore();
Chris@6 766
Chris@6 767 // looks like save/restore doesn't deal with this:
Chris@6 768 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 769 }
Chris@6 770
Chris@42 771 int
Chris@44 772 TimeValueLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@42 773 {
Chris@68 774 int w = paint.fontMetrics().width("-000.000");
Chris@68 775 if (m_plotStyle == PlotSegmentation) return w + 20;
Chris@68 776 else return w + 10;
Chris@42 777 }
Chris@42 778
Chris@42 779 void
Chris@44 780 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@42 781 {
Chris@42 782 if (!m_model) return;
Chris@42 783
Chris@68 784 int h = v->height();
Chris@68 785
Chris@68 786 int n = 10;
Chris@68 787
Chris@68 788 float max = m_model->getValueMaximum();
Chris@68 789 float min = m_model->getValueMinimum();
Chris@68 790 float val = min;
Chris@68 791 float inc = (max - val) / n;
Chris@42 792
Chris@55 793 char buffer[40];
Chris@55 794
Chris@55 795 int w = getVerticalScaleWidth(v, paint);
Chris@55 796
Chris@68 797 int tx = 5;
Chris@68 798
Chris@68 799 int boxx = 5, boxy = 5;
Chris@68 800 if (m_model->getScaleUnits() != "") {
Chris@68 801 boxy += paint.fontMetrics().height();
Chris@68 802 }
Chris@68 803 int boxw = 10, boxh = h - boxy - 5;
Chris@68 804
Chris@68 805 if (m_plotStyle == PlotSegmentation) {
Chris@68 806 tx += boxx + boxw;
Chris@68 807 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@68 808 }
Chris@68 809
Chris@68 810 if (m_plotStyle == PlotSegmentation) {
Chris@68 811 paint.save();
Chris@68 812 for (int y = 0; y < boxh; ++y) {
Chris@68 813 float val = ((boxh - y) * (max - min)) / boxh + min;
Chris@101 814 paint.setPen(getColourForValue(v, val));
Chris@68 815 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@68 816 }
Chris@68 817 paint.restore();
Chris@68 818 }
Chris@68 819
Chris@68 820 for (int i = 0; i < n; ++i) {
Chris@68 821
Chris@68 822 int y, ty;
Chris@68 823 bool drawText = true;
Chris@68 824
Chris@68 825 if (m_plotStyle == PlotSegmentation) {
Chris@68 826 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@68 827 ty = y;
Chris@68 828 } else {
Chris@68 829 if (i == n-1) {
Chris@68 830 if (m_model->getScaleUnits() != "") drawText = false;
Chris@68 831 }
Chris@68 832 y = getYForValue(v, val);
Chris@68 833 ty = y - paint.fontMetrics().height() +
Chris@68 834 paint.fontMetrics().ascent();
Chris@68 835 }
Chris@68 836
Chris@68 837 sprintf(buffer, "%.3f", val);
Chris@55 838 QString label = QString(buffer);
Chris@68 839
Chris@68 840 if (m_plotStyle != PlotSegmentation) {
Chris@68 841 paint.drawLine(w - 5, y, w, y);
Chris@68 842 } else {
Chris@68 843 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@68 844 }
Chris@68 845
Chris@68 846 if (drawText) paint.drawText(tx, ty, label);
Chris@44 847 val += inc;
Chris@42 848 }
Chris@68 849
Chris@67 850 if (m_model->getScaleUnits() != "") {
Chris@67 851 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
Chris@67 852 m_model->getScaleUnits());
Chris@67 853 }
Chris@42 854 }
Chris@42 855
Chris@21 856 void
Chris@44 857 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 858 {
Chris@101 859 // std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 860
Chris@21 861 if (!m_model) return;
Chris@21 862
Chris@44 863 long frame = v->getFrameForX(e->x());
Chris@76 864 long resolution = m_model->getResolution();
Chris@21 865 if (frame < 0) frame = 0;
Chris@76 866 frame = (frame / resolution) * resolution;
Chris@21 867
Chris@44 868 float value = getValueForY(v, e->y());
Chris@21 869
Chris@76 870 bool havePoint = false;
Chris@76 871
Chris@76 872 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 873 if (!points.empty()) {
Chris@76 874 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 875 i != points.end(); ++i) {
Chris@76 876 if (((i->frame / resolution) * resolution) != frame) {
Chris@101 877 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 878 continue;
Chris@76 879 }
Chris@76 880 m_editingPoint = *i;
Chris@76 881 havePoint = true;
Chris@76 882 }
Chris@76 883 }
Chris@76 884
Chris@76 885 if (!havePoint) {
Chris@76 886 m_editingPoint = SparseTimeValueModel::Point
Chris@76 887 (frame, value, tr("New Point"));
Chris@76 888 }
Chris@76 889
Chris@23 890 m_originalPoint = m_editingPoint;
Chris@22 891
Chris@22 892 if (m_editingCommand) m_editingCommand->finish();
Chris@22 893 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 894 tr("Draw Point"));
Chris@76 895 if (!havePoint) {
Chris@76 896 m_editingCommand->addPoint(m_editingPoint);
Chris@76 897 }
Chris@22 898
Chris@21 899 m_editing = true;
Chris@21 900 }
Chris@21 901
Chris@21 902 void
Chris@44 903 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 904 {
Chris@101 905 // std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 906
Chris@21 907 if (!m_model || !m_editing) return;
Chris@21 908
Chris@44 909 long frame = v->getFrameForX(e->x());
Chris@76 910 long resolution = m_model->getResolution();
Chris@21 911 if (frame < 0) frame = 0;
Chris@76 912 frame = (frame / resolution) * resolution;
Chris@21 913
Chris@44 914 float value = getValueForY(v, e->y());
Chris@21 915
Chris@76 916 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 917
Chris@101 918 // std::cerr << points.size() << " points" << std::endl;
Chris@76 919
Chris@76 920 bool havePoint = false;
Chris@76 921
Chris@76 922 if (!points.empty()) {
Chris@76 923 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 924 i != points.end(); ++i) {
Chris@76 925 if (i->frame == m_editingPoint.frame &&
Chris@76 926 i->value == m_editingPoint.value) {
Chris@101 927 // std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
Chris@76 928 continue;
Chris@76 929 }
Chris@76 930 if (((i->frame / resolution) * resolution) != frame) {
Chris@101 931 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 932 continue;
Chris@76 933 }
Chris@101 934 // std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
Chris@76 935 m_editingPoint = *i;
Chris@76 936 m_originalPoint = m_editingPoint;
Chris@76 937 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 938 havePoint = true;
Chris@76 939 }
Chris@76 940 }
Chris@76 941
Chris@76 942 if (!havePoint) {
Chris@76 943 if (frame == m_editingPoint.frame) {
Chris@76 944 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 945 }
Chris@76 946 }
Chris@76 947
Chris@76 948 // m_editingCommand->deletePoint(m_editingPoint);
Chris@21 949 m_editingPoint.frame = frame;
Chris@21 950 m_editingPoint.value = value;
Chris@22 951 m_editingCommand->addPoint(m_editingPoint);
Chris@21 952 }
Chris@21 953
Chris@21 954 void
Chris@44 955 TimeValueLayer::drawEnd(View *v, QMouseEvent *e)
Chris@21 956 {
Chris@101 957 // std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 958 if (!m_model || !m_editing) return;
Chris@22 959 m_editingCommand->finish();
Chris@22 960 m_editingCommand = 0;
Chris@21 961 m_editing = false;
Chris@21 962 }
Chris@21 963
Chris@21 964 void
Chris@44 965 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 966 {
Chris@101 967 // std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 968
Chris@21 969 if (!m_model) return;
Chris@21 970
Chris@44 971 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 972 if (points.empty()) return;
Chris@21 973
Chris@21 974 m_editingPoint = *points.begin();
Chris@23 975 m_originalPoint = m_editingPoint;
Chris@22 976
Chris@22 977 if (m_editingCommand) {
Chris@22 978 m_editingCommand->finish();
Chris@22 979 m_editingCommand = 0;
Chris@22 980 }
Chris@22 981
Chris@21 982 m_editing = true;
Chris@21 983 }
Chris@21 984
Chris@21 985 void
Chris@44 986 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 987 {
Chris@101 988 // std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 989
Chris@21 990 if (!m_model || !m_editing) return;
Chris@21 991
Chris@44 992 long frame = v->getFrameForX(e->x());
Chris@21 993 if (frame < 0) frame = 0;
Chris@21 994 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 995
Chris@44 996 float value = getValueForY(v, e->y());
Chris@21 997
Chris@22 998 if (!m_editingCommand) {
Chris@22 999 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 1000 tr("Drag Point"));
Chris@22 1001 }
Chris@22 1002
Chris@22 1003 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 1004 m_editingPoint.frame = frame;
Chris@21 1005 m_editingPoint.value = value;
Chris@22 1006 m_editingCommand->addPoint(m_editingPoint);
Chris@21 1007 }
Chris@21 1008
Chris@21 1009 void
Chris@44 1010 TimeValueLayer::editEnd(View *v, QMouseEvent *e)
Chris@21 1011 {
Chris@101 1012 // std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 1013 if (!m_model || !m_editing) return;
Chris@23 1014
Chris@23 1015 if (m_editingCommand) {
Chris@23 1016
Chris@23 1017 QString newName = m_editingCommand->getName();
Chris@23 1018
Chris@23 1019 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 1020 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 1021 newName = tr("Edit Point");
Chris@23 1022 } else {
Chris@23 1023 newName = tr("Relocate Point");
Chris@23 1024 }
Chris@23 1025 } else {
Chris@23 1026 newName = tr("Change Point Value");
Chris@23 1027 }
Chris@23 1028
Chris@23 1029 m_editingCommand->setName(newName);
Chris@23 1030 m_editingCommand->finish();
Chris@23 1031 }
Chris@23 1032
Chris@22 1033 m_editingCommand = 0;
Chris@21 1034 m_editing = false;
Chris@21 1035 }
Chris@21 1036
Chris@43 1037 void
Chris@70 1038 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1039 {
Chris@70 1040 if (!m_model) return;
Chris@70 1041
Chris@70 1042 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@70 1043 if (points.empty()) return;
Chris@70 1044
Chris@70 1045 SparseTimeValueModel::Point point = *points.begin();
Chris@70 1046
Chris@70 1047 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1048 (m_model->getSampleRate(),
Chris@70 1049 ItemEditDialog::ShowTime |
Chris@70 1050 ItemEditDialog::ShowValue |
Chris@73 1051 ItemEditDialog::ShowText,
Chris@73 1052 m_model->getScaleUnits());
Chris@70 1053
Chris@70 1054 dialog->setFrameTime(point.frame);
Chris@70 1055 dialog->setValue(point.value);
Chris@70 1056 dialog->setText(point.label);
Chris@70 1057
Chris@70 1058 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1059
Chris@70 1060 SparseTimeValueModel::Point newPoint = point;
Chris@70 1061 newPoint.frame = dialog->getFrameTime();
Chris@70 1062 newPoint.value = dialog->getValue();
Chris@70 1063 newPoint.label = dialog->getText();
Chris@70 1064
Chris@70 1065 SparseTimeValueModel::EditCommand *command =
Chris@70 1066 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
Chris@70 1067 command->deletePoint(point);
Chris@70 1068 command->addPoint(newPoint);
Chris@70 1069 command->finish();
Chris@70 1070 }
Chris@70 1071
Chris@70 1072 delete dialog;
Chris@70 1073 }
Chris@70 1074
Chris@70 1075 void
Chris@43 1076 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1077 {
Chris@99 1078 if (!m_model) return;
Chris@99 1079
Chris@43 1080 SparseTimeValueModel::EditCommand *command =
Chris@43 1081 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1082 tr("Drag Selection"));
Chris@43 1083
Chris@43 1084 SparseTimeValueModel::PointList points =
Chris@43 1085 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1086
Chris@43 1087 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1088 i != points.end(); ++i) {
Chris@43 1089
Chris@43 1090 if (s.contains(i->frame)) {
Chris@43 1091 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1092 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1093 command->deletePoint(*i);
Chris@43 1094 command->addPoint(newPoint);
Chris@43 1095 }
Chris@43 1096 }
Chris@43 1097
Chris@43 1098 command->finish();
Chris@43 1099 }
Chris@43 1100
Chris@43 1101 void
Chris@43 1102 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1103 {
Chris@99 1104 if (!m_model) return;
Chris@99 1105
Chris@43 1106 SparseTimeValueModel::EditCommand *command =
Chris@43 1107 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1108 tr("Resize Selection"));
Chris@43 1109
Chris@43 1110 SparseTimeValueModel::PointList points =
Chris@43 1111 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1112
Chris@43 1113 double ratio =
Chris@43 1114 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1115 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1116
Chris@43 1117 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1118 i != points.end(); ++i) {
Chris@43 1119
Chris@43 1120 if (s.contains(i->frame)) {
Chris@43 1121
Chris@43 1122 double target = i->frame;
Chris@43 1123 target = newSize.getStartFrame() +
Chris@43 1124 double(target - s.getStartFrame()) * ratio;
Chris@43 1125
Chris@43 1126 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1127 newPoint.frame = lrint(target);
Chris@43 1128 command->deletePoint(*i);
Chris@43 1129 command->addPoint(newPoint);
Chris@43 1130 }
Chris@43 1131 }
Chris@43 1132
Chris@43 1133 command->finish();
Chris@43 1134 }
Chris@43 1135
Chris@76 1136 void
Chris@76 1137 TimeValueLayer::deleteSelection(Selection s)
Chris@76 1138 {
Chris@99 1139 if (!m_model) return;
Chris@99 1140
Chris@76 1141 SparseTimeValueModel::EditCommand *command =
Chris@76 1142 new SparseTimeValueModel::EditCommand(m_model,
Chris@76 1143 tr("Delete Selected Points"));
Chris@76 1144
Chris@76 1145 SparseTimeValueModel::PointList points =
Chris@76 1146 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1147
Chris@76 1148 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1149 i != points.end(); ++i) {
Chris@76 1150
Chris@76 1151 if (s.contains(i->frame)) {
Chris@76 1152 command->deletePoint(*i);
Chris@76 1153 }
Chris@76 1154 }
Chris@76 1155
Chris@76 1156 command->finish();
Chris@76 1157 }
Chris@76 1158
Chris@76 1159 void
Chris@76 1160 TimeValueLayer::copy(Selection s, Clipboard &to)
Chris@76 1161 {
Chris@99 1162 if (!m_model) return;
Chris@99 1163
Chris@76 1164 SparseTimeValueModel::PointList points =
Chris@76 1165 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1166
Chris@76 1167 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1168 i != points.end(); ++i) {
Chris@76 1169 if (s.contains(i->frame)) {
Chris@83 1170 Clipboard::Point point(i->frame, i->value, i->label);
Chris@76 1171 to.addPoint(point);
Chris@76 1172 }
Chris@76 1173 }
Chris@76 1174 }
Chris@76 1175
Chris@125 1176 bool
Chris@125 1177 TimeValueLayer::paste(const Clipboard &from, int frameOffset,
Chris@125 1178 bool interactive)
Chris@76 1179 {
Chris@125 1180 if (!m_model) return false;
Chris@99 1181
Chris@76 1182 const Clipboard::PointList &points = from.getPoints();
Chris@76 1183
Chris@76 1184 SparseTimeValueModel::EditCommand *command =
Chris@76 1185 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
Chris@76 1186
Chris@125 1187 enum ValueAvailability {
Chris@125 1188 UnknownAvailability,
Chris@125 1189 NoValues,
Chris@125 1190 SomeValues,
Chris@125 1191 AllValues
Chris@125 1192 };
Chris@125 1193 enum ValueGeneration {
Chris@125 1194 GenerateNone,
Chris@125 1195 GenerateFromCounter,
Chris@125 1196 GenerateFromFrameNumber,
Chris@125 1197 GenerateFromRealTime,
Chris@125 1198 GenerateFromRealTimeDifference,
Chris@125 1199 GenerateFromTempo,
Chris@125 1200 GenerateFromExistingNeighbour,
Chris@125 1201 GenerateFromLabels
Chris@125 1202 };
Chris@125 1203
Chris@125 1204 ValueGeneration generation = GenerateNone;
Chris@125 1205
Chris@125 1206 bool haveUsableLabels = false;
Chris@125 1207 bool haveExistingItems = !(m_model->isEmpty());
Chris@125 1208
Chris@125 1209 if (interactive) {
Chris@125 1210
Chris@125 1211 ValueAvailability availability = UnknownAvailability;
Chris@125 1212
Chris@125 1213 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@125 1214 i != points.end(); ++i) {
Chris@125 1215
Chris@125 1216 if (!i->haveFrame()) continue;
Chris@125 1217
Chris@125 1218 if (availability == UnknownAvailability) {
Chris@125 1219 if (i->haveValue()) availability = AllValues;
Chris@125 1220 else availability = NoValues;
Chris@125 1221 continue;
Chris@125 1222 }
Chris@125 1223
Chris@125 1224 if (i->haveValue()) {
Chris@125 1225 if (availability == NoValues) {
Chris@125 1226 availability = SomeValues;
Chris@125 1227 }
Chris@125 1228 } else {
Chris@125 1229 if (availability == AllValues) {
Chris@125 1230 availability = SomeValues;
Chris@125 1231 }
Chris@125 1232 }
Chris@125 1233
Chris@125 1234 if (!haveUsableLabels) {
Chris@125 1235 if (i->haveLabel()) {
Chris@125 1236 if (i->getLabel().contains(QRegExp("[0-9]"))) {
Chris@125 1237 haveUsableLabels = true;
Chris@125 1238 }
Chris@125 1239 }
Chris@125 1240 }
Chris@125 1241
Chris@125 1242 if (availability == SomeValues && haveUsableLabels) break;
Chris@125 1243 }
Chris@125 1244
Chris@125 1245 if (availability == NoValues || availability == SomeValues) {
Chris@125 1246
Chris@125 1247 QString text;
Chris@125 1248 if (availability == NoValues) {
Chris@125 1249 text = tr("The items you are pasting do not have values.\nWhat values do you want to use for these items?");
Chris@125 1250 } else {
Chris@125 1251 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 1252 }
Chris@125 1253
Chris@125 1254 QStringList options;
Chris@125 1255 std::vector<int> genopts;
Chris@125 1256
Chris@125 1257 options << tr("Zero for all items");
Chris@125 1258 genopts.push_back(int(GenerateNone));
Chris@125 1259
Chris@125 1260 options << tr("Whole numbers counting from 1");
Chris@125 1261 genopts.push_back(int(GenerateFromCounter));
Chris@125 1262
Chris@125 1263 options << tr("Item's audio sample frame number");
Chris@125 1264 genopts.push_back(int(GenerateFromFrameNumber));
Chris@125 1265
Chris@125 1266 options << tr("Item's time in seconds");
Chris@125 1267 genopts.push_back(int(GenerateFromRealTime));
Chris@125 1268
Chris@125 1269 options << tr("Duration from the item to the following item");
Chris@125 1270 genopts.push_back(int(GenerateFromRealTimeDifference));
Chris@125 1271
Chris@125 1272 options << tr("Tempo in bpm derived from the duration");
Chris@125 1273 genopts.push_back(int(GenerateFromTempo));
Chris@125 1274
Chris@125 1275 if (haveExistingItems) {
Chris@125 1276 options << tr("Value of the nearest existing item");
Chris@125 1277 genopts.push_back(int(GenerateFromExistingNeighbour));
Chris@125 1278 }
Chris@125 1279
Chris@125 1280 if (haveUsableLabels) {
Chris@125 1281 options << tr("Value extracted from the item's label (where possible)");
Chris@125 1282 genopts.push_back(int(GenerateFromLabels));
Chris@125 1283 }
Chris@125 1284
Chris@125 1285
Chris@125 1286 static int prevSelection = 0;
Chris@125 1287
Chris@125 1288 bool ok = false;
Chris@125 1289 QString selected = ListInputDialog::getItem
Chris@125 1290 (0, tr("Choose value calculation"),
Chris@125 1291 text, options, prevSelection, &ok);
Chris@125 1292
Chris@125 1293 if (!ok) return false;
Chris@125 1294 int selection = 0;
Chris@125 1295 generation = GenerateNone;
Chris@125 1296
Chris@125 1297 for (QStringList::const_iterator i = options.begin();
Chris@125 1298 i != options.end(); ++i) {
Chris@125 1299 if (selected == *i) {
Chris@125 1300 generation = ValueGeneration(genopts[selection]);
Chris@125 1301 break;
Chris@125 1302 }
Chris@125 1303 ++selection;
Chris@125 1304 }
Chris@125 1305
Chris@125 1306 prevSelection = selection;
Chris@125 1307 }
Chris@125 1308 }
Chris@125 1309
Chris@125 1310 int counter = 1;
Chris@125 1311 float prevBpm = 120.f;
Chris@125 1312
Chris@76 1313 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1314 i != points.end(); ++i) {
Chris@76 1315
Chris@76 1316 if (!i->haveFrame()) continue;
Chris@76 1317 size_t frame = 0;
Chris@76 1318 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
Chris@76 1319 frame = i->getFrame() + frameOffset;
Chris@76 1320 }
Chris@76 1321 SparseTimeValueModel::Point newPoint(frame);
Chris@76 1322
Chris@125 1323 if (i->haveLabel()) {
Chris@125 1324 newPoint.label = i->getLabel();
Chris@125 1325 } else if (i->haveValue()) {
Chris@125 1326 newPoint.label = QString("%1").arg(i->getValue());
Chris@125 1327 }
Chris@125 1328
Chris@125 1329 if (i->haveValue()) {
Chris@125 1330 newPoint.value = i->getValue();
Chris@125 1331 } else {
Chris@125 1332
Chris@125 1333 switch (generation) {
Chris@125 1334
Chris@125 1335 case GenerateNone:
Chris@125 1336 newPoint.value = 0;
Chris@125 1337 break;
Chris@125 1338
Chris@125 1339 case GenerateFromCounter:
Chris@125 1340 newPoint.value = counter;
Chris@125 1341 break;
Chris@125 1342
Chris@125 1343 case GenerateFromFrameNumber:
Chris@125 1344 newPoint.value = frame;
Chris@125 1345 break;
Chris@125 1346
Chris@125 1347 case GenerateFromRealTime:
Chris@125 1348 newPoint.value = float(frame) / float(m_model->getSampleRate());
Chris@125 1349 break;
Chris@125 1350
Chris@125 1351 case GenerateFromRealTimeDifference:
Chris@125 1352 case GenerateFromTempo:
Chris@125 1353 {
Chris@125 1354 size_t nextFrame = frame;
Chris@125 1355 Clipboard::PointList::const_iterator j = i;
Chris@125 1356 for (; j != points.end(); ++j) {
Chris@125 1357 if (!j->haveFrame()) continue;
Chris@125 1358 if (j != i) break;
Chris@125 1359 }
Chris@125 1360 if (j != points.end()) {
Chris@125 1361 nextFrame = j->getFrame();
Chris@125 1362 }
Chris@125 1363 if (generation == GenerateFromRealTimeDifference) {
Chris@125 1364 newPoint.value = float(nextFrame - frame) /
Chris@125 1365 float(m_model->getSampleRate());
Chris@125 1366 } else {
Chris@125 1367 float bpm = prevBpm;
Chris@125 1368 if (nextFrame > frame) {
Chris@125 1369 bpm = (60.f * m_model->getSampleRate()) /
Chris@125 1370 (nextFrame - frame);
Chris@125 1371 }
Chris@125 1372 newPoint.value = bpm;
Chris@125 1373 prevBpm = bpm;
Chris@125 1374 }
Chris@125 1375 break;
Chris@125 1376 }
Chris@125 1377
Chris@125 1378 case GenerateFromExistingNeighbour:
Chris@125 1379 {
Chris@125 1380 SparseTimeValueModel::PointList points =
Chris@125 1381 m_model->getPoints(frame);
Chris@125 1382 if (points.empty()) points = m_model->getPreviousPoints(frame);
Chris@125 1383 if (points.empty()) points = m_model->getNextPoints(frame);
Chris@125 1384 if (points.empty()) {
Chris@125 1385 newPoint.value = 0.f;
Chris@125 1386 } else {
Chris@125 1387 newPoint.value = points.begin()->value;
Chris@125 1388 }
Chris@125 1389 }
Chris@125 1390
Chris@125 1391 case GenerateFromLabels:
Chris@125 1392 if (i->haveLabel()) {
Chris@125 1393 // more forgiving than QString::toFloat()
Chris@125 1394 newPoint.value = atof(i->getLabel().toLocal8Bit());
Chris@125 1395 } else {
Chris@125 1396 newPoint.value = 0.f;
Chris@125 1397 }
Chris@125 1398 }
Chris@125 1399 }
Chris@76 1400
Chris@76 1401 command->addPoint(newPoint);
Chris@125 1402
Chris@125 1403 ++counter;
Chris@76 1404 }
Chris@76 1405
Chris@76 1406 command->finish();
Chris@125 1407 return true;
Chris@76 1408 }
Chris@76 1409
Chris@6 1410 QString
Chris@6 1411 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 1412 {
Chris@6 1413 return Layer::toXmlString(indent, extraAttributes +
Chris@6 1414 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@6 1415 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@0 1416 }
Chris@0 1417
Chris@11 1418 void
Chris@11 1419 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1420 {
Chris@11 1421 QString colourSpec = attributes.value("colour");
Chris@11 1422 if (colourSpec != "") {
Chris@11 1423 QColor colour(colourSpec);
Chris@11 1424 if (colour.isValid()) {
Chris@11 1425 setBaseColour(QColor(colourSpec));
Chris@11 1426 }
Chris@11 1427 }
Chris@11 1428
Chris@11 1429 bool ok;
Chris@11 1430 PlotStyle style = (PlotStyle)
Chris@11 1431 attributes.value("plotStyle").toInt(&ok);
Chris@11 1432 if (ok) setPlotStyle(style);
Chris@11 1433 }
Chris@11 1434
Chris@0 1435
Chris@0 1436 #ifdef INCLUDE_MOCFILES
Chris@0 1437 #include "TimeValueLayer.moc.cpp"
Chris@0 1438 #endif
Chris@0 1439