diff layer/TimeInstantLayer.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/TimeInstantLayer.cpp	Tue Jul 02 11:49:52 2019 +0100
+++ b/layer/TimeInstantLayer.cpp	Tue Jul 02 14:08:44 2019 +0100
@@ -41,7 +41,6 @@
 
 TimeInstantLayer::TimeInstantLayer() :
     SingleColourLayer(),
-    m_model(nullptr),
     m_editing(false),
     m_editingPoint(0, tr("New Point")),
     m_editingCommand(nullptr),
@@ -162,7 +161,7 @@
 TimeInstantLayer::needsTextLabelHeight() const
 {
     auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
-    if (model) return m_model->hasTextLabels();
+    if (model) return model->hasTextLabels();
     else return false;
 }
 
@@ -176,7 +175,8 @@
 EventVector
 TimeInstantLayer::getLocalPoints(LayerGeometryProvider *v, int x) const
 {
-    if (!m_model) return {};
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) return {};
 
     // Return a set of points that all have the same frame number, the
     // nearest to the given x coordinate, and that are within a
@@ -184,12 +184,12 @@
 
     sv_frame_t frame = v->getFrameForX(x);
 
-    EventVector exact = m_model->getEventsStartingAt(frame);
+    EventVector exact = model->getEventsStartingAt(frame);
     if (!exact.empty()) return exact;
 
     // overspill == 1, so one event either side of the given span
-    EventVector neighbouring = m_model->getEventsWithin
-        (frame, m_model->getResolution(), 1);
+    EventVector neighbouring = model->getEventsWithin
+        (frame, model->getResolution(), 1);
 
     double fuzz = v->scaleSize(2);
     sv_frame_t suitable = 0;
@@ -213,7 +213,7 @@
     }
 
     if (have) {
-        return m_model->getEventsStartingAt(suitable);
+        return model->getEventsStartingAt(suitable);
     } else {
         return {};
     }
@@ -222,10 +222,11 @@
 QString
 TimeInstantLayer::getLabelPreceding(sv_frame_t frame) const
 {
-    if (!m_model || !m_model->hasTextLabels()) return "";
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model || !model->hasTextLabels()) return "";
 
     Event e;
