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

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "matrixfactory.h"
0014 
0015 #include "debug.h"
0016 #include "matrix.h"
0017 #include "generatedmatrix.h"
0018 #include "editablematrix.h"
0019 #include "datamatrix.h"
0020 #include "objectstore.h"
0021 #include "datasourcepluginmanager.h"
0022 
0023 namespace Kst {
0024 
0025 GeneratedMatrixFactory::GeneratedMatrixFactory()
0026 : PrimitiveFactory() {
0027   registerFactory(GeneratedMatrix::staticTypeTag, this);
0028 }
0029 
0030 
0031 GeneratedMatrixFactory::~GeneratedMatrixFactory() {
0032 }
0033 
0034 
0035 PrimitivePtr GeneratedMatrixFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0036   QByteArray data;
0037   QString descriptiveName;
0038 
0039   Q_ASSERT(store);
0040 
0041   bool xDirection=1;
0042   double gradZMin=-1, gradZMax=1, minX=0, minY=0, nX=10, nY=10, stepX=1, stepY=1;
0043 
0044   while (!xml.atEnd()) {
0045       const QString n = xml.name().toString();
0046     if (xml.isStartElement()) {
0047       if (n == GeneratedMatrix::staticTypeTag) {
0048         QXmlStreamAttributes attrs = xml.attributes();
0049         gradZMin = attrs.value("gradzmin").toString().toDouble();
0050         gradZMax = attrs.value("gradzmax").toString().toDouble();
0051         minX = attrs.value("xmin").toString().toDouble();
0052         minY = attrs.value("ymin").toString().toDouble();
0053         nX = attrs.value("nx").toString().toDouble();
0054         nY = attrs.value("ny").toString().toDouble();
0055         stepX = attrs.value("xstep").toString().toDouble();
0056         stepY = attrs.value("ystep").toString().toDouble();
0057         xDirection = attrs.value("xdirection").toString() == "true" ? true : false;
0058         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0059           descriptiveName = attrs.value("descriptiveName").toString();
0060         }
0061         Object::processShortNameIndexAttributes(attrs);
0062 
0063       } else {
0064         return 0;
0065       }
0066     } else if (xml.isEndElement()) {
0067       if (n == GeneratedMatrix::staticTypeTag) {
0068         break;
0069       } else {
0070         Debug::self()->log(QObject::tr("Error creating Generated Matrix from Kst file."), Debug::Warning);
0071         return 0;
0072       }
0073     }
0074     xml.readNext();
0075   }
0076 
0077   if (xml.hasError()) {
0078     return 0;
0079   }
0080 
0081   GeneratedMatrixPtr matrix = store->createObject<GeneratedMatrix>();
0082   matrix->change(uint(nX), uint(nY), minX, minY, stepX, stepY, gradZMin, gradZMax, xDirection);
0083   matrix->setDescriptiveName(descriptiveName);
0084 
0085   matrix->writeLock();
0086   matrix->registerChange();
0087   matrix->unlock();
0088 
0089   return matrix;
0090 }
0091 
0092 
0093 EditableMatrixFactory::EditableMatrixFactory()
0094 : PrimitiveFactory() {
0095   registerFactory(EditableMatrix::staticTypeTag, this);
0096 }
0097 
0098 
0099 EditableMatrixFactory::~EditableMatrixFactory() {
0100 }
0101 
0102 
0103 PrimitivePtr EditableMatrixFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0104   QByteArray data;
0105   QString descriptiveName;
0106 
0107   Q_ASSERT(store);
0108 
0109   double minX=0, minY=0, nX=10, nY=10, stepX=1, stepY=1;
0110 
0111   while (!xml.atEnd()) {
0112       const QString n = xml.name().toString();
0113     if (xml.isStartElement()) {
0114       if (n == EditableMatrix::staticTypeTag) {
0115         QXmlStreamAttributes attrs = xml.attributes();
0116         minX = attrs.value("xmin").toString().toDouble();
0117         minY = attrs.value("ymin").toString().toDouble();
0118         nX = attrs.value("nx").toString().toDouble();
0119         nY = attrs.value("ny").toString().toDouble();
0120         stepX = attrs.value("xstep").toString().toDouble();
0121         stepY = attrs.value("ystep").toString().toDouble();
0122         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0123           descriptiveName = attrs.value("descriptiveName").toString();
0124         }
0125         Object::processShortNameIndexAttributes(attrs);
0126       } else if (n == "data") {
0127 
0128         QString qcs(xml.readElementText().toLatin1());
0129         QByteArray qbca = QByteArray::fromBase64(qcs.toLatin1());
0130         data = qUncompress(qbca);
0131 
0132       } else {
0133         return 0;
0134       }
0135     } else if (xml.isEndElement()) {
0136       if (n == EditableMatrix::staticTypeTag) {
0137         break;
0138       } else {
0139         Debug::self()->log(QObject::tr("Error creating Editable Matrix from Kst file."), Debug::Warning);
0140         return 0;
0141       }
0142     }
0143     xml.readNext();
0144   }
0145 
0146   if (xml.hasError()) {
0147     return 0;
0148   }
0149 
0150   EditableMatrixPtr matrix = store->createObject<EditableMatrix>();
0151   matrix->change(data, uint(nX), uint(nY), minX, minY, stepX, stepY);
0152   matrix->setDescriptiveName(descriptiveName);
0153 
0154   matrix->writeLock();
0155   matrix->registerChange();
0156   matrix->unlock();
0157 
0158   return matrix;
0159 }
0160 
0161 
0162 
0163 DataMatrixFactory::DataMatrixFactory()
0164 : PrimitiveFactory() {
0165   registerFactory(DataMatrix::staticTypeTag, this);
0166 }
0167 
0168 
0169 DataMatrixFactory::~DataMatrixFactory() {
0170 }
0171 
0172 
0173 PrimitivePtr DataMatrixFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0174   QByteArray data;
0175   QString descriptiveName;
0176 
0177   Q_ASSERT(store);
0178 
0179   bool doAve=false, doSkip=false;
0180   int requestedXStart=0, requestedYStart=0, requestedXCount=-1, requestedYCount=-1, skip=0;
0181   double minX=0, minY=0, stepX=1, stepY=1;
0182   int frame=0;
0183   QString provider, file, field;
0184 
0185   while (!xml.atEnd()) {
0186       const QString n = xml.name().toString();
0187     if (xml.isStartElement()) {
0188       if (n == DataMatrix::staticTypeTag) {
0189         QXmlStreamAttributes attrs = xml.attributes();
0190         provider = attrs.value("provider").toString();
0191         file = DataPrimitive::readFilename(attrs);
0192 
0193         if (!store->override.fileName.isEmpty()) {
0194           file = store->override.fileName;
0195         }
0196 
0197         field = attrs.value("field").toString();
0198         requestedXStart = attrs.value("reqxstart").toString().toInt();
0199         requestedYStart = attrs.value("reqystart").toString().toInt();
0200         requestedXCount = attrs.value("reqnx").toString().toInt();
0201         requestedYCount = attrs.value("reqny").toString().toInt();
0202         doAve = attrs.value("doave").toString() == "true" ? true : false;
0203         doSkip = attrs.value("doskip").toString() == "true" ? true : false;
0204         skip = attrs.value("skip").toString().toInt();
0205         frame = attrs.value("frame").toString().toInt();
0206         minX = attrs.value("xmin").toString().toDouble();
0207         minY = attrs.value("ymin").toString().toDouble();
0208         stepX = attrs.value("xstep").toString().toDouble();
0209         stepY = attrs.value("ystep").toString().toDouble();
0210         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0211           descriptiveName = attrs.value("descriptiveName").toString();
0212         }
0213         Object::processShortNameIndexAttributes(attrs);
0214       } else {
0215         return 0;
0216       }
0217     } else if (xml.isEndElement()) {
0218       if (n == DataMatrix::staticTypeTag) {
0219         break;
0220       } else {
0221         Debug::self()->log(QObject::tr("Error creating Data Matrix from Kst file."), Debug::Warning);
0222         return 0;
0223       }
0224     }
0225     xml.readNext();
0226   }
0227 
0228   if (xml.hasError()) {
0229     return 0;
0230   }
0231 
0232   Q_ASSERT(store);
0233   DataSourcePtr dataSource = DataSourcePluginManager::findOrLoadSource(store, file);
0234 
0235   if (!dataSource) {
0236     return 0; //Couldn't find a suitable datasource
0237   }
0238 
0239   DataMatrixPtr matrix = store->createObject<DataMatrix>();
0240   matrix->change(dataSource, field, requestedXStart, requestedYStart, requestedXCount, requestedYCount, doAve, doSkip, skip, frame, minX, minY, stepX, stepY);
0241   matrix->setDescriptiveName(descriptiveName);
0242 
0243   matrix->writeLock();
0244   matrix->registerChange();
0245   matrix->unlock();
0246 
0247   return matrix;
0248 }
0249 
0250 
0251 }
0252 
0253 // vim: ts=2 sw=2 et