annotate layer/FlexiNoteLayer.cpp @ 627:640c3792977f tonioni

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