annotate layer/FlexiNoteLayer.cpp @ 696:e77b1673e17e tonioni

Pull out log and linear vertical scales into their own classes, make some improvements to log numbering
author Chris Cannam
date Wed, 04 Dec 2013 11:35:08 +0000
parents 6d9624e0ac55
children ceb9a2992d96
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()) {
Chris@695 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@695 861 FlexiNoteLayer::getVerticalScaleWidth(View *, bool, QPainter &paint) const
Chris@692 862 {
Chris@696 863 if (m_verticalScale == LinearScale ||
Chris@696 864 m_verticalScale == AutoAlignScale) {
Chris@696 865 return 0;
Chris@696 866 } else {
Chris@696 867 return 10;
Chris@696 868 }
Chris@692 869 }
Chris@692 870
Chris@692 871 void
Chris@695 872 FlexiNoteLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
Chris@692 873 {
Chris@696 874 if (m_verticalScale == LinearScale ||
Chris@696 875 m_verticalScale == AutoAlignScale) {
Chris@696 876 return;
Chris@696 877 } else {
Chris@696 878 float fmin, fmax;
Chris@696 879 bool log;
Chris@696 880 QString unit;
Chris@696 881 if (!getValueExtents(fmin, fmax, log, unit)) return;
Chris@696 882 PianoScale().paintPianoVertical
Chris@696 883 (v, paint, QRect(0, 0, 10, v->height()), fmin, fmax);
Chris@696 884 paint.drawLine(10, 0, 10, v->height());
Chris@696 885 }
Chris@692 886 }
Chris@692 887
Chris@30 888 void
matthiasm@620 889 FlexiNoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 890 {
matthiasm@620 891 // SVDEBUG << "FlexiNoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 892
Chris@30 893 if (!m_model) return;
Chris@30 894
Chris@44 895 long frame = v->getFrameForX(e->x());
Chris@30 896 if (frame < 0) frame = 0;
Chris@30 897 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 898
Chris@44 899 float value = getValueForY(v, e->y());
Chris@30 900
matthiasm@620 901 m_editingPoint = FlexiNoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
Chris@30 902 m_originalPoint = m_editingPoint;
Chris@30 903
Chris@376 904 if (m_editingCommand) finish(m_editingCommand);
matthiasm@620 905 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
gyorgyf@646 906 tr("Draw Point"));
Chris@30 907 m_editingCommand->addPoint(m_editingPoint);
Chris@30 908
Chris@30 909 m_editing = true;
Chris@30 910 }
Chris@30 911
Chris@30 912 void
matthiasm@620 913 FlexiNoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 914 {
matthiasm@620 915 // SVDEBUG << "FlexiNoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 916
Chris@30 917 if (!m_model || !m_editing) return;
Chris@30 918
Chris@44 919 long frame = v->getFrameForX(e->x());
Chris@30 920 if (frame < 0) frame = 0;
Chris@30 921 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 922
Chris@101 923 float newValue = getValueForY(v, e->y());
Chris@101 924
Chris@101 925 long newFrame = m_editingPoint.frame;
Chris@101 926 long newDuration = frame - newFrame;
Chris@101 927 if (newDuration < 0) {
Chris@101 928 newFrame = frame;
Chris@101 929 newDuration = -newDuration;
Chris@101 930 } else if (newDuration == 0) {
Chris@101 931 newDuration = 1;
Chris@101 932 }
Chris@30 933
Chris@30 934 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 935 m_editingPoint.frame = newFrame;
Chris@101 936 m_editingPoint.value = newValue;
Chris@101 937 m_editingPoint.duration = newDuration;
Chris@30 938 m_editingCommand->addPoint(m_editingPoint);
Chris@30 939 }
Chris@30 940
Chris@30 941 void
matthiasm@620 942 FlexiNoteLayer::drawEnd(View *, QMouseEvent *)
Chris@30 943 {
matthiasm@620 944 // SVDEBUG << "FlexiNoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@30 945 if (!m_model || !m_editing) return;
Chris@376 946 finish(m_editingCommand);
Chris@30 947 m_editingCommand = 0;
Chris@30 948 m_editing = false;
Chris@30 949 }
Chris@30 950
Chris@30 951 void
matthiasm@620 952 FlexiNoteLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 953 {
Chris@335 954 if (!m_model) return;
Chris@335 955
Chris@550 956 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 957
Chris@335 958 if (m_editingCommand) {
gyorgyf@646 959 finish(m_editingCommand);
gyorgyf@646 960 m_editingCommand = 0;
Chris@335 961 }
Chris@335 962
Chris@335 963 m_editing = true;
Chris@335 964 }
Chris@335 965
Chris@335 966 void
matthiasm@620 967 FlexiNoteLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 968 {
Chris@335 969 }
Chris@335 970
Chris@335 971 void
matthiasm@620 972 FlexiNoteLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 973 {
Chris@335 974 if (!m_model || !m_editing) return;
Chris@335 975
Chris@335 976 m_editing = false;
Chris@335 977
matthiasm@620 978 FlexiNoteModel::Point p(0);
Chris@550 979 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@550 980 if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return;
Chris@550 981
matthiasm@620 982 m_editingCommand = new FlexiNoteModel::EditCommand(m_model, tr("Erase Point"));
Chris@335 983
Chris@335 984 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 985
Chris@376 986 finish(m_editingCommand);
Chris@335 987 m_editingCommand = 0;
Chris@335 988 m_editing = false;
Chris@335 989 }
Chris@335 990
Chris@335 991 void
matthiasm@620 992 FlexiNoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 993 {
matthiasm@620 994 // SVDEBUG << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 995 std::cerr << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 996
Chris@30 997 if (!m_model) return;
Chris@30 998
Chris@550 999 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
matthiasm@651 1000 m_originalPoint = FlexiNote(m_editingPoint);
gyorgyf@649 1001
matthiasm@651 1002 if (m_editMode == RightBoundary) {
matthiasm@651 1003 m_dragPointX = v->getXForFrame(m_editingPoint.frame + m_editingPoint.duration);
gyorgyf@649 1004 } else {
gyorgyf@649 1005 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
gyorgyf@649 1006 }
Chris@551 1007 m_dragPointY = getYForValue(v, m_editingPoint.value);
Chris@551 1008
Chris@30 1009 if (m_editingCommand) {
gyorgyf@646 1010 finish(m_editingCommand);
gyorgyf@646 1011 m_editingCommand = 0;
Chris@30 1012 }
Chris@30 1013
Chris@30 1014 m_editing = true;
Chris@551 1015 m_dragStartX = e->x();
Chris@551 1016 m_dragStartY = e->y();
matthiasm@651 1017
matthiasm@651 1018 long onset = m_originalPoint.frame;
matthiasm@651 1019 long offset = m_originalPoint.frame + m_originalPoint.duration - 1;
matthiasm@651 1020
matthiasm@651 1021 m_greatestLeftNeighbourFrame = -1;
matthiasm@651 1022 m_smallestRightNeighbourFrame = std::numeric_limits<long>::max();
matthiasm@651 1023
matthiasm@651 1024 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
matthiasm@651 1025 i != m_model->getPoints().end(); ++i) {
matthiasm@651 1026 FlexiNote currentNote = *i;
matthiasm@651 1027
matthiasm@651 1028 // left boundary
matthiasm@651 1029 if (currentNote.frame + currentNote.duration - 1 < onset) {
matthiasm@651 1030 m_greatestLeftNeighbourFrame = currentNote.frame + currentNote.duration - 1;
matthiasm@651 1031 }
matthiasm@651 1032
matthiasm@651 1033 // right boundary
matthiasm@651 1034 if (currentNote.frame > offset) {
matthiasm@651 1035 m_smallestRightNeighbourFrame = currentNote.frame;
matthiasm@651 1036 break;
matthiasm@651 1037 }
matthiasm@651 1038 }
matthiasm@651 1039 std::cerr << "note frame: " << onset << ", left boundary: " << m_greatestLeftNeighbourFrame << ", right boundary: " << m_smallestRightNeighbourFrame << std::endl;
Chris@30 1040 }
Chris@30 1041
Chris@30 1042 void
matthiasm@620 1043 FlexiNoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 1044 {
matthiasm@620 1045 // SVDEBUG << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 1046 std::cerr << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 1047
Chris@30 1048 if (!m_model || !m_editing) return;
Chris@30 1049
Chris@551 1050 int xdist = e->x() - m_dragStartX;
Chris@551 1051 int ydist = e->y() - m_dragStartY;
Chris@551 1052 int newx = m_dragPointX + xdist;
Chris@551 1053 int newy = m_dragPointY + ydist;
Chris@551 1054
matthiasm@657 1055 long dragFrame = v->getFrameForX(newx);
matthiasm@657 1056 if (dragFrame < 0) dragFrame = 0;
matthiasm@657 1057 dragFrame = dragFrame / m_model->getResolution() * m_model->getResolution();
matthiasm@651 1058
Chris@551 1059 float value = getValueForY(v, newy);
Chris@30 1060
Chris@30 1061 if (!m_editingCommand) {
gyorgyf@646 1062 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
gyorgyf@646 1063 tr("Drag Point"));
Chris@30 1064 }
Chris@30 1065
Chris@30 1066 m_editingCommand->deletePoint(m_editingPoint);
matthiasm@651 1067
matthiasm@651 1068 std::cerr << "edit mode: " << m_editMode << std::endl;
matthiasm@657 1069
matthiasm@651 1070 switch (m_editMode) {
matthiasm@651 1071 case LeftBoundary : {
gyorgyf@658 1072 // left
gyorgyf@658 1073 if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1;
matthiasm@657 1074 // right
gyorgyf@658 1075 if (m_intelligentActions && dragFrame >= m_originalPoint.frame + m_originalPoint.duration) {
matthiasm@657 1076 dragFrame = m_originalPoint.frame + m_originalPoint.duration - 1;
matthiasm@651 1077 }
matthiasm@657 1078 m_editingPoint.frame = dragFrame;
matthiasm@657 1079 m_editingPoint.duration = m_originalPoint.frame - dragFrame + m_originalPoint.duration;
matthiasm@651 1080 break;
matthiasm@651 1081 }
matthiasm@651 1082 case RightBoundary : {
matthiasm@657 1083 // left
gyorgyf@658 1084 if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1;
gyorgyf@658 1085 if (m_intelligentActions && dragFrame >= m_smallestRightNeighbourFrame) dragFrame = m_smallestRightNeighbourFrame - 1;
matthiasm@657 1086 m_editingPoint.duration = dragFrame - m_originalPoint.frame + 1;
matthiasm@651 1087 break;
matthiasm@651 1088 }
matthiasm@651 1089 case DragNote : {
matthiasm@657 1090 // left
gyorgyf@658 1091 if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1;
matthiasm@657 1092 // right
gyorgyf@658 1093 if (m_intelligentActions && dragFrame + m_originalPoint.duration >= m_smallestRightNeighbourFrame) {
matthiasm@657 1094 dragFrame = m_smallestRightNeighbourFrame - m_originalPoint.duration;
matthiasm@651 1095 }
matthiasm@657 1096 m_editingPoint.frame = dragFrame;
matthiasm@651 1097 m_editingPoint.value = value;
matthiasm@651 1098 break;
matthiasm@651 1099 }
gyorgyf@649 1100 }
Chris@30 1101 m_editingCommand->addPoint(m_editingPoint);
gyorgyf@649 1102 std::cerr << "added new point(" << m_editingPoint.frame << "," << m_editingPoint.duration << ")" << std::endl;
gyorgyf@649 1103
Chris@30 1104 }
Chris@30 1105
Chris@30 1106 void
gyorgyf@635 1107 FlexiNoteLayer::editEnd(View *v, QMouseEvent *e)
Chris@30 1108 {
matthiasm@620 1109 // SVDEBUG << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
gyorgyf@635 1110 std::cerr << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
matthiasm@656 1111
Chris@30 1112 if (!m_model || !m_editing) return;
Chris@30 1113
Chris@30 1114 if (m_editingCommand) {
Chris@30 1115
gyorgyf@646 1116 QString newName = m_editingCommand->getName();
Chris@30 1117
gyorgyf@646 1118 if (m_editingPoint.frame != m_originalPoint.frame) {
gyorgyf@646 1119 if (m_editingPoint.value != m_originalPoint.value) {
gyorgyf@646 1120 newName = tr("Edit Point");
gyorgyf@646 1121 } else {
gyorgyf@646 1122 newName = tr("Relocate Point");
gyorgyf@646 1123 }
gyorgyf@646 1124 } else {
gyorgyf@646 1125 newName = tr("Change Point Value");
gyorgyf@646 1126 }
Chris@30 1127
gyorgyf@646 1128 m_editingCommand->setName(newName);
gyorgyf@646 1129 finish(m_editingCommand);
Chris@30 1130 }
Chris@30 1131
Chris@30 1132 m_editingCommand = 0;
Chris@30 1133 m_editing = false;
Chris@30 1134 }
Chris@30 1135
gyorgyf@635 1136 void
gyorgyf@635 1137 FlexiNoteLayer::splitStart(View *v, QMouseEvent *e)
gyorgyf@635 1138 {
gyorgyf@646 1139 // GF: note splitting starts (!! remove printing soon)
gyorgyf@646 1140 std::cerr << "splitStart" << std::endl;
gyorgyf@646 1141 if (!m_model) return;
gyorgyf@635 1142
gyorgyf@635 1143 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
gyorgyf@635 1144 // m_originalPoint = m_editingPoint;
gyorgyf@635 1145 //
gyorgyf@635 1146 // m_dragPointX = v->getXForFrame(m_editingPoint.frame);
gyorgyf@635 1147 // m_dragPointY = getYForValue(v, m_editingPoint.value);
gyorgyf@635 1148
gyorgyf@635 1149 if (m_editingCommand) {
gyorgyf@646 1150 finish(m_editingCommand);
gyorgyf@646 1151 m_editingCommand = 0;
gyorgyf@635 1152 }
gyorgyf@635 1153
gyorgyf@635 1154 m_editing = true;
gyorgyf@635 1155 m_dragStartX = e->x();
gyorgyf@635 1156 m_dragStartY = e->y();
gyorgyf@635 1157
gyorgyf@635 1158 }
gyorgyf@635 1159
gyorgyf@635 1160 void
gyorgyf@635 1161 FlexiNoteLayer::splitEnd(View *v, QMouseEvent *e)
gyorgyf@635 1162 {
gyorgyf@646 1163 // GF: note splitting ends. (!! remove printing soon)
gyorgyf@646 1164 std::cerr << "splitEnd" << std::endl;
matthiasm@651 1165 if (!m_model || !m_editing || m_editMode != SplitNote) return;
gyorgyf@635 1166
gyorgyf@635 1167 int xdist = e->x() - m_dragStartX;
gyorgyf@635 1168 int ydist = e->y() - m_dragStartY;
gyorgyf@635 1169 if (xdist != 0 || ydist != 0) {
gyorgyf@646 1170 std::cerr << "mouse moved" << std::endl;
gyorgyf@635 1171 return;
gyorgyf@635 1172 }
gyorgyf@635 1173
gyorgyf@649 1174 // MM: simpler declaration
matthiasm@648 1175 FlexiNote note(0);
gyorgyf@635 1176 if (!getPointToDrag(v, e->x(), e->y(), note)) return;
gyorgyf@635 1177
gyorgyf@635 1178 long frame = v->getFrameForX(e->x());
gyorgyf@635 1179
matthiasm@648 1180 int gap = 0; // MM: I prefer a gap of 0, but we can decide later
matthiasm@648 1181
gyorgyf@649 1182 // MM: changed this a bit, to make it slightly clearer (// GF: nice changes!)
matthiasm@648 1183 FlexiNote newNote1(note.frame, note.value,
matthiasm@648 1184 frame - note.frame - gap,
matthiasm@648 1185 note.level, note.label);
matthiasm@648 1186
matthiasm@648 1187 FlexiNote newNote2(frame, note.value,
matthiasm@648 1188 note.duration - newNote1.duration,
matthiasm@648 1189 note.level, note.label);
gyorgyf@658 1190
gyorgyf@658 1191 if (m_intelligentActions) {
gyorgyf@658 1192 updateNoteValue(v,newNote1);
gyorgyf@658 1193 updateNoteValue(v,newNote2);
gyorgyf@658 1194 }
gyorgyf@655 1195
gyorgyf@635 1196 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
gyorgyf@635 1197 (m_model, tr("Edit Point"));
gyorgyf@635 1198 command->deletePoint(note);
gyorgyf@649 1199 if ((e->modifiers() & Qt::ShiftModifier)) {
gyorgyf@649 1200 finish(command);
gyorgyf@649 1201 return;
gyorgyf@649 1202 }
gyorgyf@635 1203 command->addPoint(newNote1);
gyorgyf@635 1204 command->addPoint(newNote2);
gyorgyf@635 1205 finish(command);
gyorgyf@646 1206
gyorgyf@646 1207 }
gyorgyf@646 1208
gyorgyf@655 1209 void
matthiasm@660 1210 FlexiNoteLayer::addNote(View *v, QMouseEvent *e)
matthiasm@660 1211 {
matthiasm@660 1212 std::cerr << "addNote" << std::endl;
matthiasm@660 1213 if (!m_model) return;
matthiasm@660 1214
matthiasm@660 1215 long duration = 10000;
matthiasm@660 1216
matthiasm@660 1217 long frame = v->getFrameForX(e->x());
matthiasm@660 1218 float value = getValueForY(v, e->y());
matthiasm@660 1219
matthiasm@660 1220 if (m_intelligentActions) {
matthiasm@660 1221 long smallestRightNeighbourFrame = 0;
matthiasm@660 1222 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
matthiasm@660 1223 i != m_model->getPoints().end(); ++i) {
matthiasm@660 1224 FlexiNote currentNote = *i;
matthiasm@660 1225 if (currentNote.frame > frame) {
matthiasm@660 1226 smallestRightNeighbourFrame = currentNote.frame;
matthiasm@660 1227 break;
matthiasm@660 1228 }
matthiasm@660 1229 }
matthiasm@660 1230
matthiasm@660 1231 duration = std::min(smallestRightNeighbourFrame - frame + 1, duration);
matthiasm@660 1232 duration = (duration > 0) ? duration : 0;
matthiasm@660 1233 }
matthiasm@660 1234
matthiasm@660 1235 if (!m_intelligentActions ||
matthiasm@660 1236 m_model->getPoints(frame).empty() && duration > 0)
matthiasm@660 1237 {
matthiasm@660 1238 FlexiNote newNote(frame, value, duration, 100, "new note");
matthiasm@660 1239 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
matthiasm@660 1240 (m_model, tr("Add Point"));
matthiasm@660 1241 command->addPoint(newNote);
matthiasm@660 1242 finish(command);
matthiasm@660 1243 }
matthiasm@660 1244 }
matthiasm@660 1245
matthiasm@660 1246
matthiasm@660 1247 void
gyorgyf@655 1248 FlexiNoteLayer::updateNoteValue(View *v, FlexiNoteModel::Point &note) const
gyorgyf@655 1249 {
gyorgyf@655 1250 //GF: update the note value conforming the median of pitch values in the underlying note layer
gyorgyf@655 1251 Layer *layer = v->getLayer(1); // GF: !!! gross assumption about correct layer order
gyorgyf@655 1252 SparseTimeValueModel *model = 0;
gyorgyf@655 1253 if (layer && layer->getModel())
gyorgyf@655 1254 model = dynamic_cast<SparseTimeValueModel *>(layer->getModel());
gyorgyf@655 1255
gyorgyf@655 1256 if (!model) return;
gyorgyf@655 1257
gyorgyf@655 1258 std::cerr << model->getTypeName() << std::endl;
gyorgyf@655 1259
gyorgyf@655 1260 SparseModel<TimeValuePoint>::PointList dataPoints = model->getPoints(note.frame, note.frame + note.duration);
gyorgyf@655 1261 if (dataPoints.empty()) return;
gyorgyf@655 1262
gyorgyf@655 1263 // std::cerr << "frame " << note.frame << ": " << dataPoints.size() << " candidate points" << std::endl;
gyorgyf@655 1264
gyorgyf@655 1265 std::vector<float> pitchValues;
gyorgyf@655 1266
gyorgyf@655 1267 for (SparseModel<TimeValuePoint>::PointList::const_iterator i = dataPoints.begin();
gyorgyf@655 1268 i != dataPoints.end(); ++i) {
gyorgyf@655 1269 pitchValues.push_back((*i).value);
gyorgyf@655 1270 }
gyorgyf@655 1271 sort(pitchValues.begin(), pitchValues.end());
gyorgyf@655 1272 size_t size = pitchValues.size();
gyorgyf@655 1273 double median;
gyorgyf@655 1274
gyorgyf@655 1275 if (size % 2 == 0) {
gyorgyf@655 1276 median = (pitchValues[size/2 - 1] + pitchValues[size/2]) / 2;
gyorgyf@655 1277 } else {
gyorgyf@655 1278 median = pitchValues[size/2];
gyorgyf@655 1279 }
gyorgyf@655 1280
gyorgyf@655 1281 note.value = median;
gyorgyf@655 1282 }
gyorgyf@655 1283
gyorgyf@646 1284 void
gyorgyf@646 1285 FlexiNoteLayer::mouseMoveEvent(View *v, QMouseEvent *e)
gyorgyf@646 1286 {
gyorgyf@646 1287 // GF: context sensitive cursors
gyorgyf@646 1288 // v->setCursor(Qt::ArrowCursor);
gyorgyf@646 1289 FlexiNoteModel::Point note(0);
gyorgyf@646 1290 if (!getNoteToEdit(v, e->x(), e->y(), note)) {
gyorgyf@646 1291 // v->setCursor(Qt::UpArrowCursor);
gyorgyf@646 1292 return;
gyorgyf@646 1293 }
gyorgyf@646 1294
gyorgyf@646 1295 bool closeToLeft = false, closeToRight = false, closeToTop = false, closeToBottom = false;
gyorgyf@646 1296 getRelativeMousePosition(v, note, e->x(), e->y(), closeToLeft, closeToRight, closeToTop, closeToBottom);
gyorgyf@646 1297 // if (!closeToLeft) return;
gyorgyf@646 1298 // if (closeToTop) v->setCursor(Qt::SizeVerCursor);
gyorgyf@649 1299
matthiasm@651 1300 if (closeToLeft) { v->setCursor(Qt::SizeHorCursor); m_editMode = LeftBoundary; return; }
matthiasm@651 1301 if (closeToRight) { v->setCursor(Qt::SizeHorCursor); m_editMode = RightBoundary; return; }
matthiasm@651 1302 if (closeToTop) { v->setCursor(Qt::CrossCursor); m_editMode = DragNote; return; }
matthiasm@651 1303 if (closeToBottom) { v->setCursor(Qt::UpArrowCursor); m_editMode = SplitNote; return; }
gyorgyf@649 1304
gyorgyf@646 1305 v->setCursor(Qt::ArrowCursor);
gyorgyf@646 1306
gyorgyf@649 1307 // std::cerr << "Mouse moved in edit mode over FlexiNoteLayer" << std::endl;
gyorgyf@646 1308 // v->setCursor(Qt::SizeHorCursor);
gyorgyf@646 1309
gyorgyf@646 1310 }
gyorgyf@646 1311
gyorgyf@646 1312 void
gyorgyf@646 1313 FlexiNoteLayer::getRelativeMousePosition(View *v, FlexiNoteModel::Point &note, int x, int y, bool &closeToLeft, bool &closeToRight, bool &closeToTop, bool &closeToBottom) const
gyorgyf@646 1314 {
gyorgyf@649 1315 // GF: TODO: consoloidate the tolerance values
gyorgyf@646 1316 if (!m_model) return;
gyorgyf@646 1317
matthiasm@651 1318 int ctol = 0;
gyorgyf@646 1319 int noteStartX = v->getXForFrame(note.frame);
gyorgyf@646 1320 int noteEndX = v->getXForFrame(note.frame + note.duration);
gyorgyf@646 1321 int noteValueY = getYForValue(v,note.value);
gyorgyf@646 1322 int noteStartY = noteValueY - (NOTE_HEIGHT / 2);
gyorgyf@646 1323 int noteEndY = noteValueY + (NOTE_HEIGHT / 2);
gyorgyf@646 1324
gyorgyf@646 1325 bool closeToNote = false;
gyorgyf@646 1326
gyorgyf@646 1327 if (y >= noteStartY-ctol && y <= noteEndY+ctol && x >= noteStartX-ctol && x <= noteEndX+ctol) closeToNote = true;
gyorgyf@646 1328 if (!closeToNote) return;
gyorgyf@646 1329
matthiasm@651 1330 int tol = NOTE_HEIGHT / 2;
gyorgyf@646 1331
gyorgyf@646 1332 if (x >= noteStartX - tol && x <= noteStartX + tol) closeToLeft = true;
gyorgyf@646 1333 if (x >= noteEndX - tol && x <= noteEndX + tol) closeToRight = true;
gyorgyf@646 1334 if (y >= noteStartY - tol && y <= noteStartY + tol) closeToTop = true;
gyorgyf@646 1335 if (y >= noteEndY - tol && y <= noteEndY + tol) closeToBottom = true;
Chris@688 1336
Chris@688 1337 // cerr << "FlexiNoteLayer::getRelativeMousePosition: close to: left " << closeToLeft << " right " << closeToRight << " top " << closeToTop << " bottom " << closeToBottom << endl;
gyorgyf@635 1338 }
gyorgyf@635 1339
gyorgyf@635 1340
Chris@255 1341 bool
matthiasm@620 1342 FlexiNoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1343 {
gyorgyf@633 1344 std::cerr << "Opening note editor dialog" << std::endl;
Chris@255 1345 if (!m_model) return false;
Chris@70 1346
matthiasm@620 1347 FlexiNoteModel::Point note(0);
Chris@550 1348 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
Chris@550 1349
matthiasm@620 1350 // FlexiNoteModel::Point note = *points.begin();
Chris@70 1351
Chris@70 1352 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1353 (m_model->getSampleRate(),
Chris@70 1354 ItemEditDialog::ShowTime |
Chris@70 1355 ItemEditDialog::ShowDuration |
Chris@70 1356 ItemEditDialog::ShowValue |
Chris@100 1357 ItemEditDialog::ShowText,
Chris@100 1358 m_model->getScaleUnits());
Chris@70 1359
Chris@70 1360 dialog->setFrameTime(note.frame);
Chris@70 1361 dialog->setValue(note.value);
Chris@70 1362 dialog->setFrameDuration(note.duration);
Chris@70 1363 dialog->setText(note.label);
Chris@70 1364
Chris@70 1365 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1366
matthiasm@620 1367 FlexiNoteModel::Point newNote = note;
Chris@70 1368 newNote.frame = dialog->getFrameTime();
Chris@70 1369 newNote.value = dialog->getValue();
Chris@70 1370 newNote.duration = dialog->getFrameDuration();
Chris@70 1371 newNote.label = dialog->getText();
Chris@70 1372
matthiasm@620 1373 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
Chris@70 1374 (m_model, tr("Edit Point"));
Chris@70 1375 command->deletePoint(note);
Chris@70 1376 command->addPoint(newNote);
Chris@376 1377 finish(command);
Chris@70 1378 }
Chris@70 1379
Chris@70 1380 delete dialog;
Chris@255 1381 return true;
Chris@70 1382 }
Chris@70 1383
Chris@70 1384 void
matthiasm@620 1385 FlexiNoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1386 {
Chris@99 1387 if (!m_model) return;
Chris@99 1388
matthiasm@620 1389 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1390 new FlexiNoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 1391
matthiasm@620 1392 FlexiNoteModel::PointList points =
gyorgyf@646 1393 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1394
matthiasm@620 1395 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1396 i != points.end(); ++i) {
Chris@43 1397
gyorgyf@646 1398 if (s.contains(i->frame)) {
gyorgyf@646 1399 FlexiNoteModel::Point newPoint(*i);
gyorgyf@646 1400 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
gyorgyf@646 1401 command->deletePoint(*i);
gyorgyf@646 1402 command->addPoint(newPoint);
gyorgyf@646 1403 }
Chris@43 1404 }
Chris@43 1405
Chris@376 1406 finish(command);
Chris@43 1407 }
Chris@43 1408
Chris@43 1409 void
matthiasm@620 1410 FlexiNoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1411 {
Chris@99 1412 if (!m_model) return;
Chris@99 1413
matthiasm@620 1414 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1415 new FlexiNoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1416
matthiasm@620 1417 FlexiNoteModel::PointList points =
gyorgyf@646 1418 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1419
Chris@43 1420 double ratio =
gyorgyf@646 1421 double(newSize.getEndFrame() - newSize.getStartFrame()) /
gyorgyf@646 1422 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1423
matthiasm@620 1424 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1425 i != points.end(); ++i) {
Chris@43 1426
gyorgyf@646 1427 if (s.contains(i->frame)) {
Chris@43 1428
gyorgyf@646 1429 double targetStart = i->frame;
gyorgyf@646 1430 targetStart = newSize.getStartFrame() +
gyorgyf@646 1431 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 1432
gyorgyf@646 1433 double targetEnd = i->frame + i->duration;
gyorgyf@646 1434 targetEnd = newSize.getStartFrame() +
gyorgyf@646 1435 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 1436
gyorgyf@646 1437 FlexiNoteModel::Point newPoint(*i);
gyorgyf@646 1438 newPoint.frame = lrint(targetStart);
gyorgyf@646 1439 newPoint.duration = lrint(targetEnd - targetStart);
gyorgyf@646 1440 command->deletePoint(*i);
gyorgyf@646 1441 command->addPoint(newPoint);
gyorgyf@646 1442 }
Chris@43 1443 }
Chris@43 1444
Chris@376 1445 finish(command);
Chris@43 1446 }
Chris@43 1447
Chris@76 1448 void
matthiasm@620 1449 FlexiNoteLayer::deleteSelection(Selection s)
Chris@76 1450 {
Chris@99 1451 if (!m_model) return;
Chris@99 1452
matthiasm@620 1453 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1454 new FlexiNoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1455
matthiasm@620 1456 FlexiNoteModel::PointList points =
gyorgyf@646 1457 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1458
matthiasm@620 1459 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1460 i != points.end(); ++i) {
Chris@76 1461
Chris@76 1462 if (s.contains(i->frame)) {
Chris@76 1463 command->deletePoint(*i);
Chris@76 1464 }
Chris@76 1465 }
Chris@76 1466
Chris@376 1467 finish(command);
Chris@76 1468 }
Chris@76 1469
Chris@76 1470 void
matthiasm@620 1471 FlexiNoteLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1472 {
Chris@99 1473 if (!m_model) return;
Chris@99 1474
matthiasm@620 1475 FlexiNoteModel::PointList points =
gyorgyf@646 1476 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1477
matthiasm@620 1478 for (FlexiNoteModel::PointList::iterator i = points.begin();
gyorgyf@646 1479 i != points.end(); ++i) {
gyorgyf@646 1480 if (s.contains(i->frame)) {
Chris@335 1481 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
Chris@360 1482 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1483 to.addPoint(point);
Chris@76 1484 }
Chris@76 1485 }
Chris@76 1486 }
Chris@76 1487
Chris@125 1488 bool
matthiasm@620 1489 FlexiNoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
Chris@76 1490 {
Chris@125 1491 if (!m_model) return false;
Chris@99 1492
Chris@76 1493 const Clipboard::PointList &points = from.getPoints();
Chris@76 1494
Chris@360 1495 bool realign = false;
Chris@360 1496
Chris@360 1497 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1498
Chris@360 1499 QMessageBox::StandardButton button =
Chris@360 1500 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1501 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 1502 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1503 QMessageBox::Yes);
Chris@360 1504
Chris@360 1505 if (button == QMessageBox::Cancel) {
Chris@360 1506 return false;
Chris@360 1507 }
Chris@360 1508
Chris@360 1509 if (button == QMessageBox::Yes) {
Chris@360 1510 realign = true;
Chris@360 1511 }
Chris@360 1512 }
Chris@360 1513
matthiasm@620 1514 FlexiNoteModel::EditCommand *command =
gyorgyf@646 1515 new FlexiNoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1516
Chris@76 1517 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1518 i != points.end(); ++i) {
Chris@76 1519
Chris@76 1520 if (!i->haveFrame()) continue;
Chris@76 1521 size_t frame = 0;
Chris@360 1522
Chris@360 1523 if (!realign) {
Chris@360 1524
Chris@360 1525 frame = i->getFrame();
Chris@360 1526
Chris@360 1527 } else {
Chris@360 1528
Chris@360 1529 if (i->haveReferenceFrame()) {
Chris@360 1530 frame = i->getReferenceFrame();
Chris@360 1531 frame = alignFromReference(v, frame);
Chris@360 1532 } else {
Chris@360 1533 frame = i->getFrame();
Chris@360 1534 }
Chris@76 1535 }
Chris@360 1536
matthiasm@620 1537 FlexiNoteModel::Point newPoint(frame);
Chris@76 1538
Chris@76 1539 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1540 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1541 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1542 m_model->getValueMaximum()) / 2;
Chris@335 1543 if (i->haveLevel()) newPoint.level = i->getLevel();
Chris@76 1544 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 1545 else {
Chris@125 1546 size_t nextFrame = frame;
Chris@125 1547 Clipboard::PointList::const_iterator j = i;
Chris@125 1548 for (; j != points.end(); ++j) {
Chris@125 1549 if (!j->haveFrame()) continue;
Chris@125 1550 if (j != i) break;
Chris@125 1551 }
Chris@125 1552 if (j != points.end()) {
Chris@125 1553 nextFrame = j->getFrame();
Chris@125 1554 }
Chris@125 1555 if (nextFrame == frame) {
Chris@125 1556 newPoint.duration = m_model->getResolution();
Chris@125 1557 } else {
Chris@125 1558 newPoint.duration = nextFrame - frame;
Chris@125 1559 }
Chris@125 1560 }
Chris@76 1561
Chris@76 1562 command->addPoint(newPoint);
Chris@76 1563 }
Chris@76 1564
Chris@376 1565 finish(command);
Chris@125 1566 return true;
Chris@76 1567 }
Chris@76 1568
Chris@507 1569 void
matthiasm@620 1570 FlexiNoteLayer::addNoteOn(long frame, int pitch, int velocity)
Chris@507 1571 {
matthiasm@620 1572 m_pendingNoteOns.insert(FlexiNote(frame, pitch, 0, float(velocity) / 127.0, ""));
Chris@507 1573 }
Chris@507 1574
Chris@507 1575 void
matthiasm@620 1576 FlexiNoteLayer::addNoteOff(long frame, int pitch)
Chris@507 1577 {
matthiasm@620 1578 for (FlexiNoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1579 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1580 if (lrintf((*i).value) == pitch) {
matthiasm@620 1581 FlexiNote note(*i);
Chris@507 1582 m_pendingNoteOns.erase(i);
Chris@507 1583 note.duration = frame - note.frame;
Chris@507 1584 if (m_model) {
matthiasm@620 1585 FlexiNoteModel::AddPointCommand *c = new FlexiNoteModel::AddPointCommand
matthiasm@620 1586 (m_model, note, tr("Record FlexiNote"));
Chris@507 1587 // execute and bundle:
Chris@507 1588 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1589 }
Chris@507 1590 break;
Chris@507 1591 }
Chris@507 1592 }
Chris@507 1593 }
Chris@507 1594
Chris@507 1595 void
matthiasm@620 1596 FlexiNoteLayer::abandonNoteOns()
Chris@507 1597 {
Chris@507 1598 m_pendingNoteOns.clear();
Chris@507 1599 }
Chris@507 1600
Chris@287 1601 int
matthiasm@620 1602 FlexiNoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1603 {
Chris@287 1604 impose = false;
Chris@287 1605 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1606 (QString(darkbg ? "White" : "Black"));
Chris@287 1607 }
Chris@287 1608
Chris@316 1609 void
matthiasm@620 1610 FlexiNoteLayer::toXml(QTextStream &stream,
Chris@316 1611 QString indent, QString extraAttributes) const
Chris@30 1612 {
Chris@316 1613 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1614 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1615 .arg(m_verticalScale)
Chris@445 1616 .arg(m_scaleMinimum)
Chris@445 1617 .arg(m_scaleMaximum));
Chris@30 1618 }
Chris@30 1619
Chris@30 1620 void
matthiasm@620 1621 FlexiNoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1622 {
Chris@287 1623 SingleColourLayer::setProperties(attributes);
Chris@30 1624
Chris@445 1625 bool ok, alsoOk;
Chris@30 1626 VerticalScale scale = (VerticalScale)
gyorgyf@646 1627 attributes.value("verticalScale").toInt(&ok);
Chris@30 1628 if (ok) setVerticalScale(scale);
Chris@445 1629
Chris@445 1630 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1631 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@670 1632 // if (ok && alsoOk && min != max) setDisplayExtents(min, max);
Chris@30 1633 }
Chris@30 1634
matthiasm@651 1635 void
matthiasm@656 1636 FlexiNoteLayer::setVerticalRangeToNoteRange(View *v)
matthiasm@651 1637 {
matthiasm@656 1638 float minf = std::numeric_limits<float>::max();
matthiasm@651 1639 float maxf = 0;
matthiasm@656 1640 bool hasNotes = 0;
matthiasm@651 1641 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
matthiasm@651 1642 i != m_model->getPoints().end(); ++i) {
matthiasm@656 1643 hasNotes = 1;
matthiasm@651 1644 FlexiNote note = *i;
matthiasm@651 1645 if (note.value < minf) minf = note.value;
matthiasm@656 1646 if (note.value > maxf) maxf = note.value;
matthiasm@651 1647 }
matthiasm@651 1648
matthiasm@656 1649 std::cerr << "min frequency:" << minf << ", max frequency: " << maxf << std::endl;
matthiasm@656 1650
matthiasm@656 1651 if (hasNotes) {
matthiasm@659 1652 v->getLayer(1)->setDisplayExtents(minf*0.66,maxf*1.5);
matthiasm@656 1653 // MM: this is a hack because we rely on
matthiasm@656 1654 // * this layer being automatically aligned to layer 1
matthiasm@656 1655 // * layer one is a log frequency layer.
matthiasm@651 1656 }
matthiasm@651 1657 }
Chris@30 1658
matthiasm@651 1659