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 "psdfactory.h"
14 
15 #include "debug.h"
16 #include "psd.h"
17 #include "datacollection.h"
18 #include "objectstore.h"
19 
20 namespace Kst {
21 
PSDFactory()22 PSDFactory::PSDFactory()
23 : ObjectFactory() {
24   registerFactory(PSD::staticTypeTag, this);
25 }
26 
27 
~PSDFactory()28 PSDFactory::~PSDFactory() {
29 }
30 
31 
generateObject(ObjectStore * store,QXmlStreamReader & xml)32 DataObjectPtr PSDFactory::generateObject(ObjectStore *store, QXmlStreamReader& xml) {
33   Q_ASSERT(store);
34 
35   double frequency=1.0, gaussianSigma=1.0;
36   int length=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 == PSD::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         apodizeFunction = attrs.value("apodizefunction").toString().toInt();
54         outputType = attrs.value("outputtype").toString().toInt();
55 
56         average = attrs.value("average").toString() == "true" ? true : false;
57         removeMean = attrs.value("removemean").toString() == "true" ? true : false;
58         apodize = attrs.value("apodize").toString() == "true" ? true : false;
59         if (attrs.value("descriptiveNameIsManual").toString() == "true") {
60           descriptiveName = attrs.value("descriptiveName").toString();
61         }
62         Object::processShortNameIndexAttributes(attrs);
63 
64       } else {
65         return 0;
66       }
67     } else if (xml.isEndElement()) {
68       if (n == PSD::staticTypeTag) {
69         break;
70       } else {
71         Debug::self()->log(QObject::tr("Error creating PSD 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 PSD from Kst file.  Could not find Vector."), Debug::Warning);
89     return 0;
90   }
91 
92   PSDPtr powerspectrum = store->createObject<PSD>();
93   Q_ASSERT(powerspectrum);
94 
95   powerspectrum->writeLock();
96   powerspectrum->change(vector, frequency,
97         average, length, apodize, removeMean,
98         vectorUnits, rateUnits, (ApodizeFunction)apodizeFunction,
99         gaussianSigma, (PSDType)outputType);
100 
101   powerspectrum->setDescriptiveName(descriptiveName);
102 
103   powerspectrum->registerChange();
104   powerspectrum->unlock();
105 
106   return powerspectrum;
107 }
108 
109 }
110 
111 // vim: ts=2 sw=2 et
112