Bug 105915

Summary: Displayed scalars have no way of controling precision
Product: [Applications] kst Reporter: Matthew Truch <matt>
Component: generalAssignee: George Staikos <staikos>
Status: RESOLVED FIXED    
Severity: wishlist CC: kst
Priority: NOR    
Version: 1.x   
Target Milestone: ---   
Platform: Slackware   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Matthew Truch 2005-05-19 00:02:01 UTC
Version:           1.1.0-svn (using KDE KDE 3.4.0)
Installed from:    Slackware Packages
OS:                Linux

Displayed scalars (and most other values) have no way of specifying the displayed precision.  Furthermore, if there are any trailing zeroes, they are not displayed.  For example, if I show the mean of a vector while displaying live data is may show 1.0001, and a moment later show 1, and then show 0.9999.  

It would be best if a precision could be specified with the same kind of options that printf provides (number of digits, trailing and leading zeroes etc).
Comment 1 Andrew Walker 2005-05-25 19:11:54 UTC
Is this referring to displaying scalar values in labels?
Anywhere else that this spplies to?
Comment 2 Matthew Truch 2005-05-25 19:40:58 UTC
Yes, I was specifically referring to scalars displayed in labels.  I'm not sure if it's as important for the status bar (cursor position/data mode), and there it might be confusing if the precision is turned down (causing the numbers to not change when the mouse is moved in certain circumstances).  
Comment 3 George Staikos 2005-08-08 21:17:33 UTC
I'll implement this in KstViewLabel, but it won't go into plot labels until they are switched over to the new renderer.
Comment 4 George Staikos 2005-08-08 21:45:19 UTC
SVN commit 444098 by staikos:

Add support for controlling the precision of data values displayed in labels.
Does not affect 2dplot labels yet since they don't use the new label renderer.
FEATURE: 105915


 M  +27 -0     extensions/js/bind_label.cpp  
 M  +7 -0      extensions/js/bind_label.h  
 M  +21 -0     kstviewlabel.cpp  
 M  +6 -2      kstviewlabel.h  
 M  +3 -3      labelrenderer.cpp  
 M  +2 -0      labelrenderer.h  


--- trunk/extragear/graphics/kst/kst/extensions/js/bind_label.cpp #444097:444098
@@ -116,6 +116,7 @@
   { "fontSize", &KstBindLabel::setFontSize, &KstBindLabel::fontSize },
   { "justification", &KstBindLabel::setJustification, &KstBindLabel::justification },
   { "rotation", &KstBindLabel::setRotation, &KstBindLabel::rotation },
+  { "dataPrecision", &KstBindLabel::setDataPrecision, &KstBindLabel::dataPrecision },
   { "interpreted", &KstBindLabel::setInterpreted, &KstBindLabel::interpreted },
   { "scalarReplacement", &KstBindLabel::setScalarReplacement, &KstBindLabel::scalarReplacement },
   { "autoResize", &KstBindLabel::setAutoResize, &KstBindLabel::autoResize },
@@ -363,6 +364,32 @@
 }
 
 
