annotate layer/FlexiNoteLayer.cpp @ 694:ad12e428785b tonioni

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