annotate layer/FlexiNoteLayer.cpp @ 702:ceb9a2992d96 tonioni

Merge from default branch
author Chris Cannam
date Wed, 04 Dec 2013 13:46:29 +0000
parents layer/NoteLayer.cpp@084f65094203 layer/NoteLayer.cpp@e77b1673e17e
children cc6b21976261
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
gyorgyf@646 53 // m_model(0),
gyorgyf@646 54 // m_editing(false),
gyorgyf@646 55 // m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
gyorgyf@646 56 // m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
gyorgyf@646 57 // m_editingCommand(0),
gyorgyf@646 58 // m_verticalScale(AutoAlignScale),
gyorgyf@646 59 // m_scaleMinimum(0),
gyorgyf@646 60 // m_scaleMaximum(0)
gyorgyf@625 61
gyorgyf@627 62 m_model(0),
gyorgyf@628 63 m_editing(false),
gyorgyf@627 64 m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
gyorgyf@627 65 m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
gyorgyf@627 66 m_editingCommand(0),
matthiasm@634 67 m_verticalScale(AutoAlignScale),
Chris@688 68 m_editMode(DragNote),
gyorgyf@628 69 m_scaleMinimum(34),
gyorgyf@658 70 m_scaleMaximum(77),
gyorgyf@658 71 m_intelligentActions(true)
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@701 124 NoteLayer::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@216 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
gyorgyf@646 138 if (min) *min = 0;
gyorgyf@646 139 if (max) *max = 3;
Chris@216 140 if (deflt) *deflt = int(AutoAlignScale);
gyorgyf@646 141
gyorgyf@646 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
gyorgyf@646 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@287 162 int value) const
Chris@30 163 {
Chris@287 164 if (name == "Vertical Scale") {
gyorgyf@646 165 switch (value) {
gyorgyf@646 166 default:
gyorgyf@646 167 case 0: return tr("Auto-Align");
gyorgyf@646 168 case 1: return tr("Linear");
gyorgyf@646 169 case 2: return tr("Log");
gyorgyf@646 170 case 3: return tr("MIDI Notes");
gyorgyf@646 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") {
gyorgyf@646 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@101 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@44 406 long frame = v->getFrameForX(x);
Chris@30 407
matthiasm@620 408 FlexiNoteModel::PointList onPoints =
gyorgyf@646 409 m_model->getPoints(frame);
Chris@30 410
Chris@30 411 if (!onPoints.empty()) {
gyorgyf@646 412 return onPoints;
Chris@30 413 }
Chris@30 414
matthiasm@620 415 FlexiNoteModel::PointList prevPoints =
gyorgyf@646 416 m_model->getPreviousPoints(frame);
matthiasm@620 417 FlexiNoteModel::PointList nextPoints =
gyorgyf@646 418 m_model->getNextPoints(frame);
Chris@30 419
matthiasm@620 420 FlexiNoteModel::PointList usePoints = prevPoints;
Chris@30 421
Chris@30 422 if (prevPoints.empty()) {
gyorgyf@646 423 usePoints = nextPoints;
Chris@248 424 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
gyorgyf@646 425 !(nextPoints.begin()->frame > v->getEndFrame())) {
gyorgyf@646 426 usePoints = nextPoints;
Chris@248 427 } else if (long(nextPoints.begin()->frame) - frame <
gyorgyf@646 428 frame - long(prevPoints.begin()->frame)) {
gyorgyf@646 429 usePoints = nextPoints;
Chris@30 430 }
Chris@30 431
Chris@30 432 if (!usePoints.empty()) {
gyorgyf@646 433 int fuzz = 2;
gyorgyf@646 434 int px = v->getXForFrame(usePoints.begin()->frame);
gyorgyf@646 435 if ((px > x && px - x > fuzz) ||
gyorgyf@646 436 (px < x && x - px > fuzz + 1)) {
gyorgyf@646 437 usePoints.clear();
gyorgyf@646 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@550 449 long 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
gyorgyf@646 478 long 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()) {
gyorgyf@653 511 if (!m_model->isReady()) {
gyorgyf@653 512 return tr("In progress");
gyorgyf@653 513 } else {
gyorgyf@653 514 return tr("No local points");
gyorgyf@653 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
gyorgyf@653 523 int y = getYForValue(v, i->value);
gyorgyf@653 524 int h = NOTE_HEIGHT; // GF: larger notes
Chris@30 525
gyorgyf@653 526 if (m_model->getValueQuantization() != 0.0) {
gyorgyf@653 527 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
gyorgyf@653 528 if (h < NOTE_HEIGHT) h = NOTE_HEIGHT;
gyorgyf@653 529 }
Chris@30 530
gyorgyf@647 531 // GF: this is not quite correct
gyorgyf@653 532 if (pos.y() >= y - 4 && pos.y() <= y + h) {
gyorgyf@653 533 note = *i;
gyorgyf@653 534 break;
gyorgyf@653 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,
gyorgyf@653 541 m_model->getSampleRate());
Chris@30 542 RealTime rd = RealTime::frame2RealTime(note.duration,
gyorgyf@653 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 == "") {
gyorgyf@653 572 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
gyorgyf@653 573 .arg(rt.toText(true).c_str())
gyorgyf@653 574 .arg(pitchText)
gyorgyf@653 575 .arg(rd.toText(true).c_str());
Chris@30 576 } else {
gyorgyf@653 577 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
gyorgyf@653 578 .arg(rt.toText(true).c_str())
gyorgyf@653 579 .arg(pitchText)
gyorgyf@653 580 .arg(rd.toText(true).c_str())
gyorgyf@653 581 .arg(note.label);
Chris@30 582 }
Chris@30 583
Chris@44 584 pos = QPoint(v->getXForFrame(note.frame),
gyorgyf@653 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,
gyorgyf@646 591 size_t &resolution,
gyorgyf@646 592 SnapType snap) const
Chris@30 593 {
Chris@30 594 if (!m_model) {
gyorgyf@646 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
gyorgyf@646 603 points = getLocalPoints(v, v->getXForFrame(frame));
gyorgyf@646 604 if (points.empty()) return false;
gyorgyf@646 605 frame = points.begin()->frame;
gyorgyf@646 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();
gyorgyf@646 614 i != points.end(); ++i) {
Chris@30 615
gyorgyf@646 616 if (snap == SnapRight) {
Chris@30 617
gyorgyf@646 618 if (i->frame > frame) {
gyorgyf@646 619 snapped = i->frame;
gyorgyf@646 620 found = true;
gyorgyf@646 621 break;
gyorgyf@646 622 }
Chris@30 623
gyorgyf@646 624 } else if (snap == SnapLeft) {
Chris@30 625
gyorgyf@646 626 if (i->frame <= frame) {
gyorgyf@646 627 snapped = i->frame;
gyorgyf@646 628 found = true; // don't break, as the next may be better
gyorgyf@646 629 } else {
gyorgyf@646 630 break;
gyorgyf@646 631 }
Chris@30 632
gyorgyf@646 633 } else { // nearest
Chris@30 634
gyorgyf@646 635 FlexiNoteModel::PointList::const_iterator j = i;
gyorgyf@646 636 ++j;
Chris@30 637
gyorgyf@646 638 if (j == points.end()) {
Chris@30 639
gyorgyf@646 640 snapped = i->frame;
gyorgyf@646 641 found = true;
gyorgyf@646 642 break;
Chris@30 643
gyorgyf@646 644 } else if (j->frame >= frame) {
Chris@30 645
gyorgyf@646 646 if (j->frame - frame < frame - i->frame) {
gyorgyf@646 647 snapped = j->frame;
gyorgyf@646 648 } else {
gyorgyf@646 649 snapped = i->frame;
gyorgyf@646 650 }
gyorgyf@646 651 found = true;
gyorgyf@646 652 break;
gyorgyf@646 653 }
gyorgyf@646 654 }
Chris@30 655 }
Chris@30 656
Chris@30 657 frame = snapped;
Chris@30 658 return found;
Chris@30 659 }
Chris@30 660
Chris@101 661 void
matthiasm@620 662 FlexiNoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@30 663 {
Chris@101 664 min = 0.0;
Chris@101 665 max = 0.0;
Chris@101 666 log = false;
Chris@42 667
Chris@101 668 QString queryUnits;
Chris@101 669 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
Chris@701 670 else queryUnits = getScaleUnits();
Chris@30 671
Chris@439 672 if (shouldAutoAlign()) {
Chris@30 673
Chris@101 674 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@30 675
Chris@101 676 min = m_model->getValueMinimum();
Chris@101 677 max = m_model->getValueMaximum();
Chris@42 678
Chris@101 679 if (shouldConvertMIDIToHz()) {
Chris@101 680 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 681 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 682 }
Chris@42 683
Chris@667 684 #ifdef DEBUG_NOTE_LAYER
Chris@684 685 cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@667 686 #endif
Chris@105 687
Chris@101 688 } else if (log) {
Chris@101 689
Chris@197 690 LogRange::mapRange(min, max);
Chris@105 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@101 695 }
Chris@101 696
Chris@101 697 } else {
Chris@101 698
Chris@439 699 getDisplayExtents(min, max);
Chris@101 700
Chris@101 701 if (m_verticalScale == MIDIRangeScale) {
Chris@101 702 min = Pitch::getFrequencyForPitch(0);
matthiasm@623 703 max = Pitch::getFrequencyForPitch(70);
Chris@101 704 } else if (shouldConvertMIDIToHz()) {
Chris@101 705 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 706 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 707 }
Chris@101 708
Chris@101 709 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@197 710 LogRange::mapRange(min, max);
Chris@101 711 log = true;
Chris@101 712 }
Chris@30 713 }
Chris@30 714
Chris@101 715 if (max == min) max = min + 1.0;
Chris@101 716 }
Chris@30 717
Chris@101 718 int
matthiasm@620 719 FlexiNoteLayer::getYForValue(View *v, float val) const
Chris@101 720 {
Chris@101 721 float min = 0.0, max = 0.0;
Chris@101 722 bool logarithmic = false;
Chris@101 723 int h = v->height();
Chris@101 724
Chris@101 725 getScaleExtents(v, min, max, logarithmic);
Chris@101 726
Chris@667 727 #ifdef DEBUG_NOTE_LAYER
Chris@684 728 cerr << "FlexiNoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl;
Chris@667 729 #endif
Chris@101 730
Chris@101 731 if (shouldConvertMIDIToHz()) {
Chris@101 732 val = Pitch::getFrequencyForPitch(lrintf(val),
Chris@101 733 lrintf((val - lrintf(val)) * 100));
Chris@667 734 #ifdef DEBUG_NOTE_LAYER
Chris@682 735 cerr << "shouldConvertMIDIToHz true, val now = " << val << endl;
Chris@667 736 #endif
Chris@101 737 }
Chris@101 738
Chris@101 739 if (logarithmic) {
Chris@197 740 val = LogRange::map(val);
Chris@667 741 #ifdef DEBUG_NOTE_LAYER
Chris@682 742 cerr << "logarithmic true, val now = " << val << endl;
Chris@667 743 #endif
Chris@101 744 }
Chris@101 745
Chris@101 746 int y = int(h - ((val - min) * h) / (max - min)) - 1;
Chris@667 747 #ifdef DEBUG_NOTE_LAYER
Chris@682 748 cerr << "y = " << y << endl;
Chris@667 749 #endif
Chris@101 750 return y;
Chris@30 751 }
Chris@30 752
Chris@30 753 float
matthiasm@620 754 FlexiNoteLayer::getValueForY(View *v, int y) const
Chris@30 755 {
Chris@101 756 float min = 0.0, max = 0.0;
Chris@101 757 bool logarithmic = false;
Chris@44 758 int h = v->height();
Chris@30 759
Chris@101 760 getScaleExtents(v, min, max, logarithmic);
Chris@101 761
Chris@101 762 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 763
Chris@101 764 if (logarithmic) {
Chris@197 765 val = powf(10.f, val);
Chris@101 766 }
Chris@101 767
Chris@101 768 if (shouldConvertMIDIToHz()) {
Chris@101 769 val = Pitch::getPitchForFrequency(val);
Chris@101 770 }
Chris@101 771
Chris@101 772 return val;
Chris@30 773 }
Chris@30 774
Chris@439 775 bool
matthiasm@620 776 FlexiNoteLayer::shouldAutoAlign() const
Chris@439 777 {
Chris@439 778 if (!m_model) return false;
Chris@439 779 return (m_verticalScale == AutoAlignScale);
Chris@439 780 }
Chris@439 781
Chris@30 782 void
matthiasm@620 783 FlexiNoteLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@30 784 {
Chris@30 785 if (!m_model || !m_model->isOK()) return;
Chris@30 786
Chris@30 787 int sampleRate = m_model->getSampleRate();
Chris@30 788 if (!sampleRate) return;
Chris@30 789
matthiasm@620 790 // Profiler profiler("FlexiNoteLayer::paint", true);
Chris@30 791
Chris@30 792 int x0 = rect.left(), x1 = rect.right();
Chris@44 793 long frame0 = v->getFrameForX(x0);
Chris@44 794 long frame1 = v->getFrameForX(x1);
Chris@30 795
matthiasm@620 796 FlexiNoteModel::PointList points(m_model->getPoints(frame0, frame1));
Chris@30 797 if (points.empty()) return;
Chris@30 798
Chris@287 799 paint.setPen(getBaseQColor());
Chris@30 800
Chris@287 801 QColor brushColour(getBaseQColor());
Chris@30 802 brushColour.setAlpha(80);
Chris@30 803
matthiasm@620 804 // SVDEBUG << "FlexiNoteLayer::paint: resolution is "
gyorgyf@646 805 // << m_model->getResolution() << " frames" << endl;
Chris@30 806
Chris@30 807 float min = m_model->getValueMinimum();
Chris@30 808 float max = m_model->getValueMaximum();
Chris@30 809 if (max == min) max = min + 1.0;
Chris@30 810
Chris@30 811 QPoint localPos;
matthiasm@620 812 FlexiNoteModel::Point illuminatePoint(0);
Chris@551 813 bool shouldIlluminate = false;
Chris@30 814
Chris@44 815 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@551 816 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@551 817 illuminatePoint);
Chris@30 818 }
Chris@30 819
Chris@30 820 paint.save();
Chris@30 821 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 822
matthiasm@620 823 for (FlexiNoteModel::PointList::const_iterator i = points.begin();
gyorgyf@646 824 i != points.end(); ++i) {
Chris@30 825
gyorgyf@646 826 const FlexiNoteModel::Point &p(*i);
Chris@30 827
gyorgyf@646 828 int x = v->getXForFrame(p.frame);
gyorgyf@646 829 int y = getYForValue(v, p.value);
gyorgyf@646 830 int w = v->getXForFrame(p.frame + p.duration) - x;
gyorgyf@647 831 int h = NOTE_HEIGHT; //GF: larger notes
gyorgyf@646 832
gyorgyf@646 833 if (m_model->getValueQuantization() != 0.0) {
gyorgyf@646 834 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
gyorgyf@647 835 if (h < NOTE_HEIGHT) h = NOTE_HEIGHT; //GF: larger notes
gyorgyf@646 836 }
Chris@30 837
gyorgyf@646 838 if (w < 1) w = 1;
gyorgyf@646 839 paint.setPen(getBaseQColor());
gyorgyf@646 840 paint.setBrush(brushColour);
Chris@30 841
matthiasm@651 842 // if (shouldIlluminate &&
matthiasm@651 843 // // "illuminatePoint == p"
matthiasm@651 844 // !FlexiNoteModel::Point::Comparator()(illuminatePoint, p) &&
matthiasm@651 845 // !FlexiNoteModel::Point::Comparator()(p, illuminatePoint)) {
matthiasm@651 846 //
matthiasm@651 847 // paint.setPen(v->getForeground());
matthiasm@651 848 // paint.setBrush(v->getForeground());
matthiasm@651 849 //
matthiasm@651 850 // QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits());
matthiasm@651 851 // v->drawVisibleText(paint,
matthiasm@651 852 // x - paint.fontMetrics().width(vlabel) - 2,
matthiasm@651 853 // y + paint.fontMetrics().height()/2
matthiasm@651 854 // - paint.fontMetrics().descent(),
matthiasm@651 855 // vlabel, View::OutlinedText);
matthiasm@651 856 //
matthiasm@651 857 // QString hlabel = RealTime::frame2RealTime
matthiasm@651 858 // (p.frame, m_model->getSampleRate()).toText(true).c_str();
matthiasm@651 859 // v->drawVisibleText(paint,
matthiasm@651 860 // x,
matthiasm@651 861 // y - h/2 - paint.fontMetrics().descent() - 2,
matthiasm@651 862 // hlabel, View::OutlinedText);
matthiasm@651 863 // }
gyorgyf@646 864
gyorgyf@646 865 paint.drawRect(x, y - h/2, w, h);
Chris@30 866 }
Chris@30 867
Chris@30 868 paint.restore();
Chris@30 869 }
Chris@30 870
Chris@692 871 int
Chris@702 872 FlexiNoteLayer::getVerticalScaleWidth(View *v, bool, QPainter &paint) const
Chris@692 873 {
Chris@697 874 if (!m_model || shouldAutoAlign()) {
Chris@696 875 return 0;
Chris@701 876 } else {
Chris@701 877 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@701 878 return LogNumericalScale().getWidth(v, paint) + 10; // for piano
Chris@701 879 } else {
Chris@701 880 return LinearNumericalScale().getWidth(v, paint);
Chris@701 881 }
Chris@696 882 }
Chris@692 883 }
Chris@692 884
Chris@692 885 void
Chris@695 886 FlexiNoteLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
Chris@692 887 {
Chris@701 888 if (!m_model) return;
Chris@701 889
Chris@701 890 QString unit;
Chris@701 891 float min, max;
Chris@701 892 bool logarithmic;
Chris@701 893
Chris@701 894 int w = getVerticalScaleWidth(v, false, paint);
Chris@701 895 int h = v->height();
Chris@701 896
Chris@701 897 getScaleExtents(v, min, max, logarithmic);
Chris@701 898
Chris@701 899 if (logarithmic) {
Chris@701 900 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@696 901 } else {
Chris@701 902 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 903 }
Chris@701 904
Chris@701 905 if (logarithmic && (getScaleUnits() == "Hz")) {
Chris@696 906 PianoScale().paintPianoVertical
Chris@701 907 (v, paint, QRect(w - 10, 0, 10, h),
Chris@701 908 LogRange::unmap(min),
Chris@701 909 LogRange::unmap(max));
Chris@701 910 paint.drawLine(w, 0, w, h);
Chris@701 911 }
Chris@701 912
Chris@701 913 if (getScaleUnits() != "") {
Chris@701 914 int mw = w - 5;
Chris@701 915 paint.drawText(5,
Chris@701 916 5 + paint.fontMetrics().ascent(),
Chris@701 917 TextAbbrev::abbreviate(getScaleUnits(),
Chris@701 918 paint.fontMetrics(),
Chris@701 919 mw));
Chris@696 920 }
Chris@692 921 }
Chris@692 922
Chris@30 923 void
matthiasm@620 924 FlexiNoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 925 {
matthiasm@620 926 // SVDEBUG << "FlexiNoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 927
Chris@30 928 if (!m_model) return;
Chris@30 929
Chris@44 930 long frame = v->getFrameForX(e->x());
Chris@30 931 if (frame < 0) frame = 0;
Chris@30 932 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 933
Chris@44 934 float value = getValueForY(v, e->y());
Chris@30 935
matthiasm@620 936 m_editingPoint = FlexiNoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
Chris@30 937 m_originalPoint = m_editingPoint;
Chris@30 938
Chris@376 939 if (m_editingCommand) finish(m_editingCommand);
matthiasm@620 940 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
gyorgyf@646 941 tr("Draw Point"));
Chris@30 942 m_editingCommand->addPoint(m_editingPoint);
Chris@30 943
Chris@30 944 m_editing = true;
Chris@30 945 }
Chris@30 946
Chris@30 947 void
matthiasm@620 948 FlexiNoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 949 {
matthiasm@620 950 // SVDEBUG << "FlexiNoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 951
Chris@30 952 if (!m_model || !m_editing) return;
Chris@30 953
Chris@44 954 long frame = v->getFrameForX(e->x());
Chris@30 955 if (frame < 0) frame = 0;
Chris@30 956 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 957
Chris@101 958 float newValue = getValueForY(v, e->y());
Chris@101 959
Chris@101 960 long newFrame = m_editingPoint.frame;
Chris@101 961 long newDuration = frame - newFrame;
Chris@101 962 if (newDuration < 0) {
Chris@101 963 newFrame = frame;
Chris@101 964 newDuration = -newDuration;
Chris@101 965 } else if (newDuration == 0) {
Chris@101 966 newDuration = 1;
Chris@101 967 }
Chris@30 968
Chris@30 969 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 970 m_editingPoint.frame = newFrame;
Chris@101 971 m_editingPoint.value = newValue;
Chris@101 972 m_editingPoint.duration = newDuration;
Chris@30 973 m_editingCommand->addPoint(m_editingPoint);
Chris@30 974 }
Chris@30 975
Chris@30 976 void
matthiasm@620 977 FlexiNoteLayer::drawEnd(View *, QMouseEvent *)
Chris@30 978 {
matthiasm@620 979 // SVDEBUG << "FlexiNoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 980 if (!m_model || !m_editing) return;
Chris@376 981 finish(m_editingCommand);
Chris@30 982 m_editingCommand = 0;
Chris@30 983 m_editing = false;
Chris@30 984 }
Chris@30 985
Chris@30 986 void
matthiasm@620 987 FlexiNoteLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 988 {
Chris@335 989 if (!m_model) return;
Chris@335 990
Chris@550 991 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 992
Chris@335 993 if (m_editingCommand) {
gyorgyf@646 994 finish(m_editingCommand);
gyorgyf@646 995 m_editingCommand = 0;
Chris@335 996 }
Chris@335 997
Chris@335 998 m_editing = true;
Chris@335 999 }
Chris@335 1000
Chris@335 1001 void
matthiasm@620 1002 FlexiNoteLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 1003 {
Chris@335 1004 }
Chris@335 1005
Chris@335 1006 void
matthiasm@620 1007 FlexiNoteLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 1008 {
Chris@335 1009 if (!m_model || !m_editing) return;
Chris@335 1010
Chris@335 1011 m_editing = false;
Chris@335 1012
matthiasm@620 1013 FlexiNoteModel::Point p(0);
Chris@550 1014 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@550 1015 if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return;
Chris@550 1016
matthiasm@620 1017 m_editingCommand = new FlexiNoteModel::EditCommand(m_model, tr("Erase Point"));
Chris@335 1018
Chris@335 1019 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 1020
Chris@376 1021 finish(m_editingCommand);
Chris@335 1022 m_editingCommand = 0;
Chris@335 1023 m_editing = false;
Chris@335 1024 }
Chris@335 1025
Chris@335 1026 void
matthiasm@620 1027 FlexiNoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 1028 {
matthiasm@620 1029 // SVDEBUG << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 1030 std::cerr << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 1031
Chris@30 1032 if (!m_model) return;
Chris@30 1033
Chris@550 1034 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
matthiasm@651 1035 m_originalPoint = FlexiNote(m_editingPoint);
gyorgyf@649 1036
matthiasm@651 1037 if (m_editMode == RightBoundary) {
matthiasm@651 1038 m_dragPointX = v->getXForFrame(m_editingPoint.frame + m_editingPoint.duration);
gyorgyf@649 1039 } else {
gyorgyf@649 1040 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
gyorgyf@649 1041 }
Chris@551 1042 m_dragPointY = getYForValue(v, m_editingPoint.value);
Chris@551 1043
Chris@30 1044 if (m_editingCommand) {
gyorgyf@646 1045 finish(m_editingCommand);
gyorgyf@646 1046 m_editingCommand = 0;
Chris@30 1047 }
Chris@30 1048
Chris@30 1049 m_editing = true;
Chris@551 1050 m_dragStartX = e->x();
Chris@551 1051 m_dragStartY = e->y();
matthiasm@651 1052
matthiasm@651 1053 long onset = m_originalPoint.frame;
matthiasm@651 1054 long offset = m_originalPoint.frame + m_originalPoint.duration - 1;
matthiasm@651 1055
matthiasm@651 1056 m_greatestLeftNeighbourFrame = -1;
matthiasm@651 1057 m_smallestRightNeighbourFrame = std::numeric_limits<long>::max();
matthiasm@651 1058
matthiasm@651 1059 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
matthiasm@651 1060 i != m_model->getPoints().end(); ++i) {
matthiasm@651 1061 FlexiNote currentNote = *i;
matthiasm@651 1062
matthiasm@651 1063 // left boundary
matthiasm@651 1064 if (currentNote.frame + currentNote.duration - 1 < onset) {
matthiasm@651 1065 m_greatestLeftNeighbourFrame = currentNote.frame + currentNote.duration - 1;
matthiasm@651 1066 }
matthiasm@651 1067
matthiasm@651 1068 // right boundary
matthiasm@651 1069 if (currentNote.frame > offset) {
matthiasm@651 1070 m_smallestRightNeighbourFrame = currentNote.frame;
matthiasm@651 1071 break;
matthiasm@651 1072 }
matthiasm@651 1073 }
matthiasm@651 1074 std::cerr << "note frame: " << onset << ", left boundary: " << m_greatestLeftNeighbourFrame << ", right boundary: " << m_smallestRightNeighbourFrame << std::endl;
Chris@30 1075 }
Chris@30 1076
Chris@30 1077 void
matthiasm@620 1078 FlexiNoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 1079 {
matthiasm@620 1080 // SVDEBUG << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 1081 std::cerr << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 1082
Chris@30 1083 if (!m_model || !m_editing) return;
Chris@30 1084
Chris@551 1085 int xdist = e->x() - m_dragStartX;
Chris@551 1086 int ydist = e->y() - m_dragStartY;
Chris@551 1087 int newx = m_dragPointX + xdist;
Chris@551 1088 int newy = m_dragPointY + ydist;
Chris@551 1089
matthiasm@657 1090 long dragFrame = v->getFrameForX(newx);
matthiasm@657 1091 if (dragFrame < 0) dragFrame = 0;
matthiasm@657 1092 dragFrame = dragFrame / m_model->getResolution() * m_model->getResolution();
matthiasm@651 1093
Chris@551 1094 float value = getValueForY(v, newy);
Chris@30 1095
Chris@30 1096 if (!m_editingCommand) {
gyorgyf@646 1097 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
gyorgyf@646 1098 tr("Drag Point"));
Chris@30 1099 }
Chris@30 1100
Chris@30 1101 m_editingCommand->deletePoint(m_editingPoint);
matthiasm@651 1102
matthiasm@651 1103 std::cerr << "edit mode: " << m_editMode << std::endl;
matthiasm@657 1104
matthiasm@651 1105 switch (m_editMode) {
matthiasm@651 1106 case LeftBoundary : {
gyorgyf@658 1107 // left
gyorgyf@658 1108 if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1;
matthiasm@657 1109 // right
gyorgyf@658 1110 if (m_intelligentActions && dragFrame >= m_originalPoint.frame + m_originalPoint.duration) {
matthiasm@657 1111 dragFrame = m_originalPoint.frame + m_originalPoint.duration - 1;
matthiasm@651 1112 }
matthiasm@657 1113 m_editingPoint.frame = dragFrame;
matthiasm@657 1114 m_editingPoint.duration = m_originalPoint.frame - dragFrame + m_originalPoint.duration;
matthiasm@651 1115 break;
matthiasm@651 1116 }
matthiasm@651 1117 case RightBoundary : {
matthiasm@657 1118 // left
gyorgyf@658 1119 if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1;
gyorgyf@658 1120 if (m_intelligentActions && dragFrame >= m_smallestRightNeighbourFrame) dragFrame = m_smallestRightNeighbourFrame - 1;
matthiasm@657 1121 m_editingPoint.duration = dragFrame - m_originalPoint.frame + 1;
matthiasm@651 1122 break;
matthiasm@651 1123 }
matthiasm@651 1124 case DragNote : {
matthiasm@657 1125 // left
gyorgyf@658 1126 if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1;
matthiasm@657 1127 // right
gyorgyf@658 1128 if (m_intelligentActions && dragFrame + m_originalPoint.duration >= m_smallestRightNeighbourFrame) {
matthiasm@657 1129 dragFrame = m_smallestRightNeighbourFrame - m_originalPoint.duration;
matthiasm@651 1130 }
matthiasm@657 1131 m_editingPoint.frame = dragFrame;
matthiasm@651 1132 m_editingPoint.value = value;
matthiasm@651 1133 break;
matthiasm@651 1134 }
gyorgyf@649 1135 }
Chris@30 1136 m_editingCommand->addPoint(m_editingPoint);
gyorgyf@649 1137 std::cerr << "added new point(" << m_editingPoint.frame << "," << m_editingPoint.duration << ")" << std::endl;
gyorgyf@649 1138
Chris@30 1139 }
Chris@30 1140
Chris@30 1141 void
gyorgyf@635 1142 FlexiNoteLayer::editEnd(View *v, QMouseEvent *e)
Chris@30 1143 {
matthiasm@620 1144 // SVDEBUG << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 1145 std::cerr << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
matthiasm@656 1146
Chris@30 1147 if (!m_model || !m_editing) return;
Chris@30 1148
Chris@30 1149 if (m_editingCommand) {
Chris@30 1150
gyorgyf@646 1151 QString newName = m_editingCommand->getName();
Chris@30 1152
gyorgyf@646 1153 if (m_editingPoint.frame != m_originalPoint.frame) {
gyorgyf@646 1154 if (m_editingPoint.value != m_originalPoint.value) {
gyorgyf@646 1155 newName = tr("Edit Point");
gyorgyf@646 1156 } else {
gyorgyf@646 1157 newName = tr("Relocate Point");
gyorgyf@646 1158 }
gyorgyf@646 1159 } else {
gyorgyf@646 1160 newName = tr("Change Point Value");
gyorgyf@646 1161 }
Chris@30 1162
gyorgyf@646 1163 m_editingCommand->setName(newName);
gyorgyf@646 1164 finish(m_editingCommand);
Chris@30 1165 }
Chris@30 1166
Chris@30 1167 m_editingCommand = 0;
Chris@30 1168 m_editing = false;
Chris@30 1169 }
Chris@30 1170
gyorgyf@635 1171 void
gyorgyf@635 1172 FlexiNoteLayer::splitStart(View *v, QMouseEvent *e)
gyorgyf@635 1173 {
gyorgyf@646 1174 // GF: note splitting starts (!! remove printing soon)
gyorgyf@646 1175 std::cerr << "splitStart" << std::endl;
gyorgyf@646 1176 if (!m_model) return;
gyorgyf@635 1177
gyorgyf@635 1178 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
gyorgyf@635 1179 // m_originalPoint = m_editingPoint;
gyorgyf@635 1180 //
gyorgyf@635 1181 // m_dragPointX = v->getXForFrame(m_editingPoint.frame);
gyorgyf@635 1182 // m_dragPointY = getYForValue(v, m_editingPoint.value);
gyorgyf@635 1183
gyorgyf@635 1184 if (m_editingCommand) {
gyorgyf@646 1185 finish(m_editingCommand);
gyorgyf@646 1186 m_editingCommand = 0;
gyorgyf@635 1187 }
gyorgyf@635 1188
gyorgyf@635 1189 m_editing = true;
gyorgyf@635 1190 m_dragStartX = e->x();
gyorgyf@635 1191 m_dragStartY = e->y();
gyorgyf@635 1192
gyorgyf@635 1193 }
gyorgyf@635 1194
gyorgyf@635 1195 void
gyorgyf@635 1196 FlexiNoteLayer::splitEnd(View *v, QMouseEvent *e)
gyorgyf@635 1197 {
gyorgyf@646 1198 // GF: note splitting ends. (!! remove printing soon)
gyorgyf@646 1199 std::cerr << "splitEnd" << std::endl;
matthiasm@651 1200 if (!m_model || !m_editing || m_editMode != SplitNote) return;
gyorgyf@635 1201
gyorgyf@635 1202 int xdist = e->x() - m_dragStartX;
gyorgyf@635 1203 int ydist = e->y() - m_dragStartY;
gyorgyf@635 1204 if (xdist != 0 || ydist != 0) {
gyorgyf@646 1205 std::cerr << "mouse moved" << std::endl;
gyorgyf@635 1206 return;
gyorgyf@635 1207 }
gyorgyf@635 1208
gyorgyf@649 1209 // MM: simpler declaration
matthiasm@648 1210 FlexiNote note(0);
gyorgyf@635 1211 if (!getPointToDrag(v, e->x(), e->y(), note)) return;
gyorgyf@635 1212
gyorgyf@635 1213 long frame = v->getFrameForX(e->x());
gyorgyf@635 1214
matthiasm@648 1215 int gap = 0; // MM: I prefer a gap of 0, but we can decide later
matthiasm@648 1216
gyorgyf@649 1217 // MM: changed this a bit, to make it slightly clearer (// GF: nice changes!)
matthiasm@648 1218 FlexiNote newNote1(note.frame, note.value,
matthiasm@648 1219 frame - note.frame - gap,
matthiasm@648 1220 note.level, note.label);
matthiasm@648 1221
matthiasm@648 1222 FlexiNote newNote2(frame, note.value,
matthiasm@648 1223 note.duration - newNote1.duration,
matthiasm@648 1224 note.level, note.label);
gyorgyf@658 1225
gyorgyf@658 1226 if (m_intelligentActions) {
gyorgyf@658 1227 updateNoteValue(v,newNote1);
gyorgyf@658 1228 updateNoteValue(v,newNote2);
gyorgyf@658 1229 }
gyorgyf@655 1230
gyorgyf@635 1231 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
gyorgyf@635 1232 (m_model, tr("Edit Point"));
gyorgyf@635 1233 command->deletePoint(note);
gyorgyf@649 1234 if ((e->modifiers() & Qt::ShiftModifier)) {
gyorgyf@649 1235 finish(command);
gyorgyf@649 1236 return;
gyorgyf@649 1237 }
gyorgyf@635 1238 command->addPoint(newNote1);
gyorgyf@635 1239 command->addPoint(newNote2);
gyorgyf@635 1240 finish(command);
gyorgyf@646 1241
gyorgyf@646 1242 }
gyorgyf@646 1243
gyorgyf@655 1244 void
matthiasm@660 1245 FlexiNoteLayer::addNote(View *v, QMouseEvent *e)
matthiasm@660 1246 {
matthiasm@660 1247 std::cerr << "addNote" << std::endl;
matthiasm@660 1248 if (!m_model) return;
matthiasm@660 1249
matthiasm@660 1250 long duration = 10000;
matthiasm@660 1251
matthiasm@660 1252 long frame = v->getFrameForX(e->x());
matthiasm@660 1253 float value = getValueForY(v, e->y());
matthiasm@660 1254
matthiasm@660 1255 if (m_intelligentActions) {
matthiasm@660 1256 long smallestRightNeighbourFrame = 0;
matthiasm@660 1257 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
matthiasm@660 1258 i != m_model->getPoints().end(); ++i) {
matthiasm@660 1259 FlexiNote currentNote = *i;
matthiasm@660 1260 if (currentNote.frame > frame) {
matthiasm@660 1261 smallestRightNeighbourFrame = currentNote.frame;
matthiasm@660 1262 break;
matthiasm@660 1263 }
matthiasm@660 1264 }
matthiasm@660 1265
matthiasm@660 1266 duration = std::min(smallestRightNeighbourFrame - frame + 1, duration);
matthiasm@660 1267 duration = (duration > 0) ? duration : 0;
matthiasm@660 1268 }
matthiasm@660 1269
matthiasm@660 1270 if (!m_intelligentActions ||
matthiasm@660 1271 m_model->getPoints(frame).empty() && duration > 0)
matthiasm@660 1272 {
matthiasm@660 1273 FlexiNote newNote(frame, value, duration, 100, "new note");
matthiasm@660 1274 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
matthiasm@660 1275 (m_model, tr("Add Point"));
matthiasm@660 1276 command->addPoint(newNote);
matthiasm@660 1277 finish(command);
matthiasm@660 1278 }
matthiasm@660 1279 }
matthiasm@660 1280
matthiasm@660 1281
matthiasm@660 1282 void
gyorgyf@655 1283 FlexiNoteLayer::updateNoteValue(View *v, FlexiNoteModel::Point &note) const
gyorgyf@655 1284 {
gyorgyf@655 1285 //GF: update the note value conforming the median of pitch values in the underlying note layer
gyorgyf@655 1286 Layer *layer = v->getLayer(1); // GF: !!! gross assumption about correct layer order
gyorgyf@655 1287 SparseTimeValueModel *model = 0;
gyorgyf@655 1288 if (layer && layer->getModel())
gyorgyf@655 1289 model = dynamic_cast<SparseTimeValueModel *>(layer->getModel());
gyorgyf@655 1290
gyorgyf@655 1291 if (!model) return;
gyorgyf@655 1292
gyorgyf@655 1293 std::cerr << model->getTypeName() << std::endl;
gyorgyf@655 1294
gyorgyf@655 1295 SparseModel<TimeValuePoint>::PointList dataPoints = model->getPoints(note.frame, note.frame + note.duration);
gyorgyf@655 1296 if (dataPoints.empty()) return;
gyorgyf@655 1297
gyorgyf@655 1298 // std::cerr << "frame " << note.frame << ": " << dataPoints.size() << " candidate points" << std::endl;
gyorgyf@655 1299
gyorgyf@655 1300 std::vector<float> pitchValues;
gyorgyf@655 1301
gyorgyf@655 1302 for (SparseModel<TimeValuePoint>::PointList::const_iterator i = dataPoints.begin();
gyorgyf@655 1303 i != dataPoints.end(); ++i) {
gyorgyf@655 1304 pitchValues.push_back((*i).value);
gyorgyf@655 1305 }
gyorgyf@655 1306 sort(pitchValues.begin(), pitchValues.end());
gyorgyf@655 1307 size_t size = pitchValues.size();
gyorgyf@655 1308 double median;
gyorgyf@655 1309
gyorgyf@655 1310 if (size % 2 == 0) {
gyorgyf@655 1311 median = (pitchValues[size/2 - 1] + pitchValues[size/2]) / 2;
gyorgyf@655 1312 } else {
gyorgyf@655 1313 median = pitchValues[size/2];
gyorgyf@655 1314 }
gyorgyf@655 1315
gyorgyf@655 1316 note.value = median;
gyorgyf@655 1317 }
gyorgyf@655 1318
gyorgyf@646 1319 void
gyorgyf@646 1320 FlexiNoteLayer::mouseMoveEvent(View *v, QMouseEvent *e)
gyorgyf@646 1321 {
gyorgyf@646 1322 // GF: context sensitive cursors
gyorgyf@646 1323 // v->setCursor(Qt::ArrowCursor);
gyorgyf@646 1324 FlexiNoteModel::Point note(0);
gyorgyf@646 1325 if (!getNoteToEdit(v, e->x(), e->y(), note)) {
gyorgyf@646 1326 // v->setCursor(Qt::UpArrowCursor);
gyorgyf@646 1327 return;
gyorgyf@646 1328 }
gyorgyf@646 1329
gyorgyf@646 1330 bool closeToLeft = false, closeToRight = false, closeToTop = false, closeToBottom = false;
gyorgyf@646 1331 getRelativeMousePosition(v, note, e->x(), e->y(), closeToLeft, closeToRight, closeToTop, closeToBottom);
gyorgyf@646 1332 // if (!closeToLeft) return;
gyorgyf@646 1333 // if (closeToTop) v->setCursor(Qt::SizeVerCursor);
gyorgyf@649 1334
matthiasm@651 1335 if (closeToLeft) { v->setCursor(Qt::SizeHorCursor); m_editMode = LeftBoundary; return; }
matthiasm@651 1336 if (closeToRight) { v->setCursor(Qt::SizeHorCursor); m_editMode = RightBoundary; return; }
matthiasm@651 1337 if (closeToTop) { v->setCursor(Qt::CrossCursor); m_editMode = DragNote; return; }
matthiasm@651 1338 if (closeToBottom) { v->setCursor(Qt::UpArrowCursor); m_editMode = SplitNote; return; }
gyorgyf@649 1339
gyorgyf@646 1340 v->setCursor(Qt::ArrowCursor);
gyorgyf@646 1341
gyorgyf@649 1342 // std::cerr << "Mouse moved in edit mode over FlexiNoteLayer" << std::endl;
gyorgyf@646 1343 // v->setCursor(Qt::SizeHorCursor);
gyorgyf@646 1344
gyorgyf@646 1345 }
gyorgyf@646 1346
gyorgyf@646 1347 void
gyorgyf@646 1348 FlexiNoteLayer::getRelativeMousePosition(View *v, FlexiNoteModel::Point &note, int x, int y, bool &closeToLeft, bool &closeToRight, bool &closeToTop, bool &closeToBottom) const
gyorgyf@646 1349 {
gyorgyf@649 1350 // GF: TODO: consoloidate the tolerance values
gyorgyf@646 1351 if (!m_model) return;
gyorgyf@646 1352
matthiasm@651 1353 int ctol = 0;
gyorgyf@646 1354 int noteStartX = v->getXForFrame(note.frame);
gyorgyf@646 1355 int noteEndX = v->getXForFrame(note.frame + note.duration);
gyorgyf@646 1356 int noteValueY = getYForValue(v,note.value);
gyorgyf@646 1357 int noteStartY = noteValueY - (NOTE_HEIGHT / 2);
gyorgyf@646 1358 int noteEndY = noteValueY + (NOTE_HEIGHT / 2);
gyorgyf@646 1359
gyorgyf@646 1360 bool closeToNote = false;
gyorgyf@646 1361
gyorgyf@646 1362 if (y >= noteStartY-ctol && y <= noteEndY+ctol && x >= noteStartX-ctol && x <= noteEndX+ctol) closeToNote = true;
gyorgyf@646 1363 if (!closeToNote) return;
gyorgyf@646 1364
matthiasm@651 1365 int tol = NOTE_HEIGHT / 2;
gyorgyf@646 1366
gyorgyf@646 1367 if (x >= noteStartX - tol && x <= noteStartX + tol) closeToLeft = true;
gyorgyf@646 1368 if (x >= noteEndX - tol && x <= noteEndX + tol) closeToRight = true;
gyorgyf@646 1369 if (y >= noteStartY - tol && y <= noteStartY + tol) closeToTop = true;
gyorgyf@646 1370 if (y >= noteEndY - tol && y <= noteEndY + tol) closeToBottom = true;
Chris@688 1371
Chris@688 1372 // cerr << "FlexiNoteLayer::getRelativeMousePosition: close to: left " << closeToLeft << " right " << closeToRight << " top " << closeToTop << " bottom " << closeToBottom << endl;
gyorgyf@635 1373 }
gyorgyf@635 1374
gyorgyf@635 1375
Chris@255 1376 bool
matthiasm@620 1377 FlexiNoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1378 {
gyorgyf@633 1379 std::cerr << "Opening note editor dialog" << std::endl;
Chris@255 1380 if (!m_model) return false;
Chris@70 1381
matthiasm@620 1382 FlexiNoteModel::Point note(0);
Chris@550 1383 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 1384
matthiasm@620 1385 // FlexiNoteModel::Point note = *points.begin();
Chris@70 1386
Chris@70 1387 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1388 (m_model->getSampleRate(),
Chris@70 1389 ItemEditDialog::ShowTime |
Chris@70 1390 ItemEditDialog::ShowDuration |
Chris@70 1391 ItemEditDialog::ShowValue |
Chris@100 1392 ItemEditDialog::ShowText,
Chris@701 1393 getScaleUnits());
Chris@70 1394
Chris@70 1395 dialog->setFrameTime(note.frame);
Chris@70 1396 dialog->setValue(note.value);
Chris@70 1397 dialog->setFrameDuration(note.duration);
Chris@70 1398 dialog->setText(note.label);
Chris@70 1399
Chris@70 1400 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1401
matthiasm@620 1402 FlexiNoteModel::Point newNote = note;
Chris@70 1403 newNote.frame = dialog->getFrameTime();
Chris@70 1404 newNote.value = dialog->getValue();
Chris@70 1405 newNote.duration = dialog->getFrameDuration();
Chris@70 1406 newNote.label = dialog->getText();
Chris@70 1407
matthiasm@620 1408 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
Chris@70 1409 (m_model, tr("Edit Point"));
Chris@70 1410 command->deletePoint(note);
Chris@70 1411 command->addPoint(newNote);
Chris@376 1412 finish(command);
Chris@70 1413 }
Chris@70 1414
Chris@70 1415 delete dialog;
Chris@255 1416 return true;
Chris@70 1417 }
Chris@70 1418
Chris@70 1419 void
matthiasm@620 1420 FlexiNoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1421 {
Chris@99 1422 if (!m_model) return;
Chris@99 1423
matthiasm@620 1424 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1425 new FlexiNoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 1426
matthiasm@620 1427 FlexiNoteModel::PointList points =
gyorgyf@646 1428 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1429
matthiasm@620 1430 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1431 i != points.end(); ++i) {
Chris@43 1432
gyorgyf@646 1433 if (s.contains(i->frame)) {
gyorgyf@646 1434 FlexiNoteModel::Point newPoint(*i);
gyorgyf@646 1435 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
gyorgyf@646 1436 command->deletePoint(*i);
gyorgyf@646 1437 command->addPoint(newPoint);
gyorgyf@646 1438 }
Chris@43 1439 }
Chris@43 1440
Chris@376 1441 finish(command);
Chris@43 1442 }
Chris@43 1443
Chris@43 1444 void
matthiasm@620 1445 FlexiNoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1446 {
Chris@99 1447 if (!m_model) return;
Chris@99 1448
matthiasm@620 1449 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1450 new FlexiNoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1451
matthiasm@620 1452 FlexiNoteModel::PointList points =
gyorgyf@646 1453 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1454
Chris@43 1455 double ratio =
gyorgyf@646 1456 double(newSize.getEndFrame() - newSize.getStartFrame()) /
gyorgyf@646 1457 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1458
matthiasm@620 1459 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1460 i != points.end(); ++i) {
Chris@43 1461
gyorgyf@646 1462 if (s.contains(i->frame)) {
Chris@43 1463
gyorgyf@646 1464 double targetStart = i->frame;
gyorgyf@646 1465 targetStart = newSize.getStartFrame() +
gyorgyf@646 1466 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 1467
gyorgyf@646 1468 double targetEnd = i->frame + i->duration;
gyorgyf@646 1469 targetEnd = newSize.getStartFrame() +
gyorgyf@646 1470 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 1471
gyorgyf@646 1472 FlexiNoteModel::Point newPoint(*i);
gyorgyf@646 1473 newPoint.frame = lrint(targetStart);
gyorgyf@646 1474 newPoint.duration = lrint(targetEnd - targetStart);
gyorgyf@646 1475 command->deletePoint(*i);
gyorgyf@646 1476 command->addPoint(newPoint);
gyorgyf@646 1477 }
Chris@43 1478 }
Chris@43 1479
Chris@376 1480 finish(command);
Chris@43 1481 }
Chris@43 1482
Chris@76 1483 void
matthiasm@620 1484 FlexiNoteLayer::deleteSelection(Selection s)
Chris@76 1485 {
Chris@99 1486 if (!m_model) return;
Chris@99 1487
matthiasm@620 1488 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1489 new FlexiNoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1490
matthiasm@620 1491 FlexiNoteModel::PointList points =
gyorgyf@646 1492 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1493
matthiasm@620 1494 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1495 i != points.end(); ++i) {
Chris@76 1496
Chris@76 1497 if (s.contains(i->frame)) {
Chris@76 1498 command->deletePoint(*i);
Chris@76 1499 }
Chris@76 1500 }
Chris@76 1501
Chris@376 1502 finish(command);
Chris@76 1503 }
Chris@76 1504
Chris@76 1505 void
matthiasm@620 1506 FlexiNoteLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1507 {
Chris@99 1508 if (!m_model) return;
Chris@99 1509
matthiasm@620 1510 FlexiNoteModel::PointList points =
gyorgyf@646 1511 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1512
matthiasm@620 1513 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1514 i != points.end(); ++i) {
gyorgyf@646 1515 if (s.contains(i->frame)) {
Chris@335 1516 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
Chris@360 1517 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1518 to.addPoint(point);
Chris@76 1519 }
Chris@76 1520 }
Chris@76 1521 }
Chris@76 1522
Chris@125 1523 bool
matthiasm@620 1524 FlexiNoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
Chris@76 1525 {
Chris@125 1526 if (!m_model) return false;
Chris@99 1527
Chris@76 1528 const Clipboard::PointList &points = from.getPoints();
Chris@76 1529
Chris@360 1530 bool realign = false;
Chris@360 1531
Chris@360 1532 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1533
Chris@360 1534 QMessageBox::StandardButton button =
Chris@360 1535 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1536 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 1537 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1538 QMessageBox::Yes);
Chris@360 1539
Chris@360 1540 if (button == QMessageBox::Cancel) {
Chris@360 1541 return false;
Chris@360 1542 }
Chris@360 1543
Chris@360 1544 if (button == QMessageBox::Yes) {
Chris@360 1545 realign = true;
Chris@360 1546 }
Chris@360 1547 }
Chris@360 1548
matthiasm@620 1549 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1550 new FlexiNoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1551
Chris@76 1552 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1553 i != points.end(); ++i) {
Chris@76 1554
Chris@76 1555 if (!i->haveFrame()) continue;
Chris@76 1556 size_t frame = 0;
Chris@360 1557
Chris@360 1558 if (!realign) {
Chris@360 1559
Chris@360 1560 frame = i->getFrame();
Chris@360 1561
Chris@360 1562 } else {
Chris@360 1563
Chris@360 1564 if (i->haveReferenceFrame()) {
Chris@360 1565 frame = i->getReferenceFrame();
Chris@360 1566 frame = alignFromReference(v, frame);
Chris@360 1567 } else {
Chris@360 1568 frame = i->getFrame();
Chris@360 1569 }
Chris@76 1570 }
Chris@360 1571
matthiasm@620 1572 FlexiNoteModel::Point newPoint(frame);
Chris@76 1573
Chris@76 1574 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1575 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1576 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1577 m_model->getValueMaximum()) / 2;
Chris@335 1578 if (i->haveLevel()) newPoint.level = i->getLevel();
Chris@76 1579 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 1580 else {
Chris@125 1581 size_t nextFrame = frame;
Chris@125 1582 Clipboard::PointList::const_iterator j = i;
Chris@125 1583 for (; j != points.end(); ++j) {
Chris@125 1584 if (!j->haveFrame()) continue;
Chris@125 1585 if (j != i) break;
Chris@125 1586 }
Chris@125 1587 if (j != points.end()) {
Chris@125 1588 nextFrame = j->getFrame();
Chris@125 1589 }
Chris@125 1590 if (nextFrame == frame) {
Chris@125 1591 newPoint.duration = m_model->getResolution();
Chris@125 1592 } else {
Chris@125 1593 newPoint.duration = nextFrame - frame;
Chris@125 1594 }
Chris@125 1595 }
Chris@76 1596
Chris@76 1597 command->addPoint(newPoint);
Chris@76 1598 }
Chris@76 1599
Chris@376 1600 finish(command);
Chris@125 1601 return true;
Chris@76 1602 }
Chris@76 1603
Chris@507 1604 void
matthiasm@620 1605 FlexiNoteLayer::addNoteOn(long frame, int pitch, int velocity)
Chris@507 1606 {
matthiasm@620 1607 m_pendingNoteOns.insert(FlexiNote(frame, pitch, 0, float(velocity) / 127.0, ""));
Chris@507 1608 }
Chris@507 1609
Chris@507 1610 void
matthiasm@620 1611 FlexiNoteLayer::addNoteOff(long frame, int pitch)
Chris@507 1612 {
matthiasm@620 1613 for (FlexiNoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1614 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1615 if (lrintf((*i).value) == pitch) {
matthiasm@620 1616 FlexiNote note(*i);
Chris@507 1617 m_pendingNoteOns.erase(i);
Chris@507 1618 note.duration = frame - note.frame;
Chris@507 1619 if (m_model) {
matthiasm@620 1620 FlexiNoteModel::AddPointCommand *c = new FlexiNoteModel::AddPointCommand
matthiasm@620 1621 (m_model, note, tr("Record FlexiNote"));
Chris@507 1622 // execute and bundle:
Chris@507 1623 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1624 }
Chris@507 1625 break;
Chris@507 1626 }
Chris@507 1627 }
Chris@507 1628 }
Chris@507 1629
Chris@507 1630 void
matthiasm@620 1631 FlexiNoteLayer::abandonNoteOns()
Chris@507 1632 {
Chris@507 1633 m_pendingNoteOns.clear();
Chris@507 1634 }
Chris@507 1635
Chris@287 1636 int
matthiasm@620 1637 FlexiNoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1638 {
Chris@287 1639 impose = false;
Chris@287 1640 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1641 (QString(darkbg ? "White" : "Black"));
Chris@287 1642 }
Chris@287 1643
Chris@316 1644 void
matthiasm@620 1645 FlexiNoteLayer::toXml(QTextStream &stream,
Chris@316 1646 QString indent, QString extraAttributes) const
Chris@30 1647 {
Chris@316 1648 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1649 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1650 .arg(m_verticalScale)
Chris@445 1651 .arg(m_scaleMinimum)
Chris@445 1652 .arg(m_scaleMaximum));
Chris@30 1653 }
Chris@30 1654
Chris@30 1655 void
matthiasm@620 1656 FlexiNoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1657 {
Chris@287 1658 SingleColourLayer::setProperties(attributes);
Chris@30 1659
Chris@445 1660 bool ok, alsoOk;
Chris@30 1661 VerticalScale scale = (VerticalScale)
gyorgyf@646 1662 attributes.value("verticalScale").toInt(&ok);
Chris@30 1663 if (ok) setVerticalScale(scale);
Chris@445 1664
Chris@445 1665 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1666 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@670 1667 // if (ok && alsoOk && min != max) setDisplayExtents(min, max);
Chris@30 1668 }
Chris@30 1669
matthiasm@651 1670 void
matthiasm@656 1671 FlexiNoteLayer::setVerticalRangeToNoteRange(View *v)
matthiasm@651 1672 {
matthiasm@656 1673 float minf = std::numeric_limits<float>::max();
matthiasm@651 1674 float maxf = 0;
matthiasm@656 1675 bool hasNotes = 0;
matthiasm@651 1676 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
matthiasm@651 1677 i != m_model->getPoints().end(); ++i) {
matthiasm@656 1678 hasNotes = 1;
matthiasm@651 1679 FlexiNote note = *i;
matthiasm@651 1680 if (note.value < minf) minf = note.value;
matthiasm@656 1681 if (note.value > maxf) maxf = note.value;
matthiasm@651 1682 }
matthiasm@651 1683
matthiasm@656 1684 std::cerr << "min frequency:" << minf << ", max frequency: " << maxf << std::endl;
matthiasm@656 1685
matthiasm@656 1686 if (hasNotes) {
matthiasm@659 1687 v->getLayer(1)->setDisplayExtents(minf*0.66,maxf*1.5);
matthiasm@656 1688 // MM: this is a hack because we rely on
matthiasm@656 1689 // * this layer being automatically aligned to layer 1
matthiasm@656 1690 // * layer one is a log frequency layer.
matthiasm@651 1691 }
matthiasm@651 1692 }
Chris@30 1693
matthiasm@651 1694