changeset 15:47500c27ac26

* Add getXForFrame / getFrameForX in preference to using the zoom level everywhere
author Chris Cannam
date Mon, 30 Jan 2006 13:19:42 +0000 (2006-01-30)
parents b101cc2ae1ab
children cc98d496d52b
files base/Layer.cpp base/Layer.h base/View.cpp base/View.h
diffstat 4 files changed, 77 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/base/Layer.cpp	Fri Jan 27 18:04:07 2006 +0000
+++ b/base/Layer.cpp	Mon Jan 30 13:19:42 2006 +0000
@@ -41,6 +41,20 @@
     emit layerNameChanged();
 }
 
+int
+Layer::getXForFrame(long frame) const
+{
+    if (m_view) return m_view->getXForFrame(frame);
+    else return 0;
+}
+
+long
+Layer::getFrameForX(int x) const
+{
+    if (m_view) return m_view->getFrameForX(x);
+    else return 0;
+}
+
 QString
 Layer::toXmlString(QString indent, QString extraAttributes) const
 {
--- a/base/Layer.h	Fri Jan 27 18:04:07 2006 +0000
+++ b/base/Layer.h	Mon Jan 30 13:19:42 2006 +0000
@@ -149,6 +149,17 @@
     virtual void setObjectName(const QString &name);
 
     /**
+     * Return the pixel x-coordinate corresponding to a given sample
+     * frame (which may be negative).
+     */
+    int getXForFrame(long frame) const;
+
+    /**
+     * Return the closest frame to the given pixel x-coordinate.
+     */
+    long getFrameForX(int x) const;
+
+    /**
      * Convert the layer's data (though not those of the model it
      * refers to) into an XML string for file output.  This class
      * implements the basic name/type/model-id output; subclasses will
--- a/base/View.cpp	Fri Jan 27 18:04:07 2006 +0000
+++ b/base/View.cpp	Mon Jan 30 13:19:42 2006 +0000
@@ -233,6 +233,18 @@
     return changeVisible;
 }
 
+int
+View::getXForFrame(long frame) const
+{
+    return (frame - getStartFrame()) / m_zoomLevel;
+}
+
+long
+View::getFrameForX(int x) const
+{
+    return (long(x) * long(m_zoomLevel)) + getStartFrame();
+}
+
 void
 View::setZoomLevel(size_t z)
 {
@@ -414,9 +426,9 @@
     if (long(startFrame) < myStartFrame) startFrame = myStartFrame;
     if (endFrame > myEndFrame) endFrame = myEndFrame;
 
-    int x0 = (startFrame - myStartFrame) / m_zoomLevel;
-    int x1 = (endFrame - myStartFrame) / m_zoomLevel;
-    if (x1 < x0) return;
+    int x0 = getXForFrame(startFrame);
+    int x1 = getXForFrame(endFrame + 1);
+    if (x1 < x0) x1 = x0;
 
     checkProgress(obj);
 
@@ -490,7 +502,7 @@
     }
 
     if (m_playPointerFrame == f) return;
-    bool visible = (m_playPointerFrame / m_zoomLevel != f / m_zoomLevel);
+    bool visible = (getXForFrame(m_playPointerFrame) != getXForFrame(f));
     size_t oldPlayPointerFrame = m_playPointerFrame;
     m_playPointerFrame = f;
     if (!visible) return;
@@ -506,10 +518,10 @@
 
     case PlaybackScrollPage:
     { 
-	int xold = (long(oldPlayPointerFrame) - getStartFrame()) / m_zoomLevel;
+	int xold = getXForFrame(oldPlayPointerFrame);
 	repaint(xold - 1, 0, 3, height());
 
-	long w = width() * getZoomLevel();
+	long w = getEndFrame() - getStartFrame();
 	w -= w/5;
 	long sf = (f / w) * w - w/8;
 
@@ -530,18 +542,24 @@
 		  << getStartFrame() << std::endl;
 #endif
 
-	if (QApplication::mouseButtons() == Qt::NoButton &&
-	    QApplication::keyboardModifiers() == Qt::NoModifier) {
-	    bool changed =
-		setCentreFrame(sf + width() * getZoomLevel() / 2, false);
-	    if (changed) {
-		xold = (long(oldPlayPointerFrame) -
-			getStartFrame()) / m_zoomLevel;
-		update(xold - 1, 0, 3, height());
+	// We don't consider scrolling unless the pointer is outside
+	// the clearly visible range already
+
+	int xnew = getXForFrame(m_playPointerFrame);
+
+	if (xnew < width()/8 || xnew > (width()*7)/8) {
+	    if (QApplication::mouseButtons() == Qt::NoButton &&
+		QApplication::keyboardModifiers() == Qt::NoModifier) {
+		long offset = getFrameForX(width()/2) - getStartFrame();
+		long newCentre = sf + offset;
+		bool changed = setCentreFrame(newCentre, false);
+		if (changed) {
+		    xold = getXForFrame(oldPlayPointerFrame);
+		    update(xold - 1, 0, 3, height());
+		}
 	    }
 	}
 
-	int xnew = (long(m_playPointerFrame) - getStartFrame()) / m_zoomLevel;
 	update(xnew - 1, 0, 3, height());
 
 	break;
@@ -759,9 +777,9 @@
 {
     long delta;
     if (lots) {
-	delta = ((width() / 2) * m_zoomLevel);
+	delta = (getEndFrame() - getStartFrame()) / 2;
     } else {
-	delta = ((width() / 20) * m_zoomLevel);
+	delta = (getEndFrame() - getStartFrame()) / 20;
     }
     if (right) delta = -delta;
 
@@ -921,8 +939,9 @@
 
 	} else if (m_cacheCentreFrame != m_centreFrame) {
 
-	    long dx = long(m_cacheCentreFrame / m_zoomLevel) -
-		long(m_centreFrame / m_zoomLevel);
+	    long dx =
+		getXForFrame(m_cacheCentreFrame) -
+		getXForFrame(m_centreFrame);
 
 	    if (dx > -width() && dx < width()) {
 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
@@ -1070,8 +1089,7 @@
 	if (long(m_playPointerFrame) > getStartFrame() &&
 	    m_playPointerFrame < getEndFrame()) {
 
-	    int playx = (long(m_playPointerFrame) - getStartFrame()) /
-		m_zoomLevel;
+	    int playx = getXForFrame(m_playPointerFrame);
 
 	    paint.setPen(Qt::black);
 	    paint.drawLine(playx - 1, 0, playx - 1, height() - 1);
@@ -1115,10 +1133,8 @@
     for (ViewManager::SelectionList::iterator i = selections.begin();
 	 i != selections.end(); ++i) {
 
-	int p0 = -1, p1 = -1;
-
-	p0 = (long(i->getStartFrame()) - getStartFrame()) / m_zoomLevel;
-	p1 = (long(i->getEndFrame()) - getStartFrame()) / m_zoomLevel;
+	int p0 = getXForFrame(i->getStartFrame());
+	int p1 = getXForFrame(i->getEndFrame());
 
 	if (p1 < 0 || p0 > width()) continue;
 
--- a/base/View.h	Fri Jan 27 18:04:07 2006 +0000
+++ b/base/View.h	Mon Jan 30 13:19:42 2006 +0000
@@ -85,6 +85,17 @@
     virtual size_t getEndFrame() const;
 
     /**
+     * Return the pixel x-coordinate corresponding to a given sample
+     * frame (which may be negative).
+     */
+    int getXForFrame(long frame) const;
+
+    /**
+     * Return the closest frame to the given pixel x-coordinate.
+     */
+    long getFrameForX(int x) const;
+
+    /**
      * Return the zoom level, i.e. the number of frames per pixel.
      */
     virtual int getZoomLevel() const { return m_zoomLevel; }