annotate layer/FlexiNoteLayer.cpp @ 1426:e1a08da75427 single-point

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