annotate layer/TimeValueLayer.cpp @ 199:45e995ed84d9

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