[Kst] branches/work/kst/1.6/kst/src

Andrew Walker arwalker at sumusltd.com
Fri Nov 16 20:06:06 CET 2007


SVN commit 737607 by arwalker:

first draft of KstBindImage which allows access to images via javaScript

 M  +1 -0      extensions/js/Makefile.am  
 M  +1 -0      extensions/js/bind_curve.cpp  
 A             extensions/js/bind_image.cpp   [License: GPL (v2+)]
 A             extensions/js/bind_image.h   [License: GPL (v2+)]
 M  +8 -4      extensions/js/bind_matrix.h  
 M  +2 -3      extensions/js/bind_plot.cpp  
 M  +3 -3      extensions/js/js.cpp  
 M  +42 -2     extensions/js/kstbinding.cpp  
 M  +2 -3      extensions/js/kstbinding.h  
 M  +9 -4      libkst/kstmatrix.cpp  
 M  +4 -8      libkstapp/kst2dplotwidget_i.cpp  
 M  +3 -4      libkstmath/kstcsd.cpp  
 M  +26 -27    libkstmath/kstimage.cpp  


--- branches/work/kst/1.6/kst/src/extensions/js/Makefile.am #737606:737607
@@ -57,6 +57,7 @@
 			    bind_datasourcecollection.cpp \
 			    bind_histogram.cpp \
 			    bind_matrix.cpp \
+			    bind_image.cpp \
 			    bind_datamatrix.cpp \
 			    bind_debug.cpp \
 			    bind_debuglog.cpp \
--- branches/work/kst/1.6/kst/src/extensions/js/bind_curve.cpp #737606:737607
@@ -62,6 +62,7 @@
 
 KJS::Object KstBindCurve::construct(KJS::ExecState *exec, const KJS::List& args) {
   KstVectorPtr x, y, ex, ey, exm, eym;
+
   if (args.size() > 0) {
     x = extractVector(exec, args[0]);
     if (!x) {
--- branches/work/kst/1.6/kst/src/extensions/js/bind_matrix.h #737606:737607
@@ -55,11 +55,13 @@
                                editable.
      */
     KJS::Value resize(KJS::ExecState *exec, const KJS::List& args);
+
     /* @method zero
        @description Sets all values in the matrix to zero.
        @exception GeneralError Throws this exception if the matrix is not editable.
      */
     KJS::Value zero(KJS::ExecState *exec, const KJS::List& args);
+
     /* @method update
        @description Updates the statistical values associated with a matrix.
        @exception GeneralError Throws this exception if the matrix is not editable.
@@ -72,41 +74,43 @@
        checked before attempting to modify a matrix in any way.
      */
     KJS::Value editable(KJS::ExecState *exec) const;
+
     /* @property number min
        @readonly
        @description The value of the smallest sample in the matrix.
      */
     KJS::Value min(KJS::ExecState *exec) const;
+
     /* @property number max
        @readonly
        @description The value of the largest sample in the matrix.
      */
     KJS::Value max(KJS::ExecState *exec) const;
+
     /* @property number mean
        @readonly
        @description The mean value of all samples in the matrix.
      */
     KJS::Value mean(KJS::ExecState *exec) const;
+
     /* @property number numNew
        @readonly
      */
     KJS::Value numNew(KJS::ExecState *exec) const;
+
     /* @property number rows
        @readonly
      */
     KJS::Value rows(KJS::ExecState *exec) const;
+
     /* @property number columns
        @readonly
      */
     KJS::Value columns(KJS::ExecState *exec) const;
 
-
   protected:
     KstBindMatrix(int id, const char *name = 0L);
     void addBindings(KJS::ExecState *exec, KJS::Object& obj);
 };
 
-
 #endif
-
-// vim: ts=2 sw=2 et
--- branches/work/kst/1.6/kst/src/extensions/js/bind_plot.cpp #737606:737607
@@ -128,7 +128,7 @@
 
 KJS::ReferenceList KstBindPlot::propList(KJS::ExecState *exec, bool recursive) {
   KJS::ReferenceList rc = KstBindBorderedViewObject::propList(exec, recursive);
-  
+
   for (int i = 0; plotProperties[i].name; ++i) {
     rc.append(KJS::Reference(this, KJS::Identifier(plotProperties[i].name)));
   }
@@ -220,6 +220,7 @@
   }
 }
 
+
 int KstBindPlot::methodCount() const {
   return sizeof plotBindings + KstBindBorderedViewObject::methodCount();
 }
@@ -358,7 +359,5 @@
   return KJS::Boolean(false);
 }
 
