annotate layer/TimeValueLayer.cpp @ 56:fedaf3ffe80a

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