annotate layer/TimeInstantLayer.cpp @ 59:705f05ab42e3

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