annotate layer/TimeValueLayer.cpp @ 316:c0b9eec70639

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