1 /***************************************************************************
2 * *
3 * copyright : (C) 2007 The University of Toronto *
4 * netterfield@astro.utoronto.ca *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 ***************************************************************************/
12
13 #include "csdfactory.h"
14
15 #include "debug.h"
16 #include "csd.h"
17 #include "datacollection.h"
18 #include "objectstore.h"
19
20 namespace Kst {
21
CSDFactory()22 CSDFactory::CSDFactory()
23 : ObjectFactory() {
24 registerFactory(CSD::staticTypeTag, this);
25 }
26
27
~CSDFactory()28 CSDFactory::~CSDFactory() {
29 }
30
31
generateObject(ObjectStore * store,QXmlStreamReader & xml)32 DataObjectPtr CSDFactory::generateObject(ObjectStore *store, QXmlStreamReader& xml) {
33 Q_ASSERT(store);
34
35 double frequency=1.0, gaussianSigma=1.0;
36 int length=8, windowSize=8, apodizeFunction=0, outputType=0;
37 QString vectorName, vectorUnits, rateUnits, descriptiveName;
38 bool average=false, removeMean=false, apodize=false;
39
40 while (!xml.atEnd()) {
41 const QString n = xml.name().toString();
42 if (xml.isStartElement()) {
43 if (n == CSD::staticTypeTag) {
44 QXmlStreamAttributes attrs = xml.attributes();
45 vectorName = attrs.value("vector").toString();
46 vectorUnits = attrs.value("vectorunits").toString();
47 rateUnits = attrs.value("rateunits").toString();
48
49 frequency = attrs.value("samplerate").toString().toDouble();
50 gaussianSigma = attrs.value("gaussiansigma").toString().toDouble();
51
52 length = attrs.value("fftlength").toString().toInt();
53 windowSize = attrs.value("windowsize").toString().toInt();
54 apodizeFunction = attrs.value("apodizefunction").toString().toInt();
55 outputType = attrs.value("outputtype").toString().toInt();
56
57 average = attrs.value("average").toString() == "true" ? true : false;
58 removeMean = attrs.value("removemean").toString() == "true" ? true : false;
59 apodize = attrs.value("apodize").toString() == "true" ? true : false;
60 if (attrs.value("descriptiveNameIsManual").toString() == "true") {
61 descriptiveName = attrs.value("descriptiveName").toString();
62 }
63 Object::processShortNameIndexAttributes(attrs);
64 } else {
65 return 0;
66 }
67 } else if (xml.isEndElement()) {
68 if (n == CSD::staticTypeTag) {
69 break;
70 } else {
71 Debug::self()->log(QObject::tr("Error creating Spectrogram from Kst file."), Debug::Warning);
72 return 0;
73 }
74 }
75 xml.readNext();
76 }
77
78 if (xml.hasError()) {
79 return 0;
80 }
81
82 VectorPtr vector = 0;
83 if (store && !vectorName.isEmpty()) {
84 vector = kst_cast<Vector>(store->retrieveObject(vectorName));
85 }
86
87 if (!vector) {
88 Debug::self()->log(QObject::tr("Error creating Spectrogram from Kst file. Could not find Vector."), Debug::Warning);
89 return 0;
90 }
91
92 CSDPtr csd = store->createObject<CSD>();
93 csd->change(vector,
94 frequency,
95 average,
96 removeMean,
97 apodize,
98 (ApodizeFunction)apodizeFunction,
99 windowSize,
100 length,
101 gaussianSigma,
102 (PSDType)outputType,
103 vectorUnits,
104 rateUnits);
105
106 csd->setDescriptiveName(descriptiveName);
107 csd->writeLock();
108 csd->registerChange();
109 csd->unlock();
110
111 return csd;
112 }
113
114 }
115
116 // vim: ts=2 sw=2 et
117