annotate layer/TimeInstantLayer.cpp @ 74:195ad6178ef8

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