[Kst] branches/kst/hfi_calib/kst/kst

George Staikos staikos at kde.org
Thu Feb 23 19:14:24 CET 2006


SVN commit 512828 by staikos:

make labels update properly in all cases except expression and vector index
changes, which are easy to add later.  This can be made much more efficient
once view object updates happen regularly.


 A             dataref.h   [License: GPL (v2+)]
 M  +91 -0     kstviewlabel.cpp  
 M  +12 -1     kstviewlabel.h  
 M  +17 -3     labelrenderer.cpp  
 M  +6 -0      labelrenderer.h  


--- branches/kst/hfi_calib/kst/kst/kstviewlabel.cpp #512827:512828
@@ -234,6 +234,9 @@
   RenderContext rc(_fontName, _absFontSize, &p);
   rc.setSubstituteScalars(_replace);
   rc.precision = _dataPrecision;
+  rc._cache = &_cache.data;
+  _cache.valid = false;
+  _cache.data.clear();
   double rotationRadians = M_PI * (int(_rotation) % 360) / 180;
   double absin = fabs(sin(rotationRadians));
   double abcos = fabs(cos(rotationRadians));
@@ -290,6 +293,7 @@
 #endif
   if (lp && lp->chunk) {
     renderLabel(rc, lp->chunk);
+    _cache.valid = true;
   }
 #ifdef BENCHMARK
   kstdDebug() << "render took: " << t.elapsed() << endl;
@@ -322,9 +326,43 @@
 }
 
 
+KstObject::UpdateType KstViewLabel::update(int counter) {
+  if (checkUpdateCounter(counter)) {
+    return lastUpdateResult();
+  }
+
+  KstObject::UpdateType rc = NO_CHANGE;
+
+  _cache.update();
+
+  if (!_cache.valid) {
+    rc = UPDATE;
+    setDirty();
+  }
+
+  if (rc != UPDATE) {
+    rc = KstBorderedViewObject::update(counter);
+  } else {
+    KstBorderedViewObject::update(counter);
+  }
+
+  return setLastUpdateResult(rc);
+}
+
+
 void KstViewLabel::updateSelf() {
   bool wasDirty(dirty());
   KstBorderedViewObject::updateSelf();
+
+  // FIXME: remove this once update() is called often enough.
+  if (!wasDirty) {
+    _cache.update();
+
+    if (!_cache.valid) {
+      wasDirty = true;
+    }
+  }
+
   if (wasDirty) {
     if (_autoResize) {
       adjustSizeForText(_parent->geometry());
@@ -783,5 +821,58 @@
 }
 
 
+void KstViewLabel::DataCache::update() {
+  for (QValueVector<DataRef>::ConstIterator i = data.begin(); valid && i != data.end(); ++i) {
+    switch ((*i).type) {
+      case DataRef::DataRef::DRScalar:
+        {
+          KST::scalarList.lock().readLock();
+          KstScalarPtr p = *KST::scalarList.findTag((*i).name);
+          KST::scalarList.lock().readUnlock();
+          if (p) {
+            p->readLock();
+            if (QVariant(p->value()) != (*i).value) {
+              valid = false;
+            }
+            p->readUnlock();
+          }
+        }
+        break;
+      case DataRef::DRString:
+        {
+          KST::stringList.lock().readLock();
+          KstStringPtr p = *KST::stringList.findTag((*i).name);
+          KST::stringList.lock().readUnlock();
+          if (p) {
+            p->readLock();
+            if (QVariant(p->value()) != (*i).value) {
+              valid = false;
+            }
+            p->readUnlock();
+          }
+        }
+        break;
+      case DataRef::DRExpression:
+      // FIXME: implement
+        abort();
+      case DataRef::DRVector:
+        {
+          KST::vectorList.lock().readLock();
+          KstVectorPtr p = *KST::vectorList.findTag((*i).name);
+          KST::vectorList.lock().readUnlock();
+          // FIXME: check the index for a change also
+          if (p) {
+            p->readLock();
+            if (QVariant(p->value(int((*i).indexValue))) != (*i).value) {
+              valid = false;
+            }
+            p->readUnlock();
+          }
+        }
+        break;
+    }
+  }
+}
+
 #include "kstviewlabel.moc"
 // vim: ts=2 sw=2 et
--- branches/kst/hfi_calib/kst/kst/kstviewlabel.h #512827:512828
@@ -19,12 +19,14 @@
 #ifndef KSTVIEWLABEL_H
 #define KSTVIEWLABEL_H
 
+#include "dataref.h"
 #include "kstbackbuffer.h"
 #include "kstborderedviewobject.h"
 #include "kstscalar.h"
 #include "labelparser.h"
 
 #include <qguardedptr.h>
+#include <qvaluevector.h>
 
 class KstViewLabel : public KstBorderedViewObject {
   Q_OBJECT
@@ -101,6 +103,8 @@
     bool fillConfigWidget(QWidget *w, bool isNew) const;
     bool readConfigWidget(QWidget *w);
 
+    KstObject::UpdateType update(int counter);
+
   public slots:
     void adjustSizeForText(QRect w);
     void reparse();
@@ -119,7 +123,6 @@
     double _rotation;
     QString _txt;
     QString _fontName;
-    KstScalarList _scalarsUsed;
 
     bool _replace : 1;
     bool _interpret : 1;
@@ -133,6 +136,14 @@
     Label::Parsed *_parsed;
     QRegion _myClipMask;
     int _labelMargin;
+
+    struct DataCache {
+      DataCache() : valid(false) {}
+      bool valid;
+      QValueVector<DataRef> data;
+      void update();
+    };
+    DataCache _cache;
 };
 
 typedef KstSharedPtr<KstViewLabel> KstViewLabelPtr;
--- branches/kst/hfi_calib/kst/kst/labelrenderer.cpp #512827:512828
@@ -74,7 +74,11 @@
       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), 'g', rc.precision);
