annotate layer/TimeValueLayer.cpp @ 272:87e4c880b4c8

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