changeset 359:020c485aa7e0

* More work on aligning copy/paste between layers. It's a surprisingly complicated business.
author Chris Cannam
date Wed, 06 Feb 2008 12:49:49 +0000
parents 8b69f36c74be
children d58701996fae
files layer/ImageLayer.cpp layer/ImageLayer.h layer/Layer.cpp layer/Layer.h layer/NoteLayer.cpp layer/NoteLayer.h layer/TextLayer.cpp layer/TextLayer.h layer/TimeInstantLayer.cpp layer/TimeInstantLayer.h layer/TimeValueLayer.cpp layer/TimeValueLayer.h view/View.cpp
diffstat 13 files changed, 84 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/layer/ImageLayer.cpp	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/ImageLayer.cpp	Wed Feb 06 12:49:49 2008 +0000
@@ -802,7 +802,7 @@
 }
 
 void
-ImageLayer::copy(Selection s, Clipboard &to)
+ImageLayer::copy(View *v, Selection s, Clipboard &to)
 {
     if (!m_model) return;
 
@@ -821,7 +821,7 @@
 }
 
 bool
-ImageLayer::paste(const Clipboard &from, int frameOffset, bool /* interactive */)
+ImageLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
 {
     if (!m_model) return false;
 
--- a/layer/ImageLayer.h	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/ImageLayer.h	Wed Feb 06 12:49:49 2008 +0000
@@ -58,8 +58,8 @@
     virtual void resizeSelection(Selection s, Selection newSize);
     virtual void deleteSelection(Selection s);
 
-    virtual void copy(Selection s, Clipboard &to);
-    virtual bool paste(const Clipboard &from, int frameOffset,
+    virtual void copy(View *v, Selection s, Clipboard &to);
+    virtual bool paste(View *v, const Clipboard &from, int frameOffset,
                        bool interactive);
 
     virtual bool editOpen(View *, QMouseEvent *); // on double-click
--- a/layer/Layer.cpp	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/Layer.cpp	Wed Feb 06 12:49:49 2008 +0000
@@ -159,6 +159,30 @@
     return true;
 }
 
+size_t
+Layer::alignToReference(View *v, size_t frame) const
+{
+    const Model *m = getModel();
+    std::cerr << "Layer::alignToReference(" << frame << "): model = " << m << ", alignment reference = " << (m ? m->getAlignmentReference() : 0) << std::endl;
+    if (m && m->getAlignmentReference()) {
+        return m->alignToReference(frame);
+    } else {
+        return v->alignToReference(frame);
+    }
+}
+
+size_t
+Layer::alignFromReference(View *v, size_t frame) const
+{
+    const Model *m = getModel();
+    std::cerr << "Layer::alignFromReference(" << frame << "): model = " << m << ", alignment reference = " << (m ? m->getAlignmentReference() : 0) << std::endl;
+    if (m && m->getAlignmentReference()) {
+        return m->alignFromReference(frame);
+    } else {
+        return v->alignFromReference(frame);
+    }
+}
+
 bool
 Layer::MeasureRect::operator<(const MeasureRect &mr) const
 {
--- a/layer/Layer.h	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/Layer.h	Wed Feb 06 12:49:49 2008 +0000
@@ -193,7 +193,7 @@
     virtual void resizeSelection(Selection, Selection /* newSize */) { }
     virtual void deleteSelection(Selection) { }
 
-    virtual void copy(Selection, Clipboard & /* to */) { }
+    virtual void copy(View *, Selection, Clipboard & /* to */) { }
 
     /**
      * Paste from the given clipboard onto the layer at the given
@@ -202,7 +202,8 @@
      * return false if the user cancelled the paste operation.  This
      * function should return true if a paste actually occurred.
      */
-    virtual bool paste(const Clipboard & /* from */,
+    virtual bool paste(View *,
+                       const Clipboard & /* from */,
                        int /* frameOffset */,
                        bool /* interactive */) { return false; }
 
@@ -465,6 +466,9 @@
 protected:
     void connectSignals(const Model *);
 
+    virtual size_t alignToReference(View *v, size_t frame) const;
+    virtual size_t alignFromReference(View *v, size_t frame) const;
+
     struct MeasureRect {
 
         mutable QRect pixrect;
--- a/layer/NoteLayer.cpp	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/NoteLayer.cpp	Wed Feb 06 12:49:49 2008 +0000
@@ -916,7 +916,7 @@
 }    
 
 void
-NoteLayer::copy(Selection s, Clipboard &to)
+NoteLayer::copy(View *v, Selection s, Clipboard &to)
 {
     if (!m_model) return;
 
@@ -934,7 +934,7 @@
 }
 
 bool
-NoteLayer::paste(const Clipboard &from, int frameOffset, bool /* interactive */)
+NoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
 {
     if (!m_model) return false;
 
--- a/layer/NoteLayer.h	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/NoteLayer.h	Wed Feb 06 12:49:49 2008 +0000
@@ -58,8 +58,8 @@
     virtual void resizeSelection(Selection s, Selection newSize);
     virtual void deleteSelection(Selection s);
 
-    virtual void copy(Selection s, Clipboard &to);
-    virtual bool paste(const Clipboard &from, int frameOffset,
+    virtual void copy(View *v, Selection s, Clipboard &to);
+    virtual bool paste(View *v, const Clipboard &from, int frameOffset,
                        bool interactive);
 
     virtual const Model *getModel() const { return m_model; }
--- a/layer/TextLayer.cpp	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/TextLayer.cpp	Wed Feb 06 12:49:49 2008 +0000
@@ -671,7 +671,7 @@
 }
 
 void
-TextLayer::copy(Selection s, Clipboard &to)
+TextLayer::copy(View *v, Selection s, Clipboard &to)
 {
     if (!m_model) return;
 
@@ -689,7 +689,7 @@
 }
 
 bool
-TextLayer::paste(const Clipboard &from, int frameOffset, bool /* interactive */)
+TextLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
 {
     if (!m_model) return false;
 
--- a/layer/TextLayer.h	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/TextLayer.h	Wed Feb 06 12:49:49 2008 +0000
@@ -56,8 +56,8 @@
     virtual void resizeSelection(Selection s, Selection newSize);
     virtual void deleteSelection(Selection s);
 
-    virtual void copy(Selection s, Clipboard &to);
-    virtual bool paste(const Clipboard &from, int frameOffset,
+    virtual void copy(View *v, Selection s, Clipboard &to);
+    virtual bool paste(View *v, const Clipboard &from, int frameOffset,
                        bool interactive);
 
     virtual bool editOpen(View *, QMouseEvent *); // on double-click
--- a/layer/TimeInstantLayer.cpp	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/TimeInstantLayer.cpp	Wed Feb 06 12:49:49 2008 +0000
@@ -712,7 +712,7 @@
 }
 
 void
-TimeInstantLayer::copy(Selection s, Clipboard &to)
+TimeInstantLayer::copy(View *v, Selection s, Clipboard &to)
 {
     if (!m_model) return;
 
@@ -729,14 +729,17 @@
     // alignment as it.  If it was generated by deriving from another
     // layer's model, that would be... but it wasn't necessarily
 
-            point.setReferenceFrame(m_model->alignToReference(i->frame));
+            point.setReferenceFrame(alignToReference(v, i->frame));
+
+            std::cerr << "TimeInstantLayer::copy: frame = " << i->frame << ", reference frame = " << point.getReferenceFrame() << std::endl;
+
             to.addPoint(point);
         }
     }
 }
 
 bool
-TimeInstantLayer::clipboardAlignmentDiffers(const Clipboard &clip) const
+TimeInstantLayer::clipboardAlignmentDiffers(View *v, const Clipboard &clip) const
 {
     //!!! hoist -- all pastable layers will need this
 
@@ -772,7 +775,7 @@
         if (i->haveReferenceFrame()) {
             referenceFrame = i->getReferenceFrame();
         }
-        long myMappedFrame = m_model->alignToReference(sourceFrame);
+        long myMappedFrame = alignToReference(v, sourceFrame);
 
         std::cerr << "sourceFrame = " << sourceFrame << ", referenceFrame = " << referenceFrame << " (have = " << i->haveReferenceFrame() << "), myMappedFrame = " << myMappedFrame << std::endl;
 
@@ -783,7 +786,7 @@
 }
 
 bool
-TimeInstantLayer::paste(const Clipboard &from, int frameOffset, bool)
+TimeInstantLayer::paste(View *v, const Clipboard &from, int frameOffset, bool)
 {
     if (!m_model) return false;
 
@@ -845,7 +848,7 @@
 
     bool realign = false;
 
-    if (clipboardAlignmentDiffers(from)) {
+    if (clipboardAlignmentDiffers(v, from)) {
 
         std::cerr << "Offer alignment option..." << std::endl;
 
@@ -864,7 +867,6 @@
         if (selected == options[1]) realign = true;
     }
 
-
     SparseOneDimensionalModel::EditCommand *command =
 	new SparseOneDimensionalModel::EditCommand(m_model, tr("Paste"));
 
@@ -872,10 +874,29 @@
          i != points.end(); ++i) {
         
         if (!i->haveFrame()) continue;
+
         size_t frame = 0;
-        if (frameOffset > 0 || -frameOffset < i->getFrame()) {
-            frame = i->getFrame() + frameOffset;
+
+        if (!realign) {
+            
+            frame = i->getFrame();
+
+        } else {
+
+            if (i->haveReferenceFrame()) {
+                frame = i->getReferenceFrame();
+                frame = alignFromReference(v, frame);
+            } else {
+                frame = i->getFrame();
+            }
         }
+
+        if (frameOffset > 0) frame += frameOffset;
+        else if (frameOffset < 0) {
+            if (frame > -frameOffset) frame += frameOffset;
+            else frame = 0;
+        }
+
         SparseOneDimensionalModel::Point newPoint(frame);
         if (i->haveLabel()) {
             newPoint.label = i->getLabel();
--- a/layer/TimeInstantLayer.h	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/TimeInstantLayer.h	Wed Feb 06 12:49:49 2008 +0000
@@ -59,8 +59,8 @@
     virtual void resizeSelection(Selection s, Selection newSize);
     virtual void deleteSelection(Selection s);
 
-    virtual void copy(Selection s, Clipboard &to);
-    virtual bool paste(const Clipboard &from, int frameOffset,
+    virtual void copy(View *v, Selection s, Clipboard &to);
+    virtual bool paste(View *v, const Clipboard &from, int frameOffset,
                        bool interactive);
 
     virtual const Model *getModel() const { return m_model; }
@@ -105,7 +105,7 @@
 
     virtual int getDefaultColourHint(bool dark, bool &impose);
 
-    bool clipboardAlignmentDiffers(const Clipboard &) const;
+    bool clipboardAlignmentDiffers(View *v, const Clipboard &) const;
 
     SparseOneDimensionalModel *m_model;
     bool m_editing;
--- a/layer/TimeValueLayer.cpp	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/TimeValueLayer.cpp	Wed Feb 06 12:49:49 2008 +0000
@@ -1185,7 +1185,7 @@
 }    
 
 void
-TimeValueLayer::copy(Selection s, Clipboard &to)
+TimeValueLayer::copy(View *v, Selection s, Clipboard &to)
 {
     if (!m_model) return;
 
@@ -1203,7 +1203,7 @@
 }
 
 bool
-TimeValueLayer::paste(const Clipboard &from, int frameOffset,
+TimeValueLayer::paste(View *v, const Clipboard &from, int frameOffset,
                       bool interactive)
 {
     if (!m_model) return false;
--- a/layer/TimeValueLayer.h	Mon Feb 04 17:45:16 2008 +0000
+++ b/layer/TimeValueLayer.h	Wed Feb 06 12:49:49 2008 +0000
@@ -61,8 +61,8 @@
     virtual void resizeSelection(Selection s, Selection newSize);
     virtual void deleteSelection(Selection s);
 
-    virtual void copy(Selection s, Clipboard &to);
-    virtual bool paste(const Clipboard &from, int frameOffset,
+    virtual void copy(View *v, Selection s, Clipboard &to);
+    virtual bool paste(View *v, const Clipboard &from, int frameOffset,
                        bool interactive);
 
     virtual const Model *getModel() const { return m_model; }
--- a/view/View.cpp	Mon Feb 04 17:45:16 2008 +0000
+++ b/view/View.cpp	Wed Feb 06 12:49:49 2008 +0000
@@ -1120,6 +1120,7 @@
     }
 
     Model *anyModel = 0;
+    Model *alignedModel = 0;
     Model *goodModel = 0;
 
     for (LayerList::const_iterator i = m_layers.begin();
@@ -1133,8 +1134,10 @@
         Model *model = (*i)->getModel();
         if (!model) continue;
 
+        anyModel = model;
+
         if (model->getAlignmentReference()) {
-            anyModel = model;
+            alignedModel = model;
             if (layer->isLayerOpaque() ||
                 dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
                 goodModel = model;
@@ -1143,6 +1146,7 @@
     }
 
     if (goodModel) return goodModel;
+    else if (alignedModel) return alignedModel;
     else return anyModel;
 }