annotate layer/NoteLayer.cpp @ 69:6dad2724f3aa

* Ensure plugin parameters for a transform are saved in the .sv file and restored in case the plugin has to be run again * Make plugin dialog offer options for mixdown/single-channel use if the file has more than one channels but the plugin only accepts one * Fix incorrect samplerate playback for second file loaded if its samplerate differed from first * Add Zoom to Fit and Select Visible Range menu options, split out Import Audio into main model and secondary model options * Add stubs for cut, copy and paste operations (not implemented yet)
author Chris Cannam
date Thu, 30 Mar 2006 13:18:11 +0000
parents e9eac9368e29
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@30 2
Chris@30 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@30 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@30 14 */
Chris@30 15
Chris@30 16 #include "NoteLayer.h"
Chris@30 17
Chris@30 18 #include "base/Model.h"
Chris@30 19 #include "base/RealTime.h"
Chris@30 20 #include "base/Profiler.h"
Chris@30 21 #include "base/Pitch.h"
Chris@30 22 #include "base/View.h"
Chris@30 23
Chris@30 24 #include "model/NoteModel.h"
Chris@30 25
Chris@42 26 #include "SpectrogramLayer.h" // for optional frequency alignment
Chris@42 27
Chris@30 28 #include <QPainter>
Chris@30 29 #include <QPainterPath>
Chris@30 30 #include <QMouseEvent>
Chris@30 31
Chris@30 32 #include <iostream>
Chris@30 33 #include <cmath>
Chris@30 34
Chris@44 35 NoteLayer::NoteLayer() :
Chris@44 36 Layer(),
Chris@30 37 m_model(0),
Chris@30 38 m_editing(false),
Chris@30 39 m_originalPoint(0, 0.0, 0, tr("New Point")),
Chris@30 40 m_editingPoint(0, 0.0, 0, tr("New Point")),
Chris@30 41 m_editingCommand(0),
Chris@30 42 m_colour(Qt::black),
Chris@30 43 m_verticalScale(MinMaxRangeScale)
Chris@30 44 {
Chris@44 45
Chris@30 46 }
Chris@30 47
Chris@30 48 void
Chris@30 49 NoteLayer::setModel(NoteModel *model)
Chris@30 50 {
Chris@30 51 if (m_model == model) return;
Chris@30 52 m_model = model;
Chris@30 53
Chris@30 54 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@30 55 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@30 56 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@30 57
Chris@30 58 connect(m_model, SIGNAL(completionChanged()),
Chris@30 59 this, SIGNAL(modelCompletionChanged()));
Chris@30 60
Chris@30 61 std::cerr << "NoteLayer::setModel(" << model << ")" << std::endl;
Chris@30 62
Chris@30 63 emit modelReplaced();
Chris@30 64 }
Chris@30 65
Chris@30 66 Layer::PropertyList
Chris@30 67 NoteLayer::getProperties() const
Chris@30 68 {
Chris@30 69 PropertyList list;
Chris@30 70 list.push_back(tr("Colour"));
Chris@30 71 list.push_back(tr("Vertical Scale"));
Chris@30 72 return list;
Chris@30 73 }
Chris@30 74
Chris@30 75 Layer::PropertyType
Chris@30 76 NoteLayer::getPropertyType(const PropertyName &) const
Chris@30 77 {
Chris@30 78 return ValueProperty;
Chris@30 79 }
Chris@30 80
Chris@30 81 int
Chris@30 82 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@30 83 int *min, int *max) const
Chris@30 84 {
Chris@30 85 //!!! factor this colour handling stuff out into a colour manager class
Chris@30 86
Chris@30 87 int deft = 0;
Chris@30 88
Chris@30 89 if (name == tr("Colour")) {
Chris@30 90
Chris@30 91 if (min) *min = 0;
Chris@30 92 if (max) *max = 5;
Chris@30 93
Chris@30 94 if (m_colour == Qt::black) deft = 0;
Chris@30 95 else if (m_colour == Qt::darkRed) deft = 1;
Chris@30 96 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@30 97 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@30 98 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@30 99 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@30 100
Chris@30 101 } else if (name == tr("Vertical Scale")) {
Chris@30 102
Chris@30 103 if (min) *min = 0;
Chris@30 104 if (max) *max = 2;
Chris@30 105
Chris@30 106 deft = int(m_verticalScale);
Chris@30 107
Chris@30 108 } else {
Chris@30 109
Chris@30 110 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@30 111 }
Chris@30 112
Chris@30 113 return deft;
Chris@30 114 }
Chris@30 115
Chris@30 116 QString
Chris@30 117 NoteLayer::getPropertyValueLabel(const PropertyName &name,
Chris@30 118 int value) const
Chris@30 119 {
Chris@30 120 if (name == tr("Colour")) {
Chris@30 121 switch (value) {
Chris@30 122 default:
Chris@30 123 case 0: return tr("Black");
Chris@30 124 case 1: return tr("Red");
Chris@30 125 case 2: return tr("Blue");
Chris@30 126 case 3: return tr("Green");
Chris@30 127 case 4: return tr("Purple");
Chris@30 128 case 5: return tr("Orange");
Chris@30 129 }
Chris@30 130 } else if (name == tr("Vertical Scale")) {
Chris@30 131 switch (value) {
Chris@30 132 default:
Chris@30 133 case 0: return tr("Note Range In Use");
Chris@30 134 case 1: return tr("MIDI Note Range");
Chris@30 135 case 2: return tr("Frequency");
Chris@30 136 }
Chris@30 137 }
Chris@30 138 return tr("<unknown>");
Chris@30 139 }
Chris@30 140
Chris@30 141 void
Chris@30 142 NoteLayer::setProperty(const PropertyName &name, int value)
Chris@30 143 {
Chris@30 144 if (name == tr("Colour")) {
Chris@30 145 switch (value) {
Chris@30 146 default:
Chris@30 147 case 0: setBaseColour(Qt::black); break;
Chris@30 148 case 1: setBaseColour(Qt::darkRed); break;
Chris@30 149 case 2: setBaseColour(Qt::darkBlue); break;
Chris@30 150 case 3: setBaseColour(Qt::darkGreen); break;
Chris@30 151 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@30 152 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@30 153 }
Chris@30 154 } else if (name == tr("Vertical Scale")) {
Chris@30 155 setVerticalScale(VerticalScale(value));
Chris@30 156 }
Chris@30 157 }
Chris@30 158
Chris@30 159 void
Chris@30 160 NoteLayer::setBaseColour(QColor colour)
Chris@30 161 {
Chris@30 162 if (m_colour == colour) return;
Chris@30 163 m_colour = colour;
Chris@30 164 emit layerParametersChanged();
Chris@30 165 }
Chris@30 166
Chris@30 167 void
Chris@30 168 NoteLayer::setVerticalScale(VerticalScale scale)
Chris@30 169 {
Chris@30 170 if (m_verticalScale == scale) return;
Chris@30 171 m_verticalScale = scale;
Chris@30 172 emit layerParametersChanged();
Chris@30 173 }
Chris@30 174
Chris@30 175 bool
Chris@44 176 NoteLayer::isLayerScrollable(const View *v) const
Chris@30 177 {
Chris@30 178 QPoint discard;
Chris@44 179 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@30 180 }
Chris@30 181
Chris@30 182 NoteModel::PointList
Chris@44 183 NoteLayer::getLocalPoints(View *v, int x) const
Chris@30 184 {
Chris@30 185 if (!m_model) return NoteModel::PointList();
Chris@30 186
Chris@44 187 long frame = v->getFrameForX(x);
Chris@30 188
Chris@30 189 NoteModel::PointList onPoints =
Chris@30 190 m_model->getPoints(frame);
Chris@30 191
Chris@30 192 if (!onPoints.empty()) {
Chris@30 193 return onPoints;
Chris@30 194 }
Chris@30 195
Chris@30 196 NoteModel::PointList prevPoints =
Chris@30 197 m_model->getPreviousPoints(frame);
Chris@30 198 NoteModel::PointList nextPoints =
Chris@30 199 m_model->getNextPoints(frame);
Chris@30 200
Chris@30 201 NoteModel::PointList usePoints = prevPoints;
Chris@30 202
Chris@30 203 if (prevPoints.empty()) {
Chris@30 204 usePoints = nextPoints;
Chris@44 205 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
Chris@44 206 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@30 207 usePoints = nextPoints;
Chris@30 208 } else if (nextPoints.begin()->frame - frame <
Chris@30 209 frame - prevPoints.begin()->frame) {
Chris@30 210 usePoints = nextPoints;
Chris@30 211 }
Chris@30 212
Chris@30 213 if (!usePoints.empty()) {
Chris@30 214 int fuzz = 2;
Chris@44 215 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@30 216 if ((px > x && px - x > fuzz) ||
Chris@30 217 (px < x && x - px > fuzz + 1)) {
Chris@30 218 usePoints.clear();
Chris@30 219 }
Chris@30 220 }
Chris@30 221
Chris@30 222 return usePoints;
Chris@30 223 }
Chris@30 224
Chris@30 225 QString
Chris@44 226 NoteLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@30 227 {
Chris@30 228 int x = pos.x();
Chris@30 229
Chris@30 230 if (!m_model || !m_model->getSampleRate()) return "";
Chris@30 231
Chris@44 232 NoteModel::PointList points = getLocalPoints(v, x);
Chris@30 233
Chris@30 234 if (points.empty()) {
Chris@30 235 if (!m_model->isReady()) {
Chris@30 236 return tr("In progress");
Chris@30 237 } else {
Chris@30 238 return tr("No local points");
Chris@30 239 }
Chris@30 240 }
Chris@30 241
Chris@30 242 Note note(0);
Chris@30 243 NoteModel::PointList::iterator i;
Chris@30 244
Chris@30 245 for (i = points.begin(); i != points.end(); ++i) {
Chris@30 246
Chris@44 247 int y = getYForValue(v, i->value);
Chris@30 248 int h = 3;
Chris@30 249
Chris@30 250 if (m_model->getValueQuantization() != 0.0) {
Chris@44 251 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
Chris@30 252 if (h < 3) h = 3;
Chris@30 253 }
Chris@30 254
Chris@30 255 if (pos.y() >= y - h && pos.y() <= y) {
Chris@30 256 note = *i;
Chris@30 257 break;
Chris@30 258 }
Chris@30 259 }
Chris@30 260
Chris@30 261 if (i == points.end()) return tr("No local points");
Chris@30 262
Chris@30 263 RealTime rt = RealTime::frame2RealTime(note.frame,
Chris@30 264 m_model->getSampleRate());
Chris@30 265 RealTime rd = RealTime::frame2RealTime(note.duration,
Chris@30 266 m_model->getSampleRate());
Chris@30 267
Chris@30 268 QString text;
Chris@30 269
Chris@30 270 if (note.label == "") {
Chris@30 271 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
Chris@30 272 .arg(rt.toText(true).c_str())
Chris@30 273 .arg(note.value)
Chris@30 274 .arg(rd.toText(true).c_str());
Chris@30 275 } else {
Chris@30 276 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@30 277 .arg(rt.toText(true).c_str())
Chris@30 278 .arg(note.value)
Chris@30 279 .arg(rd.toText(true).c_str())
Chris@30 280 .arg(note.label);
Chris@30 281 }
Chris@30 282
Chris@44 283 pos = QPoint(v->getXForFrame(note.frame),
Chris@44 284 getYForValue(v, note.value));
Chris@30 285 return text;
Chris@30 286 }
Chris@30 287
Chris@30 288 bool
Chris@44 289 NoteLayer::snapToFeatureFrame(View *v, int &frame,
Chris@44 290 size_t &resolution,
Chris@44 291 SnapType snap) const
Chris@30 292 {
Chris@30 293 if (!m_model) {
Chris@44 294 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@30 295 }
Chris@30 296
Chris@30 297 resolution = m_model->getResolution();
Chris@30 298 NoteModel::PointList points;
Chris@30 299
Chris@30 300 if (snap == SnapNeighbouring) {
Chris@30 301
Chris@44 302 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@30 303 if (points.empty()) return false;
Chris@30 304 frame = points.begin()->frame;
Chris@30 305 return true;
Chris@30 306 }
Chris@30 307
Chris@30 308 points = m_model->getPoints(frame, frame);
Chris@30 309 int snapped = frame;
Chris@30 310 bool found = false;
Chris@30 311
Chris@30 312 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 313 i != points.end(); ++i) {
Chris@30 314
Chris@30 315 if (snap == SnapRight) {
Chris@30 316
Chris@30 317 if (i->frame > frame) {
Chris@30 318 snapped = i->frame;
Chris@30 319 found = true;
Chris@30 320 break;
Chris@30 321 }
Chris@30 322
Chris@30 323 } else if (snap == SnapLeft) {
Chris@30 324
Chris@30 325 if (i->frame <= frame) {
Chris@30 326 snapped = i->frame;
Chris@30 327 found = true; // don't break, as the next may be better
Chris@30 328 } else {
Chris@30 329 break;
Chris@30 330 }
Chris@30 331
Chris@30 332 } else { // nearest
Chris@30 333
Chris@30 334 NoteModel::PointList::const_iterator j = i;
Chris@30 335 ++j;
Chris@30 336
Chris@30 337 if (j == points.end()) {
Chris@30 338
Chris@30 339 snapped = i->frame;
Chris@30 340 found = true;
Chris@30 341 break;
Chris@30 342
Chris@30 343 } else if (j->frame >= frame) {
Chris@30 344
Chris@30 345 if (j->frame - frame < frame - i->frame) {
Chris@30 346 snapped = j->frame;
Chris@30 347 } else {
Chris@30 348 snapped = i->frame;
Chris@30 349 }
Chris@30 350 found = true;
Chris@30 351 break;
Chris@30 352 }
Chris@30 353 }
Chris@30 354 }
Chris@30 355
Chris@30 356 frame = snapped;
Chris@30 357 return found;
Chris@30 358 }
Chris@30 359
Chris@30 360 int
Chris@44 361 NoteLayer::getYForValue(View *v, float value) const
Chris@30 362 {
Chris@44 363 float min, max, h = v->height();
Chris@42 364
Chris@30 365 switch (m_verticalScale) {
Chris@30 366
Chris@30 367 case MIDIRangeScale:
Chris@30 368 min = 0.0;
Chris@30 369 max = 127.0;
Chris@30 370 break;
Chris@30 371
Chris@30 372 case MinMaxRangeScale:
Chris@30 373 min = m_model->getValueMinimum();
Chris@30 374 max = m_model->getValueMaximum();
Chris@30 375 break;
Chris@30 376
Chris@30 377 case FrequencyScale:
Chris@42 378
Chris@42 379 value = Pitch::getFrequencyForPitch(lrintf(value),
Chris@42 380 value - lrintf(value));
Chris@42 381
Chris@42 382 // If we have a spectrogram layer on the same view as us, align
Chris@42 383 // ourselves with it...
Chris@44 384 for (int i = 0; i < v->getLayerCount(); ++i) {
Chris@42 385 SpectrogramLayer *spectrogram = dynamic_cast<SpectrogramLayer *>
Chris@44 386 (v->getLayer(i));
Chris@42 387 if (spectrogram) {
Chris@44 388 return spectrogram->getYForFrequency(v, value);
Chris@42 389 }
Chris@42 390 }
Chris@42 391
Chris@42 392 // ...otherwise just interpolate
Chris@30 393 std::cerr << "FrequencyScale: value in = " << value << std::endl;
Chris@30 394 min = m_model->getValueMinimum();
Chris@30 395 min = Pitch::getFrequencyForPitch(lrintf(min), min - lrintf(min));
Chris@30 396 max = m_model->getValueMaximum();
Chris@30 397 max = Pitch::getFrequencyForPitch(lrintf(max), max - lrintf(max));
Chris@30 398 std::cerr << "FrequencyScale: min = " << min << ", max = " << max << ", value = " << value << std::endl;
Chris@30 399 break;
Chris@30 400 }
Chris@30 401
Chris@30 402 if (max < min) max = min;
Chris@30 403 max = max + 1.0;
Chris@30 404
Chris@30 405 return int(h - ((value - min) * h) / (max - min)) - 1;
Chris@30 406 }
Chris@30 407
Chris@30 408 float
Chris@44 409 NoteLayer::getValueForY(View *v, int y) const
Chris@30 410 {
Chris@66 411 //!!!
Chris@66 412
Chris@30 413 float min = m_model->getValueMinimum();
Chris@30 414 float max = m_model->getValueMaximum();
Chris@30 415 if (max == min) max = min + 1.0;
Chris@30 416
Chris@44 417 int h = v->height();
Chris@30 418
Chris@30 419 return min + (float(h - y) * float(max - min)) / h;
Chris@30 420 }
Chris@30 421
Chris@30 422 void
Chris@44 423 NoteLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@30 424 {
Chris@30 425 if (!m_model || !m_model->isOK()) return;
Chris@30 426
Chris@30 427 int sampleRate = m_model->getSampleRate();
Chris@30 428 if (!sampleRate) return;
Chris@30 429
Chris@30 430 // Profiler profiler("NoteLayer::paint", true);
Chris@30 431
Chris@30 432 int x0 = rect.left(), x1 = rect.right();
Chris@44 433 long frame0 = v->getFrameForX(x0);
Chris@44 434 long frame1 = v->getFrameForX(x1);
Chris@30 435
Chris@30 436 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
Chris@30 437 if (points.empty()) return;
Chris@30 438
Chris@30 439 paint.setPen(m_colour);
Chris@30 440
Chris@30 441 QColor brushColour(m_colour);
Chris@30 442 brushColour.setAlpha(80);
Chris@30 443
Chris@30 444 // std::cerr << "NoteLayer::paint: resolution is "
Chris@30 445 // << m_model->getResolution() << " frames" << std::endl;
Chris@30 446
Chris@30 447 float min = m_model->getValueMinimum();
Chris@30 448 float max = m_model->getValueMaximum();
Chris@30 449 if (max == min) max = min + 1.0;
Chris@30 450
Chris@44 451 int origin = int(nearbyint(v->height() -
Chris@44 452 (-min * v->height()) / (max - min)));
Chris@30 453
Chris@30 454 QPoint localPos;
Chris@30 455 long illuminateFrame = -1;
Chris@30 456
Chris@44 457 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@30 458 NoteModel::PointList localPoints =
Chris@44 459 getLocalPoints(v, localPos.x());
Chris@30 460 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@30 461 }
Chris@30 462
Chris@30 463 paint.save();
Chris@30 464 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 465
Chris@30 466 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 467 i != points.end(); ++i) {
Chris@30 468
Chris@30 469 const NoteModel::Point &p(*i);
Chris@30 470
Chris@44 471 int x = v->getXForFrame(p.frame);
Chris@44 472 int y = getYForValue(v, p.value);
Chris@44 473 int w = v->getXForFrame(p.frame + p.duration) - x;
Chris@30 474 int h = 3;
Chris@30 475
Chris@30 476 if (m_model->getValueQuantization() != 0.0) {
Chris@44 477 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
Chris@30 478 if (h < 3) h = 3;
Chris@30 479 }
Chris@30 480
Chris@30 481 if (w < 1) w = 1;
Chris@30 482 paint.setPen(m_colour);
Chris@30 483 paint.setBrush(brushColour);
Chris@30 484
Chris@30 485 if (illuminateFrame == p.frame) {
Chris@30 486 if (localPos.y() >= y - h && localPos.y() < y) {
Chris@30 487 paint.setPen(Qt::black);//!!!
Chris@30 488 paint.setBrush(Qt::black);//!!!
Chris@30 489 }
Chris@30 490 }
Chris@30 491
Chris@30 492 paint.drawRect(x, y - h, w, h);
Chris@30 493
Chris@30 494 /// if (p.label != "") {
Chris@30 495 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@30 496 /// }
Chris@30 497 }
Chris@30 498
Chris@30 499 paint.restore();
Chris@30 500 }
Chris@30 501
Chris@30 502 void
Chris@44 503 NoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 504 {
Chris@30 505 std::cerr << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 506
Chris@30 507 if (!m_model) return;
Chris@30 508
Chris@44 509 long frame = v->getFrameForX(e->x());
Chris@30 510 if (frame < 0) frame = 0;
Chris@30 511 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 512
Chris@44 513 float value = getValueForY(v, e->y());
Chris@30 514
Chris@30 515 m_editingPoint = NoteModel::Point(frame, value, 0, tr("New Point"));
Chris@30 516 m_originalPoint = m_editingPoint;
Chris@30 517
Chris@30 518 if (m_editingCommand) m_editingCommand->finish();
Chris@30 519 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 520 tr("Draw Point"));
Chris@30 521 m_editingCommand->addPoint(m_editingPoint);
Chris@30 522
Chris@30 523 m_editing = true;
Chris@30 524 }
Chris@30 525
Chris@30 526 void
Chris@44 527 NoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 528 {
Chris@30 529 std::cerr << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 530
Chris@30 531 if (!m_model || !m_editing) return;
Chris@30 532
Chris@44 533 long frame = v->getFrameForX(e->x());
Chris@30 534 if (frame < 0) frame = 0;
Chris@30 535 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 536
Chris@44 537 float value = getValueForY(v, e->y());
Chris@30 538
Chris@30 539 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 540 m_editingPoint.frame = frame;
Chris@30 541 m_editingPoint.value = value;
Chris@30 542 m_editingCommand->addPoint(m_editingPoint);
Chris@30 543 }
Chris@30 544
Chris@30 545 void
Chris@44 546 NoteLayer::drawEnd(View *v, QMouseEvent *e)
Chris@30 547 {
Chris@30 548 std::cerr << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 549 if (!m_model || !m_editing) return;
Chris@30 550 m_editingCommand->finish();
Chris@30 551 m_editingCommand = 0;
Chris@30 552 m_editing = false;
Chris@30 553 }
Chris@30 554
Chris@30 555 void
Chris@44 556 NoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 557 {
Chris@30 558 std::cerr << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 559
Chris@30 560 if (!m_model) return;
Chris@30 561
Chris@44 562 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@30 563 if (points.empty()) return;
Chris@30 564
Chris@30 565 m_editingPoint = *points.begin();
Chris@30 566 m_originalPoint = m_editingPoint;
Chris@30 567
Chris@30 568 if (m_editingCommand) {
Chris@30 569 m_editingCommand->finish();
Chris@30 570 m_editingCommand = 0;
Chris@30 571 }
Chris@30 572
Chris@30 573 m_editing = true;
Chris@30 574 }
Chris@30 575
Chris@30 576 void
Chris@44 577 NoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 578 {
Chris@30 579 std::cerr << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 580
Chris@30 581 if (!m_model || !m_editing) return;
Chris@30 582
Chris@44 583 long frame = v->getFrameForX(e->x());
Chris@30 584 if (frame < 0) frame = 0;
Chris@30 585 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 586
Chris@44 587 float value = getValueForY(v, e->y());
Chris@30 588
Chris@30 589 if (!m_editingCommand) {
Chris@30 590 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 591 tr("Drag Point"));
Chris@30 592 }
Chris@30 593
Chris@30 594 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 595 m_editingPoint.frame = frame;
Chris@30 596 m_editingPoint.value = value;
Chris@30 597 m_editingCommand->addPoint(m_editingPoint);
Chris@30 598 }
Chris@30 599
Chris@30 600 void
Chris@44 601 NoteLayer::editEnd(View *v, QMouseEvent *e)
Chris@30 602 {
Chris@30 603 std::cerr << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 604 if (!m_model || !m_editing) return;
Chris@30 605
Chris@30 606 if (m_editingCommand) {
Chris@30 607
Chris@30 608 QString newName = m_editingCommand->getName();
Chris@30 609
Chris@30 610 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@30 611 if (m_editingPoint.value != m_originalPoint.value) {
Chris@30 612 newName = tr("Edit Point");
Chris@30 613 } else {
Chris@30 614 newName = tr("Relocate Point");
Chris@30 615 }
Chris@30 616 } else {
Chris@30 617 newName = tr("Change Point Value");
Chris@30 618 }
Chris@30 619
Chris@30 620 m_editingCommand->setName(newName);
Chris@30 621 m_editingCommand->finish();
Chris@30 622 }
Chris@30 623
Chris@30 624 m_editingCommand = 0;
Chris@30 625 m_editing = false;
Chris@30 626 }
Chris@30 627
Chris@43 628 void
Chris@43 629 NoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 630 {
Chris@43 631 NoteModel::EditCommand *command =
Chris@43 632 new NoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 633
Chris@43 634 NoteModel::PointList points =
Chris@43 635 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 636
Chris@43 637 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 638 i != points.end(); ++i) {
Chris@43 639
Chris@43 640 if (s.contains(i->frame)) {
Chris@43 641 NoteModel::Point newPoint(*i);
Chris@43 642 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 643 command->deletePoint(*i);
Chris@43 644 command->addPoint(newPoint);
Chris@43 645 }
Chris@43 646 }
Chris@43 647
Chris@43 648 command->finish();
Chris@43 649 }
Chris@43 650
Chris@43 651 void
Chris@43 652 NoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 653 {
Chris@43 654 NoteModel::EditCommand *command =
Chris@43 655 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 656
Chris@43 657 NoteModel::PointList points =
Chris@43 658 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 659
Chris@43 660 double ratio =
Chris@43 661 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 662 double(s.getEndFrame() - s.getStartFrame());
Chris@43 663
Chris@43 664 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 665 i != points.end(); ++i) {
Chris@43 666
Chris@43 667 if (s.contains(i->frame)) {
Chris@43 668
Chris@43 669 double targetStart = i->frame;
Chris@43 670 targetStart = newSize.getStartFrame() +
Chris@43 671 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 672
Chris@43 673 double targetEnd = i->frame + i->duration;
Chris@43 674 targetEnd = newSize.getStartFrame() +
Chris@43 675 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 676
Chris@43 677 NoteModel::Point newPoint(*i);
Chris@43 678 newPoint.frame = lrint(targetStart);
Chris@43 679 newPoint.duration = lrint(targetEnd - targetStart);
Chris@43 680 command->deletePoint(*i);
Chris@43 681 command->addPoint(newPoint);
Chris@43 682 }
Chris@43 683 }
Chris@43 684
Chris@43 685 command->finish();
Chris@43 686 }
Chris@43 687
Chris@30 688 QString
Chris@30 689 NoteLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@30 690 {
Chris@30 691 return Layer::toXmlString(indent, extraAttributes +
Chris@30 692 QString(" colour=\"%1\" verticalScale=\"%2\"")
Chris@30 693 .arg(encodeColour(m_colour)).arg(m_verticalScale));
Chris@30 694 }
Chris@30 695
Chris@30 696 void
Chris@30 697 NoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 698 {
Chris@30 699 QString colourSpec = attributes.value("colour");
Chris@30 700 if (colourSpec != "") {
Chris@30 701 QColor colour(colourSpec);
Chris@30 702 if (colour.isValid()) {
Chris@30 703 setBaseColour(QColor(colourSpec));
Chris@30 704 }
Chris@30 705 }
Chris@30 706
Chris@30 707 bool ok;
Chris@30 708 VerticalScale scale = (VerticalScale)
Chris@30 709 attributes.value("verticalScale").toInt(&ok);
Chris@30 710 if (ok) setVerticalScale(scale);
Chris@30 711 }
Chris@30 712
Chris@30 713