annotate layer/NoteLayer.cpp @ 667:2727bc3aab59

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