[Kst] extragear/graphics/kst/kst

George Staikos staikos at kde.org
Thu Jun 16 18:28:03 CEST 2005


SVN commit 426171 by staikos:

scalar substitution and inline equation evaluation


 M  +10 -0     extensions/js/examples/testlabels.js  
 M  +71 -8     kstviewlabel.cpp  


--- trunk/extragear/graphics/kst/kst/extensions/js/examples/testlabels.js #426170:426171
@@ -16,4 +16,14 @@
 lc.font = "verdana";
 lc.fontSize = 26;
 
+ld = new Label(w);
+ld.text = "[CONST_PI]";
+ld.position = new Point(100, 194);
+ld.fontSize = 10;
 
+
+le = new Label(w);
+le.text = "[=2*sqrt(2)/sin(sqrt(2))^2]";
+le.position = new Point(100, 234);
+
+
--- trunk/extragear/graphics/kst/kst/kstviewlabel.cpp #426170:426171
@@ -16,16 +16,21 @@
  *                                                                         *
  ***************************************************************************/
 
+#include "kstviewlabel.h"
+
+#include "enodes.h"
+#include "kstdatacollection.h"
+#include "kstviewlabeldialog_i.h"
+
 #include <kdatastream.h>
 #include <kdebug.h>
 #include <klocale.h>
 #include <kpopupmenu.h>
-#include "kstviewlabeldialog_i.h"
-#include "kstviewlabel.h"
+
 #include <qptrstack.h>
 
-KstViewLabel::KstViewLabel(const QString& txt, KstLJustifyType justify,
-                           float rotation)
+
+KstViewLabel::KstViewLabel(const QString& txt, KstLJustifyType justify, float rotation)
 : KstBorderedViewObject("KstViewLabel") {
   _txt = txt;
   _interpret = true;
@@ -233,8 +238,38 @@
       f.setFamily(fi->symbol ? _symbolFontName : _fontName);
       p.setFont(f);
     }
-    p.drawText(x, y, fi->text);
-    x += p.fontMetrics().width(fi->text);
+
+    if (fi->scalar) { // Do scalar/string substitution
+      QString txt;
+      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));
+      } 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());
+          scp->readUnlock();
+        } else {
+          KST::stringList.lock().readLock();
+          KstStringPtr stp = *KST::stringList.findTag(fi->text);
+          KST::stringList.lock().readUnlock();
+          if (stp) {
+            stp->readLock();
+            txt = stp->value();
+            stp->readUnlock();
+          }
+        }
+      }
+      p.drawText(x, y, txt);
+      x += p.fontMetrics().width(txt);
+    } else {
+      p.drawText(x, y, fi->text);
+      x += p.fontMetrics().width(fi->text);
+    }
     fi = fi->next;
   }
 }
@@ -282,9 +317,37 @@
 
     QFontMetrics fm(fnt);
 
-    _textWidth += fm.width(fi->text);
+    if (fi->scalar) { // Do scalar/string substitution
+      QString txt;
+      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));
+      } 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());
+          scp->readUnlock();
+        } else {
+          KST::stringList.lock().readLock();
+          KstStringPtr stp = *KST::stringList.findTag(fi->text);
+          KST::stringList.lock().readUnlock();
+          if (stp) {
+            stp->readLock();
+            txt = stp->value();
+            stp->readUnlock();
+          }
+        }
+      }
+      _textWidth += fm.width(txt);
+    } else {
+      _textWidth += fm.width(fi->text);
+    }
     asc = QMAX(asc, - y + fm.ascent());
-    if (- y - fm.descent() < 0) {
+    if (-y - fm.descent() < 0) {
       des = QMAX(des, fm.descent() + y);
     }
 


More information about the Kst mailing list