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 // use KCodecs::base64Encode() in kmdcodecs.h
0015 // Create QDataStream into a QByteArray
0016 // qCompress the bytearray
0017 
0018 #include "editablematrix.h"
0019 #include "matrixscriptinterface.h"
0020 
0021 #include "debug.h"
0022 #include <qbytearray.h>
0023 
0024 #include <QDataStream>
0025 #include <QXmlStreamWriter>
0026 
0027 
0028 namespace Kst {
0029 
0030 const QString EditableMatrix::staticTypeString = "Editable Matrix";
0031 const QString EditableMatrix::staticTypeTag = "editablematrix";
0032 
0033 EditableMatrix::EditableMatrix(ObjectStore *store)
0034 : Matrix(store) {
0035   _editable = true;
0036   _saveable = true;
0037   resizeZ(1, true);
0038 }
0039 
0040 
0041 const QString& EditableMatrix::typeString() const {
0042   return staticTypeString;
0043 }
0044 
0045 
0046 void EditableMatrix::save(QXmlStreamWriter &xml) {
0047 
0048   QByteArray qba(_zSize*sizeof(double), '\0');
0049   QDataStream qds(&qba, QIODevice::WriteOnly);
0050 
0051   for (int i = 0; i < _zSize; i++) {
0052     qds << _z[i];
0053   }
0054 
0055   xml.writeStartElement(staticTypeTag);
0056   saveNameInfo(xml, VECTORNUM|MATRIXNUM|SCALARNUM);
0057   xml.writeAttribute("xmin", QString::number(minX()));
0058   xml.writeAttribute("ymin", QString::number(minY()));
0059   xml.writeAttribute("nx", QString::number(xNumSteps()));
0060   xml.writeAttribute("ny", QString::number(yNumSteps()));
0061   xml.writeAttribute("xstep", QString::number(xStepSize()));
0062   xml.writeAttribute("ystep", QString::number(yStepSize()));
0063   xml.writeTextElement("data", qCompress(qba).toBase64());
0064   xml.writeEndElement();
0065 
0066 }
0067 
0068 
0069 QString EditableMatrix::descriptionTip() const {
0070     return tr("%1:\n"
0071       "  %2 x %3").arg(Name()).arg(xNumSteps()).arg(yNumSteps());
0072 }
0073 
0074 QString EditableMatrix::_automaticDescriptiveName() const {
0075   return tr("Editable Matrix");
0076 }
0077 
0078 /**  used for scripting IPC.
0079      accepts an open readable file.
0080      fails silently */
0081 void EditableMatrix::loadFromTmpFile(QFile &fp, int nx, int ny ) {
0082   int size = nx*ny*sizeof(double);
0083 
0084   resize(nx, ny);
0085 
0086   fp.read((char *)_z, size);
0087 
0088   internalUpdate(); // not sure if we need this here.
0089 }
0090 
0091 
0092 ScriptInterface* EditableMatrix::createScriptInterface() {
0093   return new EditableMatrixSI(this);
0094 }
0095 
0096 
0097 }
0098 // vim: ts=2 sw=2 et