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@376: #include "ColourDatabase.h" Chris@128: #include "view/View.h" Chris@701: Chris@692: #include "PianoScale.h" Chris@701: #include "LinearNumericalScale.h" Chris@701: #include "LogNumericalScale.h" Chris@30: matthiasm@620: #include "data/model/FlexiNoteModel.h" Chris@30: 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@30: matthiasm@620: FlexiNoteLayer::FlexiNoteLayer() : gyorgyf@646: SingleColourLayer(), gyorgyf@625: Chris@714: // m_model(0), Chris@714: // m_editing(false), Chris@714: // m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")), Chris@714: // m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")), Chris@714: // m_editingCommand(0), Chris@714: // m_verticalScale(AutoAlignScale), Chris@714: // m_scaleMinimum(0), Chris@714: // m_scaleMaximum(0) gyorgyf@625: gyorgyf@627: m_model(0), gyorgyf@628: m_editing(false), 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")), gyorgyf@627: m_editingCommand(0), matthiasm@634: m_verticalScale(AutoAlignScale), Chris@688: m_editMode(DragNote), gyorgyf@628: m_scaleMinimum(34), gyorgyf@658: m_scaleMaximum(77), gyorgyf@658: m_intelligentActions(true) Chris@30: { Chris@30: } Chris@30: Chris@30: void gyorgyf@626: FlexiNoteLayer::setModel(FlexiNoteModel *model) Chris@30: { Chris@30: if (m_model == model) return; Chris@30: m_model = model; Chris@30: Chris@320: connectSignals(m_model); Chris@30: gyorgyf@628: // m_scaleMinimum = 0; gyorgyf@628: // m_scaleMaximum = 0; Chris@439: 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@701: if (m_model) return m_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@100: if (m_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@100: if (m_model) { Chris@100: m_model->setScaleUnits Chris@100: (UnitDatabase::getInstance()->getUnitById(value)); Chris@100: emit modelChanged(); 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 matthiasm@620: FlexiNoteLayer::isLayerScrollable(const View *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@101: bool matthiasm@620: FlexiNoteLayer::getValueExtents(float &min, float &max, Chris@714: bool &logarithmic, QString &unit) const Chris@79: { Chris@79: if (!m_model) return false; Chris@79: min = m_model->getValueMinimum(); Chris@79: max = m_model->getValueMaximum(); Chris@101: Chris@105: if (shouldConvertMIDIToHz()) { Chris@105: unit = "Hz"; Chris@105: min = Pitch::getFrequencyForPitch(lrintf(min)); Chris@105: max = Pitch::getFrequencyForPitch(lrintf(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 matthiasm@620: FlexiNoteLayer::getDisplayExtents(float &min, float &max) const Chris@101: { matthiasm@623: if (!m_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@455: min = m_model->getValueMinimum(); Chris@455: max = m_model->getValueMaximum(); Chris@455: } else { Chris@455: min = m_scaleMinimum; Chris@455: max = m_scaleMaximum; Chris@439: } Chris@439: Chris@101: if (shouldConvertMIDIToHz()) { Chris@101: min = Pitch::getFrequencyForPitch(lrintf(min)); Chris@101: max = Pitch::getFrequencyForPitch(lrintf(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 matthiasm@620: FlexiNoteLayer::setDisplayExtents(float min, float max) Chris@439: { Chris@439: if (!m_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@439: max = min * 1.0001; 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@439: if (!m_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@439: if (!m_model) return 0; Chris@439: Chris@439: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@439: if (!mapper) return 0; Chris@439: Chris@439: float 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@439: if (!m_model) return; Chris@439: Chris@439: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@439: if (!mapper) return; Chris@439: Chris@439: float min, max; Chris@439: bool logarithmic; Chris@439: QString unit; Chris@439: getValueExtents(min, max, logarithmic, unit); Chris@439: Chris@439: float dmin, dmax; Chris@439: getDisplayExtents(dmin, dmax); Chris@439: Chris@439: float newdist = mapper->getValueForPosition(100 - step); Chris@439: Chris@439: float newmin, newmax; Chris@439: Chris@439: if (logarithmic) { Chris@439: Chris@439: // see SpectrogramLayer::setVerticalZoomStep Chris@439: Chris@439: newmax = (newdist + sqrtf(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@439: float 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@439: if (!m_model) return 0; Chris@439: Chris@439: RangeMapper *mapper; Chris@439: Chris@439: float min, max; Chris@439: bool logarithmic; Chris@439: QString unit; Chris@439: getValueExtents(min, max, logarithmic, unit); Chris@439: Chris@439: if (min == max) return 0; 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: matthiasm@620: FlexiNoteModel::PointList matthiasm@620: FlexiNoteLayer::getLocalPoints(View *v, int x) const Chris@30: { matthiasm@620: if (!m_model) return FlexiNoteModel::PointList(); Chris@30: Chris@44: long frame = v->getFrameForX(x); Chris@30: matthiasm@620: FlexiNoteModel::PointList onPoints = Chris@714: m_model->getPoints(frame); Chris@30: Chris@30: if (!onPoints.empty()) { Chris@714: return onPoints; Chris@30: } Chris@30: matthiasm@620: FlexiNoteModel::PointList prevPoints = Chris@714: m_model->getPreviousPoints(frame); matthiasm@620: FlexiNoteModel::PointList nextPoints = Chris@714: m_model->getNextPoints(frame); Chris@30: matthiasm@620: FlexiNoteModel::PointList usePoints = prevPoints; Chris@30: Chris@30: if (prevPoints.empty()) { Chris@714: usePoints = nextPoints; Chris@248: } else if (long(prevPoints.begin()->frame) < v->getStartFrame() && Chris@714: !(nextPoints.begin()->frame > v->getEndFrame())) { Chris@714: usePoints = nextPoints; Chris@248: } else if (long(nextPoints.begin()->frame) - frame < Chris@714: frame - long(prevPoints.begin()->frame)) { Chris@714: usePoints = nextPoints; Chris@30: } Chris@30: Chris@30: if (!usePoints.empty()) { Chris@714: int fuzz = 2; Chris@714: int px = v->getXForFrame(usePoints.begin()->frame); Chris@714: if ((px > x && px - x > fuzz) || Chris@714: (px < x && x - px > fuzz + 1)) { Chris@714: usePoints.clear(); Chris@714: } Chris@30: } Chris@30: Chris@30: return usePoints; Chris@30: } Chris@30: Chris@550: bool matthiasm@622: FlexiNoteLayer::getPointToDrag(View *v, int x, int y, FlexiNoteModel::Point &p) const Chris@550: { Chris@550: if (!m_model) return false; Chris@550: Chris@550: long frame = v->getFrameForX(x); Chris@550: matthiasm@620: FlexiNoteModel::PointList onPoints = m_model->getPoints(frame); Chris@550: if (onPoints.empty()) return false; Chris@550: Chris@682: // cerr << "frame " << frame << ": " << onPoints.size() << " candidate points" << endl; Chris@550: Chris@550: int nearestDistance = -1; Chris@550: matthiasm@620: for (FlexiNoteModel::PointList::const_iterator i = onPoints.begin(); Chris@550: i != onPoints.end(); ++i) { Chris@550: Chris@550: int distance = getYForValue(v, (*i).value) - y; Chris@550: if (distance < 0) distance = -distance; Chris@550: if (nearestDistance == -1 || distance < nearestDistance) { Chris@550: nearestDistance = distance; Chris@550: p = *i; Chris@550: } Chris@550: } Chris@550: Chris@550: return true; Chris@550: } Chris@550: gyorgyf@646: bool gyorgyf@646: FlexiNoteLayer::getNoteToEdit(View *v, int x, int y, FlexiNoteModel::Point &p) const gyorgyf@646: { gyorgyf@647: // GF: find the note that is closest to the cursor gyorgyf@646: if (!m_model) return false; gyorgyf@646: gyorgyf@646: long frame = v->getFrameForX(x); gyorgyf@646: gyorgyf@646: FlexiNoteModel::PointList onPoints = m_model->getPoints(frame); gyorgyf@646: if (onPoints.empty()) return false; gyorgyf@646: gyorgyf@646: // std::cerr << "frame " << frame << ": " << onPoints.size() << " candidate points" << std::endl; gyorgyf@646: gyorgyf@646: int nearestDistance = -1; gyorgyf@646: gyorgyf@646: for (FlexiNoteModel::PointList::const_iterator i = onPoints.begin(); gyorgyf@646: i != onPoints.end(); ++i) { gyorgyf@646: gyorgyf@646: int distance = getYForValue(v, (*i).value) - y; gyorgyf@646: if (distance < 0) distance = -distance; gyorgyf@646: if (nearestDistance == -1 || distance < nearestDistance) { gyorgyf@646: nearestDistance = distance; gyorgyf@646: p = *i; gyorgyf@646: } gyorgyf@646: } gyorgyf@646: gyorgyf@646: return true; gyorgyf@646: } gyorgyf@646: Chris@30: QString matthiasm@620: FlexiNoteLayer::getFeatureDescription(View *v, QPoint &pos) const Chris@30: { Chris@30: int x = pos.x(); Chris@30: Chris@30: if (!m_model || !m_model->getSampleRate()) return ""; Chris@30: matthiasm@620: FlexiNoteModel::PointList points = getLocalPoints(v, x); Chris@30: Chris@30: if (points.empty()) { Chris@714: if (!m_model->isReady()) { Chris@714: return tr("In progress"); Chris@714: } else { Chris@714: return tr("No local points"); Chris@714: } Chris@30: } Chris@30: matthiasm@620: FlexiNote note(0); matthiasm@620: FlexiNoteModel::PointList::iterator i; Chris@30: Chris@30: for (i = points.begin(); i != points.end(); ++i) { Chris@30: Chris@714: int y = getYForValue(v, i->value); Chris@714: int h = NOTE_HEIGHT; // GF: larger notes Chris@30: Chris@714: if (m_model->getValueQuantization() != 0.0) { Chris@714: h = y - getYForValue(v, i->value + m_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@30: RealTime rt = RealTime::frame2RealTime(note.frame, Chris@714: m_model->getSampleRate()); Chris@30: RealTime rd = RealTime::frame2RealTime(note.duration, Chris@714: m_model->getSampleRate()); Chris@30: Chris@101: QString pitchText; Chris@101: Chris@101: if (shouldConvertMIDIToHz()) { Chris@101: Chris@101: int mnote = lrintf(note.value); Chris@101: int cents = lrintf((note.value - mnote) * 100); Chris@101: float 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@101: .arg(note.value) Chris@544: .arg(Pitch::getPitchLabelForFrequency(note.value)) Chris@544: .arg(Pitch::getPitchForFrequency(note.value)); Chris@101: Chris@101: } else { Chris@234: pitchText = tr("%1 %2") Chris@701: .arg(note.value).arg(getScaleUnits()); Chris@101: } Chris@101: Chris@30: QString text; Chris@30: Chris@30: if (note.label == "") { 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@714: .arg(note.label); Chris@30: } Chris@30: Chris@44: pos = QPoint(v->getXForFrame(note.frame), Chris@714: getYForValue(v, note.value)); Chris@30: return text; Chris@30: } Chris@30: Chris@30: bool matthiasm@620: FlexiNoteLayer::snapToFeatureFrame(View *v, int &frame, Chris@714: size_t &resolution, Chris@714: SnapType snap) const Chris@30: { Chris@30: if (!m_model) { Chris@714: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@30: } Chris@30: Chris@30: resolution = m_model->getResolution(); matthiasm@620: FlexiNoteModel::PointList 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@714: frame = points.begin()->frame; Chris@714: return true; Chris@30: } Chris@30: Chris@30: points = m_model->getPoints(frame, frame); Chris@30: int snapped = frame; Chris@30: bool found = false; Chris@30: matthiasm@620: for (FlexiNoteModel::PointList::const_iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@30: Chris@714: if (snap == SnapRight) { Chris@30: Chris@714: if (i->frame > frame) { Chris@714: snapped = i->frame; Chris@714: found = true; Chris@714: break; Chris@714: } Chris@714: Chris@714: } else if (snap == SnapLeft) { Chris@714: Chris@714: if (i->frame <= frame) { Chris@714: snapped = i->frame; 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@714: FlexiNoteModel::PointList::const_iterator j = i; Chris@714: ++j; Chris@714: Chris@714: if (j == points.end()) { Chris@714: Chris@714: snapped = i->frame; Chris@714: found = true; Chris@714: break; Chris@714: Chris@714: } else if (j->frame >= frame) { Chris@714: Chris@714: if (j->frame - frame < frame - i->frame) { Chris@714: snapped = j->frame; Chris@714: } else { Chris@714: snapped = i->frame; Chris@714: } Chris@714: found = true; Chris@714: break; Chris@714: } gyorgyf@646: } Chris@30: } Chris@30: Chris@30: frame = snapped; Chris@30: return found; Chris@30: } Chris@30: Chris@101: void matthiasm@620: FlexiNoteLayer::getScaleExtents(View *v, float &min, float &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@101: min = m_model->getValueMinimum(); Chris@101: max = m_model->getValueMaximum(); Chris@42: Chris@101: if (shouldConvertMIDIToHz()) { Chris@101: min = Pitch::getFrequencyForPitch(lrintf(min)); Chris@101: max = Pitch::getFrequencyForPitch(lrintf(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@101: min = Pitch::getFrequencyForPitch(lrintf(min)); Chris@101: max = Pitch::getFrequencyForPitch(lrintf(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 matthiasm@620: FlexiNoteLayer::getYForValue(View *v, float val) const Chris@101: { Chris@101: float min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@101: int h = v->height(); 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@101: val = Pitch::getFrequencyForPitch(lrintf(val), Chris@101: lrintf((val - lrintf(val)) * 100)); 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@30: float matthiasm@620: FlexiNoteLayer::getValueForY(View *v, int y) const Chris@30: { Chris@101: float min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@44: int h = v->height(); Chris@30: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@101: float val = min + (float(h - y) * float(max - min)) / h; Chris@101: Chris@101: if (logarithmic) { Chris@197: val = powf(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: if (!m_model) return false; Chris@439: return (m_verticalScale == AutoAlignScale); Chris@439: } Chris@439: Chris@30: void matthiasm@620: FlexiNoteLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@30: { Chris@30: if (!m_model || !m_model->isOK()) return; Chris@30: Chris@30: int sampleRate = m_model->getSampleRate(); Chris@30: if (!sampleRate) return; Chris@30: matthiasm@620: // Profiler profiler("FlexiNoteLayer::paint", true); Chris@30: Chris@30: int x0 = rect.left(), x1 = rect.right(); Chris@44: long frame0 = v->getFrameForX(x0); Chris@44: long frame1 = v->getFrameForX(x1); Chris@30: matthiasm@620: FlexiNoteModel::PointList points(m_model->getPoints(frame0, frame1)); 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 " gyorgyf@646: // << m_model->getResolution() << " frames" << endl; Chris@30: Chris@30: float min = m_model->getValueMinimum(); Chris@30: float max = m_model->getValueMaximum(); Chris@30: if (max == min) max = min + 1.0; Chris@30: Chris@30: QPoint localPos; matthiasm@620: FlexiNoteModel::Point 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@620: for (FlexiNoteModel::PointList::const_iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@30: Chris@714: const FlexiNoteModel::Point &p(*i); Chris@30: Chris@714: int x = v->getXForFrame(p.frame); Chris@714: int y = getYForValue(v, p.value); Chris@714: int w = v->getXForFrame(p.frame + p.duration) - x; Chris@714: int h = NOTE_HEIGHT; //GF: larger notes gyorgyf@646: Chris@714: if (m_model->getValueQuantization() != 0.0) { Chris@714: h = y - getYForValue(v, p.value + m_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@714: // if (shouldIlluminate && Chris@714: // // "illuminatePoint == p" Chris@714: // !FlexiNoteModel::Point::Comparator()(illuminatePoint, p) && Chris@714: // !FlexiNoteModel::Point::Comparator()(p, illuminatePoint)) { Chris@714: // Chris@714: // paint.setPen(v->getForeground()); Chris@714: // paint.setBrush(v->getForeground()); Chris@714: // Chris@714: // QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits()); Chris@714: // v->drawVisibleText(paint, Chris@714: // x - paint.fontMetrics().width(vlabel) - 2, Chris@714: // y + paint.fontMetrics().height()/2 Chris@714: // - paint.fontMetrics().descent(), Chris@714: // vlabel, View::OutlinedText); Chris@714: // Chris@714: // QString hlabel = RealTime::frame2RealTime Chris@714: // (p.frame, m_model->getSampleRate()).toText(true).c_str(); Chris@714: // v->drawVisibleText(paint, Chris@714: // x, Chris@714: // y - h/2 - paint.fontMetrics().descent() - 2, Chris@714: // hlabel, View::OutlinedText); Chris@714: // } 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@702: FlexiNoteLayer::getVerticalScaleWidth(View *v, bool, QPainter &paint) const Chris@692: { Chris@697: if (!m_model || 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@695: FlexiNoteLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const Chris@692: { Chris@701: if (!m_model) return; Chris@701: Chris@701: QString unit; Chris@701: float min, max; Chris@701: bool logarithmic; Chris@701: Chris@701: int w = getVerticalScaleWidth(v, false, paint); Chris@701: int h = v->height(); 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 matthiasm@620: FlexiNoteLayer::drawStart(View *v, QMouseEvent *e) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@30: if (!m_model) return; Chris@30: Chris@44: long frame = v->getFrameForX(e->x()); Chris@30: if (frame < 0) frame = 0; Chris@30: frame = frame / m_model->getResolution() * m_model->getResolution(); Chris@30: Chris@44: float value = getValueForY(v, e->y()); Chris@30: matthiasm@620: m_editingPoint = FlexiNoteModel::Point(frame, value, 0, 0.8, tr("New Point")); Chris@30: m_originalPoint = m_editingPoint; Chris@30: Chris@376: if (m_editingCommand) finish(m_editingCommand); matthiasm@620: m_editingCommand = new FlexiNoteModel::EditCommand(m_model, Chris@714: tr("Draw Point")); Chris@30: m_editingCommand->addPoint(m_editingPoint); Chris@30: Chris@30: m_editing = true; Chris@30: } Chris@30: Chris@30: void matthiasm@620: FlexiNoteLayer::drawDrag(View *v, QMouseEvent *e) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@30: if (!m_model || !m_editing) return; Chris@30: Chris@44: long frame = v->getFrameForX(e->x()); Chris@30: if (frame < 0) frame = 0; Chris@30: frame = frame / m_model->getResolution() * m_model->getResolution(); Chris@30: Chris@101: float newValue = getValueForY(v, e->y()); Chris@101: Chris@101: long newFrame = m_editingPoint.frame; Chris@101: long 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@30: m_editingCommand->deletePoint(m_editingPoint); Chris@101: m_editingPoint.frame = newFrame; Chris@101: m_editingPoint.value = newValue; Chris@101: m_editingPoint.duration = newDuration; Chris@30: m_editingCommand->addPoint(m_editingPoint); Chris@30: } Chris@30: Chris@30: void matthiasm@620: FlexiNoteLayer::drawEnd(View *, QMouseEvent *) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl; Chris@30: if (!m_model || !m_editing) return; Chris@376: finish(m_editingCommand); Chris@30: m_editingCommand = 0; Chris@30: m_editing = false; Chris@30: } Chris@30: Chris@30: void matthiasm@620: FlexiNoteLayer::eraseStart(View *v, QMouseEvent *e) Chris@335: { Chris@335: if (!m_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@714: m_editingCommand = 0; Chris@335: } Chris@335: Chris@335: m_editing = true; Chris@335: } Chris@335: Chris@335: void matthiasm@620: FlexiNoteLayer::eraseDrag(View *v, QMouseEvent *e) Chris@335: { Chris@335: } Chris@335: Chris@335: void matthiasm@620: FlexiNoteLayer::eraseEnd(View *v, QMouseEvent *e) Chris@335: { Chris@335: if (!m_model || !m_editing) return; Chris@335: Chris@335: m_editing = false; Chris@335: matthiasm@620: FlexiNoteModel::Point p(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), p)) return; Chris@550: if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return; Chris@550: matthiasm@620: m_editingCommand = new FlexiNoteModel::EditCommand(m_model, tr("Erase Point")); Chris@335: Chris@335: m_editingCommand->deletePoint(m_editingPoint); Chris@335: Chris@376: finish(m_editingCommand); Chris@335: m_editingCommand = 0; Chris@335: m_editing = false; Chris@335: } Chris@335: Chris@335: void matthiasm@620: FlexiNoteLayer::editStart(View *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@30: if (!m_model) return; Chris@30: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; matthiasm@651: m_originalPoint = FlexiNote(m_editingPoint); gyorgyf@649: matthiasm@651: if (m_editMode == RightBoundary) { matthiasm@651: m_dragPointX = v->getXForFrame(m_editingPoint.frame + m_editingPoint.duration); gyorgyf@649: } else { gyorgyf@649: m_dragPointX = v->getXForFrame(m_editingPoint.frame); gyorgyf@649: } Chris@551: m_dragPointY = getYForValue(v, m_editingPoint.value); Chris@551: Chris@30: if (m_editingCommand) { Chris@714: finish(m_editingCommand); Chris@714: m_editingCommand = 0; Chris@30: } Chris@30: Chris@30: m_editing = true; Chris@551: m_dragStartX = e->x(); Chris@551: m_dragStartY = e->y(); matthiasm@651: matthiasm@651: long onset = m_originalPoint.frame; matthiasm@651: long offset = m_originalPoint.frame + m_originalPoint.duration - 1; matthiasm@651: matthiasm@651: m_greatestLeftNeighbourFrame = -1; matthiasm@651: m_smallestRightNeighbourFrame = std::numeric_limits::max(); matthiasm@651: matthiasm@651: for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin(); matthiasm@651: i != m_model->getPoints().end(); ++i) { matthiasm@651: FlexiNote currentNote = *i; matthiasm@651: matthiasm@651: // left boundary matthiasm@651: if (currentNote.frame + currentNote.duration - 1 < onset) { matthiasm@651: m_greatestLeftNeighbourFrame = currentNote.frame + currentNote.duration - 1; matthiasm@651: } matthiasm@651: matthiasm@651: // right boundary matthiasm@651: if (currentNote.frame > offset) { matthiasm@651: m_smallestRightNeighbourFrame = currentNote.frame; matthiasm@651: break; matthiasm@651: } matthiasm@651: } matthiasm@651: std::cerr << "note frame: " << onset << ", left boundary: " << m_greatestLeftNeighbourFrame << ", right boundary: " << m_smallestRightNeighbourFrame << std::endl; Chris@30: } Chris@30: Chris@30: void matthiasm@620: FlexiNoteLayer::editDrag(View *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@30: if (!m_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: matthiasm@657: long dragFrame = v->getFrameForX(newx); matthiasm@657: if (dragFrame < 0) dragFrame = 0; matthiasm@657: dragFrame = dragFrame / m_model->getResolution() * m_model->getResolution(); matthiasm@651: Chris@551: float value = getValueForY(v, newy); Chris@30: Chris@30: if (!m_editingCommand) { Chris@714: m_editingCommand = new FlexiNoteModel::EditCommand(m_model, Chris@714: tr("Drag Point")); Chris@30: } Chris@30: Chris@30: m_editingCommand->deletePoint(m_editingPoint); matthiasm@651: matthiasm@651: std::cerr << "edit mode: " << m_editMode << std::endl; matthiasm@657: matthiasm@651: switch (m_editMode) { Chris@714: case LeftBoundary : { Chris@714: // left Chris@714: if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1; Chris@714: // right Chris@714: if (m_intelligentActions && dragFrame >= m_originalPoint.frame + m_originalPoint.duration) { Chris@714: dragFrame = m_originalPoint.frame + m_originalPoint.duration - 1; matthiasm@651: } Chris@714: m_editingPoint.frame = dragFrame; Chris@714: m_editingPoint.duration = m_originalPoint.frame - dragFrame + m_originalPoint.duration; Chris@714: break; Chris@714: } Chris@714: case RightBoundary : { Chris@714: // left Chris@714: if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1; Chris@714: if (m_intelligentActions && dragFrame >= m_smallestRightNeighbourFrame) dragFrame = m_smallestRightNeighbourFrame - 1; Chris@714: m_editingPoint.duration = dragFrame - m_originalPoint.frame + 1; Chris@714: break; Chris@714: } Chris@714: case DragNote : { Chris@714: // left Chris@714: if (m_intelligentActions && dragFrame <= m_greatestLeftNeighbourFrame) dragFrame = m_greatestLeftNeighbourFrame + 1; Chris@714: // right Chris@714: if (m_intelligentActions && dragFrame + m_originalPoint.duration >= m_smallestRightNeighbourFrame) { Chris@714: dragFrame = m_smallestRightNeighbourFrame - m_originalPoint.duration; matthiasm@651: } Chris@714: m_editingPoint.frame = dragFrame; Chris@714: m_editingPoint.value = value; Chris@714: break; Chris@714: } gyorgyf@649: } Chris@30: m_editingCommand->addPoint(m_editingPoint); gyorgyf@649: std::cerr << "added new point(" << m_editingPoint.frame << "," << m_editingPoint.duration << ")" << std::endl; gyorgyf@649: Chris@30: } Chris@30: Chris@30: void gyorgyf@635: FlexiNoteLayer::editEnd(View *v, QMouseEvent *e) Chris@30: { matthiasm@620: // SVDEBUG << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl; gyorgyf@635: std::cerr << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl; matthiasm@656: Chris@30: if (!m_model || !m_editing) return; Chris@30: Chris@30: if (m_editingCommand) { Chris@30: Chris@714: QString newName = m_editingCommand->getName(); Chris@30: Chris@714: if (m_editingPoint.frame != m_originalPoint.frame) { Chris@714: if (m_editingPoint.value != m_originalPoint.value) { 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@30: m_editingCommand = 0; Chris@30: m_editing = false; Chris@30: } Chris@30: gyorgyf@635: void gyorgyf@635: FlexiNoteLayer::splitStart(View *v, QMouseEvent *e) gyorgyf@635: { gyorgyf@646: // GF: note splitting starts (!! remove printing soon) gyorgyf@646: std::cerr << "splitStart" << std::endl; gyorgyf@646: if (!m_model) return; gyorgyf@635: gyorgyf@635: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; gyorgyf@635: // m_originalPoint = m_editingPoint; gyorgyf@635: // gyorgyf@635: // m_dragPointX = v->getXForFrame(m_editingPoint.frame); gyorgyf@635: // m_dragPointY = getYForValue(v, m_editingPoint.value); gyorgyf@635: gyorgyf@635: if (m_editingCommand) { Chris@714: finish(m_editingCommand); Chris@714: m_editingCommand = 0; 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: gyorgyf@635: void gyorgyf@635: FlexiNoteLayer::splitEnd(View *v, QMouseEvent *e) gyorgyf@635: { gyorgyf@646: // GF: note splitting ends. (!! remove printing soon) gyorgyf@646: std::cerr << "splitEnd" << std::endl; matthiasm@651: if (!m_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: gyorgyf@649: // MM: simpler declaration matthiasm@648: FlexiNote note(0); gyorgyf@635: if (!getPointToDrag(v, e->x(), e->y(), note)) return; gyorgyf@635: gyorgyf@635: long frame = v->getFrameForX(e->x()); gyorgyf@635: matthiasm@648: int gap = 0; // MM: I prefer a gap of 0, but we can decide later matthiasm@648: gyorgyf@649: // MM: changed this a bit, to make it slightly clearer (// GF: nice changes!) matthiasm@648: FlexiNote newNote1(note.frame, note.value, matthiasm@648: frame - note.frame - gap, matthiasm@648: note.level, note.label); matthiasm@648: matthiasm@648: FlexiNote newNote2(frame, note.value, matthiasm@648: note.duration - newNote1.duration, matthiasm@648: note.level, note.label); gyorgyf@658: gyorgyf@658: if (m_intelligentActions) { gyorgyf@658: updateNoteValue(v,newNote1); gyorgyf@658: updateNoteValue(v,newNote2); gyorgyf@658: } gyorgyf@655: gyorgyf@635: FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand gyorgyf@635: (m_model, tr("Edit Point")); gyorgyf@635: command->deletePoint(note); gyorgyf@649: if ((e->modifiers() & Qt::ShiftModifier)) { gyorgyf@649: finish(command); gyorgyf@649: return; gyorgyf@649: } gyorgyf@635: command->addPoint(newNote1); gyorgyf@635: command->addPoint(newNote2); gyorgyf@635: finish(command); gyorgyf@646: gyorgyf@646: } gyorgyf@646: gyorgyf@655: void matthiasm@660: FlexiNoteLayer::addNote(View *v, QMouseEvent *e) matthiasm@660: { matthiasm@660: std::cerr << "addNote" << std::endl; matthiasm@660: if (!m_model) return; matthiasm@660: matthiasm@660: long duration = 10000; matthiasm@660: matthiasm@660: long frame = v->getFrameForX(e->x()); matthiasm@660: float value = getValueForY(v, e->y()); matthiasm@660: matthiasm@660: if (m_intelligentActions) { matthiasm@660: long smallestRightNeighbourFrame = 0; matthiasm@660: for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin(); matthiasm@660: i != m_model->getPoints().end(); ++i) { matthiasm@660: FlexiNote currentNote = *i; matthiasm@660: if (currentNote.frame > frame) { matthiasm@660: smallestRightNeighbourFrame = currentNote.frame; matthiasm@660: break; matthiasm@660: } matthiasm@660: } matthiasm@660: matthiasm@660: duration = std::min(smallestRightNeighbourFrame - frame + 1, duration); matthiasm@660: duration = (duration > 0) ? duration : 0; matthiasm@660: } matthiasm@660: matthiasm@660: if (!m_intelligentActions || matthiasm@660: m_model->getPoints(frame).empty() && duration > 0) matthiasm@660: { matthiasm@660: FlexiNote newNote(frame, value, duration, 100, "new note"); matthiasm@660: FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand Chris@714: (m_model, tr("Add Point")); matthiasm@660: command->addPoint(newNote); matthiasm@660: finish(command); matthiasm@660: } matthiasm@660: } matthiasm@660: matthiasm@660: matthiasm@660: void gyorgyf@655: FlexiNoteLayer::updateNoteValue(View *v, FlexiNoteModel::Point ¬e) const gyorgyf@655: { gyorgyf@655: //GF: update the note value conforming the median of pitch values in the underlying note layer gyorgyf@655: Layer *layer = v->getLayer(1); // GF: !!! gross assumption about correct layer order gyorgyf@655: SparseTimeValueModel *model = 0; gyorgyf@655: if (layer && layer->getModel()) gyorgyf@655: model = dynamic_cast(layer->getModel()); gyorgyf@655: gyorgyf@655: if (!model) return; gyorgyf@655: gyorgyf@655: std::cerr << model->getTypeName() << std::endl; gyorgyf@655: gyorgyf@655: SparseModel::PointList dataPoints = model->getPoints(note.frame, note.frame + note.duration); gyorgyf@655: if (dataPoints.empty()) return; gyorgyf@655: Chris@714: // std::cerr << "frame " << note.frame << ": " << dataPoints.size() << " candidate points" << std::endl; gyorgyf@655: Chris@714: std::vector pitchValues; gyorgyf@655: gyorgyf@655: for (SparseModel::PointList::const_iterator i = dataPoints.begin(); Chris@714: i != dataPoints.end(); ++i) { Chris@714: pitchValues.push_back((*i).value); gyorgyf@655: } gyorgyf@655: sort(pitchValues.begin(), pitchValues.end()); gyorgyf@655: size_t size = 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: } gyorgyf@655: gyorgyf@655: note.value = median; gyorgyf@655: } gyorgyf@655: gyorgyf@646: void gyorgyf@646: FlexiNoteLayer::mouseMoveEvent(View *v, QMouseEvent *e) gyorgyf@646: { gyorgyf@646: // GF: context sensitive cursors gyorgyf@646: // v->setCursor(Qt::ArrowCursor); gyorgyf@646: FlexiNoteModel::Point note(0); gyorgyf@646: if (!getNoteToEdit(v, e->x(), e->y(), note)) { gyorgyf@646: // v->setCursor(Qt::UpArrowCursor); gyorgyf@646: return; gyorgyf@646: } gyorgyf@646: gyorgyf@646: bool closeToLeft = false, closeToRight = false, closeToTop = false, closeToBottom = false; gyorgyf@646: getRelativeMousePosition(v, note, e->x(), e->y(), closeToLeft, closeToRight, closeToTop, closeToBottom); gyorgyf@646: // if (!closeToLeft) return; gyorgyf@646: // if (closeToTop) v->setCursor(Qt::SizeVerCursor); gyorgyf@649: matthiasm@651: if (closeToLeft) { v->setCursor(Qt::SizeHorCursor); m_editMode = LeftBoundary; return; } matthiasm@651: if (closeToRight) { v->setCursor(Qt::SizeHorCursor); m_editMode = RightBoundary; return; } matthiasm@651: if (closeToTop) { v->setCursor(Qt::CrossCursor); m_editMode = DragNote; return; } matthiasm@651: if (closeToBottom) { v->setCursor(Qt::UpArrowCursor); m_editMode = SplitNote; return; } gyorgyf@649: gyorgyf@646: v->setCursor(Qt::ArrowCursor); gyorgyf@646: gyorgyf@649: // std::cerr << "Mouse moved in edit mode over FlexiNoteLayer" << std::endl; gyorgyf@646: // v->setCursor(Qt::SizeHorCursor); gyorgyf@646: gyorgyf@646: } gyorgyf@646: gyorgyf@646: void gyorgyf@646: FlexiNoteLayer::getRelativeMousePosition(View *v, FlexiNoteModel::Point ¬e, int x, int y, bool &closeToLeft, bool &closeToRight, bool &closeToTop, bool &closeToBottom) const gyorgyf@646: { gyorgyf@649: // GF: TODO: consoloidate the tolerance values gyorgyf@646: if (!m_model) return; gyorgyf@646: matthiasm@651: int ctol = 0; gyorgyf@646: int noteStartX = v->getXForFrame(note.frame); gyorgyf@646: int noteEndX = v->getXForFrame(note.frame + note.duration); gyorgyf@646: int noteValueY = getYForValue(v,note.value); 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 matthiasm@620: FlexiNoteLayer::editOpen(View *v, QMouseEvent *e) Chris@70: { gyorgyf@633: std::cerr << "Opening note editor dialog" << std::endl; Chris@255: if (!m_model) return false; Chris@70: matthiasm@620: FlexiNoteModel::Point note(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), note)) return false; Chris@550: matthiasm@620: // FlexiNoteModel::Point note = *points.begin(); Chris@70: Chris@70: ItemEditDialog *dialog = new ItemEditDialog Chris@70: (m_model->getSampleRate(), Chris@70: ItemEditDialog::ShowTime | Chris@70: ItemEditDialog::ShowDuration | Chris@70: ItemEditDialog::ShowValue | Chris@100: ItemEditDialog::ShowText, Chris@701: getScaleUnits()); Chris@70: Chris@70: dialog->setFrameTime(note.frame); Chris@70: dialog->setValue(note.value); Chris@70: dialog->setFrameDuration(note.duration); Chris@70: dialog->setText(note.label); Chris@70: Chris@70: if (dialog->exec() == QDialog::Accepted) { Chris@70: matthiasm@620: FlexiNoteModel::Point newNote = note; Chris@70: newNote.frame = dialog->getFrameTime(); Chris@70: newNote.value = dialog->getValue(); Chris@70: newNote.duration = dialog->getFrameDuration(); Chris@70: newNote.label = dialog->getText(); Chris@70: matthiasm@620: FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand Chris@70: (m_model, tr("Edit Point")); Chris@70: command->deletePoint(note); Chris@70: command->addPoint(newNote); Chris@376: finish(command); Chris@70: } Chris@70: Chris@70: delete dialog; Chris@255: return true; Chris@70: } Chris@70: Chris@70: void matthiasm@620: FlexiNoteLayer::moveSelection(Selection s, size_t newStartFrame) Chris@43: { Chris@99: if (!m_model) return; Chris@99: matthiasm@620: FlexiNoteModel::EditCommand *command = Chris@714: new FlexiNoteModel::EditCommand(m_model, tr("Drag Selection")); Chris@43: matthiasm@620: FlexiNoteModel::PointList points = Chris@714: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@43: matthiasm@620: for (FlexiNoteModel::PointList::iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@43: Chris@714: if (s.contains(i->frame)) { Chris@714: FlexiNoteModel::Point newPoint(*i); Chris@714: newPoint.frame = i->frame + newStartFrame - s.getStartFrame(); Chris@714: command->deletePoint(*i); Chris@714: command->addPoint(newPoint); Chris@714: } 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@99: if (!m_model) return; Chris@99: matthiasm@620: FlexiNoteModel::EditCommand *command = Chris@714: new FlexiNoteModel::EditCommand(m_model, tr("Resize Selection")); Chris@43: matthiasm@620: FlexiNoteModel::PointList points = Chris@714: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@43: Chris@43: double ratio = Chris@714: double(newSize.getEndFrame() - newSize.getStartFrame()) / Chris@714: double(s.getEndFrame() - s.getStartFrame()); Chris@43: matthiasm@620: for (FlexiNoteModel::PointList::iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@43: Chris@714: if (s.contains(i->frame)) { Chris@43: Chris@714: double targetStart = i->frame; Chris@714: targetStart = newSize.getStartFrame() + Chris@714: double(targetStart - s.getStartFrame()) * ratio; Chris@43: Chris@714: double targetEnd = i->frame + i->duration; Chris@714: targetEnd = newSize.getStartFrame() + Chris@714: double(targetEnd - s.getStartFrame()) * ratio; Chris@43: Chris@714: FlexiNoteModel::Point newPoint(*i); Chris@714: newPoint.frame = lrint(targetStart); Chris@714: newPoint.duration = lrint(targetEnd - targetStart); Chris@714: command->deletePoint(*i); Chris@714: command->addPoint(newPoint); Chris@714: } Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@76: void matthiasm@620: FlexiNoteLayer::deleteSelection(Selection s) Chris@76: { Chris@99: if (!m_model) return; Chris@99: matthiasm@620: FlexiNoteModel::EditCommand *command = Chris@714: new FlexiNoteModel::EditCommand(m_model, tr("Delete Selected Points")); Chris@76: matthiasm@620: FlexiNoteModel::PointList points = Chris@714: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@76: matthiasm@620: for (FlexiNoteModel::PointList::iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@76: Chris@76: if (s.contains(i->frame)) { Chris@76: command->deletePoint(*i); Chris@76: } Chris@76: } Chris@76: Chris@376: finish(command); Chris@76: } Chris@76: Chris@76: void matthiasm@620: FlexiNoteLayer::copy(View *v, Selection s, Clipboard &to) Chris@76: { Chris@99: if (!m_model) return; Chris@99: matthiasm@620: FlexiNoteModel::PointList points = Chris@714: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@76: matthiasm@620: for (FlexiNoteModel::PointList::iterator i = points.begin(); Chris@714: i != points.end(); ++i) { Chris@714: if (s.contains(i->frame)) { Chris@335: Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label); Chris@360: point.setReferenceFrame(alignToReference(v, i->frame)); Chris@76: to.addPoint(point); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@125: bool matthiasm@620: FlexiNoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */) Chris@76: { Chris@125: if (!m_model) return false; Chris@99: Chris@76: const Clipboard::PointList &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@360: QMessageBox::question(v, 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: matthiasm@620: FlexiNoteModel::EditCommand *command = Chris@714: new FlexiNoteModel::EditCommand(m_model, tr("Paste")); Chris@76: Chris@76: for (Clipboard::PointList::const_iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@76: Chris@76: if (!i->haveFrame()) continue; Chris@76: size_t frame = 0; Chris@360: Chris@360: if (!realign) { Chris@360: Chris@360: frame = i->getFrame(); Chris@360: Chris@360: } else { Chris@360: Chris@360: if (i->haveReferenceFrame()) { 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: matthiasm@620: FlexiNoteModel::Point newPoint(frame); Chris@76: Chris@76: if (i->haveLabel()) newPoint.label = i->getLabel(); Chris@76: if (i->haveValue()) newPoint.value = i->getValue(); Chris@76: else newPoint.value = (m_model->getValueMinimum() + Chris@76: m_model->getValueMaximum()) / 2; Chris@335: if (i->haveLevel()) newPoint.level = i->getLevel(); Chris@76: if (i->haveDuration()) newPoint.duration = i->getDuration(); Chris@125: else { Chris@125: size_t nextFrame = frame; Chris@125: Clipboard::PointList::const_iterator j = i; Chris@125: for (; j != points.end(); ++j) { Chris@125: if (!j->haveFrame()) continue; 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@125: newPoint.duration = m_model->getResolution(); Chris@125: } else { Chris@125: newPoint.duration = nextFrame - frame; Chris@125: } Chris@125: } Chris@76: Chris@76: command->addPoint(newPoint); Chris@76: } Chris@76: Chris@376: finish(command); Chris@125: return true; Chris@76: } Chris@76: Chris@507: void matthiasm@620: FlexiNoteLayer::addNoteOn(long frame, int pitch, int velocity) Chris@507: { matthiasm@620: m_pendingNoteOns.insert(FlexiNote(frame, pitch, 0, float(velocity) / 127.0, "")); Chris@507: } Chris@507: Chris@507: void matthiasm@620: FlexiNoteLayer::addNoteOff(long frame, int pitch) Chris@507: { matthiasm@620: for (FlexiNoteSet::iterator i = m_pendingNoteOns.begin(); Chris@507: i != m_pendingNoteOns.end(); ++i) { Chris@507: if (lrintf((*i).value) == pitch) { matthiasm@620: FlexiNote note(*i); Chris@507: m_pendingNoteOns.erase(i); Chris@507: note.duration = frame - note.frame; Chris@507: if (m_model) { matthiasm@620: FlexiNoteModel::AddPointCommand *c = new FlexiNoteModel::AddPointCommand matthiasm@620: (m_model, note, tr("Record FlexiNote")); Chris@507: // execute and bundle: Chris@507: CommandHistory::getInstance()->addCommand(c, true, true); Chris@507: } 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@445: bool ok, alsoOk; Chris@30: VerticalScale scale = (VerticalScale) Chris@714: attributes.value("verticalScale").toInt(&ok); Chris@30: if (ok) setVerticalScale(scale); Chris@445: Chris@445: float min = attributes.value("scaleMinimum").toFloat(&ok); Chris@445: float max = attributes.value("scaleMaximum").toFloat(&alsoOk); Chris@670: // if (ok && alsoOk && min != max) setDisplayExtents(min, max); Chris@30: } Chris@30: matthiasm@651: void matthiasm@656: FlexiNoteLayer::setVerticalRangeToNoteRange(View *v) matthiasm@651: { matthiasm@656: float minf = std::numeric_limits::max(); matthiasm@651: float maxf = 0; matthiasm@656: bool hasNotes = 0; matthiasm@651: for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin(); matthiasm@651: i != m_model->getPoints().end(); ++i) { Chris@714: hasNotes = 1; Chris@714: FlexiNote note = *i; Chris@714: if (note.value < minf) minf = note.value; Chris@714: if (note.value > maxf) maxf = note.value; matthiasm@651: } matthiasm@651: matthiasm@656: std::cerr << "min frequency:" << minf << ", max frequency: " << maxf << std::endl; matthiasm@656: matthiasm@656: if (hasNotes) { matthiasm@659: v->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: