annotate layer/TimeValueLayer.cpp @ 26:94381052a6c9

* Add natty segmentation display to time-value layer. Need to do the same for time-instants layer. * Make sure dense 3D model bin names are saved and restored properly. * Fix to chromagram normalization
author Chris Cannam
date Thu, 02 Feb 2006 17:31:08 +0000
parents dcdb21b62dbb
children 38fe0ea9e46e
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@0 26 TimeValueLayer::TimeValueLayer(View *w) :
Chris@0 27 Layer(w),
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@0 36 m_view->addLayer(this);
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@0 170 TimeValueLayer::isLayerScrollable() 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@0 179 return !m_view->shouldIlluminateLocalFeatures(this, discard);
Chris@0 180 }
Chris@0 181
Chris@0 182 SparseTimeValueModel::PointList
Chris@0 183 TimeValueLayer::getLocalPoints(int x) const
Chris@0 184 {
Chris@0 185 if (!m_model) return SparseTimeValueModel::PointList();
Chris@0 186
Chris@20 187 long frame = 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@20 205 } else if (prevPoints.begin()->frame < m_view->getStartFrame() &&
Chris@20 206 !(nextPoints.begin()->frame > m_view->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@0 213 return usePoints;
Chris@0 214 }
Chris@0 215
Chris@25 216 QString
Chris@25 217 TimeValueLayer::getFeatureDescription(QPoint &pos) const
Chris@0 218 {
Chris@25 219 int x = pos.x();
Chris@0 220
Chris@25 221 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 222
Chris@0 223 SparseTimeValueModel::PointList points = getLocalPoints(x);
Chris@0 224
Chris@0 225 if (points.empty()) {
Chris@0 226 if (!m_model->isReady()) {
Chris@25 227 return tr("In progress");
Chris@25 228 } else {
Chris@25 229 return tr("No local points");
Chris@0 230 }
Chris@0 231 }
Chris@0 232
Chris@0 233 long useFrame = points.begin()->frame;
Chris@0 234
Chris@0 235 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 236
Chris@25 237 QString text;
Chris@0 238
Chris@25 239 if (points.begin()->label == "") {
Chris@25 240 text = QString(tr("Time:\t%1\nValue:\t%2\nNo label"))
Chris@25 241 .arg(rt.toText(true).c_str())
Chris@25 242 .arg(points.begin()->value);
Chris@25 243 } else {
Chris@25 244 text = QString(tr("Time:\t%1\nValue:\t%2\nLabel:\t%3"))
Chris@25 245 .arg(rt.toText(true).c_str())
Chris@25 246 .arg(points.begin()->value)
Chris@25 247 .arg(points.begin()->label);
Chris@25 248 }
Chris@0 249
Chris@25 250 pos = QPoint(getXForFrame(useFrame), getYForValue(points.begin()->value));
Chris@25 251 return text;
Chris@0 252 }
Chris@0 253
Chris@13 254 int
Chris@13 255 TimeValueLayer::getNearestFeatureFrame(int frame,
Chris@13 256 size_t &resolution,
Chris@13 257 bool snapRight) const
Chris@13 258 {
Chris@13 259 if (!m_model) {
Chris@13 260 return Layer::getNearestFeatureFrame(frame, resolution, snapRight);
Chris@13 261 }
Chris@13 262
Chris@13 263 resolution = m_model->getResolution();
Chris@13 264 SparseTimeValueModel::PointList points(m_model->getPoints(frame, frame));
Chris@13 265
Chris@13 266 int returnFrame = frame;
Chris@13 267
Chris@13 268 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@13 269 i != points.end(); ++i) {
Chris@13 270
Chris@13 271 if (snapRight) {
Chris@13 272 if (i->frame > frame) {
Chris@13 273 returnFrame = i->frame;
Chris@13 274 break;
Chris@13 275 }
Chris@13 276 } else {
Chris@13 277 if (i->frame <= frame) {
Chris@13 278 returnFrame = i->frame;
Chris@13 279 }
Chris@13 280 }
Chris@13 281 }
Chris@13 282
Chris@13 283 return returnFrame;
Chris@13 284 }
Chris@13 285
Chris@21 286 int
Chris@21 287 TimeValueLayer::getYForValue(float value) const
Chris@21 288 {
Chris@21 289 float min = m_model->getValueMinimum();
Chris@21 290 float max = m_model->getValueMaximum();
Chris@21 291 if (max == min) max = min + 1.0;
Chris@21 292
Chris@21 293 int h = m_view->height();
Chris@21 294
Chris@21 295 return int(h - ((value - min) * h) / (max - min));
Chris@21 296 }
Chris@21 297
Chris@21 298 float
Chris@21 299 TimeValueLayer::getValueForY(int y) const
Chris@21 300 {
Chris@21 301 float min = m_model->getValueMinimum();
Chris@21 302 float max = m_model->getValueMaximum();
Chris@21 303 if (max == min) max = min + 1.0;
Chris@21 304
Chris@21 305 int h = m_view->height();
Chris@21 306
Chris@21 307 return min + (float(h - y) * float(max - min)) / h;
Chris@21 308 }
Chris@21 309
Chris@0 310 void
Chris@0 311 TimeValueLayer::paint(QPainter &paint, QRect rect) const
Chris@0 312 {
Chris@0 313 if (!m_model || !m_model->isOK()) return;
Chris@0 314
Chris@0 315 int sampleRate = m_model->getSampleRate();
Chris@0 316 if (!sampleRate) return;
Chris@0 317
Chris@0 318 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 319
Chris@0 320 int x0 = rect.left(), x1 = rect.right();
Chris@20 321 long frame0 = getFrameForX(x0);
Chris@20 322 long frame1 = getFrameForX(x1);
Chris@0 323
Chris@0 324 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 325 (frame0, frame1));
Chris@11 326 if (points.empty()) return;
Chris@0 327
Chris@0 328 paint.setPen(m_colour);
Chris@0 329
Chris@0 330 QColor brushColour(m_colour);
Chris@0 331 brushColour.setAlpha(80);
Chris@0 332 paint.setBrush(brushColour);
Chris@0 333
Chris@0 334 // std::cerr << "TimeValueLayer::paint: resolution is "
Chris@0 335 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 336
Chris@0 337 float min = m_model->getValueMinimum();
Chris@0 338 float max = m_model->getValueMaximum();
Chris@0 339 if (max == min) max = min + 1.0;
Chris@0 340
Chris@0 341 int origin = int(nearbyint(m_view->height() -
Chris@0 342 (-min * m_view->height()) / (max - min)));
Chris@0 343
Chris@0 344 QPoint localPos;
Chris@0 345 long illuminateFrame = -1;
Chris@0 346
Chris@0 347 if (m_view->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 348 SparseTimeValueModel::PointList localPoints =
Chris@0 349 getLocalPoints(localPos.x());
Chris@0 350 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 351 }
Chris@6 352
Chris@20 353 int w =
Chris@20 354 getXForFrame(frame0 + m_model->getResolution()) -
Chris@20 355 getXForFrame(frame0);
Chris@7 356
Chris@6 357 paint.save();
Chris@6 358
Chris@7 359 if (w > 1 &&
Chris@7 360 (m_plotStyle == PlotLines ||
Chris@7 361 m_plotStyle == PlotCurve)) {
Chris@6 362 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@6 363 }
Chris@6 364 QPainterPath path;
Chris@6 365
Chris@0 366 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 367 i != points.end(); ++i) {
Chris@0 368
Chris@0 369 const SparseTimeValueModel::Point &p(*i);
Chris@0 370
Chris@20 371 int x = getXForFrame(p.frame);
Chris@21 372 int y = getYForValue(p.value);
Chris@0 373
Chris@0 374 if (w < 1) w = 1;
Chris@26 375 paint.setPen(m_colour);
Chris@0 376
Chris@26 377 if (m_plotStyle == PlotSegmentation) {
Chris@26 378 int value = ((p.value - min) / (max - min)) * 255.999;
Chris@26 379 QColor colour = QColor::fromHsv(256 - value, value / 2 + 128, value);
Chris@26 380 paint.setBrush(QColor(colour.red(), colour.green(), colour.blue(),
Chris@26 381 120));
Chris@26 382 } else if (m_plotStyle == PlotLines ||
Chris@26 383 m_plotStyle == PlotCurve) {
Chris@6 384 paint.setBrush(Qt::NoBrush);
Chris@3 385 } else {
Chris@6 386 paint.setBrush(brushColour);
Chris@3 387 }
Chris@0 388
Chris@0 389 if (m_plotStyle == PlotStems) {
Chris@0 390 paint.setPen(brushColour);
Chris@0 391 if (y < origin - 1) {
Chris@0 392 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 393 } else if (y > origin + 1) {
Chris@0 394 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 395 }
Chris@0 396 paint.setPen(m_colour);
Chris@0 397 }
Chris@0 398
Chris@0 399 if (illuminateFrame == p.frame) {
Chris@6 400
Chris@0 401 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 402 //better to save the highlighted rects and draw them at
Chris@0 403 //the end perhaps
Chris@6 404
Chris@6 405 //!!! not equipped to illuminate the right section in line
Chris@6 406 //or curve mode
Chris@6 407
Chris@6 408 if (m_plotStyle != PlotCurve &&
Chris@6 409 m_plotStyle != PlotLines) {
Chris@6 410 paint.setPen(Qt::black);//!!!
Chris@26 411 if (m_plotStyle != PlotSegmentation) {
Chris@26 412 paint.setBrush(Qt::black);//!!!
Chris@26 413 }
Chris@6 414 }
Chris@0 415 }
Chris@0 416
Chris@6 417 if (m_plotStyle != PlotLines &&
Chris@26 418 m_plotStyle != PlotCurve &&
Chris@26 419 m_plotStyle != PlotSegmentation) {
Chris@3 420 paint.drawRect(x, y - 1, w, 2);
Chris@3 421 }
Chris@0 422
Chris@6 423 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 424 m_plotStyle == PlotLines ||
Chris@6 425 m_plotStyle == PlotCurve) {
Chris@0 426
Chris@0 427 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@0 428 ++j;
Chris@3 429
Chris@0 430 if (j != points.end()) {
Chris@3 431
Chris@0 432 const SparseTimeValueModel::Point &q(*j);
Chris@20 433 int nx = getXForFrame(q.frame);
Chris@21 434 int ny = getYForValue(q.value);
Chris@3 435
Chris@6 436 if (m_plotStyle == PlotConnectedPoints) {
Chris@6 437
Chris@3 438 paint.setPen(brushColour);
Chris@3 439 paint.drawLine(x + w, y, nx, ny);
Chris@6 440
Chris@6 441 } else if (m_plotStyle == PlotLines) {
Chris@6 442
Chris@6 443 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 444
Chris@3 445 } else {
Chris@6 446
Chris@6 447 if (path.isEmpty()) {
Chris@6 448 path.moveTo(x + w/2, y);
Chris@6 449 }
Chris@6 450
Chris@6 451 if (nx - x > 5) {
Chris@6 452 path.cubicTo(x + w, y, nx, ny, nx + w/2, ny);
Chris@6 453 } else {
Chris@6 454 path.lineTo(nx + w/2, ny);
Chris@6 455 }
Chris@3 456 }
Chris@0 457 }
Chris@0 458 }
Chris@0 459
Chris@26 460 if (m_plotStyle == PlotSegmentation) {
Chris@26 461
Chris@26 462 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@26 463 ++j;
Chris@26 464
Chris@26 465 if (j != points.end()) {
Chris@26 466
Chris@26 467 const SparseTimeValueModel::Point &q(*j);
Chris@26 468 int nx = getXForFrame(q.frame);
Chris@26 469
Chris@26 470 if (nx == x) continue;
Chris@26 471
Chris@26 472 if (nx < x + 5 && illuminateFrame != p.frame) {
Chris@26 473 paint.setPen(Qt::NoPen);
Chris@26 474 }
Chris@26 475
Chris@26 476 paint.drawRect(x, -1, nx - x, m_view->height() + 1);
Chris@26 477
Chris@26 478 } else {
Chris@26 479
Chris@26 480 paint.drawLine(x, 0, x, m_view->height());
Chris@26 481 }
Chris@26 482 }
Chris@26 483
Chris@0 484 /// if (p.label != "") {
Chris@0 485 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@0 486 /// }
Chris@0 487 }
Chris@6 488
Chris@6 489 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@6 490 paint.drawPath(path);
Chris@6 491 }
Chris@6 492
Chris@6 493 paint.restore();
Chris@6 494
Chris@6 495 // looks like save/restore doesn't deal with this:
Chris@6 496 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 497 }
Chris@6 498
Chris@21 499 void
Chris@21 500 TimeValueLayer::drawStart(QMouseEvent *e)
Chris@21 501 {
Chris@21 502 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 503
Chris@21 504 if (!m_model) return;
Chris@21 505
Chris@21 506 long frame = getFrameForX(e->x());
Chris@21 507 if (frame < 0) frame = 0;
Chris@21 508 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 509
Chris@21 510 float value = getValueForY(e->y());
Chris@21 511
Chris@21 512 m_editingPoint = SparseTimeValueModel::Point(frame, value, tr("New Point"));
Chris@23 513 m_originalPoint = m_editingPoint;
Chris@22 514
Chris@22 515 if (m_editingCommand) m_editingCommand->finish();
Chris@22 516 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 517 tr("Draw Point"));
Chris@22 518 m_editingCommand->addPoint(m_editingPoint);
Chris@22 519
Chris@21 520 m_editing = true;
Chris@21 521 }
Chris@21 522
Chris@21 523 void
Chris@21 524 TimeValueLayer::drawDrag(QMouseEvent *e)
Chris@21 525 {
Chris@21 526 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 527
Chris@21 528 if (!m_model || !m_editing) return;
Chris@21 529
Chris@21 530 long frame = getFrameForX(e->x());
Chris@21 531 if (frame < 0) frame = 0;
Chris@21 532 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 533
Chris@21 534 float value = getValueForY(e->y());
Chris@21 535
Chris@22 536 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 537 m_editingPoint.frame = frame;
Chris@21 538 m_editingPoint.value = value;
Chris@22 539 m_editingCommand->addPoint(m_editingPoint);
Chris@21 540 }
Chris@21 541
Chris@21 542 void
Chris@21 543 TimeValueLayer::drawEnd(QMouseEvent *e)
Chris@21 544 {
Chris@21 545 std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 546 if (!m_model || !m_editing) return;
Chris@22 547 m_editingCommand->finish();
Chris@22 548 m_editingCommand = 0;
Chris@21 549 m_editing = false;
Chris@21 550 }
Chris@21 551
Chris@21 552 void
Chris@21 553 TimeValueLayer::editStart(QMouseEvent *e)
Chris@21 554 {
Chris@21 555 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 556
Chris@21 557 if (!m_model) return;
Chris@21 558
Chris@21 559 SparseTimeValueModel::PointList points = getLocalPoints(e->x());
Chris@21 560 if (points.empty()) return;
Chris@21 561
Chris@21 562 m_editingPoint = *points.begin();
Chris@23 563 m_originalPoint = m_editingPoint;
Chris@22 564
Chris@22 565 if (m_editingCommand) {
Chris@22 566 m_editingCommand->finish();
Chris@22 567 m_editingCommand = 0;
Chris@22 568 }
Chris@22 569
Chris@21 570 m_editing = true;
Chris@21 571 }
Chris@21 572
Chris@21 573 void
Chris@21 574 TimeValueLayer::editDrag(QMouseEvent *e)
Chris@21 575 {
Chris@21 576 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 577
Chris@21 578 if (!m_model || !m_editing) return;
Chris@21 579
Chris@21 580 long frame = 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@21 584 float value = getValueForY(e->y());
Chris@21 585
Chris@22 586 if (!m_editingCommand) {
Chris@22 587 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 588 tr("Drag Point"));
Chris@22 589 }
Chris@22 590
Chris@22 591 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 592 m_editingPoint.frame = frame;
Chris@21 593 m_editingPoint.value = value;
Chris@22 594 m_editingCommand->addPoint(m_editingPoint);
Chris@21 595 }
Chris@21 596
Chris@21 597 void
Chris@21 598 TimeValueLayer::editEnd(QMouseEvent *e)
Chris@21 599 {
Chris@21 600 std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 601 if (!m_model || !m_editing) return;
Chris@23 602
Chris@23 603 if (m_editingCommand) {
Chris@23 604
Chris@23 605 QString newName = m_editingCommand->getName();
Chris@23 606
Chris@23 607 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 608 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 609 newName = tr("Edit Point");
Chris@23 610 } else {
Chris@23 611 newName = tr("Relocate Point");
Chris@23 612 }
Chris@23 613 } else {
Chris@23 614 newName = tr("Change Point Value");
Chris@23 615 }
Chris@23 616
Chris@23 617 m_editingCommand->setName(newName);
Chris@23 618 m_editingCommand->finish();
Chris@23 619 }
Chris@23 620
Chris@22 621 m_editingCommand = 0;
Chris@21 622 m_editing = false;
Chris@21 623 }
Chris@21 624
Chris@6 625 QString
Chris@6 626 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 627 {
Chris@6 628 return Layer::toXmlString(indent, extraAttributes +
Chris@6 629 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@6 630 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@0 631 }
Chris@0 632
Chris@11 633 void
Chris@11 634 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 635 {
Chris@11 636 QString colourSpec = attributes.value("colour");
Chris@11 637 if (colourSpec != "") {
Chris@11 638 QColor colour(colourSpec);
Chris@11 639 if (colour.isValid()) {
Chris@11 640 setBaseColour(QColor(colourSpec));
Chris@11 641 }
Chris@11 642 }
Chris@11 643
Chris@11 644 bool ok;
Chris@11 645 PlotStyle style = (PlotStyle)
Chris@11 646 attributes.value("plotStyle").toInt(&ok);
Chris@11 647 if (ok) setPlotStyle(style);
Chris@11 648 }
Chris@11 649
Chris@0 650
Chris@0 651 #ifdef INCLUDE_MOCFILES
Chris@0 652 #include "TimeValueLayer.moc.cpp"
Chris@0 653 #endif
Chris@0 654