diff layer/Layer.cpp @ 272:87e4c880b4c8

* highlight the nearest measurement rect * fix rewind during playback
author Chris Cannam
date Fri, 29 Jun 2007 13:58:08 +0000
parents 61a704654497
children e954c00cbe55
line wrap: on
line diff
--- a/layer/Layer.cpp	Thu Jun 28 14:50:58 2007 +0000
+++ b/layer/Layer.cpp	Fri Jun 29 13:58:08 2007 +0000
@@ -26,6 +26,8 @@
 #include "LayerFactory.h"
 #include "base/PlayParameterRepository.h"
 
+#include <cmath>
+
 Layer::Layer() :
     m_haveDraggingRect(false)
 {
@@ -254,18 +256,96 @@
 }
 
 void
-Layer::paintMeasurementRects(View *v, QPainter &paint) const
+Layer::paintMeasurementRects(View *v, QPainter &paint,
+                             bool showFocus, QPoint focusPoint) const
 {
+    updateMeasurementPixrects(v);
+
+    MeasureRectSet::const_iterator focusRectItr = m_measureRects.end();
+
     if (m_haveDraggingRect) {
+
         paintMeasurementRect(v, paint, m_draggingRect, true);
+
+    } else if (showFocus) {
+
+        focusRectItr = findFocusedMeasureRect(focusPoint);
     }
 
     for (MeasureRectSet::const_iterator i = m_measureRects.begin(); 
          i != m_measureRects.end(); ++i) {
-        paintMeasurementRect(v, paint, *i, true);
+        paintMeasurementRect(v, paint, *i, i == focusRectItr);
     }
 }
 
+bool
+Layer::nearestMeasurementRectChanged(View *v, QPoint prev, QPoint now) const
+{
+    updateMeasurementPixrects(v);
+    
+    MeasureRectSet::const_iterator i0 = findFocusedMeasureRect(prev);
+    MeasureRectSet::const_iterator i1 = findFocusedMeasureRect(now);
+
+    return (i0 != i1);
+}
+
+void
+Layer::updateMeasurementPixrects(View *v) const
+{
+    long sf = v->getStartFrame();
+    long ef = v->getEndFrame();
+
+    for (MeasureRectSet::const_iterator i = m_measureRects.begin(); 
+         i != m_measureRects.end(); ++i) {
+
+        if (!i->haveFrames) continue;
+
+        if (i->startFrame >= ef) break;
+        if (i->endFrame <= sf) continue;
+
+        int x0 = -1;
+        int x1 = v->width() + 1;
+        
+        if (i->startFrame >= v->getStartFrame()) {
+            x0 = v->getXForFrame(i->startFrame);
+        }
+        if (i->endFrame <= long(v->getEndFrame())) {
+            x1 = v->getXForFrame(i->endFrame);
+        }
+        
+        QRect pr = QRect(x0, i->pixrect.y(), x1 - x0, i->pixrect.height());
+        
+        i->pixrect = pr;
+    }
+}
+
+Layer::MeasureRectSet::const_iterator
+Layer::findFocusedMeasureRect(QPoint focusPoint) const
+{
+    float frDist = 0;
+    MeasureRectSet::const_iterator focusRectItr = m_measureRects.end();
+
+    for (MeasureRectSet::const_iterator i = m_measureRects.begin(); 
+         i != m_measureRects.end(); ++i) {
+
+        if (!i->pixrect.adjusted(-2, -2, 2, 2).contains(focusPoint)) continue;
+
+        int cx = i->pixrect.x() + i->pixrect.width()/2;
+        int cy = i->pixrect.y() + i->pixrect.height()/2;
+        int xd = focusPoint.x() - cx;
+        int yd = focusPoint.y() - cy;
+        
+        float d = sqrt(xd * xd + yd * yd);
+        
+        if (focusRectItr == m_measureRects.end() || d < frDist) {
+            focusRectItr = i;
+            frDist = d;
+        }
+    }
+
+    return focusRectItr;
+}
+
 void
 Layer::paintMeasurementRect(View *v, QPainter &paint,
                             const MeasureRect &r, bool focus) const
@@ -278,12 +358,11 @@
         if (r.startFrame >= v->getStartFrame()) {
             x0 = v->getXForFrame(r.startFrame);
         }
-        if (r.endFrame <= v->getEndFrame()) {
+        if (r.endFrame <= long(v->getEndFrame())) {
             x1 = v->getXForFrame(r.endFrame);
         }
         
-        QRect pr = QRect(x0, r.pixrect.y(),
-                         x1 - x0, r.pixrect.height());
+        QRect pr = QRect(x0, r.pixrect.y(), x1 - x0, r.pixrect.height());
         
         r.pixrect = pr;
     }