annotate layer/NoteLayer.cpp @ 1470:696e569ff21b by-id

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