-
 #undef makePlot
 
-// vim: ts=2 sw=2 et
--- branches/work/kst/1.6/kst/src/extensions/js/js.cpp #737606:737607
@@ -55,6 +55,7 @@
 #include "bind_ellipse.h"
 #include "bind_file.h"
 #include "bind_histogram.h"
+#include "bind_image.h"
 #include "bind_jsdataobject.h"
 #include "bind_kst.h"
 #include "bind_label.h"
@@ -181,12 +182,13 @@
 
   new KstBindPoint(exec, &globalObj);
   new KstBindSize(exec, &globalObj);
-  
+
   new KstBindVector(exec, &globalObj);
   new KstBindScalar(exec, &globalObj);
   new KstBindString(exec, &globalObj);
   new KstBindDataSource(exec, &globalObj);
   new KstBindDataVector(exec, &globalObj);
+  new KstBindImage(exec, &globalObj);
   new KstBindMatrix(exec, &globalObj);
   new KstBindDataMatrix(exec, &globalObj);
 
@@ -405,6 +407,4 @@
   return b;
 }
 
-
 #include "js.moc"
-
--- branches/work/kst/1.6/kst/src/extensions/js/kstbinding.cpp #737606:737607
@@ -3,6 +3,7 @@
                              -------------------
     begin                : Mar 23 2005
     copyright            : (C) 2005 The University of Toronto
+                           (C) 2007 The University of British Columbia
     email                :
  ***************************************************************************/
 
@@ -21,6 +22,7 @@
 #include "bind_curvecollection.h"
 #include "bind_datasource.h"
 #include "bind_datavector.h"
+#include "bind_matrix.h"
 #include "bind_object.h"
 #include "bind_plot.h"
 #include "bind_pluginmodule.h"
@@ -255,6 +257,46 @@
 }
 
 
+KstMatrixPtr KstBinding::extractMatrix(KJS::ExecState *exec, const KJS::Value& value, bool doThrow) {
+  switch (value.type()) {
+    case KJS::ObjectType:
+      {
+        KstMatrixPtr mp;
+        KstBindMatrix *imp = dynamic_cast<KstBindMatrix*>(value.toObject(exec).imp());
+        if (imp) {
+          mp = kst_cast<KstMatrix>(imp->_d);
+        } else {
+          KstBindMatrix *imp = dynamic_cast<KstBindMatrix*>(value.toObject(exec).imp());
+          if (imp) {
+            mp = kst_cast<KstMatrix>(imp->_d);
+          }
+        }
+        if (!mp && doThrow) {
+          KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+          exec->setException(eobj);
+        }
+        return mp;
+      }
+    case KJS::StringType:
+      {
+        KST::matrixList.lock().readLock();
+        KstMatrixPtr mp = *KST::matrixList.findTag(value.toString(exec).qstring());
+        KST::matrixList.lock().unlock();
+        if (mp) {
+          return mp;
+        }
+      }
+      // fall through and throw
+    default:
+      if (doThrow) {
+        KJS::Object eobj = KJS::Error::create(exec, KJS::TypeError);
+        exec->setException(eobj);
+      }
+      return 0L;
+  }
+}
+
+
 KstViewWindow *KstBinding::extractWindow(KJS::ExecState *exec, const KJS::Value& value, bool doThrow) {
   switch (value.type()) {
     case KJS::ObjectType:
@@ -411,5 +453,3 @@
   return 0;
 }
 
-
-// vim: ts=2 sw=2 et
--- branches/work/kst/1.6/kst/src/extensions/js/kstbinding.h #737606:737607
@@ -3,6 +3,7 @@
                              -------------------
     begin                : Mar 23 2005
     copyright            : (C) 2005 The University of Toronto
+                           (C) 2007 The University of British Columbia
     email                :
  ***************************************************************************/
 
@@ -54,6 +55,7 @@
     static KstSharedPtr<Plugin> extractPluginModule(KJS::ExecState*, const KJS::Value&, bool doThrow = true);
     static KstVectorPtr extractVector(KJS::ExecState*, const KJS::Value&, bool doThrow = true);
     static KstVCurvePtr extractVCurve(KJS::ExecState*, const KJS::Value&, bool doThrow = true);
+    static KstMatrixPtr extractMatrix(KJS::ExecState*, const KJS::Value&, bool doThrow = true);
     static KstViewWindow *extractWindow(KJS::ExecState*, const KJS::Value&, bool doThrow = true);
     static Kst2DPlotPtr extractPlot(KJS::ExecState*, const KJS::Value&, bool doThrow = true);
     static KstViewObjectPtr extractViewObject(KJS::ExecState*, const KJS::Value&, bool doThrow = true);
@@ -68,7 +70,4 @@
     int _id;
 };
 
