annotate layer/NoteLayer.cpp @ 625:1336439dc055 tonioni

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