annotate layer/FlexiNoteLayer.cpp @ 633:03d5f370c9a0 tonioni

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