-    if (m_model->getNearestEventMatching
+    if (model->getNearestEventMatching
         (frame,
          [](Event e) { return e.hasLabel() && e.getLabel() != ""; },
          EventSeries::Backward,
@@ -241,12 +242,13 @@
 {
     int x = pos.x();
 
-    if (!m_model || !m_model->getSampleRate()) return "";
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model || !model->getSampleRate()) return "";
 
     EventVector points = getLocalPoints(v, x);
 
     if (points.empty()) {
-        if (!m_model->isReady()) {
+        if (!model->isReady()) {
             return tr("In progress");
         } else {
             return tr("No local points");
@@ -255,7 +257,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;
 
@@ -277,7 +279,8 @@
                                      int &resolution,
                                      SnapType snap) const
 {
-    if (!m_model) {
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) {
         return Layer::snapToFeatureFrame(v, frame, resolution, snap);
     }
 
@@ -288,7 +291,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));
@@ -298,7 +301,7 @@
     }
 
     Event e;
-    if (m_model->getNearestEventMatching
+    if (model->getNearestEventMatching
         (frame,
          [](Event) { return true; },
          snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward,
@@ -313,7 +316,8 @@
 void
 TimeInstantLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
 {
-    if (!m_model || !m_model->isOK()) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model || !model->isOK()) return;
 
 //    Profiler profiler("TimeInstantLayer::paint", true);
 
@@ -329,12 +333,12 @@
         overspill = 1;
     }
     
-    EventVector points(m_model->getEventsWithin(frame0, frame1 - frame0,
+    EventVector points(model->getEventsWithin(frame0, frame1 - frame0,
                                                 overspill));
 
     bool odd = false;
     if (m_plotStyle == PlotSegmentation && !points.empty()) {
-        int index = m_model->getRowForFrame(points.begin()->getFrame());
+        int index = model->getRowForFrame(points.begin()->getFrame());
         odd = ((index % 2) == 1);
     }
 
@@ -355,13 +359,13 @@
         } else if (getBaseQColor() == Qt::darkGreen) {
             oddBrushColour = Qt::green;
         } else {
-            oddBrushColour = oddBrushColour.light(150);
+            oddBrushColour = oddBrushColour.lighter(150);
         }
         oddBrushColour.setAlpha(100);
     }
 
 //    SVDEBUG << "TimeInstantLayer::paint: resolution is "
-//              << m_model->getResolution() << " frames" << endl;
+//              << model->getResolution() << " frames" << endl;
 
     QPoint localPos;
     sv_frame_t illuminateFrame = -1;
@@ -387,7 +391,7 @@
         if (x == prevX && m_plotStyle == PlotInstants &&
             p.getFrame() != illuminateFrame) continue;
 
-        int iw = v->getXForFrame(p.getFrame() + m_model->getResolution()) - x;
+        int iw = v->getXForFrame(p.getFrame() + model->getResolution()) - x;
         if (iw < 2) {
             if (iw < 1) {
                 iw = 2;
@@ -423,7 +427,7 @@
                 Event q(*j);
                 nx = v->getXForFrame(q.getFrame());
             } else {
-                nx = v->getXForFrame(m_model->getEndFrame());
+                nx = v->getXForFrame(model->getEndFrame());
             }
 
             if (nx >= x) {
@@ -442,6 +446,11 @@
         paint.setPen(getBaseQColor());
         
         if (p.getLabel() != "") {
+            
+    // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
+    // replacement (horizontalAdvance) was only added in Qt 5.11
+    // which is too new for us
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 
             // only draw if there's enough room from here to the next point
 
@@ -472,16 +481,17 @@
     cerr << "TimeInstantLayer::drawStart(" << e->x() << ")" << endl;
 #endif
 
-    if (!m_model) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) 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();
 
     m_editingPoint = Event(frame, tr("New Point"));
 
     if (m_editingCommand) finish(m_editingCommand);
-    m_editingCommand = new ChangeEventsCommand(m_model, tr("Draw Point"));
+    m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Draw Point"));
     m_editingCommand->add(m_editingPoint);
 
     m_editing = true;
@@ -494,11 +504,12 @@
     cerr << "TimeInstantLayer::drawDrag(" << e->x() << ")" << endl;
 #endif
 
-    if (!m_model || !m_editing) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(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();
     m_editingCommand->remove(m_editingPoint);
     m_editingPoint = m_editingPoint.withFrame(frame);
     m_editingCommand->add(m_editingPoint);
@@ -510,10 +521,11 @@
 #ifdef DEBUG_TIME_INSTANT_LAYER
     cerr << "TimeInstantLayer::drawEnd(" << e->x() << ")" << endl;
 #endif
-    if (!m_model || !m_editing) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model || !m_editing) return;
     QString newName = tr("Add Point at %1 s")
         .arg(RealTime::frame2RealTime(m_editingPoint.getFrame(),
-                                      m_model->getSampleRate())
+                                      model->getSampleRate())
              .toText(false).c_str());
     m_editingCommand->setName(newName);
     finish(m_editingCommand);
@@ -524,7 +536,8 @@
 void
 TimeInstantLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
 {
-    if (!m_model) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) return;
 
     EventVector points = getLocalPoints(v, e->x());
     if (points.empty()) return;
@@ -547,7 +560,8 @@
 void
 TimeInstantLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
 {
-    if (!m_model || !m_editing) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model || !m_editing) return;
 
     m_editing = false;
 
@@ -555,7 +569,7 @@
     if (points.empty()) return;
     if (points.begin()->getFrame() != m_editingPoint.getFrame()) 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;
@@ -569,7 +583,8 @@
     cerr << "TimeInstantLayer::editStart(" << e->x() << ")" << endl;
 #endif
 
-    if (!m_model) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) return;
 
     EventVector points = getLocalPoints(v, e->x());
     if (points.empty()) return;
@@ -591,14 +606,15 @@
     cerr << "TimeInstantLayer::editDrag(" << e->x() << ")" << endl;
 #endif
 
-    if (!m_model || !m_editing) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(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();
 
     if (!m_editingCommand) {
-        m_editingCommand = new ChangeEventsCommand(m_model, tr("Drag Point"));
+        m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Drag Point"));
     }
 
     m_editingCommand->remove(m_editingPoint);
@@ -612,11 +628,12 @@
 #ifdef DEBUG_TIME_INSTANT_LAYER
     cerr << "TimeInstantLayer::editEnd(" << e->x() << ")" << endl;
 #endif
-    if (!m_model || !m_editing) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model || !m_editing) return;
     if (m_editingCommand) {
         QString newName = tr("Move Point to %1 s")
             .arg(RealTime::frame2RealTime(m_editingPoint.getFrame(),
-                                          m_model->getSampleRate())
+                                          model->getSampleRate())
                  .toText(false).c_str());
         m_editingCommand->setName(newName);
         finish(m_editingCommand);
@@ -628,7 +645,8 @@
 bool
 TimeInstantLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
 {
-    if (!m_model) return false;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) return false;
 
     EventVector points = getLocalPoints(v, e->x());
     if (points.empty()) return false;
@@ -636,7 +654,7 @@
     Event point = *points.begin();
 
     ItemEditDialog *dialog = new ItemEditDialog
-        (m_model->getSampleRate(),
+        (model->getSampleRate(),
          ItemEditDialog::ShowTime |
          ItemEditDialog::ShowText);
 
@@ -650,7 +668,7 @@
             .withLabel(dialog->getText());
         
         ChangeEventsCommand *command =
-            new ChangeEventsCommand(m_model, tr("Edit Point"));
+            new ChangeEventsCommand(m_model.untyped, tr("Edit Point"));
         command->remove(point);
         command->add(newPoint);
         finish(command);
@@ -663,13 +681,14 @@
 void
 TimeInstantLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
 {
-    if (!m_model) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(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->getEventsWithin(s.getStartFrame(), s.getDuration());
+        model->getEventsWithin(s.getStartFrame(), s.getDuration());
 
     for (auto p: points) {
         Event newPoint = p
@@ -684,13 +703,14 @@
 void
 TimeInstantLayer::resizeSelection(Selection s, Selection newSize)
 {
-    if (!m_model) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(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->getEventsWithin(s.getStartFrame(), s.getDuration());
+        model->getEventsWithin(s.getStartFrame(), s.getDuration());
 
     double ratio = double(newSize.getDuration()) / double(s.getDuration());
     double oldStart = double(s.getStartFrame());
@@ -712,13 +732,14 @@
 void
 TimeInstantLayer::deleteSelection(Selection s)
 {
-    if (!m_model) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(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->getEventsWithin(s.getStartFrame(), s.getDuration());
+        model->getEventsWithin(s.getStartFrame(), s.getDuration());
 
     for (auto p: points) {
         command->remove(p);
@@ -730,10 +751,11 @@
 void
 TimeInstantLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
 {
-    if (!m_model) return;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) return;
 
     EventVector points =
-        m_model->getEventsWithin(s.getStartFrame(), s.getDuration());
+        model->getEventsWithin(s.getStartFrame(), s.getDuration());
 
     for (auto p: points) {
         to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
@@ -743,7 +765,8 @@
 bool
 TimeInstantLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t frameOffset, bool)
 {
-    if (!m_model) return false;
+    auto model = ModelById::getAs<SparseOneDimensionalModel>(m_model);
+    if (!model) return false;
 
     EventVector points = from.getPoints();
 
@@ -767,7 +790,7 @@
     }
 
     ChangeEventsCommand *command =
-        new ChangeEventsCommand(m_model, tr("Paste"));
+        new ChangeEventsCommand(m_model.untyped, tr("Paste"));
 
     for (EventVector::const_iterator i = points.begin();
          i != points.end(); ++i) {