annotate layer/TimeValueLayer.cpp @ 25:dcdb21b62dbb

* Refactor sparse models. Previously the 1D and time-value models duplicated a lot of code; now there is a base class (SparseModel) templated on the stored point type, and the subclasses define point types with the necessary characteristics. * Add NoteModel, a new SparseModel subclass. * Reorganise local feature description display. Instead of asking the layer to draw its own, just query it for a textual description and draw that in Pane. Greatly simplifies this part of the layer code. * Add local feature descriptions to colour 3D plot and waveform layers. * Add pitch in MIDI-pitch-and-cents to spectrogram layer. * Give AudioGenerator its own mutex to shorten lock times in CallbackPlaySource. * Minor adjustments to layers menu &c
author Chris Cannam
date Thu, 02 Feb 2006 16:10:19 +0000
parents ca57f70b0e48
children 94381052a6c9
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@10 95 if (max) *max = 4;
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@0 129 }
Chris@0 130 }
Chris@0 131 return tr("<unknown>");
Chris@0 132 }
Chris@0 133
Chris@0 134 void
Chris@0 135 TimeValueLayer::setProperty(const PropertyName &name, int value)
Chris@0 136 {
Chris@0 137 if (name == tr("Colour")) {
Chris@0 138 switch (value) {
Chris@0 139 default:
Chris@0 140 case 0: setBaseColour(Qt::black); break;
Chris@0 141 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 142 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 143 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 144 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 145 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 146 }
Chris@0 147 } else if (name == tr("Plot Type")) {
Chris@0 148 setPlotStyle(PlotStyle(value));
Chris@0 149 }
Chris@0 150 }
Chris@0 151
Chris@0 152 void
Chris@0 153 TimeValueLayer::setBaseColour(QColor colour)
Chris@0 154 {
Chris@0 155 if (m_colour == colour) return;
Chris@0 156 m_colour = colour;
Chris@0 157 emit layerParametersChanged();
Chris@0 158 }
Chris@0 159
Chris@0 160 void
Chris@0 161 TimeValueLayer::setPlotStyle(PlotStyle style)
Chris@0 162 {
Chris@0 163 if (m_plotStyle == style) return;
Chris@0 164 m_plotStyle = style;
Chris@0 165 emit layerParametersChanged();
Chris@0 166 }
Chris@0 167
Chris@0 168 bool
Chris@0 169 TimeValueLayer::isLayerScrollable() const
Chris@0 170 {
Chris@6 171 // We don't illuminate sections in the line or curve modes, so
Chris@6 172 // they're always scrollable
Chris@6 173
Chris@6 174 if (m_plotStyle == PlotLines ||
Chris@6 175 m_plotStyle == PlotCurve) return true;
Chris@6 176
Chris@0 177 QPoint discard;
Chris@0 178 return !m_view->shouldIlluminateLocalFeatures(this, discard);
Chris@0 179 }
Chris@0 180
Chris@0 181 SparseTimeValueModel::PointList
Chris@0 182 TimeValueLayer::getLocalPoints(int x) const
Chris@0 183 {
Chris@0 184 if (!m_model) return SparseTimeValueModel::PointList();
Chris@0 185
Chris@20 186 long frame = getFrameForX(x);
Chris@0 187
Chris@0 188 SparseTimeValueModel::PointList onPoints =
Chris@0 189 m_model->getPoints(frame);
Chris@0 190
Chris@0 191 if (!onPoints.empty()) {
Chris@0 192 return onPoints;
Chris@0 193 }
Chris@0 194
Chris@0 195 SparseTimeValueModel::PointList prevPoints =
Chris@0 196 m_model->getPreviousPoints(frame);
Chris@0 197 SparseTimeValueModel::PointList nextPoints =
Chris@0 198 m_model->getNextPoints(frame);
Chris@0 199
Chris@0 200 SparseTimeValueModel::PointList usePoints = prevPoints;
Chris@0 201
Chris@0 202 if (prevPoints.empty()) {
Chris@0 203 usePoints = nextPoints;
Chris@20 204 } else if (prevPoints.begin()->frame < m_view->getStartFrame() &&
Chris@20 205 !(nextPoints.begin()->frame > m_view->getEndFrame())) {
Chris@0 206 usePoints = nextPoints;
Chris@0 207 } else if (nextPoints.begin()->frame - frame <
Chris@0 208 frame - prevPoints.begin()->frame) {
Chris@0 209 usePoints = nextPoints;
Chris@0 210 }
Chris@0 211
Chris@0 212 return usePoints;
Chris@0 213 }
Chris@0 214
Chris@25 215 QString
Chris@25 216 TimeValueLayer::getFeatureDescription(QPoint &pos) const
Chris@0 217 {
Chris@25 218 int x = pos.x();
Chris@0 219
Chris@25 220 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 221
Chris@0 222 SparseTimeValueModel::PointList points = getLocalPoints(x);
Chris@0 223
Chris@0 224 if (points.empty()) {
Chris@0 225 if (!m_model->isReady()) {
Chris@25 226 return tr("In progress");
Chris@25 227 } else {
Chris@25 228 return tr("No local points");
Chris@0 229 }
Chris@0 230 }
Chris@0 231
Chris@0 232 long useFrame = points.begin()->frame;
Chris@0 233
Chris@0 234 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 235
Chris@25 236 QString text;
Chris@0 237
Chris@25 238 if (points.begin()->label == "") {
Chris@25 239 text = QString(tr("Time:\t%1\nValue:\t%2\nNo label"))
Chris@25 240 .arg(rt.toText(true).c_str())
Chris@25 241 .arg(points.begin()->value);
Chris@25 242 } else {
Chris@25 243 text = QString(tr("Time:\t%1\nValue:\t%2\nLabel:\t%3"))
Chris@25 244 .arg(rt.toText(true).c_str())
Chris@25 245 .arg(points.begin()->value)
Chris@25 246 .arg(points.begin()->label);
Chris@25 247 }
Chris@0 248
Chris@25 249 pos = QPoint(getXForFrame(useFrame), getYForValue(points.begin()->value));
Chris@25 250 return text;
Chris@0 251 }
Chris@0 252
Chris@13 253 int
Chris@13 254 TimeValueLayer::getNearestFeatureFrame(int frame,
Chris@13 255 size_t &resolution,
Chris@13 256 bool snapRight) const
Chris@13 257 {
Chris@13 258 if (!m_model) {
Chris@13 259 return Layer::getNearestFeatureFrame(frame, resolution, snapRight);
Chris@13 260 }
Chris@13 261
Chris@13 262 resolution = m_model->getResolution();
Chris@13 263 SparseTimeValueModel::PointList points(m_model->getPoints(frame, frame));
Chris@13 264
Chris@13 265 int returnFrame = frame;
Chris@13 266
Chris@13 267 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@13 268 i != points.end(); ++i) {
Chris@13 269
Chris@13 270 if (snapRight) {
Chris@13 271 if (i->frame > frame) {
Chris@13 272 returnFrame = i->frame;
Chris@13 273 break;
Chris@13 274 }
Chris@13 275 } else {
Chris@13 276 if (i->frame <= frame) {
Chris@13 277 returnFrame = i->frame;
Chris@13 278 }
Chris@13 279 }
Chris@13 280 }
Chris@13 281
Chris@13 282 return returnFrame;
Chris@13 283 }
Chris@13 284
Chris@21 285 int
Chris@21 286 TimeValueLayer::getYForValue(float value) const
Chris@21 287 {
Chris@21 288 float min = m_model->getValueMinimum();
Chris@21 289 float max = m_model->getValueMaximum();
Chris@21 290 if (max == min) max = min + 1.0;
Chris@21 291
Chris@21 292 int h = m_view->height();
Chris@21 293
Chris@21 294 return int(h - ((value - min) * h) / (max - min));
Chris@21 295 }
Chris@21 296
Chris@21 297 float
Chris@21 298 TimeValueLayer::getValueForY(int y) const
Chris@21 299 {
Chris@21 300 float min = m_model->getValueMinimum();
Chris@21 301 float max = m_model->getValueMaximum();
Chris@21 302 if (max == min) max = min + 1.0;
Chris@21 303
Chris@21 304 int h = m_view->height();
Chris@21 305
Chris@21 306 return min + (float(h - y) * float(max - min)) / h;
Chris@21 307 }
Chris@21 308
Chris@0 309 void
Chris@0 310 TimeValueLayer::paint(QPainter &paint, QRect rect) const
Chris@0 311 {
Chris@0 312 if (!m_model || !m_model->isOK()) return;
Chris@0 313
Chris@0 314 int sampleRate = m_model->getSampleRate();
Chris@0 315 if (!sampleRate) return;
Chris@0 316
Chris@0 317 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 318
Chris@0 319 int x0 = rect.left(), x1 = rect.right();
Chris@20 320 long frame0 = getFrameForX(x0);
Chris@20 321 long frame1 = getFrameForX(x1);
Chris@0 322
Chris@0 323 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 324 (frame0, frame1));
Chris@11 325 if (points.empty()) return;
Chris@0 326
Chris@0 327 paint.setPen(m_colour);
Chris@0 328
Chris@0 329 QColor brushColour(m_colour);
Chris@0 330 brushColour.setAlpha(80);
Chris@0 331 paint.setBrush(brushColour);
Chris@0 332
Chris@0 333 // std::cerr << "TimeValueLayer::paint: resolution is "
Chris@0 334 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 335
Chris@0 336 float min = m_model->getValueMinimum();
Chris@0 337 float max = m_model->getValueMaximum();
Chris@0 338 if (max == min) max = min + 1.0;
Chris@0 339
Chris@0 340 int origin = int(nearbyint(m_view->height() -
Chris@0 341 (-min * m_view->height()) / (max - min)));
Chris@0 342
Chris@0 343 QPoint localPos;
Chris@0 344 long illuminateFrame = -1;
Chris@0 345
Chris@0 346 if (m_view->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 347 SparseTimeValueModel::PointList localPoints =
Chris@0 348 getLocalPoints(localPos.x());
Chris@0 349 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 350 }
Chris@6 351
Chris@20 352 int w =
Chris@20 353 getXForFrame(frame0 + m_model->getResolution()) -
Chris@20 354 getXForFrame(frame0);
Chris@7 355
Chris@6 356 paint.save();
Chris@6 357
Chris@7 358 if (w > 1 &&
Chris@7 359 (m_plotStyle == PlotLines ||
Chris@7 360 m_plotStyle == PlotCurve)) {
Chris@6 361 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@6 362 }
Chris@6 363 QPainterPath path;
Chris@6 364
Chris@0 365 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 366 i != points.end(); ++i) {
Chris@0 367
Chris@0 368 const SparseTimeValueModel::Point &p(*i);
Chris@0 369
Chris@20 370 int x = getXForFrame(p.frame);
Chris@21 371 int y = getYForValue(p.value);
Chris@0 372
Chris@0 373 if (w < 1) w = 1;
Chris@0 374
Chris@6 375 if (m_plotStyle == PlotLines ||
Chris@6 376 m_plotStyle == PlotCurve) {
Chris@6 377 paint.setPen(m_colour);
Chris@6 378 paint.setBrush(Qt::NoBrush);
Chris@3 379 } else {
Chris@3 380 paint.setPen(m_colour);
Chris@6 381 paint.setBrush(brushColour);
Chris@3 382 }
Chris@0 383
Chris@0 384 if (m_plotStyle == PlotStems) {
Chris@0 385 paint.setPen(brushColour);
Chris@0 386 if (y < origin - 1) {
Chris@0 387 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 388 } else if (y > origin + 1) {
Chris@0 389 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 390 }
Chris@0 391 paint.setPen(m_colour);
Chris@0 392 }
Chris@0 393
Chris@0 394 if (illuminateFrame == p.frame) {
Chris@6 395
Chris@0 396 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 397 //better to save the highlighted rects and draw them at
Chris@0 398 //the end perhaps
Chris@6 399
Chris@6 400 //!!! not equipped to illuminate the right section in line
Chris@6 401 //or curve mode
Chris@6 402
Chris@6 403 if (m_plotStyle != PlotCurve &&
Chris@6 404 m_plotStyle != PlotLines) {
Chris@6 405 paint.setPen(Qt::black);//!!!
Chris@6 406 paint.setBrush(Qt::black);//!!!
Chris@6 407 }
Chris@0 408 }
Chris@0 409
Chris@6 410 if (m_plotStyle != PlotLines &&
Chris@6 411 m_plotStyle != PlotCurve) {
Chris@3 412 paint.drawRect(x, y - 1, w, 2);
Chris@3 413 }
Chris@0 414
Chris@6 415 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 416 m_plotStyle == PlotLines ||
Chris@6 417 m_plotStyle == PlotCurve) {
Chris@0 418
Chris@0 419 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@0 420 ++j;
Chris@3 421
Chris@0 422 if (j != points.end()) {
Chris@3 423
Chris@0 424 const SparseTimeValueModel::Point &q(*j);
Chris@20 425 int nx = getXForFrame(q.frame);
Chris@21 426 int ny = getYForValue(q.value);
Chris@3 427
Chris@6 428 if (m_plotStyle == PlotConnectedPoints) {
Chris@6 429
Chris@3 430 paint.setPen(brushColour);
Chris@3 431 paint.drawLine(x + w, y, nx, ny);
Chris@6 432
Chris@6 433 } else if (m_plotStyle == PlotLines) {
Chris@6 434
Chris@6 435 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 436
Chris@3 437 } else {
Chris@6 438
Chris@6 439 if (path.isEmpty()) {
Chris@6 440 path.moveTo(x + w/2, y);
Chris@6 441 }
Chris@6 442
Chris@6 443 if (nx - x > 5) {
Chris@6 444 path.cubicTo(x + w, y, nx, ny, nx + w/2, ny);
Chris@6 445 } else {
Chris@6 446 path.lineTo(nx + w/2, ny);
Chris@6 447 }
Chris@3 448 }
Chris@0 449 }
Chris@0 450 }
Chris@0 451
Chris@0 452 /// if (p.label != "") {
Chris@0 453 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@0 454 /// }
Chris@0 455 }
Chris@6 456
Chris@6 457 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@6 458 paint.drawPath(path);
Chris@6 459 }
Chris@6 460
Chris@6 461 paint.restore();
Chris@6 462
Chris@6 463 // looks like save/restore doesn't deal with this:
Chris@6 464 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 465 }
Chris@6 466
Chris@21 467 void
Chris@21 468 TimeValueLayer::drawStart(QMouseEvent *e)
Chris@21 469 {
Chris@21 470 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 471
Chris@21 472 if (!m_model) return;
Chris@21 473
Chris@21 474 long frame = getFrameForX(e->x());
Chris@21 475 if (frame < 0) frame = 0;
Chris@21 476 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 477
Chris@21 478 float value = getValueForY(e->y());
Chris@21 479
Chris@21 480 m_editingPoint = SparseTimeValueModel::Point(frame, value, tr("New Point"));
Chris@23 481 m_originalPoint = m_editingPoint;
Chris@22 482
Chris@22 483 if (m_editingCommand) m_editingCommand->finish();
Chris@22 484 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 485 tr("Draw Point"));
Chris@22 486 m_editingCommand->addPoint(m_editingPoint);
Chris@22 487
Chris@21 488 m_editing = true;
Chris@21 489 }
Chris@21 490
Chris@21 491 void
Chris@21 492 TimeValueLayer::drawDrag(QMouseEvent *e)
Chris@21 493 {
Chris@21 494 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 495
Chris@21 496 if (!m_model || !m_editing) return;
Chris@21 497
Chris@21 498 long frame = getFrameForX(e->x());
Chris@21 499 if (frame < 0) frame = 0;
Chris@21 500 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 501
Chris@21 502 float value = getValueForY(e->y());
Chris@21 503
Chris@22 504 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 505 m_editingPoint.frame = frame;
Chris@21 506 m_editingPoint.value = value;
Chris@22 507 m_editingCommand->addPoint(m_editingPoint);
Chris@21 508 }
Chris@21 509
Chris@21 510 void
Chris@21 511 TimeValueLayer::drawEnd(QMouseEvent *e)
Chris@21 512 {
Chris@21 513 std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 514 if (!m_model || !m_editing) return;
Chris@22 515 m_editingCommand->finish();
Chris@22 516 m_editingCommand = 0;
Chris@21 517 m_editing = false;
Chris@21 518 }
Chris@21 519
Chris@21 520 void
Chris@21 521 TimeValueLayer::editStart(QMouseEvent *e)
Chris@21 522 {
Chris@21 523 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 524
Chris@21 525 if (!m_model) return;
Chris@21 526
Chris@21 527 SparseTimeValueModel::PointList points = getLocalPoints(e->x());
Chris@21 528 if (points.empty()) return;
Chris@21 529
Chris@21 530 m_editingPoint = *points.begin();
Chris@23 531 m_originalPoint = m_editingPoint;
Chris@22 532
Chris@22 533 if (m_editingCommand) {
Chris@22 534 m_editingCommand->finish();
Chris@22 535 m_editingCommand = 0;
Chris@22 536 }
Chris@22 537
Chris@21 538 m_editing = true;
Chris@21 539 }
Chris@21 540
Chris@21 541 void
Chris@21 542 TimeValueLayer::editDrag(QMouseEvent *e)
Chris@21 543 {
Chris@21 544 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 545
Chris@21 546 if (!m_model || !m_editing) return;
Chris@21 547
Chris@21 548 long frame = getFrameForX(e->x());
Chris@21 549 if (frame < 0) frame = 0;
Chris@21 550 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 551
Chris@21 552 float value = getValueForY(e->y());
Chris@21 553
Chris@22 554 if (!m_editingCommand) {
Chris@22 555 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 556 tr("Drag Point"));
Chris@22 557 }
Chris@22 558
Chris@22 559 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 560 m_editingPoint.frame = frame;
Chris@21 561 m_editingPoint.value = value;
Chris@22 562 m_editingCommand->addPoint(m_editingPoint);
Chris@21 563 }
Chris@21 564
Chris@21 565 void
Chris@21 566 TimeValueLayer::editEnd(QMouseEvent *e)
Chris@21 567 {
Chris@21 568 std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 569 if (!m_model || !m_editing) return;
Chris@23 570
Chris@23 571 if (m_editingCommand) {
Chris@23 572
Chris@23 573 QString newName = m_editingCommand->getName();
Chris@23 574
Chris@23 575 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 576 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 577 newName = tr("Edit Point");
Chris@23 578 } else {
Chris@23 579 newName = tr("Relocate Point");
Chris@23 580 }
Chris@23 581 } else {
Chris@23 582 newName = tr("Change Point Value");
Chris@23 583 }
Chris@23 584
Chris@23 585 m_editingCommand->setName(newName);
Chris@23 586 m_editingCommand->finish();
Chris@23 587 }
Chris@23 588
Chris@22 589 m_editingCommand = 0;
Chris@21 590 m_editing = false;
Chris@21 591 }
Chris@21 592
Chris@6 593 QString
Chris@6 594 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 595 {
Chris@6 596 return Layer::toXmlString(indent, extraAttributes +
Chris@6 597 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@6 598 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@0 599 }
Chris@0 600
Chris@11 601 void
Chris@11 602 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 603 {
Chris@11 604 QString colourSpec = attributes.value("colour");
Chris@11 605 if (colourSpec != "") {
Chris@11 606 QColor colour(colourSpec);
Chris@11 607 if (colour.isValid()) {
Chris@11 608 setBaseColour(QColor(colourSpec));
Chris@11 609 }
Chris@11 610 }
Chris@11 611
Chris@11 612 bool ok;
Chris@11 613 PlotStyle style = (PlotStyle)
Chris@11 614 attributes.value("plotStyle").toInt(&ok);
Chris@11 615 if (ok) setPlotStyle(style);
Chris@11 616 }
Chris@11 617
Chris@0 618
Chris@0 619 #ifdef INCLUDE_MOCFILES
Chris@0 620 #include "TimeValueLayer.moc.cpp"
Chris@0 621 #endif
Chris@0 622