-
 #endif
-
-// vim: ts=2 sw=2 et
--- branches/work/kst/1.6/kst/src/libkst/kstmatrix.cpp #737606:737607
@@ -80,7 +80,7 @@
   KST::scalarList.setUpdateDisplayTags(false);
   for (QDictIterator<KstScalar> iter(_statScalars); iter.current(); ++iter) {
     KST::scalarList.remove(iter.current());
-    iter.current()->_KShared_unref();  
+    iter.current()->_KShared_unref();
   }
   KST::scalarList.setUpdateDisplayTags(true);
   KST::scalarList.lock().unlock();
@@ -114,7 +114,7 @@
     return 0.0;
   }
   if (ok) {
-    (*ok) = true;  
+    (*ok) = true;
   }
   return _z[index];
 }
@@ -148,6 +148,7 @@
   return true;
 }
 
+
 double KstMatrix::minValue() const {
   return _statScalars["min"]->value();
 }
@@ -157,6 +158,7 @@
   return _statScalars["max"]->value();
 }
 
+
 double KstMatrix::minValueNoSpike() const {
   // FIXME: it is expensive to calcNoSpikeRange
   // so we have chosen here to only call it expicitly
@@ -166,16 +168,17 @@
   return _minNoSpike;
 }
 
+
 double KstMatrix::maxValueNoSpike() const {
   // FIXME: it is expensive to calcNoSpikeRange
   // so we have chosen here to only call it expicitly
   // and no attempt is made to check if it is still up to date...
   // It would be better to have these calls call 
   // calcNoSpikeRange iff the values were obsolete.
-
   return _maxNoSpike;
 }
 
+
 void KstMatrix::calcNoSpikeRange(double per) {
   double *min_list, *max_list, min_of_max, max_of_min;
   int n_list;
@@ -270,14 +273,17 @@
   free(max_list);
 }
 
+
 double KstMatrix::meanValue() const {
   return _statScalars["mean"]->value();
 }
 
+
 double KstMatrix::minValuePositive() const {
   return _statScalars["minpos"]->value();
 }
 
+
 int KstMatrix::numNew() const {
   return _numNew;
 }
@@ -557,5 +563,4 @@
   setDirty();
 }
 
-
 #include "kstmatrix.moc"
--- branches/work/kst/1.6/kst/src/libkstapp/kst2dplotwidget_i.cpp #737606:737607
@@ -291,8 +291,7 @@
 }
 
 
