annotate layer/FlexiNoteLayer.cpp @ 819:a9731b49745a tonioni

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