annotate layer/FlexiNoteLayer.cpp @ 659:65b966394650 tonioni

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