[Kst] extragear/graphics/kst/src

George Staikos staikos at kde.org
Mon Nov 27 09:55:05 CET 2006


SVN commit 608243 by staikos:

add the ability to save data to the Kst file.  This is rather incomplete and the
UI is a temporary hack for demonstration purposes.  KstAVector is now basically
a no-op.  Questions to be answered:
1) What should RVector do if there is cached data?
2) How fine grained should saving data be?  What should the UI be?


 M  +2 -1      kst/kstui.rc  
 M  +8 -59     libkst/kstavector.cpp  
 M  +4 -2      libkst/kstavector.h  
 M  +3 -6      libkst/kstrvector.cpp  
 M  +10 -8     libkst/kstsvector.cpp  
 M  +3 -2      libkst/kstsvector.h  
 M  +84 -5     libkst/kstvector.cpp  
 M  +10 -3     libkst/kstvector.h  
 M  +6 -0      libkstapp/kst.cpp  
 M  +4 -1      libkstapp/kst.h  
 M  +1 -0      libkstapp/kstdoc.cpp  


--- trunk/extragear/graphics/kst/src/kst/kstui.rc #608242:608243
@@ -1,10 +1,11 @@
 <!DOCTYPE kpartgui>
-<kpartgui version="12" name="kst">
+<kpartgui version="13" name="kst">
 <MenuBar>
   <Menu name="file"><text>&amp;File</text>
     <Action name="file_new_window"/>
     <Action name="graphfiledialog_action"/>
     <Action name="vectorsavedialog_action"/>
+    <Action name="save_vector_data"/>
     <Merge/>
   </Menu>
   <Menu name="edit"><text>&amp;Edit</text>
--- trunk/extragear/graphics/kst/src/libkst/kstavector.cpp #608242:608243
@@ -20,55 +20,12 @@
 
 #include "kstavector.h"
 #include "kstdebug.h"
-#include <qcstring.h>
-#include <qstylesheet.h>
-#include <klocale.h>
-#include <kmdcodec.h>
 
 KstAVector::KstAVector(const QDomElement &e)
-: KstVector() {
+: KstVector(e) {
   _editable = true;
-  int in_n = 2;
-  /* parse the DOM tree */
-  QDomNode n = e.firstChild();
-  while (!n.isNull()) {
-    QDomElement e = n.toElement();
-    if (!e.isNull()) {
-      if (e.tagName() == "tag") {
-        setTagName(e.text());
-      } else if (e.tagName() == "N") {
-        in_n = e.text().toInt();
-      }
-    }
-    n = n.nextSibling();
-  }
   _saveable = true;
-  resize(in_n, true);
-
-  if (in_n > 0) {
-    QDomNode n = e.firstChild();
-    while (!n.isNull()) {
-      QDomElement e = n.toElement();
-      if (!e.isNull()) {
-        if (e.tagName() == "data") {
-          QCString qcs(e.text().latin1());
-          QByteArray qbca;
-          KCodecs::base64Decode(qcs, qbca);
-          QByteArray qba = qUncompress(qbca);
-          QDataStream qds(qba, IO_ReadOnly);
-          int i;
-          for (i = 0; i < in_n && !qds.atEnd(); i++) {
-            qds >> _v[i];
-          }
-          if (i < in_n) {
-            KstDebug::self()->log(i18n("Saved vector contains less data than it claims."), KstDebug::Warning);
-            resize(i, false);
-          }
-        }
-      }
-      n = n.nextSibling();
-    }
-  }
+  _saveData = true;
 }
 
 
@@ -76,25 +33,13 @@
 : KstVector(tag, n) {
   _editable = true;
   _saveable = true;
-  resize(n, true);
+  _saveData = true;
 }
 
 
 void KstAVector::save(QTextStream &ts, const QString& indent, bool saveAbsolutePosition) {
-  Q_UNUSED(saveAbsolutePosition)
-
-  QByteArray qba(length()*sizeof(double));
-  QDataStream qds(qba, IO_WriteOnly);
-
-  for (int i = 0; i < length(); i++) {
-    qds << _v[i];
-  }
-
   ts << indent << "<avector>" << endl;
-
-  ts << indent << "  <tag>" << QStyleSheet::escape(tagName()) << "</tag>" << endl;
-  ts << indent << "  <N>" << length() << "</N>" << endl;
-  ts << indent << "  <data>" << KCodecs::base64Encode(qCompress(qba)) << "</data>" << endl;
+  KstVector::save(ts, indent + "  ", saveAbsolutePosition);
   ts << indent << "</avector>" << endl;
 }
 
@@ -115,4 +60,8 @@
 }
 
 
