annotate layer/NoteLayer.cpp @ 697:26b1ffe412f9

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