Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@30: Chris@30: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@59: This file copyright 2006 Chris Cannam. Chris@30: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@30: */ Chris@30: matthiasm@620: #include "FlexiNoteLayer.h" Chris@30: Chris@128: #include "data/model/Model.h" gyorgyf@655: #include "data/model/SparseTimeValueModel.h" Chris@30: #include "base/RealTime.h" Chris@30: #include "base/Profiler.h" Chris@30: #include "base/Pitch.h" Chris@197: #include "base/LogRange.h" Chris@439: #include "base/RangeMapper.h" Chris@1078: Chris@376: #include "ColourDatabase.h" Chris@1077: #include "LayerGeometryProvider.h" Chris@692: #include "PianoScale.h" Chris@701: #include "LinearNumericalScale.h" Chris@701: #include "LogNumericalScale.h" Chris@1078: #include "PaintAssistant.h" Chris@30: Chris@1426: #include "data/model/NoteModel.h" Chris@30: Chris@1077: #include "view/View.h" Chris@1077: Chris@70: #include "widgets/ItemEditDialog.h" Chris@701: #include "widgets/TextAbbrev.h" Chris@70: Chris@30: #include Chris@30: #include Chris@30: #include Chris@316: #include Chris@360: #include Chris@30: Chris@30: #include Chris@30: #include Chris@551: #include gyorgyf@655: #include // GF: included to compile std::numerical_limits on linux gyorgyf@655: #include gyorgyf@655: Chris@1426: #define NOTE_HEIGHT 16 Chris@30: matthiasm@620: FlexiNoteLayer::FlexiNoteLayer() : gyorgyf@646: SingleColourLayer(), gyorgyf@628: m_editing(false), Chris@805: m_intelligentActions(true), Chris@844: m_dragPointX(0), Chris@844: m_dragPointY(0), Chris@844: m_dragStartX(0), Chris@844: m_dragStartY(0), gyorgyf@627: m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")), gyorgyf@627: m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")), Chris@844: m_greatestLeftNeighbourFrame(0), Chris@844: m_smallestRightNeighbourFrame(0), Chris@1408: m_editingCommand(nullptr), matthiasm@634: m_verticalScale(AutoAlignScale), Chris@688: m_editMode(DragNote), gyorgyf@628: m_scaleMinimum(34), Chris@805: m_scaleMaximum(77) Chris@30: { Chris@30: } Chris@30: Chris@30: void Chris@1469: FlexiNoteLayer::setModel(ModelId modelId) Chris@30: { Chris@1471: auto newModel = ModelById::getAs(modelId); Chris@1471: Chris@1471: if (!modelId.isNone() && !newModel) { Chris@1471: throw std::logic_error("Not a NoteModel"); Chris@1471: } Chris@1471: Chris@1469: if (m_model == modelId) return; Chris@1469: m_model = modelId; Chris@30: Chris@1471: if (newModel) { Chris@1471: connectSignals(m_model); Chris@1471: } Chris@30: Chris@30: emit modelReplaced(); Chris@30: } Chris@30: Chris@30: Layer::PropertyList matthiasm@620: FlexiNoteLayer::getProperties() const Chris@30: { Chris@287: PropertyList list = SingleColourLayer::getProperties(); Chris@87: list.push_back("Vertical Scale"); Chris@100: list.push_back("Scale Units"); Chris@30: return list; Chris@30: } Chris@30: Chris@87: QString matthiasm@620: FlexiNoteLayer::getPropertyLabel(const PropertyName &name) const Chris@87: { Chris@87: if (name == "Vertical Scale") return tr("Vertical Scale"); Chris@116: if (name == "Scale Units") return tr("Scale Units"); Chris@287: return SingleColourLayer::getPropertyLabel(name); Chris@87: } Chris@87: Chris@30: Layer::PropertyType matthiasm@620: FlexiNoteLayer::getPropertyType(const PropertyName &name) const Chris@30: { Chris@100: if (name == "Scale Units") return UnitsProperty; Chris@287: if (name == "Vertical Scale") return ValueProperty; Chris@287: return SingleColourLayer::getPropertyType(name); Chris@30: } Chris@30: Chris@198: QString matthiasm@620: FlexiNoteLayer::getPropertyGroupName(const PropertyName &name) const Chris@198: { Chris@198: if (name == "Vertical Scale" || name == "Scale Units") { Chris@198: return tr("Scale"); Chris@198: } Chris@287: return SingleColourLayer::getPropertyGroupName(name); Chris@198: } Chris@198: Chris@701: QString Chris@703: FlexiNoteLayer::getScaleUnits() const Chris@701: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (model) return model->getScaleUnits(); Chris@701: else return ""; Chris@701: } Chris@701: Chris@30: int matthiasm@620: FlexiNoteLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@714: int *min, int *max, int *deflt) const Chris@30: { Chris@216: int val = 0; Chris@30: Chris@287: if (name == "Vertical Scale") { gyorgyf@646: Chris@714: if (min) *min = 0; Chris@714: if (max) *max = 3; Chris@216: if (deflt) *deflt = int(AutoAlignScale); gyorgyf@646: Chris@714: val = int(m_verticalScale); Chris@30: Chris@100: } else if (name == "Scale Units") { Chris@100: Chris@216: if (deflt) *deflt = 0; Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (model) { Chris@216: val = UnitDatabase::getInstance()->getUnitId Chris@701: (getScaleUnits()); Chris@100: } Chris@100: Chris@30: } else { Chris@216: Chris@714: val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); Chris@30: } Chris@30: Chris@216: return val; Chris@30: } Chris@30: Chris@30: QString matthiasm@620: FlexiNoteLayer::getPropertyValueLabel(const PropertyName &name, Chris@714: int value) const Chris@30: { Chris@287: if (name == "Vertical Scale") { Chris@714: switch (value) { Chris@714: default: Chris@714: case 0: return tr("Auto-Align"); Chris@714: case 1: return tr("Linear"); Chris@714: case 2: return tr("Log"); Chris@714: case 3: return tr("MIDI Notes"); Chris@714: } Chris@30: } Chris@287: return SingleColourLayer::getPropertyValueLabel(name, value); Chris@30: } Chris@30: Chris@30: void matthiasm@620: FlexiNoteLayer::setProperty(const PropertyName &name, int value) Chris@30: { Chris@287: if (name == "Vertical Scale") { Chris@714: setVerticalScale(VerticalScale(value)); Chris@100: } else if (name == "Scale Units") { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (model) { Chris@1469: model->setScaleUnits Chris@100: (UnitDatabase::getInstance()->getUnitById(value)); Chris@1481: emit modelChanged(m_model); Chris@100: } Chris@287: } else { Chris@287: return SingleColourLayer::setProperty(name, value); Chris@30: } Chris@30: } Chris@30: Chris@30: void matthiasm@620: FlexiNoteLayer::setVerticalScale(VerticalScale scale) Chris@30: { Chris@30: if (m_verticalScale == scale) return; Chris@30: m_verticalScale = scale; Chris@30: emit layerParametersChanged(); Chris@30: } Chris@30: Chris@30: bool Chris@916: FlexiNoteLayer::isLayerScrollable(const LayerGeometryProvider *v) const Chris@30: { Chris@30: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@30: } Chris@30: Chris@79: bool matthiasm@620: FlexiNoteLayer::shouldConvertMIDIToHz() const Chris@101: { Chris@701: QString unit = getScaleUnits(); Chris@101: return (unit != "Hz"); Chris@101: // if (unit == "" || Chris@101: // unit.startsWith("MIDI") || Chris@101: // unit.startsWith("midi")) return true; Chris@101: // return false; Chris@101: } Chris@101: Chris@1469: int Chris@1469: FlexiNoteLayer::getCompletion(LayerGeometryProvider *) const Chris@1469: { Chris@1469: auto model = ModelById::get(m_model); Chris@1469: if (model) return model->getCompletion(); Chris@1469: else return 0; Chris@1469: } Chris@1469: Chris@101: bool Chris@904: FlexiNoteLayer::getValueExtents(double &min, double &max, Chris@714: bool &logarithmic, QString &unit) const Chris@79: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return false; Chris@1469: min = model->getValueMinimum(); Chris@1469: max = model->getValueMaximum(); Chris@101: Chris@105: if (shouldConvertMIDIToHz()) { Chris@105: unit = "Hz"; Chris@904: min = Pitch::getFrequencyForPitch(int(lrint(min))); Chris@904: max = Pitch::getFrequencyForPitch(int(lrint(max + 1))); Chris@701: } else unit = getScaleUnits(); Chris@101: Chris@101: if (m_verticalScale == MIDIRangeScale || Chris@101: m_verticalScale == LogScale) logarithmic = true; Chris@101: Chris@101: return true; Chris@101: } Chris@101: Chris@101: bool Chris@904: FlexiNoteLayer::getDisplayExtents(double &min, double &max) const Chris@101: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || shouldAutoAlign()) { Chris@695: // std::cerr << "No model or shouldAutoAlign()" << std::endl; gyorgyf@646: return false; gyorgyf@646: } Chris@101: Chris@101: if (m_verticalScale == MIDIRangeScale) { Chris@101: min = Pitch::getFrequencyForPitch(0); matthiasm@634: max = Pitch::getFrequencyForPitch(127); Chris@101: return true; Chris@101: } Chris@101: Chris@439: if (m_scaleMinimum == m_scaleMaximum) { Chris@1469: min = model->getValueMinimum(); Chris@1469: max = model->getValueMaximum(); Chris@455: } else { Chris@455: min = m_scaleMinimum; Chris@455: max = m_scaleMaximum; Chris@439: } Chris@439: Chris@101: if (shouldConvertMIDIToHz()) { Chris@904: min = Pitch::getFrequencyForPitch(int(lrint(min))); Chris@904: max = Pitch::getFrequencyForPitch(int(lrint(max + 1))); Chris@101: } Chris@101: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@682: cerr << "NoteLayer::getDisplayExtents: min = " << min << ", max = " << max << " (m_scaleMinimum = " << m_scaleMinimum << ", m_scaleMaximum = " << m_scaleMaximum << ")" << endl; Chris@667: #endif Chris@667: Chris@79: return true; Chris@79: } Chris@79: Chris@439: bool Chris@904: FlexiNoteLayer::setDisplayExtents(double min, double max) Chris@439: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return false; Chris@439: Chris@439: if (min == max) { Chris@439: if (min == 0.f) { Chris@439: max = 1.f; Chris@439: } else { Chris@904: max = min * 1.0001f; Chris@439: } Chris@439: } Chris@439: Chris@439: m_scaleMinimum = min; Chris@439: m_scaleMaximum = max; Chris@439: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@684: cerr << "FlexiNoteLayer::setDisplayExtents: min = " << min << ", max = " << max << endl; Chris@667: #endif Chris@439: Chris@439: emit layerParametersChanged(); Chris@439: return true; Chris@439: } Chris@439: Chris@439: int matthiasm@620: FlexiNoteLayer::getVerticalZoomSteps(int &defaultStep) const Chris@439: { Chris@439: if (shouldAutoAlign()) return 0; Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return 0; Chris@439: Chris@439: defaultStep = 0; Chris@439: return 100; Chris@439: } Chris@439: Chris@439: int matthiasm@620: FlexiNoteLayer::getCurrentVerticalZoomStep() const Chris@439: { Chris@439: if (shouldAutoAlign()) return 0; Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return 0; Chris@439: Chris@439: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@439: if (!mapper) return 0; Chris@439: Chris@904: double dmin, dmax; Chris@439: getDisplayExtents(dmin, dmax); Chris@439: Chris@439: int nr = mapper->getPositionForValue(dmax - dmin); Chris@439: Chris@439: delete mapper; Chris@439: Chris@439: return 100 - nr; Chris@439: } Chris@439: Chris@439: //!!! lots of duplication with TimeValueLayer Chris@439: Chris@439: void matthiasm@620: FlexiNoteLayer::setVerticalZoomStep(int step) Chris@439: { Chris@439: if (shouldAutoAlign()) return; Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@439: Chris@439: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@439: if (!mapper) return; Chris@439: Chris@904: double min, max; Chris@439: bool logarithmic; Chris@439: QString unit; Chris@439: getValueExtents(min, max, logarithmic, unit); Chris@439: Chris@904: double dmin, dmax; Chris@439: getDisplayExtents(dmin, dmax); Chris@439: Chris@904: double newdist = mapper->getValueForPosition(100 - step); Chris@439: Chris@904: double newmin, newmax; Chris@439: Chris@439: if (logarithmic) { Chris@439: Chris@439: // see SpectrogramLayer::setVerticalZoomStep Chris@439: Chris@904: newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2; Chris@439: newmin = newmax - newdist; Chris@439: Chris@682: // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl; Chris@439: Chris@439: } else { Chris@904: double dmid = (dmax + dmin) / 2; Chris@439: newmin = dmid - newdist / 2; Chris@439: newmax = dmid + newdist / 2; Chris@439: } Chris@439: Chris@439: if (newmin < min) { Chris@439: newmax += (min - newmin); Chris@439: newmin = min; Chris@439: } Chris@439: if (newmax > max) { Chris@439: newmax = max; Chris@439: } Chris@439: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@684: cerr << "FlexiNoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl; Chris@667: #endif Chris@439: Chris@439: setDisplayExtents(newmin, newmax); Chris@439: } Chris@439: Chris@439: RangeMapper * matthiasm@620: FlexiNoteLayer::getNewVerticalZoomRangeMapper() const Chris@439: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return nullptr; Chris@439: Chris@439: RangeMapper *mapper; Chris@439: Chris@904: double min, max; Chris@439: bool logarithmic; Chris@439: QString unit; Chris@439: getValueExtents(min, max, logarithmic, unit); Chris@439: Chris@1408: if (min == max) return nullptr; Chris@439: Chris@439: if (logarithmic) { Chris@439: mapper = new LogRangeMapper(0, 100, min, max, unit); Chris@439: } else { Chris@439: mapper = new LinearRangeMapper(0, 100, min, max, unit); Chris@439: } Chris@439: Chris@439: return mapper; Chris@439: } Chris@439: Chris@1426: EventVector Chris@916: FlexiNoteLayer::getLocalPoints(LayerGeometryProvider *v, int x) const Chris@30: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return {}; Chris@1426: Chris@904: sv_frame_t frame = v->getFrameForX(x); Chris@30: Chris@1469: EventVector local = model->getEventsCovering(frame); Chris@1426: if (!local.empty()) return local; Chris@30: Chris@1426: int fuzz = ViewManager::scalePixelSize(2); Chris@1426: sv_frame_t start = v->getFrameForX(x - fuzz); Chris@1426: sv_frame_t end = v->getFrameForX(x + fuzz); Chris@30: Chris@1469: local = model->getEventsStartingWithin(frame, end - frame); Chris@1426: if (!local.empty()) return local; Chris@30: Chris@1469: local = model->getEventsSpanning(start, frame - start); Chris@1426: if (!local.empty()) return local; Chris@30: Chris@1426: return {}; Chris@30: } Chris@30: Chris@550: bool Chris@1426: FlexiNoteLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, Event &point) const Chris@550: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return false; Chris@550: Chris@904: sv_frame_t frame = v->getFrameForX(x); Chris@550: Chris@1469: EventVector onPoints = model->getEventsCovering(frame); Chris@550: if (onPoints.empty()) return false; Chris@550: Chris@550: int nearestDistance = -1; Chris@1426: for (const auto &p: onPoints) { Chris@1426: int distance = getYForValue(v, p.getValue()) - y; Chris@550: if (distance < 0) distance = -distance; Chris@550: if (nearestDistance == -1 || distance < nearestDistance) { Chris@550: nearestDistance = distance; Chris@1426: point = p; Chris@550: } Chris@550: } Chris@550: Chris@550: return true; Chris@550: } Chris@550: gyorgyf@646: bool Chris@1426: FlexiNoteLayer::getNoteToEdit(LayerGeometryProvider *v, int x, int y, Event &point) const gyorgyf@646: { gyorgyf@647: // GF: find the note that is closest to the cursor Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return false; gyorgyf@646: Chris@904: sv_frame_t frame = v->getFrameForX(x); gyorgyf@646: Chris@1469: EventVector onPoints = model->getEventsCovering(frame); gyorgyf@646: if (onPoints.empty()) return false; gyorgyf@646: gyorgyf@646: int nearestDistance = -1; Chris@1426: for (const auto &p: onPoints) { Chris@1426: int distance = getYForValue(v, p.getValue()) - y; gyorgyf@646: if (distance < 0) distance = -distance; gyorgyf@646: if (nearestDistance == -1 || distance < nearestDistance) { gyorgyf@646: nearestDistance = distance; Chris@1426: point = p; gyorgyf@646: } gyorgyf@646: } gyorgyf@646: gyorgyf@646: return true; gyorgyf@646: } gyorgyf@646: Chris@30: QString Chris@916: FlexiNoteLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const Chris@30: { Chris@30: int x = pos.x(); Chris@30: Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || !model->getSampleRate()) return ""; Chris@30: Chris@1426: EventVector points = getLocalPoints(v, x); Chris@30: Chris@30: if (points.empty()) { Chris@1469: if (!model->isReady()) { Chris@714: return tr("In progress"); Chris@714: } else { Chris@714: return tr("No local points"); Chris@714: } Chris@30: } Chris@30: Chris@1426: Event note(0); Chris@1426: EventVector::iterator i; Chris@30: Chris@30: for (i = points.begin(); i != points.end(); ++i) { Chris@30: Chris@1426: int y = getYForValue(v, i->getValue()); Chris@714: int h = NOTE_HEIGHT; // GF: larger notes Chris@30: Chris@1469: if (model->getValueQuantization() != 0.0) { Chris@1426: h = y - getYForValue Chris@1469: (v, i->getValue() + model->getValueQuantization()); Chris@714: if (h < NOTE_HEIGHT) h = NOTE_HEIGHT; Chris@714: } Chris@30: Chris@714: // GF: this is not quite correct Chris@714: if (pos.y() >= y - 4 && pos.y() <= y + h) { Chris@714: note = *i; Chris@714: break; Chris@714: } Chris@30: } Chris@30: Chris@30: if (i == points.end()) return tr("No local points"); Chris@30: Chris@1426: RealTime rt = RealTime::frame2RealTime(note.getFrame(), Chris@1469: model->getSampleRate()); Chris@1426: RealTime rd = RealTime::frame2RealTime(note.getDuration(), Chris@1469: model->getSampleRate()); Chris@30: Chris@101: QString pitchText; Chris@101: Chris@101: if (shouldConvertMIDIToHz()) { Chris@101: Chris@1426: int mnote = int(lrint(note.getValue())); Chris@1426: int cents = int(lrint((note.getValue() - double(mnote)) * 100)); Chris@904: double freq = Pitch::getFrequencyForPitch(mnote, cents); Chris@544: pitchText = tr("%1 (%2, %3 Hz)") Chris@544: .arg(Pitch::getPitchLabel(mnote, cents)) Chris@544: .arg(mnote) Chris@544: .arg(freq); Chris@101: Chris@701: } else if (getScaleUnits() == "Hz") { Chris@101: Chris@544: pitchText = tr("%1 Hz (%2, %3)") Chris@1426: .arg(note.getValue()) Chris@1426: .arg(Pitch::getPitchLabelForFrequency(note.getValue())) Chris@1426: .arg(Pitch::getPitchForFrequency(note.getValue())); Chris@101: Chris@101: } else { Chris@234: pitchText = tr("%1 %2") Chris@1426: .arg(note.getValue()).arg(getScaleUnits()); Chris@101: } Chris@101: Chris@30: QString text; Chris@30: Chris@1426: if (note.getLabel() == "") { Chris@714: text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label")) Chris@714: .arg(rt.toText(true).c_str()) Chris@714: .arg(pitchText) Chris@714: .arg(rd.toText(true).c_str()); Chris@30: } else { Chris@714: text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4")) Chris@714: .arg(rt.toText(true).c_str()) Chris@714: .arg(pitchText) Chris@714: .arg(rd.toText(true).c_str()) Chris@1426: .arg(note.getLabel()); Chris@30: } Chris@30: Chris@1426: pos = QPoint(v->getXForFrame(note.getFrame()), Chris@1426: getYForValue(v, note.getValue())); Chris@30: return text; Chris@30: } Chris@30: Chris@30: bool Chris@916: FlexiNoteLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, Chris@805: int &resolution, Chris@714: SnapType snap) const Chris@30: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) { Chris@714: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@30: } Chris@30: Chris@1469: resolution = model->getResolution(); Chris@1426: EventVector points; Chris@30: Chris@30: if (snap == SnapNeighbouring) { gyorgyf@646: Chris@714: points = getLocalPoints(v, v->getXForFrame(frame)); Chris@714: if (points.empty()) return false; Chris@1426: frame = points.begin()->getFrame(); Chris@714: return true; Chris@30: } Chris@30: Chris@1469: points = model->getEventsCovering(frame); Chris@904: sv_frame_t snapped = frame; Chris@30: bool found = false; Chris@30: Chris@1426: for (EventVector::const_iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@30: Chris@714: if (snap == SnapRight) { Chris@30: Chris@1426: if (i->getFrame() > frame) { Chris@1426: snapped = i->getFrame(); Chris@714: found = true; Chris@714: break; Chris@1426: } else if (i->getFrame() + i->getDuration() >= frame) { Chris@1426: snapped = i->getFrame() + i->getDuration(); Chris@715: found = true; Chris@715: break; Chris@714: } Chris@714: Chris@714: } else if (snap == SnapLeft) { Chris@714: Chris@1426: if (i->getFrame() <= frame) { Chris@1426: snapped = i->getFrame(); Chris@714: found = true; // don't break, as the next may be better Chris@714: } else { Chris@714: break; Chris@714: } Chris@714: Chris@714: } else { // nearest Chris@714: Chris@1426: EventVector::const_iterator j = i; Chris@714: ++j; Chris@714: Chris@714: if (j == points.end()) { Chris@714: Chris@1426: snapped = i->getFrame(); Chris@714: found = true; Chris@714: break; Chris@714: Chris@1426: } else if (j->getFrame() >= frame) { Chris@714: Chris@1426: if (j->getFrame() - frame < frame - i->getFrame()) { Chris@1426: snapped = j->getFrame(); Chris@714: } else { Chris@1426: snapped = i->getFrame(); Chris@714: } Chris@714: found = true; Chris@714: break; Chris@714: } gyorgyf@646: } Chris@30: } Chris@30: Chris@715: cerr << "snapToFeatureFrame: frame " << frame << " -> snapped " << snapped << ", found = " << found << endl; Chris@715: Chris@30: frame = snapped; Chris@30: return found; Chris@30: } Chris@30: Chris@101: void Chris@916: FlexiNoteLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const Chris@30: { Chris@101: min = 0.0; Chris@101: max = 0.0; Chris@101: log = false; Chris@42: Chris@101: QString queryUnits; Chris@101: if (shouldConvertMIDIToHz()) queryUnits = "Hz"; Chris@701: else queryUnits = getScaleUnits(); Chris@30: Chris@439: if (shouldAutoAlign()) { Chris@30: Chris@101: if (!v->getValueExtents(queryUnits, min, max, log)) { Chris@30: Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: min = model->getValueMinimum(); Chris@1469: max = model->getValueMaximum(); Chris@42: Chris@101: if (shouldConvertMIDIToHz()) { Chris@904: min = Pitch::getFrequencyForPitch(int(lrint(min))); Chris@904: max = Pitch::getFrequencyForPitch(int(lrint(max + 1))); Chris@101: } Chris@42: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@684: cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl; Chris@667: #endif Chris@105: Chris@101: } else if (log) { Chris@101: Chris@197: LogRange::mapRange(min, max); Chris@105: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@684: cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl; Chris@667: #endif Chris@101: } Chris@101: Chris@101: } else { Chris@101: Chris@439: getDisplayExtents(min, max); Chris@101: Chris@101: if (m_verticalScale == MIDIRangeScale) { Chris@101: min = Pitch::getFrequencyForPitch(0); matthiasm@623: max = Pitch::getFrequencyForPitch(70); Chris@101: } else if (shouldConvertMIDIToHz()) { Chris@904: min = Pitch::getFrequencyForPitch(int(lrint(min))); Chris@904: max = Pitch::getFrequencyForPitch(int(lrint(max + 1))); Chris@101: } Chris@101: Chris@101: if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) { Chris@197: LogRange::mapRange(min, max); Chris@101: log = true; Chris@101: } Chris@30: } Chris@30: Chris@101: if (max == min) max = min + 1.0; Chris@101: } Chris@30: Chris@101: int Chris@916: FlexiNoteLayer::getYForValue(LayerGeometryProvider *v, double val) const Chris@101: { Chris@904: double min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@916: int h = v->getPaintHeight(); Chris@101: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@684: cerr << "FlexiNoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl; Chris@667: #endif Chris@101: Chris@101: if (shouldConvertMIDIToHz()) { Chris@904: val = Pitch::getFrequencyForPitch(int(lrint(val)), Chris@904: int(lrint((val - floor(val)) * 100.0))); Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@682: cerr << "shouldConvertMIDIToHz true, val now = " << val << endl; Chris@667: #endif Chris@101: } Chris@101: Chris@101: if (logarithmic) { Chris@197: val = LogRange::map(val); Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@682: cerr << "logarithmic true, val now = " << val << endl; Chris@667: #endif Chris@101: } Chris@101: Chris@101: int y = int(h - ((val - min) * h) / (max - min)) - 1; Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@682: cerr << "y = " << y << endl; Chris@667: #endif Chris@101: return y; Chris@30: } Chris@30: Chris@904: double Chris@916: FlexiNoteLayer::getValueForY(LayerGeometryProvider *v, int y) const Chris@30: { Chris@904: double min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@916: int h = v->getPaintHeight(); Chris@30: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@904: double val = min + (double(h - y) * double(max - min)) / h; Chris@101: Chris@101: if (logarithmic) { Chris@904: val = pow(10.f, val); Chris@101: } Chris@101: Chris@101: if (shouldConvertMIDIToHz()) { Chris@101: val = Pitch::getPitchForFrequency(val); Chris@101: } Chris@101: Chris@101: return val; Chris@30: } Chris@30: Chris@439: bool matthiasm@620: FlexiNoteLayer::shouldAutoAlign() const Chris@439: { Chris@439: return (m_verticalScale == AutoAlignScale); Chris@439: } Chris@439: Chris@30: void Chris@916: FlexiNoteLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@30: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || !model->isOK()) return; Chris@30: Chris@1469: sv_samplerate_t sampleRate = model->getSampleRate(); Chris@30: if (!sampleRate) return; Chris@30: matthiasm@620: // Profiler profiler("FlexiNoteLayer::paint", true); Chris@30: Chris@1426: int x0 = rect.left(), x1 = rect.right(); Chris@1426: sv_frame_t frame0 = v->getFrameForX(x0); Chris@904: sv_frame_t frame1 = v->getFrameForX(x1); Chris@30: Chris@1469: EventVector points(model->getEventsSpanning(frame0, frame1 - frame0)); Chris@30: if (points.empty()) return; Chris@30: Chris@287: paint.setPen(getBaseQColor()); Chris@30: Chris@287: QColor brushColour(getBaseQColor()); Chris@30: brushColour.setAlpha(80); Chris@30: matthiasm@620: // SVDEBUG << "FlexiNoteLayer::paint: resolution is " Chris@1469: // << model->getResolution() << " frames" << endl; Chris@30: Chris@1469: double min = model->getValueMinimum(); Chris@1469: double max = model->getValueMaximum(); Chris@30: if (max == min) max = min + 1.0; Chris@30: Chris@30: QPoint localPos; Chris@1426: Event illuminatePoint(0); Chris@551: bool shouldIlluminate = false; Chris@30: Chris@44: if (v->shouldIlluminateLocalFeatures(this, localPos)) { Chris@551: shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(), Chris@551: illuminatePoint); Chris@30: } Chris@30: Chris@30: paint.save(); Chris@30: paint.setRenderHint(QPainter::Antialiasing, false); Chris@30: matthiasm@819: int noteNumber = 0; matthiasm@819: Chris@1426: for (EventVector::const_iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@30: matthiasm@819: ++noteNumber; Chris@1426: const Event &p(*i); Chris@30: Chris@1426: int x = v->getXForFrame(p.getFrame()); Chris@1426: int y = getYForValue(v, p.getValue()); Chris@1426: int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x; Chris@714: int h = NOTE_HEIGHT; //GF: larger notes gyorgyf@646: Chris@1469: if (model->getValueQuantization() != 0.0) { Chris@1469: h = y - getYForValue(v, p.getValue() + model->getValueQuantization()); Chris@714: if (h < NOTE_HEIGHT) h = NOTE_HEIGHT; //GF: larger notes Chris@714: } Chris@30: Chris@714: if (w < 1) w = 1; Chris@714: paint.setPen(getBaseQColor()); Chris@714: paint.setBrush(brushColour); Chris@30: Chris@1426: if (shouldIlluminate && illuminatePoint == p) { matthiasm@784: Chris@1426: paint.drawLine(x, -1, x, v->getPaintHeight() + 1); Chris@1426: paint.drawLine(x+w, -1, x+w, v->getPaintHeight() + 1); matthiasm@784: Chris@1426: paint.setPen(v->getForeground()); matthiasm@784: Chris@1426: QString vlabel = tr("freq: %1%2") Chris@1469: .arg(p.getValue()).arg(model->getScaleUnits()); Chris@1426: PaintAssistant::drawVisibleText Chris@1426: (v, paint, Chris@1426: x, Chris@1426: y - h/2 - 2 - paint.fontMetrics().height() Chris@1426: - paint.fontMetrics().descent(), Chris@1426: vlabel, PaintAssistant::OutlinedText); matthiasm@793: Chris@1426: QString hlabel = tr("dur: %1") Chris@1426: .arg(RealTime::frame2RealTime Chris@1469: (p.getDuration(), model->getSampleRate()).toText(true) Chris@1426: .c_str()); Chris@1426: PaintAssistant::drawVisibleText Chris@1426: (v, paint, Chris@1426: x, Chris@1426: y - h/2 - paint.fontMetrics().descent() - 2, Chris@1426: hlabel, PaintAssistant::OutlinedText); matthiasm@793: Chris@1426: QString llabel = QString("%1").arg(p.getLabel()); Chris@1426: PaintAssistant::drawVisibleText Chris@1426: (v, paint, Chris@1426: x, Chris@1426: y + h + 2 + paint.fontMetrics().descent(), Chris@1426: llabel, PaintAssistant::OutlinedText); Chris@1426: Chris@1426: QString nlabel = QString("%1").arg(noteNumber); Chris@1426: PaintAssistant::drawVisibleText Chris@1426: (v, paint, Chris@1426: x + paint.fontMetrics().averageCharWidth() / 2, Chris@1426: y + h/2 - paint.fontMetrics().descent(), Chris@1426: nlabel, PaintAssistant::OutlinedText); matthiasm@784: } gyorgyf@646: Chris@714: paint.drawRect(x, y - h/2, w, h); Chris@30: } Chris@30: Chris@30: paint.restore(); Chris@30: } Chris@30: Chris@692: int Chris@916: FlexiNoteLayer::getVerticalScaleWidth(LayerGeometryProvider *v, bool, QPainter &paint) const Chris@692: { Chris@1469: if (shouldAutoAlign()) { Chris@696: return 0; Chris@701: } else { Chris@701: if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) { Chris@701: return LogNumericalScale().getWidth(v, paint) + 10; // for piano Chris@701: } else { Chris@701: return LinearNumericalScale().getWidth(v, paint); Chris@701: } Chris@696: } Chris@692: } Chris@692: Chris@692: void Chris@916: FlexiNoteLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect) const Chris@692: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || model->isEmpty()) return; Chris@701: Chris@701: QString unit; Chris@904: double min, max; Chris@701: bool logarithmic; Chris@701: Chris@701: int w = getVerticalScaleWidth(v, false, paint); Chris@916: int h = v->getPaintHeight(); Chris@701: Chris@701: getScaleExtents(v, min, max, logarithmic); Chris@701: Chris@701: if (logarithmic) { Chris@701: LogNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@696: } else { Chris@701: LinearNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@701: } Chris@701: Chris@701: if (logarithmic && (getScaleUnits() == "Hz")) { Chris@696: PianoScale().paintPianoVertical Chris@701: (v, paint, QRect(w - 10, 0, 10, h), Chris@701: LogRange::unmap(min), Chris@701: LogRange::unmap(max)); Chris@701: paint.drawLine(w, 0, w, h); Chris@701: } Chris@701: Chris@701: if (getScaleUnits() != "") { Chris@701: int mw = w - 5; Chris@701: paint.drawText(5, Chris@701: 5 + paint.fontMetrics().ascent(), Chris@701: TextAbbrev::abbreviate(getScaleUnits(), Chris@701: paint.fontMetrics(), Chris@701: mw)); Chris@696: } Chris@692: } Chris@692: Chris@30: void Chris@916: FlexiNoteLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@30: Chris@904: sv_frame_t frame = v->getFrameForX(e->x()); Chris@30: if (frame < 0) frame = 0; Chris@1469: frame = frame / model->getResolution() * model->getResolution(); Chris@30: Chris@904: double value = getValueForY(v, e->y()); Chris@30: Chris@1426: m_editingPoint = Event(frame, float(value), 0, 0.8f, tr("New Point")); Chris@30: m_originalPoint = m_editingPoint; Chris@30: Chris@376: if (m_editingCommand) finish(m_editingCommand); Chris@1470: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Draw Point")); Chris@1426: m_editingCommand->add(m_editingPoint); Chris@30: Chris@30: m_editing = true; Chris@30: } Chris@30: Chris@30: void Chris@916: FlexiNoteLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || !m_editing) return; Chris@30: Chris@904: sv_frame_t frame = v->getFrameForX(e->x()); Chris@30: if (frame < 0) frame = 0; Chris@1469: frame = frame / model->getResolution() * model->getResolution(); Chris@30: Chris@904: double newValue = getValueForY(v, e->y()); Chris@101: Chris@1426: sv_frame_t newFrame = m_editingPoint.getFrame(); Chris@904: sv_frame_t newDuration = frame - newFrame; Chris@101: if (newDuration < 0) { Chris@101: newFrame = frame; Chris@101: newDuration = -newDuration; Chris@101: } else if (newDuration == 0) { Chris@101: newDuration = 1; Chris@101: } Chris@30: Chris@1426: m_editingCommand->remove(m_editingPoint); Chris@1426: m_editingPoint = m_editingPoint Chris@1426: .withFrame(newFrame) Chris@1426: .withValue(float(newValue)) Chris@1426: .withDuration(newDuration); Chris@1426: m_editingCommand->add(m_editingPoint); Chris@30: } Chris@30: Chris@30: void Chris@916: FlexiNoteLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl; Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || !m_editing) return; Chris@376: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@30: m_editing = false; Chris@30: } Chris@30: Chris@30: void Chris@916: FlexiNoteLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@335: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@335: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; Chris@335: Chris@335: if (m_editingCommand) { Chris@714: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@335: } Chris@335: Chris@335: m_editing = true; Chris@335: } Chris@335: Chris@335: void Chris@916: FlexiNoteLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *) Chris@335: { Chris@335: } Chris@335: Chris@335: void Chris@916: FlexiNoteLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e) Chris@335: { Chris@1469: if (!m_editing) return; Chris@335: m_editing = false; Chris@335: Chris@1426: Event p(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), p)) return; Chris@1469: if (p.getFrame() != m_editingPoint.getFrame() || Chris@1469: p.getValue() != m_editingPoint.getValue()) return; Chris@550: Chris@1470: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Erase Point")); Chris@1426: m_editingCommand->remove(m_editingPoint); Chris@376: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@335: m_editing = false; Chris@335: } Chris@335: Chris@335: void Chris@916: FlexiNoteLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl; gyorgyf@635: std::cerr << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl; Chris@30: Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@30: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; Chris@1426: m_originalPoint = m_editingPoint; gyorgyf@649: matthiasm@651: if (m_editMode == RightBoundary) { Chris@1426: m_dragPointX = v->getXForFrame Chris@1426: (m_editingPoint.getFrame() + m_editingPoint.getDuration()); gyorgyf@649: } else { Chris@1426: m_dragPointX = v->getXForFrame Chris@1426: (m_editingPoint.getFrame()); gyorgyf@649: } Chris@1426: m_dragPointY = getYForValue(v, m_editingPoint.getValue()); Chris@551: Chris@30: if (m_editingCommand) { Chris@714: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@30: } Chris@30: Chris@30: m_editing = true; Chris@551: m_dragStartX = e->x(); Chris@551: m_dragStartY = e->y(); matthiasm@651: Chris@1426: sv_frame_t onset = m_originalPoint.getFrame(); Chris@1426: sv_frame_t offset = Chris@1426: m_originalPoint.getFrame() + Chris@1426: m_originalPoint.getDuration() - 1; matthiasm@651: matthiasm@651: m_greatestLeftNeighbourFrame = -1; Chris@806: m_smallestRightNeighbourFrame = std::numeric_limits::max(); Chris@1426: Chris@1469: EventVector allEvents = model->getAllEvents(); matthiasm@651: Chris@1426: for (auto currentNote: allEvents) { matthiasm@651: matthiasm@651: // left boundary Chris@1426: if (currentNote.getFrame() + currentNote.getDuration() - 1 < onset) { Chris@1426: m_greatestLeftNeighbourFrame = Chris@1426: currentNote.getFrame() + currentNote.getDuration() - 1; matthiasm@651: } matthiasm@651: matthiasm@651: // right boundary Chris@1426: if (currentNote.getFrame() > offset) { Chris@1426: m_smallestRightNeighbourFrame = currentNote.getFrame(); matthiasm@651: break; matthiasm@651: } matthiasm@651: } Chris@1426: Chris@753: std::cerr << "editStart: mode is " << m_editMode << ", note frame: " << onset << ", left boundary: " << m_greatestLeftNeighbourFrame << ", right boundary: " << m_smallestRightNeighbourFrame << std::endl; Chris@30: } Chris@30: Chris@30: void Chris@916: FlexiNoteLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl; gyorgyf@635: std::cerr << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl; Chris@30: Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || !m_editing) return; Chris@30: Chris@551: int xdist = e->x() - m_dragStartX; Chris@551: int ydist = e->y() - m_dragStartY; Chris@551: int newx = m_dragPointX + xdist; Chris@551: int newy = m_dragPointY + ydist; Chris@551: Chris@904: sv_frame_t dragFrame = v->getFrameForX(newx); matthiasm@657: if (dragFrame < 0) dragFrame = 0; Chris@1469: dragFrame = dragFrame / model->getResolution() * model->getResolution(); matthiasm@651: Chris@904: double value = getValueForY(v, newy); Chris@30: Chris@30: if (!m_editingCommand) { Chris@1469: m_editingCommand = Chris@1470: new ChangeEventsCommand(m_model.untyped, tr("Drag Point")); Chris@30: } Chris@1426: m_editingCommand->remove(m_editingPoint); matthiasm@651: Chris@874: std::cerr << "edit mode: " << m_editMode << " intelligent actions = " Chris@874: << m_intelligentActions << std::endl; matthiasm@657: matthiasm@651: switch (m_editMode) { Chris@1426: Chris@714: case LeftBoundary : { Chris@714: // left Chris@1426: if (m_intelligentActions && Chris@1426: dragFrame <= m_greatestLeftNeighbourFrame) { Chris@1426: dragFrame = m_greatestLeftNeighbourFrame + 1; Chris@1426: } Chris@714: // right Chris@1426: if (m_intelligentActions && Chris@1426: dragFrame >= m_originalPoint.getFrame() + m_originalPoint.getDuration()) { Chris@1426: dragFrame = m_originalPoint.getFrame() + m_originalPoint.getDuration() - 1; matthiasm@651: } Chris@1426: m_editingPoint = m_editingPoint Chris@1426: .withFrame(dragFrame) Chris@1426: .withDuration(m_originalPoint.getFrame() - Chris@1426: dragFrame + m_originalPoint.getDuration()); Chris@714: break; Chris@714: } Chris@1426: Chris@714: case RightBoundary : { Chris@714: // left Chris@1426: if (m_intelligentActions && Chris@1426: dragFrame <= m_greatestLeftNeighbourFrame) { Chris@1426: dragFrame = m_greatestLeftNeighbourFrame + 1; Chris@1426: } Chris@1426: if (m_intelligentActions && Chris@1426: dragFrame >= m_smallestRightNeighbourFrame) { Chris@1426: dragFrame = m_smallestRightNeighbourFrame - 1; Chris@1426: } Chris@1426: m_editingPoint = m_editingPoint Chris@1426: .withDuration(dragFrame - m_originalPoint.getFrame() + 1); Chris@714: break; Chris@714: } Chris@1426: Chris@714: case DragNote : { Chris@714: // left Chris@1426: if (m_intelligentActions && Chris@1426: dragFrame <= m_greatestLeftNeighbourFrame) { Chris@1426: dragFrame = m_greatestLeftNeighbourFrame + 1; Chris@1426: } Chris@714: // right Chris@1426: if (m_intelligentActions && Chris@1426: dragFrame + m_originalPoint.getDuration() >= m_smallestRightNeighbourFrame) { Chris@1426: dragFrame = m_smallestRightNeighbourFrame - m_originalPoint.getDuration(); matthiasm@651: } Chris@1426: Chris@1426: m_editingPoint = m_editingPoint Chris@1426: .withFrame(dragFrame) Chris@1426: .withValue(float(value)); Chris@875: Chris@875: // Re-analyse region within +/- 1 semitone of the dragged value Chris@875: float cents = 0; Chris@1426: int midiPitch = Pitch::getPitchForFrequency(m_editingPoint.getValue(), ¢s); Chris@922: double lower = Pitch::getFrequencyForPitch(midiPitch - 1, cents); Chris@922: double higher = Pitch::getFrequencyForPitch(midiPitch + 1, cents); Chris@875: Chris@1426: emit reAnalyseRegion(m_editingPoint.getFrame(), Chris@1426: m_editingPoint.getFrame() + Chris@1426: m_editingPoint.getDuration(), Chris@922: float(lower), float(higher)); Chris@714: break; Chris@714: } Chris@1426: Chris@805: case SplitNote: // nothing Chris@805: break; gyorgyf@649: } Chris@875: Chris@1426: m_editingCommand->add(m_editingPoint); Chris@875: Chris@1426: std::cerr << "added new point(" << m_editingPoint.getFrame() << "," << m_editingPoint.getDuration() << ")" << std::endl; Chris@30: } Chris@30: Chris@30: void Chris@948: FlexiNoteLayer::editEnd(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { Chris@1426: std::cerr << "FlexiNoteLayer::editEnd(" Chris@1426: << e->x() << "," << e->y() << ")" << std::endl; matthiasm@656: Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || !m_editing) return; Chris@30: Chris@30: if (m_editingCommand) { Chris@30: Chris@714: QString newName = m_editingCommand->getName(); Chris@30: Chris@876: if (m_editMode == DragNote) { Chris@876: //!!! command nesting is wrong? Chris@876: emit materialiseReAnalysis(); Chris@876: } Chris@876: Chris@1426: m_editingCommand->remove(m_editingPoint); Chris@875: updateNoteValueFromPitchCurve(v, m_editingPoint); Chris@1426: m_editingCommand->add(m_editingPoint); Chris@875: Chris@1426: if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) { Chris@1426: if (m_editingPoint.getValue() != m_originalPoint.getValue()) { Chris@714: newName = tr("Edit Point"); Chris@714: } else { Chris@714: newName = tr("Relocate Point"); Chris@714: } gyorgyf@646: } else { Chris@714: newName = tr("Change Point Value"); gyorgyf@646: } Chris@30: Chris@714: m_editingCommand->setName(newName); Chris@714: finish(m_editingCommand); Chris@30: } Chris@30: Chris@1408: m_editingCommand = nullptr; Chris@30: m_editing = false; Chris@30: } Chris@30: gyorgyf@635: void Chris@916: FlexiNoteLayer::splitStart(LayerGeometryProvider *v, QMouseEvent *e) gyorgyf@635: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@1469: gyorgyf@646: // GF: note splitting starts (!! remove printing soon) Chris@874: std::cerr << "splitStart (n.b. editStart will be called later, if the user drags the mouse)" << std::endl; gyorgyf@635: gyorgyf@635: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; gyorgyf@635: // m_originalPoint = m_editingPoint; gyorgyf@635: // Chris@1426: // m_dragPointX = v->getXForFrame(m_editingPoint.getFrame()); Chris@1426: // m_dragPointY = getYForValue(v, m_editingPoint.getValue()); gyorgyf@635: gyorgyf@635: if (m_editingCommand) { Chris@714: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; gyorgyf@635: } gyorgyf@635: gyorgyf@635: m_editing = true; gyorgyf@635: m_dragStartX = e->x(); gyorgyf@635: m_dragStartY = e->y(); gyorgyf@635: } gyorgyf@635: gyorgyf@635: void Chris@916: FlexiNoteLayer::splitEnd(LayerGeometryProvider *v, QMouseEvent *e) gyorgyf@635: { Chris@1469: auto model = ModelById::getAs(m_model); gyorgyf@646: // GF: note splitting ends. (!! remove printing soon) gyorgyf@646: std::cerr << "splitEnd" << std::endl; Chris@1469: if (!model || !m_editing || m_editMode != SplitNote) return; gyorgyf@635: gyorgyf@635: int xdist = e->x() - m_dragStartX; gyorgyf@635: int ydist = e->y() - m_dragStartY; gyorgyf@635: if (xdist != 0 || ydist != 0) { gyorgyf@646: std::cerr << "mouse moved" << std::endl; gyorgyf@635: return; gyorgyf@635: } gyorgyf@635: Chris@904: sv_frame_t frame = v->getFrameForX(e->x()); gyorgyf@635: Chris@753: splitNotesAt(v, frame, e); Chris@746: } Chris@746: Chris@746: void Chris@916: FlexiNoteLayer::splitNotesAt(LayerGeometryProvider *v, sv_frame_t frame) Chris@746: { Chris@1408: splitNotesAt(v, frame, nullptr); Chris@753: } Chris@753: Chris@753: void Chris@916: FlexiNoteLayer::splitNotesAt(LayerGeometryProvider *v, sv_frame_t frame, QMouseEvent *e) Chris@753: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@1469: Chris@1469: EventVector onPoints = model->getEventsCovering(frame); Chris@746: if (onPoints.empty()) return; Chris@746: Chris@1426: Event note(*onPoints.begin()); gyorgyf@635: Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Edit Point")); Chris@1426: command->remove(note); Chris@753: Chris@753: if (!e || !(e->modifiers() & Qt::ShiftModifier)) { Chris@753: Chris@753: int gap = 0; // MM: I prefer a gap of 0, but we can decide later Chris@753: Chris@1426: Event newNote1(note.getFrame(), note.getValue(), Chris@1426: frame - note.getFrame() - gap, Chris@1426: note.getLevel(), note.getLabel()); Chris@753: Chris@1426: Event newNote2(frame, note.getValue(), Chris@1426: note.getDuration() - newNote1.getDuration(), Chris@1426: note.getLevel(), note.getLabel()); Chris@747: Chris@753: if (m_intelligentActions) { Chris@875: if (updateNoteValueFromPitchCurve(v, newNote1)) { Chris@1426: command->add(newNote1); Chris@753: } Chris@875: if (updateNoteValueFromPitchCurve(v, newNote2)) { Chris@1426: command->add(newNote2); Chris@753: } Chris@753: } else { Chris@1426: command->add(newNote1); Chris@1426: command->add(newNote2); Chris@747: } Chris@747: } Chris@746: gyorgyf@635: finish(command); gyorgyf@646: } gyorgyf@646: gyorgyf@655: void Chris@916: FlexiNoteLayer::addNote(LayerGeometryProvider *v, QMouseEvent *e) matthiasm@660: { Chris@1469: auto model = ModelById::getAs(m_model); matthiasm@660: std::cerr << "addNote" << std::endl; Chris@1469: if (!model) return; matthiasm@660: Chris@904: sv_frame_t duration = 10000; matthiasm@660: Chris@904: sv_frame_t frame = v->getFrameForX(e->x()); Chris@904: double value = getValueForY(v, e->y()); matthiasm@660: Chris@1469: EventVector noteList = model->getAllEvents(); matthiasm@792: matthiasm@660: if (m_intelligentActions) { Chris@904: sv_frame_t smallestRightNeighbourFrame = 0; Chris@1426: for (EventVector::const_iterator i = noteList.begin(); matthiasm@792: i != noteList.end(); ++i) { Chris@1426: Event currentNote = *i; Chris@1426: if (currentNote.getFrame() > frame) { Chris@1426: smallestRightNeighbourFrame = currentNote.getFrame(); matthiasm@660: break; matthiasm@660: } matthiasm@660: } matthiasm@792: if (smallestRightNeighbourFrame > 0) { matthiasm@792: duration = std::min(smallestRightNeighbourFrame - frame + 1, duration); matthiasm@792: duration = (duration > 0) ? duration : 0; matthiasm@792: } matthiasm@660: } matthiasm@660: matthiasm@660: if (!m_intelligentActions || Chris@1469: (model->getEventsCovering(frame).empty() && duration > 0)) { Chris@1426: Event newNote(frame, float(value), duration, 100.f, tr("new note")); Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Add Point")); Chris@1426: command->add(newNote); matthiasm@660: finish(command); matthiasm@660: } matthiasm@660: } matthiasm@660: Chris@1469: ModelId Chris@916: FlexiNoteLayer::getAssociatedPitchModel(LayerGeometryProvider *v) const Chris@745: { Chris@745: // Better than we used to do, but still not very satisfactory Chris@745: Chris@874: // cerr << "FlexiNoteLayer::getAssociatedPitchModel()" << endl; Chris@746: Chris@918: for (int i = 0; i < v->getView()->getLayerCount(); ++i) { Chris@918: Layer *layer = v->getView()->getLayer(i); Chris@795: if (layer && Chris@748: layer->getLayerPresentationName() != "candidate") { Chris@874: // cerr << "FlexiNoteLayer::getAssociatedPitchModel: looks like our layer is " << layer << endl; Chris@1469: auto modelId = layer->getModel(); Chris@1469: auto model = ModelById::getAs(modelId); Chris@745: if (model && model->getScaleUnits() == "Hz") { Chris@1469: // cerr << "FlexiNoteLayer::getAssociatedPitchModel: it's good, returning " << model << endl; Chris@1469: return modelId; Chris@745: } Chris@745: } Chris@745: } Chris@1469: // cerr << "FlexiNoteLayer::getAssociatedPitchModel: failed to find a model" << endl; Chris@1469: return {}; Chris@745: } matthiasm@660: matthiasm@660: void Chris@916: FlexiNoteLayer::snapSelectedNotesToPitchTrack(LayerGeometryProvider *v, Selection s) Chris@746: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@746: Chris@1426: EventVector points = Chris@1469: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@746: Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Snap Notes")); Chris@746: Chris@746: cerr << "snapSelectedNotesToPitchTrack: selection is from " << s.getStartFrame() << " to " << s.getEndFrame() << endl; Chris@746: Chris@1426: for (EventVector::iterator i = points.begin(); Chris@746: i != points.end(); ++i) { Chris@746: Chris@1426: Event note(*i); Chris@746: Chris@1426: cerr << "snapSelectedNotesToPitchTrack: looking at note from " << note.getFrame() << " to " << note.getFrame() + note.getDuration() << endl; Chris@746: Chris@1426: if (!s.contains(note.getFrame()) && Chris@1426: !s.contains(note.getFrame() + note.getDuration() - 1)) { Chris@746: continue; Chris@746: } Chris@746: matthiasm@773: cerr << "snapSelectedNotesToPitchTrack: making new note" << endl; Chris@1426: Event newNote(note); Chris@746: Chris@1426: command->remove(note); Chris@746: Chris@875: if (updateNoteValueFromPitchCurve(v, newNote)) { Chris@1426: command->add(newNote); matthiasm@775: } Chris@746: } Chris@747: Chris@746: finish(command); Chris@746: } Chris@746: Chris@746: void Chris@916: FlexiNoteLayer::mergeNotes(LayerGeometryProvider *v, Selection s, bool inclusive) Chris@747: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@1469: Chris@1426: EventVector points; Chris@749: if (inclusive) { Chris@1469: points = model->getEventsSpanning(s.getStartFrame(), s.getDuration()); Chris@749: } else { Chris@1469: points = model->getEventsWithin(s.getStartFrame(), s.getDuration()); Chris@747: } Chris@749: Chris@1426: EventVector::iterator i = points.begin(); Chris@747: if (i == points.end()) return; Chris@747: Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Merge Notes")); Chris@747: Chris@1426: Event newNote(*i); Chris@747: Chris@747: while (i != points.end()) { Chris@747: Chris@749: if (inclusive) { Chris@1426: if (i->getFrame() >= s.getEndFrame()) break; Chris@749: } else { Chris@1426: if (i->getFrame() + i->getDuration() > s.getEndFrame()) break; Chris@749: } Chris@747: Chris@1426: newNote = newNote.withDuration Chris@1426: (i->getFrame() + i->getDuration() - newNote.getFrame()); Chris@1426: command->remove(*i); Chris@747: Chris@747: ++i; Chris@747: } Chris@747: Chris@875: updateNoteValueFromPitchCurve(v, newNote); Chris@1426: command->add(newNote); Chris@747: finish(command); Chris@747: } Chris@747: Chris@747: bool Chris@1426: FlexiNoteLayer::updateNoteValueFromPitchCurve(LayerGeometryProvider *v, Event ¬e) const gyorgyf@655: { Chris@1469: ModelId modelId = getAssociatedPitchModel(v); Chris@1469: auto model = ModelById::getAs(modelId); Chris@747: if (!model) return false; gyorgyf@655: gyorgyf@655: std::cerr << model->getTypeName() << std::endl; gyorgyf@655: Chris@1429: EventVector dataPoints = Chris@1429: model->getEventsWithin(note.getFrame(), note.getDuration()); Chris@746: Chris@1426: std::cerr << "frame " << note.getFrame() << ": " << dataPoints.size() << " candidate points" << std::endl; Chris@746: Chris@747: if (dataPoints.empty()) return false; Chris@746: Chris@904: std::vector pitchValues; gyorgyf@655: Chris@1429: for (EventVector::const_iterator i = Chris@747: dataPoints.begin(); i != dataPoints.end(); ++i) { Chris@1429: pitchValues.push_back(i->getValue()); gyorgyf@655: } Chris@747: Chris@747: if (pitchValues.empty()) return false; Chris@747: gyorgyf@655: sort(pitchValues.begin(), pitchValues.end()); Chris@904: int size = int(pitchValues.size()); gyorgyf@655: double median; gyorgyf@655: gyorgyf@655: if (size % 2 == 0) { gyorgyf@655: median = (pitchValues[size/2 - 1] + pitchValues[size/2]) / 2; gyorgyf@655: } else { gyorgyf@655: median = pitchValues[size/2]; gyorgyf@655: } Chris@875: Chris@1426: std::cerr << "updateNoteValueFromPitchCurve: corrected from " << note.getValue() << " to median " << median << std::endl; gyorgyf@655: Chris@1426: note = note.withValue(float(median)); Chris@747: Chris@747: return true; gyorgyf@655: } gyorgyf@655: gyorgyf@646: void Chris@916: FlexiNoteLayer::mouseMoveEvent(LayerGeometryProvider *v, QMouseEvent *e) gyorgyf@646: { gyorgyf@646: // GF: context sensitive cursors Chris@918: // v->getView()->setCursor(Qt::ArrowCursor); Chris@1426: Event note(0); gyorgyf@646: if (!getNoteToEdit(v, e->x(), e->y(), note)) { Chris@918: // v->getView()->setCursor(Qt::UpArrowCursor); gyorgyf@646: return; gyorgyf@646: } gyorgyf@646: Chris@874: bool closeToLeft = false, closeToRight = false, Chris@874: closeToTop = false, closeToBottom = false; Chris@874: getRelativeMousePosition(v, note, e->x(), e->y(), Chris@874: closeToLeft, closeToRight, Chris@874: closeToTop, closeToBottom); gyorgyf@649: Chris@874: if (closeToLeft) { Chris@945: v->getView()->setCursor(Qt::SizeHorCursor); Chris@874: m_editMode = LeftBoundary; Chris@874: cerr << "edit mode -> LeftBoundary" << endl; Chris@874: } else if (closeToRight) { Chris@945: v->getView()->setCursor(Qt::SizeHorCursor); Chris@874: m_editMode = RightBoundary; Chris@874: cerr << "edit mode -> RightBoundary" << endl; Chris@874: } else if (closeToTop) { Chris@945: v->getView()->setCursor(Qt::CrossCursor); Chris@874: m_editMode = DragNote; Chris@874: cerr << "edit mode -> DragNote" << endl; Chris@874: } else if (closeToBottom) { Chris@945: v->getView()->setCursor(Qt::UpArrowCursor); Chris@874: m_editMode = SplitNote; Chris@874: cerr << "edit mode -> SplitNote" << endl; Chris@874: } else { Chris@945: v->getView()->setCursor(Qt::ArrowCursor); Chris@874: } gyorgyf@646: } gyorgyf@646: gyorgyf@646: void Chris@1426: FlexiNoteLayer::getRelativeMousePosition(LayerGeometryProvider *v, Event ¬e, int x, int y, bool &closeToLeft, bool &closeToRight, bool &closeToTop, bool &closeToBottom) const gyorgyf@646: { Chris@1469: // GF: TODO: consolidate the tolerance values gyorgyf@646: matthiasm@651: int ctol = 0; Chris@1426: int noteStartX = v->getXForFrame(note.getFrame()); Chris@1426: int noteEndX = v->getXForFrame(note.getFrame() + note.getDuration()); Chris@1426: int noteValueY = getYForValue(v,note.getValue()); gyorgyf@646: int noteStartY = noteValueY - (NOTE_HEIGHT / 2); gyorgyf@646: int noteEndY = noteValueY + (NOTE_HEIGHT / 2); gyorgyf@646: gyorgyf@646: bool closeToNote = false; gyorgyf@646: gyorgyf@646: if (y >= noteStartY-ctol && y <= noteEndY+ctol && x >= noteStartX-ctol && x <= noteEndX+ctol) closeToNote = true; gyorgyf@646: if (!closeToNote) return; gyorgyf@646: matthiasm@651: int tol = NOTE_HEIGHT / 2; gyorgyf@646: gyorgyf@646: if (x >= noteStartX - tol && x <= noteStartX + tol) closeToLeft = true; gyorgyf@646: if (x >= noteEndX - tol && x <= noteEndX + tol) closeToRight = true; gyorgyf@646: if (y >= noteStartY - tol && y <= noteStartY + tol) closeToTop = true; gyorgyf@646: if (y >= noteEndY - tol && y <= noteEndY + tol) closeToBottom = true; Chris@688: Chris@688: // cerr << "FlexiNoteLayer::getRelativeMousePosition: close to: left " << closeToLeft << " right " << closeToRight << " top " << closeToTop << " bottom " << closeToBottom << endl; gyorgyf@635: } gyorgyf@635: gyorgyf@635: Chris@255: bool Chris@916: FlexiNoteLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e) Chris@70: { gyorgyf@633: std::cerr << "Opening note editor dialog" << std::endl; Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return false; Chris@70: Chris@1426: Event note(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), note)) return false; Chris@550: Chris@1426: // Event note = *points.begin(); Chris@70: Chris@70: ItemEditDialog *dialog = new ItemEditDialog Chris@1469: (model->getSampleRate(), Chris@70: ItemEditDialog::ShowTime | Chris@70: ItemEditDialog::ShowDuration | Chris@70: ItemEditDialog::ShowValue | Chris@100: ItemEditDialog::ShowText, Chris@701: getScaleUnits()); Chris@70: Chris@1426: dialog->setFrameTime(note.getFrame()); Chris@1426: dialog->setValue(note.getValue()); Chris@1426: dialog->setFrameDuration(note.getDuration()); Chris@1426: dialog->setText(note.getLabel()); Chris@70: Chris@70: if (dialog->exec() == QDialog::Accepted) { Chris@70: Chris@1426: Event newNote = note Chris@1426: .withFrame(dialog->getFrameTime()) Chris@1426: .withValue(dialog->getValue()) Chris@1426: .withDuration(dialog->getFrameDuration()) Chris@1426: .withLabel(dialog->getText()); Chris@70: Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Edit Point")); Chris@1426: command->remove(note); Chris@1426: command->add(newNote); Chris@376: finish(command); Chris@70: } Chris@70: Chris@70: delete dialog; Chris@255: return true; Chris@70: } Chris@70: Chris@70: void Chris@905: FlexiNoteLayer::moveSelection(Selection s, sv_frame_t newStartFrame) Chris@43: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@1469: Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Drag Selection")); Chris@43: Chris@1426: EventVector points = Chris@1469: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1426: for (Event p: points) { Chris@1426: command->remove(p); Chris@1426: Event moved = p.withFrame(p.getFrame() + Chris@1426: newStartFrame - s.getStartFrame()); Chris@1426: command->add(moved); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@43: void matthiasm@620: FlexiNoteLayer::resizeSelection(Selection s, Selection newSize) Chris@43: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model || !s.getDuration()) return; Chris@99: Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Resize Selection")); Chris@43: Chris@1426: EventVector points = Chris@1469: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1426: double ratio = double(newSize.getDuration()) / double(s.getDuration()); Chris@1426: double oldStart = double(s.getStartFrame()); Chris@1426: double newStart = double(newSize.getStartFrame()); Chris@1426: Chris@1426: for (Event p: points) { Chris@43: Chris@1426: double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart; Chris@1426: double newDuration = double(p.getDuration()) * ratio; Chris@43: Chris@1426: Event newPoint = p Chris@1426: .withFrame(lrint(newFrame)) Chris@1426: .withDuration(lrint(newDuration)); Chris@1426: command->remove(p); Chris@1426: command->add(newPoint); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@76: void matthiasm@620: FlexiNoteLayer::deleteSelection(Selection s) Chris@76: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@99: Chris@1469: auto command = Chris@1470: new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points")); Chris@76: Chris@1426: EventVector points = Chris@1469: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@76: Chris@1426: for (Event p: points) { Chris@1426: command->remove(p); Chris@76: } Chris@76: Chris@376: finish(command); Chris@76: } Chris@76: Chris@76: void matthiasm@784: FlexiNoteLayer::deleteSelectionInclusive(Selection s) matthiasm@784: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; matthiasm@784: Chris@1469: auto command = Chris@1470: new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points")); matthiasm@784: Chris@1426: EventVector points = Chris@1469: model->getEventsSpanning(s.getStartFrame(), s.getDuration()); matthiasm@784: Chris@1426: for (Event p: points) { Chris@1426: command->remove(p); matthiasm@784: } matthiasm@784: matthiasm@784: finish(command); matthiasm@784: } matthiasm@784: matthiasm@784: void Chris@916: FlexiNoteLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to) Chris@76: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@99: Chris@1426: EventVector points = Chris@1469: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@76: Chris@1426: for (Event p: points) { Chris@1426: to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame()))); Chris@76: } Chris@76: } Chris@76: Chris@125: bool Chris@916: FlexiNoteLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /*frameOffset */, bool /* interactive */) Chris@76: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return false; Chris@99: Chris@1423: const EventVector &points = from.getPoints(); Chris@76: Chris@360: bool realign = false; Chris@360: Chris@360: if (clipboardHasDifferentAlignment(v, from)) { Chris@360: Chris@360: QMessageBox::StandardButton button = Chris@918: QMessageBox::question(v->getView(), tr("Re-align pasted items?"), Chris@360: 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: QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, Chris@360: QMessageBox::Yes); Chris@360: Chris@360: if (button == QMessageBox::Cancel) { Chris@360: return false; Chris@360: } Chris@360: Chris@360: if (button == QMessageBox::Yes) { Chris@360: realign = true; Chris@360: } Chris@360: } Chris@360: Chris@1470: auto command = new ChangeEventsCommand(m_model.untyped, tr("Paste")); Chris@76: Chris@1423: for (EventVector::const_iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: Chris@904: sv_frame_t frame = 0; Chris@360: Chris@360: if (!realign) { Chris@360: Chris@360: frame = i->getFrame(); Chris@360: Chris@360: } else { Chris@360: Chris@1423: if (i->hasReferenceFrame()) { Chris@360: frame = i->getReferenceFrame(); Chris@360: frame = alignFromReference(v, frame); Chris@360: } else { Chris@360: frame = i->getFrame(); Chris@360: } Chris@76: } Chris@360: Chris@1426: Event p = *i; Chris@1426: Event newPoint = p; Chris@1426: if (!p.hasValue()) { Chris@1469: newPoint = newPoint.withValue((model->getValueMinimum() + Chris@1469: model->getValueMaximum()) / 2); Chris@1426: } Chris@1426: if (!p.hasDuration()) { Chris@904: sv_frame_t nextFrame = frame; Chris@1423: EventVector::const_iterator j = i; Chris@125: for (; j != points.end(); ++j) { Chris@125: if (j != i) break; Chris@125: } Chris@125: if (j != points.end()) { Chris@125: nextFrame = j->getFrame(); Chris@125: } Chris@125: if (nextFrame == frame) { Chris@1469: newPoint = newPoint.withDuration(model->getResolution()); Chris@125: } else { Chris@1426: newPoint = newPoint.withDuration(nextFrame - frame); Chris@125: } Chris@125: } Chris@76: Chris@1426: command->add(newPoint); Chris@76: } Chris@76: Chris@376: finish(command); Chris@125: return true; Chris@76: } Chris@76: Chris@507: void Chris@904: FlexiNoteLayer::addNoteOn(sv_frame_t frame, int pitch, int velocity) Chris@507: { Chris@1426: m_pendingNoteOns.insert(Event(frame, float(pitch), 0, Chris@1426: float(velocity / 127.0), "")); Chris@507: } Chris@507: Chris@507: void Chris@904: FlexiNoteLayer::addNoteOff(sv_frame_t frame, int pitch) Chris@507: { Chris@1426: for (NoteSet::iterator i = m_pendingNoteOns.begin(); Chris@507: i != m_pendingNoteOns.end(); ++i) { Chris@1426: Chris@1426: Event p = *i; Chris@1426: Chris@1426: if (lrintf(p.getValue()) == pitch) { Chris@507: m_pendingNoteOns.erase(i); Chris@1426: Event note = p.withDuration(frame - p.getFrame()); Chris@1470: auto c = new ChangeEventsCommand Chris@1470: (m_model.untyped, tr("Record Note")); Chris@1469: c->add(note); Chris@1469: // execute and bundle: Chris@1469: CommandHistory::getInstance()->addCommand(c, true, true); Chris@507: break; Chris@507: } Chris@507: } Chris@507: } Chris@507: Chris@507: void matthiasm@620: FlexiNoteLayer::abandonNoteOns() Chris@507: { Chris@507: m_pendingNoteOns.clear(); Chris@507: } Chris@507: Chris@287: int matthiasm@620: FlexiNoteLayer::getDefaultColourHint(bool darkbg, bool &impose) Chris@287: { Chris@287: impose = false; Chris@287: return ColourDatabase::getInstance()->getColourIndex Chris@287: (QString(darkbg ? "White" : "Black")); Chris@287: } Chris@287: Chris@316: void matthiasm@620: FlexiNoteLayer::toXml(QTextStream &stream, Chris@714: QString indent, QString extraAttributes) const Chris@30: { Chris@316: SingleColourLayer::toXml(stream, indent, extraAttributes + Chris@445: QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ") Chris@445: .arg(m_verticalScale) Chris@445: .arg(m_scaleMinimum) Chris@445: .arg(m_scaleMaximum)); Chris@30: } Chris@30: Chris@30: void matthiasm@620: FlexiNoteLayer::setProperties(const QXmlAttributes &attributes) Chris@30: { Chris@287: SingleColourLayer::setProperties(attributes); Chris@30: Chris@805: bool ok; Chris@30: VerticalScale scale = (VerticalScale) Chris@714: attributes.value("verticalScale").toInt(&ok); Chris@30: if (ok) setVerticalScale(scale); Chris@30: } Chris@30: matthiasm@651: void Chris@916: FlexiNoteLayer::setVerticalRangeToNoteRange(LayerGeometryProvider *v) matthiasm@651: { Chris@1469: auto model = ModelById::getAs(m_model); Chris@1469: if (!model) return; Chris@1469: Chris@904: double minf = std::numeric_limits::max(); Chris@904: double maxf = 0; matthiasm@656: bool hasNotes = 0; Chris@1469: EventVector allPoints = model->getAllEvents(); Chris@1426: for (EventVector::const_iterator i = allPoints.begin(); Chris@1426: i != allPoints.end(); ++i) { Chris@714: hasNotes = 1; Chris@1426: Event note = *i; Chris@1426: if (note.getValue() < minf) minf = note.getValue(); Chris@1426: if (note.getValue() > maxf) maxf = note.getValue(); matthiasm@651: } matthiasm@651: matthiasm@656: std::cerr << "min frequency:" << minf << ", max frequency: " << maxf << std::endl; matthiasm@656: matthiasm@656: if (hasNotes) { Chris@918: v->getView()->getLayer(1)->setDisplayExtents(minf*0.66,maxf*1.5); matthiasm@656: // MM: this is a hack because we rely on matthiasm@656: // * this layer being automatically aligned to layer 1 matthiasm@656: // * layer one is a log frequency layer. matthiasm@651: } matthiasm@651: } Chris@30: matthiasm@651: