changeset 220:8dc50f57d480

* Make the vertical scale a bit less thtoopid for many linear scale spectra * Don't forget to reload slice layer properties when loading a session, as well as spectrum layer (i.e. subclass) properties
author Chris Cannam
date Mon, 05 Mar 2007 13:04:52 +0000
parents 1fff998ae4a9
children 9e739f92c0b8
files layer/PaintAssistant.cpp layer/PaintAssistant.h layer/SliceLayer.cpp layer/SpectrumLayer.cpp
diffstat 4 files changed, 70 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/layer/PaintAssistant.cpp	Mon Mar 05 13:01:14 2007 +0000
+++ b/layer/PaintAssistant.cpp	Mon Mar 05 13:04:52 2007 +0000
@@ -25,7 +25,8 @@
 void
 PaintAssistant::paintVerticalLevelScale(QPainter &paint, QRect rect,
 					float minVal, float maxVal,
-                                        Scale scale, std::vector<int> *vy)
+                                        Scale scale, int &mult,
+                                        std::vector<int> *vy)
 {
     static float meterdbs[] = { -40, -30, -20, -15, -10,
                                 -5, -3, -2, -1, -0.5, 0 };
@@ -39,6 +40,27 @@
     int n = 10;
     if (vy) vy->clear();
 
+    float step = 0;
+    mult = 1;
+    if (scale == LinearScale) {
+        step = (maxVal - minVal) / n;
+        int round = 0, limit = 10000000;
+        do {
+            round = int(minVal + step * mult);
+            mult *= 10;
+        } while (!round && mult < limit);
+        if (round) {
+            mult /= 10;
+//            std::cerr << "\n\nstep goes from " << step;
+            step = float(round) / mult;
+            n = lrintf((maxVal - minVal) / step);
+            if (mult > 1) {
+                mult /= 10;
+            }
+//            std::cerr << " to " << step << " (n = " << n << ")" << std::endl;
+        }
+    }
+
     for (int i = 0; i <= n; ++i) {
         
         float val = 0.0, nval = 0.0;
@@ -47,15 +69,8 @@
         switch (scale) {
                 
         case LinearScale:
-            val = minVal + ((maxVal - minVal) * i) / n;
-            text = QString("%1").arg(val);//float(i) / n); // ... val
-/*
-            if (i == 0) text = "0.0";
-            else {
-                nval = -val;
-                if (i == n) text = "1.0";
-            }
-*/
+            val = (minVal + (i * step));
+            text = QString("%1").arg(mult * val);
             break;
             
         case MeterScale: // ... min, max
@@ -97,7 +112,8 @@
         if (spaceForLabel) {
             
             int tx = 3;
-            if (scale != LinearScale) {
+//            if (scale != LinearScale) {
+            if (paint.fontMetrics().width(text) < w - 10) {
                 tx = w - 10 - paint.fontMetrics().width(text);
             }
             
--- a/layer/PaintAssistant.h	Mon Mar 05 13:01:14 2007 +0000
+++ b/layer/PaintAssistant.h	Mon Mar 05 13:04:52 2007 +0000
@@ -28,7 +28,7 @@
 
     static void paintVerticalLevelScale(QPainter &p, QRect rect,
                                         float minVal, float maxVal,
-                                        Scale scale,
+                                        Scale scale, int &multRtn,
                                         std::vector<int> *markCoordRtns = 0);
 
     static int getYForValue(Scale scale, float value,
--- a/layer/SliceLayer.cpp	Mon Mar 05 13:01:14 2007 +0000
+++ b/layer/SliceLayer.cpp	Mon Mar 05 13:04:52 2007 +0000
@@ -436,7 +436,8 @@
 SliceLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
 {
     if (m_energyScale == LinearScale) {
-	return paint.fontMetrics().width("0.0") + 13;
+	return std::max(paint.fontMetrics().width("0.0") + 13,
+                        paint.fontMetrics().width("x10-10"));
     } else {
 	return std::max(paint.fontMetrics().width(tr("0dB")),
 			paint.fontMetrics().width(tr("-Inf"))) + 13;
@@ -460,10 +461,22 @@
 
     QRect actual(rect.x(), rect.y() + yorigin - h, rect.width(), h);
 
+    int mult = 1;
+
     PaintAssistant::paintVerticalLevelScale
         (paint, actual, thresh, 1.0 / m_gain,
          PaintAssistant::Scale(m_energyScale),
+         mult,
          const_cast<std::vector<int> *>(&m_scalePoints));
+
+    if (mult != 1 && mult != 0) {
+        int log = lrintf(log10f(mult));
+        QString a = tr("x10");
+        QString b = QString("%1").arg(-log);
+        paint.drawText(3, 8 + paint.fontMetrics().ascent(), a);
+        paint.drawText(3 + paint.fontMetrics().width(a),
+                       3 + paint.fontMetrics().ascent(), b);
+    }
 }
 
 Layer::PropertyList
@@ -840,7 +853,7 @@
 
 bool
 SliceLayer::getValueExtents(float &min, float &max, bool &logarithmic,
-                               QString &units) const
+                            QString &units) const
 {
     return false;
 }
--- a/layer/SpectrumLayer.cpp	Mon Mar 05 13:01:14 2007 +0000
+++ b/layer/SpectrumLayer.cpp	Mon Mar 05 13:04:52 2007 +0000
@@ -241,31 +241,6 @@
     }
 }
 
-QString
-SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
-{
-    QString s;
-    
-    s += QString("windowSize=\"%1\" "
-		 "windowHopLevel=\"%2\"")
-        .arg(m_windowSize)
-        .arg(m_windowHopLevel);
-
-    return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
-}
-
-void
-SpectrumLayer::setProperties(const QXmlAttributes &attributes)
-{
-    bool ok = false;
-
-    size_t windowSize = attributes.value("windowSize").toUInt(&ok);
-    if (ok) setWindowSize(windowSize);
-
-    size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
-    if (ok) setWindowHopLevel(windowHopLevel);
-}
-
 bool
 SpectrumLayer::getValueExtents(float &min, float &max, bool &logarithmic,
                                QString &units) const
@@ -363,4 +338,31 @@
 }
 
 
+QString
+SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
+{
+    QString s;
     
+    s += QString("windowSize=\"%1\" "
+		 "windowHopLevel=\"%2\"")
+        .arg(m_windowSize)
+        .arg(m_windowHopLevel);
+
+    return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
+}
+
+void
+SpectrumLayer::setProperties(const QXmlAttributes &attributes)
+{
+    SliceLayer::setProperties(attributes);
+
+    bool ok = false;
+
+    size_t windowSize = attributes.value("windowSize").toUInt(&ok);
+    if (ok) setWindowSize(windowSize);
+
+    size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
+    if (ok) setWindowHopLevel(windowHopLevel);
+}
+
+