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

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 "vectorfactory.h"
0014 
0015 #include "debug.h"
0016 #include "vector.h"
0017 #include "generatedvector.h"
0018 #include "editablevector.h"
0019 #include "datavector.h"
0020 #include "datacollection.h"
0021 #include "objectstore.h"
0022 #include "datasourcepluginmanager.h"
0023 
0024 namespace Kst {
0025 
0026 VectorFactory::VectorFactory() 
0027 : PrimitiveFactory() {
0028   registerFactory(Vector::staticTypeTag, this);
0029 }
0030 
0031 
0032 VectorFactory::~VectorFactory() {
0033 }
0034 
0035 
0036 PrimitivePtr VectorFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0037   QByteArray data;
0038   QString descriptiveName;
0039   Q_ASSERT(store);
0040   int saveVer=-1;
0041 
0042   while (!xml.atEnd()) {
0043       const QString n = xml.name().toString();
0044     if (xml.isStartElement()) {
0045       if (n == Vector::staticTypeTag) {
0046         QXmlStreamAttributes attrs = xml.attributes();
0047         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0048           descriptiveName = attrs.value("descriptiveName").toString();
0049         }
0050         Object::processShortNameIndexAttributes(attrs);
0051       } else if (n == "data"||n=="data_v2") {
0052 
0053         QString qcs(xml.readElementText().toLatin1());
0054         QByteArray qbca = QByteArray::fromBase64(qcs.toLatin1());
0055         data = qUncompress(qbca);
0056         saveVer=(n=="data")?1:2;
0057 
0058       } else {
0059         return 0;
0060       }
0061     } else if (xml.isEndElement()) {
0062       if (n == Vector::staticTypeTag) {
0063         break;
0064       } else {
0065         Debug::self()->log(QObject::tr("Error creating vector from Kst file."), Debug::Warning);
0066         return 0;
0067       }
0068     }
0069     xml.readNext();
0070   }
0071 
0072   if (xml.hasError()) {
0073     return 0;
0074   }
0075 
0076   VectorPtr vector = store->createObject<Vector>();
0077   if(saveVer==2) {
0078       vector->change(data);
0079   } else {
0080       vector->oldChange(data);
0081   }
0082   vector->setDescriptiveName(descriptiveName);
0083 
0084   vector->writeLock();
0085   vector->registerChange();
0086   vector->unlock();
0087 
0088   return vector;
0089 }
0090 
0091 
0092 GeneratedVectorFactory::GeneratedVectorFactory()
0093 : PrimitiveFactory() {
0094   registerFactory(GeneratedVector::staticTypeTag, this);
0095 }
0096 
0097 
0098 GeneratedVectorFactory::~GeneratedVectorFactory() {
0099 }
0100 
0101 
0102 PrimitivePtr GeneratedVectorFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0103   double first=-1.0, last=1.0;
0104   int count=0;
0105   QString descriptiveName;
0106 
0107   while (!xml.atEnd()) {
0108       const QString n = xml.name().toString();
0109     if (xml.isStartElement()) {
0110       if (n == GeneratedVector::staticTypeTag) {
0111         QXmlStreamAttributes attrs = xml.attributes();
0112         if (attrs.value("first").isEmpty()) {
0113           // Legacy: assume the generated vector is increasing
0114           first = attrs.value("min").toString().toDouble();
0115           last = attrs.value("max").toString().toDouble();
0116         } else {
0117           first = attrs.value("first").toString().toDouble();
0118           last = attrs.value("last").toString().toDouble();
0119         }
0120         count = attrs.value("count").toString().toInt();
0121         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0122           descriptiveName = attrs.value("descriptiveName").toString();
0123         }
0124         Object::processShortNameIndexAttributes(attrs);
0125       } else {
0126         return 0;
0127       }
0128     } else if (xml.isEndElement()) {
0129       if (n == GeneratedVector::staticTypeTag) {
0130         break;
0131       } else {
0132         Debug::self()->log(QObject::tr("Error creating generated vector from Kst file."), Debug::Warning);
0133         return 0;
0134       }
0135     }
0136     xml.readNext();
0137   }
0138 
0139   if (xml.hasError()) {
0140     return 0;
0141   }
0142 
0143   GeneratedVectorPtr vector = store->createObject<GeneratedVector>();
0144   vector->changeRange(first, last, count);
0145   vector->setDescriptiveName(descriptiveName);
0146 
0147   vector->writeLock();
0148   vector->registerChange();
0149   vector->unlock();
0150 
0151   return vector;
0152 }
0153 
0154 EditableVectorFactory::EditableVectorFactory()
0155 : PrimitiveFactory() {
0156   registerFactory(EditableVector::staticTypeTag, this);
0157 }
0158 
0159 
0160 EditableVectorFactory::~EditableVectorFactory() {
0161 }
0162 
0163 
0164 PrimitivePtr EditableVectorFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0165   QByteArray data;
0166   QString descriptiveName;
0167   int dataVer=-1;
0168 
0169   while (!xml.atEnd()) {
0170       const QString n = xml.name().toString();
0171     if (xml.isStartElement()) {
0172       if (n == EditableVector::staticTypeTag) {
0173         QXmlStreamAttributes attrs = xml.attributes();
0174         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0175           descriptiveName = attrs.value("descriptiveName").toString();
0176         }
0177         Object::processShortNameIndexAttributes(attrs);
0178       } else if (n == "data" || n == "data_v2") {
0179         QString qcs(xml.readElementText().toLatin1());
0180         QByteArray qbca = QByteArray::fromBase64(qcs.toLatin1());
0181         data = qUncompress(qbca);
0182         dataVer=(n=="data_v2")?2:1;
0183       } else {
0184         return 0;
0185       }
0186     } else if (xml.isEndElement()) {
0187       if (n == EditableVector::staticTypeTag) {
0188         break;
0189       } else {
0190         Debug::self()->log(QObject::tr("Error creating vector from Kst file."), Debug::Warning);
0191         return 0;
0192       }
0193     }
0194     xml.readNext();
0195   }
0196 
0197   if (xml.hasError()) {
0198     return 0;
0199   }
0200 
0201   EditableVectorPtr vector = store->createObject<EditableVector>();
0202 
0203   if(dataVer==2) {
0204       vector->change(data);
0205   } else {
0206       vector->oldChange(data);
0207   }
0208   vector->setDescriptiveName(descriptiveName);
0209 
0210   vector->writeLock();
0211   vector->registerChange();
0212   vector->unlock();
0213 
0214   return vector;
0215 }
0216 
0217 
0218 DataVectorFactory::DataVectorFactory()
0219 : PrimitiveFactory() {
0220   registerFactory(DataVector::staticTypeTag, this);
0221 }
0222 
0223 
0224 DataVectorFactory::~DataVectorFactory() {
0225 }
0226 
0227 
0228 PrimitivePtr DataVectorFactory::generatePrimitive(ObjectStore *store, QXmlStreamReader& xml) {
0229   QByteArray data;
0230   //QString provider;
0231   QString file, field;
0232   QString descriptiveName;
0233   int start=0, count=0, skip = -1;
0234   bool doAve=false;
0235   QString start_units;
0236   QString range_units;
0237 
0238   while (!xml.atEnd()) {
0239       const QString n = xml.name().toString();
0240     if (xml.isStartElement()) {
0241       if (n == DataVector::staticTypeTag) {
0242         QXmlStreamAttributes attrs = xml.attributes();
0243 
0244         //provider = attrs.value("provider").toString();
0245         file = DataPrimitive::readFilename(attrs);
0246         field = attrs.value("field").toString();
0247         start = attrs.value("start").toString().toInt();
0248         count = attrs.value("count").toString().toInt();
0249         skip = attrs.value("skip").toString().toInt();
0250         doAve = attrs.value("doAve").toString() == "true" ? true : false;
0251         start_units = attrs.value("startUnits").toString();
0252         range_units = attrs.value("rangeUnits").toString();
0253 
0254         // set overrides if set from command line
0255         if (!store->override.fileName.isEmpty()) {
0256           file = store->override.fileName;
0257         }
0258         if (store->override.f0 != -5) {
0259           start = store->override.f0;
0260         }
0261         if (store->override.N != -5) {
0262           count = store->override.N;
0263         }
0264         if (store->override.skip != -5 ) {
0265           skip = store->override.skip;
0266         }
0267         if (store->override.doAve != -5) {
0268           doAve = store->override.doAve;
0269         }
0270         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
0271           descriptiveName = attrs.value("descriptiveName").toString();
0272         }
0273         Object::processShortNameIndexAttributes(attrs);
0274 
0275       } else if (n == "data") {
0276 
0277         QString qcs(xml.readElementText().toLatin1());
0278         QByteArray qbca = QByteArray::fromBase64(qcs.toLatin1());
0279         data = qUncompress(qbca);
0280 
0281       } else {
0282         return 0;
0283       }
0284     } else if (xml.isEndElement()) {
0285       if (n == DataVector::staticTypeTag) {
0286         break;
0287       } else {
0288         Debug::self()->log(QObject::tr("Error creating vector from Kst file."), Debug::Warning);
0289         return 0;
0290       }
0291     }
0292     xml.readNext();
0293   }
0294 
0295   if (xml.hasError()) {
0296     return 0;
0297   }
0298 
0299   Q_ASSERT(store);
0300   DataSourcePtr dataSource = DataSourcePluginManager::findOrLoadSource(store, file);
0301 
0302   if (!dataSource) {
0303     return 0; //Couldn't find a suitable datasource
0304   }
0305 
0306   DataVectorPtr vector = store->createObject<DataVector>();
0307 
0308   vector->writeLock();
0309   vector->change(dataSource, field,
0310       start,
0311       count,
0312       skip,
0313       (skip != -1),
0314       doAve);
0315 
0316   vector->setDescriptiveName(descriptiveName);
0317   vector->setStartUnits(start_units);
0318   vector->setRangeUnits(range_units);
0319   vector->registerChange();
0320   vector->unlock();
0321 
0322   return vector;
0323 }
0324 
0325 }
0326 
0327 // vim: ts=2 sw=2 et