annotate layer/FlexiNoteLayer.cpp @ 648:f61a54364d1d tonioni

slightly tidied george's splitEnd method
author matthiasm
date Sat, 15 Jun 2013 19:52:06 +0100
parents 87fe9e05e076
children 2ad082c5a090 12d570c27d85
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;
Chris@30 947
Chris@551 948 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
Chris@551 949 m_dragPointY = getYForValue(v, m_editingPoint.value);
Chris@551 950
Chris@30 951 if (m_editingCommand) {
gyorgyf@646 952 finish(m_editingCommand);
gyorgyf@646 953 m_editingCommand = 0;
Chris@30 954 }
Chris@30 955
Chris@30 956 m_editing = true;
Chris@551 957 m_dragStartX = e->x();
Chris@551 958 m_dragStartY = e->y();
Chris@30 959 }
Chris@30 960
Chris@30 961 void
matthiasm@620 962 FlexiNoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 963 {
matthiasm@620 964 // SVDEBUG << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 965 std::cerr << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 966
Chris@30 967 if (!m_model || !m_editing) return;
Chris@30 968
Chris@551 969 int xdist = e->x() - m_dragStartX;
Chris@551 970 int ydist = e->y() - m_dragStartY;
Chris@551 971 int newx = m_dragPointX + xdist;
Chris@551 972 int newy = m_dragPointY + ydist;
Chris@551 973
Chris@551 974 long frame = v->getFrameForX(newx);
Chris@30 975 if (frame < 0) frame = 0;
Chris@30 976 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 977
Chris@551 978 float value = getValueForY(v, newy);
Chris@30 979
Chris@30 980 if (!m_editingCommand) {
gyorgyf@646 981 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
gyorgyf@646 982 tr("Drag Point"));
Chris@30 983 }
Chris@30 984
Chris@30 985 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 986 m_editingPoint.frame = frame;
Chris@30 987 m_editingPoint.value = value;
Chris@30 988 m_editingCommand->addPoint(m_editingPoint);
Chris@30 989 }
Chris@30 990
Chris@30 991 void
gyorgyf@635 992 FlexiNoteLayer::editEnd(View *v, QMouseEvent *e)
Chris@30 993 {
matthiasm@620 994 // SVDEBUG << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 995 std::cerr << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
gyorgyf@635 996
Chris@30 997 if (!m_model || !m_editing) return;
Chris@30 998
Chris@30 999 if (m_editingCommand) {
Chris@30 1000
gyorgyf@646 1001 QString newName = m_editingCommand->getName();
Chris@30 1002
gyorgyf@646 1003 if (m_editingPoint.frame != m_originalPoint.frame) {
gyorgyf@646 1004 if (m_editingPoint.value != m_originalPoint.value) {
gyorgyf@646 1005 newName = tr("Edit Point");
gyorgyf@646 1006 } else {
gyorgyf@646 1007 newName = tr("Relocate Point");
gyorgyf@646 1008 }
gyorgyf@646 1009 } else {
gyorgyf@646 1010 newName = tr("Change Point Value");
gyorgyf@646 1011 }
Chris@30 1012
gyorgyf@646 1013 m_editingCommand->setName(newName);
gyorgyf@646 1014 finish(m_editingCommand);
Chris@30 1015 }
Chris@30 1016
Chris@30 1017 m_editingCommand = 0;
Chris@30 1018 m_editing = false;
Chris@30 1019 }
Chris@30 1020
gyorgyf@635 1021 void
gyorgyf@635 1022 FlexiNoteLayer::splitStart(View *v, QMouseEvent *e)
gyorgyf@635 1023 {
gyorgyf@646 1024 // GF: note splitting starts (!! remove printing soon)
gyorgyf@646 1025 std::cerr << "splitStart" << std::endl;
gyorgyf@646 1026 if (!m_model) return;
gyorgyf@635 1027
gyorgyf@635 1028 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
gyorgyf@635 1029 // m_originalPoint = m_editingPoint;
gyorgyf@635 1030 //
gyorgyf@635 1031 // m_dragPointX = v->getXForFrame(m_editingPoint.frame);
gyorgyf@635 1032 // m_dragPointY = getYForValue(v, m_editingPoint.value);
gyorgyf@635 1033
gyorgyf@635 1034 if (m_editingCommand) {
gyorgyf@646 1035 finish(m_editingCommand);
gyorgyf@646 1036 m_editingCommand = 0;
gyorgyf@635 1037 }
gyorgyf@635 1038
gyorgyf@635 1039 m_editing = true;
gyorgyf@635 1040 m_dragStartX = e->x();
gyorgyf@635 1041 m_dragStartY = e->y();
gyorgyf@635 1042
gyorgyf@635 1043 }
gyorgyf@635 1044
gyorgyf@635 1045 void
gyorgyf@635 1046 FlexiNoteLayer::splitEnd(View *v, QMouseEvent *e)
gyorgyf@635 1047 {
gyorgyf@646 1048 // GF: note splitting ends. (!! remove printing soon)
gyorgyf@646 1049 std::cerr << "splitEnd" << std::endl;
gyorgyf@646 1050 if (!m_model || !m_editing) return;
gyorgyf@635 1051
gyorgyf@635 1052 int xdist = e->x() - m_dragStartX;
gyorgyf@635 1053 int ydist = e->y() - m_dragStartY;
gyorgyf@635 1054 if (xdist != 0 || ydist != 0) {
gyorgyf@646 1055 std::cerr << "mouse moved" << std::endl;
gyorgyf@635 1056 return;
gyorgyf@635 1057 }
gyorgyf@635 1058
matthiasm@648 1059 // MM simpler declaration
matthiasm@648 1060 FlexiNote note(0);
gyorgyf@635 1061 if (!getPointToDrag(v, e->x(), e->y(), note)) return;
gyorgyf@635 1062
gyorgyf@635 1063 long frame = v->getFrameForX(e->x());
gyorgyf@635 1064
matthiasm@648 1065 int gap = 0; // MM: I prefer a gap of 0, but we can decide later
matthiasm@648 1066
matthiasm@648 1067 // MM: changed this a bit, to make it slightly clearer
matthiasm@648 1068 FlexiNote newNote1(note.frame, note.value,
matthiasm@648 1069 frame - note.frame - gap,
matthiasm@648 1070 note.level, note.label);
matthiasm@648 1071
matthiasm@648 1072 FlexiNote newNote2(frame, note.value,
matthiasm@648 1073 note.duration - newNote1.duration,
matthiasm@648 1074 note.level, note.label);
gyorgyf@635 1075
gyorgyf@635 1076 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
gyorgyf@635 1077 (m_model, tr("Edit Point"));
gyorgyf@635 1078 command->deletePoint(note);
gyorgyf@635 1079 command->addPoint(newNote1);
gyorgyf@635 1080 command->addPoint(newNote2);
gyorgyf@635 1081 finish(command);
gyorgyf@646 1082
gyorgyf@646 1083 }
gyorgyf@646 1084
gyorgyf@646 1085 void
gyorgyf@646 1086 FlexiNoteLayer::mouseMoveEvent(View *v, QMouseEvent *e)
gyorgyf@646 1087 {
gyorgyf@646 1088 // GF: context sensitive cursors
gyorgyf@646 1089 // v->setCursor(Qt::ArrowCursor);
gyorgyf@646 1090 FlexiNoteModel::Point note(0);
gyorgyf@646 1091 if (!getNoteToEdit(v, e->x(), e->y(), note)) {
gyorgyf@646 1092 // v->setCursor(Qt::UpArrowCursor);
gyorgyf@646 1093 return;
gyorgyf@646 1094 }
gyorgyf@646 1095
gyorgyf@646 1096 bool closeToLeft = false, closeToRight = false, closeToTop = false, closeToBottom = false;
gyorgyf@646 1097 getRelativeMousePosition(v, note, e->x(), e->y(), closeToLeft, closeToRight, closeToTop, closeToBottom);
gyorgyf@646 1098 // if (!closeToLeft) return;
gyorgyf@646 1099 if (closeToLeft || closeToRight) { v->setCursor(Qt::SizeHorCursor); return; }
gyorgyf@646 1100 // if (closeToTop) v->setCursor(Qt::SizeVerCursor);
gyorgyf@646 1101 if (closeToTop) { v->setCursor(Qt::CrossCursor); return; }
gyorgyf@646 1102 if (closeToBottom) { v->setCursor(Qt::UpArrowCursor); return; }
gyorgyf@646 1103 v->setCursor(Qt::ArrowCursor);
gyorgyf@646 1104
gyorgyf@646 1105
gyorgyf@646 1106 std::cerr << "Mouse moved in edit mode over FlexiNoteLayer" << std::endl;
gyorgyf@646 1107 // v->setCursor(Qt::SizeHorCursor);
gyorgyf@646 1108
gyorgyf@646 1109 }
gyorgyf@646 1110
gyorgyf@646 1111 void
gyorgyf@646 1112 FlexiNoteLayer::getRelativeMousePosition(View *v, FlexiNoteModel::Point &note, int x, int y, bool &closeToLeft, bool &closeToRight, bool &closeToTop, bool &closeToBottom) const
gyorgyf@646 1113 {
gyorgyf@646 1114 if (!m_model) return;
gyorgyf@646 1115
gyorgyf@646 1116 int ctol = 2;
gyorgyf@646 1117 int noteStartX = v->getXForFrame(note.frame);
gyorgyf@646 1118 int noteEndX = v->getXForFrame(note.frame + note.duration);
gyorgyf@646 1119 int noteValueY = getYForValue(v,note.value);
gyorgyf@646 1120 int noteStartY = noteValueY - (NOTE_HEIGHT / 2);
gyorgyf@646 1121 int noteEndY = noteValueY + (NOTE_HEIGHT / 2);
gyorgyf@646 1122
gyorgyf@646 1123 bool closeToNote = false;
gyorgyf@646 1124
gyorgyf@646 1125 if (y >= noteStartY-ctol && y <= noteEndY+ctol && x >= noteStartX-ctol && x <= noteEndX+ctol) closeToNote = true;
gyorgyf@646 1126 if (!closeToNote) return;
gyorgyf@646 1127
gyorgyf@646 1128 int tol = 4;
gyorgyf@646 1129
gyorgyf@646 1130 if (x >= noteStartX - tol && x <= noteStartX + tol) closeToLeft = true;
gyorgyf@646 1131 if (x >= noteEndX - tol && x <= noteEndX + tol) closeToRight = true;
gyorgyf@646 1132 if (y >= noteStartY - tol && y <= noteStartY + tol) closeToTop = true;
gyorgyf@646 1133 if (y >= noteEndY - tol && y <= noteEndY + tol) closeToBottom = true;
gyorgyf@646 1134
gyorgyf@635 1135 }
gyorgyf@635 1136
gyorgyf@635 1137
Chris@255 1138 bool
matthiasm@620 1139 FlexiNoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1140 {
gyorgyf@633 1141 std::cerr << "Opening note editor dialog" << std::endl;
Chris@255 1142 if (!m_model) return false;
Chris@70 1143
matthiasm@620 1144 FlexiNoteModel::Point note(0);
Chris@550 1145 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 1146
matthiasm@620 1147 // FlexiNoteModel::Point note = *points.begin();
Chris@70 1148
Chris@70 1149 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1150 (m_model->getSampleRate(),
Chris@70 1151 ItemEditDialog::ShowTime |
Chris@70 1152 ItemEditDialog::ShowDuration |
Chris@70 1153 ItemEditDialog::ShowValue |
Chris@100 1154 ItemEditDialog::ShowText,
Chris@100 1155 m_model->getScaleUnits());
Chris@70 1156
Chris@70 1157 dialog->setFrameTime(note.frame);
Chris@70 1158 dialog->setValue(note.value);
Chris@70 1159 dialog->setFrameDuration(note.duration);
Chris@70 1160 dialog->setText(note.label);
Chris@70 1161
Chris@70 1162 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1163
matthiasm@620 1164 FlexiNoteModel::Point newNote = note;
Chris@70 1165 newNote.frame = dialog->getFrameTime();
Chris@70 1166 newNote.value = dialog->getValue();
Chris@70 1167 newNote.duration = dialog->getFrameDuration();
Chris@70 1168 newNote.label = dialog->getText();
Chris@70 1169
matthiasm@620 1170 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
Chris@70 1171 (m_model, tr("Edit Point"));
Chris@70 1172 command->deletePoint(note);
Chris@70 1173 command->addPoint(newNote);
Chris@376 1174 finish(command);
Chris@70 1175 }
Chris@70 1176
Chris@70 1177 delete dialog;
Chris@255 1178 return true;
Chris@70 1179 }
Chris@70 1180
Chris@70 1181 void
matthiasm@620 1182 FlexiNoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1183 {
Chris@99 1184 if (!m_model) return;
Chris@99 1185
matthiasm@620 1186 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1187 new FlexiNoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 1188
matthiasm@620 1189 FlexiNoteModel::PointList points =
gyorgyf@646 1190 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1191
matthiasm@620 1192 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1193 i != points.end(); ++i) {
Chris@43 1194
gyorgyf@646 1195 if (s.contains(i->frame)) {
gyorgyf@646 1196 FlexiNoteModel::Point newPoint(*i);
gyorgyf@646 1197 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
gyorgyf@646 1198 command->deletePoint(*i);
gyorgyf@646 1199 command->addPoint(newPoint);
gyorgyf@646 1200 }
Chris@43 1201 }
Chris@43 1202
Chris@376 1203 finish(command);
Chris@43 1204 }
Chris@43 1205
Chris@43 1206 void
matthiasm@620 1207 FlexiNoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1208 {
Chris@99 1209 if (!m_model) return;
Chris@99 1210
matthiasm@620 1211 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1212 new FlexiNoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1213
matthiasm@620 1214 FlexiNoteModel::PointList points =
gyorgyf@646 1215 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1216
Chris@43 1217 double ratio =
gyorgyf@646 1218 double(newSize.getEndFrame() - newSize.getStartFrame()) /
gyorgyf@646 1219 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1220
matthiasm@620 1221 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1222 i != points.end(); ++i) {
Chris@43 1223
gyorgyf@646 1224 if (s.contains(i->frame)) {
Chris@43 1225
gyorgyf@646 1226 double targetStart = i->frame;
gyorgyf@646 1227 targetStart = newSize.getStartFrame() +
gyorgyf@646 1228 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 1229
gyorgyf@646 1230 double targetEnd = i->frame + i->duration;
gyorgyf@646 1231 targetEnd = newSize.getStartFrame() +
gyorgyf@646 1232 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 1233
gyorgyf@646 1234 FlexiNoteModel::Point newPoint(*i);
gyorgyf@646 1235 newPoint.frame = lrint(targetStart);
gyorgyf@646 1236 newPoint.duration = lrint(targetEnd - targetStart);
gyorgyf@646 1237 command->deletePoint(*i);
gyorgyf@646 1238 command->addPoint(newPoint);
gyorgyf@646 1239 }
Chris@43 1240 }
Chris@43 1241
Chris@376 1242 finish(command);
Chris@43 1243 }
Chris@43 1244
Chris@76 1245 void
matthiasm@620 1246 FlexiNoteLayer::deleteSelection(Selection s)
Chris@76 1247 {
Chris@99 1248 if (!m_model) return;
Chris@99 1249
matthiasm@620 1250 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1251 new FlexiNoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1252
matthiasm@620 1253 FlexiNoteModel::PointList points =
gyorgyf@646 1254 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1255
matthiasm@620 1256 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1257 i != points.end(); ++i) {
Chris@76 1258
Chris@76 1259 if (s.contains(i->frame)) {
Chris@76 1260 command->deletePoint(*i);
Chris@76 1261 }
Chris@76 1262 }
Chris@76 1263
Chris@376 1264 finish(command);
Chris@76 1265 }
Chris@76 1266
Chris@76 1267 void
matthiasm@620 1268 FlexiNoteLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1269 {
Chris@99 1270 if (!m_model) return;
Chris@99 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) {
gyorgyf@646 1277 if (s.contains(i->frame)) {
Chris@335 1278 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
Chris@360 1279 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1280 to.addPoint(point);
Chris@76 1281 }
Chris@76 1282 }
Chris@76 1283 }
Chris@76 1284
Chris@125 1285 bool
matthiasm@620 1286 FlexiNoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
Chris@76 1287 {
Chris@125 1288 if (!m_model) return false;
Chris@99 1289
Chris@76 1290 const Clipboard::PointList &points = from.getPoints();
Chris@76 1291
Chris@360 1292 bool realign = false;
Chris@360 1293
Chris@360 1294 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1295
Chris@360 1296 QMessageBox::StandardButton button =
Chris@360 1297 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1298 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 1299 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1300 QMessageBox::Yes);
Chris@360 1301
Chris@360 1302 if (button == QMessageBox::Cancel) {
Chris@360 1303 return false;
Chris@360 1304 }
Chris@360 1305
Chris@360 1306 if (button == QMessageBox::Yes) {
Chris@360 1307 realign = true;
Chris@360 1308 }
Chris@360 1309 }
Chris@360 1310
matthiasm@620 1311 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1312 new FlexiNoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1313
Chris@76 1314 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1315 i != points.end(); ++i) {
Chris@76 1316
Chris@76 1317 if (!i->haveFrame()) continue;
Chris@76 1318 size_t frame = 0;
Chris@360 1319
Chris@360 1320 if (!realign) {
Chris@360 1321
Chris@360 1322 frame = i->getFrame();
Chris@360 1323
Chris@360 1324 } else {
Chris@360 1325
Chris@360 1326 if (i->haveReferenceFrame()) {
Chris@360 1327 frame = i->getReferenceFrame();
Chris@360 1328 frame = alignFromReference(v, frame);
Chris@360 1329 } else {
Chris@360 1330 frame = i->getFrame();
Chris@360 1331 }
Chris@76 1332 }
Chris@360 1333
matthiasm@620 1334 FlexiNoteModel::Point newPoint(frame);
Chris@76 1335
Chris@76 1336 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1337 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1338 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1339 m_model->getValueMaximum()) / 2;
Chris@335 1340 if (i->haveLevel()) newPoint.level = i->getLevel();
Chris@76 1341 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 1342 else {
Chris@125 1343 size_t nextFrame = frame;
Chris@125 1344 Clipboard::PointList::const_iterator j = i;
Chris@125 1345 for (; j != points.end(); ++j) {
Chris@125 1346 if (!j->haveFrame()) continue;
Chris@125 1347 if (j != i) break;
Chris@125 1348 }
Chris@125 1349 if (j != points.end()) {
Chris@125 1350 nextFrame = j->getFrame();
Chris@125 1351 }
Chris@125 1352 if (nextFrame == frame) {
Chris@125 1353 newPoint.duration = m_model->getResolution();
Chris@125 1354 } else {
Chris@125 1355 newPoint.duration = nextFrame - frame;
Chris@125 1356 }
Chris@125 1357 }
Chris@76 1358
Chris@76 1359 command->addPoint(newPoint);
Chris@76 1360 }
Chris@76 1361
Chris@376 1362 finish(command);
Chris@125 1363 return true;
Chris@76 1364 }
Chris@76 1365
Chris@507 1366 void
matthiasm@620 1367 FlexiNoteLayer::addNoteOn(long frame, int pitch, int velocity)
Chris@507 1368 {
matthiasm@620 1369 m_pendingNoteOns.insert(FlexiNote(frame, pitch, 0, float(velocity) / 127.0, ""));
Chris@507 1370 }
Chris@507 1371
Chris@507 1372 void
matthiasm@620 1373 FlexiNoteLayer::addNoteOff(long frame, int pitch)
Chris@507 1374 {
matthiasm@620 1375 for (FlexiNoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1376 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1377 if (lrintf((*i).value) == pitch) {
matthiasm@620 1378 FlexiNote note(*i);
Chris@507 1379 m_pendingNoteOns.erase(i);
Chris@507 1380 note.duration = frame - note.frame;
Chris@507 1381 if (m_model) {
matthiasm@620 1382 FlexiNoteModel::AddPointCommand *c = new FlexiNoteModel::AddPointCommand
matthiasm@620 1383 (m_model, note, tr("Record FlexiNote"));
Chris@507 1384 // execute and bundle:
Chris@507 1385 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1386 }
Chris@507 1387 break;
Chris@507 1388 }
Chris@507 1389 }
Chris@507 1390 }
Chris@507 1391
Chris@507 1392 void
matthiasm@620 1393 FlexiNoteLayer::abandonNoteOns()
Chris@507 1394 {
Chris@507 1395 m_pendingNoteOns.clear();
Chris@507 1396 }
Chris@507 1397
Chris@287 1398 int
matthiasm@620 1399 FlexiNoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1400 {
Chris@287 1401 impose = false;
Chris@287 1402 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1403 (QString(darkbg ? "White" : "Black"));
Chris@287 1404 }
Chris@287 1405
Chris@316 1406 void
matthiasm@620 1407 FlexiNoteLayer::toXml(QTextStream &stream,
Chris@316 1408 QString indent, QString extraAttributes) const
Chris@30 1409 {
Chris@316 1410 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1411 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1412 .arg(m_verticalScale)
Chris@445 1413 .arg(m_scaleMinimum)
Chris@445 1414 .arg(m_scaleMaximum));
Chris@30 1415 }
Chris@30 1416
Chris@30 1417 void
matthiasm@620 1418 FlexiNoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1419 {
Chris@287 1420 SingleColourLayer::setProperties(attributes);
Chris@30 1421
Chris@445 1422 bool ok, alsoOk;
Chris@30 1423 VerticalScale scale = (VerticalScale)
gyorgyf@646 1424 attributes.value("verticalScale").toInt(&ok);
Chris@30 1425 if (ok) setVerticalScale(scale);
Chris@445 1426
Chris@445 1427 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1428 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@445 1429 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@30 1430 }
Chris@30 1431
Chris@30 1432