annotate layer/TimeInstantLayer.cpp @ 216:34bbbcb3c01f sv1-1.0pre1

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