annotate layer/TimeValueLayer.cpp @ 44:ad214997dddb

* Refactor Layer classes so as no longer to store a single View pointer; instead they need to be able to draw themselves on any View on demand. Layers with caches (e.g. spectrogram) will need to be further refactored so as to maintain a per-View cache * Begin refactoring MainWindow by pulling out the document stuff (set of layers, models etc) into a Document class. Not yet in use. This revision is fairly unstable.
author Chris Cannam
date Thu, 02 Mar 2006 16:58:49 +0000
parents 78515b1e29eb
children 128ebfeeebee
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@7 409 if (w > 1 &&
Chris@7 410 (m_plotStyle == PlotLines ||
Chris@7 411 m_plotStyle == PlotCurve)) {
Chris@6 412 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@6 413 }
Chris@6 414 QPainterPath path;
Chris@6 415
Chris@0 416 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 417 i != points.end(); ++i) {
Chris@0 418
Chris@0 419 const SparseTimeValueModel::Point &p(*i);
Chris@0 420
Chris@44 421 int x = v->getXForFrame(p.frame);
Chris@44 422 int y = getYForValue(v, p.value);
Chris@0 423
Chris@34 424 bool haveNext = false;
Chris@44 425 int nx = v->getXForFrame(m_model->getEndFrame());
Chris@34 426 int ny = y;
Chris@34 427
Chris@34 428 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 429 ++j;
Chris@34 430
Chris@34 431 if (j != points.end()) {
Chris@34 432 const SparseTimeValueModel::Point &q(*j);
Chris@44 433 nx = v->getXForFrame(q.frame);
Chris@44 434 ny = getYForValue(v, q.value);
Chris@34 435 haveNext = true;
Chris@34 436 }
Chris@34 437
Chris@34 438 int labelY = y;
Chris@34 439
Chris@0 440 if (w < 1) w = 1;
Chris@26 441 paint.setPen(m_colour);
Chris@0 442
Chris@26 443 if (m_plotStyle == PlotSegmentation) {
Chris@26 444 int value = ((p.value - min) / (max - min)) * 255.999;
Chris@26 445 QColor colour = QColor::fromHsv(256 - value, value / 2 + 128, value);
Chris@26 446 paint.setBrush(QColor(colour.red(), colour.green(), colour.blue(),
Chris@26 447 120));
Chris@44 448 labelY = v->height();
Chris@26 449 } else if (m_plotStyle == PlotLines ||
Chris@26 450 m_plotStyle == PlotCurve) {
Chris@6 451 paint.setBrush(Qt::NoBrush);
Chris@3 452 } else {
Chris@6 453 paint.setBrush(brushColour);
Chris@3 454 }
Chris@0 455
Chris@0 456 if (m_plotStyle == PlotStems) {
Chris@0 457 paint.setPen(brushColour);
Chris@0 458 if (y < origin - 1) {
Chris@0 459 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 460 } else if (y > origin + 1) {
Chris@0 461 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 462 }
Chris@0 463 paint.setPen(m_colour);
Chris@0 464 }
Chris@0 465
Chris@0 466 if (illuminateFrame == p.frame) {
Chris@6 467
Chris@0 468 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 469 //better to save the highlighted rects and draw them at
Chris@0 470 //the end perhaps
Chris@6 471
Chris@6 472 //!!! not equipped to illuminate the right section in line
Chris@6 473 //or curve mode
Chris@6 474
Chris@6 475 if (m_plotStyle != PlotCurve &&
Chris@6 476 m_plotStyle != PlotLines) {
Chris@6 477 paint.setPen(Qt::black);//!!!
Chris@26 478 if (m_plotStyle != PlotSegmentation) {
Chris@26 479 paint.setBrush(Qt::black);//!!!
Chris@26 480 }
Chris@6 481 }
Chris@0 482 }
Chris@0 483
Chris@6 484 if (m_plotStyle != PlotLines &&
Chris@26 485 m_plotStyle != PlotCurve &&
Chris@26 486 m_plotStyle != PlotSegmentation) {
Chris@3 487 paint.drawRect(x, y - 1, w, 2);
Chris@3 488 }
Chris@0 489
Chris@6 490 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 491 m_plotStyle == PlotLines ||
Chris@6 492 m_plotStyle == PlotCurve) {
Chris@0 493
Chris@34 494 if (haveNext) {
Chris@3 495
Chris@6 496 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 497
Chris@3 498 paint.setPen(brushColour);
Chris@3 499 paint.drawLine(x + w, y, nx, ny);
Chris@6 500
Chris@6 501 } else if (m_plotStyle == PlotLines) {
Chris@6 502
Chris@6 503 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 504
Chris@3 505 } else {
Chris@6 506
Chris@6 507 if (path.isEmpty()) {
Chris@6 508 path.moveTo(x + w/2, y);
Chris@6 509 }
Chris@6 510
Chris@6 511 if (nx - x > 5) {
Chris@6 512 path.cubicTo(x + w, y, nx, ny, nx + w/2, ny);
Chris@6 513 } else {
Chris@6 514 path.lineTo(nx + w/2, ny);
Chris@6 515 }
Chris@3 516 }
Chris@0 517 }
Chris@0 518 }
Chris@0 519
Chris@26 520 if (m_plotStyle == PlotSegmentation) {
Chris@26 521
Chris@27 522 if (nx <= x) continue;
Chris@26 523
Chris@28 524 if (illuminateFrame != p.frame &&
Chris@44 525 (nx < x + 5 || x >= v->width() - 1)) {
Chris@27 526 paint.setPen(Qt::NoPen);
Chris@27 527 }
Chris@26 528
Chris@44 529 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 530 }
Chris@26 531
Chris@0 532 /// if (p.label != "") {
Chris@0 533 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@0 534 /// }
Chris@0 535 }
Chris@6 536
Chris@6 537 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@6 538 paint.drawPath(path);
Chris@6 539 }
Chris@6 540
Chris@6 541 paint.restore();
Chris@6 542
Chris@6 543 // looks like save/restore doesn't deal with this:
Chris@6 544 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 545 }
Chris@6 546
Chris@42 547 int
Chris@44 548 TimeValueLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@42 549 {
Chris@42 550 return 100; //!!!
Chris@42 551 }
Chris@42 552
Chris@42 553 void
Chris@44 554 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@42 555 {
Chris@42 556 if (!m_model) return;
Chris@42 557
Chris@44 558 float val = m_model->getValueMinimum();
Chris@44 559 float inc = (m_model->getValueMaximum() - val) / 10;
Chris@42 560
Chris@44 561 while (val < m_model->getValueMaximum()) {
Chris@44 562 int y = getYForValue(v, val);
Chris@44 563 QString label = QString("%1").arg(val);
Chris@42 564 paint.drawLine(100 - 10, y, 100, y);
Chris@42 565 paint.drawText(100 - 15 - paint.fontMetrics().width(label),
Chris@42 566 y - paint.fontMetrics().height() /2 + paint.fontMetrics().ascent(),
Chris@42 567 label);
Chris@44 568 val += inc;
Chris@42 569 }
Chris@42 570
Chris@42 571 }
Chris@42 572
Chris@21 573 void
Chris@44 574 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 575 {
Chris@21 576 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 577
Chris@21 578 if (!m_model) return;
Chris@21 579
Chris@44 580 long frame = v->getFrameForX(e->x());
Chris@21 581 if (frame < 0) frame = 0;
Chris@21 582 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 583
Chris@44 584 float value = getValueForY(v, e->y());
Chris@21 585
Chris@21 586 m_editingPoint = SparseTimeValueModel::Point(frame, value, tr("New Point"));
Chris@23 587 m_originalPoint = m_editingPoint;
Chris@22 588
Chris@22 589 if (m_editingCommand) m_editingCommand->finish();
Chris@22 590 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 591 tr("Draw Point"));
Chris@22 592 m_editingCommand->addPoint(m_editingPoint);
Chris@22 593
Chris@21 594 m_editing = true;
Chris@21 595 }
Chris@21 596
Chris@21 597 void
Chris@44 598 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 599 {
Chris@21 600 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 601
Chris@21 602 if (!m_model || !m_editing) 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@22 610 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 611 m_editingPoint.frame = frame;
Chris@21 612 m_editingPoint.value = value;
Chris@22 613 m_editingCommand->addPoint(m_editingPoint);
Chris@21 614 }
Chris@21 615
Chris@21 616 void
Chris@44 617 TimeValueLayer::drawEnd(View *v, QMouseEvent *e)
Chris@21 618 {
Chris@21 619 std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 620 if (!m_model || !m_editing) return;
Chris@22 621 m_editingCommand->finish();
Chris@22 622 m_editingCommand = 0;
Chris@21 623 m_editing = false;
Chris@21 624 }
Chris@21 625
Chris@21 626 void
Chris@44 627 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 628 {
Chris@21 629 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 630
Chris@21 631 if (!m_model) return;
Chris@21 632
Chris@44 633 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 634 if (points.empty()) return;
Chris@21 635
Chris@21 636 m_editingPoint = *points.begin();
Chris@23 637 m_originalPoint = m_editingPoint;
Chris@22 638
Chris@22 639 if (m_editingCommand) {
Chris@22 640 m_editingCommand->finish();
Chris@22 641 m_editingCommand = 0;
Chris@22 642 }
Chris@22 643
Chris@21 644 m_editing = true;
Chris@21 645 }
Chris@21 646
Chris@21 647 void
Chris@44 648 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 649 {
Chris@21 650 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 651
Chris@21 652 if (!m_model || !m_editing) return;
Chris@21 653
Chris@44 654 long frame = v->getFrameForX(e->x());
Chris@21 655 if (frame < 0) frame = 0;
Chris@21 656 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 657
Chris@44 658 float value = getValueForY(v, e->y());
Chris@21 659
Chris@22 660 if (!m_editingCommand) {
Chris@22 661 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 662 tr("Drag Point"));
Chris@22 663 }
Chris@22 664
Chris@22 665 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 666 m_editingPoint.frame = frame;
Chris@21 667 m_editingPoint.value = value;
Chris@22 668 m_editingCommand->addPoint(m_editingPoint);
Chris@21 669 }
Chris@21 670
Chris@21 671 void
Chris@44 672 TimeValueLayer::editEnd(View *v, QMouseEvent *e)
Chris@21 673 {
Chris@21 674 std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 675 if (!m_model || !m_editing) return;
Chris@23 676
Chris@23 677 if (m_editingCommand) {
Chris@23 678
Chris@23 679 QString newName = m_editingCommand->getName();
Chris@23 680
Chris@23 681 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 682 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 683 newName = tr("Edit Point");
Chris@23 684 } else {
Chris@23 685 newName = tr("Relocate Point");
Chris@23 686 }
Chris@23 687 } else {
Chris@23 688 newName = tr("Change Point Value");
Chris@23 689 }
Chris@23 690
Chris@23 691 m_editingCommand->setName(newName);
Chris@23 692 m_editingCommand->finish();
Chris@23 693 }
Chris@23 694
Chris@22 695 m_editingCommand = 0;
Chris@21 696 m_editing = false;
Chris@21 697 }
Chris@21 698
Chris@43 699 void
Chris@43 700 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 701 {
Chris@43 702 SparseTimeValueModel::EditCommand *command =
Chris@43 703 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 704 tr("Drag Selection"));
Chris@43 705
Chris@43 706 SparseTimeValueModel::PointList points =
Chris@43 707 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 708
Chris@43 709 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 710 i != points.end(); ++i) {
Chris@43 711
Chris@43 712 if (s.contains(i->frame)) {
Chris@43 713 SparseTimeValueModel::Point newPoint(*i);
Chris@43 714 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 715 command->deletePoint(*i);
Chris@43 716 command->addPoint(newPoint);
Chris@43 717 }
Chris@43 718 }
Chris@43 719
Chris@43 720 command->finish();
Chris@43 721 }
Chris@43 722
Chris@43 723 void
Chris@43 724 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 725 {
Chris@43 726 SparseTimeValueModel::EditCommand *command =
Chris@43 727 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 728 tr("Resize 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 double ratio =
Chris@43 734 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 735 double(s.getEndFrame() - s.getStartFrame());
Chris@43 736
Chris@43 737 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 738 i != points.end(); ++i) {
Chris@43 739
Chris@43 740 if (s.contains(i->frame)) {
Chris@43 741
Chris@43 742 double target = i->frame;
Chris@43 743 target = newSize.getStartFrame() +
Chris@43 744 double(target - s.getStartFrame()) * ratio;
Chris@43 745
Chris@43 746 SparseTimeValueModel::Point newPoint(*i);
Chris@43 747 newPoint.frame = lrint(target);
Chris@43 748 command->deletePoint(*i);
Chris@43 749 command->addPoint(newPoint);
Chris@43 750 }
Chris@43 751 }
Chris@43 752
Chris@43 753 command->finish();
Chris@43 754 }
Chris@43 755
Chris@6 756 QString
Chris@6 757 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 758 {
Chris@6 759 return Layer::toXmlString(indent, extraAttributes +
Chris@6 760 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@6 761 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@0 762 }
Chris@0 763
Chris@11 764 void
Chris@11 765 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 766 {
Chris@11 767 QString colourSpec = attributes.value("colour");
Chris@11 768 if (colourSpec != "") {
Chris@11 769 QColor colour(colourSpec);
Chris@11 770 if (colour.isValid()) {
Chris@11 771 setBaseColour(QColor(colourSpec));
Chris@11 772 }
Chris@11 773 }
Chris@11 774
Chris@11 775 bool ok;
Chris@11 776 PlotStyle style = (PlotStyle)
Chris@11 777 attributes.value("plotStyle").toInt(&ok);
Chris@11 778 if (ok) setPlotStyle(style);
Chris@11 779 }
Chris@11 780
Chris@0 781
Chris@0 782 #ifdef INCLUDE_MOCFILES
Chris@0 783 #include "TimeValueLayer.moc.cpp"
Chris@0 784 #endif
Chris@0 785