-void Kst2dPlotWidget::addDisplayedCurve()
-{
+void Kst2dPlotWidget::addDisplayedCurve() {
   uint count = AvailableCurveList->count();
   if (count > 0) {
     for (int i = count-1; i >= 0; i--) {
@@ -308,8 +307,7 @@
 }
 
 
-void Kst2dPlotWidget::removeDisplayedCurve()
-{
+void Kst2dPlotWidget::removeDisplayedCurve() {
   uint count = DisplayedCurveList->count();
   if (count > 0) {
     for (int i = count-1; i >= 0; i--) {
@@ -325,16 +323,14 @@
 }
 
 
-void Kst2dPlotWidget::upDisplayedCurve()
-{
+void Kst2dPlotWidget::upDisplayedCurve() {
   if (DisplayedCurveList->up()) {
     emit changed();
   }
 }
 
 
-void Kst2dPlotWidget::downDisplayedCurve()
-{
+void Kst2dPlotWidget::downDisplayedCurve() {
   if (DisplayedCurveList->down()) {
     emit changed();
   }
--- branches/work/kst/1.6/kst/src/libkstmath/kstcsd.cpp #737606:737607
@@ -16,9 +16,6 @@
  *                                                                         *
  ***************************************************************************/
 
-/** A class for handling spectrograms for kst
- */
-
 #include <assert.h>
 #include <math.h>
 
@@ -40,6 +37,7 @@
 static const QString& OUTMATRIX = KGlobal::staticQString("M");
 
 #define KSTCSDMAXLEN 27
+
 KstCSD::KstCSD(const QString &in_tag, KstVectorPtr in_V,
                double in_freq, bool in_average, bool in_removeMean, bool in_apodize, 
                ApodizeFunction in_apodizeFxn, int in_windowSize, int in_averageLength, double in_gaussianSigma, 
@@ -57,7 +55,8 @@
 
   QString in_tag;
   QString vecName;
-  QString in_vectorUnits, in_rateUnits;
+  QString in_vectorUnits;
+  QString in_rateUnits;
   KstVectorPtr in_V;
   double in_freq = 60.0;
   bool in_average = true;
--- branches/work/kst/1.6/kst/src/libkstmath/kstimage.cpp #737606:737607
@@ -212,7 +212,7 @@
   if (_inputMatrices.contains(THEMATRIX)) {
     KstMatrixPtr mp = _inputMatrices[THEMATRIX];
     bool updated = UPDATE == mp->update(update_counter);
-  
+
     if (updated || force) {
       // stats
       NS = mp->sampleCount();
@@ -270,8 +270,9 @@
 
 QColor KstImage::getMappedColor(double x, double y) {
   bool ok;
-  
-  double z = _inputMatrices[THEMATRIX]->value(x, y, &ok);
+  double z;
+
+  z = _inputMatrices[THEMATRIX]->value(x, y, &ok);
   if (ok) {
     int index;
     if (_zUpper - _zLower != 0) {
@@ -280,7 +281,7 @@
       } else if (z < _zLower) {
         index = 0;
       } else {
-          index = (int)floor(((z - _zLower) * (_pal->nrColors() - 1)) / (_zUpper - _zLower));
+        index = (int)floor(((z - _zLower) * (_pal->nrColors() - 1)) / (_zUpper - _zLower));
       }
     } else {
       index = 0;
@@ -318,8 +319,9 @@
   _zLower = z;
 }
 
+
 void KstImage::setThresholdToSpikeInsensitive(double per) {
-  if (per==0) {
+  if (per == 0.0) {
     setAutoThreshold(true);
   } else {
     matrix()->writeLock();
@@ -474,7 +476,7 @@
   if (_pal) {
     newPalette = new KPalette(*_pal);
   }
-  
+
   QString name(tagName() + '\'');
   while (KstData::self()->dataTagNameNotUnique(name, false)) {
     name += '\'';
@@ -564,20 +566,18 @@
 
 
 void KstImage::paint(const KstCurveRenderContext& context) {
+  KstImagePtr image = this;
+  KstPainter* p = context.p;
+  QColor invalid = context.backgroundColor;
   double Lx = context.Lx, Hx = context.Hx, Ly = context.Ly, Hy = context.Hy;
   double m_X = context.m_X, m_Y = context.m_Y, b_X = context.b_X, b_Y = context.b_Y;
   double x_max = context.x_max, y_max = context.y_max, x_min = context.x_min, y_min = context.y_min;
-  bool xLog = context.xLog, yLog = context.yLog;
   double xLogBase = context.xLogBase;
   double yLogBase = context.yLogBase;
-  KstPainter* p = context.p;
-  QColor invalid = context.backgroundColor;
-  
   double x, y, width, height;
-  double img_Lx_pix = 0, img_Ly_pix = 0, img_Hx_pix = 0, img_Hy_pix = 0;
+  double img_Lx_pix = 0.0, img_Ly_pix = 0.0, img_Hx_pix = 0.0, img_Hy_pix = 0.0;
+  bool xLog = context.xLog, yLog = context.yLog;
 
-  KstImagePtr image = this;
-  
   if (_inputMatrices.contains(THEMATRIX)) { // don't paint if we have no matrix
     image->matrixDimensions(x, y, width, height);
 
@@ -629,7 +629,7 @@
           img_Ly_pix = (y + height) * m_Y + b_Y;
         }
       }
-  
+
       // color map
       QColor thisPixel;
       if (image->hasColorMap()) {
@@ -702,17 +702,17 @@
                 new_y_small = pow(yLogBase, new_y_small);
                 new_y_large = pow(yLogBase, new_y_large);
               }
-    
+
               zTL = mp->value(new_x_small, new_y_small, 0L);
               zTR = mp->value(new_x_large, new_y_small, 0L);
               zBL = mp->value(new_x_small, new_y_large, 0L);
               zBR = mp->value(new_x_large, new_y_large, 0L);
-    
+
               // determine the lines to draw
               int numPoints = 0;
               bool passTop = false, passBottom = false, passLeft = false, passRight = false;
               QPoint topPoint, bottomPoint, leftPoint, rightPoint;
-    
+
               // passes through the top
               if (hasPrevBottom) {
                 topPoint = lastPoint;
@@ -725,7 +725,7 @@
                 topPoint.setY(j);
               }
               hasPrevBottom = false;
-    
+
               // passes through the bottom
               if ((lineK < zBR && lineK > zBL) || (lineK < zBL && lineK > zBR)) {
                 ++numPoints;
@@ -737,7 +737,7 @@
                   hasPrevBottom = true;
                 }
               }
-    
+
               // passes through the left
               if ((lineK < zBL && lineK > zTL) || (lineK < zTL && lineK > zBL)) {
                 ++numPoints;
@@ -753,7 +753,7 @@
                 rightPoint.setY(int(((lineK - zTR)*CONTOUR_STEP + (zBR - zTR)*j) / (zBR - zTR)));
                 rightPoint.setX(i + CONTOUR_STEP);
               }
-    
+
               if (numPoints == 4) {
                 // draw a cross
                 p->drawLine(topPoint, bottomPoint);
@@ -777,7 +777,7 @@
                 // two points - connect them
                 QPoint point1, point2;
                 bool true1 = false;
-    
+
                 if (passTop) {
                   point1 = topPoint;
                   true1 = true;
@@ -813,7 +813,7 @@
 
 void KstImage::yRange(double xFrom, double xTo, double* yMin, double* yMax) {
   if (!yMin || !yMax) {
-    return;  
+    return;
   }
   // if x range overlaps with image x range, just return image y range
   if ((xFrom <= MinX && xTo >= MinX) ||
@@ -824,8 +824,9 @@
     *yMax = MaxY;
     return;
   }
-  *yMin = 0;
-  *yMax = 0;
+  *yMin = 0.0;
+  *yMax = 0.0;
+
   return;
 }
 
@@ -833,6 +834,7 @@
 void KstImage::paintLegendSymbol(KstPainter *p, const QRect& bound) {
   if (hasColorMap() && _pal) {
     int l = bound.left(), r = bound.right(), t = bound.top(), b = bound.bottom();
+
     // draw the color palette
     for (int i = l; i <= r; i++) {
       int index = (int)floor(((i - l) * (_pal->nrColors() - 1)) / (r - l));
@@ -847,6 +849,3 @@
     p->drawRect(bound.left(), bound.top(), bound.width(), bound.height());
   }
 }
-
-
-// vim: ts=2 sw=2 et


More information about the Kst mailing list