annotate layer/FlexiNoteLayer.cpp @ 623:49afde2dd704 tonioni

put the FlexiNote layer before the note layer to see what happens -- not much
author matthiasm
date Tue, 26 Mar 2013 20:14:08 +0000
parents 765bb3774c4f
children 1336439dc055
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"
Chris@30 19 #include "base/RealTime.h"
Chris@30 20 #include "base/Profiler.h"
Chris@30 21 #include "base/Pitch.h"
Chris@197 22 #include "base/LogRange.h"
Chris@439 23 #include "base/RangeMapper.h"
Chris@376 24 #include "ColourDatabase.h"
Chris@128 25 #include "view/View.h"
Chris@30 26
matthiasm@620 27 #include "data/model/FlexiNoteModel.h"
Chris@30 28
Chris@70 29 #include "widgets/ItemEditDialog.h"
Chris@70 30
Chris@30 31 #include <QPainter>
Chris@30 32 #include <QPainterPath>
Chris@30 33 #include <QMouseEvent>
Chris@316 34 #include <QTextStream>
Chris@360 35 #include <QMessageBox>
Chris@30 36
Chris@30 37 #include <iostream>
Chris@30 38 #include <cmath>
Chris@551 39 #include <utility>
Chris@30 40
matthiasm@620 41 FlexiNoteLayer::FlexiNoteLayer() :
Chris@287 42 SingleColourLayer(),
Chris@30 43 m_model(0),
Chris@30 44 m_editing(false),
Chris@335 45 m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
Chris@335 46 m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
Chris@30 47 m_editingCommand(0),
matthiasm@623 48 m_verticalScale(MIDIRangeScale),
matthiasm@623 49 m_scaleMinimum(34),
matthiasm@623 50 m_scaleMaximum(77)
Chris@30 51 {
Chris@30 52 }
Chris@30 53
Chris@30 54 void
matthiasm@620 55 FlexiNoteLayer::setModel(FlexiNoteModel *model)
Chris@30 56 {
Chris@30 57 if (m_model == model) return;
Chris@30 58 m_model = model;
Chris@30 59
Chris@320 60 connectSignals(m_model);
Chris@30 61
matthiasm@620 62 // SVDEBUG << "FlexiNoteLayer::setModel(" << model << ")" << endl;
Chris@30 63
Chris@439 64 m_scaleMinimum = 0;
Chris@439 65 m_scaleMaximum = 0;
Chris@439 66
Chris@30 67 emit modelReplaced();
Chris@30 68 }
Chris@30 69
Chris@30 70 Layer::PropertyList
matthiasm@620 71 FlexiNoteLayer::getProperties() const
Chris@30 72 {
Chris@287 73 PropertyList list = SingleColourLayer::getProperties();
Chris@87 74 list.push_back("Vertical Scale");
Chris@100 75 list.push_back("Scale Units");
Chris@30 76 return list;
Chris@30 77 }
Chris@30 78
Chris@87 79 QString
matthiasm@620 80 FlexiNoteLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 81 {
Chris@87 82 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@116 83 if (name == "Scale Units") return tr("Scale Units");
Chris@287 84 return SingleColourLayer::getPropertyLabel(name);
Chris@87 85 }
Chris@87 86
Chris@30 87 Layer::PropertyType
matthiasm@620 88 FlexiNoteLayer::getPropertyType(const PropertyName &name) const
Chris@30 89 {
Chris@100 90 if (name == "Scale Units") return UnitsProperty;
Chris@287 91 if (name == "Vertical Scale") return ValueProperty;
Chris@287 92 return SingleColourLayer::getPropertyType(name);
Chris@30 93 }
Chris@30 94
Chris@198 95 QString
matthiasm@620 96 FlexiNoteLayer::getPropertyGroupName(const PropertyName &name) const
Chris@198 97 {
Chris@198 98 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@198 99 return tr("Scale");
Chris@198 100 }
Chris@287 101 return SingleColourLayer::getPropertyGroupName(name);
Chris@198 102 }
Chris@198 103
Chris@30 104 int
matthiasm@620 105 FlexiNoteLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 106 int *min, int *max, int *deflt) const
Chris@30 107 {
Chris@216 108 int val = 0;
Chris@30 109
Chris@287 110 if (name == "Vertical Scale") {
Chris@30 111
Chris@30 112 if (min) *min = 0;
Chris@101 113 if (max) *max = 3;
Chris@216 114 if (deflt) *deflt = int(AutoAlignScale);
Chris@30 115
Chris@216 116 val = int(m_verticalScale);
Chris@30 117
Chris@100 118 } else if (name == "Scale Units") {
Chris@100 119
Chris@216 120 if (deflt) *deflt = 0;
Chris@100 121 if (m_model) {
Chris@216 122 val = UnitDatabase::getInstance()->getUnitId
Chris@100 123 (m_model->getScaleUnits());
Chris@100 124 }
Chris@100 125
Chris@30 126 } else {
Chris@216 127
Chris@287 128 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@30 129 }
Chris@30 130
Chris@216 131 return val;
Chris@30 132 }
Chris@30 133
Chris@30 134 QString
matthiasm@620 135 FlexiNoteLayer::getPropertyValueLabel(const PropertyName &name,
Chris@287 136 int value) const
Chris@30 137 {
Chris@287 138 if (name == "Vertical Scale") {
Chris@30 139 switch (value) {
Chris@30 140 default:
Chris@101 141 case 0: return tr("Auto-Align");
Chris@198 142 case 1: return tr("Linear");
Chris@198 143 case 2: return tr("Log");
Chris@198 144 case 3: return tr("MIDI Notes");
Chris@30 145 }
Chris@30 146 }
Chris@287 147 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@30 148 }
Chris@30 149
Chris@30 150 void
matthiasm@620 151 FlexiNoteLayer::setProperty(const PropertyName &name, int value)
Chris@30 152 {
Chris@287 153 if (name == "Vertical Scale") {
Chris@30 154 setVerticalScale(VerticalScale(value));
Chris@100 155 } else if (name == "Scale Units") {
Chris@100 156 if (m_model) {
Chris@100 157 m_model->setScaleUnits
Chris@100 158 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 159 emit modelChanged();
Chris@100 160 }
Chris@287 161 } else {
Chris@287 162 return SingleColourLayer::setProperty(name, value);
Chris@30 163 }
Chris@30 164 }
Chris@30 165
Chris@30 166 void
matthiasm@620 167 FlexiNoteLayer::setVerticalScale(VerticalScale scale)
Chris@30 168 {
Chris@30 169 if (m_verticalScale == scale) return;
Chris@30 170 m_verticalScale = scale;
Chris@30 171 emit layerParametersChanged();
Chris@30 172 }
Chris@30 173
Chris@30 174 bool
matthiasm@620 175 FlexiNoteLayer::isLayerScrollable(const View *v) const
Chris@30 176 {
Chris@30 177 QPoint discard;
Chris@44 178 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@30 179 }
Chris@30 180
Chris@79 181 bool
matthiasm@620 182 FlexiNoteLayer::shouldConvertMIDIToHz() const
Chris@101 183 {
Chris@101 184 QString unit = m_model->getScaleUnits();
Chris@101 185 return (unit != "Hz");
Chris@101 186 // if (unit == "" ||
Chris@101 187 // unit.startsWith("MIDI") ||
Chris@101 188 // unit.startsWith("midi")) return true;
Chris@101 189 // return false;
Chris@101 190 }
Chris@101 191
Chris@101 192 bool
matthiasm@620 193 FlexiNoteLayer::getValueExtents(float &min, float &max,
Chris@101 194 bool &logarithmic, QString &unit) const
Chris@79 195 {
Chris@79 196 if (!m_model) return false;
Chris@79 197 min = m_model->getValueMinimum();
Chris@79 198 max = m_model->getValueMaximum();
Chris@101 199
Chris@105 200 if (shouldConvertMIDIToHz()) {
Chris@105 201 unit = "Hz";
Chris@105 202 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@105 203 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@105 204 } else unit = m_model->getScaleUnits();
Chris@101 205
Chris@101 206 if (m_verticalScale == MIDIRangeScale ||
Chris@101 207 m_verticalScale == LogScale) logarithmic = true;
Chris@101 208
Chris@101 209 return true;
Chris@101 210 }
Chris@101 211
Chris@101 212 bool
matthiasm@620 213 FlexiNoteLayer::getDisplayExtents(float &min, float &max) const
Chris@101 214 {
matthiasm@623 215 if (!m_model || shouldAutoAlign()) {
matthiasm@623 216 std::cerr << "No model or shouldAutoAlign()" << std::endl;
matthiasm@623 217 return false;
matthiasm@623 218 }
Chris@101 219
Chris@101 220 if (m_verticalScale == MIDIRangeScale) {
Chris@101 221 min = Pitch::getFrequencyForPitch(0);
matthiasm@623 222 max = Pitch::getFrequencyForPitch(70);
Chris@101 223 return true;
Chris@101 224 }
Chris@101 225
Chris@439 226 if (m_scaleMinimum == m_scaleMaximum) {
Chris@455 227 min = m_model->getValueMinimum();
Chris@455 228 max = m_model->getValueMaximum();
Chris@455 229 } else {
Chris@455 230 min = m_scaleMinimum;
Chris@455 231 max = m_scaleMaximum;
Chris@439 232 }
Chris@439 233
Chris@101 234 if (shouldConvertMIDIToHz()) {
Chris@101 235 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 236 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 237 }
Chris@101 238
Chris@79 239 return true;
Chris@79 240 }
Chris@79 241
Chris@439 242 bool
matthiasm@620 243 FlexiNoteLayer::setDisplayExtents(float min, float max)
Chris@439 244 {
Chris@439 245 if (!m_model) return false;
Chris@439 246
Chris@439 247 if (min == max) {
Chris@439 248 if (min == 0.f) {
Chris@439 249 max = 1.f;
Chris@439 250 } else {
Chris@439 251 max = min * 1.0001;
Chris@439 252 }
Chris@439 253 }
Chris@439 254
Chris@439 255 m_scaleMinimum = min;
Chris@439 256 m_scaleMaximum = max;
Chris@439 257
matthiasm@620 258 // SVDEBUG << "FlexiNoteLayer::setDisplayExtents: min = " << min << ", max = " << max << endl;
Chris@439 259
Chris@439 260 emit layerParametersChanged();
Chris@439 261 return true;
Chris@439 262 }
Chris@439 263
Chris@439 264 int
matthiasm@620 265 FlexiNoteLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@439 266 {
Chris@439 267 if (shouldAutoAlign()) return 0;
Chris@439 268 if (!m_model) return 0;
Chris@439 269
Chris@439 270 defaultStep = 0;
Chris@439 271 return 100;
Chris@439 272 }
Chris@439 273
Chris@439 274 int
matthiasm@620 275 FlexiNoteLayer::getCurrentVerticalZoomStep() const
Chris@439 276 {
Chris@439 277 if (shouldAutoAlign()) return 0;
Chris@439 278 if (!m_model) return 0;
Chris@439 279
Chris@439 280 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 281 if (!mapper) return 0;
Chris@439 282
Chris@439 283 float dmin, dmax;
Chris@439 284 getDisplayExtents(dmin, dmax);
Chris@439 285
Chris@439 286 int nr = mapper->getPositionForValue(dmax - dmin);
Chris@439 287
Chris@439 288 delete mapper;
Chris@439 289
Chris@439 290 return 100 - nr;
Chris@439 291 }
Chris@439 292
Chris@439 293 //!!! lots of duplication with TimeValueLayer
Chris@439 294
Chris@439 295 void
matthiasm@620 296 FlexiNoteLayer::setVerticalZoomStep(int step)
Chris@439 297 {
Chris@439 298 if (shouldAutoAlign()) return;
Chris@439 299 if (!m_model) return;
Chris@439 300
Chris@439 301 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 302 if (!mapper) return;
Chris@439 303
Chris@439 304 float min, max;
Chris@439 305 bool logarithmic;
Chris@439 306 QString unit;
Chris@439 307 getValueExtents(min, max, logarithmic, unit);
Chris@439 308
Chris@439 309 float dmin, dmax;
Chris@439 310 getDisplayExtents(dmin, dmax);
Chris@439 311
Chris@439 312 float newdist = mapper->getValueForPosition(100 - step);
Chris@439 313
Chris@439 314 float newmin, newmax;
Chris@439 315
Chris@439 316 if (logarithmic) {
Chris@439 317
Chris@439 318 // see SpectrogramLayer::setVerticalZoomStep
Chris@439 319
Chris@439 320 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@439 321 newmin = newmax - newdist;
Chris@439 322
Chris@439 323 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
Chris@439 324
Chris@439 325 } else {
Chris@439 326 float dmid = (dmax + dmin) / 2;
Chris@439 327 newmin = dmid - newdist / 2;
Chris@439 328 newmax = dmid + newdist / 2;
Chris@439 329 }
Chris@439 330
Chris@439 331 if (newmin < min) {
Chris@439 332 newmax += (min - newmin);
Chris@439 333 newmin = min;
Chris@439 334 }
Chris@439 335 if (newmax > max) {
Chris@439 336 newmax = max;
Chris@439 337 }
Chris@439 338
matthiasm@620 339 SVDEBUG << "FlexiNoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@439 340
Chris@439 341 setDisplayExtents(newmin, newmax);
Chris@439 342 }
Chris@439 343
Chris@439 344 RangeMapper *
matthiasm@620 345 FlexiNoteLayer::getNewVerticalZoomRangeMapper() const
Chris@439 346 {
Chris@439 347 if (!m_model) return 0;
Chris@439 348
Chris@439 349 RangeMapper *mapper;
Chris@439 350
Chris@439 351 float min, max;
Chris@439 352 bool logarithmic;
Chris@439 353 QString unit;
Chris@439 354 getValueExtents(min, max, logarithmic, unit);
Chris@439 355
Chris@439 356 if (min == max) return 0;
Chris@439 357
Chris@439 358 if (logarithmic) {
Chris@439 359 mapper = new LogRangeMapper(0, 100, min, max, unit);
Chris@439 360 } else {
Chris@439 361 mapper = new LinearRangeMapper(0, 100, min, max, unit);
Chris@439 362 }
Chris@439 363
Chris@439 364 return mapper;
Chris@439 365 }
Chris@439 366
matthiasm@620 367 FlexiNoteModel::PointList
matthiasm@620 368 FlexiNoteLayer::getLocalPoints(View *v, int x) const
Chris@30 369 {
matthiasm@620 370 if (!m_model) return FlexiNoteModel::PointList();
Chris@30 371
Chris@44 372 long frame = v->getFrameForX(x);
Chris@30 373
matthiasm@620 374 FlexiNoteModel::PointList onPoints =
Chris@30 375 m_model->getPoints(frame);
Chris@30 376
Chris@30 377 if (!onPoints.empty()) {
Chris@30 378 return onPoints;
Chris@30 379 }
Chris@30 380
matthiasm@620 381 FlexiNoteModel::PointList prevPoints =
Chris@30 382 m_model->getPreviousPoints(frame);
matthiasm@620 383 FlexiNoteModel::PointList nextPoints =
Chris@30 384 m_model->getNextPoints(frame);
Chris@30 385
matthiasm@620 386 FlexiNoteModel::PointList usePoints = prevPoints;
Chris@30 387
Chris@30 388 if (prevPoints.empty()) {
Chris@30 389 usePoints = nextPoints;
Chris@248 390 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
Chris@44 391 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@30 392 usePoints = nextPoints;
Chris@248 393 } else if (long(nextPoints.begin()->frame) - frame <
Chris@248 394 frame - long(prevPoints.begin()->frame)) {
Chris@30 395 usePoints = nextPoints;
Chris@30 396 }
Chris@30 397
Chris@30 398 if (!usePoints.empty()) {
Chris@30 399 int fuzz = 2;
Chris@44 400 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@30 401 if ((px > x && px - x > fuzz) ||
Chris@30 402 (px < x && x - px > fuzz + 1)) {
Chris@30 403 usePoints.clear();
Chris@30 404 }
Chris@30 405 }
Chris@30 406
Chris@30 407 return usePoints;
Chris@30 408 }
Chris@30 409
Chris@550 410 bool
matthiasm@622 411 FlexiNoteLayer::getPointToDrag(View *v, int x, int y, FlexiNoteModel::Point &p) const
Chris@550 412 {
Chris@550 413 if (!m_model) return false;
Chris@550 414
Chris@550 415 long frame = v->getFrameForX(x);
Chris@550 416
matthiasm@620 417 FlexiNoteModel::PointList onPoints = m_model->getPoints(frame);
Chris@550 418 if (onPoints.empty()) return false;
Chris@550 419
Chris@551 420 // std::cerr << "frame " << frame << ": " << onPoints.size() << " candidate points" << std::endl;
Chris@550 421
Chris@550 422 int nearestDistance = -1;
Chris@550 423
matthiasm@620 424 for (FlexiNoteModel::PointList::const_iterator i = onPoints.begin();
Chris@550 425 i != onPoints.end(); ++i) {
Chris@550 426
Chris@550 427 int distance = getYForValue(v, (*i).value) - y;
Chris@550 428 if (distance < 0) distance = -distance;
Chris@550 429 if (nearestDistance == -1 || distance < nearestDistance) {
Chris@550 430 nearestDistance = distance;
Chris@550 431 p = *i;
Chris@550 432 }
Chris@550 433 }
Chris@550 434
Chris@550 435 return true;
Chris@550 436 }
Chris@550 437
Chris@30 438 QString
matthiasm@620 439 FlexiNoteLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@30 440 {
Chris@30 441 int x = pos.x();
Chris@30 442
Chris@30 443 if (!m_model || !m_model->getSampleRate()) return "";
Chris@30 444
matthiasm@620 445 FlexiNoteModel::PointList points = getLocalPoints(v, x);
Chris@30 446
Chris@30 447 if (points.empty()) {
Chris@30 448 if (!m_model->isReady()) {
Chris@30 449 return tr("In progress");
Chris@30 450 } else {
Chris@30 451 return tr("No local points");
Chris@30 452 }
Chris@30 453 }
Chris@30 454
matthiasm@620 455 FlexiNote note(0);
matthiasm@620 456 FlexiNoteModel::PointList::iterator i;
Chris@30 457
Chris@30 458 for (i = points.begin(); i != points.end(); ++i) {
Chris@30 459
Chris@44 460 int y = getYForValue(v, i->value);
Chris@30 461 int h = 3;
Chris@30 462
Chris@30 463 if (m_model->getValueQuantization() != 0.0) {
Chris@44 464 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
Chris@30 465 if (h < 3) h = 3;
Chris@30 466 }
Chris@30 467
Chris@30 468 if (pos.y() >= y - h && pos.y() <= y) {
Chris@30 469 note = *i;
Chris@30 470 break;
Chris@30 471 }
Chris@30 472 }
Chris@30 473
Chris@30 474 if (i == points.end()) return tr("No local points");
Chris@30 475
Chris@30 476 RealTime rt = RealTime::frame2RealTime(note.frame,
Chris@30 477 m_model->getSampleRate());
Chris@30 478 RealTime rd = RealTime::frame2RealTime(note.duration,
Chris@30 479 m_model->getSampleRate());
Chris@30 480
Chris@101 481 QString pitchText;
Chris@101 482
Chris@101 483 if (shouldConvertMIDIToHz()) {
Chris@101 484
Chris@101 485 int mnote = lrintf(note.value);
Chris@101 486 int cents = lrintf((note.value - mnote) * 100);
Chris@101 487 float freq = Pitch::getFrequencyForPitch(mnote, cents);
Chris@544 488 pitchText = tr("%1 (%2, %3 Hz)")
Chris@544 489 .arg(Pitch::getPitchLabel(mnote, cents))
Chris@544 490 .arg(mnote)
Chris@544 491 .arg(freq);
Chris@101 492
Chris@101 493 } else if (m_model->getScaleUnits() == "Hz") {
Chris@101 494
Chris@544 495 pitchText = tr("%1 Hz (%2, %3)")
Chris@101 496 .arg(note.value)
Chris@544 497 .arg(Pitch::getPitchLabelForFrequency(note.value))
Chris@544 498 .arg(Pitch::getPitchForFrequency(note.value));
Chris@101 499
Chris@101 500 } else {
Chris@234 501 pitchText = tr("%1 %2")
Chris@101 502 .arg(note.value).arg(m_model->getScaleUnits());
Chris@101 503 }
Chris@101 504
Chris@30 505 QString text;
Chris@30 506
Chris@30 507 if (note.label == "") {
Chris@30 508 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
Chris@30 509 .arg(rt.toText(true).c_str())
Chris@101 510 .arg(pitchText)
Chris@30 511 .arg(rd.toText(true).c_str());
Chris@30 512 } else {
Chris@30 513 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@30 514 .arg(rt.toText(true).c_str())
Chris@101 515 .arg(pitchText)
Chris@30 516 .arg(rd.toText(true).c_str())
Chris@30 517 .arg(note.label);
Chris@30 518 }
Chris@30 519
Chris@44 520 pos = QPoint(v->getXForFrame(note.frame),
Chris@44 521 getYForValue(v, note.value));
Chris@30 522 return text;
Chris@30 523 }
Chris@30 524
Chris@30 525 bool
matthiasm@620 526 FlexiNoteLayer::snapToFeatureFrame(View *v, int &frame,
Chris@44 527 size_t &resolution,
Chris@44 528 SnapType snap) const
Chris@30 529 {
Chris@30 530 if (!m_model) {
Chris@44 531 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@30 532 }
Chris@30 533
Chris@30 534 resolution = m_model->getResolution();
matthiasm@620 535 FlexiNoteModel::PointList points;
Chris@30 536
Chris@30 537 if (snap == SnapNeighbouring) {
Chris@30 538
Chris@44 539 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@30 540 if (points.empty()) return false;
Chris@30 541 frame = points.begin()->frame;
Chris@30 542 return true;
Chris@30 543 }
Chris@30 544
Chris@30 545 points = m_model->getPoints(frame, frame);
Chris@30 546 int snapped = frame;
Chris@30 547 bool found = false;
Chris@30 548
matthiasm@620 549 for (FlexiNoteModel::PointList::const_iterator i = points.begin();
Chris@30 550 i != points.end(); ++i) {
Chris@30 551
Chris@30 552 if (snap == SnapRight) {
Chris@30 553
Chris@30 554 if (i->frame > frame) {
Chris@30 555 snapped = i->frame;
Chris@30 556 found = true;
Chris@30 557 break;
Chris@30 558 }
Chris@30 559
Chris@30 560 } else if (snap == SnapLeft) {
Chris@30 561
Chris@30 562 if (i->frame <= frame) {
Chris@30 563 snapped = i->frame;
Chris@30 564 found = true; // don't break, as the next may be better
Chris@30 565 } else {
Chris@30 566 break;
Chris@30 567 }
Chris@30 568
Chris@30 569 } else { // nearest
Chris@30 570
matthiasm@620 571 FlexiNoteModel::PointList::const_iterator j = i;
Chris@30 572 ++j;
Chris@30 573
Chris@30 574 if (j == points.end()) {
Chris@30 575
Chris@30 576 snapped = i->frame;
Chris@30 577 found = true;
Chris@30 578 break;
Chris@30 579
Chris@30 580 } else if (j->frame >= frame) {
Chris@30 581
Chris@30 582 if (j->frame - frame < frame - i->frame) {
Chris@30 583 snapped = j->frame;
Chris@30 584 } else {
Chris@30 585 snapped = i->frame;
Chris@30 586 }
Chris@30 587 found = true;
Chris@30 588 break;
Chris@30 589 }
Chris@30 590 }
Chris@30 591 }
Chris@30 592
Chris@30 593 frame = snapped;
Chris@30 594 return found;
Chris@30 595 }
Chris@30 596
Chris@101 597 void
matthiasm@620 598 FlexiNoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@30 599 {
Chris@101 600 min = 0.0;
Chris@101 601 max = 0.0;
Chris@101 602 log = false;
Chris@42 603
Chris@101 604 QString queryUnits;
Chris@101 605 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
Chris@101 606 else queryUnits = m_model->getScaleUnits();
Chris@30 607
Chris@439 608 if (shouldAutoAlign()) {
Chris@30 609
Chris@101 610 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@30 611
Chris@101 612 min = m_model->getValueMinimum();
Chris@101 613 max = m_model->getValueMaximum();
Chris@42 614
Chris@101 615 if (shouldConvertMIDIToHz()) {
Chris@101 616 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 617 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 618 }
Chris@42 619
matthiasm@620 620 std::cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 621
Chris@101 622 } else if (log) {
Chris@101 623
Chris@197 624 LogRange::mapRange(min, max);
Chris@105 625
matthiasm@620 626 std::cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 627
Chris@101 628 }
Chris@101 629
Chris@101 630 } else {
Chris@101 631
Chris@439 632 getDisplayExtents(min, max);
Chris@101 633
Chris@101 634 if (m_verticalScale == MIDIRangeScale) {
Chris@101 635 min = Pitch::getFrequencyForPitch(0);
matthiasm@623 636 max = Pitch::getFrequencyForPitch(70);
Chris@101 637 } else if (shouldConvertMIDIToHz()) {
Chris@101 638 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 639 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 640 }
Chris@101 641
Chris@101 642 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@197 643 LogRange::mapRange(min, max);
Chris@101 644 log = true;
Chris@101 645 }
Chris@30 646 }
Chris@30 647
Chris@101 648 if (max == min) max = min + 1.0;
Chris@101 649 }
Chris@30 650
Chris@101 651 int
matthiasm@620 652 FlexiNoteLayer::getYForValue(View *v, float val) const
Chris@101 653 {
Chris@101 654 float min = 0.0, max = 0.0;
Chris@101 655 bool logarithmic = false;
Chris@101 656 int h = v->height();
Chris@101 657
Chris@101 658 getScaleExtents(v, min, max, logarithmic);
Chris@101 659
matthiasm@620 660 // std::cerr << "FlexiNoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl;
Chris@101 661
Chris@101 662 if (shouldConvertMIDIToHz()) {
Chris@101 663 val = Pitch::getFrequencyForPitch(lrintf(val),
Chris@101 664 lrintf((val - lrintf(val)) * 100));
Chris@101 665 // std::cerr << "shouldConvertMIDIToHz true, val now = " << val << std::endl;
Chris@101 666 }
Chris@101 667
Chris@101 668 if (logarithmic) {
Chris@197 669 val = LogRange::map(val);
Chris@101 670 // std::cerr << "logarithmic true, val now = " << val << std::endl;
Chris@101 671 }
Chris@101 672
Chris@101 673 int y = int(h - ((val - min) * h) / (max - min)) - 1;
Chris@101 674 // std::cerr << "y = " << y << std::endl;
Chris@101 675 return y;
Chris@30 676 }
Chris@30 677
Chris@30 678 float
matthiasm@620 679 FlexiNoteLayer::getValueForY(View *v, int y) const
Chris@30 680 {
Chris@101 681 float min = 0.0, max = 0.0;
Chris@101 682 bool logarithmic = false;
Chris@44 683 int h = v->height();
Chris@30 684
Chris@101 685 getScaleExtents(v, min, max, logarithmic);
Chris@101 686
Chris@101 687 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 688
Chris@101 689 if (logarithmic) {
Chris@197 690 val = powf(10.f, val);
Chris@101 691 }
Chris@101 692
Chris@101 693 if (shouldConvertMIDIToHz()) {
Chris@101 694 val = Pitch::getPitchForFrequency(val);
Chris@101 695 }
Chris@101 696
Chris@101 697 return val;
Chris@30 698 }
Chris@30 699
Chris@439 700 bool
matthiasm@620 701 FlexiNoteLayer::shouldAutoAlign() const
Chris@439 702 {
Chris@439 703 if (!m_model) return false;
Chris@439 704 return (m_verticalScale == AutoAlignScale);
Chris@439 705 }
Chris@439 706
Chris@30 707 void
matthiasm@620 708 FlexiNoteLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@30 709 {
Chris@30 710 if (!m_model || !m_model->isOK()) return;
Chris@30 711
Chris@30 712 int sampleRate = m_model->getSampleRate();
Chris@30 713 if (!sampleRate) return;
Chris@30 714
matthiasm@620 715 // Profiler profiler("FlexiNoteLayer::paint", true);
Chris@30 716
Chris@30 717 int x0 = rect.left(), x1 = rect.right();
Chris@44 718 long frame0 = v->getFrameForX(x0);
Chris@44 719 long frame1 = v->getFrameForX(x1);
Chris@30 720
matthiasm@620 721 FlexiNoteModel::PointList points(m_model->getPoints(frame0, frame1));
Chris@30 722 if (points.empty()) return;
Chris@30 723
Chris@287 724 paint.setPen(getBaseQColor());
Chris@30 725
Chris@287 726 QColor brushColour(getBaseQColor());
Chris@30 727 brushColour.setAlpha(80);
Chris@30 728
matthiasm@620 729 // SVDEBUG << "FlexiNoteLayer::paint: resolution is "
Chris@585 730 // << m_model->getResolution() << " frames" << endl;
Chris@30 731
Chris@30 732 float min = m_model->getValueMinimum();
Chris@30 733 float max = m_model->getValueMaximum();
Chris@30 734 if (max == min) max = min + 1.0;
Chris@30 735
Chris@30 736 QPoint localPos;
matthiasm@620 737 FlexiNoteModel::Point illuminatePoint(0);
Chris@551 738 bool shouldIlluminate = false;
Chris@30 739
Chris@44 740 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@551 741 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@551 742 illuminatePoint);
Chris@30 743 }
Chris@30 744
Chris@30 745 paint.save();
Chris@30 746 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 747
matthiasm@620 748 for (FlexiNoteModel::PointList::const_iterator i = points.begin();
Chris@30 749 i != points.end(); ++i) {
Chris@30 750
matthiasm@620 751 const FlexiNoteModel::Point &p(*i);
Chris@30 752
Chris@44 753 int x = v->getXForFrame(p.frame);
Chris@44 754 int y = getYForValue(v, p.value);
Chris@44 755 int w = v->getXForFrame(p.frame + p.duration) - x;
Chris@30 756 int h = 3;
Chris@30 757
Chris@30 758 if (m_model->getValueQuantization() != 0.0) {
Chris@44 759 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
Chris@30 760 if (h < 3) h = 3;
Chris@30 761 }
Chris@30 762
Chris@30 763 if (w < 1) w = 1;
Chris@287 764 paint.setPen(getBaseQColor());
Chris@30 765 paint.setBrush(brushColour);
Chris@30 766
Chris@551 767 if (shouldIlluminate &&
Chris@551 768 // "illuminatePoint == p"
matthiasm@620 769 !FlexiNoteModel::Point::Comparator()(illuminatePoint, p) &&
matthiasm@620 770 !FlexiNoteModel::Point::Comparator()(p, illuminatePoint)) {
Chris@551 771
Chris@551 772 paint.setPen(v->getForeground());
Chris@551 773 paint.setBrush(v->getForeground());
Chris@551 774
Chris@551 775 QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits());
Chris@551 776 v->drawVisibleText(paint,
Chris@551 777 x - paint.fontMetrics().width(vlabel) - 2,
Chris@551 778 y + paint.fontMetrics().height()/2
Chris@551 779 - paint.fontMetrics().descent(),
Chris@551 780 vlabel, View::OutlinedText);
Chris@551 781
Chris@551 782 QString hlabel = RealTime::frame2RealTime
Chris@551 783 (p.frame, m_model->getSampleRate()).toText(true).c_str();
Chris@551 784 v->drawVisibleText(paint,
Chris@551 785 x,
Chris@551 786 y - h/2 - paint.fontMetrics().descent() - 2,
Chris@551 787 hlabel, View::OutlinedText);
Chris@30 788 }
Chris@30 789
Chris@124 790 paint.drawRect(x, y - h/2, w, h);
Chris@30 791 }
Chris@30 792
Chris@30 793 paint.restore();
Chris@30 794 }
Chris@30 795
Chris@30 796 void
matthiasm@620 797 FlexiNoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 798 {
matthiasm@620 799 // SVDEBUG << "FlexiNoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 800
Chris@30 801 if (!m_model) return;
Chris@30 802
Chris@44 803 long frame = v->getFrameForX(e->x());
Chris@30 804 if (frame < 0) frame = 0;
Chris@30 805 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 806
Chris@44 807 float value = getValueForY(v, e->y());
Chris@30 808
matthiasm@620 809 m_editingPoint = FlexiNoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
Chris@30 810 m_originalPoint = m_editingPoint;
Chris@30 811
Chris@376 812 if (m_editingCommand) finish(m_editingCommand);
matthiasm@620 813 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
Chris@30 814 tr("Draw Point"));
Chris@30 815 m_editingCommand->addPoint(m_editingPoint);
Chris@30 816
Chris@30 817 m_editing = true;
Chris@30 818 }
Chris@30 819
Chris@30 820 void
matthiasm@620 821 FlexiNoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 822 {
matthiasm@620 823 // SVDEBUG << "FlexiNoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 824
Chris@30 825 if (!m_model || !m_editing) return;
Chris@30 826
Chris@44 827 long frame = v->getFrameForX(e->x());
Chris@30 828 if (frame < 0) frame = 0;
Chris@30 829 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 830
Chris@101 831 float newValue = getValueForY(v, e->y());
Chris@101 832
Chris@101 833 long newFrame = m_editingPoint.frame;
Chris@101 834 long newDuration = frame - newFrame;
Chris@101 835 if (newDuration < 0) {
Chris@101 836 newFrame = frame;
Chris@101 837 newDuration = -newDuration;
Chris@101 838 } else if (newDuration == 0) {
Chris@101 839 newDuration = 1;
Chris@101 840 }
Chris@30 841
Chris@30 842 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 843 m_editingPoint.frame = newFrame;
Chris@101 844 m_editingPoint.value = newValue;
Chris@101 845 m_editingPoint.duration = newDuration;
Chris@30 846 m_editingCommand->addPoint(m_editingPoint);
Chris@30 847 }
Chris@30 848
Chris@30 849 void
matthiasm@620 850 FlexiNoteLayer::drawEnd(View *, QMouseEvent *)
Chris@30 851 {
matthiasm@620 852 // SVDEBUG << "FlexiNoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 853 if (!m_model || !m_editing) return;
Chris@376 854 finish(m_editingCommand);
Chris@30 855 m_editingCommand = 0;
Chris@30 856 m_editing = false;
Chris@30 857 }
Chris@30 858
Chris@30 859 void
matthiasm@620 860 FlexiNoteLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 861 {
Chris@335 862 if (!m_model) return;
Chris@335 863
Chris@550 864 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 865
Chris@335 866 if (m_editingCommand) {
Chris@376 867 finish(m_editingCommand);
Chris@335 868 m_editingCommand = 0;
Chris@335 869 }
Chris@335 870
Chris@335 871 m_editing = true;
Chris@335 872 }
Chris@335 873
Chris@335 874 void
matthiasm@620 875 FlexiNoteLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 876 {
Chris@335 877 }
Chris@335 878
Chris@335 879 void
matthiasm@620 880 FlexiNoteLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 881 {
Chris@335 882 if (!m_model || !m_editing) return;
Chris@335 883
Chris@335 884 m_editing = false;
Chris@335 885
matthiasm@620 886 FlexiNoteModel::Point p(0);
Chris@550 887 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@550 888 if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return;
Chris@550 889
matthiasm@620 890 m_editingCommand = new FlexiNoteModel::EditCommand(m_model, tr("Erase Point"));
Chris@335 891
Chris@335 892 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 893
Chris@376 894 finish(m_editingCommand);
Chris@335 895 m_editingCommand = 0;
Chris@335 896 m_editing = false;
Chris@335 897 }
Chris@335 898
Chris@335 899 void
matthiasm@620 900 FlexiNoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 901 {
matthiasm@620 902 // SVDEBUG << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 903
Chris@30 904 if (!m_model) return;
Chris@30 905
Chris@550 906 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@30 907 m_originalPoint = m_editingPoint;
Chris@30 908
Chris@551 909 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
Chris@551 910 m_dragPointY = getYForValue(v, m_editingPoint.value);
Chris@551 911
Chris@30 912 if (m_editingCommand) {
Chris@376 913 finish(m_editingCommand);
Chris@30 914 m_editingCommand = 0;
Chris@30 915 }
Chris@30 916
Chris@30 917 m_editing = true;
Chris@551 918 m_dragStartX = e->x();
Chris@551 919 m_dragStartY = e->y();
Chris@30 920 }
Chris@30 921
Chris@30 922 void
matthiasm@620 923 FlexiNoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 924 {
matthiasm@620 925 // SVDEBUG << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 926
Chris@30 927 if (!m_model || !m_editing) return;
Chris@30 928
Chris@551 929 int xdist = e->x() - m_dragStartX;
Chris@551 930 int ydist = e->y() - m_dragStartY;
Chris@551 931 int newx = m_dragPointX + xdist;
Chris@551 932 int newy = m_dragPointY + ydist;
Chris@551 933
Chris@551 934 long frame = v->getFrameForX(newx);
Chris@30 935 if (frame < 0) frame = 0;
Chris@30 936 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 937
Chris@551 938 float value = getValueForY(v, newy);
Chris@30 939
Chris@30 940 if (!m_editingCommand) {
matthiasm@620 941 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
Chris@30 942 tr("Drag Point"));
Chris@30 943 }
Chris@30 944
Chris@30 945 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 946 m_editingPoint.frame = frame;
Chris@30 947 m_editingPoint.value = value;
Chris@30 948 m_editingCommand->addPoint(m_editingPoint);
Chris@30 949 }
Chris@30 950
Chris@30 951 void
matthiasm@620 952 FlexiNoteLayer::editEnd(View *, QMouseEvent *)
Chris@30 953 {
matthiasm@620 954 // SVDEBUG << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 955 if (!m_model || !m_editing) return;
Chris@30 956
Chris@30 957 if (m_editingCommand) {
Chris@30 958
Chris@30 959 QString newName = m_editingCommand->getName();
Chris@30 960
Chris@30 961 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@30 962 if (m_editingPoint.value != m_originalPoint.value) {
Chris@30 963 newName = tr("Edit Point");
Chris@30 964 } else {
Chris@30 965 newName = tr("Relocate Point");
Chris@30 966 }
Chris@30 967 } else {
Chris@30 968 newName = tr("Change Point Value");
Chris@30 969 }
Chris@30 970
Chris@30 971 m_editingCommand->setName(newName);
Chris@376 972 finish(m_editingCommand);
Chris@30 973 }
Chris@30 974
Chris@30 975 m_editingCommand = 0;
Chris@30 976 m_editing = false;
Chris@30 977 }
Chris@30 978
Chris@255 979 bool
matthiasm@620 980 FlexiNoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 981 {
Chris@255 982 if (!m_model) return false;
Chris@70 983
matthiasm@620 984 FlexiNoteModel::Point note(0);
Chris@550 985 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 986
matthiasm@620 987 // FlexiNoteModel::Point note = *points.begin();
Chris@70 988
Chris@70 989 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 990 (m_model->getSampleRate(),
Chris@70 991 ItemEditDialog::ShowTime |
Chris@70 992 ItemEditDialog::ShowDuration |
Chris@70 993 ItemEditDialog::ShowValue |
Chris@100 994 ItemEditDialog::ShowText,
Chris@100 995 m_model->getScaleUnits());
Chris@70 996
Chris@70 997 dialog->setFrameTime(note.frame);
Chris@70 998 dialog->setValue(note.value);
Chris@70 999 dialog->setFrameDuration(note.duration);
Chris@70 1000 dialog->setText(note.label);
Chris@70 1001
Chris@70 1002 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1003
matthiasm@620 1004 FlexiNoteModel::Point newNote = note;
Chris@70 1005 newNote.frame = dialog->getFrameTime();
Chris@70 1006 newNote.value = dialog->getValue();
Chris@70 1007 newNote.duration = dialog->getFrameDuration();
Chris@70 1008 newNote.label = dialog->getText();
Chris@70 1009
matthiasm@620 1010 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
Chris@70 1011 (m_model, tr("Edit Point"));
Chris@70 1012 command->deletePoint(note);
Chris@70 1013 command->addPoint(newNote);
Chris@376 1014 finish(command);
Chris@70 1015 }
Chris@70 1016
Chris@70 1017 delete dialog;
Chris@255 1018 return true;
Chris@70 1019 }
Chris@70 1020
Chris@70 1021 void
matthiasm@620 1022 FlexiNoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1023 {
Chris@99 1024 if (!m_model) return;
Chris@99 1025
matthiasm@620 1026 FlexiNoteModel::EditCommand *command =
matthiasm@620 1027 new FlexiNoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 1028
matthiasm@620 1029 FlexiNoteModel::PointList points =
Chris@43 1030 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1031
matthiasm@620 1032 for (FlexiNoteModel::PointList::iterator i = points.begin();
Chris@43 1033 i != points.end(); ++i) {
Chris@43 1034
Chris@43 1035 if (s.contains(i->frame)) {
matthiasm@620 1036 FlexiNoteModel::Point newPoint(*i);
Chris@43 1037 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1038 command->deletePoint(*i);
Chris@43 1039 command->addPoint(newPoint);
Chris@43 1040 }
Chris@43 1041 }
Chris@43 1042
Chris@376 1043 finish(command);
Chris@43 1044 }
Chris@43 1045
Chris@43 1046 void
matthiasm@620 1047 FlexiNoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1048 {
Chris@99 1049 if (!m_model) return;
Chris@99 1050
matthiasm@620 1051 FlexiNoteModel::EditCommand *command =
matthiasm@620 1052 new FlexiNoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1053
matthiasm@620 1054 FlexiNoteModel::PointList points =
Chris@43 1055 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1056
Chris@43 1057 double ratio =
Chris@43 1058 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1059 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1060
matthiasm@620 1061 for (FlexiNoteModel::PointList::iterator i = points.begin();
Chris@43 1062 i != points.end(); ++i) {
Chris@43 1063
Chris@43 1064 if (s.contains(i->frame)) {
Chris@43 1065
Chris@43 1066 double targetStart = i->frame;
Chris@43 1067 targetStart = newSize.getStartFrame() +
Chris@43 1068 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 1069
Chris@43 1070 double targetEnd = i->frame + i->duration;
Chris@43 1071 targetEnd = newSize.getStartFrame() +
Chris@43 1072 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 1073
matthiasm@620 1074 FlexiNoteModel::Point newPoint(*i);
Chris@43 1075 newPoint.frame = lrint(targetStart);
Chris@43 1076 newPoint.duration = lrint(targetEnd - targetStart);
Chris@43 1077 command->deletePoint(*i);
Chris@43 1078 command->addPoint(newPoint);
Chris@43 1079 }
Chris@43 1080 }
Chris@43 1081
Chris@376 1082 finish(command);
Chris@43 1083 }
Chris@43 1084
Chris@76 1085 void
matthiasm@620 1086 FlexiNoteLayer::deleteSelection(Selection s)
Chris@76 1087 {
Chris@99 1088 if (!m_model) return;
Chris@99 1089
matthiasm@620 1090 FlexiNoteModel::EditCommand *command =
matthiasm@620 1091 new FlexiNoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1092
matthiasm@620 1093 FlexiNoteModel::PointList points =
Chris@76 1094 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1095
matthiasm@620 1096 for (FlexiNoteModel::PointList::iterator i = points.begin();
Chris@76 1097 i != points.end(); ++i) {
Chris@76 1098
Chris@76 1099 if (s.contains(i->frame)) {
Chris@76 1100 command->deletePoint(*i);
Chris@76 1101 }
Chris@76 1102 }
Chris@76 1103
Chris@376 1104 finish(command);
Chris@76 1105 }
Chris@76 1106
Chris@76 1107 void
matthiasm@620 1108 FlexiNoteLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1109 {
Chris@99 1110 if (!m_model) return;
Chris@99 1111
matthiasm@620 1112 FlexiNoteModel::PointList points =
Chris@76 1113 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1114
matthiasm@620 1115 for (FlexiNoteModel::PointList::iterator i = points.begin();
Chris@76 1116 i != points.end(); ++i) {
Chris@76 1117 if (s.contains(i->frame)) {
Chris@335 1118 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
Chris@360 1119 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1120 to.addPoint(point);
Chris@76 1121 }
Chris@76 1122 }
Chris@76 1123 }
Chris@76 1124
Chris@125 1125 bool
matthiasm@620 1126 FlexiNoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
Chris@76 1127 {
Chris@125 1128 if (!m_model) return false;
Chris@99 1129
Chris@76 1130 const Clipboard::PointList &points = from.getPoints();
Chris@76 1131
Chris@360 1132 bool realign = false;
Chris@360 1133
Chris@360 1134 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1135
Chris@360 1136 QMessageBox::StandardButton button =
Chris@360 1137 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1138 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 1139 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1140 QMessageBox::Yes);
Chris@360 1141
Chris@360 1142 if (button == QMessageBox::Cancel) {
Chris@360 1143 return false;
Chris@360 1144 }
Chris@360 1145
Chris@360 1146 if (button == QMessageBox::Yes) {
Chris@360 1147 realign = true;
Chris@360 1148 }
Chris@360 1149 }
Chris@360 1150
matthiasm@620 1151 FlexiNoteModel::EditCommand *command =
matthiasm@620 1152 new FlexiNoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1153
Chris@76 1154 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1155 i != points.end(); ++i) {
Chris@76 1156
Chris@76 1157 if (!i->haveFrame()) continue;
Chris@76 1158 size_t frame = 0;
Chris@360 1159
Chris@360 1160 if (!realign) {
Chris@360 1161
Chris@360 1162 frame = i->getFrame();
Chris@360 1163
Chris@360 1164 } else {
Chris@360 1165
Chris@360 1166 if (i->haveReferenceFrame()) {
Chris@360 1167 frame = i->getReferenceFrame();
Chris@360 1168 frame = alignFromReference(v, frame);
Chris@360 1169 } else {
Chris@360 1170 frame = i->getFrame();
Chris@360 1171 }
Chris@76 1172 }
Chris@360 1173
matthiasm@620 1174 FlexiNoteModel::Point newPoint(frame);
Chris@76 1175
Chris@76 1176 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1177 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1178 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1179 m_model->getValueMaximum()) / 2;
Chris@335 1180 if (i->haveLevel()) newPoint.level = i->getLevel();
Chris@76 1181 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 1182 else {
Chris@125 1183 size_t nextFrame = frame;
Chris@125 1184 Clipboard::PointList::const_iterator j = i;
Chris@125 1185 for (; j != points.end(); ++j) {
Chris@125 1186 if (!j->haveFrame()) continue;
Chris@125 1187 if (j != i) break;
Chris@125 1188 }
Chris@125 1189 if (j != points.end()) {
Chris@125 1190 nextFrame = j->getFrame();
Chris@125 1191 }
Chris@125 1192 if (nextFrame == frame) {
Chris@125 1193 newPoint.duration = m_model->getResolution();
Chris@125 1194 } else {
Chris@125 1195 newPoint.duration = nextFrame - frame;
Chris@125 1196 }
Chris@125 1197 }
Chris@76 1198
Chris@76 1199 command->addPoint(newPoint);
Chris@76 1200 }
Chris@76 1201
Chris@376 1202 finish(command);
Chris@125 1203 return true;
Chris@76 1204 }
Chris@76 1205
Chris@507 1206 void
matthiasm@620 1207 FlexiNoteLayer::addNoteOn(long frame, int pitch, int velocity)
Chris@507 1208 {
matthiasm@620 1209 m_pendingNoteOns.insert(FlexiNote(frame, pitch, 0, float(velocity) / 127.0, ""));
Chris@507 1210 }
Chris@507 1211
Chris@507 1212 void
matthiasm@620 1213 FlexiNoteLayer::addNoteOff(long frame, int pitch)
Chris@507 1214 {
matthiasm@620 1215 for (FlexiNoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1216 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1217 if (lrintf((*i).value) == pitch) {
matthiasm@620 1218 FlexiNote note(*i);
Chris@507 1219 m_pendingNoteOns.erase(i);
Chris@507 1220 note.duration = frame - note.frame;
Chris@507 1221 if (m_model) {
matthiasm@620 1222 FlexiNoteModel::AddPointCommand *c = new FlexiNoteModel::AddPointCommand
matthiasm@620 1223 (m_model, note, tr("Record FlexiNote"));
Chris@507 1224 // execute and bundle:
Chris@507 1225 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1226 }
Chris@507 1227 break;
Chris@507 1228 }
Chris@507 1229 }
Chris@507 1230 }
Chris@507 1231
Chris@507 1232 void
matthiasm@620 1233 FlexiNoteLayer::abandonNoteOns()
Chris@507 1234 {
Chris@507 1235 m_pendingNoteOns.clear();
Chris@507 1236 }
Chris@507 1237
Chris@287 1238 int
matthiasm@620 1239 FlexiNoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1240 {
Chris@287 1241 impose = false;
Chris@287 1242 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1243 (QString(darkbg ? "White" : "Black"));
Chris@287 1244 }
Chris@287 1245
Chris@316 1246 void
matthiasm@620 1247 FlexiNoteLayer::toXml(QTextStream &stream,
Chris@316 1248 QString indent, QString extraAttributes) const
Chris@30 1249 {
Chris@316 1250 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1251 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1252 .arg(m_verticalScale)
Chris@445 1253 .arg(m_scaleMinimum)
Chris@445 1254 .arg(m_scaleMaximum));
Chris@30 1255 }
Chris@30 1256
Chris@30 1257 void
matthiasm@620 1258 FlexiNoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1259 {
Chris@287 1260 SingleColourLayer::setProperties(attributes);
Chris@30 1261
Chris@445 1262 bool ok, alsoOk;
Chris@30 1263 VerticalScale scale = (VerticalScale)
Chris@30 1264 attributes.value("verticalScale").toInt(&ok);
Chris@30 1265 if (ok) setVerticalScale(scale);
Chris@445 1266
Chris@445 1267 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1268 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@445 1269 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@30 1270 }
Chris@30 1271
Chris@30 1272