annotate layer/TimeValueLayer.cpp @ 240:101f71a97df4

...
author Chris Cannam
date Mon, 23 Apr 2007 12:15:13 +0000
parents 34bbbcb3c01f
children 28c8e8e3c537
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@44 339 } else if (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 float logthresh = -80;
Chris@197 554
Chris@197 555 if (min > max) std::swap(min, max);
Chris@197 556 if (max == min) max = min + 1;
Chris@197 557
Chris@101 558 if (log) {
Chris@197 559 LogRange::mapRange(min, max);
Chris@197 560 val = LogRange::map(val);
Chris@68 561 }
Chris@68 562
Chris@197 563 // std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
Chris@197 564 // << max << ", log " << log << ", value " << val << std::endl;
Chris@68 565
Chris@197 566 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
Chris@197 567 return QColor(solid.red(), solid.green(), solid.blue(), 120);
Chris@68 568 }
Chris@68 569
Chris@0 570 void
Chris@44 571 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 572 {
Chris@0 573 if (!m_model || !m_model->isOK()) return;
Chris@0 574
Chris@0 575 int sampleRate = m_model->getSampleRate();
Chris@0 576 if (!sampleRate) return;
Chris@0 577
Chris@0 578 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 579
Chris@0 580 int x0 = rect.left(), x1 = rect.right();
Chris@44 581 long frame0 = v->getFrameForX(x0);
Chris@44 582 long frame1 = v->getFrameForX(x1);
Chris@0 583
Chris@0 584 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 585 (frame0, frame1));
Chris@11 586 if (points.empty()) return;
Chris@0 587
Chris@0 588 paint.setPen(m_colour);
Chris@0 589
Chris@0 590 QColor brushColour(m_colour);
Chris@0 591 brushColour.setAlpha(80);
Chris@0 592 paint.setBrush(brushColour);
Chris@0 593
Chris@0 594 // std::cerr << "TimeValueLayer::paint: resolution is "
Chris@0 595 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 596
Chris@0 597 float min = m_model->getValueMinimum();
Chris@0 598 float max = m_model->getValueMaximum();
Chris@0 599 if (max == min) max = min + 1.0;
Chris@0 600
Chris@44 601 int origin = int(nearbyint(v->height() -
Chris@44 602 (-min * v->height()) / (max - min)));
Chris@0 603
Chris@0 604 QPoint localPos;
Chris@0 605 long illuminateFrame = -1;
Chris@0 606
Chris@44 607 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 608 SparseTimeValueModel::PointList localPoints =
Chris@44 609 getLocalPoints(v, localPos.x());
Chris@0 610 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 611 }
Chris@6 612
Chris@20 613 int w =
Chris@44 614 v->getXForFrame(frame0 + m_model->getResolution()) -
Chris@44 615 v->getXForFrame(frame0);
Chris@7 616
Chris@6 617 paint.save();
Chris@6 618
Chris@6 619 QPainterPath path;
Chris@55 620 int pointCount = 0;
Chris@79 621
Chris@79 622 int textY = 0;
Chris@79 623 if (m_plotStyle == PlotSegmentation) {
Chris@79 624 textY = v->getTextLabelHeight(this, paint);
Chris@79 625 }
Chris@6 626
Chris@0 627 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 628 i != points.end(); ++i) {
Chris@0 629
Chris@0 630 const SparseTimeValueModel::Point &p(*i);
Chris@0 631
Chris@44 632 int x = v->getXForFrame(p.frame);
Chris@44 633 int y = getYForValue(v, p.value);
Chris@0 634
Chris@79 635 if (m_plotStyle != PlotSegmentation) {
Chris@79 636 textY = y - paint.fontMetrics().height()
Chris@79 637 + paint.fontMetrics().ascent();
Chris@79 638 }
Chris@79 639
Chris@34 640 bool haveNext = false;
Chris@76 641 int nx = v->getXForFrame(v->getModelsEndFrame());
Chris@76 642 // m_model->getEndFrame());
Chris@34 643 int ny = y;
Chris@34 644
Chris@34 645 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 646 ++j;
Chris@34 647
Chris@34 648 if (j != points.end()) {
Chris@34 649 const SparseTimeValueModel::Point &q(*j);
Chris@44 650 nx = v->getXForFrame(q.frame);
Chris@44 651 ny = getYForValue(v, q.value);
Chris@34 652 haveNext = true;
Chris@76 653 }
Chris@76 654
Chris@76 655 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
Chris@76 656 // << ", nx = " << nx << std::endl;
Chris@34 657
Chris@34 658 int labelY = y;
Chris@34 659
Chris@0 660 if (w < 1) w = 1;
Chris@26 661 paint.setPen(m_colour);
Chris@0 662
Chris@26 663 if (m_plotStyle == PlotSegmentation) {
Chris@197 664 paint.setPen(Qt::black);
Chris@101 665 paint.setBrush(getColourForValue(v, p.value));
Chris@44 666 labelY = v->height();
Chris@26 667 } else if (m_plotStyle == PlotLines ||
Chris@26 668 m_plotStyle == PlotCurve) {
Chris@6 669 paint.setBrush(Qt::NoBrush);
Chris@3 670 } else {
Chris@6 671 paint.setBrush(brushColour);
Chris@3 672 }
Chris@0 673
Chris@0 674 if (m_plotStyle == PlotStems) {
Chris@0 675 paint.setPen(brushColour);
Chris@0 676 if (y < origin - 1) {
Chris@0 677 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 678 } else if (y > origin + 1) {
Chris@0 679 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 680 }
Chris@0 681 paint.setPen(m_colour);
Chris@0 682 }
Chris@0 683
Chris@0 684 if (illuminateFrame == p.frame) {
Chris@6 685
Chris@0 686 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 687 //better to save the highlighted rects and draw them at
Chris@0 688 //the end perhaps
Chris@6 689
Chris@6 690 //!!! not equipped to illuminate the right section in line
Chris@6 691 //or curve mode
Chris@6 692
Chris@6 693 if (m_plotStyle != PlotCurve &&
Chris@6 694 m_plotStyle != PlotLines) {
Chris@6 695 paint.setPen(Qt::black);//!!!
Chris@26 696 if (m_plotStyle != PlotSegmentation) {
Chris@26 697 paint.setBrush(Qt::black);//!!!
Chris@26 698 }
Chris@6 699 }
Chris@0 700 }
Chris@0 701
Chris@6 702 if (m_plotStyle != PlotLines &&
Chris@26 703 m_plotStyle != PlotCurve &&
Chris@26 704 m_plotStyle != PlotSegmentation) {
Chris@3 705 paint.drawRect(x, y - 1, w, 2);
Chris@3 706 }
Chris@0 707
Chris@6 708 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 709 m_plotStyle == PlotLines ||
Chris@6 710 m_plotStyle == PlotCurve) {
Chris@0 711
Chris@34 712 if (haveNext) {
Chris@3 713
Chris@6 714 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 715
Chris@79 716 paint.save();
Chris@3 717 paint.setPen(brushColour);
Chris@3 718 paint.drawLine(x + w, y, nx, ny);
Chris@79 719 paint.restore();
Chris@6 720
Chris@6 721 } else if (m_plotStyle == PlotLines) {
Chris@6 722
Chris@6 723 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 724
Chris@3 725 } else {
Chris@6 726
Chris@55 727 float x0 = x + float(w)/2;
Chris@55 728 float x1 = nx + float(w)/2;
Chris@55 729
Chris@55 730 float y0 = y;
Chris@55 731 float y1 = ny;
Chris@55 732
Chris@55 733 if (pointCount == 0) {
Chris@55 734 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 735 }
Chris@55 736 ++pointCount;
Chris@6 737
Chris@6 738 if (nx - x > 5) {
Chris@55 739 path.cubicTo(x0, y0,
Chris@55 740 x0, y0,
Chris@55 741 (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 742
Chris@55 743 // // or
Chris@55 744 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 745
Chris@6 746 } else {
Chris@55 747 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 748 }
Chris@3 749 }
Chris@0 750 }
Chris@0 751 }
Chris@0 752
Chris@26 753 if (m_plotStyle == PlotSegmentation) {
Chris@76 754
Chris@76 755 // std::cerr << "drawing rect" << std::endl;
Chris@26 756
Chris@27 757 if (nx <= x) continue;
Chris@26 758
Chris@28 759 if (illuminateFrame != p.frame &&
Chris@44 760 (nx < x + 5 || x >= v->width() - 1)) {
Chris@27 761 paint.setPen(Qt::NoPen);
Chris@27 762 }
Chris@26 763
Chris@44 764 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 765 }
Chris@26 766
Chris@55 767 if (p.label != "") {
Chris@79 768 if (!haveNext || nx > x + 6 + paint.fontMetrics().width(p.label)) {
Chris@79 769 paint.drawText(x + 5, textY, p.label);
Chris@79 770 }
Chris@55 771 }
Chris@0 772 }
Chris@6 773
Chris@6 774 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@55 775 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
Chris@6 776 paint.drawPath(path);
Chris@6 777 }
Chris@6 778
Chris@6 779 paint.restore();
Chris@6 780
Chris@6 781 // looks like save/restore doesn't deal with this:
Chris@6 782 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 783 }
Chris@6 784
Chris@42 785 int
Chris@44 786 TimeValueLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@42 787 {
Chris@68 788 int w = paint.fontMetrics().width("-000.000");
Chris@68 789 if (m_plotStyle == PlotSegmentation) return w + 20;
Chris@68 790 else return w + 10;
Chris@42 791 }
Chris@42 792
Chris@42 793 void
Chris@44 794 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@42 795 {
Chris@42 796 if (!m_model) return;
Chris@42 797
Chris@68 798 int h = v->height();
Chris@68 799
Chris@68 800 int n = 10;
Chris@68 801
Chris@68 802 float max = m_model->getValueMaximum();
Chris@68 803 float min = m_model->getValueMinimum();
Chris@68 804 float val = min;
Chris@68 805 float inc = (max - val) / n;
Chris@42 806
Chris@55 807 char buffer[40];
Chris@55 808
Chris@55 809 int w = getVerticalScaleWidth(v, paint);
Chris@55 810
Chris@68 811 int tx = 5;
Chris@68 812
Chris@68 813 int boxx = 5, boxy = 5;
Chris@68 814 if (m_model->getScaleUnits() != "") {
Chris@68 815 boxy += paint.fontMetrics().height();
Chris@68 816 }
Chris@68 817 int boxw = 10, boxh = h - boxy - 5;
Chris@68 818
Chris@68 819 if (m_plotStyle == PlotSegmentation) {
Chris@68 820 tx += boxx + boxw;
Chris@68 821 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@68 822 }
Chris@68 823
Chris@68 824 if (m_plotStyle == PlotSegmentation) {
Chris@68 825 paint.save();
Chris@68 826 for (int y = 0; y < boxh; ++y) {
Chris@68 827 float val = ((boxh - y) * (max - min)) / boxh + min;
Chris@101 828 paint.setPen(getColourForValue(v, val));
Chris@68 829 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@68 830 }
Chris@68 831 paint.restore();
Chris@68 832 }
Chris@68 833
Chris@68 834 for (int i = 0; i < n; ++i) {
Chris@68 835
Chris@68 836 int y, ty;
Chris@68 837 bool drawText = true;
Chris@68 838
Chris@68 839 if (m_plotStyle == PlotSegmentation) {
Chris@68 840 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@68 841 ty = y;
Chris@68 842 } else {
Chris@68 843 if (i == n-1) {
Chris@68 844 if (m_model->getScaleUnits() != "") drawText = false;
Chris@68 845 }
Chris@68 846 y = getYForValue(v, val);
Chris@68 847 ty = y - paint.fontMetrics().height() +
Chris@68 848 paint.fontMetrics().ascent();
Chris@68 849 }
Chris@68 850
Chris@68 851 sprintf(buffer, "%.3f", val);
Chris@55 852 QString label = QString(buffer);
Chris@68 853
Chris@68 854 if (m_plotStyle != PlotSegmentation) {
Chris@68 855 paint.drawLine(w - 5, y, w, y);
Chris@68 856 } else {
Chris@68 857 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@68 858 }
Chris@68 859
Chris@68 860 if (drawText) paint.drawText(tx, ty, label);
Chris@44 861 val += inc;
Chris@42 862 }
Chris@68 863
Chris@67 864 if (m_model->getScaleUnits() != "") {
Chris@67 865 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
Chris@67 866 m_model->getScaleUnits());
Chris@67 867 }
Chris@42 868 }
Chris@42 869
Chris@21 870 void
Chris@44 871 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 872 {
Chris@101 873 // std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 874
Chris@21 875 if (!m_model) return;
Chris@21 876
Chris@44 877 long frame = v->getFrameForX(e->x());
Chris@76 878 long resolution = m_model->getResolution();
Chris@21 879 if (frame < 0) frame = 0;
Chris@76 880 frame = (frame / resolution) * resolution;
Chris@21 881
Chris@44 882 float value = getValueForY(v, e->y());
Chris@21 883
Chris@76 884 bool havePoint = false;
Chris@76 885
Chris@76 886 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 887 if (!points.empty()) {
Chris@76 888 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 889 i != points.end(); ++i) {
Chris@76 890 if (((i->frame / resolution) * resolution) != frame) {
Chris@101 891 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 892 continue;
Chris@76 893 }
Chris@76 894 m_editingPoint = *i;
Chris@76 895 havePoint = true;
Chris@76 896 }
Chris@76 897 }
Chris@76 898
Chris@76 899 if (!havePoint) {
Chris@76 900 m_editingPoint = SparseTimeValueModel::Point
Chris@76 901 (frame, value, tr("New Point"));
Chris@76 902 }
Chris@76 903
Chris@23 904 m_originalPoint = m_editingPoint;
Chris@22 905
Chris@22 906 if (m_editingCommand) m_editingCommand->finish();
Chris@22 907 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 908 tr("Draw Point"));
Chris@76 909 if (!havePoint) {
Chris@76 910 m_editingCommand->addPoint(m_editingPoint);
Chris@76 911 }
Chris@22 912
Chris@21 913 m_editing = true;
Chris@21 914 }
Chris@21 915
Chris@21 916 void
Chris@44 917 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 918 {
Chris@101 919 // std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 920
Chris@21 921 if (!m_model || !m_editing) return;
Chris@21 922
Chris@44 923 long frame = v->getFrameForX(e->x());
Chris@76 924 long resolution = m_model->getResolution();
Chris@21 925 if (frame < 0) frame = 0;
Chris@76 926 frame = (frame / resolution) * resolution;
Chris@21 927
Chris@44 928 float value = getValueForY(v, e->y());
Chris@21 929
Chris@76 930 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 931
Chris@101 932 // std::cerr << points.size() << " points" << std::endl;
Chris@76 933
Chris@76 934 bool havePoint = false;
Chris@76 935
Chris@76 936 if (!points.empty()) {
Chris@76 937 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 938 i != points.end(); ++i) {
Chris@76 939 if (i->frame == m_editingPoint.frame &&
Chris@76 940 i->value == m_editingPoint.value) {
Chris@101 941 // std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
Chris@76 942 continue;
Chris@76 943 }
Chris@76 944 if (((i->frame / resolution) * resolution) != frame) {
Chris@101 945 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 946 continue;
Chris@76 947 }
Chris@101 948 // std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
Chris@76 949 m_editingPoint = *i;
Chris@76 950 m_originalPoint = m_editingPoint;
Chris@76 951 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 952 havePoint = true;
Chris@76 953 }
Chris@76 954 }
Chris@76 955
Chris@76 956 if (!havePoint) {
Chris@76 957 if (frame == m_editingPoint.frame) {
Chris@76 958 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 959 }
Chris@76 960 }
Chris@76 961
Chris@76 962 // m_editingCommand->deletePoint(m_editingPoint);
Chris@21 963 m_editingPoint.frame = frame;
Chris@21 964 m_editingPoint.value = value;
Chris@22 965 m_editingCommand->addPoint(m_editingPoint);
Chris@21 966 }
Chris@21 967
Chris@21 968 void
Chris@44 969 TimeValueLayer::drawEnd(View *v, QMouseEvent *e)
Chris@21 970 {
Chris@101 971 // std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 972 if (!m_model || !m_editing) return;
Chris@22 973 m_editingCommand->finish();
Chris@22 974 m_editingCommand = 0;
Chris@21 975 m_editing = false;
Chris@21 976 }
Chris@21 977
Chris@21 978 void
Chris@44 979 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 980 {
Chris@101 981 // std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 982
Chris@21 983 if (!m_model) return;
Chris@21 984
Chris@44 985 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 986 if (points.empty()) return;
Chris@21 987
Chris@21 988 m_editingPoint = *points.begin();
Chris@23 989 m_originalPoint = m_editingPoint;
Chris@22 990
Chris@22 991 if (m_editingCommand) {
Chris@22 992 m_editingCommand->finish();
Chris@22 993 m_editingCommand = 0;
Chris@22 994 }
Chris@22 995
Chris@21 996 m_editing = true;
Chris@21 997 }
Chris@21 998
Chris@21 999 void
Chris@44 1000 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 1001 {
Chris@101 1002 // std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 1003
Chris@21 1004 if (!m_model || !m_editing) return;
Chris@21 1005
Chris@44 1006 long frame = v->getFrameForX(e->x());
Chris@21 1007 if (frame < 0) frame = 0;
Chris@21 1008 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 1009
Chris@44 1010 float value = getValueForY(v, e->y());
Chris@21 1011
Chris@22 1012 if (!m_editingCommand) {
Chris@22 1013 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 1014 tr("Drag Point"));
Chris@22 1015 }
Chris@22 1016
Chris@22 1017 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 1018 m_editingPoint.frame = frame;
Chris@21 1019 m_editingPoint.value = value;
Chris@22 1020 m_editingCommand->addPoint(m_editingPoint);
Chris@21 1021 }
Chris@21 1022
Chris@21 1023 void
Chris@44 1024 TimeValueLayer::editEnd(View *v, QMouseEvent *e)
Chris@21 1025 {
Chris@101 1026 // std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 1027 if (!m_model || !m_editing) return;
Chris@23 1028
Chris@23 1029 if (m_editingCommand) {
Chris@23 1030
Chris@23 1031 QString newName = m_editingCommand->getName();
Chris@23 1032
Chris@23 1033 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 1034 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 1035 newName = tr("Edit Point");
Chris@23 1036 } else {
Chris@23 1037 newName = tr("Relocate Point");
Chris@23 1038 }
Chris@23 1039 } else {
Chris@23 1040 newName = tr("Change Point Value");
Chris@23 1041 }
Chris@23 1042
Chris@23 1043 m_editingCommand->setName(newName);
Chris@23 1044 m_editingCommand->finish();
Chris@23 1045 }
Chris@23 1046
Chris@22 1047 m_editingCommand = 0;
Chris@21 1048 m_editing = false;
Chris@21 1049 }
Chris@21 1050
Chris@43 1051 void
Chris@70 1052 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1053 {
Chris@70 1054 if (!m_model) return;
Chris@70 1055
Chris@70 1056 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@70 1057 if (points.empty()) return;
Chris@70 1058
Chris@70 1059 SparseTimeValueModel::Point point = *points.begin();
Chris@70 1060
Chris@70 1061 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1062 (m_model->getSampleRate(),
Chris@70 1063 ItemEditDialog::ShowTime |
Chris@70 1064 ItemEditDialog::ShowValue |
Chris@73 1065 ItemEditDialog::ShowText,
Chris@73 1066 m_model->getScaleUnits());
Chris@70 1067
Chris@70 1068 dialog->setFrameTime(point.frame);
Chris@70 1069 dialog->setValue(point.value);
Chris@70 1070 dialog->setText(point.label);
Chris@70 1071
Chris@70 1072 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1073
Chris@70 1074 SparseTimeValueModel::Point newPoint = point;
Chris@70 1075 newPoint.frame = dialog->getFrameTime();
Chris@70 1076 newPoint.value = dialog->getValue();
Chris@70 1077 newPoint.label = dialog->getText();
Chris@70 1078
Chris@70 1079 SparseTimeValueModel::EditCommand *command =
Chris@70 1080 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
Chris@70 1081 command->deletePoint(point);
Chris@70 1082 command->addPoint(newPoint);
Chris@70 1083 command->finish();
Chris@70 1084 }
Chris@70 1085
Chris@70 1086 delete dialog;
Chris@70 1087 }
Chris@70 1088
Chris@70 1089 void
Chris@43 1090 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1091 {
Chris@99 1092 if (!m_model) return;
Chris@99 1093
Chris@43 1094 SparseTimeValueModel::EditCommand *command =
Chris@43 1095 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1096 tr("Drag Selection"));
Chris@43 1097
Chris@43 1098 SparseTimeValueModel::PointList points =
Chris@43 1099 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1100
Chris@43 1101 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1102 i != points.end(); ++i) {
Chris@43 1103
Chris@43 1104 if (s.contains(i->frame)) {
Chris@43 1105 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1106 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1107 command->deletePoint(*i);
Chris@43 1108 command->addPoint(newPoint);
Chris@43 1109 }
Chris@43 1110 }
Chris@43 1111
Chris@43 1112 command->finish();
Chris@43 1113 }
Chris@43 1114
Chris@43 1115 void
Chris@43 1116 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1117 {
Chris@99 1118 if (!m_model) return;
Chris@99 1119
Chris@43 1120 SparseTimeValueModel::EditCommand *command =
Chris@43 1121 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1122 tr("Resize Selection"));
Chris@43 1123
Chris@43 1124 SparseTimeValueModel::PointList points =
Chris@43 1125 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1126
Chris@43 1127 double ratio =
Chris@43 1128 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1129 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1130
Chris@43 1131 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1132 i != points.end(); ++i) {
Chris@43 1133
Chris@43 1134 if (s.contains(i->frame)) {
Chris@43 1135
Chris@43 1136 double target = i->frame;
Chris@43 1137 target = newSize.getStartFrame() +
Chris@43 1138 double(target - s.getStartFrame()) * ratio;
Chris@43 1139
Chris@43 1140 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1141 newPoint.frame = lrint(target);
Chris@43 1142 command->deletePoint(*i);
Chris@43 1143 command->addPoint(newPoint);
Chris@43 1144 }
Chris@43 1145 }
Chris@43 1146
Chris@43 1147 command->finish();
Chris@43 1148 }
Chris@43 1149
Chris@76 1150 void
Chris@76 1151 TimeValueLayer::deleteSelection(Selection s)
Chris@76 1152 {
Chris@99 1153 if (!m_model) return;
Chris@99 1154
Chris@76 1155 SparseTimeValueModel::EditCommand *command =
Chris@76 1156 new SparseTimeValueModel::EditCommand(m_model,
Chris@76 1157 tr("Delete Selected Points"));
Chris@76 1158
Chris@76 1159 SparseTimeValueModel::PointList points =
Chris@76 1160 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1161
Chris@76 1162 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1163 i != points.end(); ++i) {
Chris@76 1164
Chris@76 1165 if (s.contains(i->frame)) {
Chris@76 1166 command->deletePoint(*i);
Chris@76 1167 }
Chris@76 1168 }
Chris@76 1169
Chris@76 1170 command->finish();
Chris@76 1171 }
Chris@76 1172
Chris@76 1173 void
Chris@76 1174 TimeValueLayer::copy(Selection s, Clipboard &to)
Chris@76 1175 {
Chris@99 1176 if (!m_model) return;
Chris@99 1177
Chris@76 1178 SparseTimeValueModel::PointList points =
Chris@76 1179 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1180
Chris@76 1181 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1182 i != points.end(); ++i) {
Chris@76 1183 if (s.contains(i->frame)) {
Chris@83 1184 Clipboard::Point point(i->frame, i->value, i->label);
Chris@76 1185 to.addPoint(point);
Chris@76 1186 }
Chris@76 1187 }
Chris@76 1188 }
Chris@76 1189
Chris@125 1190 bool
Chris@125 1191 TimeValueLayer::paste(const Clipboard &from, int frameOffset,
Chris@125 1192 bool interactive)
Chris@76 1193 {
Chris@125 1194 if (!m_model) return false;
Chris@99 1195
Chris@76 1196 const Clipboard::PointList &points = from.getPoints();
Chris@76 1197
Chris@76 1198 SparseTimeValueModel::EditCommand *command =
Chris@76 1199 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
Chris@76 1200
Chris@125 1201 enum ValueAvailability {
Chris@125 1202 UnknownAvailability,
Chris@125 1203 NoValues,
Chris@125 1204 SomeValues,
Chris@125 1205 AllValues
Chris@125 1206 };
Chris@125 1207 enum ValueGeneration {
Chris@125 1208 GenerateNone,
Chris@125 1209 GenerateFromCounter,
Chris@125 1210 GenerateFromFrameNumber,
Chris@125 1211 GenerateFromRealTime,
Chris@125 1212 GenerateFromRealTimeDifference,
Chris@125 1213 GenerateFromTempo,
Chris@125 1214 GenerateFromExistingNeighbour,
Chris@125 1215 GenerateFromLabels
Chris@125 1216 };
Chris@125 1217
Chris@125 1218 ValueGeneration generation = GenerateNone;
Chris@125 1219
Chris@125 1220 bool haveUsableLabels = false;
Chris@125 1221 bool haveExistingItems = !(m_model->isEmpty());
Chris@125 1222
Chris@125 1223 if (interactive) {
Chris@125 1224
Chris@125 1225 ValueAvailability availability = UnknownAvailability;
Chris@125 1226
Chris@125 1227 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@125 1228 i != points.end(); ++i) {
Chris@125 1229
Chris@125 1230 if (!i->haveFrame()) continue;
Chris@125 1231
Chris@125 1232 if (availability == UnknownAvailability) {
Chris@125 1233 if (i->haveValue()) availability = AllValues;
Chris@125 1234 else availability = NoValues;
Chris@125 1235 continue;
Chris@125 1236 }
Chris@125 1237
Chris@125 1238 if (i->haveValue()) {
Chris@125 1239 if (availability == NoValues) {
Chris@125 1240 availability = SomeValues;
Chris@125 1241 }
Chris@125 1242 } else {
Chris@125 1243 if (availability == AllValues) {
Chris@125 1244 availability = SomeValues;
Chris@125 1245 }
Chris@125 1246 }
Chris@125 1247
Chris@125 1248 if (!haveUsableLabels) {
Chris@125 1249 if (i->haveLabel()) {
Chris@125 1250 if (i->getLabel().contains(QRegExp("[0-9]"))) {
Chris@125 1251 haveUsableLabels = true;
Chris@125 1252 }
Chris@125 1253 }
Chris@125 1254 }
Chris@125 1255
Chris@125 1256 if (availability == SomeValues && haveUsableLabels) break;
Chris@125 1257 }
Chris@125 1258
Chris@125 1259 if (availability == NoValues || availability == SomeValues) {
Chris@125 1260
Chris@125 1261 QString text;
Chris@125 1262 if (availability == NoValues) {
Chris@125 1263 text = tr("The items you are pasting do not have values.\nWhat values do you want to use for these items?");
Chris@125 1264 } else {
Chris@125 1265 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 1266 }
Chris@125 1267
Chris@125 1268 QStringList options;
Chris@125 1269 std::vector<int> genopts;
Chris@125 1270
Chris@125 1271 options << tr("Zero for all items");
Chris@125 1272 genopts.push_back(int(GenerateNone));
Chris@125 1273
Chris@125 1274 options << tr("Whole numbers counting from 1");
Chris@125 1275 genopts.push_back(int(GenerateFromCounter));
Chris@125 1276
Chris@125 1277 options << tr("Item's audio sample frame number");
Chris@125 1278 genopts.push_back(int(GenerateFromFrameNumber));
Chris@125 1279
Chris@125 1280 options << tr("Item's time in seconds");
Chris@125 1281 genopts.push_back(int(GenerateFromRealTime));
Chris@125 1282
Chris@125 1283 options << tr("Duration from the item to the following item");
Chris@125 1284 genopts.push_back(int(GenerateFromRealTimeDifference));
Chris@125 1285
Chris@125 1286 options << tr("Tempo in bpm derived from the duration");
Chris@125 1287 genopts.push_back(int(GenerateFromTempo));
Chris@125 1288
Chris@125 1289 if (haveExistingItems) {
Chris@125 1290 options << tr("Value of the nearest existing item");
Chris@125 1291 genopts.push_back(int(GenerateFromExistingNeighbour));
Chris@125 1292 }
Chris@125 1293
Chris@125 1294 if (haveUsableLabels) {
Chris@125 1295 options << tr("Value extracted from the item's label (where possible)");
Chris@125 1296 genopts.push_back(int(GenerateFromLabels));
Chris@125 1297 }
Chris@125 1298
Chris@125 1299
Chris@125 1300 static int prevSelection = 0;
Chris@125 1301
Chris@125 1302 bool ok = false;
Chris@125 1303 QString selected = ListInputDialog::getItem
Chris@125 1304 (0, tr("Choose value calculation"),
Chris@125 1305 text, options, prevSelection, &ok);
Chris@125 1306
Chris@125 1307 if (!ok) return false;
Chris@125 1308 int selection = 0;
Chris@125 1309 generation = GenerateNone;
Chris@125 1310
Chris@125 1311 for (QStringList::const_iterator i = options.begin();
Chris@125 1312 i != options.end(); ++i) {
Chris@125 1313 if (selected == *i) {
Chris@125 1314 generation = ValueGeneration(genopts[selection]);
Chris@125 1315 break;
Chris@125 1316 }
Chris@125 1317 ++selection;
Chris@125 1318 }
Chris@125 1319
Chris@125 1320 prevSelection = selection;
Chris@125 1321 }
Chris@125 1322 }
Chris@125 1323
Chris@125 1324 int counter = 1;
Chris@125 1325 float prevBpm = 120.f;
Chris@125 1326
Chris@76 1327 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1328 i != points.end(); ++i) {
Chris@76 1329
Chris@76 1330 if (!i->haveFrame()) continue;
Chris@76 1331 size_t frame = 0;
Chris@76 1332 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
Chris@76 1333 frame = i->getFrame() + frameOffset;
Chris@76 1334 }
Chris@76 1335 SparseTimeValueModel::Point newPoint(frame);
Chris@76 1336
Chris@125 1337 if (i->haveLabel()) {
Chris@125 1338 newPoint.label = i->getLabel();
Chris@125 1339 } else if (i->haveValue()) {
Chris@125 1340 newPoint.label = QString("%1").arg(i->getValue());
Chris@125 1341 }
Chris@125 1342
Chris@125 1343 if (i->haveValue()) {
Chris@125 1344 newPoint.value = i->getValue();
Chris@125 1345 } else {
Chris@125 1346
Chris@125 1347 switch (generation) {
Chris@125 1348
Chris@125 1349 case GenerateNone:
Chris@125 1350 newPoint.value = 0;
Chris@125 1351 break;
Chris@125 1352
Chris@125 1353 case GenerateFromCounter:
Chris@125 1354 newPoint.value = counter;
Chris@125 1355 break;
Chris@125 1356
Chris@125 1357 case GenerateFromFrameNumber:
Chris@125 1358 newPoint.value = frame;
Chris@125 1359 break;
Chris@125 1360
Chris@125 1361 case GenerateFromRealTime:
Chris@125 1362 newPoint.value = float(frame) / float(m_model->getSampleRate());
Chris@125 1363 break;
Chris@125 1364
Chris@125 1365 case GenerateFromRealTimeDifference:
Chris@125 1366 case GenerateFromTempo:
Chris@125 1367 {
Chris@125 1368 size_t nextFrame = frame;
Chris@125 1369 Clipboard::PointList::const_iterator j = i;
Chris@125 1370 for (; j != points.end(); ++j) {
Chris@125 1371 if (!j->haveFrame()) continue;
Chris@125 1372 if (j != i) break;
Chris@125 1373 }
Chris@125 1374 if (j != points.end()) {
Chris@125 1375 nextFrame = j->getFrame();
Chris@125 1376 }
Chris@125 1377 if (generation == GenerateFromRealTimeDifference) {
Chris@125 1378 newPoint.value = float(nextFrame - frame) /
Chris@125 1379 float(m_model->getSampleRate());
Chris@125 1380 } else {
Chris@125 1381 float bpm = prevBpm;
Chris@125 1382 if (nextFrame > frame) {
Chris@125 1383 bpm = (60.f * m_model->getSampleRate()) /
Chris@125 1384 (nextFrame - frame);
Chris@125 1385 }
Chris@125 1386 newPoint.value = bpm;
Chris@125 1387 prevBpm = bpm;
Chris@125 1388 }
Chris@125 1389 break;
Chris@125 1390 }
Chris@125 1391
Chris@125 1392 case GenerateFromExistingNeighbour:
Chris@125 1393 {
Chris@125 1394 SparseTimeValueModel::PointList points =
Chris@125 1395 m_model->getPoints(frame);
Chris@125 1396 if (points.empty()) points = m_model->getPreviousPoints(frame);
Chris@125 1397 if (points.empty()) points = m_model->getNextPoints(frame);
Chris@125 1398 if (points.empty()) {
Chris@125 1399 newPoint.value = 0.f;
Chris@125 1400 } else {
Chris@125 1401 newPoint.value = points.begin()->value;
Chris@125 1402 }
Chris@125 1403 }
Chris@125 1404
Chris@125 1405 case GenerateFromLabels:
Chris@125 1406 if (i->haveLabel()) {
Chris@125 1407 // more forgiving than QString::toFloat()
Chris@125 1408 newPoint.value = atof(i->getLabel().toLocal8Bit());
Chris@125 1409 } else {
Chris@125 1410 newPoint.value = 0.f;
Chris@125 1411 }
Chris@125 1412 }
Chris@125 1413 }
Chris@76 1414
Chris@76 1415 command->addPoint(newPoint);
Chris@125 1416
Chris@125 1417 ++counter;
Chris@76 1418 }
Chris@76 1419
Chris@76 1420 command->finish();
Chris@125 1421 return true;
Chris@76 1422 }
Chris@76 1423
Chris@6 1424 QString
Chris@6 1425 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 1426 {
Chris@6 1427 return Layer::toXmlString(indent, extraAttributes +
Chris@6 1428 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@6 1429 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@0 1430 }
Chris@0 1431
Chris@11 1432 void
Chris@11 1433 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1434 {
Chris@11 1435 QString colourSpec = attributes.value("colour");
Chris@11 1436 if (colourSpec != "") {
Chris@11 1437 QColor colour(colourSpec);
Chris@11 1438 if (colour.isValid()) {
Chris@11 1439 setBaseColour(QColor(colourSpec));
Chris@11 1440 }
Chris@11 1441 }
Chris@11 1442
Chris@11 1443 bool ok;
Chris@11 1444 PlotStyle style = (PlotStyle)
Chris@11 1445 attributes.value("plotStyle").toInt(&ok);
Chris@11 1446 if (ok) setPlotStyle(style);
Chris@11 1447 }
Chris@11 1448