+void KstBindLabel::setDataPrecision(KJS::ExecState *exec, const KJS::Value& value) {
+  if (value.type() != KJS::NumberType) {
+    KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+    exec->setException(eobj);
+    return;
+  }
+  KstViewLabelPtr d = makeLabel(_d);
+  if (d) {
+    KstWriteLocker wl(d);
+    d->setDataPrecision(value.toNumber(exec));
+    KstApp::inst()->paintAll(P_PAINT);
+  }
+}
+
+
+KJS::Value KstBindLabel::dataPrecision(KJS::ExecState *exec) const {
+  Q_UNUSED(exec)
+  KstViewLabelPtr d = makeLabel(_d);
+  if (d) {
+    KstReadLocker rl(d);
+    return KJS::Number(d->dataPrecision());
+  }
+  return KJS::Number(0);
+}
+
+
 void KstBindLabel::setInterpreted(KJS::ExecState *exec, const KJS::Value& value) {
   if (value.type() != KJS::BooleanType) {
     KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
--- trunk/extragear/graphics/kst/kst/extensions/js/bind_label.h #444097:444098
@@ -101,6 +101,13 @@
     */
     void setRotation(KJS::ExecState *exec, const KJS::Value& value);
     KJS::Value rotation(KJS::ExecState *exec) const;
+    /* @property number dataPrecision
+       @description Contains the number of digits of precision to display data
+                    values in the label.  Restricted to a value between 0 and
+                    16 inclusive.
+    */
+    void setDataPrecision(KJS::ExecState *exec, const KJS::Value& value);
+    KJS::Value dataPrecision(KJS::ExecState *exec) const;
     /* @property boolean interpreted
        @description Determines if the contents of the label should be
                     interpreted in order to substitute LaTeX-like
--- trunk/extragear/graphics/kst/kst/kstviewlabel.cpp #444097:444098
@@ -46,6 +46,7 @@
 
 KstViewLabel::KstViewLabel(const QString& txt, KstLJustifyType justify, float rotation)
 : KstBorderedViewObject("Label") {
+  _dataPrecision = 8;
   _autoResize = false; // avoid madness
   _txt = txt;
   _interpret = true;
@@ -208,6 +209,7 @@
   }
 
   RenderContext rc(_fontName, _symbolFontName, _fontSize, &p);
+  rc.precision = _dataPrecision;
   double rotationRadians = M_PI * (int(_rotation) % 360) / 180;
   double absin = fabs(sin(rotationRadians));
   double abcos = fabs(cos(rotationRadians));
@@ -259,6 +261,7 @@
 
 void KstViewLabel::computeTextSize(Label::Parsed *lp) {
   RenderContext rc(_fontName, _symbolFontName, _fontSize, 0L);
+  rc.precision = _dataPrecision;
   renderLabel(rc, lp->chunk);
   _textWidth = rc.xMax;
   _ascent = rc.ascent;
@@ -451,9 +454,27 @@
   map.insert(QString("Font"), qMakePair(QString("Font"), QString("KFontCombo")));
   map.insert(QString("ForeColor"), qMakePair(QString("Font color"), QString("KColorButton")));
   map.insert(QString("BackColor"), qMakePair(QString("Background color"), QString("KColorButton")));
+  map.insert(QString("DataPrecision"), qMakePair(QString("Data precision"), QString("QSpinBox")));
   return map;
 }
 
 
+void KstViewLabel::setDataPrecision(int prec) {
+  int n = kClamp(prec, 0, 16);
+  if (n != _dataPrecision) {
+    setDirty();
+    _dataPrecision = n;
+    drawToBuffer(_parsed);
+    if (_autoResize) {
+      adjustSizeForText();
+    }
+  }
+}
+
+
+int KstViewLabel::dataPrecision() const {
+  return _dataPrecision;
+}
+
 #include "kstviewlabel.moc"
 // vim: ts=2 sw=2 et
--- trunk/extragear/graphics/kst/kst/kstviewlabel.h #444097:444098
@@ -33,9 +33,9 @@
   Q_PROPERTY(QString Text READ text WRITE setText)
   Q_PROPERTY(double Rotation READ rotation WRITE setRotation)  
   Q_PROPERTY(QString Font READ fontName WRITE setFontName)
+  Q_PROPERTY(int DataPrecision READ dataPrecision WRITE setDataPrecision)
   public:
-    KstViewLabel(const QString& txt, KstLJustifyType justify = 0L,
-        float rotation = 0.0);
+    KstViewLabel(const QString& txt, KstLJustifyType justify = 0L, float rotation = 0.0);
     virtual ~KstViewLabel();
 
     void setText(const QString& text);
@@ -67,6 +67,9 @@
     void setDoScalarReplacement(bool in_do);
     bool doScalarReplacement() const;
 
+    void setDataPrecision(int prec);
+    int dataPrecision() const;
+
     virtual void paint(KstPaintType type, QPainter& p);
     virtual void resize(const QSize&);
     virtual QRegion clipRegion();
@@ -99,6 +102,7 @@
     bool _interpret : 1;
     bool _autoResize : 1;
     unsigned int _fontSize : 8; // points
+    int _dataPrecision : 6;
     int _textWidth, _textHeight, _ascent;
     KstLJustifyType _justify;
     KstBackBuffer _backBuffer;
--- trunk/extragear/graphics/kst/kst/labelrenderer.cpp #444097:444098
@@ -67,14 +67,14 @@
       if (!fi->text.isEmpty() && fi->text[0] == '=') {
         // Parse and evaluate as an equation
         bool ok = false;
-        txt = QString::number(Equation::interpret(fi->text.mid(1).latin1(), &ok));
+        txt = QString::number(Equation::interpret(fi->text.mid(1).latin1(), &ok), 'g', rc.precision);
       } else {
         KST::scalarList.lock().readLock();
         KstScalarPtr scp = *KST::scalarList.findTag(fi->text);
         KST::scalarList.lock().readUnlock();
         if (scp) {
           scp->readLock();
-          txt = QString::number(scp->value());
+          txt = QString::number(scp->value(), 'g', rc.precision);
           scp->readUnlock();
         } else {
           KST::stringList.lock().readLock();
@@ -104,7 +104,7 @@
           double idx = Equation::interpret(fi->expression.latin1(), &ok);
           if (ok) {
             vp->readLock();
-            txt = QString::number(vp->value()[int(idx)]);
+            txt = QString::number(vp->value()[int(idx)], 'g', rc.precision);
             vp->readUnlock();
           } else {
             txt = "NAN";
--- trunk/extragear/graphics/kst/kst/labelrenderer.h #444097:444098
@@ -28,6 +28,7 @@
   : fontName(fontName), symbolFontName(symbolFontName), size(fontSize), p(p), prevSym(false), _fm(_font) {
     x = y = xMax = xStart = 0;
     ascent = descent = 0;
+    precision = 8;
     if (p) {
       p->setFont(QFont(fontName, fontSize));
     } else {
@@ -92,6 +93,7 @@
   int size;
   QPainter *p;
   bool prevSym;
+  int precision;
   
   private:
   QFont _font;