+void KstAVector::setSaveData(bool save) {
+  Q_UNUSED(save)
+}
+
 // vim: ts=2 sw=2 et
--- trunk/extragear/graphics/kst/src/libkst/kstavector.h #608242:608243
@@ -28,9 +28,11 @@
   KST_EXPORT KstAVector(const QDomElement &e);
   KST_EXPORT KstAVector(int n, const QString& tag);
 
-  virtual void save(QTextStream &ts, const QString& indent = QString::null, bool saveAbsolutePosition = false);
+  void save(QTextStream &ts, const QString& indent = QString::null, bool saveAbsolutePosition = false);
 
-  virtual KstObject::UpdateType update(int update_counter);
+  KstObject::UpdateType update(int update_counter);
+
+  void setSaveData(bool save);
 };
 
 typedef KstSharedPtr<KstAVector> KstAVectorPtr;
--- trunk/extragear/graphics/kst/src/libkst/kstrvector.cpp #608242:608243
@@ -52,7 +52,7 @@
 
 KstRVector::KstRVector(const QDomElement &e, const QString &o_file,
                        int o_n, int o_f, int o_s, bool o_ave)
-: KstVector() {
+: KstVector(e) {
   KstDataSourcePtr in_file;
   QString in_field;
   int in_f0 = 0;
@@ -66,9 +66,7 @@
   while (!n.isNull()) {
     QDomElement e = n.toElement();
     if (!e.isNull()) {
-      if (e.tagName() == "tag") {
-        setTagName(e.text());
-      } else if (e.tagName() == "filename") {
+      if (e.tagName() == "filename") {
         KST::dataSourceList.lock().readLock();
         if (o_file == "|") {
           in_file = *KST::dataSourceList.findFileName(e.text());
@@ -304,8 +302,7 @@
 void KstRVector::save(QTextStream &ts, const QString& indent, bool saveAbsolutePosition) {
   if (_file) {    
     ts << indent << "<vector>" << endl;
-
-    ts << indent << "  <tag>" << QStyleSheet::escape(tagName()) << "</tag>" << endl;
+    KstVector::save(ts, indent + "  ", saveAbsolutePosition);
     _file->readLock();
     ts << indent << "  <filename>" << QStyleSheet::escape(_file->fileName()) << "</filename>" << endl;
     _file->unlock();
--- trunk/extragear/graphics/kst/src/libkst/kstsvector.cpp #608242:608243
@@ -18,7 +18,7 @@
 #include "ksdebug.h"
 #include <qstylesheet.h>
 
-KstSVector::KstSVector(const QDomElement &e) : KstVector() {
+KstSVector::KstSVector(const QDomElement &e) : KstVector(e) {
   double in_x0 = 0.0;
   double in_x1 = 1.0;
   int in_n = 2;
@@ -28,9 +28,7 @@
   while (!n.isNull()) {
     QDomElement e = n.toElement();
     if (!e.isNull()) {
-      if (e.tagName() == "tag") {
-        setTagName(e.text());
-      } else if (e.tagName() == "N") {
+      if (e.tagName() == "N") {
         in_n = e.text().toInt();
       } else if (e.tagName() == "min") {
         in_x0 = e.text().toDouble();
@@ -42,22 +40,21 @@
   }
 
   _saveable = true;
+  _saveData = false;
   changeRange( in_x0,  in_x1,  in_n );
 }
 
 
 KstSVector::KstSVector(double x0, double x1, int n, const QString &tag) : KstVector(tag, n) {
   _saveable = true;
+  _saveData = false;
   changeRange( x0, x1, n );
 }
 
 
 void KstSVector::save(QTextStream &ts, const QString& indent, bool saveAbsolutePosition) {
-  Q_UNUSED( saveAbsolutePosition )
   ts << indent << "<svector>" << endl;
-
-  ts << indent << "  <tag>" << QStyleSheet::escape(tagName())
-    << "</tag>" << endl;
+  KstVector::save(ts, indent + "  ", saveAbsolutePosition);
   ts << indent << "  <min>" << min() << "</min>" << endl;
   ts << indent << "  <max>" << max() << "</max>" << endl;
   ts << indent << "  <N>" << length() << "</N>" << endl;
@@ -105,4 +102,9 @@
 }
 
 
+void KstSVector::setSaveData(bool save) {
+  Q_UNUSED(save)
+}
+
+
 // vim: ts=2 sw=2 et
--- trunk/extragear/graphics/kst/src/libkst/kstsvector.h #608242:608243
@@ -28,10 +28,11 @@
   KstSVector(const QDomElement &e);
   KstSVector(double x0, double x1, int n, const QString& tag);
 
-  virtual void save(QTextStream &ts, const QString& indent = QString::null, bool saveAbsolutePosition = false);
+  void save(QTextStream &ts, const QString& indent = QString::null, bool saveAbsolutePosition = false);
 
   void changeRange(double x0, double x1, int n);
-  virtual KstObject::UpdateType update(int update_counter);
+  KstObject::UpdateType update(int update_counter);
+  void setSaveData(bool save);
 };
 
 typedef KstSharedPtr<KstSVector> KstSVectorPtr;
--- trunk/extragear/graphics/kst/src/libkst/kstvector.cpp #608242:608243
@@ -20,11 +20,15 @@
 #include <math.h>
 #include <stdlib.h>
 
+#include <qcstring.h>
 #include <qdeepcopy.h>
 #include <qstylesheet.h>
 
+#include <kglobal.h>
+#include <klocale.h>
+#include <kmdcodec.h>
+
 #include "ksdebug.h"
-#include <klocale.h>
 #include "kstdatacollection.h"
 #include "defaultprimitivenames.h"
 #include "kstmath.h"
@@ -47,8 +51,8 @@
   _editable = false;
   NumShifted = 0;
   NumNew = 0;
+  _saveData = false;
   _isScalarList = isScalarList;
-  _label = QString::null;
 
   _saveable = false;
 
@@ -82,6 +86,63 @@
 }
 
 
+KstVector::KstVector(const QDomElement& e)
+: KstPrimitive(), _nsum(0), _scalars(11) {
+  QByteArray qba;
+  _v = 0L;
+  int sz = INITSIZE;
+
+  _editable = false;
+  NumShifted = 0;
+  NumNew = 0;
+  _isScalarList = false;
+  _saveable = false;
+  _saveData = false;
+
+  QDomNode n = e.firstChild();
+  while (!n.isNull()) {
+    QDomElement e = n.toElement();
+    if (!e.isNull()) {
+      if (e.tagName() == "tag") {
+        KstObject::setTagName(e.text());
+      } else if (e.tagName() == "data") {
+        QCString qcs(e.text().latin1());
+        QByteArray qbca;
+        KCodecs::base64Decode(qcs, qbca);
+        qba = qUncompress(qbca);
+        sz = kMax(unsigned(INITSIZE), qba.size()/sizeof(double));
+      }
+    }
+    n = n.nextSibling();
+  }
+
+  if (tagName().isEmpty()) {
+    QString nt = i18n("Anonymous Vector %1");
+    do {
+      KstObject::setTagName(nt.arg(anonymousVectorCounter++));
+    } while (KstData::self()->vectorTagNameNotUnique(tagName(), false));
+  } else {
+    while (KstData::self()->vectorTagNameNotUnique(tagName(), false)) {
+      KstObject::setTagName(tagName() + '\'');
+    }
+  }
+
+  CreateScalars();
+  resize(sz, true);
+
+  if (!qba.isEmpty()) {
+    _saveable = true;
+    _saveData = true;
+    QDataStream qds(qba, IO_ReadOnly);
+    for (int i = 0; !qds.atEnd(); ++i) {
+      qds >> _v[i];
+    }
+  }
+
+  _is_rising = false;
+}
+
+
 KstVector::~KstVector() {
   //kstdDebug() << "+++ DELETING VECTOR: " << (void*) this << endl;
   KST::scalarList.lock().writeLock();
@@ -510,9 +571,17 @@
 
 void KstVector::save(QTextStream &ts, const QString& indent, bool saveAbsolutePosition) {
   Q_UNUSED(saveAbsolutePosition)
-    Q_UNUSED(ts)
-    Q_UNUSED(indent)
-    abort();
+  ts << indent << "<tag>" << QStyleSheet::escape(tagName()) << "</tag>" << endl;
+  if (_saveData) {
+    QByteArray qba(length()*sizeof(double));
+    QDataStream qds(qba, IO_WriteOnly);
+
+    for (int i = 0; i < length(); i++) {
+      qds << _v[i];
+    }
+
+    ts << indent << "<data>" << KCodecs::base64Encode(qCompress(qba)) << "</data>" << endl;
+  }
 }
 
 
@@ -614,6 +683,16 @@
 }
 
 
+bool KstVector::saveData() const {
+  return _saveData;
+}
+
+
+void KstVector::setSaveData(bool save) {
+  _saveData = save;
+}
+
+
 #undef ZERO_MEMORY
 #undef INITSIZE
 
--- trunk/extragear/graphics/kst/src/libkst/kstvector.h #608242:608243
@@ -55,6 +55,7 @@
      * Vectors do not automatically add themselves to the global vector list
      */
     KstVector(const QString& name = QString::null, int size = 0, KstObject *provider = 0L, bool bIsScalarList = false);
+    KstVector(const QDomElement& e);
 
     virtual ~KstVector();
 
@@ -144,13 +145,13 @@
     bool editable() const;
     void setEditable(bool editable);
 
+    bool saveData() const;
+    virtual void setSaveData(bool save);
+
   protected:
     /** current number of samples */
     int _size;
 
-    /** can/should the vector be saved... */
-    bool _saveable;
-
     /** number of valid points */
     int _nsum;
 
@@ -181,6 +182,12 @@
     this is false. */
     bool _editable : 1;
 
+    /** can/should the vector be saved */
+    bool _saveable : 1;
+
+    /** should the vector data be saved? */
+    bool _saveData : 1;
+
     double _min, _max, _mean, _minPos;
 
     /** Possibly null.  Be careful, this is non-standard usage of a KstShared.
--- trunk/extragear/graphics/kst/src/libkstapp/kst.cpp #608242:608243
@@ -436,6 +436,12 @@
   connect(PauseAction, SIGNAL(toggled(bool)), this, SLOT(updatePausedState(bool)));
 
   /************/
+  _saveData = new KToggleAction(i18n("Save Da&ta"),"save_vector_data", 0,
+                                  actionCollection(), "save_vector_data");
+  _saveData->setToolTip(i18n("Save Vector Data To Disk"));
+  _saveData->setWhatsThis(i18n("When selected, data in vectors will be saved into the Kst file."));
+
+  /************/
   XYZoomAction = new KRadioAction(i18n("XY Mouse &Zoom"), "kst_zoomxy",
                                   KShortcut(Key_F2),
                                   this, SLOT(toggleMouseMode()),
--- trunk/extragear/graphics/kst/src/libkstapp/kst.h #608242:608243
@@ -129,7 +129,8 @@
     /** Get XY zoom radio button state */
     KstZoomType getZoomRadio();
     KstGraphicType getGraphicType();
-    
+    bool saveData() const { return _saveData->isChecked(); }
+
     KstTopLevelView::ViewMode currentViewMode();
     QString currentCreateType();
     
@@ -549,6 +550,8 @@
     KAction *fileCopy;
     KAction *filePaste;
 
+    KToggleAction *_saveData;
+
     KRadioAction *_gfxLineAction;
     KRadioAction *_gfxRectangleAction;
     KRadioAction *_gfxEllipseAction;
--- trunk/extragear/graphics/kst/src/libkstapp/kstdoc.cpp #608242:608243
@@ -676,6 +676,7 @@
   // save vectors
   for (KstVectorList::Iterator it = KST::vectorList.begin(); it != KST::vectorList.end(); ++it) {
     if ((*it)->saveable()) {
+      (*it)->setSaveData(KstApp::inst()->saveData());
       (*it)->save(ts, "  ", saveAbsoluteVectorPositions);
     }
   }


More information about the Kst mailing list