annotate layer/NoteLayer.cpp @ 666:12e6efc8cdc0

Merge
author Chris Cannam
date Tue, 16 Jul 2013 13:43:01 +0100
parents ab9fefdaa103
children 2727bc3aab59 123ebecd12e6
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@128 18 #include "data/model/Model.h"
Chris@30 19 #include "base/RealTime.h"
Chris@30 20 #include "base/Profiler.h"
Chris@30 21 #include "base/Pitch.h"
Chris@197 22 #include "base/LogRange.h"
Chris@439 23 #include "base/RangeMapper.h"
Chris@376 24 #include "ColourDatabase.h"
Chris@128 25 #include "view/View.h"
Chris@30 26
Chris@128 27 #include "data/model/NoteModel.h"
Chris@30 28
Chris@70 29 #include "widgets/ItemEditDialog.h"
Chris@70 30
Chris@30 31 #include <QPainter>
Chris@30 32 #include <QPainterPath>
Chris@30 33 #include <QMouseEvent>
Chris@316 34 #include <QTextStream>
Chris@360 35 #include <QMessageBox>
Chris@30 36
Chris@30 37 #include <iostream>
Chris@30 38 #include <cmath>
Chris@551 39 #include <utility>
Chris@30 40
Chris@665 41 //#define DEBUG_NOTE_LAYER 1
Chris@665 42
Chris@44 43 NoteLayer::NoteLayer() :
Chris@287 44 SingleColourLayer(),
Chris@30 45 m_model(0),
Chris@30 46 m_editing(false),
Chris@335 47 m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
Chris@335 48 m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
Chris@30 49 m_editingCommand(0),
Chris@439 50 m_verticalScale(AutoAlignScale),
Chris@439 51 m_scaleMinimum(0),
Chris@439 52 m_scaleMaximum(0)
Chris@30 53 {
Chris@44 54
Chris@30 55 }
Chris@30 56
Chris@30 57 void
Chris@30 58 NoteLayer::setModel(NoteModel *model)
Chris@30 59 {
Chris@30 60 if (m_model == model) return;
Chris@30 61 m_model = model;
Chris@30 62
Chris@320 63 connectSignals(m_model);
Chris@30 64
Chris@587 65 // SVDEBUG << "NoteLayer::setModel(" << model << ")" << endl;
Chris@30 66
Chris@439 67 m_scaleMinimum = 0;
Chris@439 68 m_scaleMaximum = 0;
Chris@439 69
Chris@30 70 emit modelReplaced();
Chris@30 71 }
Chris@30 72
Chris@30 73 Layer::PropertyList
Chris@30 74 NoteLayer::getProperties() const
Chris@30 75 {
Chris@287 76 PropertyList list = SingleColourLayer::getProperties();
Chris@87 77 list.push_back("Vertical Scale");
Chris@100 78 list.push_back("Scale Units");
Chris@30 79 return list;
Chris@30 80 }
Chris@30 81
Chris@87 82 QString
Chris@87 83 NoteLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 84 {
Chris@87 85 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@116 86 if (name == "Scale Units") return tr("Scale Units");
Chris@287 87 return SingleColourLayer::getPropertyLabel(name);
Chris@87 88 }
Chris@87 89
Chris@30 90 Layer::PropertyType
Chris@100 91 NoteLayer::getPropertyType(const PropertyName &name) const
Chris@30 92 {
Chris@100 93 if (name == "Scale Units") return UnitsProperty;
Chris@287 94 if (name == "Vertical Scale") return ValueProperty;
Chris@287 95 return SingleColourLayer::getPropertyType(name);
Chris@30 96 }
Chris@30 97
Chris@198 98 QString
Chris@198 99 NoteLayer::getPropertyGroupName(const PropertyName &name) const
Chris@198 100 {
Chris@198 101 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@198 102 return tr("Scale");
Chris@198 103 }
Chris@287 104 return SingleColourLayer::getPropertyGroupName(name);
Chris@198 105 }
Chris@198 106
Chris@30 107 int
Chris@30 108 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 109 int *min, int *max, int *deflt) const
Chris@30 110 {
Chris@216 111 int val = 0;
Chris@30 112
Chris@287 113 if (name == "Vertical Scale") {
Chris@30 114
Chris@30 115 if (min) *min = 0;
Chris@101 116 if (max) *max = 3;
Chris@216 117 if (deflt) *deflt = int(AutoAlignScale);
Chris@30 118
Chris@216 119 val = int(m_verticalScale);
Chris@30 120
Chris@100 121 } else if (name == "Scale Units") {
Chris@100 122
Chris@216 123 if (deflt) *deflt = 0;
Chris@100 124 if (m_model) {
Chris@216 125 val = UnitDatabase::getInstance()->getUnitId
Chris@100 126 (m_model->getScaleUnits());
Chris@100 127 }
Chris@100 128
Chris@30 129 } else {
Chris@216 130
Chris@287 131 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@30 132 }
Chris@30 133
Chris@216 134 return val;
Chris@30 135 }
Chris@30 136
Chris@30 137 QString
Chris@30 138 NoteLayer::getPropertyValueLabel(const PropertyName &name,
Chris@287 139 int value) const
Chris@30 140 {
Chris@287 141 if (name == "Vertical Scale") {
Chris@30 142 switch (value) {
Chris@30 143 default:
Chris@101 144 case 0: return tr("Auto-Align");
Chris@198 145 case 1: return tr("Linear");
Chris@198 146 case 2: return tr("Log");
Chris@198 147 case 3: return tr("MIDI Notes");
Chris@30 148 }
Chris@30 149 }
Chris@287 150 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@30 151 }
Chris@30 152
Chris@30 153 void
Chris@30 154 NoteLayer::setProperty(const PropertyName &name, int value)
Chris@30 155 {
Chris@287 156 if (name == "Vertical Scale") {
Chris@30 157 setVerticalScale(VerticalScale(value));
Chris@100 158 } else if (name == "Scale Units") {
Chris@100 159 if (m_model) {
Chris@100 160 m_model->setScaleUnits
Chris@100 161 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 162 emit modelChanged();
Chris@100 163 }
Chris@287 164 } else {
Chris@287 165 return SingleColourLayer::setProperty(name, value);
Chris@30 166 }
Chris@30 167 }
Chris@30 168
Chris@30 169 void
Chris@30 170 NoteLayer::setVerticalScale(VerticalScale scale)
Chris@30 171 {
Chris@30 172 if (m_verticalScale == scale) return;
Chris@30 173 m_verticalScale = scale;
Chris@30 174 emit layerParametersChanged();
Chris@30 175 }
Chris@30 176
Chris@30 177 bool
Chris@44 178 NoteLayer::isLayerScrollable(const View *v) const
Chris@30 179 {
Chris@30 180 QPoint discard;
Chris@44 181 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@30 182 }
Chris@30 183
Chris@79 184 bool
Chris@101 185 NoteLayer::shouldConvertMIDIToHz() const
Chris@101 186 {
Chris@101 187 QString unit = m_model->getScaleUnits();
Chris@101 188 return (unit != "Hz");
Chris@101 189 // if (unit == "" ||
Chris@101 190 // unit.startsWith("MIDI") ||
Chris@101 191 // unit.startsWith("midi")) return true;
Chris@101 192 // return false;
Chris@101 193 }
Chris@101 194
Chris@101 195 bool
Chris@101 196 NoteLayer::getValueExtents(float &min, float &max,
Chris@101 197 bool &logarithmic, QString &unit) const
Chris@79 198 {
Chris@79 199 if (!m_model) return false;
Chris@79 200 min = m_model->getValueMinimum();
Chris@79 201 max = m_model->getValueMaximum();
Chris@101 202
Chris@105 203 if (shouldConvertMIDIToHz()) {
Chris@105 204 unit = "Hz";
Chris@105 205 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@105 206 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@105 207 } else unit = m_model->getScaleUnits();
Chris@101 208
Chris@101 209 if (m_verticalScale == MIDIRangeScale ||
Chris@101 210 m_verticalScale == LogScale) logarithmic = true;
Chris@101 211
Chris@101 212 return true;
Chris@101 213 }
Chris@101 214
Chris@101 215 bool
Chris@101 216 NoteLayer::getDisplayExtents(float &min, float &max) const
Chris@101 217 {
Chris@439 218 if (!m_model || shouldAutoAlign()) return false;
Chris@101 219
Chris@101 220 if (m_verticalScale == MIDIRangeScale) {
Chris@101 221 min = Pitch::getFrequencyForPitch(0);
Chris@101 222 max = Pitch::getFrequencyForPitch(127);
Chris@101 223 return true;
Chris@101 224 }
Chris@101 225
Chris@439 226 if (m_scaleMinimum == m_scaleMaximum) {
Chris@455 227 min = m_model->getValueMinimum();
Chris@455 228 max = m_model->getValueMaximum();
Chris@455 229 } else {
Chris@455 230 min = m_scaleMinimum;
Chris@455 231 max = m_scaleMaximum;
Chris@439 232 }
Chris@439 233
Chris@101 234 if (shouldConvertMIDIToHz()) {
Chris@101 235 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 236 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 237 }
Chris@101 238
Chris@79 239 return true;
Chris@79 240 }
Chris@79 241
Chris@439 242 bool
Chris@439 243 NoteLayer::setDisplayExtents(float min, float max)
Chris@439 244 {
Chris@439 245 if (!m_model) return false;
Chris@439 246
Chris@439 247 if (min == max) {
Chris@439 248 if (min == 0.f) {
Chris@439 249 max = 1.f;
Chris@439 250 } else {
Chris@439 251 max = min * 1.0001;
Chris@439 252 }
Chris@439 253 }
Chris@439 254
Chris@439 255 m_scaleMinimum = min;
Chris@439 256 m_scaleMaximum = max;
Chris@439 257
Chris@587 258 // SVDEBUG << "NoteLayer::setDisplayExtents: min = " << min << ", max = " << max << endl;
Chris@439 259
Chris@439 260 emit layerParametersChanged();
Chris@439 261 return true;
Chris@439 262 }
Chris@439 263
Chris@439 264 int
Chris@439 265 NoteLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@439 266 {
Chris@439 267 if (shouldAutoAlign()) return 0;
Chris@439 268 if (!m_model) return 0;
Chris@439 269
Chris@439 270 defaultStep = 0;
Chris@439 271 return 100;
Chris@439 272 }
Chris@439 273
Chris@439 274 int
Chris@439 275 NoteLayer::getCurrentVerticalZoomStep() const
Chris@439 276 {
Chris@439 277 if (shouldAutoAlign()) return 0;
Chris@439 278 if (!m_model) return 0;
Chris@439 279
Chris@439 280 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 281 if (!mapper) return 0;
Chris@439 282
Chris@439 283 float dmin, dmax;
Chris@439 284 getDisplayExtents(dmin, dmax);
Chris@439 285
Chris@439 286 int nr = mapper->getPositionForValue(dmax - dmin);
Chris@439 287
Chris@439 288 delete mapper;
Chris@439 289
Chris@439 290 return 100 - nr;
Chris@439 291 }
Chris@439 292
Chris@439 293 //!!! lots of duplication with TimeValueLayer
Chris@439 294
Chris@439 295 void
Chris@439 296 NoteLayer::setVerticalZoomStep(int step)
Chris@439 297 {
Chris@439 298 if (shouldAutoAlign()) return;
Chris@439 299 if (!m_model) return;
Chris@439 300
Chris@439 301 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 302 if (!mapper) return;
Chris@439 303
Chris@439 304 float min, max;
Chris@439 305 bool logarithmic;
Chris@439 306 QString unit;
Chris@439 307 getValueExtents(min, max, logarithmic, unit);
Chris@439 308
Chris@439 309 float dmin, dmax;
Chris@439 310 getDisplayExtents(dmin, dmax);
Chris@439 311
Chris@439 312 float newdist = mapper->getValueForPosition(100 - step);
Chris@439 313
Chris@439 314 float newmin, newmax;
Chris@439 315
Chris@439 316 if (logarithmic) {
Chris@439 317
Chris@439 318 // see SpectrogramLayer::setVerticalZoomStep
Chris@439 319
Chris@439 320 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@439 321 newmin = newmax - newdist;
Chris@439 322
Chris@439 323 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
Chris@439 324
Chris@439 325 } else {
Chris@439 326 float dmid = (dmax + dmin) / 2;
Chris@439 327 newmin = dmid - newdist / 2;
Chris@439 328 newmax = dmid + newdist / 2;
Chris@439 329 }
Chris@439 330
Chris@439 331 if (newmin < min) {
Chris@439 332 newmax += (min - newmin);
Chris@439 333 newmin = min;
Chris@439 334 }
Chris@439 335 if (newmax > max) {
Chris@439 336 newmax = max;
Chris@439 337 }
Chris@439 338
Chris@587 339 SVDEBUG << "NoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@439 340
Chris@439 341 setDisplayExtents(newmin, newmax);
Chris@439 342 }
Chris@439 343
Chris@439 344 RangeMapper *
Chris@439 345 NoteLayer::getNewVerticalZoomRangeMapper() const
Chris@439 346 {
Chris@439 347 if (!m_model) return 0;
Chris@439 348
Chris@439 349 RangeMapper *mapper;
Chris@439 350
Chris@439 351 float min, max;
Chris@439 352 bool logarithmic;
Chris@439 353 QString unit;
Chris@439 354 getValueExtents(min, max, logarithmic, unit);
Chris@439 355
Chris@439 356 if (min == max) return 0;
Chris@439 357
Chris@439 358 if (logarithmic) {
Chris@439 359 mapper = new LogRangeMapper(0, 100, min, max, unit);
Chris@439 360 } else {
Chris@439 361 mapper = new LinearRangeMapper(0, 100, min, max, unit);
Chris@439 362 }
Chris@439 363
Chris@439 364 return mapper;
Chris@439 365 }
Chris@439 366
Chris@30 367 NoteModel::PointList
Chris@44 368 NoteLayer::getLocalPoints(View *v, int x) const
Chris@30 369 {
Chris@30 370 if (!m_model) return NoteModel::PointList();
Chris@30 371
Chris@44 372 long frame = v->getFrameForX(x);
Chris@30 373
Chris@30 374 NoteModel::PointList onPoints =
Chris@30 375 m_model->getPoints(frame);
Chris@30 376
Chris@30 377 if (!onPoints.empty()) {
Chris@30 378 return onPoints;
Chris@30 379 }
Chris@30 380
Chris@30 381 NoteModel::PointList prevPoints =
Chris@30 382 m_model->getPreviousPoints(frame);
Chris@30 383 NoteModel::PointList nextPoints =
Chris@30 384 m_model->getNextPoints(frame);
Chris@30 385
Chris@30 386 NoteModel::PointList usePoints = prevPoints;
Chris@30 387
Chris@30 388 if (prevPoints.empty()) {
Chris@30 389 usePoints = nextPoints;
Chris@248 390 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
Chris@44 391 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@30 392 usePoints = nextPoints;
Chris@248 393 } else if (long(nextPoints.begin()->frame) - frame <
Chris@248 394 frame - long(prevPoints.begin()->frame)) {
Chris@30 395 usePoints = nextPoints;
Chris@30 396 }
Chris@30 397
Chris@30 398 if (!usePoints.empty()) {
Chris@30 399 int fuzz = 2;
Chris@44 400 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@30 401 if ((px > x && px - x > fuzz) ||
Chris@30 402 (px < x && x - px > fuzz + 1)) {
Chris@30 403 usePoints.clear();
Chris@30 404 }
Chris@30 405 }
Chris@30 406
Chris@30 407 return usePoints;
Chris@30 408 }
Chris@30 409
Chris@550 410 bool
Chris@550 411 NoteLayer::getPointToDrag(View *v, int x, int y, NoteModel::Point &p) const
Chris@550 412 {
Chris@550 413 if (!m_model) return false;
Chris@550 414
Chris@550 415 long frame = v->getFrameForX(x);
Chris@550 416
Chris@550 417 NoteModel::PointList onPoints = m_model->getPoints(frame);
Chris@550 418 if (onPoints.empty()) return false;
Chris@550 419
Chris@551 420 // std::cerr << "frame " << frame << ": " << onPoints.size() << " candidate points" << std::endl;
Chris@550 421
Chris@550 422 int nearestDistance = -1;
Chris@550 423
Chris@550 424 for (NoteModel::PointList::const_iterator i = onPoints.begin();
Chris@550 425 i != onPoints.end(); ++i) {
Chris@550 426
Chris@550 427 int distance = getYForValue(v, (*i).value) - y;
Chris@550 428 if (distance < 0) distance = -distance;
Chris@550 429 if (nearestDistance == -1 || distance < nearestDistance) {
Chris@550 430 nearestDistance = distance;
Chris@550 431 p = *i;
Chris@550 432 }
Chris@550 433 }
Chris@550 434
Chris@550 435 return true;
Chris@550 436 }
Chris@550 437
Chris@30 438 QString
Chris@44 439 NoteLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@30 440 {
Chris@30 441 int x = pos.x();
Chris@30 442
Chris@30 443 if (!m_model || !m_model->getSampleRate()) return "";
Chris@30 444
Chris@44 445 NoteModel::PointList points = getLocalPoints(v, x);
Chris@30 446
Chris@30 447 if (points.empty()) {
Chris@30 448 if (!m_model->isReady()) {
Chris@30 449 return tr("In progress");
Chris@30 450 } else {
Chris@30 451 return tr("No local points");
Chris@30 452 }
Chris@30 453 }
Chris@30 454
Chris@30 455 Note note(0);
Chris@30 456 NoteModel::PointList::iterator i;
Chris@30 457
Chris@30 458 for (i = points.begin(); i != points.end(); ++i) {
Chris@30 459
Chris@44 460 int y = getYForValue(v, i->value);
Chris@30 461 int h = 3;
Chris@30 462
Chris@30 463 if (m_model->getValueQuantization() != 0.0) {
Chris@44 464 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
Chris@30 465 if (h < 3) h = 3;
Chris@30 466 }
Chris@30 467
Chris@30 468 if (pos.y() >= y - h && pos.y() <= y) {
Chris@30 469 note = *i;
Chris@30 470 break;
Chris@30 471 }
Chris@30 472 }
Chris@30 473
Chris@30 474 if (i == points.end()) return tr("No local points");
Chris@30 475
Chris@30 476 RealTime rt = RealTime::frame2RealTime(note.frame,
Chris@30 477 m_model->getSampleRate());
Chris@30 478 RealTime rd = RealTime::frame2RealTime(note.duration,
Chris@30 479 m_model->getSampleRate());
Chris@30 480
Chris@101 481 QString pitchText;
Chris@101 482
Chris@101 483 if (shouldConvertMIDIToHz()) {
Chris@101 484
Chris@101 485 int mnote = lrintf(note.value);
Chris@101 486 int cents = lrintf((note.value - mnote) * 100);
Chris@101 487 float freq = Pitch::getFrequencyForPitch(mnote, cents);
Chris@544 488 pitchText = tr("%1 (%2, %3 Hz)")
Chris@544 489 .arg(Pitch::getPitchLabel(mnote, cents))
Chris@544 490 .arg(mnote)
Chris@544 491 .arg(freq);
Chris@101 492
Chris@101 493 } else if (m_model->getScaleUnits() == "Hz") {
Chris@101 494
Chris@544 495 pitchText = tr("%1 Hz (%2, %3)")
Chris@101 496 .arg(note.value)
Chris@544 497 .arg(Pitch::getPitchLabelForFrequency(note.value))
Chris@544 498 .arg(Pitch::getPitchForFrequency(note.value));
Chris@101 499
Chris@101 500 } else {
Chris@234 501 pitchText = tr("%1 %2")
Chris@101 502 .arg(note.value).arg(m_model->getScaleUnits());
Chris@101 503 }
Chris@101 504
Chris@30 505 QString text;
Chris@30 506
Chris@30 507 if (note.label == "") {
Chris@30 508 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
Chris@30 509 .arg(rt.toText(true).c_str())
Chris@101 510 .arg(pitchText)
Chris@30 511 .arg(rd.toText(true).c_str());
Chris@30 512 } else {
Chris@30 513 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@30 514 .arg(rt.toText(true).c_str())
Chris@101 515 .arg(pitchText)
Chris@30 516 .arg(rd.toText(true).c_str())
Chris@30 517 .arg(note.label);
Chris@30 518 }
Chris@30 519
Chris@44 520 pos = QPoint(v->getXForFrame(note.frame),
Chris@44 521 getYForValue(v, note.value));
Chris@30 522 return text;
Chris@30 523 }
Chris@30 524
Chris@30 525 bool
Chris@44 526 NoteLayer::snapToFeatureFrame(View *v, int &frame,
Chris@44 527 size_t &resolution,
Chris@44 528 SnapType snap) const
Chris@30 529 {
Chris@30 530 if (!m_model) {
Chris@44 531 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@30 532 }
Chris@30 533
Chris@30 534 resolution = m_model->getResolution();
Chris@30 535 NoteModel::PointList points;
Chris@30 536
Chris@30 537 if (snap == SnapNeighbouring) {
Chris@30 538
Chris@44 539 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@30 540 if (points.empty()) return false;
Chris@30 541 frame = points.begin()->frame;
Chris@30 542 return true;
Chris@30 543 }
Chris@30 544
Chris@30 545 points = m_model->getPoints(frame, frame);
Chris@30 546 int snapped = frame;
Chris@30 547 bool found = false;
Chris@30 548
Chris@30 549 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 550 i != points.end(); ++i) {
Chris@30 551
Chris@30 552 if (snap == SnapRight) {
Chris@30 553
Chris@30 554 if (i->frame > frame) {
Chris@30 555 snapped = i->frame;
Chris@30 556 found = true;
Chris@30 557 break;
Chris@30 558 }
Chris@30 559
Chris@30 560 } else if (snap == SnapLeft) {
Chris@30 561
Chris@30 562 if (i->frame <= frame) {
Chris@30 563 snapped = i->frame;
Chris@30 564 found = true; // don't break, as the next may be better
Chris@30 565 } else {
Chris@30 566 break;
Chris@30 567 }
Chris@30 568
Chris@30 569 } else { // nearest
Chris@30 570
Chris@30 571 NoteModel::PointList::const_iterator j = i;
Chris@30 572 ++j;
Chris@30 573
Chris@30 574 if (j == points.end()) {
Chris@30 575
Chris@30 576 snapped = i->frame;
Chris@30 577 found = true;
Chris@30 578 break;
Chris@30 579
Chris@30 580 } else if (j->frame >= frame) {
Chris@30 581
Chris@30 582 if (j->frame - frame < frame - i->frame) {
Chris@30 583 snapped = j->frame;
Chris@30 584 } else {
Chris@30 585 snapped = i->frame;
Chris@30 586 }
Chris@30 587 found = true;
Chris@30 588 break;
Chris@30 589 }
Chris@30 590 }
Chris@30 591 }
Chris@30 592
Chris@30 593 frame = snapped;
Chris@30 594 return found;
Chris@30 595 }
Chris@30 596
Chris@101 597 void
Chris@101 598 NoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@30 599 {
Chris@101 600 min = 0.0;
Chris@101 601 max = 0.0;
Chris@101 602 log = false;
Chris@42 603
Chris@101 604 QString queryUnits;
Chris@101 605 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
Chris@101 606 else queryUnits = m_model->getScaleUnits();
Chris@30 607
Chris@439 608 if (shouldAutoAlign()) {
Chris@30 609
Chris@101 610 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@30 611
Chris@101 612 min = m_model->getValueMinimum();
Chris@101 613 max = m_model->getValueMaximum();
Chris@42 614
Chris@101 615 if (shouldConvertMIDIToHz()) {
Chris@101 616 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 617 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 618 }
Chris@42 619
Chris@665 620 #ifdef DEBUG_NOTE_LAYER
Chris@105 621 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@665 622 #endif
Chris@105 623
Chris@101 624 } else if (log) {
Chris@101 625
Chris@197 626 LogRange::mapRange(min, max);
Chris@105 627
Chris@665 628 #ifdef DEBUG_NOTE_LAYER
Chris@105 629 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@665 630 #endif
Chris@105 631
Chris@101 632 }
Chris@101 633
Chris@101 634 } else {
Chris@101 635
Chris@439 636 getDisplayExtents(min, max);
Chris@101 637
Chris@101 638 if (m_verticalScale == MIDIRangeScale) {
Chris@101 639 min = Pitch::getFrequencyForPitch(0);
Chris@101 640 max = Pitch::getFrequencyForPitch(127);
Chris@101 641 } else if (shouldConvertMIDIToHz()) {
Chris@101 642 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 643 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 644 }
Chris@101 645
Chris@101 646 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@197 647 LogRange::mapRange(min, max);
Chris@101 648 log = true;
Chris@101 649 }
Chris@30 650 }
Chris@30 651
Chris@101 652 if (max == min) max = min + 1.0;
Chris@101 653 }
Chris@30 654
Chris@101 655 int
Chris@101 656 NoteLayer::getYForValue(View *v, float val) const
Chris@101 657 {
Chris@101 658 float min = 0.0, max = 0.0;
Chris@101 659 bool logarithmic = false;
Chris@101 660 int h = v->height();
Chris@101 661
Chris@101 662 getScaleExtents(v, min, max, logarithmic);
Chris@101 663
Chris@665 664 #ifdef DEBUG_NOTE_LAYER
Chris@665 665 std::cerr << "NoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl;
Chris@665 666 #endif
Chris@101 667
Chris@101 668 if (shouldConvertMIDIToHz()) {
Chris@101 669 val = Pitch::getFrequencyForPitch(lrintf(val),
Chris@101 670 lrintf((val - lrintf(val)) * 100));
Chris@665 671 #ifdef DEBUG_NOTE_LAYER
Chris@665 672 std::cerr << "shouldConvertMIDIToHz true, val now = " << val << std::endl;
Chris@665 673 #endif
Chris@101 674 }
Chris@101 675
Chris@101 676 if (logarithmic) {
Chris@197 677 val = LogRange::map(val);
Chris@665 678 #ifdef DEBUG_NOTE_LAYER
Chris@665 679 std::cerr << "logarithmic true, val now = " << val << std::endl;
Chris@665 680 #endif
Chris@101 681 }
Chris@101 682
Chris@101 683 int y = int(h - ((val - min) * h) / (max - min)) - 1;
Chris@665 684 #ifdef DEBUG_NOTE_LAYER
Chris@665 685 std::cerr << "y = " << y << std::endl;
Chris@665 686 #endif
Chris@101 687 return y;
Chris@30 688 }
Chris@30 689
Chris@30 690 float
Chris@44 691 NoteLayer::getValueForY(View *v, int y) const
Chris@30 692 {
Chris@101 693 float min = 0.0, max = 0.0;
Chris@101 694 bool logarithmic = false;
Chris@44 695 int h = v->height();
Chris@30 696
Chris@101 697 getScaleExtents(v, min, max, logarithmic);
Chris@101 698
Chris@101 699 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 700
Chris@101 701 if (logarithmic) {
Chris@197 702 val = powf(10.f, val);
Chris@101 703 }
Chris@101 704
Chris@101 705 if (shouldConvertMIDIToHz()) {
Chris@101 706 val = Pitch::getPitchForFrequency(val);
Chris@101 707 }
Chris@101 708
Chris@101 709 return val;
Chris@30 710 }
Chris@30 711
Chris@439 712 bool
Chris@439 713 NoteLayer::shouldAutoAlign() const
Chris@439 714 {
Chris@439 715 if (!m_model) return false;
Chris@439 716 return (m_verticalScale == AutoAlignScale);
Chris@439 717 }
Chris@439 718
Chris@30 719 void
Chris@44 720 NoteLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@30 721 {
Chris@30 722 if (!m_model || !m_model->isOK()) return;
Chris@30 723
Chris@30 724 int sampleRate = m_model->getSampleRate();
Chris@30 725 if (!sampleRate) return;
Chris@30 726
Chris@30 727 // Profiler profiler("NoteLayer::paint", true);
Chris@30 728
Chris@30 729 int x0 = rect.left(), x1 = rect.right();
Chris@44 730 long frame0 = v->getFrameForX(x0);
Chris@44 731 long frame1 = v->getFrameForX(x1);
Chris@30 732
Chris@30 733 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
Chris@30 734 if (points.empty()) return;
Chris@30 735
Chris@287 736 paint.setPen(getBaseQColor());
Chris@30 737
Chris@287 738 QColor brushColour(getBaseQColor());
Chris@30 739 brushColour.setAlpha(80);
Chris@30 740
Chris@587 741 // SVDEBUG << "NoteLayer::paint: resolution is "
Chris@585 742 // << m_model->getResolution() << " frames" << endl;
Chris@30 743
Chris@30 744 float min = m_model->getValueMinimum();
Chris@30 745 float max = m_model->getValueMaximum();
Chris@30 746 if (max == min) max = min + 1.0;
Chris@30 747
Chris@30 748 QPoint localPos;
Chris@551 749 NoteModel::Point illuminatePoint(0);
Chris@551 750 bool shouldIlluminate = false;
Chris@30 751
Chris@44 752 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@551 753 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@551 754 illuminatePoint);
Chris@30 755 }
Chris@30 756
Chris@30 757 paint.save();
Chris@30 758 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 759
Chris@30 760 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 761 i != points.end(); ++i) {
Chris@30 762
Chris@30 763 const NoteModel::Point &p(*i);
Chris@30 764
Chris@44 765 int x = v->getXForFrame(p.frame);
Chris@44 766 int y = getYForValue(v, p.value);
Chris@44 767 int w = v->getXForFrame(p.frame + p.duration) - x;
Chris@30 768 int h = 3;
Chris@30 769
Chris@30 770 if (m_model->getValueQuantization() != 0.0) {
Chris@44 771 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
Chris@30 772 if (h < 3) h = 3;
Chris@30 773 }
Chris@30 774
Chris@30 775 if (w < 1) w = 1;
Chris@287 776 paint.setPen(getBaseQColor());
Chris@30 777 paint.setBrush(brushColour);
Chris@30 778
Chris@551 779 if (shouldIlluminate &&
Chris@551 780 // "illuminatePoint == p"
Chris@551 781 !NoteModel::Point::Comparator()(illuminatePoint, p) &&
Chris@551 782 !NoteModel::Point::Comparator()(p, illuminatePoint)) {
Chris@551 783
Chris@551 784 paint.setPen(v->getForeground());
Chris@551 785 paint.setBrush(v->getForeground());
Chris@551 786
Chris@551 787 QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits());
Chris@551 788 v->drawVisibleText(paint,
Chris@551 789 x - paint.fontMetrics().width(vlabel) - 2,
Chris@551 790 y + paint.fontMetrics().height()/2
Chris@551 791 - paint.fontMetrics().descent(),
Chris@551 792 vlabel, View::OutlinedText);
Chris@551 793
Chris@551 794 QString hlabel = RealTime::frame2RealTime
Chris@551 795 (p.frame, m_model->getSampleRate()).toText(true).c_str();
Chris@551 796 v->drawVisibleText(paint,
Chris@551 797 x,
Chris@551 798 y - h/2 - paint.fontMetrics().descent() - 2,
Chris@551 799 hlabel, View::OutlinedText);
Chris@30 800 }
Chris@30 801
Chris@124 802 paint.drawRect(x, y - h/2, w, h);
Chris@30 803 }
Chris@30 804
Chris@30 805 paint.restore();
Chris@30 806 }
Chris@30 807
Chris@30 808 void
Chris@44 809 NoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 810 {
Chris@587 811 // SVDEBUG << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 812
Chris@30 813 if (!m_model) return;
Chris@30 814
Chris@44 815 long frame = v->getFrameForX(e->x());
Chris@30 816 if (frame < 0) frame = 0;
Chris@30 817 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 818
Chris@44 819 float value = getValueForY(v, e->y());
Chris@30 820
Chris@335 821 m_editingPoint = NoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
Chris@30 822 m_originalPoint = m_editingPoint;
Chris@30 823
Chris@376 824 if (m_editingCommand) finish(m_editingCommand);
Chris@30 825 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 826 tr("Draw Point"));
Chris@30 827 m_editingCommand->addPoint(m_editingPoint);
Chris@30 828
Chris@30 829 m_editing = true;
Chris@30 830 }
Chris@30 831
Chris@30 832 void
Chris@44 833 NoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 834 {
Chris@587 835 // SVDEBUG << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 836
Chris@30 837 if (!m_model || !m_editing) return;
Chris@30 838
Chris@44 839 long frame = v->getFrameForX(e->x());
Chris@30 840 if (frame < 0) frame = 0;
Chris@30 841 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 842
Chris@101 843 float newValue = getValueForY(v, e->y());
Chris@101 844
Chris@101 845 long newFrame = m_editingPoint.frame;
Chris@101 846 long newDuration = frame - newFrame;
Chris@101 847 if (newDuration < 0) {
Chris@101 848 newFrame = frame;
Chris@101 849 newDuration = -newDuration;
Chris@101 850 } else if (newDuration == 0) {
Chris@101 851 newDuration = 1;
Chris@101 852 }
Chris@30 853
Chris@30 854 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 855 m_editingPoint.frame = newFrame;
Chris@101 856 m_editingPoint.value = newValue;
Chris@101 857 m_editingPoint.duration = newDuration;
Chris@30 858 m_editingCommand->addPoint(m_editingPoint);
Chris@30 859 }
Chris@30 860
Chris@30 861 void
Chris@248 862 NoteLayer::drawEnd(View *, QMouseEvent *)
Chris@30 863 {
Chris@587 864 // SVDEBUG << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 865 if (!m_model || !m_editing) return;
Chris@376 866 finish(m_editingCommand);
Chris@30 867 m_editingCommand = 0;
Chris@30 868 m_editing = false;
Chris@30 869 }
Chris@30 870
Chris@30 871 void
Chris@335 872 NoteLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 873 {
Chris@335 874 if (!m_model) return;
Chris@335 875
Chris@550 876 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 877
Chris@335 878 if (m_editingCommand) {
Chris@376 879 finish(m_editingCommand);
Chris@335 880 m_editingCommand = 0;
Chris@335 881 }
Chris@335 882
Chris@335 883 m_editing = true;
Chris@335 884 }
Chris@335 885
Chris@335 886 void
Chris@335 887 NoteLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 888 {
Chris@335 889 }
Chris@335 890
Chris@335 891 void
Chris@335 892 NoteLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 893 {
Chris@335 894 if (!m_model || !m_editing) return;
Chris@335 895
Chris@335 896 m_editing = false;
Chris@335 897
Chris@550 898 NoteModel::Point p(0);
Chris@550 899 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@550 900 if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return;
Chris@550 901
Chris@550 902 m_editingCommand = new NoteModel::EditCommand(m_model, tr("Erase Point"));
Chris@335 903
Chris@335 904 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 905
Chris@376 906 finish(m_editingCommand);
Chris@335 907 m_editingCommand = 0;
Chris@335 908 m_editing = false;
Chris@335 909 }
Chris@335 910
Chris@335 911 void
Chris@44 912 NoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 913 {
Chris@587 914 // SVDEBUG << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 915
Chris@30 916 if (!m_model) return;
Chris@30 917
Chris@550 918 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@30 919 m_originalPoint = m_editingPoint;
Chris@30 920
Chris@551 921 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
Chris@551 922 m_dragPointY = getYForValue(v, m_editingPoint.value);
Chris@551 923
Chris@30 924 if (m_editingCommand) {
Chris@376 925 finish(m_editingCommand);
Chris@30 926 m_editingCommand = 0;
Chris@30 927 }
Chris@30 928
Chris@30 929 m_editing = true;
Chris@551 930 m_dragStartX = e->x();
Chris@551 931 m_dragStartY = e->y();
Chris@30 932 }
Chris@30 933
Chris@30 934 void
Chris@44 935 NoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 936 {
Chris@587 937 // SVDEBUG << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 938
Chris@30 939 if (!m_model || !m_editing) return;
Chris@30 940
Chris@551 941 int xdist = e->x() - m_dragStartX;
Chris@551 942 int ydist = e->y() - m_dragStartY;
Chris@551 943 int newx = m_dragPointX + xdist;
Chris@551 944 int newy = m_dragPointY + ydist;
Chris@551 945
Chris@551 946 long frame = v->getFrameForX(newx);
Chris@30 947 if (frame < 0) frame = 0;
Chris@30 948 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 949
Chris@551 950 float value = getValueForY(v, newy);
Chris@30 951
Chris@30 952 if (!m_editingCommand) {
Chris@30 953 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 954 tr("Drag Point"));
Chris@30 955 }
Chris@30 956
Chris@30 957 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 958 m_editingPoint.frame = frame;
Chris@30 959 m_editingPoint.value = value;
Chris@30 960 m_editingCommand->addPoint(m_editingPoint);
Chris@30 961 }
Chris@30 962
Chris@30 963 void
Chris@248 964 NoteLayer::editEnd(View *, QMouseEvent *)
Chris@30 965 {
Chris@587 966 // SVDEBUG << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 967 if (!m_model || !m_editing) return;
Chris@30 968
Chris@30 969 if (m_editingCommand) {
Chris@30 970
Chris@30 971 QString newName = m_editingCommand->getName();
Chris@30 972
Chris@30 973 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@30 974 if (m_editingPoint.value != m_originalPoint.value) {
Chris@30 975 newName = tr("Edit Point");
Chris@30 976 } else {
Chris@30 977 newName = tr("Relocate Point");
Chris@30 978 }
Chris@30 979 } else {
Chris@30 980 newName = tr("Change Point Value");
Chris@30 981 }
Chris@30 982
Chris@30 983 m_editingCommand->setName(newName);
Chris@376 984 finish(m_editingCommand);
Chris@30 985 }
Chris@30 986
Chris@30 987 m_editingCommand = 0;
Chris@30 988 m_editing = false;
Chris@30 989 }
Chris@30 990
Chris@255 991 bool
Chris@70 992 NoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 993 {
Chris@255 994 if (!m_model) return false;
Chris@70 995
Chris@550 996 NoteModel::Point note(0);
Chris@550 997 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 998
Chris@550 999 // NoteModel::Point note = *points.begin();
Chris@70 1000
Chris@70 1001 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1002 (m_model->getSampleRate(),
Chris@70 1003 ItemEditDialog::ShowTime |
Chris@70 1004 ItemEditDialog::ShowDuration |
Chris@70 1005 ItemEditDialog::ShowValue |
Chris@100 1006 ItemEditDialog::ShowText,
Chris@100 1007 m_model->getScaleUnits());
Chris@70 1008
Chris@70 1009 dialog->setFrameTime(note.frame);
Chris@70 1010 dialog->setValue(note.value);
Chris@70 1011 dialog->setFrameDuration(note.duration);
Chris@70 1012 dialog->setText(note.label);
Chris@70 1013
Chris@70 1014 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1015
Chris@70 1016 NoteModel::Point newNote = note;
Chris@70 1017 newNote.frame = dialog->getFrameTime();
Chris@70 1018 newNote.value = dialog->getValue();
Chris@70 1019 newNote.duration = dialog->getFrameDuration();
Chris@70 1020 newNote.label = dialog->getText();
Chris@70 1021
Chris@70 1022 NoteModel::EditCommand *command = new NoteModel::EditCommand
Chris@70 1023 (m_model, tr("Edit Point"));
Chris@70 1024 command->deletePoint(note);
Chris@70 1025 command->addPoint(newNote);
Chris@376 1026 finish(command);
Chris@70 1027 }
Chris@70 1028
Chris@70 1029 delete dialog;
Chris@255 1030 return true;
Chris@70 1031 }
Chris@70 1032
Chris@70 1033 void
Chris@43 1034 NoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1035 {
Chris@99 1036 if (!m_model) return;
Chris@99 1037
Chris@43 1038 NoteModel::EditCommand *command =
Chris@43 1039 new NoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 1040
Chris@43 1041 NoteModel::PointList points =
Chris@43 1042 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1043
Chris@43 1044 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 1045 i != points.end(); ++i) {
Chris@43 1046
Chris@43 1047 if (s.contains(i->frame)) {
Chris@43 1048 NoteModel::Point newPoint(*i);
Chris@43 1049 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1050 command->deletePoint(*i);
Chris@43 1051 command->addPoint(newPoint);
Chris@43 1052 }
Chris@43 1053 }
Chris@43 1054
Chris@376 1055 finish(command);
Chris@43 1056 }
Chris@43 1057
Chris@43 1058 void
Chris@43 1059 NoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1060 {
Chris@99 1061 if (!m_model) return;
Chris@99 1062
Chris@43 1063 NoteModel::EditCommand *command =
Chris@43 1064 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1065
Chris@43 1066 NoteModel::PointList points =
Chris@43 1067 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1068
Chris@43 1069 double ratio =
Chris@43 1070 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1071 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1072
Chris@43 1073 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 1074 i != points.end(); ++i) {
Chris@43 1075
Chris@43 1076 if (s.contains(i->frame)) {
Chris@43 1077
Chris@43 1078 double targetStart = i->frame;
Chris@43 1079 targetStart = newSize.getStartFrame() +
Chris@43 1080 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 1081
Chris@43 1082 double targetEnd = i->frame + i->duration;
Chris@43 1083 targetEnd = newSize.getStartFrame() +
Chris@43 1084 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 1085
Chris@43 1086 NoteModel::Point newPoint(*i);
Chris@43 1087 newPoint.frame = lrint(targetStart);
Chris@43 1088 newPoint.duration = lrint(targetEnd - targetStart);
Chris@43 1089 command->deletePoint(*i);
Chris@43 1090 command->addPoint(newPoint);
Chris@43 1091 }
Chris@43 1092 }
Chris@43 1093
Chris@376 1094 finish(command);
Chris@43 1095 }
Chris@43 1096
Chris@76 1097 void
Chris@76 1098 NoteLayer::deleteSelection(Selection s)
Chris@76 1099 {
Chris@99 1100 if (!m_model) return;
Chris@99 1101
Chris@76 1102 NoteModel::EditCommand *command =
Chris@76 1103 new NoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1104
Chris@76 1105 NoteModel::PointList points =
Chris@76 1106 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1107
Chris@76 1108 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 1109 i != points.end(); ++i) {
Chris@76 1110
Chris@76 1111 if (s.contains(i->frame)) {
Chris@76 1112 command->deletePoint(*i);
Chris@76 1113 }
Chris@76 1114 }
Chris@76 1115
Chris@376 1116 finish(command);
Chris@76 1117 }
Chris@76 1118
Chris@76 1119 void
Chris@359 1120 NoteLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1121 {
Chris@99 1122 if (!m_model) return;
Chris@99 1123
Chris@76 1124 NoteModel::PointList points =
Chris@76 1125 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1126
Chris@76 1127 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 1128 i != points.end(); ++i) {
Chris@76 1129 if (s.contains(i->frame)) {
Chris@335 1130 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
Chris@360 1131 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1132 to.addPoint(point);
Chris@76 1133 }
Chris@76 1134 }
Chris@76 1135 }
Chris@76 1136
Chris@125 1137 bool
Chris@359 1138 NoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
Chris@76 1139 {
Chris@125 1140 if (!m_model) return false;
Chris@99 1141
Chris@76 1142 const Clipboard::PointList &points = from.getPoints();
Chris@76 1143
Chris@360 1144 bool realign = false;
Chris@360 1145
Chris@360 1146 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1147
Chris@360 1148 QMessageBox::StandardButton button =
Chris@360 1149 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1150 tr("The items 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 1151 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1152 QMessageBox::Yes);
Chris@360 1153
Chris@360 1154 if (button == QMessageBox::Cancel) {
Chris@360 1155 return false;
Chris@360 1156 }
Chris@360 1157
Chris@360 1158 if (button == QMessageBox::Yes) {
Chris@360 1159 realign = true;
Chris@360 1160 }
Chris@360 1161 }
Chris@360 1162
Chris@76 1163 NoteModel::EditCommand *command =
Chris@76 1164 new NoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1165
Chris@76 1166 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1167 i != points.end(); ++i) {
Chris@76 1168
Chris@76 1169 if (!i->haveFrame()) continue;
Chris@76 1170 size_t frame = 0;
Chris@360 1171
Chris@360 1172 if (!realign) {
Chris@360 1173
Chris@360 1174 frame = i->getFrame();
Chris@360 1175
Chris@360 1176 } else {
Chris@360 1177
Chris@360 1178 if (i->haveReferenceFrame()) {
Chris@360 1179 frame = i->getReferenceFrame();
Chris@360 1180 frame = alignFromReference(v, frame);
Chris@360 1181 } else {
Chris@360 1182 frame = i->getFrame();
Chris@360 1183 }
Chris@76 1184 }
Chris@360 1185
Chris@76 1186 NoteModel::Point newPoint(frame);
Chris@76 1187
Chris@76 1188 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1189 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1190 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1191 m_model->getValueMaximum()) / 2;
Chris@335 1192 if (i->haveLevel()) newPoint.level = i->getLevel();
Chris@76 1193 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 1194 else {
Chris@125 1195 size_t nextFrame = frame;
Chris@125 1196 Clipboard::PointList::const_iterator j = i;
Chris@125 1197 for (; j != points.end(); ++j) {
Chris@125 1198 if (!j->haveFrame()) continue;
Chris@125 1199 if (j != i) break;
Chris@125 1200 }
Chris@125 1201 if (j != points.end()) {
Chris@125 1202 nextFrame = j->getFrame();
Chris@125 1203 }
Chris@125 1204 if (nextFrame == frame) {
Chris@125 1205 newPoint.duration = m_model->getResolution();
Chris@125 1206 } else {
Chris@125 1207 newPoint.duration = nextFrame - frame;
Chris@125 1208 }
Chris@125 1209 }
Chris@76 1210
Chris@76 1211 command->addPoint(newPoint);
Chris@76 1212 }
Chris@76 1213
Chris@376 1214 finish(command);
Chris@125 1215 return true;
Chris@76 1216 }
Chris@76 1217
Chris@507 1218 void
Chris@507 1219 NoteLayer::addNoteOn(long frame, int pitch, int velocity)
Chris@507 1220 {
Chris@507 1221 m_pendingNoteOns.insert(Note(frame, pitch, 0, float(velocity) / 127.0, ""));
Chris@507 1222 }
Chris@507 1223
Chris@507 1224 void
Chris@507 1225 NoteLayer::addNoteOff(long frame, int pitch)
Chris@507 1226 {
Chris@507 1227 for (NoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1228 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1229 if (lrintf((*i).value) == pitch) {
Chris@507 1230 Note note(*i);
Chris@507 1231 m_pendingNoteOns.erase(i);
Chris@507 1232 note.duration = frame - note.frame;
Chris@507 1233 if (m_model) {
Chris@507 1234 NoteModel::AddPointCommand *c = new NoteModel::AddPointCommand
Chris@507 1235 (m_model, note, tr("Record Note"));
Chris@507 1236 // execute and bundle:
Chris@507 1237 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1238 }
Chris@507 1239 break;
Chris@507 1240 }
Chris@507 1241 }
Chris@507 1242 }
Chris@507 1243
Chris@507 1244 void
Chris@507 1245 NoteLayer::abandonNoteOns()
Chris@507 1246 {
Chris@507 1247 m_pendingNoteOns.clear();
Chris@507 1248 }
Chris@507 1249
Chris@287 1250 int
Chris@287 1251 NoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1252 {
Chris@287 1253 impose = false;
Chris@287 1254 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1255 (QString(darkbg ? "White" : "Black"));
Chris@287 1256 }
Chris@287 1257
Chris@316 1258 void
Chris@316 1259 NoteLayer::toXml(QTextStream &stream,
Chris@316 1260 QString indent, QString extraAttributes) const
Chris@30 1261 {
Chris@316 1262 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1263 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1264 .arg(m_verticalScale)
Chris@445 1265 .arg(m_scaleMinimum)
Chris@445 1266 .arg(m_scaleMaximum));
Chris@30 1267 }
Chris@30 1268
Chris@30 1269 void
Chris@30 1270 NoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1271 {
Chris@287 1272 SingleColourLayer::setProperties(attributes);
Chris@30 1273
Chris@445 1274 bool ok, alsoOk;
Chris@30 1275 VerticalScale scale = (VerticalScale)
Chris@30 1276 attributes.value("verticalScale").toInt(&ok);
Chris@30 1277 if (ok) setVerticalScale(scale);
Chris@445 1278
Chris@445 1279 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1280 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@445 1281 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@30 1282 }
Chris@30 1283
Chris@30 1284