Mercurial > hg > svgui
diff layer/TextLayer.cpp @ 1474:36ad3cdabf55 by-id
Further layer updates for ModelById
author | Chris Cannam |
---|---|
date | Tue, 02 Jul 2019 14:08:44 +0100 |
parents | f2525e6cbdf1 |
children | 37df1530519d |
line wrap: on
line diff
--- a/layer/TextLayer.cpp Tue Jul 02 11:49:52 2019 +0100 +++ b/layer/TextLayer.cpp Tue Jul 02 14:08:44 2019 +0100 @@ -34,7 +34,6 @@ TextLayer::TextLayer() : SingleColourLayer(), - m_model(nullptr), m_editing(false), m_originalPoint(0, 0.0, tr("Empty Label")), m_editingPoint(0, 0.0, tr("Empty Label")), @@ -125,14 +124,15 @@ EventVector TextLayer::getLocalPoints(LayerGeometryProvider *v, int x, int y) const { - if (!m_model) return {}; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return {}; int overlap = ViewManager::scalePixelSize(150); sv_frame_t frame0 = v->getFrameForX(-overlap); sv_frame_t frame1 = v->getFrameForX(v->getPaintWidth() + overlap); - EventVector points(m_model->getEventsSpanning(frame0, frame1 - frame0)); + EventVector points(model->getEventsSpanning(frame0, frame1 - frame0)); EventVector rv; QFontMetrics metrics = QFontMetrics(QFont()); @@ -170,11 +170,12 @@ bool TextLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, Event &p) const { - if (!m_model) return false; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return false; sv_frame_t a = v->getFrameForX(x - ViewManager::scalePixelSize(120)); sv_frame_t b = v->getFrameForX(x + ViewManager::scalePixelSize(10)); - EventVector onPoints = m_model->getEventsWithin(a, b); + EventVector onPoints = model->getEventsWithin(a, b); if (onPoints.empty()) return false; double nearestDistance = -1; @@ -200,12 +201,13 @@ { int x = pos.x(); - if (!m_model || !m_model->getSampleRate()) return ""; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model || !model->getSampleRate()) return ""; EventVector points = getLocalPoints(v, x, pos.y()); if (points.empty()) { - if (!m_model->isReady()) { + if (!model->isReady()) { return tr("In progress"); } else { return ""; @@ -214,7 +216,7 @@ sv_frame_t useFrame = points.begin()->getFrame(); - RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); + RealTime rt = RealTime::frame2RealTime(useFrame, model->getSampleRate()); QString text; @@ -238,7 +240,8 @@ int &resolution, SnapType snap) const { - if (!m_model) { + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) { return Layer::snapToFeatureFrame(v, frame, resolution, snap); } @@ -249,7 +252,7 @@ // an editing operation, i.e. closest feature in either direction // but only if it is "close enough" - resolution = m_model->getResolution(); + resolution = model->getResolution(); if (snap == SnapNeighbouring) { EventVector points = getLocalPoints(v, v->getXForFrame(frame), -1); @@ -259,7 +262,7 @@ } Event e; - if (m_model->getNearestEventMatching + if (model->getNearestEventMatching (frame, [](Event) { return true; }, snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward, @@ -288,9 +291,10 @@ void TextLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const { - if (!m_model || !m_model->isOK()) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model || !model->isOK()) return; - sv_samplerate_t sampleRate = m_model->getSampleRate(); + sv_samplerate_t sampleRate = model->getSampleRate(); if (!sampleRate) return; // Profiler profiler("TextLayer::paint", true); @@ -300,7 +304,7 @@ sv_frame_t frame0 = v->getFrameForX(x0 - overlap); sv_frame_t frame1 = v->getFrameForX(x1 + overlap); - EventVector points(m_model->getEventsWithin(frame0, frame1 - frame0, 2)); + EventVector points(model->getEventsWithin(frame0, frame1 - frame0, 2)); if (points.empty()) return; QColor brushColour(getBaseQColor()); @@ -313,7 +317,7 @@ penColour = v->getForeground(); // SVDEBUG << "TextLayer::paint: resolution is " -// << m_model->getResolution() << " frames" << endl; +// << model->getResolution() << " frames" << endl; QPoint localPos; Event illuminatePoint(0); @@ -393,14 +397,15 @@ { // SVDEBUG << "TextLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl; - if (!m_model) { + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) { SVDEBUG << "TextLayer::drawStart: no model" << endl; return; } sv_frame_t frame = v->getFrameForX(e->x()); if (frame < 0) frame = 0; - frame = frame / m_model->getResolution() * m_model->getResolution(); + frame = frame / model->getResolution() * model->getResolution(); double height = getHeightForY(v, e->y()); @@ -408,7 +413,7 @@ m_originalPoint = m_editingPoint; if (m_editingCommand) finish(m_editingCommand); - m_editingCommand = new ChangeEventsCommand(m_model, "Add Label"); + m_editingCommand = new ChangeEventsCommand(m_model.untyped, "Add Label"); m_editingCommand->add(m_editingPoint); m_editing = true; @@ -419,11 +424,12 @@ { // SVDEBUG << "TextLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl; - if (!m_model || !m_editing) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model || !m_editing) return; sv_frame_t frame = v->getFrameForX(e->x()); if (frame < 0) frame = 0; - frame = frame / m_model->getResolution() * m_model->getResolution(); + frame = frame / model->getResolution() * model->getResolution(); double height = getHeightForY(v, e->y()); @@ -438,7 +444,8 @@ TextLayer::drawEnd(LayerGeometryProvider *v, QMouseEvent *) { // SVDEBUG << "TextLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl; - if (!m_model || !m_editing) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model || !m_editing) return; bool ok = false; QString label = QInputDialog::getText(v->getView(), tr("Enter label"), @@ -461,7 +468,8 @@ void TextLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e) { - if (!m_model) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return; if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; @@ -481,7 +489,8 @@ void TextLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e) { - if (!m_model || !m_editing) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model || !m_editing) return; m_editing = false; @@ -490,7 +499,7 @@ if (p.getFrame() != m_editingPoint.getFrame() || p.getValue() != m_editingPoint.getValue()) return; - m_editingCommand = new ChangeEventsCommand(m_model, tr("Erase Point")); + m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Erase Point")); m_editingCommand->remove(m_editingPoint); finish(m_editingCommand); m_editingCommand = nullptr; @@ -502,7 +511,8 @@ { // SVDEBUG << "TextLayer::editStart(" << e->x() << "," << e->y() << ")" << endl; - if (!m_model) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return; if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) { return; @@ -522,7 +532,8 @@ void TextLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e) { - if (!m_model || !m_editing) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model || !m_editing) return; sv_frame_t frameDiff = v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x()); @@ -533,10 +544,10 @@ double height = m_originalPoint.getValue() + heightDiff; if (frame < 0) frame = 0; - frame = (frame / m_model->getResolution()) * m_model->getResolution(); + frame = (frame / model->getResolution()) * model->getResolution(); if (!m_editingCommand) { - m_editingCommand = new ChangeEventsCommand(m_model, tr("Drag Label")); + m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Drag Label")); } m_editingCommand->remove(m_editingPoint); @@ -550,7 +561,8 @@ TextLayer::editEnd(LayerGeometryProvider *, QMouseEvent *) { // SVDEBUG << "TextLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl; - if (!m_model || !m_editing) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model || !m_editing) return; if (m_editingCommand) { @@ -577,7 +589,8 @@ bool TextLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e) { - if (!m_model) return false; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return false; Event text; if (!getPointToDrag(v, e->x(), e->y(), text)) return false; @@ -590,7 +603,7 @@ QLineEdit::Normal, label, &ok); if (ok && label != text.getLabel()) { ChangeEventsCommand *command = - new ChangeEventsCommand(m_model, tr("Re-Label Point")); + new ChangeEventsCommand(m_model.untyped, tr("Re-Label Point")); command->remove(text); command->add(text.withLabel(label)); finish(command); @@ -602,13 +615,14 @@ void TextLayer::moveSelection(Selection s, sv_frame_t newStartFrame) { - if (!m_model) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return; ChangeEventsCommand *command = - new ChangeEventsCommand(m_model, tr("Drag Selection")); + new ChangeEventsCommand(m_model.untyped, tr("Drag Selection")); EventVector points = - m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); + model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); for (Event p: points) { command->remove(p); @@ -623,13 +637,14 @@ void TextLayer::resizeSelection(Selection s, Selection newSize) { - if (!m_model) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return; ChangeEventsCommand *command = - new ChangeEventsCommand(m_model, tr("Resize Selection")); + new ChangeEventsCommand(m_model.untyped, tr("Resize Selection")); EventVector points = - m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); + model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); double ratio = double(newSize.getDuration()) / double(s.getDuration()); double oldStart = double(s.getStartFrame()); @@ -651,13 +666,14 @@ void TextLayer::deleteSelection(Selection s) { - if (!m_model) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return; ChangeEventsCommand *command = - new ChangeEventsCommand(m_model, tr("Delete Selection")); + new ChangeEventsCommand(m_model.untyped, tr("Delete Selection")); EventVector points = - m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); + model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); for (Event p: points) { command->remove(p); @@ -669,10 +685,11 @@ void TextLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to) { - if (!m_model) return; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return; EventVector points = - m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); + model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); for (Event p: points) { to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame()))); @@ -682,7 +699,8 @@ bool TextLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */) { - if (!m_model) return false; + auto model = ModelById::getAs<TextModel>(m_model); + if (!model) return false; const EventVector &points = from.getPoints(); @@ -706,7 +724,7 @@ } ChangeEventsCommand *command = - new ChangeEventsCommand(m_model, tr("Paste")); + new ChangeEventsCommand(m_model.untyped, tr("Paste")); double valueMin = 0.0, valueMax = 1.0; for (EventVector::const_iterator i = points.begin();