[Kst] branches/work/kst/portto4/kst/src/libkstapp

Barth Netterfield netterfield at astro.utoronto.ca
Fri May 27 20:37:07 CEST 2011


SVN commit 1233799 by netterfield:

Scientific Notation for log mode.

When in log mode, the numbers will be 10^i.

This is the RightThing (tm) except, maybe, for 10^0 (ie 1) and 10^1 (ie, 10).

What we do for 1 and 10 may need some discussion.


 M  +2 -1      plotaxis.cpp  
 M  +28 -8     plotitem.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/plotaxis.cpp #1233798:1233799
@@ -684,7 +684,8 @@
     if (majorPoint == 0) majorPoint = -350;
     if (i >= min && i <= max) {
       ticks << majorPoint;
-      labels.insert(majorPoint, QString::number(majorPoint, 'g', format_precision));
+      // 'x' is a sign to the plot item to write 10^i rather than 1.0x10^i
+      labels.insert(majorPoint, QString("xe%1").arg(i));
     }
 
     if (tick == 1.0) {
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #1233798:1233799
@@ -1369,19 +1369,33 @@
   QRectF r = rec;
   QStringList base_mantisa = text.split('e');
 
+  //painter->drawRect(r);
+
   if (base_mantisa.size()<=1) {
     painter->drawText(r, flags, text);
   } else {
+    QString base;
+    if (base_mantisa[0]==QString('x')) {
+      base = "10";
+      base_mantisa[0].clear();
+    } else {
+      base = "x10";
+    }
+    base_mantisa[1].remove('+');
+    if (base_mantisa[1].contains(']')) {
+      base_mantisa[1].remove(']');
+      base_mantisa.append(QString(']'));
+    }
+    qreal w = painter->fontMetrics().width(base_mantisa[0] + base) +
+        painter->fontMetrics().width(base_mantisa[1])*superscript_scale;
+    if (base_mantisa.size()>2) {
+      w += painter->fontMetrics().width(base_mantisa[2]);
+    }
     if (flags & Qt::AlignRight) {
-      qreal w = painter->fontMetrics().width(base_mantisa[0] + "x10") +
-          painter->fontMetrics().width(base_mantisa[1].remove('+'))*superscript_scale;
       qreal right = r.right();
       r.setWidth(w);
       r.moveRight(right);
-
     } else if (flags & Qt::AlignCenter) {
-      qreal w = painter->fontMetrics().width(base_mantisa[0] + "x10") +
-          painter->fontMetrics().width(base_mantisa[1].remove('+'))*superscript_scale;
       QPointF center = r.center();
       r.setWidth(w);
       r.moveCenter(center);
@@ -1390,17 +1404,23 @@
     //painter->drawRect(r);
     painter->drawText(p, base_mantisa[0]);
     p.setX(p.x() + painter->fontMetrics().width(base_mantisa[0]));
-    painter->drawText(p,"x10");
-    p.setX(p.x() + painter->fontMetrics().width("x10"));
+    painter->drawText(p,base);
+    qreal ly = p.y();
+    p.setX(p.x() + painter->fontMetrics().width(base));
     p.setY(p.y() - superscript_raise * painter->fontMetrics().height());
     painter->save();
     QFont f = painter->font();
     f.setPointSizeF(f.pointSizeF()*superscript_scale); // FIXME
     painter->setFont(f);
-    painter->drawText(p,base_mantisa[1].remove('+'));
+    painter->drawText(p,base_mantisa[1]);
+    p.setX(p.x() + painter->fontMetrics().width(base_mantisa[1]));
     painter->restore();
+    if (base_mantisa.size()>2) {
+      p.setY(ly);
+      painter->drawText(p,base_mantisa[2]);
   }
 }
+}
 
 void PlotItem::paintBottomTickLabels(QPainter *painter) {
   int flags = Qt::TextSingleLine/* | Qt::AlignCenter*/;


More information about the Kst mailing list