annotate layer/NoteLayer.cpp @ 87:4b98bda7e94d

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