File indexing completed on 2024-12-22 04:17:20

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *   copyright : (C) 2005  University of British Columbia                  *
0006  *                   dscott@phas.ubc.ca                                    *
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  ***************************************************************************/
0014 
0015 #include "editablevector.h"
0016 
0017 #include "vectorscriptinterface.h"
0018 
0019 // use KCodecs::base64Encode() in kmdcodecs.h
0020 // Create QDataStream into a QByteArray
0021 // qCompress the bytearray
0022 #include <QXmlStreamWriter>
0023 #include <QFile>
0024 #include <QDataStream>
0025 
0026 #include "debug.h"
0027 namespace Kst {
0028 
0029 const QString EditableVector::staticTypeString = "Editable Vector";
0030 const QString EditableVector::staticTypeTag = "editablevector";
0031 
0032 EditableVector::EditableVector(ObjectStore *store)
0033     : Vector(store), _sum(0.0) {
0034   _editable = true;
0035   _saveable = true;
0036   _saveData = true;
0037 }
0038 
0039 
0040 const QString& EditableVector::typeString() const {
0041   return staticTypeString;
0042 }
0043 
0044 
0045 ScriptInterface* EditableVector::createScriptInterface() {
0046   return new EditableVectorSI(this);
0047 }
0048 
0049 void EditableVector::setSaveData(bool save) {
0050   Q_UNUSED(save)
0051 }
0052 
0053 void EditableVector::setValue(const int &i, const double &val) { //sa Vector::change(...)
0054     writeLock();
0055     Q_ASSERT(i>=0);
0056     if(i>_size) {
0057         resize(i,1);
0058     }
0059     _scalars["sum"]->setValue(_sum+val-_v_out[i]);
0060     _scalars["sumsquared"]->setValue(_sum*_sum);
0061     _scalars["max"]->setValue(qMax(_max,val));
0062     _scalars["min"]->setValue(qMin(_min,val));
0063     if (val>=0.0) {
0064       _scalars["minpos"]->setValue(qMin(_minPos,val));
0065     }
0066     _scalars["last"]->setValue(_v_out[_size-1]);
0067     _scalars["first"]->setValue(_v_out[0]);
0068     _v_raw[i]=val;
0069     unlock();
0070 }
0071 
0072 /** Save vector information */
0073 void EditableVector::save(QXmlStreamWriter &s) {
0074   s.writeStartElement("editablevector");
0075   saveNameInfo(s, VECTORNUM|SCALARNUM);
0076 
0077   if (_saveData) {
0078     QByteArray qba(length()*sizeof(double), '\0');
0079     QDataStream qds(&qba, QIODevice::WriteOnly);
0080 
0081     for (int i = 0; i < length(); i++) {
0082       qds << _v_raw[i];
0083     }
0084 
0085     s.writeTextElement("data", qCompress(qba).toBase64());
0086   }
0087   s.writeEndElement();
0088 }
0089 
0090 /**  used for scripting IPC.
0091      accepts an open readable file.
0092      fails silently */
0093 void EditableVector::loadFromTmpFile(QFile &fp) {
0094   qint64 n_read;
0095 
0096   resize(fp.size()/sizeof(double));
0097 
0098   n_read = fp.read((char *)_v_raw, fp.size());
0099 
0100   if (n_read != fp.size()) {
0101     resize(n_read/sizeof(double));
0102   }
0103   internalUpdate(); // not sure if we need this here.
0104 }
0105 
0106 
0107 QString EditableVector::_automaticDescriptiveName() const {
0108 
0109   QString name("(");
0110   if (length()>=1) {
0111     name += QString::number(_v_out[0]);
0112   }
0113   if (length()>=2) {
0114     name += " " + QString::number(_v_out[1]);
0115   }
0116 
0117   if (length()>=3) {
0118     name += " ...";
0119   }
0120 
0121   name += ')';
0122 
0123   return name;
0124 }
0125 
0126 QString EditableVector::descriptionTip() const {
0127     return tr("Editable Vector: %1\n"
0128       "  %2 values").arg(Name()).arg(length());
0129 
0130 }
0131 
0132 }
0133 // vim: ts=2 sw=2 et