annotate layer/NoteLayer.cpp @ 1427:f792a5001d80 single-point

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