annotate layer/NoteLayer.cpp @ 1424:2ee77c9974d7 single-point

Toward getting NoteLayer to work with single-point APIs
author Chris Cannam
date Wed, 13 Mar 2019 16:00:55 +0000
parents 62e908518c71
children f9e3126d223c
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@30 736 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
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@551 752 NoteModel::Point illuminatePoint(0);
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@30 766 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@1266 767 i != points.end(); ++i) {
Chris@30 768
Chris@1266 769 const NoteModel::Point &p(*i);
Chris@30 770
Chris@1266 771 int x = v->getXForFrame(p.frame);
Chris@1266 772 int y = getYForValue(v, p.value);
Chris@1266 773 int w = v->getXForFrame(p.frame + p.duration) - x;
Chris@1266 774 int h = 3;
Chris@1266 775
Chris@1266 776 if (m_model->getValueQuantization() != 0.0) {
Chris@1266 777 h = y - getYForValue(v, p.value + 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@1266 785 if (shouldIlluminate &&
Chris@551 786 // "illuminatePoint == p"
Chris@551 787 !NoteModel::Point::Comparator()(illuminatePoint, p) &&
Chris@551 788 !NoteModel::Point::Comparator()(p, illuminatePoint)) {
Chris@551 789
Chris@551 790 paint.setPen(v->getForeground());
Chris@551 791 paint.setBrush(v->getForeground());
Chris@551 792
Chris@701 793 QString vlabel = QString("%1%2").arg(p.value).arg(getScaleUnits());
Chris@1078 794 PaintAssistant::drawVisibleText(v, paint,
Chris@551 795 x - paint.fontMetrics().width(vlabel) - 2,
Chris@551 796 y + paint.fontMetrics().height()/2
Chris@551 797 - paint.fontMetrics().descent(),
Chris@1078 798 vlabel, PaintAssistant::OutlinedText);
Chris@551 799
Chris@551 800 QString hlabel = RealTime::frame2RealTime
Chris@551 801 (p.frame, m_model->getSampleRate()).toText(true).c_str();
Chris@1078 802 PaintAssistant::drawVisibleText(v, paint,
Chris@551 803 x,
Chris@551 804 y - h/2 - paint.fontMetrics().descent() - 2,
Chris@1078 805 hlabel, PaintAssistant::OutlinedText);
Chris@1266 806 }
Chris@1266 807
Chris@1266 808 paint.drawRect(x, y - h/2, w, h);
Chris@30 809 }
Chris@30 810
Chris@30 811 paint.restore();
Chris@30 812 }
Chris@30 813
Chris@692 814 int
Chris@918 815 NoteLayer::getVerticalScaleWidth(LayerGeometryProvider *v, bool, QPainter &paint) const
Chris@692 816 {
Chris@1315 817 if (!m_model) {
Chris@697 818 return 0;
Chris@1315 819 }
Chris@1315 820
Chris@1315 821 if (shouldAutoAlign() && !valueExtentsMatchMine(v)) {
Chris@1315 822 return 0;
Chris@1315 823 }
Chris@1315 824
Chris@1315 825 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@1315 826 return LogNumericalScale().getWidth(v, paint) + 10; // for piano
Chris@1315 827 } else {
Chris@1315 828 return LinearNumericalScale().getWidth(v, paint);
Chris@697 829 }
Chris@692 830 }
Chris@692 831
Chris@692 832 void
Chris@918 833 NoteLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect) const
Chris@692 834 {
Chris@717 835 if (!m_model || m_model->getPoints().empty()) return;
Chris@701 836
Chris@701 837 QString unit;
Chris@905 838 double min, max;
Chris@701 839 bool logarithmic;
Chris@701 840
Chris@701 841 int w = getVerticalScaleWidth(v, false, paint);
Chris@918 842 int h = v->getPaintHeight();
Chris@701 843
Chris@701 844 getScaleExtents(v, min, max, logarithmic);
Chris@701 845
Chris@701 846 if (logarithmic) {
Chris@701 847 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 848 } else {
Chris@701 849 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 850 }
Chris@701 851
Chris@701 852 if (logarithmic && (getScaleUnits() == "Hz")) {
Chris@697 853 PianoScale().paintPianoVertical
Chris@701 854 (v, paint, QRect(w - 10, 0, 10, h),
Chris@701 855 LogRange::unmap(min),
Chris@701 856 LogRange::unmap(max));
Chris@701 857 paint.drawLine(w, 0, w, h);
Chris@701 858 }
Chris@701 859
Chris@701 860 if (getScaleUnits() != "") {
Chris@701 861 int mw = w - 5;
Chris@701 862 paint.drawText(5,
Chris@701 863 5 + paint.fontMetrics().ascent(),
Chris@701 864 TextAbbrev::abbreviate(getScaleUnits(),
Chris@701 865 paint.fontMetrics(),
Chris@701 866 mw));
Chris@697 867 }
Chris@692 868 }
Chris@692 869
Chris@30 870 void
Chris@918 871 NoteLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 872 {
Chris@587 873 // SVDEBUG << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 874
Chris@30 875 if (!m_model) return;
Chris@30 876
Chris@905 877 sv_frame_t frame = v->getFrameForX(e->x());
Chris@30 878 if (frame < 0) frame = 0;
Chris@30 879 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 880
Chris@905 881 double value = getValueForY(v, e->y());
Chris@30 882
Chris@905 883 m_editingPoint = NoteModel::Point(frame, float(value), 0, 0.8f, tr("New Point"));
Chris@30 884 m_originalPoint = m_editingPoint;
Chris@30 885
Chris@376 886 if (m_editingCommand) finish(m_editingCommand);
Chris@30 887 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@1266 888 tr("Draw Point"));
Chris@30 889 m_editingCommand->addPoint(m_editingPoint);
Chris@30 890
Chris@30 891 m_editing = true;
Chris@30 892 }
Chris@30 893
Chris@30 894 void
Chris@918 895 NoteLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 896 {
Chris@587 897 // SVDEBUG << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 898
Chris@30 899 if (!m_model || !m_editing) return;
Chris@30 900
Chris@905 901 sv_frame_t frame = v->getFrameForX(e->x());
Chris@30 902 if (frame < 0) frame = 0;
Chris@30 903 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 904
Chris@905 905 double newValue = getValueForY(v, e->y());
Chris@101 906
Chris@905 907 sv_frame_t newFrame = m_editingPoint.frame;
Chris@905 908 sv_frame_t newDuration = frame - newFrame;
Chris@101 909 if (newDuration < 0) {
Chris@101 910 newFrame = frame;
Chris@101 911 newDuration = -newDuration;
Chris@101 912 } else if (newDuration == 0) {
Chris@101 913 newDuration = 1;
Chris@101 914 }
Chris@30 915
Chris@30 916 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 917 m_editingPoint.frame = newFrame;
Chris@905 918 m_editingPoint.value = float(newValue);
Chris@101 919 m_editingPoint.duration = newDuration;
Chris@30 920 m_editingCommand->addPoint(m_editingPoint);
Chris@30 921 }
Chris@30 922
Chris@30 923 void
Chris@918 924 NoteLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@30 925 {
Chris@587 926 // SVDEBUG << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 927 if (!m_model || !m_editing) return;
Chris@376 928 finish(m_editingCommand);
Chris@1408 929 m_editingCommand = nullptr;
Chris@30 930 m_editing = false;
Chris@30 931 }
Chris@30 932
Chris@30 933 void
Chris@918 934 NoteLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@335 935 {
Chris@335 936 if (!m_model) return;
Chris@335 937
Chris@550 938 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 939
Chris@335 940 if (m_editingCommand) {
Chris@1266 941 finish(m_editingCommand);
Chris@1408 942 m_editingCommand = nullptr;
Chris@335 943 }
Chris@335 944
Chris@335 945 m_editing = true;
Chris@335 946 }
Chris@335 947
Chris@335 948 void
Chris@918 949 NoteLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
Chris@335 950 {
Chris@335 951 }
Chris@335 952
Chris@335 953 void
Chris@918 954 NoteLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@335 955 {
Chris@335 956 if (!m_model || !m_editing) return;
Chris@335 957
Chris@335 958 m_editing = false;
Chris@335 959
Chris@550 960 NoteModel::Point p(0);
Chris@550 961 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@550 962 if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return;
Chris@550 963
Chris@550 964 m_editingCommand = new NoteModel::EditCommand(m_model, tr("Erase Point"));
Chris@335 965
Chris@335 966 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 967
Chris@376 968 finish(m_editingCommand);
Chris@1408 969 m_editingCommand = nullptr;
Chris@335 970 m_editing = false;
Chris@335 971 }
Chris@335 972
Chris@335 973 void
Chris@918 974 NoteLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 975 {
Chris@587 976 // SVDEBUG << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 977
Chris@30 978 if (!m_model) return;
Chris@30 979
Chris@550 980 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@30 981 m_originalPoint = m_editingPoint;
Chris@30 982
Chris@551 983 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
Chris@551 984 m_dragPointY = getYForValue(v, m_editingPoint.value);
Chris@551 985
Chris@30 986 if (m_editingCommand) {
Chris@1266 987 finish(m_editingCommand);
Chris@1408 988 m_editingCommand = nullptr;
Chris@30 989 }
Chris@30 990
Chris@30 991 m_editing = true;
Chris@551 992 m_dragStartX = e->x();
Chris@551 993 m_dragStartY = e->y();
Chris@30 994 }
Chris@30 995
Chris@30 996 void
Chris@918 997 NoteLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 998 {
Chris@587 999 // SVDEBUG << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 1000
Chris@30 1001 if (!m_model || !m_editing) return;
Chris@30 1002
Chris@551 1003 int xdist = e->x() - m_dragStartX;
Chris@551 1004 int ydist = e->y() - m_dragStartY;
Chris@551 1005 int newx = m_dragPointX + xdist;
Chris@551 1006 int newy = m_dragPointY + ydist;
Chris@551 1007
Chris@905 1008 sv_frame_t frame = v->getFrameForX(newx);
Chris@30 1009 if (frame < 0) frame = 0;
Chris@30 1010 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 1011
Chris@905 1012 double value = getValueForY(v, newy);
Chris@30 1013
Chris@30 1014 if (!m_editingCommand) {
Chris@1266 1015 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@1266 1016 tr("Drag Point"));
Chris@30 1017 }
Chris@30 1018
Chris@30 1019 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 1020 m_editingPoint.frame = frame;
Chris@905 1021 m_editingPoint.value = float(value);
Chris@30 1022 m_editingCommand->addPoint(m_editingPoint);
Chris@30 1023 }
Chris@30 1024
Chris@30 1025 void
Chris@918 1026 NoteLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@30 1027 {
Chris@587 1028 // SVDEBUG << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 1029 if (!m_model || !m_editing) return;
Chris@30 1030
Chris@30 1031 if (m_editingCommand) {
Chris@30 1032
Chris@1266 1033 QString newName = m_editingCommand->getName();
Chris@30 1034
Chris@1266 1035 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@1266 1036 if (m_editingPoint.value != m_originalPoint.value) {
Chris@1266 1037 newName = tr("Edit Point");
Chris@1266 1038 } else {
Chris@1266 1039 newName = tr("Relocate Point");
Chris@1266 1040 }
Chris@1266 1041 } else {
Chris@1266 1042 newName = tr("Change Point Value");
Chris@1266 1043 }
Chris@30 1044
Chris@1266 1045 m_editingCommand->setName(newName);
Chris@1266 1046 finish(m_editingCommand);
Chris@30 1047 }
Chris@30 1048
Chris@1408 1049 m_editingCommand = nullptr;
Chris@30 1050 m_editing = false;
Chris@30 1051 }
Chris@30 1052
Chris@255 1053 bool
Chris@918 1054 NoteLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
Chris@70 1055 {
Chris@255 1056 if (!m_model) return false;
Chris@70 1057
Chris@550 1058 NoteModel::Point note(0);
Chris@550 1059 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 1060
Chris@550 1061 // NoteModel::Point note = *points.begin();
Chris@70 1062
Chris@70 1063 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1064 (m_model->getSampleRate(),
Chris@70 1065 ItemEditDialog::ShowTime |
Chris@70 1066 ItemEditDialog::ShowDuration |
Chris@70 1067 ItemEditDialog::ShowValue |
Chris@100 1068 ItemEditDialog::ShowText,
Chris@701 1069 getScaleUnits());
Chris@70 1070
Chris@70 1071 dialog->setFrameTime(note.frame);
Chris@70 1072 dialog->setValue(note.value);
Chris@70 1073 dialog->setFrameDuration(note.duration);
Chris@70 1074 dialog->setText(note.label);
Chris@70 1075
Chris@1422 1076 m_editingPoint = note;
Chris@1422 1077 m_editIsOpen = true;
Chris@1422 1078
Chris@70 1079 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1080
Chris@70 1081 NoteModel::Point newNote = note;
Chris@70 1082 newNote.frame = dialog->getFrameTime();
Chris@70 1083 newNote.value = dialog->getValue();
Chris@70 1084 newNote.duration = dialog->getFrameDuration();
Chris@70 1085 newNote.label = dialog->getText();
Chris@70 1086
Chris@70 1087 NoteModel::EditCommand *command = new NoteModel::EditCommand
Chris@70 1088 (m_model, tr("Edit Point"));
Chris@70 1089 command->deletePoint(note);
Chris@70 1090 command->addPoint(newNote);
Chris@376 1091 finish(command);
Chris@70 1092 }
Chris@70 1093
Chris@1422 1094 m_editingPoint = 0;
Chris@1422 1095 m_editIsOpen = false;
Chris@1422 1096
Chris@70 1097 delete dialog;
Chris@255 1098 return true;
Chris@70 1099 }
Chris@70 1100
Chris@70 1101 void
Chris@905 1102 NoteLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
Chris@43 1103 {
Chris@99 1104 if (!m_model) return;
Chris@99 1105
Chris@43 1106 NoteModel::EditCommand *command =
Chris@1266 1107 new NoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 1108
Chris@43 1109 NoteModel::PointList points =
Chris@1266 1110 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1111
Chris@43 1112 for (NoteModel::PointList::iterator i = points.begin();
Chris@1266 1113 i != points.end(); ++i) {
Chris@43 1114
Chris@1266 1115 if (s.contains(i->frame)) {
Chris@1266 1116 NoteModel::Point newPoint(*i);
Chris@1266 1117 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@1266 1118 command->deletePoint(*i);
Chris@1266 1119 command->addPoint(newPoint);
Chris@1266 1120 }
Chris@43 1121 }
Chris@43 1122
Chris@376 1123 finish(command);
Chris@43 1124 }
Chris@43 1125
Chris@43 1126 void
Chris@43 1127 NoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1128 {
Chris@99 1129 if (!m_model) return;
Chris@99 1130
Chris@43 1131 NoteModel::EditCommand *command =
Chris@1266 1132 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1133
Chris@43 1134 NoteModel::PointList points =
Chris@1266 1135 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1136
Chris@43 1137 double ratio =
Chris@1266 1138 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@1266 1139 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1140
Chris@43 1141 for (NoteModel::PointList::iterator i = points.begin();
Chris@1266 1142 i != points.end(); ++i) {
Chris@43 1143
Chris@1266 1144 if (s.contains(i->frame)) {
Chris@43 1145
Chris@1266 1146 double targetStart = double(i->frame);
Chris@1266 1147 targetStart = double(newSize.getStartFrame()) +
Chris@1266 1148 targetStart - double(s.getStartFrame()) * ratio;
Chris@43 1149
Chris@1266 1150 double targetEnd = double(i->frame + i->duration);
Chris@1266 1151 targetEnd = double(newSize.getStartFrame()) +
Chris@1266 1152 targetEnd - double(s.getStartFrame()) * ratio;
Chris@43 1153
Chris@1266 1154 NoteModel::Point newPoint(*i);
Chris@1266 1155 newPoint.frame = lrint(targetStart);
Chris@1266 1156 newPoint.duration = lrint(targetEnd - targetStart);
Chris@1266 1157 command->deletePoint(*i);
Chris@1266 1158 command->addPoint(newPoint);
Chris@1266 1159 }
Chris@43 1160 }
Chris@43 1161
Chris@376 1162 finish(command);
Chris@43 1163 }
Chris@43 1164
Chris@76 1165 void
Chris@76 1166 NoteLayer::deleteSelection(Selection s)
Chris@76 1167 {
Chris@99 1168 if (!m_model) return;
Chris@99 1169
Chris@76 1170 NoteModel::EditCommand *command =
Chris@1266 1171 new NoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1172
Chris@76 1173 NoteModel::PointList points =
Chris@1266 1174 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1175
Chris@76 1176 for (NoteModel::PointList::iterator i = points.begin();
Chris@1266 1177 i != points.end(); ++i) {
Chris@76 1178
Chris@76 1179 if (s.contains(i->frame)) {
Chris@76 1180 command->deletePoint(*i);
Chris@76 1181 }
Chris@76 1182 }
Chris@76 1183
Chris@376 1184 finish(command);
Chris@76 1185 }
Chris@76 1186
Chris@76 1187 void
Chris@918 1188 NoteLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
Chris@76 1189 {
Chris@99 1190 if (!m_model) return;
Chris@99 1191
Chris@76 1192 NoteModel::PointList points =
Chris@1266 1193 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1194
Chris@76 1195 for (NoteModel::PointList::iterator i = points.begin();
Chris@1266 1196 i != points.end(); ++i) {
Chris@1266 1197 if (s.contains(i->frame)) {
Chris@1423 1198 Event point(i->frame, i->value, i->duration, i->level, i->label);
Chris@1423 1199 to.addPoint(point.withReferenceFrame(alignToReference(v, i->frame)));
Chris@76 1200 }
Chris@76 1201 }
Chris@76 1202 }
Chris@76 1203
Chris@125 1204 bool
Chris@918 1205 NoteLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */)
Chris@76 1206 {
Chris@125 1207 if (!m_model) return false;
Chris@99 1208
Chris@1423 1209 const EventVector &points = from.getPoints();
Chris@76 1210
Chris@360 1211 bool realign = false;
Chris@360 1212
Chris@360 1213 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1214
Chris@360 1215 QMessageBox::StandardButton button =
Chris@918 1216 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
Chris@360 1217 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 1218 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1219 QMessageBox::Yes);
Chris@360 1220
Chris@360 1221 if (button == QMessageBox::Cancel) {
Chris@360 1222 return false;
Chris@360 1223 }
Chris@360 1224
Chris@360 1225 if (button == QMessageBox::Yes) {
Chris@360 1226 realign = true;
Chris@360 1227 }
Chris@360 1228 }
Chris@360 1229
Chris@76 1230 NoteModel::EditCommand *command =
Chris@1266 1231 new NoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1232
Chris@1423 1233 for (EventVector::const_iterator i = points.begin();
Chris@76 1234 i != points.end(); ++i) {
Chris@76 1235
Chris@905 1236 sv_frame_t frame = 0;
Chris@360 1237
Chris@360 1238 if (!realign) {
Chris@360 1239
Chris@360 1240 frame = i->getFrame();
Chris@360 1241
Chris@360 1242 } else {
Chris@360 1243
Chris@1423 1244 if (i->hasReferenceFrame()) {
Chris@360 1245 frame = i->getReferenceFrame();
Chris@360 1246 frame = alignFromReference(v, frame);
Chris@360 1247 } else {
Chris@360 1248 frame = i->getFrame();
Chris@360 1249 }
Chris@76 1250 }
Chris@360 1251
Chris@76 1252 NoteModel::Point newPoint(frame);
Chris@76 1253
Chris@1423 1254 if (i->hasLabel()) newPoint.label = i->getLabel();
Chris@1423 1255 if (i->hasValue()) newPoint.value = i->getValue();
Chris@76 1256 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1257 m_model->getValueMaximum()) / 2;
Chris@1423 1258 if (i->hasLevel()) newPoint.level = i->getLevel();
Chris@1423 1259 if (i->hasDuration()) newPoint.duration = i->getDuration();
Chris@125 1260 else {
Chris@905 1261 sv_frame_t nextFrame = frame;
Chris@1423 1262 EventVector::const_iterator j = i;
Chris@125 1263 for (; j != points.end(); ++j) {
Chris@125 1264 if (j != i) break;
Chris@125 1265 }
Chris@125 1266 if (j != points.end()) {
Chris@125 1267 nextFrame = j->getFrame();
Chris@125 1268 }
Chris@125 1269 if (nextFrame == frame) {
Chris@125 1270 newPoint.duration = m_model->getResolution();
Chris@125 1271 } else {
Chris@125 1272 newPoint.duration = nextFrame - frame;
Chris@125 1273 }
Chris@125 1274 }
Chris@76 1275
Chris@76 1276 command->addPoint(newPoint);
Chris@76 1277 }
Chris@76 1278
Chris@376 1279 finish(command);
Chris@125 1280 return true;
Chris@76 1281 }
Chris@76 1282
Chris@507 1283 void
Chris@905 1284 NoteLayer::addNoteOn(sv_frame_t frame, int pitch, int velocity)
Chris@507 1285 {
Chris@905 1286 m_pendingNoteOns.insert(Note(frame, float(pitch), 0, float(velocity) / 127.f, ""));
Chris@507 1287 }
Chris@507 1288
Chris@507 1289 void
Chris@905 1290 NoteLayer::addNoteOff(sv_frame_t frame, int pitch)
Chris@507 1291 {
Chris@507 1292 for (NoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1293 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1294 if (lrintf((*i).value) == pitch) {
Chris@507 1295 Note note(*i);
Chris@507 1296 m_pendingNoteOns.erase(i);
Chris@507 1297 note.duration = frame - note.frame;
Chris@507 1298 if (m_model) {
Chris@507 1299 NoteModel::AddPointCommand *c = new NoteModel::AddPointCommand
Chris@507 1300 (m_model, note, tr("Record Note"));
Chris@507 1301 // execute and bundle:
Chris@507 1302 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1303 }
Chris@507 1304 break;
Chris@507 1305 }
Chris@507 1306 }
Chris@507 1307 }
Chris@507 1308
Chris@507 1309 void
Chris@507 1310 NoteLayer::abandonNoteOns()
Chris@507 1311 {
Chris@507 1312 m_pendingNoteOns.clear();
Chris@507 1313 }
Chris@507 1314
Chris@287 1315 int
Chris@287 1316 NoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1317 {
Chris@287 1318 impose = false;
Chris@287 1319 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1320 (QString(darkbg ? "White" : "Black"));
Chris@287 1321 }
Chris@287 1322
Chris@316 1323 void
Chris@316 1324 NoteLayer::toXml(QTextStream &stream,
Chris@316 1325 QString indent, QString extraAttributes) const
Chris@30 1326 {
Chris@316 1327 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1328 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1329 .arg(m_verticalScale)
Chris@445 1330 .arg(m_scaleMinimum)
Chris@445 1331 .arg(m_scaleMaximum));
Chris@30 1332 }
Chris@30 1333
Chris@30 1334 void
Chris@30 1335 NoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1336 {
Chris@287 1337 SingleColourLayer::setProperties(attributes);
Chris@30 1338
Chris@445 1339 bool ok, alsoOk;
Chris@30 1340 VerticalScale scale = (VerticalScale)
Chris@1266 1341 attributes.value("verticalScale").toInt(&ok);
Chris@30 1342 if (ok) setVerticalScale(scale);
Chris@445 1343
Chris@445 1344 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1345 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@667 1346 if (ok && alsoOk && min != max) setDisplayExtents(min, max);
Chris@30 1347 }
Chris@30 1348
Chris@30 1349