annotate layer/TimeInstantLayer.cpp @ 46:2152e19d7db9

had to include <cmath> in some files to make it compile on the mac
author Martin Gasser
date Fri, 03 Mar 2006 17:23:27 +0000
parents ad214997dddb
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@5 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 "TimeInstantLayer.h"
Chris@0 11
Chris@0 12 #include "base/Model.h"
Chris@0 13 #include "base/RealTime.h"
Chris@0 14 #include "base/View.h"
Chris@0 15 #include "base/Profiler.h"
Chris@0 16
Chris@0 17 #include "model/SparseOneDimensionalModel.h"
Chris@0 18
Chris@0 19 #include <QPainter>
Chris@17 20 #include <QMouseEvent>
Chris@0 21
Chris@0 22 #include <iostream>
Martin@46 23 #include <cmath>
Chris@0 24
Chris@44 25 TimeInstantLayer::TimeInstantLayer() :
Chris@44 26 Layer(),
Chris@0 27 m_model(0),
Chris@18 28 m_editing(false),
Chris@17 29 m_editingPoint(0, tr("New Point")),
Chris@22 30 m_editingCommand(0),
Chris@28 31 m_colour(QColor(200, 50, 255)),
Chris@28 32 m_plotStyle(PlotInstants)
Chris@0 33 {
Chris@44 34
Chris@0 35 }
Chris@0 36
Chris@0 37 void
Chris@0 38 TimeInstantLayer::setModel(SparseOneDimensionalModel *model)
Chris@0 39 {
Chris@0 40 if (m_model == model) return;
Chris@0 41 m_model = model;
Chris@0 42
Chris@0 43 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 44 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 45 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 46
Chris@0 47 connect(m_model, SIGNAL(completionChanged()),
Chris@0 48 this, SIGNAL(modelCompletionChanged()));
Chris@0 49
Chris@0 50 std::cerr << "TimeInstantLayer::setModel(" << model << ")" << std::endl;
Chris@0 51
Chris@0 52 emit modelReplaced();
Chris@0 53 }
Chris@0 54
Chris@0 55 Layer::PropertyList
Chris@0 56 TimeInstantLayer::getProperties() const
Chris@0 57 {
Chris@0 58 PropertyList list;
Chris@0 59 list.push_back(tr("Colour"));
Chris@28 60 list.push_back(tr("Plot Type"));
Chris@0 61 return list;
Chris@0 62 }
Chris@0 63
Chris@0 64 Layer::PropertyType
Chris@20 65 TimeInstantLayer::getPropertyType(const PropertyName &) const
Chris@0 66 {
Chris@0 67 return ValueProperty;
Chris@0 68 }
Chris@0 69
Chris@0 70 int
Chris@0 71 TimeInstantLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@0 72 int *min, int *max) const
Chris@0 73 {
Chris@0 74 int deft = 0;
Chris@0 75
Chris@0 76 if (name == tr("Colour")) {
Chris@0 77
Chris@10 78 if (min) *min = 0;
Chris@10 79 if (max) *max = 5;
Chris@0 80
Chris@0 81 if (m_colour == Qt::black) deft = 0;
Chris@0 82 else if (m_colour == Qt::darkRed) deft = 1;
Chris@0 83 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@0 84 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@0 85 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@0 86 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@0 87
Chris@28 88 } else if (name == tr("Plot Type")) {
Chris@28 89
Chris@28 90 if (min) *min = 0;
Chris@28 91 if (max) *max = 1;
Chris@28 92
Chris@28 93 deft = int(m_plotStyle);
Chris@28 94
Chris@0 95 } else {
Chris@0 96
Chris@0 97 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 98 }
Chris@0 99
Chris@0 100 return deft;
Chris@0 101 }
Chris@0 102
Chris@0 103 QString
Chris@0 104 TimeInstantLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 105 int value) const
Chris@0 106 {
Chris@0 107 if (name == tr("Colour")) {
Chris@0 108 switch (value) {
Chris@0 109 default:
Chris@0 110 case 0: return tr("Black");
Chris@0 111 case 1: return tr("Red");
Chris@0 112 case 2: return tr("Blue");
Chris@0 113 case 3: return tr("Green");
Chris@0 114 case 4: return tr("Purple");
Chris@0 115 case 5: return tr("Orange");
Chris@0 116 }
Chris@28 117 } else if (name == tr("Plot Type")) {
Chris@28 118 switch (value) {
Chris@28 119 default:
Chris@28 120 case 0: return tr("Instants");
Chris@28 121 case 1: return tr("Segmentation");
Chris@28 122 }
Chris@0 123 }
Chris@0 124 return tr("<unknown>");
Chris@0 125 }
Chris@0 126
Chris@0 127 void
Chris@0 128 TimeInstantLayer::setProperty(const PropertyName &name, int value)
Chris@0 129 {
Chris@0 130 if (name == tr("Colour")) {
Chris@0 131 switch (value) {
Chris@0 132 default:
Chris@0 133 case 0: setBaseColour(Qt::black); break;
Chris@0 134 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 135 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 136 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 137 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 138 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 139 }
Chris@28 140 } else if (name == tr("Plot Type")) {
Chris@28 141 setPlotStyle(PlotStyle(value));
Chris@0 142 }
Chris@0 143 }
Chris@0 144
Chris@0 145 void
Chris@0 146 TimeInstantLayer::setBaseColour(QColor colour)
Chris@0 147 {
Chris@0 148 if (m_colour == colour) return;
Chris@0 149 m_colour = colour;
Chris@0 150 emit layerParametersChanged();
Chris@0 151 }
Chris@0 152
Chris@28 153 void
Chris@28 154 TimeInstantLayer::setPlotStyle(PlotStyle style)
Chris@28 155 {
Chris@28 156 if (m_plotStyle == style) return;
Chris@28 157 m_plotStyle = style;
Chris@28 158 emit layerParametersChanged();
Chris@28 159 }
Chris@28 160
Chris@0 161 bool
Chris@44 162 TimeInstantLayer::isLayerScrollable(const View *v) const
Chris@0 163 {
Chris@0 164 QPoint discard;
Chris@44 165 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@0 166 }
Chris@0 167
Chris@0 168 SparseOneDimensionalModel::PointList
Chris@44 169 TimeInstantLayer::getLocalPoints(View *v, int x) const
Chris@0 170 {
Chris@28 171 // Return a set of points that all have the same frame number, the
Chris@28 172 // nearest to the given x coordinate, and that are within a
Chris@28 173 // certain fuzz distance of that x coordinate.
Chris@28 174
Chris@0 175 if (!m_model) return SparseOneDimensionalModel::PointList();
Chris@0 176
Chris@44 177 long frame = v->getFrameForX(x);
Chris@0 178
Chris@0 179 SparseOneDimensionalModel::PointList onPoints =
Chris@0 180 m_model->getPoints(frame);
Chris@0 181
Chris@0 182 if (!onPoints.empty()) {
Chris@0 183 return onPoints;
Chris@0 184 }
Chris@0 185
Chris@0 186 SparseOneDimensionalModel::PointList prevPoints =
Chris@0 187 m_model->getPreviousPoints(frame);
Chris@0 188 SparseOneDimensionalModel::PointList nextPoints =
Chris@0 189 m_model->getNextPoints(frame);
Chris@0 190
Chris@0 191 SparseOneDimensionalModel::PointList usePoints = prevPoints;
Chris@0 192
Chris@0 193 if (prevPoints.empty()) {
Chris@0 194 usePoints = nextPoints;
Chris@44 195 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
Chris@44 196 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@0 197 usePoints = nextPoints;
Chris@0 198 } else if (nextPoints.begin()->frame - frame <
Chris@0 199 frame - prevPoints.begin()->frame) {
Chris@0 200 usePoints = nextPoints;
Chris@0 201 }
Chris@0 202
Chris@28 203 if (!usePoints.empty()) {
Chris@28 204 int fuzz = 2;
Chris@44 205 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@28 206 if ((px > x && px - x > fuzz) ||
Chris@28 207 (px < x && x - px > fuzz + 1)) {
Chris@28 208 usePoints.clear();
Chris@28 209 }
Chris@28 210 }
Chris@28 211
Chris@0 212 return usePoints;
Chris@0 213 }
Chris@0 214
Chris@25 215 QString
Chris@44 216 TimeInstantLayer::getFeatureDescription(View *v, 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@44 222 SparseOneDimensionalModel::PointList points = getLocalPoints(v, 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\nNo label"))
Chris@25 240 .arg(rt.toText(true).c_str());
Chris@25 241 } else {
Chris@25 242 text = QString(tr("Time:\t%1\nLabel:\t%2"))
Chris@25 243 .arg(rt.toText(true).c_str())
Chris@25 244 .arg(points.begin()->label);
Chris@25 245 }
Chris@0 246
Chris@44 247 pos = QPoint(v->getXForFrame(useFrame), pos.y());
Chris@25 248 return text;
Chris@0 249 }
Chris@0 250
Chris@28 251 bool
Chris@44 252 TimeInstantLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 253 size_t &resolution,
Chris@28 254 SnapType snap) const
Chris@13 255 {
Chris@13 256 if (!m_model) {
Chris@44 257 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@13 258 }
Chris@13 259
Chris@13 260 resolution = m_model->getResolution();
Chris@28 261 SparseOneDimensionalModel::PointList points;
Chris@13 262
Chris@28 263 if (snap == SnapNeighbouring) {
Chris@28 264
Chris@44 265 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@28 266 if (points.empty()) return false;
Chris@28 267 frame = points.begin()->frame;
Chris@28 268 return true;
Chris@28 269 }
Chris@28 270
Chris@28 271 points = m_model->getPoints(frame, frame);
Chris@28 272 int snapped = frame;
Chris@28 273 bool found = false;
Chris@13 274
Chris@13 275 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@13 276 i != points.end(); ++i) {
Chris@13 277
Chris@28 278 if (snap == SnapRight) {
Chris@28 279
Chris@28 280 if (i->frame >= frame) {
Chris@28 281 snapped = i->frame;
Chris@28 282 found = true;
Chris@13 283 break;
Chris@13 284 }
Chris@28 285
Chris@28 286 } else if (snap == SnapLeft) {
Chris@28 287
Chris@13 288 if (i->frame <= frame) {
Chris@28 289 snapped = i->frame;
Chris@28 290 found = true; // don't break, as the next may be better
Chris@28 291 } else {
Chris@28 292 break;
Chris@28 293 }
Chris@28 294
Chris@28 295 } else { // nearest
Chris@28 296
Chris@28 297 SparseOneDimensionalModel::PointList::const_iterator j = i;
Chris@28 298 ++j;
Chris@28 299
Chris@28 300 if (j == points.end()) {
Chris@28 301
Chris@28 302 snapped = i->frame;
Chris@28 303 found = true;
Chris@28 304 break;
Chris@28 305
Chris@28 306 } else if (j->frame >= frame) {
Chris@28 307
Chris@28 308 if (j->frame - frame < frame - i->frame) {
Chris@28 309 snapped = j->frame;
Chris@28 310 } else {
Chris@28 311 snapped = i->frame;
Chris@28 312 }
Chris@28 313 found = true;
Chris@28 314 break;
Chris@13 315 }
Chris@13 316 }
Chris@13 317 }
Chris@13 318
Chris@28 319 frame = snapped;
Chris@28 320 return found;
Chris@13 321 }
Chris@13 322
Chris@0 323 void
Chris@44 324 TimeInstantLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 325 {
Chris@0 326 if (!m_model || !m_model->isOK()) return;
Chris@0 327
Chris@0 328 // Profiler profiler("TimeInstantLayer::paint", true);
Chris@0 329
Chris@20 330 int x0 = rect.left(), x1 = rect.right();
Chris@0 331
Chris@44 332 long frame0 = v->getFrameForX(x0);
Chris@44 333 long frame1 = v->getFrameForX(x1);
Chris@0 334
Chris@0 335 SparseOneDimensionalModel::PointList points(m_model->getPoints
Chris@0 336 (frame0, frame1));
Chris@0 337
Chris@28 338 bool odd = false;
Chris@28 339 if (m_plotStyle == PlotSegmentation && !points.empty()) {
Chris@28 340 int index = m_model->getIndexOf(*points.begin());
Chris@28 341 odd = ((index % 2) == 1);
Chris@28 342 }
Chris@28 343
Chris@0 344 paint.setPen(m_colour);
Chris@0 345
Chris@0 346 QColor brushColour(m_colour);
Chris@0 347 brushColour.setAlpha(100);
Chris@0 348 paint.setBrush(brushColour);
Chris@0 349
Chris@28 350 QColor oddBrushColour(brushColour);
Chris@28 351 if (m_plotStyle == PlotSegmentation) {
Chris@28 352 if (m_colour == Qt::black) {
Chris@28 353 oddBrushColour = Qt::gray;
Chris@28 354 } else if (m_colour == Qt::darkRed) {
Chris@28 355 oddBrushColour = Qt::red;
Chris@28 356 } else if (m_colour == Qt::darkBlue) {
Chris@28 357 oddBrushColour = Qt::blue;
Chris@28 358 } else if (m_colour == Qt::darkGreen) {
Chris@28 359 oddBrushColour = Qt::green;
Chris@28 360 } else {
Chris@28 361 oddBrushColour = oddBrushColour.light(150);
Chris@28 362 }
Chris@28 363 oddBrushColour.setAlpha(100);
Chris@28 364 }
Chris@28 365
Chris@0 366 // std::cerr << "TimeInstantLayer::paint: resolution is "
Chris@0 367 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 368
Chris@0 369 QPoint localPos;
Chris@0 370 long illuminateFrame = -1;
Chris@0 371
Chris@44 372 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 373 SparseOneDimensionalModel::PointList localPoints =
Chris@44 374 getLocalPoints(v, localPos.x());
Chris@0 375 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 376 }
Chris@0 377
Chris@23 378 int prevX = -1;
Chris@23 379
Chris@0 380 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@0 381 i != points.end(); ++i) {
Chris@0 382
Chris@0 383 const SparseOneDimensionalModel::Point &p(*i);
Chris@17 384 SparseOneDimensionalModel::PointList::const_iterator j = i;
Chris@17 385 ++j;
Chris@0 386
Chris@44 387 int x = v->getXForFrame(p.frame);
Chris@23 388 if (x == prevX && p.frame != illuminateFrame) continue;
Chris@23 389
Chris@44 390 int iw = v->getXForFrame(p.frame + m_model->getResolution()) - x;
Chris@16 391 if (iw < 2) {
Chris@17 392 if (iw < 1) {
Chris@17 393 iw = 2;
Chris@17 394 if (j != points.end()) {
Chris@44 395 int nx = v->getXForFrame(j->frame);
Chris@17 396 if (nx < x + 3) iw = 1;
Chris@17 397 }
Chris@17 398 } else {
Chris@17 399 iw = 2;
Chris@17 400 }
Chris@16 401 }
Chris@20 402
Chris@0 403 if (p.frame == illuminateFrame) {
Chris@0 404 paint.setPen(Qt::black); //!!!
Chris@0 405 } else {
Chris@0 406 paint.setPen(brushColour);
Chris@0 407 }
Chris@23 408
Chris@28 409 if (m_plotStyle == PlotInstants) {
Chris@28 410 if (iw > 1) {
Chris@44 411 paint.drawRect(x, 0, iw - 1, v->height() - 1);
Chris@28 412 } else {
Chris@44 413 paint.drawLine(x, 0, x, v->height() - 1);
Chris@28 414 }
Chris@23 415 } else {
Chris@28 416
Chris@28 417 if (odd) paint.setBrush(oddBrushColour);
Chris@28 418 else paint.setBrush(brushColour);
Chris@28 419
Chris@28 420 int nx;
Chris@28 421
Chris@28 422 if (j != points.end()) {
Chris@28 423 const SparseOneDimensionalModel::Point &q(*j);
Chris@44 424 nx = v->getXForFrame(q.frame);
Chris@28 425 } else {
Chris@44 426 nx = v->getXForFrame(m_model->getEndFrame());
Chris@28 427 }
Chris@28 428
Chris@28 429 if (nx >= x) {
Chris@28 430
Chris@28 431 if (illuminateFrame != p.frame &&
Chris@44 432 (nx < x + 5 || x >= v->width() - 1)) {
Chris@28 433 paint.setPen(Qt::NoPen);
Chris@28 434 }
Chris@28 435
Chris@44 436 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@28 437 }
Chris@28 438
Chris@28 439 odd = !odd;
Chris@23 440 }
Chris@28 441
Chris@0 442 paint.setPen(m_colour);
Chris@0 443
Chris@0 444 if (p.label != "") {
Chris@0 445
Chris@0 446 // only draw if there's enough room from here to the next point
Chris@0 447
Chris@0 448 int lw = paint.fontMetrics().width(p.label);
Chris@0 449 bool good = true;
Chris@0 450
Chris@17 451 if (j != points.end()) {
Chris@44 452 int nx = v->getXForFrame(j->frame);
Chris@20 453 if (nx >= x && nx - x - iw - 3 <= lw) good = false;
Chris@0 454 }
Chris@0 455
Chris@0 456 if (good) {
Chris@20 457 paint.drawText(x + iw + 2,
Chris@44 458 v->height() - paint.fontMetrics().height(),
Chris@0 459 p.label);
Chris@0 460 }
Chris@0 461 }
Chris@23 462
Chris@23 463 prevX = x;
Chris@0 464 }
Chris@0 465 }
Chris@0 466
Chris@17 467 void
Chris@44 468 TimeInstantLayer::drawStart(View *v, QMouseEvent *e)
Chris@17 469 {
Chris@17 470 std::cerr << "TimeInstantLayer::drawStart(" << e->x() << ")" << std::endl;
Chris@17 471
Chris@17 472 if (!m_model) return;
Chris@17 473
Chris@44 474 long frame = v->getFrameForX(e->x());
Chris@17 475 if (frame < 0) frame = 0;
Chris@21 476 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 477
Chris@17 478 m_editingPoint = SparseOneDimensionalModel::Point(frame, tr("New Point"));
Chris@22 479
Chris@22 480 if (m_editingCommand) m_editingCommand->finish();
Chris@22 481 m_editingCommand = new SparseOneDimensionalModel::EditCommand(m_model,
Chris@22 482 tr("Draw Point"));
Chris@22 483 m_editingCommand->addPoint(m_editingPoint);
Chris@22 484
Chris@18 485 m_editing = true;
Chris@17 486 }
Chris@17 487
Chris@17 488 void
Chris@44 489 TimeInstantLayer::drawDrag(View *v, QMouseEvent *e)
Chris@17 490 {
Chris@17 491 std::cerr << "TimeInstantLayer::drawDrag(" << e->x() << ")" << std::endl;
Chris@17 492
Chris@18 493 if (!m_model || !m_editing) return;
Chris@17 494
Chris@44 495 long frame = v->getFrameForX(e->x());
Chris@17 496 if (frame < 0) frame = 0;
Chris@21 497 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 498 m_editingCommand->deletePoint(m_editingPoint);
Chris@17 499 m_editingPoint.frame = frame;
Chris@22 500 m_editingCommand->addPoint(m_editingPoint);
Chris@17 501 }
Chris@17 502
Chris@17 503 void
Chris@44 504 TimeInstantLayer::drawEnd(View *v, QMouseEvent *e)
Chris@17 505 {
Chris@17 506 std::cerr << "TimeInstantLayer::drawEnd(" << e->x() << ")" << std::endl;
Chris@18 507 if (!m_model || !m_editing) return;
Chris@23 508 QString newName = tr("Add Point at %1 s")
Chris@23 509 .arg(RealTime::frame2RealTime(m_editingPoint.frame,
Chris@23 510 m_model->getSampleRate())
Chris@23 511 .toText(false).c_str());
Chris@23 512 m_editingCommand->setName(newName);
Chris@22 513 m_editingCommand->finish();
Chris@22 514 m_editingCommand = 0;
Chris@18 515 m_editing = false;
Chris@18 516 }
Chris@18 517
Chris@18 518 void
Chris@44 519 TimeInstantLayer::editStart(View *v, QMouseEvent *e)
Chris@18 520 {
Chris@18 521 std::cerr << "TimeInstantLayer::editStart(" << e->x() << ")" << std::endl;
Chris@18 522
Chris@17 523 if (!m_model) return;
Chris@18 524
Chris@44 525 SparseOneDimensionalModel::PointList points = getLocalPoints(v, e->x());
Chris@18 526 if (points.empty()) return;
Chris@18 527
Chris@18 528 m_editingPoint = *points.begin();
Chris@22 529
Chris@22 530 if (m_editingCommand) {
Chris@22 531 m_editingCommand->finish();
Chris@22 532 m_editingCommand = 0;
Chris@22 533 }
Chris@22 534
Chris@18 535 m_editing = true;
Chris@18 536 }
Chris@18 537
Chris@18 538 void
Chris@44 539 TimeInstantLayer::editDrag(View *v, QMouseEvent *e)
Chris@18 540 {
Chris@18 541 std::cerr << "TimeInstantLayer::editDrag(" << e->x() << ")" << std::endl;
Chris@18 542
Chris@18 543 if (!m_model || !m_editing) return;
Chris@18 544
Chris@44 545 long frame = v->getFrameForX(e->x());
Chris@18 546 if (frame < 0) frame = 0;
Chris@21 547 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 548
Chris@22 549 if (!m_editingCommand) {
Chris@22 550 m_editingCommand = new SparseOneDimensionalModel::EditCommand(m_model,
Chris@22 551 tr("Drag Point"));
Chris@22 552 }
Chris@22 553
Chris@22 554 m_editingCommand->deletePoint(m_editingPoint);
Chris@18 555 m_editingPoint.frame = frame;
Chris@22 556 m_editingCommand->addPoint(m_editingPoint);
Chris@18 557 }
Chris@18 558
Chris@18 559 void
Chris@44 560 TimeInstantLayer::editEnd(View *v, QMouseEvent *e)
Chris@18 561 {
Chris@18 562 std::cerr << "TimeInstantLayer::editEnd(" << e->x() << ")" << std::endl;
Chris@18 563 if (!m_model || !m_editing) return;
Chris@23 564 if (m_editingCommand) {
Chris@23 565 QString newName = tr("Move Point to %1 s")
Chris@23 566 .arg(RealTime::frame2RealTime(m_editingPoint.frame,
Chris@23 567 m_model->getSampleRate())
Chris@23 568 .toText(false).c_str());
Chris@23 569 m_editingCommand->setName(newName);
Chris@23 570 m_editingCommand->finish();
Chris@23 571 }
Chris@22 572 m_editingCommand = 0;
Chris@18 573 m_editing = false;
Chris@17 574 }
Chris@17 575
Chris@43 576 void
Chris@43 577 TimeInstantLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 578 {
Chris@43 579 SparseOneDimensionalModel::EditCommand *command =
Chris@43 580 new SparseOneDimensionalModel::EditCommand(m_model,
Chris@43 581 tr("Drag Selection"));
Chris@43 582
Chris@43 583 SparseOneDimensionalModel::PointList points =
Chris@43 584 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 585
Chris@43 586 for (SparseOneDimensionalModel::PointList::iterator i = points.begin();
Chris@43 587 i != points.end(); ++i) {
Chris@43 588
Chris@43 589 if (s.contains(i->frame)) {
Chris@43 590 SparseOneDimensionalModel::Point newPoint(*i);
Chris@43 591 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 592 command->deletePoint(*i);
Chris@43 593 command->addPoint(newPoint);
Chris@43 594 }
Chris@43 595 }
Chris@43 596
Chris@43 597 command->finish();
Chris@43 598 }
Chris@43 599
Chris@43 600 void
Chris@43 601 TimeInstantLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 602 {
Chris@43 603 SparseOneDimensionalModel::EditCommand *command =
Chris@43 604 new SparseOneDimensionalModel::EditCommand(m_model,
Chris@43 605 tr("Resize Selection"));
Chris@43 606
Chris@43 607 SparseOneDimensionalModel::PointList points =
Chris@43 608 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 609
Chris@43 610 double ratio =
Chris@43 611 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 612 double(s.getEndFrame() - s.getStartFrame());
Chris@43 613
Chris@43 614 for (SparseOneDimensionalModel::PointList::iterator i = points.begin();
Chris@43 615 i != points.end(); ++i) {
Chris@43 616
Chris@43 617 if (s.contains(i->frame)) {
Chris@43 618
Chris@43 619 double target = i->frame;
Chris@43 620 target = newSize.getStartFrame() +
Chris@43 621 double(target - s.getStartFrame()) * ratio;
Chris@43 622
Chris@43 623 SparseOneDimensionalModel::Point newPoint(*i);
Chris@43 624 newPoint.frame = lrint(target);
Chris@43 625 command->deletePoint(*i);
Chris@43 626 command->addPoint(newPoint);
Chris@43 627 }
Chris@43 628 }
Chris@43 629
Chris@43 630 command->finish();
Chris@43 631 }
Chris@43 632
Chris@43 633 void
Chris@43 634 TimeInstantLayer::deleteSelection(Selection s)
Chris@43 635 {
Chris@43 636 SparseOneDimensionalModel::EditCommand *command =
Chris@43 637 new SparseOneDimensionalModel::EditCommand(m_model,
Chris@43 638 tr("Delete Selection"));
Chris@43 639
Chris@43 640 SparseOneDimensionalModel::PointList points =
Chris@43 641 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 642
Chris@43 643 for (SparseOneDimensionalModel::PointList::iterator i = points.begin();
Chris@43 644 i != points.end(); ++i) {
Chris@43 645 if (s.contains(i->frame)) command->deletePoint(*i);
Chris@43 646 }
Chris@43 647
Chris@43 648 command->finish();
Chris@43 649 }
Chris@43 650
Chris@43 651
Chris@6 652 QString
Chris@6 653 TimeInstantLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 654 {
Chris@6 655 return Layer::toXmlString(indent, extraAttributes +
Chris@28 656 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@28 657 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@6 658 }
Chris@0 659
Chris@11 660 void
Chris@11 661 TimeInstantLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 662 {
Chris@11 663 QString colourSpec = attributes.value("colour");
Chris@11 664 if (colourSpec != "") {
Chris@11 665 QColor colour(colourSpec);
Chris@11 666 if (colour.isValid()) {
Chris@11 667 setBaseColour(QColor(colourSpec));
Chris@11 668 }
Chris@11 669 }
Chris@28 670
Chris@28 671 bool ok;
Chris@28 672 PlotStyle style = (PlotStyle)
Chris@28 673 attributes.value("plotStyle").toInt(&ok);
Chris@28 674 if (ok) setPlotStyle(style);
Chris@11 675 }
Chris@11 676
Chris@0 677 #ifdef INCLUDE_MOCFILES
Chris@0 678 #include "TimeInstantLayer.moc.cpp"
Chris@0 679 #endif
Chris@0 680