annotate layer/TimeValueLayer.cpp @ 59:705f05ab42e3

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