annotate layer/FlexiNoteLayer.cpp @ 625:1336439dc055 tonioni

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