annotate layer/NoteLayer.cpp @ 316:c0b9eec70639

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