annotate layer/NoteLayer.cpp @ 1550:e95cefd4aa81

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