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: Chris@30: #include "NoteLayer.h" Chris@30: Chris@30: #include "base/Model.h" Chris@30: #include "base/RealTime.h" Chris@30: #include "base/Profiler.h" Chris@30: #include "base/Pitch.h" Chris@30: #include "base/View.h" Chris@30: Chris@30: #include "model/NoteModel.h" Chris@30: Chris@70: #include "widgets/ItemEditDialog.h" Chris@70: Chris@42: #include "SpectrogramLayer.h" // for optional frequency alignment Chris@42: Chris@30: #include Chris@30: #include Chris@30: #include Chris@30: Chris@30: #include Chris@30: #include Chris@30: Chris@44: NoteLayer::NoteLayer() : Chris@44: Layer(), Chris@30: m_model(0), Chris@30: m_editing(false), Chris@30: m_originalPoint(0, 0.0, 0, tr("New Point")), Chris@30: m_editingPoint(0, 0.0, 0, tr("New Point")), Chris@30: m_editingCommand(0), Chris@30: m_colour(Qt::black), Chris@30: m_verticalScale(MinMaxRangeScale) Chris@30: { Chris@44: Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::setModel(NoteModel *model) Chris@30: { Chris@30: if (m_model == model) return; Chris@30: m_model = model; Chris@30: Chris@30: connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged())); Chris@30: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@30: this, SIGNAL(modelChanged(size_t, size_t))); Chris@30: Chris@30: connect(m_model, SIGNAL(completionChanged()), Chris@30: this, SIGNAL(modelCompletionChanged())); Chris@30: Chris@30: std::cerr << "NoteLayer::setModel(" << model << ")" << std::endl; Chris@30: Chris@30: emit modelReplaced(); Chris@30: } Chris@30: Chris@30: Layer::PropertyList Chris@30: NoteLayer::getProperties() const Chris@30: { Chris@30: PropertyList list; Chris@30: list.push_back(tr("Colour")); Chris@30: list.push_back(tr("Vertical Scale")); Chris@30: return list; Chris@30: } Chris@30: Chris@30: Layer::PropertyType Chris@30: NoteLayer::getPropertyType(const PropertyName &) const Chris@30: { Chris@30: return ValueProperty; Chris@30: } Chris@30: Chris@30: int Chris@30: NoteLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@30: int *min, int *max) const Chris@30: { Chris@30: //!!! factor this colour handling stuff out into a colour manager class Chris@30: Chris@30: int deft = 0; Chris@30: Chris@30: if (name == tr("Colour")) { Chris@30: Chris@30: if (min) *min = 0; Chris@30: if (max) *max = 5; Chris@30: Chris@30: if (m_colour == Qt::black) deft = 0; Chris@30: else if (m_colour == Qt::darkRed) deft = 1; Chris@30: else if (m_colour == Qt::darkBlue) deft = 2; Chris@30: else if (m_colour == Qt::darkGreen) deft = 3; Chris@30: else if (m_colour == QColor(200, 50, 255)) deft = 4; Chris@30: else if (m_colour == QColor(255, 150, 50)) deft = 5; Chris@30: Chris@30: } else if (name == tr("Vertical Scale")) { Chris@30: Chris@30: if (min) *min = 0; Chris@30: if (max) *max = 2; Chris@30: Chris@30: deft = int(m_verticalScale); Chris@30: Chris@30: } else { Chris@30: Chris@30: deft = Layer::getPropertyRangeAndValue(name, min, max); Chris@30: } Chris@30: Chris@30: return deft; Chris@30: } Chris@30: Chris@30: QString Chris@30: NoteLayer::getPropertyValueLabel(const PropertyName &name, Chris@30: int value) const Chris@30: { Chris@30: if (name == tr("Colour")) { Chris@30: switch (value) { Chris@30: default: Chris@30: case 0: return tr("Black"); Chris@30: case 1: return tr("Red"); Chris@30: case 2: return tr("Blue"); Chris@30: case 3: return tr("Green"); Chris@30: case 4: return tr("Purple"); Chris@30: case 5: return tr("Orange"); Chris@30: } Chris@30: } else if (name == tr("Vertical Scale")) { Chris@30: switch (value) { Chris@30: default: Chris@30: case 0: return tr("Note Range In Use"); Chris@30: case 1: return tr("MIDI Note Range"); Chris@30: case 2: return tr("Frequency"); Chris@30: } Chris@30: } Chris@30: return tr(""); Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::setProperty(const PropertyName &name, int value) Chris@30: { Chris@30: if (name == tr("Colour")) { Chris@30: switch (value) { Chris@30: default: Chris@30: case 0: setBaseColour(Qt::black); break; Chris@30: case 1: setBaseColour(Qt::darkRed); break; Chris@30: case 2: setBaseColour(Qt::darkBlue); break; Chris@30: case 3: setBaseColour(Qt::darkGreen); break; Chris@30: case 4: setBaseColour(QColor(200, 50, 255)); break; Chris@30: case 5: setBaseColour(QColor(255, 150, 50)); break; Chris@30: } Chris@30: } else if (name == tr("Vertical Scale")) { Chris@30: setVerticalScale(VerticalScale(value)); Chris@30: } Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::setBaseColour(QColor colour) Chris@30: { Chris@30: if (m_colour == colour) return; Chris@30: m_colour = colour; Chris@30: emit layerParametersChanged(); Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::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@44: NoteLayer::isLayerScrollable(const View *v) const Chris@30: { Chris@30: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@30: } Chris@30: Chris@30: NoteModel::PointList Chris@44: NoteLayer::getLocalPoints(View *v, int x) const Chris@30: { Chris@30: if (!m_model) return NoteModel::PointList(); Chris@30: Chris@44: long frame = v->getFrameForX(x); Chris@30: Chris@30: NoteModel::PointList onPoints = Chris@30: m_model->getPoints(frame); Chris@30: Chris@30: if (!onPoints.empty()) { Chris@30: return onPoints; Chris@30: } Chris@30: Chris@30: NoteModel::PointList prevPoints = Chris@30: m_model->getPreviousPoints(frame); Chris@30: NoteModel::PointList nextPoints = Chris@30: m_model->getNextPoints(frame); Chris@30: Chris@30: NoteModel::PointList usePoints = prevPoints; Chris@30: Chris@30: if (prevPoints.empty()) { Chris@30: usePoints = nextPoints; Chris@44: } else if (prevPoints.begin()->frame < v->getStartFrame() && Chris@44: !(nextPoints.begin()->frame > v->getEndFrame())) { Chris@30: usePoints = nextPoints; Chris@30: } else if (nextPoints.begin()->frame - frame < Chris@30: frame - prevPoints.begin()->frame) { Chris@30: usePoints = nextPoints; Chris@30: } Chris@30: Chris@30: if (!usePoints.empty()) { Chris@30: int fuzz = 2; Chris@44: int px = v->getXForFrame(usePoints.begin()->frame); Chris@30: if ((px > x && px - x > fuzz) || Chris@30: (px < x && x - px > fuzz + 1)) { Chris@30: usePoints.clear(); Chris@30: } Chris@30: } Chris@30: Chris@30: return usePoints; Chris@30: } Chris@30: Chris@30: QString Chris@44: NoteLayer::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: Chris@44: NoteModel::PointList points = getLocalPoints(v, x); Chris@30: Chris@30: if (points.empty()) { Chris@30: if (!m_model->isReady()) { Chris@30: return tr("In progress"); Chris@30: } else { Chris@30: return tr("No local points"); Chris@30: } Chris@30: } Chris@30: Chris@30: Note note(0); Chris@30: NoteModel::PointList::iterator i; Chris@30: Chris@30: for (i = points.begin(); i != points.end(); ++i) { Chris@30: Chris@44: int y = getYForValue(v, i->value); Chris@30: int h = 3; Chris@30: Chris@30: if (m_model->getValueQuantization() != 0.0) { Chris@44: h = y - getYForValue(v, i->value + m_model->getValueQuantization()); Chris@30: if (h < 3) h = 3; Chris@30: } Chris@30: Chris@30: if (pos.y() >= y - h && pos.y() <= y) { Chris@30: note = *i; Chris@30: break; Chris@30: } 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@30: m_model->getSampleRate()); Chris@30: RealTime rd = RealTime::frame2RealTime(note.duration, Chris@30: m_model->getSampleRate()); Chris@30: Chris@30: QString text; Chris@30: Chris@30: if (note.label == "") { Chris@30: text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label")) Chris@30: .arg(rt.toText(true).c_str()) Chris@30: .arg(note.value) Chris@30: .arg(rd.toText(true).c_str()); Chris@30: } else { Chris@30: text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4")) Chris@30: .arg(rt.toText(true).c_str()) Chris@30: .arg(note.value) Chris@30: .arg(rd.toText(true).c_str()) Chris@30: .arg(note.label); Chris@30: } Chris@30: Chris@44: pos = QPoint(v->getXForFrame(note.frame), Chris@44: getYForValue(v, note.value)); Chris@30: return text; Chris@30: } Chris@30: Chris@30: bool Chris@44: NoteLayer::snapToFeatureFrame(View *v, int &frame, Chris@44: size_t &resolution, Chris@44: SnapType snap) const Chris@30: { Chris@30: if (!m_model) { Chris@44: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@30: } Chris@30: Chris@30: resolution = m_model->getResolution(); Chris@30: NoteModel::PointList points; Chris@30: Chris@30: if (snap == SnapNeighbouring) { Chris@30: Chris@44: points = getLocalPoints(v, v->getXForFrame(frame)); Chris@30: if (points.empty()) return false; Chris@30: frame = points.begin()->frame; Chris@30: 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: Chris@30: for (NoteModel::PointList::const_iterator i = points.begin(); Chris@30: i != points.end(); ++i) { Chris@30: Chris@30: if (snap == SnapRight) { Chris@30: Chris@30: if (i->frame > frame) { Chris@30: snapped = i->frame; Chris@30: found = true; Chris@30: break; Chris@30: } Chris@30: Chris@30: } else if (snap == SnapLeft) { Chris@30: Chris@30: if (i->frame <= frame) { Chris@30: snapped = i->frame; Chris@30: found = true; // don't break, as the next may be better Chris@30: } else { Chris@30: break; Chris@30: } Chris@30: Chris@30: } else { // nearest Chris@30: Chris@30: NoteModel::PointList::const_iterator j = i; Chris@30: ++j; Chris@30: Chris@30: if (j == points.end()) { Chris@30: Chris@30: snapped = i->frame; Chris@30: found = true; Chris@30: break; Chris@30: Chris@30: } else if (j->frame >= frame) { Chris@30: Chris@30: if (j->frame - frame < frame - i->frame) { Chris@30: snapped = j->frame; Chris@30: } else { Chris@30: snapped = i->frame; Chris@30: } Chris@30: found = true; Chris@30: break; Chris@30: } Chris@30: } Chris@30: } Chris@30: Chris@30: frame = snapped; Chris@30: return found; Chris@30: } Chris@30: Chris@30: int Chris@44: NoteLayer::getYForValue(View *v, float value) const Chris@30: { Chris@44: float min, max, h = v->height(); Chris@42: Chris@30: switch (m_verticalScale) { Chris@30: Chris@30: case MIDIRangeScale: Chris@30: min = 0.0; Chris@30: max = 127.0; Chris@30: break; Chris@30: Chris@30: case MinMaxRangeScale: Chris@30: min = m_model->getValueMinimum(); Chris@30: max = m_model->getValueMaximum(); Chris@30: break; Chris@30: Chris@30: case FrequencyScale: Chris@42: Chris@42: value = Pitch::getFrequencyForPitch(lrintf(value), Chris@42: value - lrintf(value)); Chris@42: Chris@42: // If we have a spectrogram layer on the same view as us, align Chris@42: // ourselves with it... Chris@44: for (int i = 0; i < v->getLayerCount(); ++i) { Chris@42: SpectrogramLayer *spectrogram = dynamic_cast Chris@44: (v->getLayer(i)); Chris@42: if (spectrogram) { Chris@44: return spectrogram->getYForFrequency(v, value); Chris@42: } Chris@42: } Chris@42: Chris@42: // ...otherwise just interpolate Chris@30: std::cerr << "FrequencyScale: value in = " << value << std::endl; Chris@30: min = m_model->getValueMinimum(); Chris@30: min = Pitch::getFrequencyForPitch(lrintf(min), min - lrintf(min)); Chris@30: max = m_model->getValueMaximum(); Chris@30: max = Pitch::getFrequencyForPitch(lrintf(max), max - lrintf(max)); Chris@30: std::cerr << "FrequencyScale: min = " << min << ", max = " << max << ", value = " << value << std::endl; Chris@30: break; Chris@30: } Chris@30: Chris@30: if (max < min) max = min; Chris@30: max = max + 1.0; Chris@30: Chris@30: return int(h - ((value - min) * h) / (max - min)) - 1; Chris@30: } Chris@30: Chris@30: float Chris@44: NoteLayer::getValueForY(View *v, int y) const Chris@30: { Chris@66: //!!! Chris@66: 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@44: int h = v->height(); Chris@30: Chris@30: return min + (float(h - y) * float(max - min)) / h; Chris@30: } Chris@30: Chris@30: void Chris@44: NoteLayer::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: Chris@30: // Profiler profiler("NoteLayer::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: Chris@30: NoteModel::PointList points(m_model->getPoints(frame0, frame1)); Chris@30: if (points.empty()) return; Chris@30: Chris@30: paint.setPen(m_colour); Chris@30: Chris@30: QColor brushColour(m_colour); Chris@30: brushColour.setAlpha(80); Chris@30: Chris@30: // std::cerr << "NoteLayer::paint: resolution is " Chris@30: // << m_model->getResolution() << " frames" << std::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@44: int origin = int(nearbyint(v->height() - Chris@44: (-min * v->height()) / (max - min))); Chris@30: Chris@30: QPoint localPos; Chris@30: long illuminateFrame = -1; Chris@30: Chris@44: if (v->shouldIlluminateLocalFeatures(this, localPos)) { Chris@30: NoteModel::PointList localPoints = Chris@44: getLocalPoints(v, localPos.x()); Chris@30: if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame; Chris@30: } Chris@30: Chris@30: paint.save(); Chris@30: paint.setRenderHint(QPainter::Antialiasing, false); Chris@30: Chris@30: for (NoteModel::PointList::const_iterator i = points.begin(); Chris@30: i != points.end(); ++i) { Chris@30: Chris@30: const NoteModel::Point &p(*i); Chris@30: Chris@44: int x = v->getXForFrame(p.frame); Chris@44: int y = getYForValue(v, p.value); Chris@44: int w = v->getXForFrame(p.frame + p.duration) - x; Chris@30: int h = 3; Chris@30: Chris@30: if (m_model->getValueQuantization() != 0.0) { Chris@44: h = y - getYForValue(v, p.value + m_model->getValueQuantization()); Chris@30: if (h < 3) h = 3; Chris@30: } Chris@30: Chris@30: if (w < 1) w = 1; Chris@30: paint.setPen(m_colour); Chris@30: paint.setBrush(brushColour); Chris@30: Chris@30: if (illuminateFrame == p.frame) { Chris@30: if (localPos.y() >= y - h && localPos.y() < y) { Chris@30: paint.setPen(Qt::black);//!!! Chris@30: paint.setBrush(Qt::black);//!!! Chris@30: } Chris@30: } Chris@30: Chris@30: paint.drawRect(x, y - h, w, h); Chris@30: Chris@30: /// if (p.label != "") { Chris@30: /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label); Chris@30: /// } Chris@30: } Chris@30: Chris@30: paint.restore(); Chris@30: } Chris@30: Chris@30: void Chris@44: NoteLayer::drawStart(View *v, QMouseEvent *e) Chris@30: { Chris@30: std::cerr << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::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: Chris@30: m_editingPoint = NoteModel::Point(frame, value, 0, tr("New Point")); Chris@30: m_originalPoint = m_editingPoint; Chris@30: Chris@30: if (m_editingCommand) m_editingCommand->finish(); Chris@30: m_editingCommand = new NoteModel::EditCommand(m_model, Chris@30: tr("Draw Point")); Chris@30: m_editingCommand->addPoint(m_editingPoint); Chris@30: Chris@30: m_editing = true; Chris@30: } Chris@30: Chris@30: void Chris@44: NoteLayer::drawDrag(View *v, QMouseEvent *e) Chris@30: { Chris@30: std::cerr << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::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@44: float value = getValueForY(v, e->y()); Chris@30: Chris@30: m_editingCommand->deletePoint(m_editingPoint); Chris@30: m_editingPoint.frame = frame; Chris@30: m_editingPoint.value = value; Chris@30: m_editingCommand->addPoint(m_editingPoint); Chris@30: } Chris@30: Chris@30: void Chris@44: NoteLayer::drawEnd(View *v, QMouseEvent *e) Chris@30: { Chris@30: std::cerr << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl; Chris@30: if (!m_model || !m_editing) return; Chris@30: m_editingCommand->finish(); Chris@30: m_editingCommand = 0; Chris@30: m_editing = false; Chris@30: } Chris@30: Chris@30: void Chris@44: NoteLayer::editStart(View *v, QMouseEvent *e) Chris@30: { Chris@30: std::cerr << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl; Chris@30: Chris@30: if (!m_model) return; Chris@30: Chris@44: NoteModel::PointList points = getLocalPoints(v, e->x()); Chris@30: if (points.empty()) return; Chris@30: Chris@30: m_editingPoint = *points.begin(); Chris@30: m_originalPoint = m_editingPoint; Chris@30: Chris@30: if (m_editingCommand) { Chris@30: m_editingCommand->finish(); Chris@30: m_editingCommand = 0; Chris@30: } Chris@30: Chris@30: m_editing = true; Chris@30: } Chris@30: Chris@30: void Chris@44: NoteLayer::editDrag(View *v, QMouseEvent *e) Chris@30: { Chris@30: std::cerr << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::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@44: float value = getValueForY(v, e->y()); Chris@30: Chris@30: if (!m_editingCommand) { Chris@30: m_editingCommand = new NoteModel::EditCommand(m_model, Chris@30: tr("Drag Point")); Chris@30: } Chris@30: Chris@30: m_editingCommand->deletePoint(m_editingPoint); Chris@30: m_editingPoint.frame = frame; Chris@30: m_editingPoint.value = value; Chris@30: m_editingCommand->addPoint(m_editingPoint); Chris@30: } Chris@30: Chris@30: void Chris@44: NoteLayer::editEnd(View *v, QMouseEvent *e) Chris@30: { Chris@30: std::cerr << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl; Chris@30: if (!m_model || !m_editing) return; Chris@30: Chris@30: if (m_editingCommand) { Chris@30: Chris@30: QString newName = m_editingCommand->getName(); Chris@30: Chris@30: if (m_editingPoint.frame != m_originalPoint.frame) { Chris@30: if (m_editingPoint.value != m_originalPoint.value) { Chris@30: newName = tr("Edit Point"); Chris@30: } else { Chris@30: newName = tr("Relocate Point"); Chris@30: } Chris@30: } else { Chris@30: newName = tr("Change Point Value"); Chris@30: } Chris@30: Chris@30: m_editingCommand->setName(newName); Chris@30: m_editingCommand->finish(); Chris@30: } Chris@30: Chris@30: m_editingCommand = 0; Chris@30: m_editing = false; Chris@30: } Chris@30: Chris@43: void Chris@70: NoteLayer::editOpen(View *v, QMouseEvent *e) Chris@70: { Chris@70: if (!m_model) return; Chris@70: Chris@70: NoteModel::PointList points = getLocalPoints(v, e->x()); Chris@70: if (points.empty()) return; Chris@70: Chris@70: NoteModel::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@70: ItemEditDialog::ShowText); 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: Chris@70: NoteModel::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: Chris@70: NoteModel::EditCommand *command = new NoteModel::EditCommand Chris@70: (m_model, tr("Edit Point")); Chris@70: command->deletePoint(note); Chris@70: command->addPoint(newNote); Chris@70: command->finish(); Chris@70: } Chris@70: Chris@70: delete dialog; Chris@70: } Chris@70: Chris@70: void Chris@43: NoteLayer::moveSelection(Selection s, size_t newStartFrame) Chris@43: { Chris@43: NoteModel::EditCommand *command = Chris@43: new NoteModel::EditCommand(m_model, tr("Drag Selection")); Chris@43: Chris@43: NoteModel::PointList points = Chris@43: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@43: Chris@43: for (NoteModel::PointList::iterator i = points.begin(); Chris@43: i != points.end(); ++i) { Chris@43: Chris@43: if (s.contains(i->frame)) { Chris@43: NoteModel::Point newPoint(*i); Chris@43: newPoint.frame = i->frame + newStartFrame - s.getStartFrame(); Chris@43: command->deletePoint(*i); Chris@43: command->addPoint(newPoint); Chris@43: } Chris@43: } Chris@43: Chris@43: command->finish(); Chris@43: } Chris@43: Chris@43: void Chris@43: NoteLayer::resizeSelection(Selection s, Selection newSize) Chris@43: { Chris@43: NoteModel::EditCommand *command = Chris@43: new NoteModel::EditCommand(m_model, tr("Resize Selection")); Chris@43: Chris@43: NoteModel::PointList points = Chris@43: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@43: Chris@43: double ratio = Chris@43: double(newSize.getEndFrame() - newSize.getStartFrame()) / Chris@43: double(s.getEndFrame() - s.getStartFrame()); Chris@43: Chris@43: for (NoteModel::PointList::iterator i = points.begin(); Chris@43: i != points.end(); ++i) { Chris@43: Chris@43: if (s.contains(i->frame)) { Chris@43: Chris@43: double targetStart = i->frame; Chris@43: targetStart = newSize.getStartFrame() + Chris@43: double(targetStart - s.getStartFrame()) * ratio; Chris@43: Chris@43: double targetEnd = i->frame + i->duration; Chris@43: targetEnd = newSize.getStartFrame() + Chris@43: double(targetEnd - s.getStartFrame()) * ratio; Chris@43: Chris@43: NoteModel::Point newPoint(*i); Chris@43: newPoint.frame = lrint(targetStart); Chris@43: newPoint.duration = lrint(targetEnd - targetStart); Chris@43: command->deletePoint(*i); Chris@43: command->addPoint(newPoint); Chris@43: } Chris@43: } Chris@43: Chris@43: command->finish(); Chris@43: } Chris@43: Chris@30: QString Chris@30: NoteLayer::toXmlString(QString indent, QString extraAttributes) const Chris@30: { Chris@30: return Layer::toXmlString(indent, extraAttributes + Chris@30: QString(" colour=\"%1\" verticalScale=\"%2\"") Chris@30: .arg(encodeColour(m_colour)).arg(m_verticalScale)); Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::setProperties(const QXmlAttributes &attributes) Chris@30: { Chris@30: QString colourSpec = attributes.value("colour"); Chris@30: if (colourSpec != "") { Chris@30: QColor colour(colourSpec); Chris@30: if (colour.isValid()) { Chris@30: setBaseColour(QColor(colourSpec)); Chris@30: } Chris@30: } Chris@30: Chris@30: bool ok; Chris@30: VerticalScale scale = (VerticalScale) Chris@30: attributes.value("verticalScale").toInt(&ok); Chris@30: if (ok) setVerticalScale(scale); Chris@30: } Chris@30: Chris@30: