annotate layer/TimeValueLayer.cpp @ 100:0db5e7492ce8

* Add units repository, and unit property on time-value and note layers.
author Chris Cannam
date Thu, 11 May 2006 15:02:14 +0000
parents 453f7da3534e
children 0f36cdf407a6
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@0 18 #include "base/Model.h"
Chris@0 19 #include "base/RealTime.h"
Chris@0 20 #include "base/Profiler.h"
Chris@0 21 #include "base/View.h"
Chris@0 22
Chris@0 23 #include "model/SparseTimeValueModel.h"
Chris@0 24
Chris@70 25 #include "widgets/ItemEditDialog.h"
Chris@70 26
Chris@66 27 #include "SpectrogramLayer.h" // for optional frequency alignment
Chris@66 28
Chris@0 29 #include <QPainter>
Chris@6 30 #include <QPainterPath>
Chris@21 31 #include <QMouseEvent>
Chris@0 32
Chris@0 33 #include <iostream>
Chris@0 34 #include <cmath>
Chris@0 35
Chris@44 36 TimeValueLayer::TimeValueLayer() :
Chris@44 37 Layer(),
Chris@0 38 m_model(0),
Chris@21 39 m_editing(false),
Chris@23 40 m_originalPoint(0, 0.0, tr("New Point")),
Chris@21 41 m_editingPoint(0, 0.0, tr("New Point")),
Chris@22 42 m_editingCommand(0),
Chris@67 43 m_colour(Qt::darkGreen),
Chris@66 44 m_plotStyle(PlotConnectedPoints),
Chris@66 45 m_verticalScale(LinearScale)
Chris@0 46 {
Chris@44 47
Chris@0 48 }
Chris@0 49
Chris@0 50 void
Chris@0 51 TimeValueLayer::setModel(SparseTimeValueModel *model)
Chris@0 52 {
Chris@0 53 if (m_model == model) return;
Chris@0 54 m_model = model;
Chris@0 55
Chris@0 56 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 57 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 58 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 59
Chris@0 60 connect(m_model, SIGNAL(completionChanged()),
Chris@0 61 this, SIGNAL(modelCompletionChanged()));
Chris@0 62
Chris@0 63 std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
Chris@0 64
Chris@0 65 emit modelReplaced();
Chris@0 66 }
Chris@0 67
Chris@0 68 Layer::PropertyList
Chris@0 69 TimeValueLayer::getProperties() const
Chris@0 70 {
Chris@0 71 PropertyList list;
Chris@87 72 list.push_back("Colour");
Chris@87 73 list.push_back("Plot Type");
Chris@87 74 list.push_back("Vertical Scale");
Chris@100 75 list.push_back("Scale Units");
Chris@0 76 return list;
Chris@0 77 }
Chris@0 78
Chris@87 79 QString
Chris@87 80 TimeValueLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 81 {
Chris@87 82 if (name == "Colour") return tr("Colour");
Chris@87 83 if (name == "Plot Type") return tr("Plot Type");
Chris@87 84 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@100 85 if (name == "Scale Units") return tr("Scale Units");
Chris@87 86 return "";
Chris@87 87 }
Chris@87 88
Chris@0 89 Layer::PropertyType
Chris@0 90 TimeValueLayer::getPropertyType(const PropertyName &name) const
Chris@0 91 {
Chris@100 92 if (name == "Scale Units") return UnitsProperty;
Chris@100 93 else return ValueProperty;
Chris@0 94 }
Chris@0 95
Chris@0 96 int
Chris@0 97 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@0 98 int *min, int *max) const
Chris@0 99 {
Chris@0 100 //!!! factor this colour handling stuff out into a colour manager class
Chris@0 101
Chris@0 102 int deft = 0;
Chris@0 103
Chris@87 104 if (name == "Colour") {
Chris@0 105
Chris@10 106 if (min) *min = 0;
Chris@10 107 if (max) *max = 5;
Chris@0 108
Chris@0 109 if (m_colour == Qt::black) deft = 0;
Chris@0 110 else if (m_colour == Qt::darkRed) deft = 1;
Chris@0 111 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@0 112 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@0 113 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@0 114 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@0 115
Chris@87 116 } else if (name == "Plot Type") {
Chris@0 117
Chris@10 118 if (min) *min = 0;
Chris@26 119 if (max) *max = 5;
Chris@0 120
Chris@0 121 deft = int(m_plotStyle);
Chris@0 122
Chris@87 123 } else if (name == "Vertical Scale") {
Chris@66 124
Chris@66 125 if (min) *min = 0;
Chris@66 126 if (max) *max = 3;
Chris@66 127
Chris@66 128 deft = int(m_verticalScale);
Chris@66 129
Chris@100 130 } else if (name == "Scale Units") {
Chris@100 131
Chris@100 132 if (m_model) {
Chris@100 133 deft = UnitDatabase::getInstance()->getUnitId
Chris@100 134 (m_model->getScaleUnits());
Chris@100 135 }
Chris@100 136
Chris@0 137 } else {
Chris@0 138
Chris@0 139 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 140 }
Chris@0 141
Chris@0 142 return deft;
Chris@0 143 }
Chris@0 144
Chris@0 145 QString
Chris@0 146 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 147 int value) const
Chris@0 148 {
Chris@87 149 if (name == "Colour") {
Chris@0 150 switch (value) {
Chris@0 151 default:
Chris@0 152 case 0: return tr("Black");
Chris@0 153 case 1: return tr("Red");
Chris@0 154 case 2: return tr("Blue");
Chris@0 155 case 3: return tr("Green");
Chris@0 156 case 4: return tr("Purple");
Chris@0 157 case 5: return tr("Orange");
Chris@0 158 }
Chris@87 159 } else if (name == "Plot Type") {
Chris@0 160 switch (value) {
Chris@0 161 default:
Chris@0 162 case 0: return tr("Points");
Chris@0 163 case 1: return tr("Stems");
Chris@6 164 case 2: return tr("Connected Points");
Chris@6 165 case 3: return tr("Lines");
Chris@6 166 case 4: return tr("Curve");
Chris@26 167 case 5: return tr("Segmentation");
Chris@0 168 }
Chris@87 169 } else if (name == "Vertical Scale") {
Chris@66 170 switch (value) {
Chris@66 171 default:
Chris@66 172 case 0: return tr("Linear Scale");
Chris@66 173 case 1: return tr("Log Scale");
Chris@66 174 case 2: return tr("+/-1 Scale");
Chris@66 175 case 3: return tr("Frequency Scale");
Chris@66 176 }
Chris@0 177 }
Chris@0 178 return tr("<unknown>");
Chris@0 179 }
Chris@0 180
Chris@0 181 void
Chris@0 182 TimeValueLayer::setProperty(const PropertyName &name, int value)
Chris@0 183 {
Chris@87 184 if (name == "Colour") {
Chris@0 185 switch (value) {
Chris@0 186 default:
Chris@0 187 case 0: setBaseColour(Qt::black); break;
Chris@0 188 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 189 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 190 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 191 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 192 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 193 }
Chris@87 194 } else if (name == "Plot Type") {
Chris@0 195 setPlotStyle(PlotStyle(value));
Chris@87 196 } else if (name == "Vertical Scale") {
Chris@66 197 setVerticalScale(VerticalScale(value));
Chris@100 198 } else if (name == "Scale Units") {
Chris@100 199 if (m_model) {
Chris@100 200 m_model->setScaleUnits
Chris@100 201 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 202 emit modelChanged();
Chris@100 203 }
Chris@0 204 }
Chris@0 205 }
Chris@0 206
Chris@0 207 void
Chris@0 208 TimeValueLayer::setBaseColour(QColor colour)
Chris@0 209 {
Chris@0 210 if (m_colour == colour) return;
Chris@0 211 m_colour = colour;
Chris@0 212 emit layerParametersChanged();
Chris@0 213 }
Chris@0 214
Chris@0 215 void
Chris@0 216 TimeValueLayer::setPlotStyle(PlotStyle style)
Chris@0 217 {
Chris@0 218 if (m_plotStyle == style) return;
Chris@0 219 m_plotStyle = style;
Chris@0 220 emit layerParametersChanged();
Chris@0 221 }
Chris@0 222
Chris@66 223 void
Chris@66 224 TimeValueLayer::setVerticalScale(VerticalScale scale)
Chris@66 225 {
Chris@66 226 if (m_verticalScale == scale) return;
Chris@66 227 m_verticalScale = scale;
Chris@66 228 emit layerParametersChanged();
Chris@66 229 }
Chris@66 230
Chris@0 231 bool
Chris@44 232 TimeValueLayer::isLayerScrollable(const View *v) const
Chris@0 233 {
Chris@6 234 // We don't illuminate sections in the line or curve modes, so
Chris@6 235 // they're always scrollable
Chris@6 236
Chris@6 237 if (m_plotStyle == PlotLines ||
Chris@6 238 m_plotStyle == PlotCurve) return true;
Chris@6 239
Chris@0 240 QPoint discard;
Chris@44 241 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@0 242 }
Chris@0 243
Chris@79 244 bool
Chris@79 245 TimeValueLayer::getValueExtents(float &min, float &max, QString &unit) const
Chris@79 246 {
Chris@79 247 min = m_model->getValueMinimum();
Chris@79 248 max = m_model->getValueMaximum();
Chris@79 249 unit = m_model->getScaleUnits();
Chris@79 250 return true;
Chris@79 251 }
Chris@79 252
Chris@0 253 SparseTimeValueModel::PointList
Chris@44 254 TimeValueLayer::getLocalPoints(View *v, int x) const
Chris@0 255 {
Chris@0 256 if (!m_model) return SparseTimeValueModel::PointList();
Chris@0 257
Chris@44 258 long frame = v->getFrameForX(x);
Chris@0 259
Chris@0 260 SparseTimeValueModel::PointList onPoints =
Chris@0 261 m_model->getPoints(frame);
Chris@0 262
Chris@0 263 if (!onPoints.empty()) {
Chris@0 264 return onPoints;
Chris@0 265 }
Chris@0 266
Chris@0 267 SparseTimeValueModel::PointList prevPoints =
Chris@0 268 m_model->getPreviousPoints(frame);
Chris@0 269 SparseTimeValueModel::PointList nextPoints =
Chris@0 270 m_model->getNextPoints(frame);
Chris@0 271
Chris@0 272 SparseTimeValueModel::PointList usePoints = prevPoints;
Chris@0 273
Chris@0 274 if (prevPoints.empty()) {
Chris@0 275 usePoints = nextPoints;
Chris@44 276 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
Chris@44 277 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@0 278 usePoints = nextPoints;
Chris@0 279 } else if (nextPoints.begin()->frame - frame <
Chris@0 280 frame - prevPoints.begin()->frame) {
Chris@0 281 usePoints = nextPoints;
Chris@0 282 }
Chris@0 283
Chris@28 284 if (!usePoints.empty()) {
Chris@28 285 int fuzz = 2;
Chris@44 286 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@28 287 if ((px > x && px - x > fuzz) ||
Chris@28 288 (px < x && x - px > fuzz + 1)) {
Chris@28 289 usePoints.clear();
Chris@28 290 }
Chris@28 291 }
Chris@28 292
Chris@0 293 return usePoints;
Chris@0 294 }
Chris@0 295
Chris@25 296 QString
Chris@44 297 TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@0 298 {
Chris@25 299 int x = pos.x();
Chris@0 300
Chris@25 301 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 302
Chris@44 303 SparseTimeValueModel::PointList points = getLocalPoints(v, x);
Chris@0 304
Chris@0 305 if (points.empty()) {
Chris@0 306 if (!m_model->isReady()) {
Chris@25 307 return tr("In progress");
Chris@25 308 } else {
Chris@25 309 return tr("No local points");
Chris@0 310 }
Chris@0 311 }
Chris@0 312
Chris@0 313 long useFrame = points.begin()->frame;
Chris@0 314
Chris@0 315 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 316
Chris@25 317 QString text;
Chris@0 318
Chris@25 319 if (points.begin()->label == "") {
Chris@25 320 text = QString(tr("Time:\t%1\nValue:\t%2\nNo label"))
Chris@25 321 .arg(rt.toText(true).c_str())
Chris@25 322 .arg(points.begin()->value);
Chris@25 323 } else {
Chris@25 324 text = QString(tr("Time:\t%1\nValue:\t%2\nLabel:\t%3"))
Chris@25 325 .arg(rt.toText(true).c_str())
Chris@25 326 .arg(points.begin()->value)
Chris@25 327 .arg(points.begin()->label);
Chris@25 328 }
Chris@0 329
Chris@44 330 pos = QPoint(v->getXForFrame(useFrame),
Chris@44 331 getYForValue(v, points.begin()->value));
Chris@25 332 return text;
Chris@0 333 }
Chris@0 334
Chris@28 335 bool
Chris@44 336 TimeValueLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 337 size_t &resolution,
Chris@28 338 SnapType snap) const
Chris@13 339 {
Chris@13 340 if (!m_model) {
Chris@44 341 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@13 342 }
Chris@13 343
Chris@13 344 resolution = m_model->getResolution();
Chris@28 345 SparseTimeValueModel::PointList points;
Chris@13 346
Chris@28 347 if (snap == SnapNeighbouring) {
Chris@28 348
Chris@44 349 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@28 350 if (points.empty()) return false;
Chris@28 351 frame = points.begin()->frame;
Chris@28 352 return true;
Chris@28 353 }
Chris@28 354
Chris@28 355 points = m_model->getPoints(frame, frame);
Chris@28 356 int snapped = frame;
Chris@28 357 bool found = false;
Chris@13 358
Chris@13 359 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@13 360 i != points.end(); ++i) {
Chris@13 361
Chris@28 362 if (snap == SnapRight) {
Chris@28 363
Chris@13 364 if (i->frame > frame) {
Chris@28 365 snapped = i->frame;
Chris@28 366 found = true;
Chris@13 367 break;
Chris@13 368 }
Chris@28 369
Chris@28 370 } else if (snap == SnapLeft) {
Chris@28 371
Chris@13 372 if (i->frame <= frame) {
Chris@28 373 snapped = i->frame;
Chris@28 374 found = true; // don't break, as the next may be better
Chris@28 375 } else {
Chris@28 376 break;
Chris@28 377 }
Chris@28 378
Chris@28 379 } else { // nearest
Chris@28 380
Chris@28 381 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@28 382 ++j;
Chris@28 383
Chris@28 384 if (j == points.end()) {
Chris@28 385
Chris@28 386 snapped = i->frame;
Chris@28 387 found = true;
Chris@28 388 break;
Chris@28 389
Chris@28 390 } else if (j->frame >= frame) {
Chris@28 391
Chris@28 392 if (j->frame - frame < frame - i->frame) {
Chris@28 393 snapped = j->frame;
Chris@28 394 } else {
Chris@28 395 snapped = i->frame;
Chris@28 396 }
Chris@28 397 found = true;
Chris@28 398 break;
Chris@13 399 }
Chris@13 400 }
Chris@13 401 }
Chris@13 402
Chris@28 403 frame = snapped;
Chris@28 404 return found;
Chris@13 405 }
Chris@13 406
Chris@21 407 int
Chris@66 408 TimeValueLayer::getYForValue(View *v, float val) const
Chris@21 409 {
Chris@79 410 float min = 0.0, max = 0.0;
Chris@79 411 int h = v->height();
Chris@79 412
Chris@79 413 if (!v->getValueExtents(m_model->getScaleUnits(), min, max)) {
Chris@79 414 min = m_model->getValueMinimum();
Chris@79 415 max = m_model->getValueMaximum();
Chris@79 416 }
Chris@79 417
Chris@79 418 if (max == min) max = min + 1.0;
Chris@79 419
Chris@79 420 /*!!!
Chris@21 421 float min = m_model->getValueMinimum();
Chris@21 422 float max = m_model->getValueMaximum();
Chris@21 423 if (max == min) max = min + 1.0;
Chris@21 424
Chris@44 425 int h = v->height();
Chris@21 426
Chris@66 427 if (m_verticalScale == FrequencyScale || m_verticalScale == LogScale) {
Chris@66 428
Chris@66 429 if (m_verticalScale == FrequencyScale) {
Chris@66 430 // If we have a spectrogram layer on the same view as us, align
Chris@66 431 // ourselves with it...
Chris@66 432 for (int i = 0; i < v->getLayerCount(); ++i) {
Chris@66 433 SpectrogramLayer *spectrogram = dynamic_cast<SpectrogramLayer *>
Chris@66 434 (v->getLayer(i));
Chris@66 435 if (spectrogram) {
Chris@66 436 return spectrogram->getYForFrequency(v, val);
Chris@66 437 }
Chris@66 438 }
Chris@66 439 }
Chris@66 440
Chris@66 441 min = (min < 0.0) ? -log10(-min) : (min == 0.0) ? 0.0 : log10(min);
Chris@66 442 max = (max < 0.0) ? -log10(-max) : (max == 0.0) ? 0.0 : log10(max);
Chris@66 443 val = (val < 0.0) ? -log10(-val) : (val == 0.0) ? 0.0 : log10(val);
Chris@66 444
Chris@66 445 } else if (m_verticalScale == PlusMinusOneScale) {
Chris@66 446 min = -1.0;
Chris@66 447 max = 1.0;
Chris@66 448 }
Chris@79 449 */
Chris@66 450
Chris@66 451 return int(h - ((val - min) * h) / (max - min));
Chris@21 452 }
Chris@21 453
Chris@21 454 float
Chris@44 455 TimeValueLayer::getValueForY(View *v, int y) const
Chris@21 456 {
Chris@66 457 //!!!
Chris@66 458
Chris@21 459 float min = m_model->getValueMinimum();
Chris@21 460 float max = m_model->getValueMaximum();
Chris@21 461 if (max == min) max = min + 1.0;
Chris@21 462
Chris@44 463 int h = v->height();
Chris@21 464
Chris@21 465 return min + (float(h - y) * float(max - min)) / h;
Chris@21 466 }
Chris@21 467
Chris@68 468 QColor
Chris@68 469 TimeValueLayer::getColourForValue(float val) const
Chris@68 470 {
Chris@68 471 float min = m_model->getValueMinimum();
Chris@68 472 float max = m_model->getValueMaximum();
Chris@68 473 if (max == min) max = min + 1.0;
Chris@68 474
Chris@68 475 if (m_verticalScale == FrequencyScale || m_verticalScale == LogScale) {
Chris@68 476 min = (min < 0.0) ? -log10(-min) : (min == 0.0) ? 0.0 : log10(min);
Chris@68 477 max = (max < 0.0) ? -log10(-max) : (max == 0.0) ? 0.0 : log10(max);
Chris@68 478 val = (val < 0.0) ? -log10(-val) : (val == 0.0) ? 0.0 : log10(val);
Chris@68 479 } else if (m_verticalScale == PlusMinusOneScale) {
Chris@68 480 min = -1.0;
Chris@68 481 max = 1.0;
Chris@68 482 }
Chris@68 483
Chris@68 484 int iv = ((val - min) / (max - min)) * 255.999;
Chris@68 485
Chris@68 486 QColor colour = QColor::fromHsv(256 - iv, iv / 2 + 128, iv);
Chris@68 487 return QColor(colour.red(), colour.green(), colour.blue(), 120);
Chris@68 488 }
Chris@68 489
Chris@0 490 void
Chris@44 491 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 492 {
Chris@0 493 if (!m_model || !m_model->isOK()) return;
Chris@0 494
Chris@0 495 int sampleRate = m_model->getSampleRate();
Chris@0 496 if (!sampleRate) return;
Chris@0 497
Chris@0 498 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 499
Chris@0 500 int x0 = rect.left(), x1 = rect.right();
Chris@44 501 long frame0 = v->getFrameForX(x0);
Chris@44 502 long frame1 = v->getFrameForX(x1);
Chris@0 503
Chris@0 504 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 505 (frame0, frame1));
Chris@11 506 if (points.empty()) return;
Chris@0 507
Chris@0 508 paint.setPen(m_colour);
Chris@0 509
Chris@0 510 QColor brushColour(m_colour);
Chris@0 511 brushColour.setAlpha(80);
Chris@0 512 paint.setBrush(brushColour);
Chris@0 513
Chris@0 514 // std::cerr << "TimeValueLayer::paint: resolution is "
Chris@0 515 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 516
Chris@0 517 float min = m_model->getValueMinimum();
Chris@0 518 float max = m_model->getValueMaximum();
Chris@0 519 if (max == min) max = min + 1.0;
Chris@0 520
Chris@44 521 int origin = int(nearbyint(v->height() -
Chris@44 522 (-min * v->height()) / (max - min)));
Chris@0 523
Chris@0 524 QPoint localPos;
Chris@0 525 long illuminateFrame = -1;
Chris@0 526
Chris@44 527 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 528 SparseTimeValueModel::PointList localPoints =
Chris@44 529 getLocalPoints(v, localPos.x());
Chris@0 530 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 531 }
Chris@6 532
Chris@20 533 int w =
Chris@44 534 v->getXForFrame(frame0 + m_model->getResolution()) -
Chris@44 535 v->getXForFrame(frame0);
Chris@7 536
Chris@6 537 paint.save();
Chris@6 538
Chris@6 539 QPainterPath path;
Chris@55 540 int pointCount = 0;
Chris@79 541
Chris@79 542 int textY = 0;
Chris@79 543 if (m_plotStyle == PlotSegmentation) {
Chris@79 544 textY = v->getTextLabelHeight(this, paint);
Chris@79 545 }
Chris@6 546
Chris@0 547 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 548 i != points.end(); ++i) {
Chris@0 549
Chris@0 550 const SparseTimeValueModel::Point &p(*i);
Chris@0 551
Chris@44 552 int x = v->getXForFrame(p.frame);
Chris@44 553 int y = getYForValue(v, p.value);
Chris@0 554
Chris@79 555 if (m_plotStyle != PlotSegmentation) {
Chris@79 556 textY = y - paint.fontMetrics().height()
Chris@79 557 + paint.fontMetrics().ascent();
Chris@79 558 }
Chris@79 559
Chris@34 560 bool haveNext = false;
Chris@76 561 int nx = v->getXForFrame(v->getModelsEndFrame());
Chris@76 562 // m_model->getEndFrame());
Chris@34 563 int ny = y;
Chris@34 564
Chris@34 565 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 566 ++j;
Chris@34 567
Chris@34 568 if (j != points.end()) {
Chris@34 569 const SparseTimeValueModel::Point &q(*j);
Chris@44 570 nx = v->getXForFrame(q.frame);
Chris@44 571 ny = getYForValue(v, q.value);
Chris@34 572 haveNext = true;
Chris@76 573 }
Chris@76 574
Chris@76 575 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
Chris@76 576 // << ", nx = " << nx << std::endl;
Chris@34 577
Chris@34 578 int labelY = y;
Chris@34 579
Chris@0 580 if (w < 1) w = 1;
Chris@26 581 paint.setPen(m_colour);
Chris@0 582
Chris@26 583 if (m_plotStyle == PlotSegmentation) {
Chris@68 584 paint.setBrush(getColourForValue(p.value));
Chris@44 585 labelY = v->height();
Chris@26 586 } else if (m_plotStyle == PlotLines ||
Chris@26 587 m_plotStyle == PlotCurve) {
Chris@6 588 paint.setBrush(Qt::NoBrush);
Chris@3 589 } else {
Chris@6 590 paint.setBrush(brushColour);
Chris@3 591 }
Chris@0 592
Chris@0 593 if (m_plotStyle == PlotStems) {
Chris@0 594 paint.setPen(brushColour);
Chris@0 595 if (y < origin - 1) {
Chris@0 596 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 597 } else if (y > origin + 1) {
Chris@0 598 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 599 }
Chris@0 600 paint.setPen(m_colour);
Chris@0 601 }
Chris@0 602
Chris@0 603 if (illuminateFrame == p.frame) {
Chris@6 604
Chris@0 605 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 606 //better to save the highlighted rects and draw them at
Chris@0 607 //the end perhaps
Chris@6 608
Chris@6 609 //!!! not equipped to illuminate the right section in line
Chris@6 610 //or curve mode
Chris@6 611
Chris@6 612 if (m_plotStyle != PlotCurve &&
Chris@6 613 m_plotStyle != PlotLines) {
Chris@6 614 paint.setPen(Qt::black);//!!!
Chris@26 615 if (m_plotStyle != PlotSegmentation) {
Chris@26 616 paint.setBrush(Qt::black);//!!!
Chris@26 617 }
Chris@6 618 }
Chris@0 619 }
Chris@0 620
Chris@6 621 if (m_plotStyle != PlotLines &&
Chris@26 622 m_plotStyle != PlotCurve &&
Chris@26 623 m_plotStyle != PlotSegmentation) {
Chris@3 624 paint.drawRect(x, y - 1, w, 2);
Chris@3 625 }
Chris@0 626
Chris@6 627 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 628 m_plotStyle == PlotLines ||
Chris@6 629 m_plotStyle == PlotCurve) {
Chris@0 630
Chris@34 631 if (haveNext) {
Chris@3 632
Chris@6 633 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 634
Chris@79 635 paint.save();
Chris@3 636 paint.setPen(brushColour);
Chris@3 637 paint.drawLine(x + w, y, nx, ny);
Chris@79 638 paint.restore();
Chris@6 639
Chris@6 640 } else if (m_plotStyle == PlotLines) {
Chris@6 641
Chris@6 642 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 643
Chris@3 644 } else {
Chris@6 645
Chris@55 646 float x0 = x + float(w)/2;
Chris@55 647 float x1 = nx + float(w)/2;
Chris@55 648
Chris@55 649 float y0 = y;
Chris@55 650 float y1 = ny;
Chris@55 651
Chris@55 652 if (pointCount == 0) {
Chris@55 653 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 654 }
Chris@55 655 ++pointCount;
Chris@6 656
Chris@6 657 if (nx - x > 5) {
Chris@55 658 path.cubicTo(x0, y0,
Chris@55 659 x0, y0,
Chris@55 660 (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 661
Chris@55 662 // // or
Chris@55 663 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 664
Chris@6 665 } else {
Chris@55 666 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 667 }
Chris@3 668 }
Chris@0 669 }
Chris@0 670 }
Chris@0 671
Chris@26 672 if (m_plotStyle == PlotSegmentation) {
Chris@76 673
Chris@76 674 // std::cerr << "drawing rect" << std::endl;
Chris@26 675
Chris@27 676 if (nx <= x) continue;
Chris@26 677
Chris@28 678 if (illuminateFrame != p.frame &&
Chris@44 679 (nx < x + 5 || x >= v->width() - 1)) {
Chris@27 680 paint.setPen(Qt::NoPen);
Chris@27 681 }
Chris@26 682
Chris@44 683 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 684 }
Chris@26 685
Chris@55 686 if (p.label != "") {
Chris@79 687 if (!haveNext || nx > x + 6 + paint.fontMetrics().width(p.label)) {
Chris@79 688 paint.drawText(x + 5, textY, p.label);
Chris@79 689 }
Chris@55 690 }
Chris@0 691 }
Chris@6 692
Chris@6 693 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@55 694 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
Chris@6 695 paint.drawPath(path);
Chris@6 696 }
Chris@6 697
Chris@6 698 paint.restore();
Chris@6 699
Chris@6 700 // looks like save/restore doesn't deal with this:
Chris@6 701 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 702 }
Chris@6 703
Chris@42 704 int
Chris@44 705 TimeValueLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@42 706 {
Chris@68 707 int w = paint.fontMetrics().width("-000.000");
Chris@68 708 if (m_plotStyle == PlotSegmentation) return w + 20;
Chris@68 709 else return w + 10;
Chris@42 710 }
Chris@42 711
Chris@42 712 void
Chris@44 713 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@42 714 {
Chris@42 715 if (!m_model) return;
Chris@42 716
Chris@68 717 int h = v->height();
Chris@68 718
Chris@68 719 int n = 10;
Chris@68 720
Chris@68 721 float max = m_model->getValueMaximum();
Chris@68 722 float min = m_model->getValueMinimum();
Chris@68 723 float val = min;
Chris@68 724 float inc = (max - val) / n;
Chris@42 725
Chris@55 726 char buffer[40];
Chris@55 727
Chris@55 728 int w = getVerticalScaleWidth(v, paint);
Chris@55 729
Chris@68 730 int tx = 5;
Chris@68 731
Chris@68 732 int boxx = 5, boxy = 5;
Chris@68 733 if (m_model->getScaleUnits() != "") {
Chris@68 734 boxy += paint.fontMetrics().height();
Chris@68 735 }
Chris@68 736 int boxw = 10, boxh = h - boxy - 5;
Chris@68 737
Chris@68 738 if (m_plotStyle == PlotSegmentation) {
Chris@68 739 tx += boxx + boxw;
Chris@68 740 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@68 741 }
Chris@68 742
Chris@68 743 if (m_plotStyle == PlotSegmentation) {
Chris@68 744 paint.save();
Chris@68 745 for (int y = 0; y < boxh; ++y) {
Chris@68 746 float val = ((boxh - y) * (max - min)) / boxh + min;
Chris@68 747 paint.setPen(getColourForValue(val));
Chris@68 748 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@68 749 }
Chris@68 750 paint.restore();
Chris@68 751 }
Chris@68 752
Chris@68 753 for (int i = 0; i < n; ++i) {
Chris@68 754
Chris@68 755 int y, ty;
Chris@68 756 bool drawText = true;
Chris@68 757
Chris@68 758 if (m_plotStyle == PlotSegmentation) {
Chris@68 759 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@68 760 ty = y;
Chris@68 761 } else {
Chris@68 762 if (i == n-1) {
Chris@68 763 if (m_model->getScaleUnits() != "") drawText = false;
Chris@68 764 }
Chris@68 765 y = getYForValue(v, val);
Chris@68 766 ty = y - paint.fontMetrics().height() +
Chris@68 767 paint.fontMetrics().ascent();
Chris@68 768 }
Chris@68 769
Chris@68 770 sprintf(buffer, "%.3f", val);
Chris@55 771 QString label = QString(buffer);
Chris@68 772
Chris@68 773 if (m_plotStyle != PlotSegmentation) {
Chris@68 774 paint.drawLine(w - 5, y, w, y);
Chris@68 775 } else {
Chris@68 776 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@68 777 }
Chris@68 778
Chris@68 779 if (drawText) paint.drawText(tx, ty, label);
Chris@44 780 val += inc;
Chris@42 781 }
Chris@68 782
Chris@67 783 if (m_model->getScaleUnits() != "") {
Chris@67 784 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
Chris@67 785 m_model->getScaleUnits());
Chris@67 786 }
Chris@42 787 }
Chris@42 788
Chris@21 789 void
Chris@44 790 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 791 {
Chris@21 792 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 793
Chris@21 794 if (!m_model) return;
Chris@21 795
Chris@44 796 long frame = v->getFrameForX(e->x());
Chris@76 797 long resolution = m_model->getResolution();
Chris@21 798 if (frame < 0) frame = 0;
Chris@76 799 frame = (frame / resolution) * resolution;
Chris@21 800
Chris@44 801 float value = getValueForY(v, e->y());
Chris@21 802
Chris@76 803 bool havePoint = false;
Chris@76 804
Chris@76 805 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 806 if (!points.empty()) {
Chris@76 807 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 808 i != points.end(); ++i) {
Chris@76 809 if (((i->frame / resolution) * resolution) != frame) {
Chris@76 810 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 811 continue;
Chris@76 812 }
Chris@76 813 m_editingPoint = *i;
Chris@76 814 havePoint = true;
Chris@76 815 }
Chris@76 816 }
Chris@76 817
Chris@76 818 if (!havePoint) {
Chris@76 819 m_editingPoint = SparseTimeValueModel::Point
Chris@76 820 (frame, value, tr("New Point"));
Chris@76 821 }
Chris@76 822
Chris@23 823 m_originalPoint = m_editingPoint;
Chris@22 824
Chris@22 825 if (m_editingCommand) m_editingCommand->finish();
Chris@22 826 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 827 tr("Draw Point"));
Chris@76 828 if (!havePoint) {
Chris@76 829 m_editingCommand->addPoint(m_editingPoint);
Chris@76 830 }
Chris@22 831
Chris@21 832 m_editing = true;
Chris@21 833 }
Chris@21 834
Chris@21 835 void
Chris@44 836 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 837 {
Chris@21 838 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 839
Chris@21 840 if (!m_model || !m_editing) return;
Chris@21 841
Chris@44 842 long frame = v->getFrameForX(e->x());
Chris@76 843 long resolution = m_model->getResolution();
Chris@21 844 if (frame < 0) frame = 0;
Chris@76 845 frame = (frame / resolution) * resolution;
Chris@21 846
Chris@44 847 float value = getValueForY(v, e->y());
Chris@21 848
Chris@76 849 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 850
Chris@76 851 std::cerr << points.size() << " points" << std::endl;
Chris@76 852
Chris@76 853 bool havePoint = false;
Chris@76 854
Chris@76 855 if (!points.empty()) {
Chris@76 856 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 857 i != points.end(); ++i) {
Chris@76 858 if (i->frame == m_editingPoint.frame &&
Chris@76 859 i->value == m_editingPoint.value) {
Chris@76 860 std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
Chris@76 861 continue;
Chris@76 862 }
Chris@76 863 if (((i->frame / resolution) * resolution) != frame) {
Chris@76 864 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 865 continue;
Chris@76 866 }
Chris@76 867 std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
Chris@76 868 m_editingPoint = *i;
Chris@76 869 m_originalPoint = m_editingPoint;
Chris@76 870 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 871 havePoint = true;
Chris@76 872 }
Chris@76 873 }
Chris@76 874
Chris@76 875 if (!havePoint) {
Chris@76 876 if (frame == m_editingPoint.frame) {
Chris@76 877 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 878 }
Chris@76 879 }
Chris@76 880
Chris@76 881 // m_editingCommand->deletePoint(m_editingPoint);
Chris@21 882 m_editingPoint.frame = frame;
Chris@21 883 m_editingPoint.value = value;
Chris@22 884 m_editingCommand->addPoint(m_editingPoint);
Chris@21 885 }
Chris@21 886
Chris@21 887 void
Chris@44 888 TimeValueLayer::drawEnd(View *v, QMouseEvent *e)
Chris@21 889 {
Chris@21 890 std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 891 if (!m_model || !m_editing) return;
Chris@22 892 m_editingCommand->finish();
Chris@22 893 m_editingCommand = 0;
Chris@21 894 m_editing = false;
Chris@21 895 }
Chris@21 896
Chris@21 897 void
Chris@44 898 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 899 {
Chris@21 900 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 901
Chris@21 902 if (!m_model) return;
Chris@21 903
Chris@44 904 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 905 if (points.empty()) return;
Chris@21 906
Chris@21 907 m_editingPoint = *points.begin();
Chris@23 908 m_originalPoint = m_editingPoint;
Chris@22 909
Chris@22 910 if (m_editingCommand) {
Chris@22 911 m_editingCommand->finish();
Chris@22 912 m_editingCommand = 0;
Chris@22 913 }
Chris@22 914
Chris@21 915 m_editing = true;
Chris@21 916 }
Chris@21 917
Chris@21 918 void
Chris@44 919 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 920 {
Chris@21 921 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 922
Chris@21 923 if (!m_model || !m_editing) return;
Chris@21 924
Chris@44 925 long frame = v->getFrameForX(e->x());
Chris@21 926 if (frame < 0) frame = 0;
Chris@21 927 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 928
Chris@44 929 float value = getValueForY(v, e->y());
Chris@21 930
Chris@22 931 if (!m_editingCommand) {
Chris@22 932 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 933 tr("Drag Point"));
Chris@22 934 }
Chris@22 935
Chris@22 936 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 937 m_editingPoint.frame = frame;
Chris@21 938 m_editingPoint.value = value;
Chris@22 939 m_editingCommand->addPoint(m_editingPoint);
Chris@21 940 }
Chris@21 941
Chris@21 942 void
Chris@44 943 TimeValueLayer::editEnd(View *v, QMouseEvent *e)
Chris@21 944 {
Chris@21 945 std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 946 if (!m_model || !m_editing) return;
Chris@23 947
Chris@23 948 if (m_editingCommand) {
Chris@23 949
Chris@23 950 QString newName = m_editingCommand->getName();
Chris@23 951
Chris@23 952 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 953 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 954 newName = tr("Edit Point");
Chris@23 955 } else {
Chris@23 956 newName = tr("Relocate Point");
Chris@23 957 }
Chris@23 958 } else {
Chris@23 959 newName = tr("Change Point Value");
Chris@23 960 }
Chris@23 961
Chris@23 962 m_editingCommand->setName(newName);
Chris@23 963 m_editingCommand->finish();
Chris@23 964 }
Chris@23 965
Chris@22 966 m_editingCommand = 0;
Chris@21 967 m_editing = false;
Chris@21 968 }
Chris@21 969
Chris@43 970 void
Chris@70 971 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 972 {
Chris@70 973 if (!m_model) return;
Chris@70 974
Chris@70 975 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@70 976 if (points.empty()) return;
Chris@70 977
Chris@70 978 SparseTimeValueModel::Point point = *points.begin();
Chris@70 979
Chris@70 980 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 981 (m_model->getSampleRate(),
Chris@70 982 ItemEditDialog::ShowTime |
Chris@70 983 ItemEditDialog::ShowValue |
Chris@73 984 ItemEditDialog::ShowText,
Chris@73 985 m_model->getScaleUnits());
Chris@70 986
Chris@70 987 dialog->setFrameTime(point.frame);
Chris@70 988 dialog->setValue(point.value);
Chris@70 989 dialog->setText(point.label);
Chris@70 990
Chris@70 991 if (dialog->exec() == QDialog::Accepted) {
Chris@70 992
Chris@70 993 SparseTimeValueModel::Point newPoint = point;
Chris@70 994 newPoint.frame = dialog->getFrameTime();
Chris@70 995 newPoint.value = dialog->getValue();
Chris@70 996 newPoint.label = dialog->getText();
Chris@70 997
Chris@70 998 SparseTimeValueModel::EditCommand *command =
Chris@70 999 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
Chris@70 1000 command->deletePoint(point);
Chris@70 1001 command->addPoint(newPoint);
Chris@70 1002 command->finish();
Chris@70 1003 }
Chris@70 1004
Chris@70 1005 delete dialog;
Chris@70 1006 }
Chris@70 1007
Chris@70 1008 void
Chris@43 1009 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1010 {
Chris@99 1011 if (!m_model) return;
Chris@99 1012
Chris@43 1013 SparseTimeValueModel::EditCommand *command =
Chris@43 1014 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1015 tr("Drag Selection"));
Chris@43 1016
Chris@43 1017 SparseTimeValueModel::PointList points =
Chris@43 1018 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1019
Chris@43 1020 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1021 i != points.end(); ++i) {
Chris@43 1022
Chris@43 1023 if (s.contains(i->frame)) {
Chris@43 1024 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1025 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1026 command->deletePoint(*i);
Chris@43 1027 command->addPoint(newPoint);
Chris@43 1028 }
Chris@43 1029 }
Chris@43 1030
Chris@43 1031 command->finish();
Chris@43 1032 }
Chris@43 1033
Chris@43 1034 void
Chris@43 1035 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1036 {
Chris@99 1037 if (!m_model) return;
Chris@99 1038
Chris@43 1039 SparseTimeValueModel::EditCommand *command =
Chris@43 1040 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1041 tr("Resize Selection"));
Chris@43 1042
Chris@43 1043 SparseTimeValueModel::PointList points =
Chris@43 1044 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1045
Chris@43 1046 double ratio =
Chris@43 1047 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1048 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1049
Chris@43 1050 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1051 i != points.end(); ++i) {
Chris@43 1052
Chris@43 1053 if (s.contains(i->frame)) {
Chris@43 1054
Chris@43 1055 double target = i->frame;
Chris@43 1056 target = newSize.getStartFrame() +
Chris@43 1057 double(target - s.getStartFrame()) * ratio;
Chris@43 1058
Chris@43 1059 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1060 newPoint.frame = lrint(target);
Chris@43 1061 command->deletePoint(*i);
Chris@43 1062 command->addPoint(newPoint);
Chris@43 1063 }
Chris@43 1064 }
Chris@43 1065
Chris@43 1066 command->finish();
Chris@43 1067 }
Chris@43 1068
Chris@76 1069 void
Chris@76 1070 TimeValueLayer::deleteSelection(Selection s)
Chris@76 1071 {
Chris@99 1072 if (!m_model) return;
Chris@99 1073
Chris@76 1074 SparseTimeValueModel::EditCommand *command =
Chris@76 1075 new SparseTimeValueModel::EditCommand(m_model,
Chris@76 1076 tr("Delete Selected Points"));
Chris@76 1077
Chris@76 1078 SparseTimeValueModel::PointList points =
Chris@76 1079 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1080
Chris@76 1081 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1082 i != points.end(); ++i) {
Chris@76 1083
Chris@76 1084 if (s.contains(i->frame)) {
Chris@76 1085 command->deletePoint(*i);
Chris@76 1086 }
Chris@76 1087 }
Chris@76 1088
Chris@76 1089 command->finish();
Chris@76 1090 }
Chris@76 1091
Chris@76 1092 void
Chris@76 1093 TimeValueLayer::copy(Selection s, Clipboard &to)
Chris@76 1094 {
Chris@99 1095 if (!m_model) return;
Chris@99 1096
Chris@76 1097 SparseTimeValueModel::PointList points =
Chris@76 1098 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1099
Chris@76 1100 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1101 i != points.end(); ++i) {
Chris@76 1102 if (s.contains(i->frame)) {
Chris@83 1103 Clipboard::Point point(i->frame, i->value, i->label);
Chris@76 1104 to.addPoint(point);
Chris@76 1105 }
Chris@76 1106 }
Chris@76 1107 }
Chris@76 1108
Chris@76 1109 void
Chris@76 1110 TimeValueLayer::paste(const Clipboard &from, int frameOffset)
Chris@76 1111 {
Chris@99 1112 if (!m_model) return;
Chris@99 1113
Chris@76 1114 const Clipboard::PointList &points = from.getPoints();
Chris@76 1115
Chris@76 1116 SparseTimeValueModel::EditCommand *command =
Chris@76 1117 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
Chris@76 1118
Chris@76 1119 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1120 i != points.end(); ++i) {
Chris@76 1121
Chris@76 1122 if (!i->haveFrame()) continue;
Chris@76 1123 size_t frame = 0;
Chris@76 1124 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
Chris@76 1125 frame = i->getFrame() + frameOffset;
Chris@76 1126 }
Chris@76 1127 SparseTimeValueModel::Point newPoint(frame);
Chris@76 1128
Chris@76 1129 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1130 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1131 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1132 m_model->getValueMaximum()) / 2;
Chris@76 1133
Chris@76 1134 command->addPoint(newPoint);
Chris@76 1135 }
Chris@76 1136
Chris@76 1137 command->finish();
Chris@76 1138 }
Chris@76 1139
Chris@6 1140 QString
Chris@6 1141 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 1142 {
Chris@6 1143 return Layer::toXmlString(indent, extraAttributes +
Chris@6 1144 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@6 1145 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@0 1146 }
Chris@0 1147
Chris@11 1148 void
Chris@11 1149 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1150 {
Chris@11 1151 QString colourSpec = attributes.value("colour");
Chris@11 1152 if (colourSpec != "") {
Chris@11 1153 QColor colour(colourSpec);
Chris@11 1154 if (colour.isValid()) {
Chris@11 1155 setBaseColour(QColor(colourSpec));
Chris@11 1156 }
Chris@11 1157 }
Chris@11 1158
Chris@11 1159 bool ok;
Chris@11 1160 PlotStyle style = (PlotStyle)
Chris@11 1161 attributes.value("plotStyle").toInt(&ok);
Chris@11 1162 if (ok) setPlotStyle(style);
Chris@11 1163 }
Chris@11 1164
Chris@0 1165
Chris@0 1166 #ifdef INCLUDE_MOCFILES
Chris@0 1167 #include "TimeValueLayer.moc.cpp"
Chris@0 1168 #endif
Chris@0 1169