annotate layer/NoteLayer.cpp @ 1423:62e908518c71 single-point

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