annotate layer/NoteLayer.cpp @ 216:34bbbcb3c01f sv1-1.0pre1

* Make getPropertyRangeAndValue return the default separately from the current value. Previously some contexts were incorrectly treating the current value as a default.
author Chris Cannam
date Fri, 02 Mar 2007 14:00:12 +0000
parents c2ed5014d4ff
children b4809e942e7d
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
Chris@30 16 #include "NoteLayer.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@128 23 #include "view/View.h"
Chris@30 24
Chris@128 25 #include "data/model/NoteModel.h"
Chris@30 26
Chris@70 27 #include "widgets/ItemEditDialog.h"
Chris@70 28
Chris@42 29 #include "SpectrogramLayer.h" // for optional frequency alignment
Chris@42 30
Chris@30 31 #include <QPainter>
Chris@30 32 #include <QPainterPath>
Chris@30 33 #include <QMouseEvent>
Chris@30 34
Chris@30 35 #include <iostream>
Chris@30 36 #include <cmath>
Chris@30 37
Chris@44 38 NoteLayer::NoteLayer() :
Chris@44 39 Layer(),
Chris@30 40 m_model(0),
Chris@30 41 m_editing(false),
Chris@30 42 m_originalPoint(0, 0.0, 0, tr("New Point")),
Chris@30 43 m_editingPoint(0, 0.0, 0, tr("New Point")),
Chris@30 44 m_editingCommand(0),
Chris@30 45 m_colour(Qt::black),
Chris@101 46 m_verticalScale(AutoAlignScale)
Chris@30 47 {
Chris@44 48
Chris@30 49 }
Chris@30 50
Chris@30 51 void
Chris@30 52 NoteLayer::setModel(NoteModel *model)
Chris@30 53 {
Chris@30 54 if (m_model == model) return;
Chris@30 55 m_model = model;
Chris@30 56
Chris@30 57 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@30 58 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@30 59 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@30 60
Chris@30 61 connect(m_model, SIGNAL(completionChanged()),
Chris@30 62 this, SIGNAL(modelCompletionChanged()));
Chris@30 63
Chris@101 64 // std::cerr << "NoteLayer::setModel(" << model << ")" << std::endl;
Chris@30 65
Chris@30 66 emit modelReplaced();
Chris@30 67 }
Chris@30 68
Chris@30 69 Layer::PropertyList
Chris@30 70 NoteLayer::getProperties() const
Chris@30 71 {
Chris@30 72 PropertyList list;
Chris@87 73 list.push_back("Colour");
Chris@87 74 list.push_back("Vertical Scale");
Chris@100 75 list.push_back("Scale Units");
Chris@30 76 return list;
Chris@30 77 }
Chris@30 78
Chris@87 79 QString
Chris@87 80 NoteLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 81 {
Chris@87 82 if (name == "Colour") return tr("Colour");
Chris@87 83 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@116 84 if (name == "Scale Units") return tr("Scale Units");
Chris@87 85 return "";
Chris@87 86 }
Chris@87 87
Chris@30 88 Layer::PropertyType
Chris@100 89 NoteLayer::getPropertyType(const PropertyName &name) const
Chris@30 90 {
Chris@100 91 if (name == "Scale Units") return UnitsProperty;
Chris@30 92 return ValueProperty;
Chris@30 93 }
Chris@30 94
Chris@198 95 QString
Chris@198 96 NoteLayer::getPropertyGroupName(const PropertyName &name) const
Chris@198 97 {
Chris@198 98 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@198 99 return tr("Scale");
Chris@198 100 }
Chris@198 101 return QString();
Chris@198 102 }
Chris@198 103
Chris@30 104 int
Chris@30 105 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 106 int *min, int *max, int *deflt) const
Chris@30 107 {
Chris@30 108 //!!! factor this colour handling stuff out into a colour manager class
Chris@30 109
Chris@216 110 int val = 0;
Chris@30 111
Chris@87 112 if (name == "Colour") {
Chris@30 113
Chris@30 114 if (min) *min = 0;
Chris@30 115 if (max) *max = 5;
Chris@216 116 if (deflt) *deflt = 0;
Chris@30 117
Chris@216 118 if (m_colour == Qt::black) val = 0;
Chris@216 119 else if (m_colour == Qt::darkRed) val = 1;
Chris@216 120 else if (m_colour == Qt::darkBlue) val = 2;
Chris@216 121 else if (m_colour == Qt::darkGreen) val = 3;
Chris@216 122 else if (m_colour == QColor(200, 50, 255)) val = 4;
Chris@216 123 else if (m_colour == QColor(255, 150, 50)) val = 5;
Chris@30 124
Chris@87 125 } else if (name == "Vertical Scale") {
Chris@30 126
Chris@30 127 if (min) *min = 0;
Chris@101 128 if (max) *max = 3;
Chris@216 129 if (deflt) *deflt = int(AutoAlignScale);
Chris@30 130
Chris@216 131 val = int(m_verticalScale);
Chris@30 132
Chris@100 133 } else if (name == "Scale Units") {
Chris@100 134
Chris@216 135 if (deflt) *deflt = 0;
Chris@100 136 if (m_model) {
Chris@216 137 val = UnitDatabase::getInstance()->getUnitId
Chris@100 138 (m_model->getScaleUnits());
Chris@100 139 }
Chris@100 140
Chris@30 141 } else {
Chris@216 142
Chris@216 143 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@30 144 }
Chris@30 145
Chris@216 146 return val;
Chris@30 147 }
Chris@30 148
Chris@30 149 QString
Chris@30 150 NoteLayer::getPropertyValueLabel(const PropertyName &name,
Chris@30 151 int value) const
Chris@30 152 {
Chris@87 153 if (name == "Colour") {
Chris@30 154 switch (value) {
Chris@30 155 default:
Chris@30 156 case 0: return tr("Black");
Chris@30 157 case 1: return tr("Red");
Chris@30 158 case 2: return tr("Blue");
Chris@30 159 case 3: return tr("Green");
Chris@30 160 case 4: return tr("Purple");
Chris@30 161 case 5: return tr("Orange");
Chris@30 162 }
Chris@87 163 } else if (name == "Vertical Scale") {
Chris@30 164 switch (value) {
Chris@30 165 default:
Chris@101 166 case 0: return tr("Auto-Align");
Chris@198 167 case 1: return tr("Linear");
Chris@198 168 case 2: return tr("Log");
Chris@198 169 case 3: return tr("MIDI Notes");
Chris@30 170 }
Chris@30 171 }
Chris@30 172 return tr("<unknown>");
Chris@30 173 }
Chris@30 174
Chris@30 175 void
Chris@30 176 NoteLayer::setProperty(const PropertyName &name, int value)
Chris@30 177 {
Chris@87 178 if (name == "Colour") {
Chris@30 179 switch (value) {
Chris@30 180 default:
Chris@30 181 case 0: setBaseColour(Qt::black); break;
Chris@30 182 case 1: setBaseColour(Qt::darkRed); break;
Chris@30 183 case 2: setBaseColour(Qt::darkBlue); break;
Chris@30 184 case 3: setBaseColour(Qt::darkGreen); break;
Chris@30 185 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@30 186 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@30 187 }
Chris@87 188 } else if (name == "Vertical Scale") {
Chris@30 189 setVerticalScale(VerticalScale(value));
Chris@100 190 } else if (name == "Scale Units") {
Chris@100 191 if (m_model) {
Chris@100 192 m_model->setScaleUnits
Chris@100 193 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 194 emit modelChanged();
Chris@100 195 }
Chris@30 196 }
Chris@30 197 }
Chris@30 198
Chris@30 199 void
Chris@30 200 NoteLayer::setBaseColour(QColor colour)
Chris@30 201 {
Chris@30 202 if (m_colour == colour) return;
Chris@30 203 m_colour = colour;
Chris@30 204 emit layerParametersChanged();
Chris@30 205 }
Chris@30 206
Chris@30 207 void
Chris@30 208 NoteLayer::setVerticalScale(VerticalScale scale)
Chris@30 209 {
Chris@30 210 if (m_verticalScale == scale) return;
Chris@30 211 m_verticalScale = scale;
Chris@30 212 emit layerParametersChanged();
Chris@30 213 }
Chris@30 214
Chris@30 215 bool
Chris@44 216 NoteLayer::isLayerScrollable(const View *v) const
Chris@30 217 {
Chris@30 218 QPoint discard;
Chris@44 219 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@30 220 }
Chris@30 221
Chris@79 222 bool
Chris@101 223 NoteLayer::shouldConvertMIDIToHz() const
Chris@101 224 {
Chris@101 225 QString unit = m_model->getScaleUnits();
Chris@101 226 return (unit != "Hz");
Chris@101 227 // if (unit == "" ||
Chris@101 228 // unit.startsWith("MIDI") ||
Chris@101 229 // unit.startsWith("midi")) return true;
Chris@101 230 // return false;
Chris@101 231 }
Chris@101 232
Chris@101 233 bool
Chris@101 234 NoteLayer::getValueExtents(float &min, float &max,
Chris@101 235 bool &logarithmic, QString &unit) const
Chris@79 236 {
Chris@79 237 if (!m_model) return false;
Chris@79 238 min = m_model->getValueMinimum();
Chris@79 239 max = m_model->getValueMaximum();
Chris@101 240
Chris@105 241 if (shouldConvertMIDIToHz()) {
Chris@105 242 unit = "Hz";
Chris@105 243 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@105 244 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@105 245 } else unit = m_model->getScaleUnits();
Chris@101 246
Chris@101 247 if (m_verticalScale == MIDIRangeScale ||
Chris@101 248 m_verticalScale == LogScale) logarithmic = true;
Chris@101 249
Chris@101 250 return true;
Chris@101 251 }
Chris@101 252
Chris@101 253 bool
Chris@101 254 NoteLayer::getDisplayExtents(float &min, float &max) const
Chris@101 255 {
Chris@101 256 if (!m_model || m_verticalScale == AutoAlignScale) return false;
Chris@101 257
Chris@101 258 if (m_verticalScale == MIDIRangeScale) {
Chris@101 259 min = Pitch::getFrequencyForPitch(0);
Chris@101 260 max = Pitch::getFrequencyForPitch(127);
Chris@101 261 return true;
Chris@101 262 }
Chris@101 263
Chris@101 264 min = m_model->getValueMinimum();
Chris@101 265 max = m_model->getValueMaximum();
Chris@101 266
Chris@101 267 if (shouldConvertMIDIToHz()) {
Chris@101 268 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 269 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 270 }
Chris@101 271
Chris@79 272 return true;
Chris@79 273 }
Chris@79 274
Chris@30 275 NoteModel::PointList
Chris@44 276 NoteLayer::getLocalPoints(View *v, int x) const
Chris@30 277 {
Chris@30 278 if (!m_model) return NoteModel::PointList();
Chris@30 279
Chris@44 280 long frame = v->getFrameForX(x);
Chris@30 281
Chris@30 282 NoteModel::PointList onPoints =
Chris@30 283 m_model->getPoints(frame);
Chris@30 284
Chris@30 285 if (!onPoints.empty()) {
Chris@30 286 return onPoints;
Chris@30 287 }
Chris@30 288
Chris@30 289 NoteModel::PointList prevPoints =
Chris@30 290 m_model->getPreviousPoints(frame);
Chris@30 291 NoteModel::PointList nextPoints =
Chris@30 292 m_model->getNextPoints(frame);
Chris@30 293
Chris@30 294 NoteModel::PointList usePoints = prevPoints;
Chris@30 295
Chris@30 296 if (prevPoints.empty()) {
Chris@30 297 usePoints = nextPoints;
Chris@44 298 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
Chris@44 299 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@30 300 usePoints = nextPoints;
Chris@30 301 } else if (nextPoints.begin()->frame - frame <
Chris@30 302 frame - prevPoints.begin()->frame) {
Chris@30 303 usePoints = nextPoints;
Chris@30 304 }
Chris@30 305
Chris@30 306 if (!usePoints.empty()) {
Chris@30 307 int fuzz = 2;
Chris@44 308 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@30 309 if ((px > x && px - x > fuzz) ||
Chris@30 310 (px < x && x - px > fuzz + 1)) {
Chris@30 311 usePoints.clear();
Chris@30 312 }
Chris@30 313 }
Chris@30 314
Chris@30 315 return usePoints;
Chris@30 316 }
Chris@30 317
Chris@30 318 QString
Chris@44 319 NoteLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@30 320 {
Chris@30 321 int x = pos.x();
Chris@30 322
Chris@30 323 if (!m_model || !m_model->getSampleRate()) return "";
Chris@30 324
Chris@44 325 NoteModel::PointList points = getLocalPoints(v, x);
Chris@30 326
Chris@30 327 if (points.empty()) {
Chris@30 328 if (!m_model->isReady()) {
Chris@30 329 return tr("In progress");
Chris@30 330 } else {
Chris@30 331 return tr("No local points");
Chris@30 332 }
Chris@30 333 }
Chris@30 334
Chris@30 335 Note note(0);
Chris@30 336 NoteModel::PointList::iterator i;
Chris@30 337
Chris@30 338 for (i = points.begin(); i != points.end(); ++i) {
Chris@30 339
Chris@44 340 int y = getYForValue(v, i->value);
Chris@30 341 int h = 3;
Chris@30 342
Chris@30 343 if (m_model->getValueQuantization() != 0.0) {
Chris@44 344 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
Chris@30 345 if (h < 3) h = 3;
Chris@30 346 }
Chris@30 347
Chris@30 348 if (pos.y() >= y - h && pos.y() <= y) {
Chris@30 349 note = *i;
Chris@30 350 break;
Chris@30 351 }
Chris@30 352 }
Chris@30 353
Chris@30 354 if (i == points.end()) return tr("No local points");
Chris@30 355
Chris@30 356 RealTime rt = RealTime::frame2RealTime(note.frame,
Chris@30 357 m_model->getSampleRate());
Chris@30 358 RealTime rd = RealTime::frame2RealTime(note.duration,
Chris@30 359 m_model->getSampleRate());
Chris@30 360
Chris@101 361 QString pitchText;
Chris@101 362
Chris@101 363 if (shouldConvertMIDIToHz()) {
Chris@101 364
Chris@101 365 int mnote = lrintf(note.value);
Chris@101 366 int cents = lrintf((note.value - mnote) * 100);
Chris@101 367 float freq = Pitch::getFrequencyForPitch(mnote, cents);
Chris@101 368 pitchText = QString("%1 (%2 Hz)")
Chris@101 369 .arg(Pitch::getPitchLabel(mnote, cents)).arg(freq);
Chris@101 370
Chris@101 371 } else if (m_model->getScaleUnits() == "Hz") {
Chris@101 372
Chris@101 373 pitchText = QString("%1 Hz (%2)")
Chris@101 374 .arg(note.value)
Chris@101 375 .arg(Pitch::getPitchLabelForFrequency(note.value));
Chris@101 376
Chris@101 377 } else {
Chris@101 378 pitchText = QString("%1 %2")
Chris@101 379 .arg(note.value).arg(m_model->getScaleUnits());
Chris@101 380 }
Chris@101 381
Chris@30 382 QString text;
Chris@30 383
Chris@30 384 if (note.label == "") {
Chris@30 385 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
Chris@30 386 .arg(rt.toText(true).c_str())
Chris@101 387 .arg(pitchText)
Chris@30 388 .arg(rd.toText(true).c_str());
Chris@30 389 } else {
Chris@30 390 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@30 391 .arg(rt.toText(true).c_str())
Chris@101 392 .arg(pitchText)
Chris@30 393 .arg(rd.toText(true).c_str())
Chris@30 394 .arg(note.label);
Chris@30 395 }
Chris@30 396
Chris@44 397 pos = QPoint(v->getXForFrame(note.frame),
Chris@44 398 getYForValue(v, note.value));
Chris@30 399 return text;
Chris@30 400 }
Chris@30 401
Chris@30 402 bool
Chris@44 403 NoteLayer::snapToFeatureFrame(View *v, int &frame,
Chris@44 404 size_t &resolution,
Chris@44 405 SnapType snap) const
Chris@30 406 {
Chris@30 407 if (!m_model) {
Chris@44 408 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@30 409 }
Chris@30 410
Chris@30 411 resolution = m_model->getResolution();
Chris@30 412 NoteModel::PointList points;
Chris@30 413
Chris@30 414 if (snap == SnapNeighbouring) {
Chris@30 415
Chris@44 416 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@30 417 if (points.empty()) return false;
Chris@30 418 frame = points.begin()->frame;
Chris@30 419 return true;
Chris@30 420 }
Chris@30 421
Chris@30 422 points = m_model->getPoints(frame, frame);
Chris@30 423 int snapped = frame;
Chris@30 424 bool found = false;
Chris@30 425
Chris@30 426 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 427 i != points.end(); ++i) {
Chris@30 428
Chris@30 429 if (snap == SnapRight) {
Chris@30 430
Chris@30 431 if (i->frame > frame) {
Chris@30 432 snapped = i->frame;
Chris@30 433 found = true;
Chris@30 434 break;
Chris@30 435 }
Chris@30 436
Chris@30 437 } else if (snap == SnapLeft) {
Chris@30 438
Chris@30 439 if (i->frame <= frame) {
Chris@30 440 snapped = i->frame;
Chris@30 441 found = true; // don't break, as the next may be better
Chris@30 442 } else {
Chris@30 443 break;
Chris@30 444 }
Chris@30 445
Chris@30 446 } else { // nearest
Chris@30 447
Chris@30 448 NoteModel::PointList::const_iterator j = i;
Chris@30 449 ++j;
Chris@30 450
Chris@30 451 if (j == points.end()) {
Chris@30 452
Chris@30 453 snapped = i->frame;
Chris@30 454 found = true;
Chris@30 455 break;
Chris@30 456
Chris@30 457 } else if (j->frame >= frame) {
Chris@30 458
Chris@30 459 if (j->frame - frame < frame - i->frame) {
Chris@30 460 snapped = j->frame;
Chris@30 461 } else {
Chris@30 462 snapped = i->frame;
Chris@30 463 }
Chris@30 464 found = true;
Chris@30 465 break;
Chris@30 466 }
Chris@30 467 }
Chris@30 468 }
Chris@30 469
Chris@30 470 frame = snapped;
Chris@30 471 return found;
Chris@30 472 }
Chris@30 473
Chris@101 474 void
Chris@101 475 NoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@30 476 {
Chris@101 477 min = 0.0;
Chris@101 478 max = 0.0;
Chris@101 479 log = false;
Chris@42 480
Chris@101 481 QString queryUnits;
Chris@101 482 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
Chris@101 483 else queryUnits = m_model->getScaleUnits();
Chris@30 484
Chris@101 485 if (m_verticalScale == AutoAlignScale) {
Chris@30 486
Chris@101 487 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@30 488
Chris@101 489 min = m_model->getValueMinimum();
Chris@101 490 max = m_model->getValueMaximum();
Chris@42 491
Chris@101 492 if (shouldConvertMIDIToHz()) {
Chris@101 493 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 494 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 495 }
Chris@42 496
Chris@105 497 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 498
Chris@101 499 } else if (log) {
Chris@101 500
Chris@197 501 LogRange::mapRange(min, max);
Chris@105 502
Chris@105 503 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 504
Chris@101 505 }
Chris@101 506
Chris@101 507 } else {
Chris@101 508
Chris@101 509 min = m_model->getValueMinimum();
Chris@101 510 max = m_model->getValueMaximum();
Chris@101 511
Chris@101 512 if (m_verticalScale == MIDIRangeScale) {
Chris@101 513 min = Pitch::getFrequencyForPitch(0);
Chris@101 514 max = Pitch::getFrequencyForPitch(127);
Chris@101 515 } else if (shouldConvertMIDIToHz()) {
Chris@101 516 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 517 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 518 }
Chris@101 519
Chris@101 520 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@197 521 LogRange::mapRange(min, max);
Chris@101 522 log = true;
Chris@101 523 }
Chris@30 524 }
Chris@30 525
Chris@101 526 if (max == min) max = min + 1.0;
Chris@101 527 }
Chris@30 528
Chris@101 529 int
Chris@101 530 NoteLayer::getYForValue(View *v, float val) const
Chris@101 531 {
Chris@101 532 float min = 0.0, max = 0.0;
Chris@101 533 bool logarithmic = false;
Chris@101 534 int h = v->height();
Chris@101 535
Chris@101 536 getScaleExtents(v, min, max, logarithmic);
Chris@101 537
Chris@101 538 // std::cerr << "NoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl;
Chris@101 539
Chris@101 540 if (shouldConvertMIDIToHz()) {
Chris@101 541 val = Pitch::getFrequencyForPitch(lrintf(val),
Chris@101 542 lrintf((val - lrintf(val)) * 100));
Chris@101 543 // std::cerr << "shouldConvertMIDIToHz true, val now = " << val << std::endl;
Chris@101 544 }
Chris@101 545
Chris@101 546 if (logarithmic) {
Chris@197 547 val = LogRange::map(val);
Chris@101 548 // std::cerr << "logarithmic true, val now = " << val << std::endl;
Chris@101 549 }
Chris@101 550
Chris@101 551 int y = int(h - ((val - min) * h) / (max - min)) - 1;
Chris@101 552 // std::cerr << "y = " << y << std::endl;
Chris@101 553 return y;
Chris@30 554 }
Chris@30 555
Chris@30 556 float
Chris@44 557 NoteLayer::getValueForY(View *v, int y) const
Chris@30 558 {
Chris@101 559 float min = 0.0, max = 0.0;
Chris@101 560 bool logarithmic = false;
Chris@44 561 int h = v->height();
Chris@30 562
Chris@101 563 getScaleExtents(v, min, max, logarithmic);
Chris@101 564
Chris@101 565 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 566
Chris@101 567 if (logarithmic) {
Chris@197 568 val = powf(10.f, val);
Chris@101 569 }
Chris@101 570
Chris@101 571 if (shouldConvertMIDIToHz()) {
Chris@101 572 val = Pitch::getPitchForFrequency(val);
Chris@101 573 }
Chris@101 574
Chris@101 575 return val;
Chris@30 576 }
Chris@30 577
Chris@30 578 void
Chris@44 579 NoteLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@30 580 {
Chris@30 581 if (!m_model || !m_model->isOK()) return;
Chris@30 582
Chris@30 583 int sampleRate = m_model->getSampleRate();
Chris@30 584 if (!sampleRate) return;
Chris@30 585
Chris@30 586 // Profiler profiler("NoteLayer::paint", true);
Chris@30 587
Chris@30 588 int x0 = rect.left(), x1 = rect.right();
Chris@44 589 long frame0 = v->getFrameForX(x0);
Chris@44 590 long frame1 = v->getFrameForX(x1);
Chris@30 591
Chris@30 592 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
Chris@30 593 if (points.empty()) return;
Chris@30 594
Chris@30 595 paint.setPen(m_colour);
Chris@30 596
Chris@30 597 QColor brushColour(m_colour);
Chris@30 598 brushColour.setAlpha(80);
Chris@30 599
Chris@30 600 // std::cerr << "NoteLayer::paint: resolution is "
Chris@30 601 // << m_model->getResolution() << " frames" << std::endl;
Chris@30 602
Chris@30 603 float min = m_model->getValueMinimum();
Chris@30 604 float max = m_model->getValueMaximum();
Chris@30 605 if (max == min) max = min + 1.0;
Chris@30 606
Chris@44 607 int origin = int(nearbyint(v->height() -
Chris@44 608 (-min * v->height()) / (max - min)));
Chris@30 609
Chris@30 610 QPoint localPos;
Chris@30 611 long illuminateFrame = -1;
Chris@30 612
Chris@44 613 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@30 614 NoteModel::PointList localPoints =
Chris@44 615 getLocalPoints(v, localPos.x());
Chris@30 616 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@30 617 }
Chris@30 618
Chris@30 619 paint.save();
Chris@30 620 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 621
Chris@30 622 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 623 i != points.end(); ++i) {
Chris@30 624
Chris@30 625 const NoteModel::Point &p(*i);
Chris@30 626
Chris@44 627 int x = v->getXForFrame(p.frame);
Chris@44 628 int y = getYForValue(v, p.value);
Chris@44 629 int w = v->getXForFrame(p.frame + p.duration) - x;
Chris@30 630 int h = 3;
Chris@30 631
Chris@30 632 if (m_model->getValueQuantization() != 0.0) {
Chris@44 633 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
Chris@30 634 if (h < 3) h = 3;
Chris@30 635 }
Chris@30 636
Chris@30 637 if (w < 1) w = 1;
Chris@30 638 paint.setPen(m_colour);
Chris@30 639 paint.setBrush(brushColour);
Chris@30 640
Chris@30 641 if (illuminateFrame == p.frame) {
Chris@30 642 if (localPos.y() >= y - h && localPos.y() < y) {
Chris@30 643 paint.setPen(Qt::black);//!!!
Chris@30 644 paint.setBrush(Qt::black);//!!!
Chris@30 645 }
Chris@30 646 }
Chris@30 647
Chris@124 648 paint.drawRect(x, y - h/2, w, h);
Chris@30 649
Chris@30 650 /// if (p.label != "") {
Chris@30 651 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@30 652 /// }
Chris@30 653 }
Chris@30 654
Chris@30 655 paint.restore();
Chris@30 656 }
Chris@30 657
Chris@30 658 void
Chris@44 659 NoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 660 {
Chris@101 661 // std::cerr << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 662
Chris@30 663 if (!m_model) return;
Chris@30 664
Chris@44 665 long frame = v->getFrameForX(e->x());
Chris@30 666 if (frame < 0) frame = 0;
Chris@30 667 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 668
Chris@44 669 float value = getValueForY(v, e->y());
Chris@30 670
Chris@30 671 m_editingPoint = NoteModel::Point(frame, value, 0, tr("New Point"));
Chris@30 672 m_originalPoint = m_editingPoint;
Chris@30 673
Chris@30 674 if (m_editingCommand) m_editingCommand->finish();
Chris@30 675 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 676 tr("Draw Point"));
Chris@30 677 m_editingCommand->addPoint(m_editingPoint);
Chris@30 678
Chris@30 679 m_editing = true;
Chris@30 680 }
Chris@30 681
Chris@30 682 void
Chris@44 683 NoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 684 {
Chris@101 685 // std::cerr << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 686
Chris@30 687 if (!m_model || !m_editing) return;
Chris@30 688
Chris@44 689 long frame = v->getFrameForX(e->x());
Chris@30 690 if (frame < 0) frame = 0;
Chris@30 691 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 692
Chris@101 693 float newValue = getValueForY(v, e->y());
Chris@101 694
Chris@101 695 long newFrame = m_editingPoint.frame;
Chris@101 696 long newDuration = frame - newFrame;
Chris@101 697 if (newDuration < 0) {
Chris@101 698 newFrame = frame;
Chris@101 699 newDuration = -newDuration;
Chris@101 700 } else if (newDuration == 0) {
Chris@101 701 newDuration = 1;
Chris@101 702 }
Chris@30 703
Chris@30 704 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 705 m_editingPoint.frame = newFrame;
Chris@101 706 m_editingPoint.value = newValue;
Chris@101 707 m_editingPoint.duration = newDuration;
Chris@30 708 m_editingCommand->addPoint(m_editingPoint);
Chris@30 709 }
Chris@30 710
Chris@30 711 void
Chris@44 712 NoteLayer::drawEnd(View *v, QMouseEvent *e)
Chris@30 713 {
Chris@101 714 // std::cerr << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 715 if (!m_model || !m_editing) return;
Chris@30 716 m_editingCommand->finish();
Chris@30 717 m_editingCommand = 0;
Chris@30 718 m_editing = false;
Chris@30 719 }
Chris@30 720
Chris@30 721 void
Chris@44 722 NoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 723 {
Chris@101 724 // std::cerr << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 725
Chris@30 726 if (!m_model) return;
Chris@30 727
Chris@44 728 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@30 729 if (points.empty()) return;
Chris@30 730
Chris@30 731 m_editingPoint = *points.begin();
Chris@30 732 m_originalPoint = m_editingPoint;
Chris@30 733
Chris@30 734 if (m_editingCommand) {
Chris@30 735 m_editingCommand->finish();
Chris@30 736 m_editingCommand = 0;
Chris@30 737 }
Chris@30 738
Chris@30 739 m_editing = true;
Chris@30 740 }
Chris@30 741
Chris@30 742 void
Chris@44 743 NoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 744 {
Chris@101 745 // std::cerr << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 746
Chris@30 747 if (!m_model || !m_editing) return;
Chris@30 748
Chris@44 749 long frame = v->getFrameForX(e->x());
Chris@30 750 if (frame < 0) frame = 0;
Chris@30 751 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 752
Chris@44 753 float value = getValueForY(v, e->y());
Chris@30 754
Chris@30 755 if (!m_editingCommand) {
Chris@30 756 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 757 tr("Drag Point"));
Chris@30 758 }
Chris@30 759
Chris@30 760 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 761 m_editingPoint.frame = frame;
Chris@30 762 m_editingPoint.value = value;
Chris@30 763 m_editingCommand->addPoint(m_editingPoint);
Chris@30 764 }
Chris@30 765
Chris@30 766 void
Chris@44 767 NoteLayer::editEnd(View *v, QMouseEvent *e)
Chris@30 768 {
Chris@101 769 // std::cerr << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 770 if (!m_model || !m_editing) return;
Chris@30 771
Chris@30 772 if (m_editingCommand) {
Chris@30 773
Chris@30 774 QString newName = m_editingCommand->getName();
Chris@30 775
Chris@30 776 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@30 777 if (m_editingPoint.value != m_originalPoint.value) {
Chris@30 778 newName = tr("Edit Point");
Chris@30 779 } else {
Chris@30 780 newName = tr("Relocate Point");
Chris@30 781 }
Chris@30 782 } else {
Chris@30 783 newName = tr("Change Point Value");
Chris@30 784 }
Chris@30 785
Chris@30 786 m_editingCommand->setName(newName);
Chris@30 787 m_editingCommand->finish();
Chris@30 788 }
Chris@30 789
Chris@30 790 m_editingCommand = 0;
Chris@30 791 m_editing = false;
Chris@30 792 }
Chris@30 793
Chris@43 794 void
Chris@70 795 NoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 796 {
Chris@70 797 if (!m_model) return;
Chris@70 798
Chris@70 799 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@70 800 if (points.empty()) return;
Chris@70 801
Chris@70 802 NoteModel::Point note = *points.begin();
Chris@70 803
Chris@70 804 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 805 (m_model->getSampleRate(),
Chris@70 806 ItemEditDialog::ShowTime |
Chris@70 807 ItemEditDialog::ShowDuration |
Chris@70 808 ItemEditDialog::ShowValue |
Chris@100 809 ItemEditDialog::ShowText,
Chris@100 810 m_model->getScaleUnits());
Chris@70 811
Chris@70 812 dialog->setFrameTime(note.frame);
Chris@70 813 dialog->setValue(note.value);
Chris@70 814 dialog->setFrameDuration(note.duration);
Chris@70 815 dialog->setText(note.label);
Chris@70 816
Chris@70 817 if (dialog->exec() == QDialog::Accepted) {
Chris@70 818
Chris@70 819 NoteModel::Point newNote = note;
Chris@70 820 newNote.frame = dialog->getFrameTime();
Chris@70 821 newNote.value = dialog->getValue();
Chris@70 822 newNote.duration = dialog->getFrameDuration();
Chris@70 823 newNote.label = dialog->getText();
Chris@70 824
Chris@70 825 NoteModel::EditCommand *command = new NoteModel::EditCommand
Chris@70 826 (m_model, tr("Edit Point"));
Chris@70 827 command->deletePoint(note);
Chris@70 828 command->addPoint(newNote);
Chris@70 829 command->finish();
Chris@70 830 }
Chris@70 831
Chris@70 832 delete dialog;
Chris@70 833 }
Chris@70 834
Chris@70 835 void
Chris@43 836 NoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 837 {
Chris@99 838 if (!m_model) return;
Chris@99 839
Chris@43 840 NoteModel::EditCommand *command =
Chris@43 841 new NoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 842
Chris@43 843 NoteModel::PointList points =
Chris@43 844 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 845
Chris@43 846 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 847 i != points.end(); ++i) {
Chris@43 848
Chris@43 849 if (s.contains(i->frame)) {
Chris@43 850 NoteModel::Point newPoint(*i);
Chris@43 851 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 852 command->deletePoint(*i);
Chris@43 853 command->addPoint(newPoint);
Chris@43 854 }
Chris@43 855 }
Chris@43 856
Chris@43 857 command->finish();
Chris@43 858 }
Chris@43 859
Chris@43 860 void
Chris@43 861 NoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 862 {
Chris@99 863 if (!m_model) return;
Chris@99 864
Chris@43 865 NoteModel::EditCommand *command =
Chris@43 866 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 867
Chris@43 868 NoteModel::PointList points =
Chris@43 869 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 870
Chris@43 871 double ratio =
Chris@43 872 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 873 double(s.getEndFrame() - s.getStartFrame());
Chris@43 874
Chris@43 875 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 876 i != points.end(); ++i) {
Chris@43 877
Chris@43 878 if (s.contains(i->frame)) {
Chris@43 879
Chris@43 880 double targetStart = i->frame;
Chris@43 881 targetStart = newSize.getStartFrame() +
Chris@43 882 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 883
Chris@43 884 double targetEnd = i->frame + i->duration;
Chris@43 885 targetEnd = newSize.getStartFrame() +
Chris@43 886 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 887
Chris@43 888 NoteModel::Point newPoint(*i);
Chris@43 889 newPoint.frame = lrint(targetStart);
Chris@43 890 newPoint.duration = lrint(targetEnd - targetStart);
Chris@43 891 command->deletePoint(*i);
Chris@43 892 command->addPoint(newPoint);
Chris@43 893 }
Chris@43 894 }
Chris@43 895
Chris@43 896 command->finish();
Chris@43 897 }
Chris@43 898
Chris@76 899 void
Chris@76 900 NoteLayer::deleteSelection(Selection s)
Chris@76 901 {
Chris@99 902 if (!m_model) return;
Chris@99 903
Chris@76 904 NoteModel::EditCommand *command =
Chris@76 905 new NoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 906
Chris@76 907 NoteModel::PointList points =
Chris@76 908 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 909
Chris@76 910 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 911 i != points.end(); ++i) {
Chris@76 912
Chris@76 913 if (s.contains(i->frame)) {
Chris@76 914 command->deletePoint(*i);
Chris@76 915 }
Chris@76 916 }
Chris@76 917
Chris@76 918 command->finish();
Chris@76 919 }
Chris@76 920
Chris@76 921 void
Chris@76 922 NoteLayer::copy(Selection s, Clipboard &to)
Chris@76 923 {
Chris@99 924 if (!m_model) return;
Chris@99 925
Chris@76 926 NoteModel::PointList points =
Chris@76 927 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 928
Chris@76 929 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 930 i != points.end(); ++i) {
Chris@76 931 if (s.contains(i->frame)) {
Chris@83 932 Clipboard::Point point(i->frame, i->value, i->duration, i->label);
Chris@76 933 to.addPoint(point);
Chris@76 934 }
Chris@76 935 }
Chris@76 936 }
Chris@76 937
Chris@125 938 bool
Chris@125 939 NoteLayer::paste(const Clipboard &from, int frameOffset, bool interactive)
Chris@76 940 {
Chris@125 941 if (!m_model) return false;
Chris@99 942
Chris@76 943 const Clipboard::PointList &points = from.getPoints();
Chris@76 944
Chris@76 945 NoteModel::EditCommand *command =
Chris@76 946 new NoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 947
Chris@76 948 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 949 i != points.end(); ++i) {
Chris@76 950
Chris@76 951 if (!i->haveFrame()) continue;
Chris@76 952 size_t frame = 0;
Chris@76 953 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
Chris@76 954 frame = i->getFrame() + frameOffset;
Chris@76 955 }
Chris@76 956 NoteModel::Point newPoint(frame);
Chris@76 957
Chris@76 958 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 959 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 960 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 961 m_model->getValueMaximum()) / 2;
Chris@76 962 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 963 else {
Chris@125 964 size_t nextFrame = frame;
Chris@125 965 Clipboard::PointList::const_iterator j = i;
Chris@125 966 for (; j != points.end(); ++j) {
Chris@125 967 if (!j->haveFrame()) continue;
Chris@125 968 if (j != i) break;
Chris@125 969 }
Chris@125 970 if (j != points.end()) {
Chris@125 971 nextFrame = j->getFrame();
Chris@125 972 }
Chris@125 973 if (nextFrame == frame) {
Chris@125 974 newPoint.duration = m_model->getResolution();
Chris@125 975 } else {
Chris@125 976 newPoint.duration = nextFrame - frame;
Chris@125 977 }
Chris@125 978 }
Chris@76 979
Chris@76 980 command->addPoint(newPoint);
Chris@76 981 }
Chris@76 982
Chris@76 983 command->finish();
Chris@125 984 return true;
Chris@76 985 }
Chris@76 986
Chris@30 987 QString
Chris@30 988 NoteLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@30 989 {
Chris@30 990 return Layer::toXmlString(indent, extraAttributes +
Chris@30 991 QString(" colour=\"%1\" verticalScale=\"%2\"")
Chris@30 992 .arg(encodeColour(m_colour)).arg(m_verticalScale));
Chris@30 993 }
Chris@30 994
Chris@30 995 void
Chris@30 996 NoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 997 {
Chris@30 998 QString colourSpec = attributes.value("colour");
Chris@30 999 if (colourSpec != "") {
Chris@30 1000 QColor colour(colourSpec);
Chris@30 1001 if (colour.isValid()) {
Chris@30 1002 setBaseColour(QColor(colourSpec));
Chris@30 1003 }
Chris@30 1004 }
Chris@30 1005
Chris@30 1006 bool ok;
Chris@30 1007 VerticalScale scale = (VerticalScale)
Chris@30 1008 attributes.value("verticalScale").toInt(&ok);
Chris@30 1009 if (ok) setVerticalScale(scale);
Chris@30 1010 }
Chris@30 1011
Chris@30 1012