annotate layer/TimeValueLayer.cpp @ 68:193b569a975f

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