+        const double eqResult(Equation::interpret(fi->text.mid(1).latin1(), &ok));
+        txt = QString::number(eqResult, 'g', rc.precision);
+        if (rc._cache) {
+          rc._cache->append(DataRef(DataRef::DRExpression, fi->text, QString::null, 0.0, QVariant(eqResult)));
+        }
       } else {
         KST::scalarList.lock().readLock();
         KstScalarPtr scp = *KST::scalarList.findTag(fi->text);
@@ -82,6 +86,9 @@
         if (scp) {
           scp->readLock();
           txt = QString::number(scp->value(), 'g', rc.precision);
+          if (rc._cache) {
+            rc._cache->append(DataRef(DataRef::DRScalar, fi->text, QString::null, 0.0, QVariant(scp->value())));
+          }
           scp->readUnlock();
         } else {
           KST::stringList.lock().readLock();
@@ -90,6 +97,9 @@
           if (stp) {
             stp->readLock();
             txt = stp->value();
+            if (rc._cache) {
+              rc._cache->append(DataRef(DataRef::DRString, fi->text, QString::null, 0.0, QVariant(stp->value())));
+            }
             stp->readUnlock();
           }
         }
@@ -108,10 +118,14 @@
           // Parse and evaluate as an equation
           bool ok = false;
           // FIXME: make more efficient: cache the parsed equation
-          double idx = Equation::interpret(fi->expression.latin1(), &ok);
+          const double idx = Equation::interpret(fi->expression.latin1(), &ok);
           if (ok) {
             vp->readLock();
-            txt = QString::number(vp->value()[int(idx)], 'g', rc.precision);
+            const double vVal(vp->value()[int(idx)]);
+            txt = QString::number(vVal, 'g', rc.precision);
+            if (rc._cache) {
+              rc._cache->append(DataRef(DataRef::DRVector, fi->text, fi->expression, idx, QVariant(vVal)));
+            }
             vp->readUnlock();
           } else {
             txt = "NAN";
--- branches/kst/hfi_calib/kst/kst/labelrenderer.h #512827:512828
@@ -20,8 +20,12 @@
 
 #include <qfont.h>
 #include <qpainter.h>
+#include <qpair.h>
 #include <qstring.h>
+#include <qvariant.h>
+#include <qvaluevector.h>
 
+#include "dataref.h"
 #include "kst_export.h"
 
 // inline for speed.
@@ -31,6 +35,7 @@
     x = y = xMax = xStart = 0;
     ascent = descent = 0;
     precision = 8;
+    _cache = 0L;
     substitute = true;
     setFont(QFont(fontName, fontSize));
   }
@@ -99,6 +104,7 @@
   QPainter *p;
   int precision;
   bool substitute;
+  QValueVector<DataRef> *_cache;
   
   private:
     QFont _font;


More information about the Kst mailing list