annotate layer/NoteLayer.cpp @ 550:d666f5f8b154

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