annotate layer/FlexiNoteLayer.cpp @ 688:212644efa523 tonioni

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