annotate layer/TimeInstantLayer.cpp @ 376:e1a9e478b7f2

* juggle some files around in order to free audioio, base, and system libraries from dependency on QtGui
author Chris Cannam
date Wed, 12 Mar 2008 17:42:56 +0000
parents d58701996fae
children f329416bf1a5
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@128 18 #include "data/model/Model.h"
Chris@0 19 #include "base/RealTime.h"
Chris@128 20 #include "view/View.h"
Chris@0 21 #include "base/Profiler.h"
Chris@76 22 #include "base/Clipboard.h"
Chris@376 23 #include "ColourDatabase.h"
Chris@0 24
Chris@128 25 #include "data/model/SparseOneDimensionalModel.h"
Chris@0 26
Chris@70 27 #include "widgets/ItemEditDialog.h"
Chris@358 28 #include "widgets/ListInputDialog.h"
Chris@70 29
Chris@0 30 #include <QPainter>
Chris@17 31 #include <QMouseEvent>
Chris@316 32 #include <QTextStream>
Chris@360 33 #include <QMessageBox>
Chris@0 34
Chris@0 35 #include <iostream>
Martin@46 36 #include <cmath>
Chris@0 37
Chris@44 38 TimeInstantLayer::TimeInstantLayer() :
Chris@287 39 SingleColourLayer(),
Chris@0 40 m_model(0),
Chris@18 41 m_editing(false),
Chris@17 42 m_editingPoint(0, tr("New Point")),
Chris@22 43 m_editingCommand(0),
Chris@28 44 m_plotStyle(PlotInstants)
Chris@0 45 {
Chris@308 46 }
Chris@308 47
Chris@308 48 TimeInstantLayer::~TimeInstantLayer()
Chris@308 49 {
Chris@0 50 }
Chris@0 51
Chris@0 52 void
Chris@0 53 TimeInstantLayer::setModel(SparseOneDimensionalModel *model)
Chris@0 54 {
Chris@0 55 if (m_model == model) return;
Chris@0 56 m_model = model;
Chris@0 57
Chris@320 58 connectSignals(m_model);
Chris@0 59
Chris@0 60 std::cerr << "TimeInstantLayer::setModel(" << model << ")" << std::endl;
Chris@0 61
Chris@0 62 emit modelReplaced();
Chris@0 63 }
Chris@0 64
Chris@0 65 Layer::PropertyList
Chris@0 66 TimeInstantLayer::getProperties() const
Chris@0 67 {
Chris@287 68 PropertyList list = SingleColourLayer::getProperties();
Chris@87 69 list.push_back("Plot Type");
Chris@0 70 return list;
Chris@0 71 }
Chris@0 72
Chris@87 73 QString
Chris@87 74 TimeInstantLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 75 {
Chris@87 76 if (name == "Plot Type") return tr("Plot Type");
Chris@287 77 return SingleColourLayer::getPropertyLabel(name);
Chris@87 78 }
Chris@87 79
Chris@0 80 Layer::PropertyType
Chris@287 81 TimeInstantLayer::getPropertyType(const PropertyName &name) const
Chris@0 82 {
Chris@287 83 if (name == "Plot Type") return ValueProperty;
Chris@287 84 return SingleColourLayer::getPropertyType(name);
Chris@0 85 }
Chris@0 86
Chris@0 87 int
Chris@0 88 TimeInstantLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 89 int *min, int *max, int *deflt) const
Chris@0 90 {
Chris@216 91 int val = 0;
Chris@0 92
Chris@287 93 if (name == "Plot Type") {
Chris@28 94
Chris@28 95 if (min) *min = 0;
Chris@28 96 if (max) *max = 1;
Chris@216 97 if (deflt) *deflt = 0;
Chris@28 98
Chris@216 99 val = int(m_plotStyle);
Chris@28 100
Chris@0 101 } else {
Chris@0 102
Chris@287 103 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 104 }
Chris@0 105
Chris@216 106 return val;
Chris@0 107 }
Chris@0 108
Chris@0 109 QString
Chris@0 110 TimeInstantLayer::getPropertyValueLabel(const PropertyName &name,
Chris@287 111 int value) const
Chris@0 112 {
Chris@287 113 if (name == "Plot Type") {
Chris@28 114 switch (value) {
Chris@28 115 default:
Chris@28 116 case 0: return tr("Instants");
Chris@28 117 case 1: return tr("Segmentation");
Chris@28 118 }
Chris@0 119 }
Chris@287 120 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@0 121 }
Chris@0 122
Chris@0 123 void
Chris@0 124 TimeInstantLayer::setProperty(const PropertyName &name, int value)
Chris@0 125 {
Chris@287 126 if (name == "Plot Type") {
Chris@28 127 setPlotStyle(PlotStyle(value));
Chris@287 128 } else {
Chris@287 129 SingleColourLayer::setProperty(name, value);
Chris@0 130 }
Chris@0 131 }
Chris@0 132
Chris@0 133 void
Chris@28 134 TimeInstantLayer::setPlotStyle(PlotStyle style)
Chris@28 135 {
Chris@28 136 if (m_plotStyle == style) return;
Chris@28 137 m_plotStyle = style;
Chris@28 138 emit layerParametersChanged();
Chris@28 139 }
Chris@28 140
Chris@0 141 bool
Chris@44 142 TimeInstantLayer::isLayerScrollable(const View *v) const
Chris@0 143 {
Chris@0 144 QPoint discard;
Chris@44 145 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@0 146 }
Chris@0 147
Chris@0 148 SparseOneDimensionalModel::PointList
Chris@44 149 TimeInstantLayer::getLocalPoints(View *v, int x) const
Chris@0 150 {
Chris@28 151 // Return a set of points that all have the same frame number, the
Chris@28 152 // nearest to the given x coordinate, and that are within a
Chris@28 153 // certain fuzz distance of that x coordinate.
Chris@28 154
Chris@0 155 if (!m_model) return SparseOneDimensionalModel::PointList();
Chris@0 156
Chris@44 157 long frame = v->getFrameForX(x);
Chris@0 158
Chris@0 159 SparseOneDimensionalModel::PointList onPoints =
Chris@0 160 m_model->getPoints(frame);
Chris@0 161
Chris@0 162 if (!onPoints.empty()) {
Chris@0 163 return onPoints;
Chris@0 164 }
Chris@0 165
Chris@0 166 SparseOneDimensionalModel::PointList prevPoints =
Chris@0 167 m_model->getPreviousPoints(frame);
Chris@0 168 SparseOneDimensionalModel::PointList nextPoints =
Chris@0 169 m_model->getNextPoints(frame);
Chris@0 170
Chris@0 171 SparseOneDimensionalModel::PointList usePoints = prevPoints;
Chris@0 172
Chris@0 173 if (prevPoints.empty()) {
Chris@0 174 usePoints = nextPoints;
Chris@248 175 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
Chris@44 176 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@0 177 usePoints = nextPoints;
Chris@0 178 } else if (nextPoints.begin()->frame - frame <
Chris@0 179 frame - prevPoints.begin()->frame) {
Chris@0 180 usePoints = nextPoints;
Chris@0 181 }
Chris@0 182
Chris@28 183 if (!usePoints.empty()) {
Chris@28 184 int fuzz = 2;
Chris@44 185 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@28 186 if ((px > x && px - x > fuzz) ||
Chris@28 187 (px < x && x - px > fuzz + 1)) {
Chris@28 188 usePoints.clear();
Chris@28 189 }
Chris@28 190 }
Chris@28 191
Chris@0 192 return usePoints;
Chris@0 193 }
Chris@0 194
Chris@25 195 QString
Chris@44 196 TimeInstantLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@0 197 {
Chris@25 198 int x = pos.x();
Chris@0 199
Chris@25 200 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 201
Chris@44 202 SparseOneDimensionalModel::PointList points = getLocalPoints(v, x);
Chris@0 203
Chris@0 204 if (points.empty()) {
Chris@0 205 if (!m_model->isReady()) {
Chris@25 206 return tr("In progress");
Chris@25 207 } else {
Chris@25 208 return tr("No local points");
Chris@0 209 }
Chris@0 210 }
Chris@0 211
Chris@0 212 long useFrame = points.begin()->frame;
Chris@0 213
Chris@0 214 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 215
Chris@25 216 QString text;
Chris@0 217
Chris@25 218 if (points.begin()->label == "") {
Chris@25 219 text = QString(tr("Time:\t%1\nNo label"))
Chris@25 220 .arg(rt.toText(true).c_str());
Chris@25 221 } else {
Chris@25 222 text = QString(tr("Time:\t%1\nLabel:\t%2"))
Chris@25 223 .arg(rt.toText(true).c_str())
Chris@25 224 .arg(points.begin()->label);
Chris@25 225 }
Chris@0 226
Chris@44 227 pos = QPoint(v->getXForFrame(useFrame), pos.y());
Chris@25 228 return text;
Chris@0 229 }
Chris@0 230
Chris@28 231 bool
Chris@44 232 TimeInstantLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 233 size_t &resolution,
Chris@28 234 SnapType snap) const
Chris@13 235 {
Chris@13 236 if (!m_model) {
Chris@44 237 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@13 238 }
Chris@13 239
Chris@13 240 resolution = m_model->getResolution();
Chris@28 241 SparseOneDimensionalModel::PointList points;
Chris@13 242
Chris@28 243 if (snap == SnapNeighbouring) {
Chris@28 244
Chris@44 245 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@28 246 if (points.empty()) return false;
Chris@28 247 frame = points.begin()->frame;
Chris@28 248 return true;
Chris@28 249 }
Chris@28 250
Chris@28 251 points = m_model->getPoints(frame, frame);
Chris@28 252 int snapped = frame;
Chris@28 253 bool found = false;
Chris@13 254
Chris@13 255 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@13 256 i != points.end(); ++i) {
Chris@13 257
Chris@28 258 if (snap == SnapRight) {
Chris@28 259
Chris@28 260 if (i->frame >= frame) {
Chris@28 261 snapped = i->frame;
Chris@28 262 found = true;
Chris@13 263 break;
Chris@13 264 }
Chris@28 265
Chris@28 266 } else if (snap == SnapLeft) {
Chris@28 267
Chris@13 268 if (i->frame <= frame) {
Chris@28 269 snapped = i->frame;
Chris@28 270 found = true; // don't break, as the next may be better
Chris@28 271 } else {
Chris@28 272 break;
Chris@28 273 }
Chris@28 274
Chris@28 275 } else { // nearest
Chris@28 276
Chris@28 277 SparseOneDimensionalModel::PointList::const_iterator j = i;
Chris@28 278 ++j;
Chris@28 279
Chris@28 280 if (j == points.end()) {
Chris@28 281
Chris@28 282 snapped = i->frame;
Chris@28 283 found = true;
Chris@28 284 break;
Chris@28 285
Chris@28 286 } else if (j->frame >= frame) {
Chris@28 287
Chris@28 288 if (j->frame - frame < frame - i->frame) {
Chris@28 289 snapped = j->frame;
Chris@28 290 } else {
Chris@28 291 snapped = i->frame;
Chris@28 292 }
Chris@28 293 found = true;
Chris@28 294 break;
Chris@13 295 }
Chris@13 296 }
Chris@13 297 }
Chris@13 298
Chris@28 299 frame = snapped;
Chris@28 300 return found;
Chris@13 301 }
Chris@13 302
Chris@0 303 void
Chris@44 304 TimeInstantLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 305 {
Chris@0 306 if (!m_model || !m_model->isOK()) return;
Chris@0 307
Chris@0 308 // Profiler profiler("TimeInstantLayer::paint", true);
Chris@0 309
Chris@20 310 int x0 = rect.left(), x1 = rect.right();
Chris@0 311
Chris@44 312 long frame0 = v->getFrameForX(x0);
Chris@44 313 long frame1 = v->getFrameForX(x1);
Chris@0 314
Chris@0 315 SparseOneDimensionalModel::PointList points(m_model->getPoints
Chris@0 316 (frame0, frame1));
Chris@0 317
Chris@28 318 bool odd = false;
Chris@28 319 if (m_plotStyle == PlotSegmentation && !points.empty()) {
Chris@28 320 int index = m_model->getIndexOf(*points.begin());
Chris@28 321 odd = ((index % 2) == 1);
Chris@28 322 }
Chris@28 323
Chris@287 324 paint.setPen(getBaseQColor());
Chris@0 325
Chris@287 326 QColor brushColour(getBaseQColor());
Chris@0 327 brushColour.setAlpha(100);
Chris@0 328 paint.setBrush(brushColour);
Chris@0 329
Chris@28 330 QColor oddBrushColour(brushColour);
Chris@28 331 if (m_plotStyle == PlotSegmentation) {
Chris@287 332 if (getBaseQColor() == Qt::black) {
Chris@28 333 oddBrushColour = Qt::gray;
Chris@287 334 } else if (getBaseQColor() == Qt::darkRed) {
Chris@28 335 oddBrushColour = Qt::red;
Chris@287 336 } else if (getBaseQColor() == Qt::darkBlue) {
Chris@28 337 oddBrushColour = Qt::blue;
Chris@287 338 } else if (getBaseQColor() == Qt::darkGreen) {
Chris@28 339 oddBrushColour = Qt::green;
Chris@28 340 } else {
Chris@28 341 oddBrushColour = oddBrushColour.light(150);
Chris@28 342 }
Chris@28 343 oddBrushColour.setAlpha(100);
Chris@28 344 }
Chris@28 345
Chris@0 346 // std::cerr << "TimeInstantLayer::paint: resolution is "
Chris@0 347 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 348
Chris@0 349 QPoint localPos;
Chris@0 350 long illuminateFrame = -1;
Chris@0 351
Chris@44 352 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 353 SparseOneDimensionalModel::PointList localPoints =
Chris@44 354 getLocalPoints(v, localPos.x());
Chris@0 355 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 356 }
Chris@0 357
Chris@23 358 int prevX = -1;
Chris@79 359 int textY = v->getTextLabelHeight(this, paint);
Chris@79 360
Chris@0 361 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@0 362 i != points.end(); ++i) {
Chris@0 363
Chris@0 364 const SparseOneDimensionalModel::Point &p(*i);
Chris@17 365 SparseOneDimensionalModel::PointList::const_iterator j = i;
Chris@17 366 ++j;
Chris@0 367
Chris@44 368 int x = v->getXForFrame(p.frame);
Chris@23 369 if (x == prevX && p.frame != illuminateFrame) continue;
Chris@23 370
Chris@44 371 int iw = v->getXForFrame(p.frame + m_model->getResolution()) - x;
Chris@16 372 if (iw < 2) {
Chris@17 373 if (iw < 1) {
Chris@17 374 iw = 2;
Chris@17 375 if (j != points.end()) {
Chris@44 376 int nx = v->getXForFrame(j->frame);
Chris@17 377 if (nx < x + 3) iw = 1;
Chris@17 378 }
Chris@17 379 } else {
Chris@17 380 iw = 2;
Chris@17 381 }
Chris@16 382 }
Chris@20 383
Chris@0 384 if (p.frame == illuminateFrame) {
Chris@287 385 paint.setPen(getForegroundQColor(v));
Chris@0 386 } else {
Chris@0 387 paint.setPen(brushColour);
Chris@0 388 }
Chris@23 389
Chris@28 390 if (m_plotStyle == PlotInstants) {
Chris@28 391 if (iw > 1) {
Chris@44 392 paint.drawRect(x, 0, iw - 1, v->height() - 1);
Chris@28 393 } else {
Chris@44 394 paint.drawLine(x, 0, x, v->height() - 1);
Chris@28 395 }
Chris@23 396 } else {
Chris@28 397
Chris@28 398 if (odd) paint.setBrush(oddBrushColour);
Chris@28 399 else paint.setBrush(brushColour);
Chris@28 400
Chris@28 401 int nx;
Chris@28 402
Chris@28 403 if (j != points.end()) {
Chris@28 404 const SparseOneDimensionalModel::Point &q(*j);
Chris@44 405 nx = v->getXForFrame(q.frame);
Chris@28 406 } else {
Chris@44 407 nx = v->getXForFrame(m_model->getEndFrame());
Chris@28 408 }
Chris@28 409
Chris@28 410 if (nx >= x) {
Chris@28 411
Chris@28 412 if (illuminateFrame != p.frame &&
Chris@44 413 (nx < x + 5 || x >= v->width() - 1)) {
Chris@28 414 paint.setPen(Qt::NoPen);
Chris@28 415 }
Chris@28 416
Chris@44 417 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@28 418 }
Chris@28 419
Chris@28 420 odd = !odd;
Chris@23 421 }
Chris@28 422
Chris@287 423 paint.setPen(getBaseQColor());
Chris@0 424
Chris@0 425 if (p.label != "") {
Chris@0 426
Chris@0 427 // only draw if there's enough room from here to the next point
Chris@0 428
Chris@0 429 int lw = paint.fontMetrics().width(p.label);
Chris@0 430 bool good = true;
Chris@0 431
Chris@17 432 if (j != points.end()) {
Chris@44 433 int nx = v->getXForFrame(j->frame);
Chris@20 434 if (nx >= x && nx - x - iw - 3 <= lw) good = false;
Chris@0 435 }
Chris@0 436
Chris@0 437 if (good) {
Chris@79 438 paint.drawText(x + iw + 2, textY, p.label);
Chris@0 439 }
Chris@0 440 }
Chris@23 441
Chris@23 442 prevX = x;
Chris@0 443 }
Chris@0 444 }
Chris@0 445
Chris@17 446 void
Chris@44 447 TimeInstantLayer::drawStart(View *v, QMouseEvent *e)
Chris@17 448 {
Chris@17 449 std::cerr << "TimeInstantLayer::drawStart(" << e->x() << ")" << std::endl;
Chris@17 450
Chris@17 451 if (!m_model) return;
Chris@17 452
Chris@44 453 long frame = v->getFrameForX(e->x());
Chris@17 454 if (frame < 0) frame = 0;
Chris@21 455 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 456
Chris@17 457 m_editingPoint = SparseOneDimensionalModel::Point(frame, tr("New Point"));
Chris@22 458
Chris@376 459 if (m_editingCommand) finish(m_editingCommand);
Chris@22 460 m_editingCommand = new SparseOneDimensionalModel::EditCommand(m_model,
Chris@22 461 tr("Draw Point"));
Chris@22 462 m_editingCommand->addPoint(m_editingPoint);
Chris@22 463
Chris@18 464 m_editing = true;
Chris@17 465 }
Chris@17 466
Chris@17 467 void
Chris@44 468 TimeInstantLayer::drawDrag(View *v, QMouseEvent *e)
Chris@17 469 {
Chris@17 470 std::cerr << "TimeInstantLayer::drawDrag(" << e->x() << ")" << std::endl;
Chris@17 471
Chris@18 472 if (!m_model || !m_editing) 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 m_editingCommand->deletePoint(m_editingPoint);
Chris@17 478 m_editingPoint.frame = frame;
Chris@22 479 m_editingCommand->addPoint(m_editingPoint);
Chris@17 480 }
Chris@17 481
Chris@17 482 void
Chris@248 483 TimeInstantLayer::drawEnd(View *, QMouseEvent *e)
Chris@17 484 {
Chris@17 485 std::cerr << "TimeInstantLayer::drawEnd(" << e->x() << ")" << std::endl;
Chris@18 486 if (!m_model || !m_editing) return;
Chris@23 487 QString newName = tr("Add Point at %1 s")
Chris@23 488 .arg(RealTime::frame2RealTime(m_editingPoint.frame,
Chris@23 489 m_model->getSampleRate())
Chris@23 490 .toText(false).c_str());
Chris@23 491 m_editingCommand->setName(newName);
Chris@376 492 finish(m_editingCommand);
Chris@22 493 m_editingCommand = 0;
Chris@18 494 m_editing = false;
Chris@18 495 }
Chris@18 496
Chris@18 497 void
Chris@335 498 TimeInstantLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 499 {
Chris@335 500 if (!m_model) return;
Chris@335 501
Chris@335 502 SparseOneDimensionalModel::PointList points = getLocalPoints(v, e->x());
Chris@335 503 if (points.empty()) return;
Chris@335 504
Chris@335 505 m_editingPoint = *points.begin();
Chris@335 506
Chris@335 507 if (m_editingCommand) {
Chris@376 508 finish(m_editingCommand);
Chris@335 509 m_editingCommand = 0;
Chris@335 510 }
Chris@335 511
Chris@335 512 m_editing = true;
Chris@335 513 }
Chris@335 514
Chris@335 515 void
Chris@335 516 TimeInstantLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 517 {
Chris@335 518 }
Chris@335 519
Chris@335 520 void
Chris@335 521 TimeInstantLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 522 {
Chris@335 523 if (!m_model || !m_editing) return;
Chris@335 524
Chris@335 525 m_editing = false;
Chris@335 526
Chris@335 527 SparseOneDimensionalModel::PointList points = getLocalPoints(v, e->x());
Chris@335 528 if (points.empty()) return;
Chris@335 529 if (points.begin()->frame != m_editingPoint.frame) return;
Chris@335 530
Chris@335 531 m_editingCommand = new SparseOneDimensionalModel::EditCommand
Chris@335 532 (m_model, tr("Erase Point"));
Chris@335 533
Chris@335 534 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 535
Chris@376 536 finish(m_editingCommand);
Chris@335 537 m_editingCommand = 0;
Chris@335 538 m_editing = false;
Chris@335 539 }
Chris@335 540
Chris@335 541 void
Chris@44 542 TimeInstantLayer::editStart(View *v, QMouseEvent *e)
Chris@18 543 {
Chris@18 544 std::cerr << "TimeInstantLayer::editStart(" << e->x() << ")" << std::endl;
Chris@18 545
Chris@17 546 if (!m_model) return;
Chris@18 547
Chris@44 548 SparseOneDimensionalModel::PointList points = getLocalPoints(v, e->x());
Chris@18 549 if (points.empty()) return;
Chris@18 550
Chris@18 551 m_editingPoint = *points.begin();
Chris@22 552
Chris@22 553 if (m_editingCommand) {
Chris@376 554 finish(m_editingCommand);
Chris@22 555 m_editingCommand = 0;
Chris@22 556 }
Chris@22 557
Chris@18 558 m_editing = true;
Chris@18 559 }
Chris@18 560
Chris@18 561 void
Chris@44 562 TimeInstantLayer::editDrag(View *v, QMouseEvent *e)
Chris@18 563 {
Chris@18 564 std::cerr << "TimeInstantLayer::editDrag(" << e->x() << ")" << std::endl;
Chris@18 565
Chris@18 566 if (!m_model || !m_editing) return;
Chris@18 567
Chris@44 568 long frame = v->getFrameForX(e->x());
Chris@18 569 if (frame < 0) frame = 0;
Chris@21 570 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 571
Chris@22 572 if (!m_editingCommand) {
Chris@22 573 m_editingCommand = new SparseOneDimensionalModel::EditCommand(m_model,
Chris@22 574 tr("Drag Point"));
Chris@22 575 }
Chris@22 576
Chris@22 577 m_editingCommand->deletePoint(m_editingPoint);
Chris@18 578 m_editingPoint.frame = frame;
Chris@22 579 m_editingCommand->addPoint(m_editingPoint);
Chris@18 580 }
Chris@18 581
Chris@18 582 void
Chris@248 583 TimeInstantLayer::editEnd(View *, QMouseEvent *e)
Chris@18 584 {
Chris@18 585 std::cerr << "TimeInstantLayer::editEnd(" << e->x() << ")" << std::endl;
Chris@18 586 if (!m_model || !m_editing) return;
Chris@23 587 if (m_editingCommand) {
Chris@23 588 QString newName = tr("Move Point to %1 s")
Chris@23 589 .arg(RealTime::frame2RealTime(m_editingPoint.frame,
Chris@23 590 m_model->getSampleRate())
Chris@23 591 .toText(false).c_str());
Chris@23 592 m_editingCommand->setName(newName);
Chris@376 593 finish(m_editingCommand);
Chris@23 594 }
Chris@22 595 m_editingCommand = 0;
Chris@18 596 m_editing = false;
Chris@17 597 }
Chris@17 598
Chris@255 599 bool
Chris@70 600 TimeInstantLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 601 {
Chris@255 602 if (!m_model) return false;
Chris@70 603
Chris@70 604 SparseOneDimensionalModel::PointList points = getLocalPoints(v, e->x());
Chris@255 605 if (points.empty()) return false;
Chris@70 606
Chris@70 607 SparseOneDimensionalModel::Point point = *points.begin();
Chris@70 608
Chris@70 609 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 610 (m_model->getSampleRate(),
Chris@70 611 ItemEditDialog::ShowTime |
Chris@70 612 ItemEditDialog::ShowText);
Chris@70 613
Chris@70 614 dialog->setFrameTime(point.frame);
Chris@70 615 dialog->setText(point.label);
Chris@70 616
Chris@70 617 if (dialog->exec() == QDialog::Accepted) {
Chris@70 618
Chris@70 619 SparseOneDimensionalModel::Point newPoint = point;
Chris@70 620 newPoint.frame = dialog->getFrameTime();
Chris@70 621 newPoint.label = dialog->getText();
Chris@70 622
Chris@70 623 SparseOneDimensionalModel::EditCommand *command =
Chris@70 624 new SparseOneDimensionalModel::EditCommand(m_model, tr("Edit Point"));
Chris@70 625 command->deletePoint(point);
Chris@70 626 command->addPoint(newPoint);
Chris@376 627 finish(command);
Chris@70 628 }
Chris@70 629
Chris@70 630 delete dialog;
Chris@255 631 return true;
Chris@70 632 }
Chris@70 633
Chris@70 634 void
Chris@43 635 TimeInstantLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 636 {
Chris@99 637 if (!m_model) return;
Chris@99 638
Chris@43 639 SparseOneDimensionalModel::EditCommand *command =
Chris@43 640 new SparseOneDimensionalModel::EditCommand(m_model,
Chris@43 641 tr("Drag Selection"));
Chris@43 642
Chris@43 643 SparseOneDimensionalModel::PointList points =
Chris@43 644 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 645
Chris@43 646 for (SparseOneDimensionalModel::PointList::iterator i = points.begin();
Chris@43 647 i != points.end(); ++i) {
Chris@43 648
Chris@43 649 if (s.contains(i->frame)) {
Chris@43 650 SparseOneDimensionalModel::Point newPoint(*i);
Chris@43 651 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 652 command->deletePoint(*i);
Chris@43 653 command->addPoint(newPoint);
Chris@43 654 }
Chris@43 655 }
Chris@43 656
Chris@376 657 finish(command);
Chris@43 658 }
Chris@43 659
Chris@43 660 void
Chris@43 661 TimeInstantLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 662 {
Chris@99 663 if (!m_model) return;
Chris@99 664
Chris@43 665 SparseOneDimensionalModel::EditCommand *command =
Chris@43 666 new SparseOneDimensionalModel::EditCommand(m_model,
Chris@43 667 tr("Resize Selection"));
Chris@43 668
Chris@43 669 SparseOneDimensionalModel::PointList points =
Chris@43 670 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 671
Chris@43 672 double ratio =
Chris@43 673 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 674 double(s.getEndFrame() - s.getStartFrame());
Chris@43 675
Chris@43 676 for (SparseOneDimensionalModel::PointList::iterator i = points.begin();
Chris@43 677 i != points.end(); ++i) {
Chris@43 678
Chris@43 679 if (s.contains(i->frame)) {
Chris@43 680
Chris@43 681 double target = i->frame;
Chris@43 682 target = newSize.getStartFrame() +
Chris@43 683 double(target - s.getStartFrame()) * ratio;
Chris@43 684
Chris@43 685 SparseOneDimensionalModel::Point newPoint(*i);
Chris@43 686 newPoint.frame = lrint(target);
Chris@43 687 command->deletePoint(*i);
Chris@43 688 command->addPoint(newPoint);
Chris@43 689 }
Chris@43 690 }
Chris@43 691
Chris@376 692 finish(command);
Chris@43 693 }
Chris@43 694
Chris@43 695 void
Chris@43 696 TimeInstantLayer::deleteSelection(Selection s)
Chris@43 697 {
Chris@99 698 if (!m_model) return;
Chris@99 699
Chris@43 700 SparseOneDimensionalModel::EditCommand *command =
Chris@43 701 new SparseOneDimensionalModel::EditCommand(m_model,
Chris@43 702 tr("Delete Selection"));
Chris@43 703
Chris@43 704 SparseOneDimensionalModel::PointList points =
Chris@43 705 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 706
Chris@43 707 for (SparseOneDimensionalModel::PointList::iterator i = points.begin();
Chris@43 708 i != points.end(); ++i) {
Chris@43 709 if (s.contains(i->frame)) command->deletePoint(*i);
Chris@43 710 }
Chris@43 711
Chris@376 712 finish(command);
Chris@43 713 }
Chris@76 714
Chris@76 715 void
Chris@359 716 TimeInstantLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 717 {
Chris@99 718 if (!m_model) return;
Chris@99 719
Chris@76 720 SparseOneDimensionalModel::PointList points =
Chris@76 721 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 722
Chris@76 723 for (SparseOneDimensionalModel::PointList::iterator i = points.begin();
Chris@76 724 i != points.end(); ++i) {
Chris@76 725 if (s.contains(i->frame)) {
Chris@76 726 Clipboard::Point point(i->frame, i->label);
Chris@359 727 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 728 to.addPoint(point);
Chris@76 729 }
Chris@76 730 }
Chris@76 731 }
Chris@76 732
Chris@125 733 bool
Chris@359 734 TimeInstantLayer::paste(View *v, const Clipboard &from, int frameOffset, bool)
Chris@76 735 {
Chris@125 736 if (!m_model) return false;
Chris@99 737
Chris@76 738 const Clipboard::PointList &points = from.getPoints();
Chris@76 739
Chris@358 740 bool realign = false;
Chris@358 741
Chris@360 742 if (clipboardHasDifferentAlignment(v, from)) {
Chris@358 743
Chris@360 744 QMessageBox::StandardButton button =
Chris@360 745 QMessageBox::question(v, tr("Re-align pasted instants?"),
Chris@360 746 tr("The instants you are pasting came from a layer with different source material from this one. Do you want to re-align them in time, to match the source material for this layer?"),
Chris@360 747 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 748 QMessageBox::Yes);
Chris@358 749
Chris@360 750 if (button == QMessageBox::Cancel) {
Chris@360 751 return false;
Chris@360 752 }
Chris@358 753
Chris@360 754 if (button == QMessageBox::Yes) {
Chris@360 755 realign = true;
Chris@360 756 }
Chris@358 757 }
Chris@358 758
Chris@358 759 SparseOneDimensionalModel::EditCommand *command =
Chris@358 760 new SparseOneDimensionalModel::EditCommand(m_model, tr("Paste"));
Chris@358 761
Chris@76 762 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 763 i != points.end(); ++i) {
Chris@76 764
Chris@76 765 if (!i->haveFrame()) continue;
Chris@359 766
Chris@76 767 size_t frame = 0;
Chris@359 768
Chris@359 769 if (!realign) {
Chris@359 770
Chris@359 771 frame = i->getFrame();
Chris@359 772
Chris@359 773 } else {
Chris@359 774
Chris@359 775 if (i->haveReferenceFrame()) {
Chris@359 776 frame = i->getReferenceFrame();
Chris@359 777 frame = alignFromReference(v, frame);
Chris@359 778 } else {
Chris@359 779 frame = i->getFrame();
Chris@359 780 }
Chris@76 781 }
Chris@359 782
Chris@359 783 if (frameOffset > 0) frame += frameOffset;
Chris@359 784 else if (frameOffset < 0) {
Chris@359 785 if (frame > -frameOffset) frame += frameOffset;
Chris@359 786 else frame = 0;
Chris@359 787 }
Chris@359 788
Chris@76 789 SparseOneDimensionalModel::Point newPoint(frame);
Chris@125 790 if (i->haveLabel()) {
Chris@125 791 newPoint.label = i->getLabel();
Chris@125 792 } else if (i->haveValue()) {
Chris@125 793 newPoint.label = QString("%1").arg(i->getValue());
Chris@125 794 }
Chris@76 795
Chris@76 796 command->addPoint(newPoint);
Chris@76 797 }
Chris@76 798
Chris@376 799 finish(command);
Chris@125 800 return true;
Chris@76 801 }
Chris@43 802
Chris@287 803 int
Chris@287 804 TimeInstantLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 805 {
Chris@287 806 impose = false;
Chris@287 807 return ColourDatabase::getInstance()->getColourIndex
Chris@287 808 (QString(darkbg ? "Bright Purple" : "Purple"));
Chris@287 809 }
Chris@287 810
Chris@316 811 void
Chris@316 812 TimeInstantLayer::toXml(QTextStream &stream,
Chris@316 813 QString indent, QString extraAttributes) const
Chris@6 814 {
Chris@316 815 SingleColourLayer::toXml(stream, indent,
Chris@316 816 extraAttributes +
Chris@316 817 QString(" plotStyle=\"%1\"")
Chris@316 818 .arg(m_plotStyle));
Chris@6 819 }
Chris@0 820
Chris@11 821 void
Chris@11 822 TimeInstantLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 823 {
Chris@287 824 SingleColourLayer::setProperties(attributes);
Chris@28 825
Chris@28 826 bool ok;
Chris@28 827 PlotStyle style = (PlotStyle)
Chris@28 828 attributes.value("plotStyle").toInt(&ok);
Chris@28 829 if (ok) setPlotStyle(style);
Chris@11 830 }
Chris@11 831