annotate layer/FlexiNoteLayer.cpp @ 626:7256926d3af1 tonioni

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