annotate layer/NoteLayer.cpp @ 694:ad12e428785b tonioni

Merge from default branch
author Chris Cannam
date Tue, 03 Dec 2013 17:58:40 +0000
parents 917039d333bb 4dcf10bd2863
children 6d9624e0ac55
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 {
gyorgyf@625 55 SVDEBUG << "constructed NoteLayer" << endl;
Chris@30 56 }
Chris@30 57
Chris@30 58 void
Chris@30 59 NoteLayer::setModel(NoteModel *model)
gyorgyf@626 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@692 820 return 10;
Chris@692 821 }
Chris@692 822
Chris@692 823 void
Chris@692 824 NoteLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
Chris@692 825 {
Chris@692 826 float fmin, fmax;
Chris@692 827 getDisplayExtents(fmin, fmax);
Chris@692 828 PianoScale().paintPianoVertical
Chris@692 829 (v, paint, QRect(0, 0, 10, v->height()), fmin, fmax);
Chris@692 830 paint.drawLine(10, 0, 10, v->height());
Chris@692 831 }
Chris@692 832
Chris@30 833 void
Chris@44 834 NoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 835 {
Chris@587 836 // SVDEBUG << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 837
Chris@30 838 if (!m_model) return;
Chris@30 839
Chris@44 840 long frame = v->getFrameForX(e->x());
Chris@30 841 if (frame < 0) frame = 0;
Chris@30 842 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 843
Chris@44 844 float value = getValueForY(v, e->y());
Chris@30 845
Chris@335 846 m_editingPoint = NoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
Chris@30 847 m_originalPoint = m_editingPoint;
Chris@30 848
Chris@376 849 if (m_editingCommand) finish(m_editingCommand);
Chris@30 850 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 851 tr("Draw Point"));
Chris@30 852 m_editingCommand->addPoint(m_editingPoint);
Chris@30 853
Chris@30 854 m_editing = true;
Chris@30 855 }
Chris@30 856
Chris@30 857 void
Chris@44 858 NoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 859 {
Chris@587 860 // SVDEBUG << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 861
Chris@30 862 if (!m_model || !m_editing) return;
Chris@30 863
Chris@44 864 long frame = v->getFrameForX(e->x());
Chris@30 865 if (frame < 0) frame = 0;
Chris@30 866 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 867
Chris@101 868 float newValue = getValueForY(v, e->y());
Chris@101 869
Chris@101 870 long newFrame = m_editingPoint.frame;
Chris@101 871 long newDuration = frame - newFrame;
Chris@101 872 if (newDuration < 0) {
Chris@101 873 newFrame = frame;
Chris@101 874 newDuration = -newDuration;
Chris@101 875 } else if (newDuration == 0) {
Chris@101 876 newDuration = 1;
Chris@101 877 }
Chris@30 878
Chris@30 879 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 880 m_editingPoint.frame = newFrame;
Chris@101 881 m_editingPoint.value = newValue;
Chris@101 882 m_editingPoint.duration = newDuration;
Chris@30 883 m_editingCommand->addPoint(m_editingPoint);
Chris@30 884 }
Chris@30 885
Chris@30 886 void
Chris@248 887 NoteLayer::drawEnd(View *, QMouseEvent *)
Chris@30 888 {
Chris@587 889 // SVDEBUG << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 890 if (!m_model || !m_editing) return;
Chris@376 891 finish(m_editingCommand);
Chris@30 892 m_editingCommand = 0;
Chris@30 893 m_editing = false;
Chris@30 894 }
Chris@30 895
Chris@30 896 void
Chris@335 897 NoteLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 898 {
Chris@335 899 if (!m_model) return;
Chris@335 900
Chris@550 901 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 902
Chris@335 903 if (m_editingCommand) {
Chris@376 904 finish(m_editingCommand);
Chris@335 905 m_editingCommand = 0;
Chris@335 906 }
Chris@335 907
Chris@335 908 m_editing = true;
Chris@335 909 }
Chris@335 910
Chris@335 911 void
Chris@335 912 NoteLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 913 {
Chris@335 914 }
Chris@335 915
Chris@335 916 void
Chris@335 917 NoteLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 918 {
Chris@335 919 if (!m_model || !m_editing) return;
Chris@335 920
Chris@335 921 m_editing = false;
Chris@335 922
Chris@550 923 NoteModel::Point p(0);
Chris@550 924 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@550 925 if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return;
Chris@550 926
Chris@550 927 m_editingCommand = new NoteModel::EditCommand(m_model, tr("Erase Point"));
Chris@335 928
Chris@335 929 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 930
Chris@376 931 finish(m_editingCommand);
Chris@335 932 m_editingCommand = 0;
Chris@335 933 m_editing = false;
Chris@335 934 }
Chris@335 935
Chris@335 936 void
Chris@44 937 NoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 938 {
Chris@587 939 // SVDEBUG << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 940
Chris@30 941 if (!m_model) return;
Chris@30 942
Chris@550 943 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@30 944 m_originalPoint = m_editingPoint;
Chris@30 945
Chris@551 946 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
Chris@551 947 m_dragPointY = getYForValue(v, m_editingPoint.value);
Chris@551 948
Chris@30 949 if (m_editingCommand) {
Chris@376 950 finish(m_editingCommand);
Chris@30 951 m_editingCommand = 0;
Chris@30 952 }
Chris@30 953
Chris@30 954 m_editing = true;
Chris@551 955 m_dragStartX = e->x();
Chris@551 956 m_dragStartY = e->y();
Chris@30 957 }
Chris@30 958
Chris@30 959 void
Chris@44 960 NoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 961 {
Chris@587 962 // SVDEBUG << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 963
Chris@30 964 if (!m_model || !m_editing) return;
Chris@30 965
Chris@551 966 int xdist = e->x() - m_dragStartX;
Chris@551 967 int ydist = e->y() - m_dragStartY;
Chris@551 968 int newx = m_dragPointX + xdist;
Chris@551 969 int newy = m_dragPointY + ydist;
Chris@551 970
Chris@551 971 long frame = v->getFrameForX(newx);
Chris@30 972 if (frame < 0) frame = 0;
Chris@30 973 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 974
Chris@551 975 float value = getValueForY(v, newy);
Chris@30 976
Chris@30 977 if (!m_editingCommand) {
Chris@30 978 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 979 tr("Drag Point"));
Chris@30 980 }
Chris@30 981
Chris@30 982 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 983 m_editingPoint.frame = frame;
Chris@30 984 m_editingPoint.value = value;
Chris@30 985 m_editingCommand->addPoint(m_editingPoint);
Chris@30 986 }
Chris@30 987
Chris@30 988 void
Chris@248 989 NoteLayer::editEnd(View *, QMouseEvent *)
Chris@30 990 {
Chris@587 991 // SVDEBUG << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 992 if (!m_model || !m_editing) return;
Chris@30 993
Chris@30 994 if (m_editingCommand) {
Chris@30 995
Chris@30 996 QString newName = m_editingCommand->getName();
Chris@30 997
Chris@30 998 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@30 999 if (m_editingPoint.value != m_originalPoint.value) {
Chris@30 1000 newName = tr("Edit Point");
Chris@30 1001 } else {
Chris@30 1002 newName = tr("Relocate Point");
Chris@30 1003 }
Chris@30 1004 } else {
Chris@30 1005 newName = tr("Change Point Value");
Chris@30 1006 }
Chris@30 1007
Chris@30 1008 m_editingCommand->setName(newName);
Chris@376 1009 finish(m_editingCommand);
Chris@30 1010 }
Chris@30 1011
Chris@30 1012 m_editingCommand = 0;
Chris@30 1013 m_editing = false;
Chris@30 1014 }
Chris@30 1015
Chris@255 1016 bool
Chris@70 1017 NoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1018 {
Chris@255 1019 if (!m_model) return false;
Chris@70 1020
Chris@550 1021 NoteModel::Point note(0);
Chris@550 1022 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 1023
Chris@550 1024 // NoteModel::Point note = *points.begin();
Chris@70 1025
Chris@70 1026 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1027 (m_model->getSampleRate(),
Chris@70 1028 ItemEditDialog::ShowTime |
Chris@70 1029 ItemEditDialog::ShowDuration |
Chris@70 1030 ItemEditDialog::ShowValue |
Chris@100 1031 ItemEditDialog::ShowText,
Chris@100 1032 m_model->getScaleUnits());
Chris@70 1033
Chris@70 1034 dialog->setFrameTime(note.frame);
Chris@70 1035 dialog->setValue(note.value);
Chris@70 1036 dialog->setFrameDuration(note.duration);
Chris@70 1037 dialog->setText(note.label);
Chris@70 1038
Chris@70 1039 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1040
Chris@70 1041 NoteModel::Point newNote = note;
Chris@70 1042 newNote.frame = dialog->getFrameTime();
Chris@70 1043 newNote.value = dialog->getValue();
Chris@70 1044 newNote.duration = dialog->getFrameDuration();
Chris@70 1045 newNote.label = dialog->getText();
Chris@70 1046
Chris@70 1047 NoteModel::EditCommand *command = new NoteModel::EditCommand
Chris@70 1048 (m_model, tr("Edit Point"));
Chris@70 1049 command->deletePoint(note);
Chris@70 1050 command->addPoint(newNote);
Chris@376 1051 finish(command);
Chris@70 1052 }
Chris@70 1053
Chris@70 1054 delete dialog;
Chris@255 1055 return true;
Chris@70 1056 }
Chris@70 1057
Chris@70 1058 void
Chris@43 1059 NoteLayer::moveSelection(Selection s, size_t newStartFrame)
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("Drag 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 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 1070 i != points.end(); ++i) {
Chris@43 1071
Chris@43 1072 if (s.contains(i->frame)) {
Chris@43 1073 NoteModel::Point newPoint(*i);
Chris@43 1074 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1075 command->deletePoint(*i);
Chris@43 1076 command->addPoint(newPoint);
Chris@43 1077 }
Chris@43 1078 }
Chris@43 1079
Chris@376 1080 finish(command);
Chris@43 1081 }
Chris@43 1082
Chris@43 1083 void
Chris@43 1084 NoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1085 {
Chris@99 1086 if (!m_model) return;
Chris@99 1087
Chris@43 1088 NoteModel::EditCommand *command =
Chris@43 1089 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1090
Chris@43 1091 NoteModel::PointList points =
Chris@43 1092 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1093
Chris@43 1094 double ratio =
Chris@43 1095 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1096 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1097
Chris@43 1098 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 1099 i != points.end(); ++i) {
Chris@43 1100
Chris@43 1101 if (s.contains(i->frame)) {
Chris@43 1102
Chris@43 1103 double targetStart = i->frame;
Chris@43 1104 targetStart = newSize.getStartFrame() +
Chris@43 1105 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 1106
Chris@43 1107 double targetEnd = i->frame + i->duration;
Chris@43 1108 targetEnd = newSize.getStartFrame() +
Chris@43 1109 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 1110
Chris@43 1111 NoteModel::Point newPoint(*i);
Chris@43 1112 newPoint.frame = lrint(targetStart);
Chris@43 1113 newPoint.duration = lrint(targetEnd - targetStart);
Chris@43 1114 command->deletePoint(*i);
Chris@43 1115 command->addPoint(newPoint);
Chris@43 1116 }
Chris@43 1117 }
Chris@43 1118
Chris@376 1119 finish(command);
Chris@43 1120 }
Chris@43 1121
Chris@76 1122 void
Chris@76 1123 NoteLayer::deleteSelection(Selection s)
Chris@76 1124 {
Chris@99 1125 if (!m_model) return;
Chris@99 1126
Chris@76 1127 NoteModel::EditCommand *command =
Chris@76 1128 new NoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1129
Chris@76 1130 NoteModel::PointList points =
Chris@76 1131 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1132
Chris@76 1133 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 1134 i != points.end(); ++i) {
Chris@76 1135
Chris@76 1136 if (s.contains(i->frame)) {
Chris@76 1137 command->deletePoint(*i);
Chris@76 1138 }
Chris@76 1139 }
Chris@76 1140
Chris@376 1141 finish(command);
Chris@76 1142 }
Chris@76 1143
Chris@76 1144 void
Chris@359 1145 NoteLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1146 {
Chris@99 1147 if (!m_model) return;
Chris@99 1148
Chris@76 1149 NoteModel::PointList points =
Chris@76 1150 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1151
Chris@76 1152 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 1153 i != points.end(); ++i) {
Chris@76 1154 if (s.contains(i->frame)) {
Chris@335 1155 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
Chris@360 1156 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1157 to.addPoint(point);
Chris@76 1158 }
Chris@76 1159 }
Chris@76 1160 }
Chris@76 1161
Chris@125 1162 bool
Chris@359 1163 NoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
Chris@76 1164 {
Chris@125 1165 if (!m_model) return false;
Chris@99 1166
Chris@76 1167 const Clipboard::PointList &points = from.getPoints();
Chris@76 1168
Chris@360 1169 bool realign = false;
Chris@360 1170
Chris@360 1171 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1172
Chris@360 1173 QMessageBox::StandardButton button =
Chris@360 1174 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1175 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 1176 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1177 QMessageBox::Yes);
Chris@360 1178
Chris@360 1179 if (button == QMessageBox::Cancel) {
Chris@360 1180 return false;
Chris@360 1181 }
Chris@360 1182
Chris@360 1183 if (button == QMessageBox::Yes) {
Chris@360 1184 realign = true;
Chris@360 1185 }
Chris@360 1186 }
Chris@360 1187
Chris@76 1188 NoteModel::EditCommand *command =
Chris@76 1189 new NoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1190
Chris@76 1191 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1192 i != points.end(); ++i) {
Chris@76 1193
Chris@76 1194 if (!i->haveFrame()) continue;
Chris@76 1195 size_t frame = 0;
Chris@360 1196
Chris@360 1197 if (!realign) {
Chris@360 1198
Chris@360 1199 frame = i->getFrame();
Chris@360 1200
Chris@360 1201 } else {
Chris@360 1202
Chris@360 1203 if (i->haveReferenceFrame()) {
Chris@360 1204 frame = i->getReferenceFrame();
Chris@360 1205 frame = alignFromReference(v, frame);
Chris@360 1206 } else {
Chris@360 1207 frame = i->getFrame();
Chris@360 1208 }
Chris@76 1209 }
Chris@360 1210
Chris@76 1211 NoteModel::Point newPoint(frame);
Chris@76 1212
Chris@76 1213 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1214 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1215 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1216 m_model->getValueMaximum()) / 2;
Chris@335 1217 if (i->haveLevel()) newPoint.level = i->getLevel();
Chris@76 1218 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 1219 else {
Chris@125 1220 size_t nextFrame = frame;
Chris@125 1221 Clipboard::PointList::const_iterator j = i;
Chris@125 1222 for (; j != points.end(); ++j) {
Chris@125 1223 if (!j->haveFrame()) continue;
Chris@125 1224 if (j != i) break;
Chris@125 1225 }
Chris@125 1226 if (j != points.end()) {
Chris@125 1227 nextFrame = j->getFrame();
Chris@125 1228 }
Chris@125 1229 if (nextFrame == frame) {
Chris@125 1230 newPoint.duration = m_model->getResolution();
Chris@125 1231 } else {
Chris@125 1232 newPoint.duration = nextFrame - frame;
Chris@125 1233 }
Chris@125 1234 }
Chris@76 1235
Chris@76 1236 command->addPoint(newPoint);
Chris@76 1237 }
Chris@76 1238
Chris@376 1239 finish(command);
Chris@125 1240 return true;
Chris@76 1241 }
Chris@76 1242
Chris@507 1243 void
Chris@507 1244 NoteLayer::addNoteOn(long frame, int pitch, int velocity)
Chris@507 1245 {
Chris@507 1246 m_pendingNoteOns.insert(Note(frame, pitch, 0, float(velocity) / 127.0, ""));
Chris@507 1247 }
Chris@507 1248
Chris@507 1249 void
Chris@507 1250 NoteLayer::addNoteOff(long frame, int pitch)
Chris@507 1251 {
Chris@507 1252 for (NoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1253 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1254 if (lrintf((*i).value) == pitch) {
Chris@507 1255 Note note(*i);
Chris@507 1256 m_pendingNoteOns.erase(i);
Chris@507 1257 note.duration = frame - note.frame;
Chris@507 1258 if (m_model) {
Chris@507 1259 NoteModel::AddPointCommand *c = new NoteModel::AddPointCommand
Chris@507 1260 (m_model, note, tr("Record Note"));
Chris@507 1261 // execute and bundle:
Chris@507 1262 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1263 }
Chris@507 1264 break;
Chris@507 1265 }
Chris@507 1266 }
Chris@507 1267 }
Chris@507 1268
Chris@507 1269 void
Chris@507 1270 NoteLayer::abandonNoteOns()
Chris@507 1271 {
Chris@507 1272 m_pendingNoteOns.clear();
Chris@507 1273 }
Chris@507 1274
Chris@287 1275 int
Chris@287 1276 NoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1277 {
Chris@287 1278 impose = false;
Chris@287 1279 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1280 (QString(darkbg ? "White" : "Black"));
Chris@287 1281 }
Chris@287 1282
Chris@316 1283 void
Chris@316 1284 NoteLayer::toXml(QTextStream &stream,
Chris@316 1285 QString indent, QString extraAttributes) const
Chris@30 1286 {
Chris@316 1287 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1288 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1289 .arg(m_verticalScale)
Chris@445 1290 .arg(m_scaleMinimum)
Chris@445 1291 .arg(m_scaleMaximum));
Chris@30 1292 }
Chris@30 1293
Chris@30 1294 void
Chris@30 1295 NoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1296 {
Chris@287 1297 SingleColourLayer::setProperties(attributes);
Chris@30 1298
Chris@445 1299 bool ok, alsoOk;
Chris@30 1300 VerticalScale scale = (VerticalScale)
Chris@30 1301 attributes.value("verticalScale").toInt(&ok);
Chris@30 1302 if (ok) setVerticalScale(scale);
Chris@445 1303
Chris@445 1304 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1305 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@667 1306 if (ok && alsoOk && min != max) setDisplayExtents(min, max);
Chris@30 1307 }
Chris@30 1308
Chris@30 1309