annotate layer/FlexiNoteLayer.cpp @ 1469:11a150e65ee1 by-id

Some work on updating layers for ModelId bits
author Chris Cannam
date Thu, 27 Jun 2019 13:16:25 +0100
parents 8a7c82282fbc
children 696e569ff21b
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
matthiasm@620 16 #include "FlexiNoteLayer.h"
Chris@30 17
Chris@128 18 #include "data/model/Model.h"
gyorgyf@655 19 #include "data/model/SparseTimeValueModel.h"
Chris@30 20 #include "base/RealTime.h"
Chris@30 21 #include "base/Profiler.h"
Chris@30 22 #include "base/Pitch.h"
Chris@197 23 #include "base/LogRange.h"
Chris@439 24 #include "base/RangeMapper.h"
Chris@1078 25
Chris@376 26 #include "ColourDatabase.h"
Chris@1077 27 #include "LayerGeometryProvider.h"
Chris@692 28 #include "PianoScale.h"
Chris@701 29 #include "LinearNumericalScale.h"
Chris@701 30 #include "LogNumericalScale.h"
Chris@1078 31 #include "PaintAssistant.h"
Chris@30 32
Chris@1426 33 #include "data/model/NoteModel.h"
Chris@30 34
Chris@1077 35 #include "view/View.h"
Chris@1077 36
Chris@70 37 #include "widgets/ItemEditDialog.h"
Chris@701 38 #include "widgets/TextAbbrev.h"
Chris@70 39
Chris@30 40 #include <QPainter>
Chris@30 41 #include <QPainterPath>
Chris@30 42 #include <QMouseEvent>
Chris@316 43 #include <QTextStream>
Chris@360 44 #include <QMessageBox>
Chris@30 45
Chris@30 46 #include <iostream>
Chris@30 47 #include <cmath>
Chris@551 48 #include <utility>
gyorgyf@655 49 #include <limits> // GF: included to compile std::numerical_limits on linux
gyorgyf@655 50 #include <vector>
gyorgyf@655 51
Chris@1426 52 #define NOTE_HEIGHT 16
Chris@30 53
matthiasm@620 54 FlexiNoteLayer::FlexiNoteLayer() :
gyorgyf@646 55 SingleColourLayer(),
gyorgyf@628 56 m_editing(false),
Chris@805 57 m_intelligentActions(true),
Chris@844 58 m_dragPointX(0),
Chris@844 59 m_dragPointY(0),
Chris@844 60 m_dragStartX(0),
Chris@844 61 m_dragStartY(0),
gyorgyf@627 62 m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
gyorgyf@627 63 m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
Chris@844 64 m_greatestLeftNeighbourFrame(0),
Chris@844 65 m_smallestRightNeighbourFrame(0),
Chris@1408 66 m_editingCommand(nullptr),
matthiasm@634 67 m_verticalScale(AutoAlignScale),
Chris@688 68 m_editMode(DragNote),
gyorgyf@628 69 m_scaleMinimum(34),
Chris@805 70 m_scaleMaximum(77)
Chris@30 71 {
Chris@30 72 }
Chris@30 73
Chris@30 74 void
Chris@1469 75 FlexiNoteLayer::setModel(ModelId modelId)
Chris@30 76 {
Chris@1469 77 if (m_model == modelId) return;
Chris@1469 78
Chris@1469 79 auto model = ModelById::getAs<NoteModel>(modelId);
Chris@1469 80 if (!model) throw std::logic_error("Not a NoteModel");
Chris@1469 81
Chris@1469 82 m_model = modelId;
Chris@30 83
Chris@320 84 connectSignals(m_model);
Chris@30 85
Chris@30 86 emit modelReplaced();
Chris@30 87 }
Chris@30 88
Chris@30 89 Layer::PropertyList
matthiasm@620 90 FlexiNoteLayer::getProperties() const
Chris@30 91 {
Chris@287 92 PropertyList list = SingleColourLayer::getProperties();
Chris@87 93 list.push_back("Vertical Scale");
Chris@100 94 list.push_back("Scale Units");
Chris@30 95 return list;
Chris@30 96 }
Chris@30 97
Chris@87 98 QString
matthiasm@620 99 FlexiNoteLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 100 {
Chris@87 101 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@116 102 if (name == "Scale Units") return tr("Scale Units");
Chris@287 103 return SingleColourLayer::getPropertyLabel(name);
Chris@87 104 }
Chris@87 105
Chris@30 106 Layer::PropertyType
matthiasm@620 107 FlexiNoteLayer::getPropertyType(const PropertyName &name) const
Chris@30 108 {
Chris@100 109 if (name == "Scale Units") return UnitsProperty;
Chris@287 110 if (name == "Vertical Scale") return ValueProperty;
Chris@287 111 return SingleColourLayer::getPropertyType(name);
Chris@30 112 }
Chris@30 113
Chris@198 114 QString
matthiasm@620 115 FlexiNoteLayer::getPropertyGroupName(const PropertyName &name) const
Chris@198 116 {
Chris@198 117 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@198 118 return tr("Scale");
Chris@198 119 }
Chris@287 120 return SingleColourLayer::getPropertyGroupName(name);
Chris@198 121 }
Chris@198 122
Chris@701 123 QString
Chris@703 124 FlexiNoteLayer::getScaleUnits() const
Chris@701 125 {
Chris@1469 126 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 127 if (model) return model->getScaleUnits();
Chris@701 128 else return "";
Chris@701 129 }
Chris@701 130
Chris@30 131 int
matthiasm@620 132 FlexiNoteLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@714 133 int *min, int *max, int *deflt) const
Chris@30 134 {
Chris@216 135 int val = 0;
Chris@30 136
Chris@287 137 if (name == "Vertical Scale") {
gyorgyf@646 138
Chris@714 139 if (min) *min = 0;
Chris@714 140 if (max) *max = 3;
Chris@216 141 if (deflt) *deflt = int(AutoAlignScale);
gyorgyf@646 142
Chris@714 143 val = int(m_verticalScale);
Chris@30 144
Chris@100 145 } else if (name == "Scale Units") {
Chris@100 146
Chris@216 147 if (deflt) *deflt = 0;
Chris@1469 148 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 149 if (model) {
Chris@216 150 val = UnitDatabase::getInstance()->getUnitId
Chris@701 151 (getScaleUnits());
Chris@100 152 }
Chris@100 153
Chris@30 154 } else {
Chris@216 155
Chris@714 156 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@30 157 }
Chris@30 158
Chris@216 159 return val;
Chris@30 160 }
Chris@30 161
Chris@30 162 QString
matthiasm@620 163 FlexiNoteLayer::getPropertyValueLabel(const PropertyName &name,
Chris@714 164 int value) const
Chris@30 165 {
Chris@287 166 if (name == "Vertical Scale") {
Chris@714 167 switch (value) {
Chris@714 168 default:
Chris@714 169 case 0: return tr("Auto-Align");
Chris@714 170 case 1: return tr("Linear");
Chris@714 171 case 2: return tr("Log");
Chris@714 172 case 3: return tr("MIDI Notes");
Chris@714 173 }
Chris@30 174 }
Chris@287 175 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@30 176 }
Chris@30 177
Chris@30 178 void
matthiasm@620 179 FlexiNoteLayer::setProperty(const PropertyName &name, int value)
Chris@30 180 {
Chris@287 181 if (name == "Vertical Scale") {
Chris@714 182 setVerticalScale(VerticalScale(value));
Chris@100 183 } else if (name == "Scale Units") {
Chris@1469 184 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 185 if (model) {
Chris@1469 186 model->setScaleUnits
Chris@100 187 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 188 emit modelChanged();
Chris@100 189 }
Chris@287 190 } else {
Chris@287 191 return SingleColourLayer::setProperty(name, value);
Chris@30 192 }
Chris@30 193 }
Chris@30 194
Chris@30 195 void
matthiasm@620 196 FlexiNoteLayer::setVerticalScale(VerticalScale scale)
Chris@30 197 {
Chris@30 198 if (m_verticalScale == scale) return;
Chris@30 199 m_verticalScale = scale;
Chris@30 200 emit layerParametersChanged();
Chris@30 201 }
Chris@30 202
Chris@30 203 bool
Chris@916 204 FlexiNoteLayer::isLayerScrollable(const LayerGeometryProvider *v) const
Chris@30 205 {
Chris@30 206 QPoint discard;
Chris@44 207 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@30 208 }
Chris@30 209
Chris@79 210 bool
matthiasm@620 211 FlexiNoteLayer::shouldConvertMIDIToHz() const
Chris@101 212 {
Chris@701 213 QString unit = getScaleUnits();
Chris@101 214 return (unit != "Hz");
Chris@101 215 // if (unit == "" ||
Chris@101 216 // unit.startsWith("MIDI") ||
Chris@101 217 // unit.startsWith("midi")) return true;
Chris@101 218 // return false;
Chris@101 219 }
Chris@101 220
Chris@1469 221 int
Chris@1469 222 FlexiNoteLayer::getCompletion(LayerGeometryProvider *) const
Chris@1469 223 {
Chris@1469 224 auto model = ModelById::get(m_model);
Chris@1469 225 if (model) return model->getCompletion();
Chris@1469 226 else return 0;
Chris@1469 227 }
Chris@1469 228
Chris@101 229 bool
Chris@904 230 FlexiNoteLayer::getValueExtents(double &min, double &max,
Chris@714 231 bool &logarithmic, QString &unit) const
Chris@79 232 {
Chris@1469 233 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 234 if (!model) return false;
Chris@1469 235 min = model->getValueMinimum();
Chris@1469 236 max = model->getValueMaximum();
Chris@101 237
Chris@105 238 if (shouldConvertMIDIToHz()) {
Chris@105 239 unit = "Hz";
Chris@904 240 min = Pitch::getFrequencyForPitch(int(lrint(min)));
Chris@904 241 max = Pitch::getFrequencyForPitch(int(lrint(max + 1)));
Chris@701 242 } else unit = getScaleUnits();
Chris@101 243
Chris@101 244 if (m_verticalScale == MIDIRangeScale ||
Chris@101 245 m_verticalScale == LogScale) logarithmic = true;
Chris@101 246
Chris@101 247 return true;
Chris@101 248 }
Chris@101 249
Chris@101 250 bool
Chris@904 251 FlexiNoteLayer::getDisplayExtents(double &min, double &max) const
Chris@101 252 {
Chris@1469 253 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 254 if (!model || shouldAutoAlign()) {
Chris@695 255 // std::cerr << "No model or shouldAutoAlign()" << std::endl;
gyorgyf@646 256 return false;
gyorgyf@646 257 }
Chris@101 258
Chris@101 259 if (m_verticalScale == MIDIRangeScale) {
Chris@101 260 min = Pitch::getFrequencyForPitch(0);
matthiasm@634 261 max = Pitch::getFrequencyForPitch(127);
Chris@101 262 return true;
Chris@101 263 }
Chris@101 264
Chris@439 265 if (m_scaleMinimum == m_scaleMaximum) {
Chris@1469 266 min = model->getValueMinimum();
Chris@1469 267 max = model->getValueMaximum();
Chris@455 268 } else {
Chris@455 269 min = m_scaleMinimum;
Chris@455 270 max = m_scaleMaximum;
Chris@439 271 }
Chris@439 272
Chris@101 273 if (shouldConvertMIDIToHz()) {
Chris@904 274 min = Pitch::getFrequencyForPitch(int(lrint(min)));
Chris@904 275 max = Pitch::getFrequencyForPitch(int(lrint(max + 1)));
Chris@101 276 }
Chris@101 277
Chris@667 278 #ifdef DEBUG_NOTE_LAYER
Chris@682 279 cerr << "NoteLayer::getDisplayExtents: min = " << min << ", max = " << max << " (m_scaleMinimum = " << m_scaleMinimum << ", m_scaleMaximum = " << m_scaleMaximum << ")" << endl;
Chris@667 280 #endif
Chris@667 281
Chris@79 282 return true;
Chris@79 283 }
Chris@79 284
Chris@439 285 bool
Chris@904 286 FlexiNoteLayer::setDisplayExtents(double min, double max)
Chris@439 287 {
Chris@1469 288 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 289 if (!model) return false;
Chris@439 290
Chris@439 291 if (min == max) {
Chris@439 292 if (min == 0.f) {
Chris@439 293 max = 1.f;
Chris@439 294 } else {
Chris@904 295 max = min * 1.0001f;
Chris@439 296 }
Chris@439 297 }
Chris@439 298
Chris@439 299 m_scaleMinimum = min;
Chris@439 300 m_scaleMaximum = max;
Chris@439 301
Chris@667 302 #ifdef DEBUG_NOTE_LAYER
Chris@684 303 cerr << "FlexiNoteLayer::setDisplayExtents: min = " << min << ", max = " << max << endl;
Chris@667 304 #endif
Chris@439 305
Chris@439 306 emit layerParametersChanged();
Chris@439 307 return true;
Chris@439 308 }
Chris@439 309
Chris@439 310 int
matthiasm@620 311 FlexiNoteLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@439 312 {
Chris@439 313 if (shouldAutoAlign()) return 0;
Chris@1469 314 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 315 if (!model) return 0;
Chris@439 316
Chris@439 317 defaultStep = 0;
Chris@439 318 return 100;
Chris@439 319 }
Chris@439 320
Chris@439 321 int
matthiasm@620 322 FlexiNoteLayer::getCurrentVerticalZoomStep() const
Chris@439 323 {
Chris@439 324 if (shouldAutoAlign()) return 0;
Chris@1469 325 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 326 if (!model) return 0;
Chris@439 327
Chris@439 328 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 329 if (!mapper) return 0;
Chris@439 330
Chris@904 331 double dmin, dmax;
Chris@439 332 getDisplayExtents(dmin, dmax);
Chris@439 333
Chris@439 334 int nr = mapper->getPositionForValue(dmax - dmin);
Chris@439 335
Chris@439 336 delete mapper;
Chris@439 337
Chris@439 338 return 100 - nr;
Chris@439 339 }
Chris@439 340
Chris@439 341 //!!! lots of duplication with TimeValueLayer
Chris@439 342
Chris@439 343 void
matthiasm@620 344 FlexiNoteLayer::setVerticalZoomStep(int step)
Chris@439 345 {
Chris@439 346 if (shouldAutoAlign()) return;
Chris@1469 347 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 348 if (!model) return;
Chris@439 349
Chris@439 350 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 351 if (!mapper) return;
Chris@439 352
Chris@904 353 double min, max;
Chris@439 354 bool logarithmic;
Chris@439 355 QString unit;
Chris@439 356 getValueExtents(min, max, logarithmic, unit);
Chris@439 357
Chris@904 358 double dmin, dmax;
Chris@439 359 getDisplayExtents(dmin, dmax);
Chris@439 360
Chris@904 361 double newdist = mapper->getValueForPosition(100 - step);
Chris@439 362
Chris@904 363 double newmin, newmax;
Chris@439 364
Chris@439 365 if (logarithmic) {
Chris@439 366
Chris@439 367 // see SpectrogramLayer::setVerticalZoomStep
Chris@439 368
Chris@904 369 newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@439 370 newmin = newmax - newdist;
Chris@439 371
Chris@682 372 // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
Chris@439 373
Chris@439 374 } else {
Chris@904 375 double dmid = (dmax + dmin) / 2;
Chris@439 376 newmin = dmid - newdist / 2;
Chris@439 377 newmax = dmid + newdist / 2;
Chris@439 378 }
Chris@439 379
Chris@439 380 if (newmin < min) {
Chris@439 381 newmax += (min - newmin);
Chris@439 382 newmin = min;
Chris@439 383 }
Chris@439 384 if (newmax > max) {
Chris@439 385 newmax = max;
Chris@439 386 }
Chris@439 387
Chris@667 388 #ifdef DEBUG_NOTE_LAYER
Chris@684 389 cerr << "FlexiNoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@667 390 #endif
Chris@439 391
Chris@439 392 setDisplayExtents(newmin, newmax);
Chris@439 393 }
Chris@439 394
Chris@439 395 RangeMapper *
matthiasm@620 396 FlexiNoteLayer::getNewVerticalZoomRangeMapper() const
Chris@439 397 {
Chris@1469 398 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 399 if (!model) return nullptr;
Chris@439 400
Chris@439 401 RangeMapper *mapper;
Chris@439 402
Chris@904 403 double min, max;
Chris@439 404 bool logarithmic;
Chris@439 405 QString unit;
Chris@439 406 getValueExtents(min, max, logarithmic, unit);
Chris@439 407
Chris@1408 408 if (min == max) return nullptr;
Chris@439 409
Chris@439 410 if (logarithmic) {
Chris@439 411 mapper = new LogRangeMapper(0, 100, min, max, unit);
Chris@439 412 } else {
Chris@439 413 mapper = new LinearRangeMapper(0, 100, min, max, unit);
Chris@439 414 }
Chris@439 415
Chris@439 416 return mapper;
Chris@439 417 }
Chris@439 418
Chris@1426 419 EventVector
Chris@916 420 FlexiNoteLayer::getLocalPoints(LayerGeometryProvider *v, int x) const
Chris@30 421 {
Chris@1469 422 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 423 if (!model) return {};
Chris@1426 424
Chris@904 425 sv_frame_t frame = v->getFrameForX(x);
Chris@30 426
Chris@1469 427 EventVector local = model->getEventsCovering(frame);
Chris@1426 428 if (!local.empty()) return local;
Chris@30 429
Chris@1426 430 int fuzz = ViewManager::scalePixelSize(2);
Chris@1426 431 sv_frame_t start = v->getFrameForX(x - fuzz);
Chris@1426 432 sv_frame_t end = v->getFrameForX(x + fuzz);
Chris@30 433
Chris@1469 434 local = model->getEventsStartingWithin(frame, end - frame);
Chris@1426 435 if (!local.empty()) return local;
Chris@30 436
Chris@1469 437 local = model->getEventsSpanning(start, frame - start);
Chris@1426 438 if (!local.empty()) return local;
Chris@30 439
Chris@1426 440 return {};
Chris@30 441 }
Chris@30 442
Chris@550 443 bool
Chris@1426 444 FlexiNoteLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, Event &point) const
Chris@550 445 {
Chris@1469 446 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 447 if (!model) return false;
Chris@550 448
Chris@904 449 sv_frame_t frame = v->getFrameForX(x);
Chris@550 450
Chris@1469 451 EventVector onPoints = model->getEventsCovering(frame);
Chris@550 452 if (onPoints.empty()) return false;
Chris@550 453
Chris@550 454 int nearestDistance = -1;
Chris@1426 455 for (const auto &p: onPoints) {
Chris@1426 456 int distance = getYForValue(v, p.getValue()) - y;
Chris@550 457 if (distance < 0) distance = -distance;
Chris@550 458 if (nearestDistance == -1 || distance < nearestDistance) {
Chris@550 459 nearestDistance = distance;
Chris@1426 460 point = p;
Chris@550 461 }
Chris@550 462 }
Chris@550 463
Chris@550 464 return true;
Chris@550 465 }
Chris@550 466
gyorgyf@646 467 bool
Chris@1426 468 FlexiNoteLayer::getNoteToEdit(LayerGeometryProvider *v, int x, int y, Event &point) const
gyorgyf@646 469 {
gyorgyf@647 470 // GF: find the note that is closest to the cursor
Chris@1469 471 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 472 if (!model) return false;
gyorgyf@646 473
Chris@904 474 sv_frame_t frame = v->getFrameForX(x);
gyorgyf@646 475
Chris@1469 476 EventVector onPoints = model->getEventsCovering(frame);
gyorgyf@646 477 if (onPoints.empty()) return false;
gyorgyf@646 478
gyorgyf@646 479 int nearestDistance = -1;
Chris@1426 480 for (const auto &p: onPoints) {
Chris@1426 481 int distance = getYForValue(v, p.getValue()) - y;
gyorgyf@646 482 if (distance < 0) distance = -distance;
gyorgyf@646 483 if (nearestDistance == -1 || distance < nearestDistance) {
gyorgyf@646 484 nearestDistance = distance;
Chris@1426 485 point = p;
gyorgyf@646 486 }
gyorgyf@646 487 }
gyorgyf@646 488
gyorgyf@646 489 return true;
gyorgyf@646 490 }
gyorgyf@646 491
Chris@30 492 QString
Chris@916 493 FlexiNoteLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@30 494 {
Chris@30 495 int x = pos.x();
Chris@30 496
Chris@1469 497 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 498 if (!model || !model->getSampleRate()) return "";
Chris@30 499
Chris@1426 500 EventVector points = getLocalPoints(v, x);
Chris@30 501
Chris@30 502 if (points.empty()) {
Chris@1469 503 if (!model->isReady()) {
Chris@714 504 return tr("In progress");
Chris@714 505 } else {
Chris@714 506 return tr("No local points");
Chris@714 507 }
Chris@30 508 }
Chris@30 509
Chris@1426 510 Event note(0);
Chris@1426 511 EventVector::iterator i;
Chris@30 512
Chris@30 513 for (i = points.begin(); i != points.end(); ++i) {
Chris@30 514
Chris@1426 515 int y = getYForValue(v, i->getValue());
Chris@714 516 int h = NOTE_HEIGHT; // GF: larger notes
Chris@30 517
Chris@1469 518 if (model->getValueQuantization() != 0.0) {
Chris@1426 519 h = y - getYForValue
Chris@1469 520 (v, i->getValue() + model->getValueQuantization());
Chris@714 521 if (h < NOTE_HEIGHT) h = NOTE_HEIGHT;
Chris@714 522 }
Chris@30 523
Chris@714 524 // GF: this is not quite correct
Chris@714 525 if (pos.y() >= y - 4 && pos.y() <= y + h) {
Chris@714 526 note = *i;
Chris@714 527 break;
Chris@714 528 }
Chris@30 529 }
Chris@30 530
Chris@30 531 if (i == points.end()) return tr("No local points");
Chris@30 532
Chris@1426 533 RealTime rt = RealTime::frame2RealTime(note.getFrame(),
Chris@1469 534 model->getSampleRate());
Chris@1426 535 RealTime rd = RealTime::frame2RealTime(note.getDuration(),
Chris@1469 536 model->getSampleRate());
Chris@30 537
Chris@101 538 QString pitchText;
Chris@101 539
Chris@101 540 if (shouldConvertMIDIToHz()) {
Chris@101 541
Chris@1426 542 int mnote = int(lrint(note.getValue()));
Chris@1426 543 int cents = int(lrint((note.getValue() - double(mnote)) * 100));
Chris@904 544 double freq = Pitch::getFrequencyForPitch(mnote, cents);
Chris@544 545 pitchText = tr("%1 (%2, %3 Hz)")
Chris@544 546 .arg(Pitch::getPitchLabel(mnote, cents))
Chris@544 547 .arg(mnote)
Chris@544 548 .arg(freq);
Chris@101 549
Chris@701 550 } else if (getScaleUnits() == "Hz") {
Chris@101 551
Chris@544 552 pitchText = tr("%1 Hz (%2, %3)")
Chris@1426 553 .arg(note.getValue())
Chris@1426 554 .arg(Pitch::getPitchLabelForFrequency(note.getValue()))
Chris@1426 555 .arg(Pitch::getPitchForFrequency(note.getValue()));
Chris@101 556
Chris@101 557 } else {
Chris@234 558 pitchText = tr("%1 %2")
Chris@1426 559 .arg(note.getValue()).arg(getScaleUnits());
Chris@101 560 }
Chris@101 561
Chris@30 562 QString text;
Chris@30 563
Chris@1426 564 if (note.getLabel() == "") {
Chris@714 565 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
Chris@714 566 .arg(rt.toText(true).c_str())
Chris@714 567 .arg(pitchText)
Chris@714 568 .arg(rd.toText(true).c_str());
Chris@30 569 } else {
Chris@714 570 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@714 571 .arg(rt.toText(true).c_str())
Chris@714 572 .arg(pitchText)
Chris@714 573 .arg(rd.toText(true).c_str())
Chris@1426 574 .arg(note.getLabel());
Chris@30 575 }
Chris@30 576
Chris@1426 577 pos = QPoint(v->getXForFrame(note.getFrame()),
Chris@1426 578 getYForValue(v, note.getValue()));
Chris@30 579 return text;
Chris@30 580 }
Chris@30 581
Chris@30 582 bool
Chris@916 583 FlexiNoteLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 584 int &resolution,
Chris@714 585 SnapType snap) const
Chris@30 586 {
Chris@1469 587 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 588 if (!model) {
Chris@714 589 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@30 590 }
Chris@30 591
Chris@1469 592 resolution = model->getResolution();
Chris@1426 593 EventVector points;
Chris@30 594
Chris@30 595 if (snap == SnapNeighbouring) {
gyorgyf@646 596
Chris@714 597 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@714 598 if (points.empty()) return false;
Chris@1426 599 frame = points.begin()->getFrame();
Chris@714 600 return true;
Chris@30 601 }
Chris@30 602
Chris@1469 603 points = model->getEventsCovering(frame);
Chris@904 604 sv_frame_t snapped = frame;
Chris@30 605 bool found = false;
Chris@30 606
Chris@1426 607 for (EventVector::const_iterator i = points.begin();
Chris@714 608 i != points.end(); ++i) {
Chris@30 609
Chris@714 610 if (snap == SnapRight) {
Chris@30 611
Chris@1426 612 if (i->getFrame() > frame) {
Chris@1426 613 snapped = i->getFrame();
Chris@714 614 found = true;
Chris@714 615 break;
Chris@1426 616 } else if (i->getFrame() + i->getDuration() >= frame) {
Chris@1426 617 snapped = i->getFrame() + i->getDuration();
Chris@715 618 found = true;
Chris@715 619 break;
Chris@714 620 }
Chris@714 621
Chris@714 622 } else if (snap == SnapLeft) {
Chris@714 623
Chris@1426 624 if (i->getFrame() <= frame) {
Chris@1426 625 snapped = i->getFrame();
Chris@714 626 found = true; // don't break, as the next may be better
Chris@714 627 } else {
Chris@714 628 break;
Chris@714 629 }
Chris@714 630
Chris@714 631 } else { // nearest
Chris@714 632
Chris@1426 633 EventVector::const_iterator j = i;
Chris@714 634 ++j;
Chris@714 635
Chris@714 636 if (j == points.end()) {
Chris@714 637
Chris@1426 638 snapped = i->getFrame();
Chris@714 639 found = true;
Chris@714 640 break;
Chris@714 641
Chris@1426 642 } else if (j->getFrame() >= frame) {
Chris@714 643
Chris@1426 644 if (j->getFrame() - frame < frame - i->getFrame()) {
Chris@1426 645 snapped = j->getFrame();
Chris@714 646 } else {
Chris@1426 647 snapped = i->getFrame();
Chris@714 648 }
Chris@714 649 found = true;
Chris@714 650 break;
Chris@714 651 }
gyorgyf@646 652 }
Chris@30 653 }
Chris@30 654
Chris@715 655 cerr << "snapToFeatureFrame: frame " << frame << " -> snapped " << snapped << ", found = " << found << endl;
Chris@715 656
Chris@30 657 frame = snapped;
Chris@30 658 return found;
Chris@30 659 }
Chris@30 660
Chris@101 661 void
Chris@916 662 FlexiNoteLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const
Chris@30 663 {
Chris@101 664 min = 0.0;
Chris@101 665 max = 0.0;
Chris@101 666 log = false;
Chris@42 667
Chris@101 668 QString queryUnits;
Chris@101 669 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
Chris@701 670 else queryUnits = getScaleUnits();
Chris@30 671
Chris@439 672 if (shouldAutoAlign()) {
Chris@30 673
Chris@101 674 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@30 675
Chris@1469 676 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 677 min = model->getValueMinimum();
Chris@1469 678 max = model->getValueMaximum();
Chris@42 679
Chris@101 680 if (shouldConvertMIDIToHz()) {
Chris@904 681 min = Pitch::getFrequencyForPitch(int(lrint(min)));
Chris@904 682 max = Pitch::getFrequencyForPitch(int(lrint(max + 1)));
Chris@101 683 }
Chris@42 684
Chris@667 685 #ifdef DEBUG_NOTE_LAYER
Chris@684 686 cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@667 687 #endif
Chris@105 688
Chris@101 689 } else if (log) {
Chris@101 690
Chris@197 691 LogRange::mapRange(min, max);
Chris@105 692
Chris@667 693 #ifdef DEBUG_NOTE_LAYER
Chris@684 694 cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@667 695 #endif
Chris@101 696 }
Chris@101 697
Chris@101 698 } else {
Chris@101 699
Chris@439 700 getDisplayExtents(min, max);
Chris@101 701
Chris@101 702 if (m_verticalScale == MIDIRangeScale) {
Chris@101 703 min = Pitch::getFrequencyForPitch(0);
matthiasm@623 704 max = Pitch::getFrequencyForPitch(70);
Chris@101 705 } else if (shouldConvertMIDIToHz()) {
Chris@904 706 min = Pitch::getFrequencyForPitch(int(lrint(min)));
Chris@904 707 max = Pitch::getFrequencyForPitch(int(lrint(max + 1)));
Chris@101 708 }
Chris@101 709
Chris@101 710 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@197 711 LogRange::mapRange(min, max);
Chris@101 712 log = true;
Chris@101 713 }
Chris@30 714 }
Chris@30 715
Chris@101 716 if (max == min) max = min + 1.0;
Chris@101 717 }
Chris@30 718
Chris@101 719 int
Chris@916 720 FlexiNoteLayer::getYForValue(LayerGeometryProvider *v, double val) const
Chris@101 721 {
Chris@904 722 double min = 0.0, max = 0.0;
Chris@101 723 bool logarithmic = false;
Chris@916 724 int h = v->getPaintHeight();
Chris@101 725
Chris@101 726 getScaleExtents(v, min, max, logarithmic);
Chris@101 727
Chris@667 728 #ifdef DEBUG_NOTE_LAYER
Chris@684 729 cerr << "FlexiNoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl;
Chris@667 730 #endif
Chris@101 731
Chris@101 732 if (shouldConvertMIDIToHz()) {
Chris@904 733 val = Pitch::getFrequencyForPitch(int(lrint(val)),
Chris@904 734 int(lrint((val - floor(val)) * 100.0)));
Chris@667 735 #ifdef DEBUG_NOTE_LAYER
Chris@682 736 cerr << "shouldConvertMIDIToHz true, val now = " << val << endl;
Chris@667 737 #endif
Chris@101 738 }
Chris@101 739
Chris@101 740 if (logarithmic) {
Chris@197 741 val = LogRange::map(val);
Chris@667 742 #ifdef DEBUG_NOTE_LAYER
Chris@682 743 cerr << "logarithmic true, val now = " << val << endl;
Chris@667 744 #endif
Chris@101 745 }
Chris@101 746
Chris@101 747 int y = int(h - ((val - min) * h) / (max - min)) - 1;
Chris@667 748 #ifdef DEBUG_NOTE_LAYER
Chris@682 749 cerr << "y = " << y << endl;
Chris@667 750 #endif
Chris@101 751 return y;
Chris@30 752 }
Chris@30 753
Chris@904 754 double
Chris@916 755 FlexiNoteLayer::getValueForY(LayerGeometryProvider *v, int y) const
Chris@30 756 {
Chris@904 757 double min = 0.0, max = 0.0;
Chris@101 758 bool logarithmic = false;
Chris@916 759 int h = v->getPaintHeight();
Chris@30 760
Chris@101 761 getScaleExtents(v, min, max, logarithmic);
Chris@101 762
Chris@904 763 double val = min + (double(h - y) * double(max - min)) / h;
Chris@101 764
Chris@101 765 if (logarithmic) {
Chris@904 766 val = pow(10.f, val);
Chris@101 767 }
Chris@101 768
Chris@101 769 if (shouldConvertMIDIToHz()) {
Chris@101 770 val = Pitch::getPitchForFrequency(val);
Chris@101 771 }
Chris@101 772
Chris@101 773 return val;
Chris@30 774 }
Chris@30 775
Chris@439 776 bool
matthiasm@620 777 FlexiNoteLayer::shouldAutoAlign() const
Chris@439 778 {
Chris@439 779 return (m_verticalScale == AutoAlignScale);
Chris@439 780 }
Chris@439 781
Chris@30 782 void
Chris@916 783 FlexiNoteLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@30 784 {
Chris@1469 785 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 786 if (!model || !model->isOK()) return;
Chris@30 787
Chris@1469 788 sv_samplerate_t sampleRate = model->getSampleRate();
Chris@30 789 if (!sampleRate) return;
Chris@30 790
matthiasm@620 791 // Profiler profiler("FlexiNoteLayer::paint", true);
Chris@30 792
Chris@1426 793 int x0 = rect.left(), x1 = rect.right();
Chris@1426 794 sv_frame_t frame0 = v->getFrameForX(x0);
Chris@904 795 sv_frame_t frame1 = v->getFrameForX(x1);
Chris@30 796
Chris@1469 797 EventVector points(model->getEventsSpanning(frame0, frame1 - frame0));
Chris@30 798 if (points.empty()) return;
Chris@30 799
Chris@287 800 paint.setPen(getBaseQColor());
Chris@30 801
Chris@287 802 QColor brushColour(getBaseQColor());
Chris@30 803 brushColour.setAlpha(80);
Chris@30 804
matthiasm@620 805 // SVDEBUG << "FlexiNoteLayer::paint: resolution is "
Chris@1469 806 // << model->getResolution() << " frames" << endl;
Chris@30 807
Chris@1469 808 double min = model->getValueMinimum();
Chris@1469 809 double max = model->getValueMaximum();
Chris@30 810 if (max == min) max = min + 1.0;
Chris@30 811
Chris@30 812 QPoint localPos;
Chris@1426 813 Event illuminatePoint(0);
Chris@551 814 bool shouldIlluminate = false;
Chris@30 815
Chris@44 816 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@551 817 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@551 818 illuminatePoint);
Chris@30 819 }
Chris@30 820
Chris@30 821 paint.save();
Chris@30 822 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 823
matthiasm@819 824 int noteNumber = 0;
matthiasm@819 825
Chris@1426 826 for (EventVector::const_iterator i = points.begin();
Chris@714 827 i != points.end(); ++i) {
Chris@30 828
matthiasm@819 829 ++noteNumber;
Chris@1426 830 const Event &p(*i);
Chris@30 831
Chris@1426 832 int x = v->getXForFrame(p.getFrame());
Chris@1426 833 int y = getYForValue(v, p.getValue());
Chris@1426 834 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
Chris@714 835 int h = NOTE_HEIGHT; //GF: larger notes
gyorgyf@646 836
Chris@1469 837 if (model->getValueQuantization() != 0.0) {
Chris@1469 838 h = y - getYForValue(v, p.getValue() + model->getValueQuantization());
Chris@714 839 if (h < NOTE_HEIGHT) h = NOTE_HEIGHT; //GF: larger notes
Chris@714 840 }
Chris@30 841
Chris@714 842 if (w < 1) w = 1;
Chris@714 843 paint.setPen(getBaseQColor());
Chris@714 844 paint.setBrush(brushColour);
Chris@30 845
Chris@1426 846 if (shouldIlluminate && illuminatePoint == p) {
matthiasm@784 847
Chris@1426 848 paint.drawLine(x, -1, x, v->getPaintHeight() + 1);
Chris@1426 849 paint.drawLine(x+w, -1, x+w, v->getPaintHeight() + 1);
matthiasm@784 850
Chris@1426 851 paint.setPen(v->getForeground());
matthiasm@784 852
Chris@1426 853 QString vlabel = tr("freq: %1%2")
Chris@1469 854 .arg(p.getValue()).arg(model->getScaleUnits());
Chris@1426 855 PaintAssistant::drawVisibleText
Chris@1426 856 (v, paint,
Chris@1426 857 x,
Chris@1426 858 y - h/2 - 2 - paint.fontMetrics().height()
Chris@1426 859 - paint.fontMetrics().descent(),
Chris@1426 860 vlabel, PaintAssistant::OutlinedText);
matthiasm@793 861
Chris@1426 862 QString hlabel = tr("dur: %1")
Chris@1426 863 .arg(RealTime::frame2RealTime
Chris@1469 864 (p.getDuration(), model->getSampleRate()).toText(true)
Chris@1426 865 .c_str());
Chris@1426 866 PaintAssistant::drawVisibleText
Chris@1426 867 (v, paint,
Chris@1426 868 x,
Chris@1426 869 y - h/2 - paint.fontMetrics().descent() - 2,
Chris@1426 870 hlabel, PaintAssistant::OutlinedText);
matthiasm@793 871
Chris@1426 872 QString llabel = QString("%1").arg(p.getLabel());
Chris@1426 873 PaintAssistant::drawVisibleText
Chris@1426 874 (v, paint,
Chris@1426 875 x,
Chris@1426 876 y + h + 2 + paint.fontMetrics().descent(),
Chris@1426 877 llabel, PaintAssistant::OutlinedText);
Chris@1426 878
Chris@1426 879 QString nlabel = QString("%1").arg(noteNumber);
Chris@1426 880 PaintAssistant::drawVisibleText
Chris@1426 881 (v, paint,
Chris@1426 882 x + paint.fontMetrics().averageCharWidth() / 2,
Chris@1426 883 y + h/2 - paint.fontMetrics().descent(),
Chris@1426 884 nlabel, PaintAssistant::OutlinedText);
matthiasm@784 885 }
gyorgyf@646 886
Chris@714 887 paint.drawRect(x, y - h/2, w, h);
Chris@30 888 }
Chris@30 889
Chris@30 890 paint.restore();
Chris@30 891 }
Chris@30 892
Chris@692 893 int
Chris@916 894 FlexiNoteLayer::getVerticalScaleWidth(LayerGeometryProvider *v, bool, QPainter &paint) const
Chris@692 895 {
Chris@1469 896 if (shouldAutoAlign()) {
Chris@696 897 return 0;
Chris@701 898 } else {
Chris@701 899 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@701 900 return LogNumericalScale().getWidth(v, paint) + 10; // for piano
Chris@701 901 } else {
Chris@701 902 return LinearNumericalScale().getWidth(v, paint);
Chris@701 903 }
Chris@696 904 }
Chris@692 905 }
Chris@692 906
Chris@692 907 void
Chris@916 908 FlexiNoteLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect) const
Chris@692 909 {
Chris@1469 910 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 911 if (!model || model->isEmpty()) return;
Chris@701 912
Chris@701 913 QString unit;
Chris@904 914 double min, max;
Chris@701 915 bool logarithmic;
Chris@701 916
Chris@701 917 int w = getVerticalScaleWidth(v, false, paint);
Chris@916 918 int h = v->getPaintHeight();
Chris@701 919
Chris@701 920 getScaleExtents(v, min, max, logarithmic);
Chris@701 921
Chris@701 922 if (logarithmic) {
Chris@701 923 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@696 924 } else {
Chris@701 925 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 926 }
Chris@701 927
Chris@701 928 if (logarithmic && (getScaleUnits() == "Hz")) {
Chris@696 929 PianoScale().paintPianoVertical
Chris@701 930 (v, paint, QRect(w - 10, 0, 10, h),
Chris@701 931 LogRange::unmap(min),
Chris@701 932 LogRange::unmap(max));
Chris@701 933 paint.drawLine(w, 0, w, h);
Chris@701 934 }
Chris@701 935
Chris@701 936 if (getScaleUnits() != "") {
Chris@701 937 int mw = w - 5;
Chris@701 938 paint.drawText(5,
Chris@701 939 5 + paint.fontMetrics().ascent(),
Chris@701 940 TextAbbrev::abbreviate(getScaleUnits(),
Chris@701 941 paint.fontMetrics(),
Chris@701 942 mw));
Chris@696 943 }
Chris@692 944 }
Chris@692 945
Chris@30 946 void
Chris@916 947 FlexiNoteLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 948 {
matthiasm@620 949 // SVDEBUG << "FlexiNoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 950
Chris@1469 951 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 952 if (!model) return;
Chris@30 953
Chris@904 954 sv_frame_t frame = v->getFrameForX(e->x());
Chris@30 955 if (frame < 0) frame = 0;
Chris@1469 956 frame = frame / model->getResolution() * model->getResolution();
Chris@30 957
Chris@904 958 double value = getValueForY(v, e->y());
Chris@30 959
Chris@1426 960 m_editingPoint = Event(frame, float(value), 0, 0.8f, tr("New Point"));
Chris@30 961 m_originalPoint = m_editingPoint;
Chris@30 962
Chris@376 963 if (m_editingCommand) finish(m_editingCommand);
Chris@1469 964 m_editingCommand = new ChangeEventsCommand<Model>(m_model, tr("Draw Point"));
Chris@1426 965 m_editingCommand->add(m_editingPoint);
Chris@30 966
Chris@30 967 m_editing = true;
Chris@30 968 }
Chris@30 969
Chris@30 970 void
Chris@916 971 FlexiNoteLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 972 {
matthiasm@620 973 // SVDEBUG << "FlexiNoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 974
Chris@1469 975 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 976 if (!model || !m_editing) return;
Chris@30 977
Chris@904 978 sv_frame_t frame = v->getFrameForX(e->x());
Chris@30 979 if (frame < 0) frame = 0;
Chris@1469 980 frame = frame / model->getResolution() * model->getResolution();
Chris@30 981
Chris@904 982 double newValue = getValueForY(v, e->y());
Chris@101 983
Chris@1426 984 sv_frame_t newFrame = m_editingPoint.getFrame();
Chris@904 985 sv_frame_t newDuration = frame - newFrame;
Chris@101 986 if (newDuration < 0) {
Chris@101 987 newFrame = frame;
Chris@101 988 newDuration = -newDuration;
Chris@101 989 } else if (newDuration == 0) {
Chris@101 990 newDuration = 1;
Chris@101 991 }
Chris@30 992
Chris@1426 993 m_editingCommand->remove(m_editingPoint);
Chris@1426 994 m_editingPoint = m_editingPoint
Chris@1426 995 .withFrame(newFrame)
Chris@1426 996 .withValue(float(newValue))
Chris@1426 997 .withDuration(newDuration);
Chris@1426 998 m_editingCommand->add(m_editingPoint);
Chris@30 999 }
Chris@30 1000
Chris@30 1001 void
Chris@916 1002 FlexiNoteLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@30 1003 {
matthiasm@620 1004 // SVDEBUG << "FlexiNoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@1469 1005 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1006 if (!model || !m_editing) return;
Chris@376 1007 finish(m_editingCommand);
Chris@1408 1008 m_editingCommand = nullptr;
Chris@30 1009 m_editing = false;
Chris@30 1010 }
Chris@30 1011
Chris@30 1012 void
Chris@916 1013 FlexiNoteLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@335 1014 {
Chris@1469 1015 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1016 if (!model) return;
Chris@335 1017
Chris@550 1018 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 1019
Chris@335 1020 if (m_editingCommand) {
Chris@714 1021 finish(m_editingCommand);
Chris@1408 1022 m_editingCommand = nullptr;
Chris@335 1023 }
Chris@335 1024
Chris@335 1025 m_editing = true;
Chris@335 1026 }
Chris@335 1027
Chris@335 1028 void
Chris@916 1029 FlexiNoteLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
Chris@335 1030 {
Chris@335 1031 }
Chris@335 1032
Chris@335 1033 void
Chris@916 1034 FlexiNoteLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@335 1035 {
Chris@1469 1036 if (!m_editing) return;
Chris@335 1037 m_editing = false;
Chris@335 1038
Chris@1426 1039 Event p(0);
Chris@550 1040 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@1469 1041 if (p.getFrame() != m_editingPoint.getFrame() ||
Chris@1469 1042 p.getValue() != m_editingPoint.getValue()) return;
Chris@550 1043
Chris@1469 1044 m_editingCommand = new ChangeEventsCommand<Model>(m_model, tr("Erase Point"));
Chris@1426 1045 m_editingCommand->remove(m_editingPoint);
Chris@376 1046 finish(m_editingCommand);
Chris@1408 1047 m_editingCommand = nullptr;
Chris@335 1048 m_editing = false;
Chris@335 1049 }
Chris@335 1050
Chris@335 1051 void
Chris@916 1052 FlexiNoteLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 1053 {
matthiasm@620 1054 // SVDEBUG << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 1055 std::cerr << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 1056
Chris@1469 1057 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1058 if (!model) return;
Chris@30 1059
Chris@550 1060 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@1426 1061 m_originalPoint = m_editingPoint;
gyorgyf@649 1062
matthiasm@651 1063 if (m_editMode == RightBoundary) {
Chris@1426 1064 m_dragPointX = v->getXForFrame
Chris@1426 1065 (m_editingPoint.getFrame() + m_editingPoint.getDuration());
gyorgyf@649 1066 } else {
Chris@1426 1067 m_dragPointX = v->getXForFrame
Chris@1426 1068 (m_editingPoint.getFrame());
gyorgyf@649 1069 }
Chris@1426 1070 m_dragPointY = getYForValue(v, m_editingPoint.getValue());
Chris@551 1071
Chris@30 1072 if (m_editingCommand) {
Chris@714 1073 finish(m_editingCommand);
Chris@1408 1074 m_editingCommand = nullptr;
Chris@30 1075 }
Chris@30 1076
Chris@30 1077 m_editing = true;
Chris@551 1078 m_dragStartX = e->x();
Chris@551 1079 m_dragStartY = e->y();
matthiasm@651 1080
Chris@1426 1081 sv_frame_t onset = m_originalPoint.getFrame();
Chris@1426 1082 sv_frame_t offset =
Chris@1426 1083 m_originalPoint.getFrame() +
Chris@1426 1084 m_originalPoint.getDuration() - 1;
matthiasm@651 1085
matthiasm@651 1086 m_greatestLeftNeighbourFrame = -1;
Chris@806 1087 m_smallestRightNeighbourFrame = std::numeric_limits<int>::max();
Chris@1426 1088
Chris@1469 1089 EventVector allEvents = model->getAllEvents();
matthiasm@651 1090
Chris@1426 1091 for (auto currentNote: allEvents) {
matthiasm@651 1092
matthiasm@651 1093 // left boundary
Chris@1426 1094 if (currentNote.getFrame() + currentNote.getDuration() - 1 < onset) {
Chris@1426 1095 m_greatestLeftNeighbourFrame =
Chris@1426 1096 currentNote.getFrame() + currentNote.getDuration() - 1;
matthiasm@651 1097 }
matthiasm@651 1098
matthiasm@651 1099 // right boundary
Chris@1426 1100 if (currentNote.getFrame() > offset) {
Chris@1426 1101 m_smallestRightNeighbourFrame = currentNote.getFrame();
matthiasm@651 1102 break;
matthiasm@651 1103 }
matthiasm@651 1104 }
Chris@1426 1105
Chris@753 1106 std::cerr << "editStart: mode is " << m_editMode << ", note frame: " << onset << ", left boundary: " << m_greatestLeftNeighbourFrame << ", right boundary: " << m_smallestRightNeighbourFrame << std::endl;
Chris@30 1107 }
Chris@30 1108
Chris@30 1109 void
Chris@916 1110 FlexiNoteLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 1111 {
matthiasm@620 1112 // SVDEBUG << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 1113 std::cerr << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 1114
Chris@1469 1115 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1116 if (!model || !m_editing) return;
Chris@30 1117
Chris@551 1118 int xdist = e->x() - m_dragStartX;
Chris@551 1119 int ydist = e->y() - m_dragStartY;
Chris@551 1120 int newx = m_dragPointX + xdist;
Chris@551 1121 int newy = m_dragPointY + ydist;
Chris@551 1122
Chris@904 1123 sv_frame_t dragFrame = v->getFrameForX(newx);
matthiasm@657 1124 if (dragFrame < 0) dragFrame = 0;
Chris@1469 1125 dragFrame = dragFrame / model->getResolution() * model->getResolution();
matthiasm@651 1126
Chris@904 1127 double value = getValueForY(v, newy);
Chris@30 1128
Chris@30 1129 if (!m_editingCommand) {
Chris@1469 1130 m_editingCommand =
Chris@1469 1131 new ChangeEventsCommand<Model>(m_model, tr("Drag Point"));
Chris@30 1132 }
Chris@1426 1133 m_editingCommand->remove(m_editingPoint);
matthiasm@651 1134
Chris@874 1135 std::cerr << "edit mode: " << m_editMode << " intelligent actions = "
Chris@874 1136 << m_intelligentActions << std::endl;
matthiasm@657 1137
matthiasm@651 1138 switch (m_editMode) {
Chris@1426 1139
Chris@714 1140 case LeftBoundary : {
Chris@714 1141 // left
Chris@1426 1142 if (m_intelligentActions &&
Chris@1426 1143 dragFrame <= m_greatestLeftNeighbourFrame) {
Chris@1426 1144 dragFrame = m_greatestLeftNeighbourFrame + 1;
Chris@1426 1145 }
Chris@714 1146 // right
Chris@1426 1147 if (m_intelligentActions &&
Chris@1426 1148 dragFrame >= m_originalPoint.getFrame() + m_originalPoint.getDuration()) {
Chris@1426 1149 dragFrame = m_originalPoint.getFrame() + m_originalPoint.getDuration() - 1;
matthiasm@651 1150 }
Chris@1426 1151 m_editingPoint = m_editingPoint
Chris@1426 1152 .withFrame(dragFrame)
Chris@1426 1153 .withDuration(m_originalPoint.getFrame() -
Chris@1426 1154 dragFrame + m_originalPoint.getDuration());
Chris@714 1155 break;
Chris@714 1156 }
Chris@1426 1157
Chris@714 1158 case RightBoundary : {
Chris@714 1159 // left
Chris@1426 1160 if (m_intelligentActions &&
Chris@1426 1161 dragFrame <= m_greatestLeftNeighbourFrame) {
Chris@1426 1162 dragFrame = m_greatestLeftNeighbourFrame + 1;
Chris@1426 1163 }
Chris@1426 1164 if (m_intelligentActions &&
Chris@1426 1165 dragFrame >= m_smallestRightNeighbourFrame) {
Chris@1426 1166 dragFrame = m_smallestRightNeighbourFrame - 1;
Chris@1426 1167 }
Chris@1426 1168 m_editingPoint = m_editingPoint
Chris@1426 1169 .withDuration(dragFrame - m_originalPoint.getFrame() + 1);
Chris@714 1170 break;
Chris@714 1171 }
Chris@1426 1172
Chris@714 1173 case DragNote : {
Chris@714 1174 // left
Chris@1426 1175 if (m_intelligentActions &&
Chris@1426 1176 dragFrame <= m_greatestLeftNeighbourFrame) {
Chris@1426 1177 dragFrame = m_greatestLeftNeighbourFrame + 1;
Chris@1426 1178 }
Chris@714 1179 // right
Chris@1426 1180 if (m_intelligentActions &&
Chris@1426 1181 dragFrame + m_originalPoint.getDuration() >= m_smallestRightNeighbourFrame) {
Chris@1426 1182 dragFrame = m_smallestRightNeighbourFrame - m_originalPoint.getDuration();
matthiasm@651 1183 }
Chris@1426 1184
Chris@1426 1185 m_editingPoint = m_editingPoint
Chris@1426 1186 .withFrame(dragFrame)
Chris@1426 1187 .withValue(float(value));
Chris@875 1188
Chris@875 1189 // Re-analyse region within +/- 1 semitone of the dragged value
Chris@875 1190 float cents = 0;
Chris@1426 1191 int midiPitch = Pitch::getPitchForFrequency(m_editingPoint.getValue(), &cents);
Chris@922 1192 double lower = Pitch::getFrequencyForPitch(midiPitch - 1, cents);
Chris@922 1193 double higher = Pitch::getFrequencyForPitch(midiPitch + 1, cents);
Chris@875 1194
Chris@1426 1195 emit reAnalyseRegion(m_editingPoint.getFrame(),
Chris@1426 1196 m_editingPoint.getFrame() +
Chris@1426 1197 m_editingPoint.getDuration(),
Chris@922 1198 float(lower), float(higher));
Chris@714 1199 break;
Chris@714 1200 }
Chris@1426 1201
Chris@805 1202 case SplitNote: // nothing
Chris@805 1203 break;
gyorgyf@649 1204 }
Chris@875 1205
Chris@1426 1206 m_editingCommand->add(m_editingPoint);
Chris@875 1207
Chris@1426 1208 std::cerr << "added new point(" << m_editingPoint.getFrame() << "," << m_editingPoint.getDuration() << ")" << std::endl;
Chris@30 1209 }
Chris@30 1210
Chris@30 1211 void
Chris@948 1212 FlexiNoteLayer::editEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@30 1213 {
Chris@1426 1214 std::cerr << "FlexiNoteLayer::editEnd("
Chris@1426 1215 << e->x() << "," << e->y() << ")" << std::endl;
matthiasm@656 1216
Chris@1469 1217 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1218 if (!model || !m_editing) return;
Chris@30 1219
Chris@30 1220 if (m_editingCommand) {
Chris@30 1221
Chris@714 1222 QString newName = m_editingCommand->getName();
Chris@30 1223
Chris@876 1224 if (m_editMode == DragNote) {
Chris@876 1225 //!!! command nesting is wrong?
Chris@876 1226 emit materialiseReAnalysis();
Chris@876 1227 }
Chris@876 1228
Chris@1426 1229 m_editingCommand->remove(m_editingPoint);
Chris@875 1230 updateNoteValueFromPitchCurve(v, m_editingPoint);
Chris@1426 1231 m_editingCommand->add(m_editingPoint);
Chris@875 1232
Chris@1426 1233 if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) {
Chris@1426 1234 if (m_editingPoint.getValue() != m_originalPoint.getValue()) {
Chris@714 1235 newName = tr("Edit Point");
Chris@714 1236 } else {
Chris@714 1237 newName = tr("Relocate Point");
Chris@714 1238 }
gyorgyf@646 1239 } else {
Chris@714 1240 newName = tr("Change Point Value");
gyorgyf@646 1241 }
Chris@30 1242
Chris@714 1243 m_editingCommand->setName(newName);
Chris@714 1244 finish(m_editingCommand);
Chris@30 1245 }
Chris@30 1246
Chris@1408 1247 m_editingCommand = nullptr;
Chris@30 1248 m_editing = false;
Chris@30 1249 }
Chris@30 1250
gyorgyf@635 1251 void
Chris@916 1252 FlexiNoteLayer::splitStart(LayerGeometryProvider *v, QMouseEvent *e)
gyorgyf@635 1253 {
Chris@1469 1254 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1255 if (!model) return;
Chris@1469 1256
gyorgyf@646 1257 // GF: note splitting starts (!! remove printing soon)
Chris@874 1258 std::cerr << "splitStart (n.b. editStart will be called later, if the user drags the mouse)" << std::endl;
gyorgyf@635 1259
gyorgyf@635 1260 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
gyorgyf@635 1261 // m_originalPoint = m_editingPoint;
gyorgyf@635 1262 //
Chris@1426 1263 // m_dragPointX = v->getXForFrame(m_editingPoint.getFrame());
Chris@1426 1264 // m_dragPointY = getYForValue(v, m_editingPoint.getValue());
gyorgyf@635 1265
gyorgyf@635 1266 if (m_editingCommand) {
Chris@714 1267 finish(m_editingCommand);
Chris@1408 1268 m_editingCommand = nullptr;
gyorgyf@635 1269 }
gyorgyf@635 1270
gyorgyf@635 1271 m_editing = true;
gyorgyf@635 1272 m_dragStartX = e->x();
gyorgyf@635 1273 m_dragStartY = e->y();
gyorgyf@635 1274 }
gyorgyf@635 1275
gyorgyf@635 1276 void
Chris@916 1277 FlexiNoteLayer::splitEnd(LayerGeometryProvider *v, QMouseEvent *e)
gyorgyf@635 1278 {
Chris@1469 1279 auto model = ModelById::getAs<NoteModel>(m_model);
gyorgyf@646 1280 // GF: note splitting ends. (!! remove printing soon)
gyorgyf@646 1281 std::cerr << "splitEnd" << std::endl;
Chris@1469 1282 if (!model || !m_editing || m_editMode != SplitNote) return;
gyorgyf@635 1283
gyorgyf@635 1284 int xdist = e->x() - m_dragStartX;
gyorgyf@635 1285 int ydist = e->y() - m_dragStartY;
gyorgyf@635 1286 if (xdist != 0 || ydist != 0) {
gyorgyf@646 1287 std::cerr << "mouse moved" << std::endl;
gyorgyf@635 1288 return;
gyorgyf@635 1289 }
gyorgyf@635 1290
Chris@904 1291 sv_frame_t frame = v->getFrameForX(e->x());
gyorgyf@635 1292
Chris@753 1293 splitNotesAt(v, frame, e);
Chris@746 1294 }
Chris@746 1295
Chris@746 1296 void
Chris@916 1297 FlexiNoteLayer::splitNotesAt(LayerGeometryProvider *v, sv_frame_t frame)
Chris@746 1298 {
Chris@1408 1299 splitNotesAt(v, frame, nullptr);
Chris@753 1300 }
Chris@753 1301
Chris@753 1302 void
Chris@916 1303 FlexiNoteLayer::splitNotesAt(LayerGeometryProvider *v, sv_frame_t frame, QMouseEvent *e)
Chris@753 1304 {
Chris@1469 1305 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1306 if (!model) return;
Chris@1469 1307
Chris@1469 1308 EventVector onPoints = model->getEventsCovering(frame);
Chris@746 1309 if (onPoints.empty()) return;
Chris@746 1310
Chris@1426 1311 Event note(*onPoints.begin());
gyorgyf@635 1312
Chris@1469 1313 auto command = new ChangeEventsCommand<Model>(m_model, tr("Edit Point"));
Chris@1426 1314 command->remove(note);
Chris@753 1315
Chris@753 1316 if (!e || !(e->modifiers() & Qt::ShiftModifier)) {
Chris@753 1317
Chris@753 1318 int gap = 0; // MM: I prefer a gap of 0, but we can decide later
Chris@753 1319
Chris@1426 1320 Event newNote1(note.getFrame(), note.getValue(),
Chris@1426 1321 frame - note.getFrame() - gap,
Chris@1426 1322 note.getLevel(), note.getLabel());
Chris@753 1323
Chris@1426 1324 Event newNote2(frame, note.getValue(),
Chris@1426 1325 note.getDuration() - newNote1.getDuration(),
Chris@1426 1326 note.getLevel(), note.getLabel());
Chris@747 1327
Chris@753 1328 if (m_intelligentActions) {
Chris@875 1329 if (updateNoteValueFromPitchCurve(v, newNote1)) {
Chris@1426 1330 command->add(newNote1);
Chris@753 1331 }
Chris@875 1332 if (updateNoteValueFromPitchCurve(v, newNote2)) {
Chris@1426 1333 command->add(newNote2);
Chris@753 1334 }
Chris@753 1335 } else {
Chris@1426 1336 command->add(newNote1);
Chris@1426 1337 command->add(newNote2);
Chris@747 1338 }
Chris@747 1339 }
Chris@746 1340
gyorgyf@635 1341 finish(command);
gyorgyf@646 1342 }
gyorgyf@646 1343
gyorgyf@655 1344 void
Chris@916 1345 FlexiNoteLayer::addNote(LayerGeometryProvider *v, QMouseEvent *e)
matthiasm@660 1346 {
Chris@1469 1347 auto model = ModelById::getAs<NoteModel>(m_model);
matthiasm@660 1348 std::cerr << "addNote" << std::endl;
Chris@1469 1349 if (!model) return;
matthiasm@660 1350
Chris@904 1351 sv_frame_t duration = 10000;
matthiasm@660 1352
Chris@904 1353 sv_frame_t frame = v->getFrameForX(e->x());
Chris@904 1354 double value = getValueForY(v, e->y());
matthiasm@660 1355
Chris@1469 1356 EventVector noteList = model->getAllEvents();
matthiasm@792 1357
matthiasm@660 1358 if (m_intelligentActions) {
Chris@904 1359 sv_frame_t smallestRightNeighbourFrame = 0;
Chris@1426 1360 for (EventVector::const_iterator i = noteList.begin();
matthiasm@792 1361 i != noteList.end(); ++i) {
Chris@1426 1362 Event currentNote = *i;
Chris@1426 1363 if (currentNote.getFrame() > frame) {
Chris@1426 1364 smallestRightNeighbourFrame = currentNote.getFrame();
matthiasm@660 1365 break;
matthiasm@660 1366 }
matthiasm@660 1367 }
matthiasm@792 1368 if (smallestRightNeighbourFrame > 0) {
matthiasm@792 1369 duration = std::min(smallestRightNeighbourFrame - frame + 1, duration);
matthiasm@792 1370 duration = (duration > 0) ? duration : 0;
matthiasm@792 1371 }
matthiasm@660 1372 }
matthiasm@660 1373
matthiasm@660 1374 if (!m_intelligentActions ||
Chris@1469 1375 (model->getEventsCovering(frame).empty() && duration > 0)) {
Chris@1426 1376 Event newNote(frame, float(value), duration, 100.f, tr("new note"));
Chris@1469 1377 auto command = new ChangeEventsCommand<Model>(m_model, tr("Add Point"));
Chris@1426 1378 command->add(newNote);
matthiasm@660 1379 finish(command);
matthiasm@660 1380 }
matthiasm@660 1381 }
matthiasm@660 1382
Chris@1469 1383 ModelId
Chris@916 1384 FlexiNoteLayer::getAssociatedPitchModel(LayerGeometryProvider *v) const
Chris@745 1385 {
Chris@745 1386 // Better than we used to do, but still not very satisfactory
Chris@745 1387
Chris@874 1388 // cerr << "FlexiNoteLayer::getAssociatedPitchModel()" << endl;
Chris@746 1389
Chris@918 1390 for (int i = 0; i < v->getView()->getLayerCount(); ++i) {
Chris@918 1391 Layer *layer = v->getView()->getLayer(i);
Chris@795 1392 if (layer &&
Chris@748 1393 layer->getLayerPresentationName() != "candidate") {
Chris@874 1394 // cerr << "FlexiNoteLayer::getAssociatedPitchModel: looks like our layer is " << layer << endl;
Chris@1469 1395 auto modelId = layer->getModel();
Chris@1469 1396 auto model = ModelById::getAs<SparseTimeValueModel>(modelId);
Chris@745 1397 if (model && model->getScaleUnits() == "Hz") {
Chris@1469 1398 // cerr << "FlexiNoteLayer::getAssociatedPitchModel: it's good, returning " << model << endl;
Chris@1469 1399 return modelId;
Chris@745 1400 }
Chris@745 1401 }
Chris@745 1402 }
Chris@1469 1403 // cerr << "FlexiNoteLayer::getAssociatedPitchModel: failed to find a model" << endl;
Chris@1469 1404 return {};
Chris@745 1405 }
matthiasm@660 1406
matthiasm@660 1407 void
Chris@916 1408 FlexiNoteLayer::snapSelectedNotesToPitchTrack(LayerGeometryProvider *v, Selection s)
Chris@746 1409 {
Chris@1469 1410 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1411 if (!model) return;
Chris@746 1412
Chris@1426 1413 EventVector points =
Chris@1469 1414 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@746 1415
Chris@1469 1416 auto command = new ChangeEventsCommand<Model>(m_model, tr("Snap Notes"));
Chris@746 1417
Chris@746 1418 cerr << "snapSelectedNotesToPitchTrack: selection is from " << s.getStartFrame() << " to " << s.getEndFrame() << endl;
Chris@746 1419
Chris@1426 1420 for (EventVector::iterator i = points.begin();
Chris@746 1421 i != points.end(); ++i) {
Chris@746 1422
Chris@1426 1423 Event note(*i);
Chris@746 1424
Chris@1426 1425 cerr << "snapSelectedNotesToPitchTrack: looking at note from " << note.getFrame() << " to " << note.getFrame() + note.getDuration() << endl;
Chris@746 1426
Chris@1426 1427 if (!s.contains(note.getFrame()) &&
Chris@1426 1428 !s.contains(note.getFrame() + note.getDuration() - 1)) {
Chris@746 1429 continue;
Chris@746 1430 }
Chris@746 1431
matthiasm@773 1432 cerr << "snapSelectedNotesToPitchTrack: making new note" << endl;
Chris@1426 1433 Event newNote(note);
Chris@746 1434
Chris@1426 1435 command->remove(note);
Chris@746 1436
Chris@875 1437 if (updateNoteValueFromPitchCurve(v, newNote)) {
Chris@1426 1438 command->add(newNote);
matthiasm@775 1439 }
Chris@746 1440 }
Chris@747 1441
Chris@746 1442 finish(command);
Chris@746 1443 }
Chris@746 1444
Chris@746 1445 void
Chris@916 1446 FlexiNoteLayer::mergeNotes(LayerGeometryProvider *v, Selection s, bool inclusive)
Chris@747 1447 {
Chris@1469 1448 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1449 if (!model) return;
Chris@1469 1450
Chris@1426 1451 EventVector points;
Chris@749 1452 if (inclusive) {
Chris@1469 1453 points = model->getEventsSpanning(s.getStartFrame(), s.getDuration());
Chris@749 1454 } else {
Chris@1469 1455 points = model->getEventsWithin(s.getStartFrame(), s.getDuration());
Chris@747 1456 }
Chris@749 1457
Chris@1426 1458 EventVector::iterator i = points.begin();
Chris@747 1459 if (i == points.end()) return;
Chris@747 1460
Chris@1469 1461 auto command = new ChangeEventsCommand<Model>(m_model, tr("Merge Notes"));
Chris@747 1462
Chris@1426 1463 Event newNote(*i);
Chris@747 1464
Chris@747 1465 while (i != points.end()) {
Chris@747 1466
Chris@749 1467 if (inclusive) {
Chris@1426 1468 if (i->getFrame() >= s.getEndFrame()) break;
Chris@749 1469 } else {
Chris@1426 1470 if (i->getFrame() + i->getDuration() > s.getEndFrame()) break;
Chris@749 1471 }
Chris@747 1472
Chris@1426 1473 newNote = newNote.withDuration
Chris@1426 1474 (i->getFrame() + i->getDuration() - newNote.getFrame());
Chris@1426 1475 command->remove(*i);
Chris@747 1476
Chris@747 1477 ++i;
Chris@747 1478 }
Chris@747 1479
Chris@875 1480 updateNoteValueFromPitchCurve(v, newNote);
Chris@1426 1481 command->add(newNote);
Chris@747 1482 finish(command);
Chris@747 1483 }
Chris@747 1484
Chris@747 1485 bool
Chris@1426 1486 FlexiNoteLayer::updateNoteValueFromPitchCurve(LayerGeometryProvider *v, Event &note) const
gyorgyf@655 1487 {
Chris@1469 1488 ModelId modelId = getAssociatedPitchModel(v);
Chris@1469 1489 auto model = ModelById::getAs<SparseTimeValueModel>(modelId);
Chris@747 1490 if (!model) return false;
gyorgyf@655 1491
gyorgyf@655 1492 std::cerr << model->getTypeName() << std::endl;
gyorgyf@655 1493
Chris@1429 1494 EventVector dataPoints =
Chris@1429 1495 model->getEventsWithin(note.getFrame(), note.getDuration());
Chris@746 1496
Chris@1426 1497 std::cerr << "frame " << note.getFrame() << ": " << dataPoints.size() << " candidate points" << std::endl;
Chris@746 1498
Chris@747 1499 if (dataPoints.empty()) return false;
Chris@746 1500
Chris@904 1501 std::vector<double> pitchValues;
gyorgyf@655 1502
Chris@1429 1503 for (EventVector::const_iterator i =
Chris@747 1504 dataPoints.begin(); i != dataPoints.end(); ++i) {
Chris@1429 1505 pitchValues.push_back(i->getValue());
gyorgyf@655 1506 }
Chris@747 1507
Chris@747 1508 if (pitchValues.empty()) return false;
Chris@747 1509
gyorgyf@655 1510 sort(pitchValues.begin(), pitchValues.end());
Chris@904 1511 int size = int(pitchValues.size());
gyorgyf@655 1512 double median;
gyorgyf@655 1513
gyorgyf@655 1514 if (size % 2 == 0) {
gyorgyf@655 1515 median = (pitchValues[size/2 - 1] + pitchValues[size/2]) / 2;
gyorgyf@655 1516 } else {
gyorgyf@655 1517 median = pitchValues[size/2];
gyorgyf@655 1518 }
Chris@875 1519
Chris@1426 1520 std::cerr << "updateNoteValueFromPitchCurve: corrected from " << note.getValue() << " to median " << median << std::endl;
gyorgyf@655 1521
Chris@1426 1522 note = note.withValue(float(median));
Chris@747 1523
Chris@747 1524 return true;
gyorgyf@655 1525 }
gyorgyf@655 1526
gyorgyf@646 1527 void
Chris@916 1528 FlexiNoteLayer::mouseMoveEvent(LayerGeometryProvider *v, QMouseEvent *e)
gyorgyf@646 1529 {
gyorgyf@646 1530 // GF: context sensitive cursors
Chris@918 1531 // v->getView()->setCursor(Qt::ArrowCursor);
Chris@1426 1532 Event note(0);
gyorgyf@646 1533 if (!getNoteToEdit(v, e->x(), e->y(), note)) {
Chris@918 1534 // v->getView()->setCursor(Qt::UpArrowCursor);
gyorgyf@646 1535 return;
gyorgyf@646 1536 }
gyorgyf@646 1537
Chris@874 1538 bool closeToLeft = false, closeToRight = false,
Chris@874 1539 closeToTop = false, closeToBottom = false;
Chris@874 1540 getRelativeMousePosition(v, note, e->x(), e->y(),
Chris@874 1541 closeToLeft, closeToRight,
Chris@874 1542 closeToTop, closeToBottom);
gyorgyf@649 1543
Chris@874 1544 if (closeToLeft) {
Chris@945 1545 v->getView()->setCursor(Qt::SizeHorCursor);
Chris@874 1546 m_editMode = LeftBoundary;
Chris@874 1547 cerr << "edit mode -> LeftBoundary" << endl;
Chris@874 1548 } else if (closeToRight) {
Chris@945 1549 v->getView()->setCursor(Qt::SizeHorCursor);
Chris@874 1550 m_editMode = RightBoundary;
Chris@874 1551 cerr << "edit mode -> RightBoundary" << endl;
Chris@874 1552 } else if (closeToTop) {
Chris@945 1553 v->getView()->setCursor(Qt::CrossCursor);
Chris@874 1554 m_editMode = DragNote;
Chris@874 1555 cerr << "edit mode -> DragNote" << endl;
Chris@874 1556 } else if (closeToBottom) {
Chris@945 1557 v->getView()->setCursor(Qt::UpArrowCursor);
Chris@874 1558 m_editMode = SplitNote;
Chris@874 1559 cerr << "edit mode -> SplitNote" << endl;
Chris@874 1560 } else {
Chris@945 1561 v->getView()->setCursor(Qt::ArrowCursor);
Chris@874 1562 }
gyorgyf@646 1563 }
gyorgyf@646 1564
gyorgyf@646 1565 void
Chris@1426 1566 FlexiNoteLayer::getRelativeMousePosition(LayerGeometryProvider *v, Event &note, int x, int y, bool &closeToLeft, bool &closeToRight, bool &closeToTop, bool &closeToBottom) const
gyorgyf@646 1567 {
Chris@1469 1568 // GF: TODO: consolidate the tolerance values
gyorgyf@646 1569
matthiasm@651 1570 int ctol = 0;
Chris@1426 1571 int noteStartX = v->getXForFrame(note.getFrame());
Chris@1426 1572 int noteEndX = v->getXForFrame(note.getFrame() + note.getDuration());
Chris@1426 1573 int noteValueY = getYForValue(v,note.getValue());
gyorgyf@646 1574 int noteStartY = noteValueY - (NOTE_HEIGHT / 2);
gyorgyf@646 1575 int noteEndY = noteValueY + (NOTE_HEIGHT / 2);
gyorgyf@646 1576
gyorgyf@646 1577 bool closeToNote = false;
gyorgyf@646 1578
gyorgyf@646 1579 if (y >= noteStartY-ctol && y <= noteEndY+ctol && x >= noteStartX-ctol && x <= noteEndX+ctol) closeToNote = true;
gyorgyf@646 1580 if (!closeToNote) return;
gyorgyf@646 1581
matthiasm@651 1582 int tol = NOTE_HEIGHT / 2;
gyorgyf@646 1583
gyorgyf@646 1584 if (x >= noteStartX - tol && x <= noteStartX + tol) closeToLeft = true;
gyorgyf@646 1585 if (x >= noteEndX - tol && x <= noteEndX + tol) closeToRight = true;
gyorgyf@646 1586 if (y >= noteStartY - tol && y <= noteStartY + tol) closeToTop = true;
gyorgyf@646 1587 if (y >= noteEndY - tol && y <= noteEndY + tol) closeToBottom = true;
Chris@688 1588
Chris@688 1589 // cerr << "FlexiNoteLayer::getRelativeMousePosition: close to: left " << closeToLeft << " right " << closeToRight << " top " << closeToTop << " bottom " << closeToBottom << endl;
gyorgyf@635 1590 }
gyorgyf@635 1591
gyorgyf@635 1592
Chris@255 1593 bool
Chris@916 1594 FlexiNoteLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
Chris@70 1595 {
gyorgyf@633 1596 std::cerr << "Opening note editor dialog" << std::endl;
Chris@1469 1597 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1598 if (!model) return false;
Chris@70 1599
Chris@1426 1600 Event note(0);
Chris@550 1601 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 1602
Chris@1426 1603 // Event note = *points.begin();
Chris@70 1604
Chris@70 1605 ItemEditDialog *dialog = new ItemEditDialog
Chris@1469 1606 (model->getSampleRate(),
Chris@70 1607 ItemEditDialog::ShowTime |
Chris@70 1608 ItemEditDialog::ShowDuration |
Chris@70 1609 ItemEditDialog::ShowValue |
Chris@100 1610 ItemEditDialog::ShowText,
Chris@701 1611 getScaleUnits());
Chris@70 1612
Chris@1426 1613 dialog->setFrameTime(note.getFrame());
Chris@1426 1614 dialog->setValue(note.getValue());
Chris@1426 1615 dialog->setFrameDuration(note.getDuration());
Chris@1426 1616 dialog->setText(note.getLabel());
Chris@70 1617
Chris@70 1618 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1619
Chris@1426 1620 Event newNote = note
Chris@1426 1621 .withFrame(dialog->getFrameTime())
Chris@1426 1622 .withValue(dialog->getValue())
Chris@1426 1623 .withDuration(dialog->getFrameDuration())
Chris@1426 1624 .withLabel(dialog->getText());
Chris@70 1625
Chris@1469 1626 auto command = new ChangeEventsCommand<Model>(m_model, tr("Edit Point"));
Chris@1426 1627 command->remove(note);
Chris@1426 1628 command->add(newNote);
Chris@376 1629 finish(command);
Chris@70 1630 }
Chris@70 1631
Chris@70 1632 delete dialog;
Chris@255 1633 return true;
Chris@70 1634 }
Chris@70 1635
Chris@70 1636 void
Chris@905 1637 FlexiNoteLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
Chris@43 1638 {
Chris@1469 1639 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1640 if (!model) return;
Chris@1469 1641
Chris@1469 1642 auto command = new ChangeEventsCommand<Model>(m_model, tr("Drag Selection"));
Chris@43 1643
Chris@1426 1644 EventVector points =
Chris@1469 1645 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@43 1646
Chris@1426 1647 for (Event p: points) {
Chris@1426 1648 command->remove(p);
Chris@1426 1649 Event moved = p.withFrame(p.getFrame() +
Chris@1426 1650 newStartFrame - s.getStartFrame());
Chris@1426 1651 command->add(moved);
Chris@43 1652 }
Chris@43 1653
Chris@376 1654 finish(command);
Chris@43 1655 }
Chris@43 1656
Chris@43 1657 void
matthiasm@620 1658 FlexiNoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1659 {
Chris@1469 1660 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1661 if (!model || !s.getDuration()) return;
Chris@99 1662
Chris@1469 1663 auto command = new ChangeEventsCommand<Model>(m_model, tr("Resize Selection"));
Chris@43 1664
Chris@1426 1665 EventVector points =
Chris@1469 1666 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@43 1667
Chris@1426 1668 double ratio = double(newSize.getDuration()) / double(s.getDuration());
Chris@1426 1669 double oldStart = double(s.getStartFrame());
Chris@1426 1670 double newStart = double(newSize.getStartFrame());
Chris@1426 1671
Chris@1426 1672 for (Event p: points) {
Chris@43 1673
Chris@1426 1674 double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart;
Chris@1426 1675 double newDuration = double(p.getDuration()) * ratio;
Chris@43 1676
Chris@1426 1677 Event newPoint = p
Chris@1426 1678 .withFrame(lrint(newFrame))
Chris@1426 1679 .withDuration(lrint(newDuration));
Chris@1426 1680 command->remove(p);
Chris@1426 1681 command->add(newPoint);
Chris@43 1682 }
Chris@43 1683
Chris@376 1684 finish(command);
Chris@43 1685 }
Chris@43 1686
Chris@76 1687 void
matthiasm@620 1688 FlexiNoteLayer::deleteSelection(Selection s)
Chris@76 1689 {
Chris@1469 1690 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1691 if (!model) return;
Chris@99 1692
Chris@1469 1693 auto command =
Chris@1469 1694 new ChangeEventsCommand<Model>(m_model, tr("Delete Selected Points"));
Chris@76 1695
Chris@1426 1696 EventVector points =
Chris@1469 1697 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@76 1698
Chris@1426 1699 for (Event p: points) {
Chris@1426 1700 command->remove(p);
Chris@76 1701 }
Chris@76 1702
Chris@376 1703 finish(command);
Chris@76 1704 }
Chris@76 1705
Chris@76 1706 void
matthiasm@784 1707 FlexiNoteLayer::deleteSelectionInclusive(Selection s)
matthiasm@784 1708 {
Chris@1469 1709 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1710 if (!model) return;
matthiasm@784 1711
Chris@1469 1712 auto command =
Chris@1469 1713 new ChangeEventsCommand<Model>(m_model, tr("Delete Selected Points"));
matthiasm@784 1714
Chris@1426 1715 EventVector points =
Chris@1469 1716 model->getEventsSpanning(s.getStartFrame(), s.getDuration());
matthiasm@784 1717
Chris@1426 1718 for (Event p: points) {
Chris@1426 1719 command->remove(p);
matthiasm@784 1720 }
matthiasm@784 1721
matthiasm@784 1722 finish(command);
matthiasm@784 1723 }
matthiasm@784 1724
matthiasm@784 1725 void
Chris@916 1726 FlexiNoteLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
Chris@76 1727 {
Chris@1469 1728 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1729 if (!model) return;
Chris@99 1730
Chris@1426 1731 EventVector points =
Chris@1469 1732 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@76 1733
Chris@1426 1734 for (Event p: points) {
Chris@1426 1735 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
Chris@76 1736 }
Chris@76 1737 }
Chris@76 1738
Chris@125 1739 bool
Chris@916 1740 FlexiNoteLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /*frameOffset */, bool /* interactive */)
Chris@76 1741 {
Chris@1469 1742 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1743 if (!model) return false;
Chris@99 1744
Chris@1423 1745 const EventVector &points = from.getPoints();
Chris@76 1746
Chris@360 1747 bool realign = false;
Chris@360 1748
Chris@360 1749 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1750
Chris@360 1751 QMessageBox::StandardButton button =
Chris@918 1752 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
Chris@360 1753 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 1754 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1755 QMessageBox::Yes);
Chris@360 1756
Chris@360 1757 if (button == QMessageBox::Cancel) {
Chris@360 1758 return false;
Chris@360 1759 }
Chris@360 1760
Chris@360 1761 if (button == QMessageBox::Yes) {
Chris@360 1762 realign = true;
Chris@360 1763 }
Chris@360 1764 }
Chris@360 1765
Chris@1469 1766 auto command = new ChangeEventsCommand<Model>(m_model, tr("Paste"));
Chris@76 1767
Chris@1423 1768 for (EventVector::const_iterator i = points.begin();
Chris@76 1769 i != points.end(); ++i) {
Chris@76 1770
Chris@904 1771 sv_frame_t frame = 0;
Chris@360 1772
Chris@360 1773 if (!realign) {
Chris@360 1774
Chris@360 1775 frame = i->getFrame();
Chris@360 1776
Chris@360 1777 } else {
Chris@360 1778
Chris@1423 1779 if (i->hasReferenceFrame()) {
Chris@360 1780 frame = i->getReferenceFrame();
Chris@360 1781 frame = alignFromReference(v, frame);
Chris@360 1782 } else {
Chris@360 1783 frame = i->getFrame();
Chris@360 1784 }
Chris@76 1785 }
Chris@360 1786
Chris@1426 1787 Event p = *i;
Chris@1426 1788 Event newPoint = p;
Chris@1426 1789 if (!p.hasValue()) {
Chris@1469 1790 newPoint = newPoint.withValue((model->getValueMinimum() +
Chris@1469 1791 model->getValueMaximum()) / 2);
Chris@1426 1792 }
Chris@1426 1793 if (!p.hasDuration()) {
Chris@904 1794 sv_frame_t nextFrame = frame;
Chris@1423 1795 EventVector::const_iterator j = i;
Chris@125 1796 for (; j != points.end(); ++j) {
Chris@125 1797 if (j != i) break;
Chris@125 1798 }
Chris@125 1799 if (j != points.end()) {
Chris@125 1800 nextFrame = j->getFrame();
Chris@125 1801 }
Chris@125 1802 if (nextFrame == frame) {
Chris@1469 1803 newPoint = newPoint.withDuration(model->getResolution());
Chris@125 1804 } else {
Chris@1426 1805 newPoint = newPoint.withDuration(nextFrame - frame);
Chris@125 1806 }
Chris@125 1807 }
Chris@76 1808
Chris@1426 1809 command->add(newPoint);
Chris@76 1810 }
Chris@76 1811
Chris@376 1812 finish(command);
Chris@125 1813 return true;
Chris@76 1814 }
Chris@76 1815
Chris@507 1816 void
Chris@904 1817 FlexiNoteLayer::addNoteOn(sv_frame_t frame, int pitch, int velocity)
Chris@507 1818 {
Chris@1426 1819 m_pendingNoteOns.insert(Event(frame, float(pitch), 0,
Chris@1426 1820 float(velocity / 127.0), ""));
Chris@507 1821 }
Chris@507 1822
Chris@507 1823 void
Chris@904 1824 FlexiNoteLayer::addNoteOff(sv_frame_t frame, int pitch)
Chris@507 1825 {
Chris@1426 1826 for (NoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1827 i != m_pendingNoteOns.end(); ++i) {
Chris@1426 1828
Chris@1426 1829 Event p = *i;
Chris@1426 1830
Chris@1426 1831 if (lrintf(p.getValue()) == pitch) {
Chris@507 1832 m_pendingNoteOns.erase(i);
Chris@1426 1833 Event note = p.withDuration(frame - p.getFrame());
Chris@1469 1834 auto c = new ChangeEventsCommand<Model>
Chris@1469 1835 (m_model, tr("Record Note"));
Chris@1469 1836 c->add(note);
Chris@1469 1837 // execute and bundle:
Chris@1469 1838 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1839 break;
Chris@507 1840 }
Chris@507 1841 }
Chris@507 1842 }
Chris@507 1843
Chris@507 1844 void
matthiasm@620 1845 FlexiNoteLayer::abandonNoteOns()
Chris@507 1846 {
Chris@507 1847 m_pendingNoteOns.clear();
Chris@507 1848 }
Chris@507 1849
Chris@287 1850 int
matthiasm@620 1851 FlexiNoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1852 {
Chris@287 1853 impose = false;
Chris@287 1854 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1855 (QString(darkbg ? "White" : "Black"));
Chris@287 1856 }
Chris@287 1857
Chris@316 1858 void
matthiasm@620 1859 FlexiNoteLayer::toXml(QTextStream &stream,
Chris@714 1860 QString indent, QString extraAttributes) const
Chris@30 1861 {
Chris@316 1862 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1863 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1864 .arg(m_verticalScale)
Chris@445 1865 .arg(m_scaleMinimum)
Chris@445 1866 .arg(m_scaleMaximum));
Chris@30 1867 }
Chris@30 1868
Chris@30 1869 void
matthiasm@620 1870 FlexiNoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1871 {
Chris@287 1872 SingleColourLayer::setProperties(attributes);
Chris@30 1873
Chris@805 1874 bool ok;
Chris@30 1875 VerticalScale scale = (VerticalScale)
Chris@714 1876 attributes.value("verticalScale").toInt(&ok);
Chris@30 1877 if (ok) setVerticalScale(scale);
Chris@30 1878 }
Chris@30 1879
matthiasm@651 1880 void
Chris@916 1881 FlexiNoteLayer::setVerticalRangeToNoteRange(LayerGeometryProvider *v)
matthiasm@651 1882 {
Chris@1469 1883 auto model = ModelById::getAs<NoteModel>(m_model);
Chris@1469 1884 if (!model) return;
Chris@1469 1885
Chris@904 1886 double minf = std::numeric_limits<double>::max();
Chris@904 1887 double maxf = 0;
matthiasm@656 1888 bool hasNotes = 0;
Chris@1469 1889 EventVector allPoints = model->getAllEvents();
Chris@1426 1890 for (EventVector::const_iterator i = allPoints.begin();
Chris@1426 1891 i != allPoints.end(); ++i) {
Chris@714 1892 hasNotes = 1;
Chris@1426 1893 Event note = *i;
Chris@1426 1894 if (note.getValue() < minf) minf = note.getValue();
Chris@1426 1895 if (note.getValue() > maxf) maxf = note.getValue();
matthiasm@651 1896 }
matthiasm@651 1897
matthiasm@656 1898 std::cerr << "min frequency:" << minf << ", max frequency: " << maxf << std::endl;
matthiasm@656 1899
matthiasm@656 1900 if (hasNotes) {
Chris@918 1901 v->getView()->getLayer(1)->setDisplayExtents(minf*0.66,maxf*1.5);
matthiasm@656 1902 // MM: this is a hack because we rely on
matthiasm@656 1903 // * this layer being automatically aligned to layer 1
matthiasm@656 1904 // * layer one is a log frequency layer.
matthiasm@651 1905 }
matthiasm@651 1906 }
Chris@30 1907
matthiasm@651 1908