annotate layer/FlexiNoteLayer.cpp @ 649:2ad082c5a090 tonioni

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