File indexing completed on 2024-04-21 03:41:37
0001 /* 0002 SPDX-FileCopyrightText: 2005, 2008 Carsten Niehaus <cniehaus@kde.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "spectrumparser.h" 0007 0008 0009 #include "kalzium_libscience_debug.h" 0010 #include <QList> 0011 0012 class SpectrumParser::Private 0013 { 0014 public: 0015 Private() 0016 { 0017 } 0018 0019 ~Private() 0020 { 0021 delete currentSpectrum; 0022 delete currentPeak; 0023 } 0024 0025 Spectrum *currentSpectrum = nullptr; 0026 Spectrum::peak *currentPeak = nullptr; 0027 0028 bool inMetadata_ = false; 0029 bool inSpectrum_ = false; 0030 bool inSpectrumList_ = false; 0031 bool inPeakList_ = false; 0032 bool inPeak_ = false; 0033 0034 double wavelength; 0035 int intensity; 0036 0037 QList<Spectrum *> spectra; 0038 }; 0039 0040 SpectrumParser::SpectrumParser() 0041 : QXmlDefaultHandler() 0042 , d(new Private) 0043 { 0044 } 0045 0046 SpectrumParser::~SpectrumParser() 0047 { 0048 delete d; 0049 } 0050 0051 bool SpectrumParser::startElement(const QString &, const QString &localName, const QString &, const QXmlAttributes &attrs) 0052 { 0053 if (localName == QLatin1String("spectrum")) { 0054 d->currentSpectrum = new Spectrum(); 0055 d->inSpectrum_ = true; 0056 0057 // now save the element of the current spectrum 0058 for (int i = 0; i < attrs.length(); ++i) { 0059 if (attrs.localName(i) == QLatin1String("id")) { 0060 currentElementID = attrs.value(i); 0061 } 0062 } 0063 0064 } else if (d->inSpectrum_ && localName == QLatin1String("peakList")) { 0065 d->inPeakList_ = true; 0066 } else if (d->inSpectrum_ && d->inPeakList_ && localName == QLatin1String("peak")) { 0067 d->inPeak_ = true; 0068 for (int i = 0; i < attrs.length(); ++i) { 0069 if (attrs.localName(i) == QLatin1String("xValue")) { 0070 d->intensity = attrs.value(i).toInt(); 0071 } else if (attrs.localName(i) == QLatin1String("yValue")) { 0072 d->wavelength = attrs.value(i).toDouble(); 0073 } 0074 } 0075 d->currentPeak = new Spectrum::peak(d->wavelength, d->intensity); 0076 } 0077 return true; 0078 } 0079 0080 bool SpectrumParser::endElement(const QString &, const QString &localName, const QString &) 0081 { 0082 if (localName == QLatin1String("spectrum")) { 0083 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0084 int num = currentElementID.midRef(1).toInt(); 0085 #else 0086 int num = QStringView(currentElementID).mid(1).toInt(); 0087 #endif 0088 d->currentSpectrum->setParentElementNumber(num); 0089 0090 d->spectra.append(d->currentSpectrum); 0091 0092 d->currentSpectrum = nullptr; 0093 d->inSpectrum_ = false; 0094 } else if (localName == QLatin1String("peakList")) { 0095 d->inSpectrumList_ = false; 0096 } else if (localName == QLatin1String("peak")) { 0097 // X qCDebug(KALZIUM_LIBSCIENCE_LOG) << "in 'peak'" << " with this data: " << d->currentPeak->intensity << " (intensity)" ; 0098 d->currentSpectrum->addPeak(d->currentPeak); 0099 d->currentPeak = nullptr; 0100 d->inPeak_ = false; 0101 } 0102 return true; 0103 } 0104 0105 bool SpectrumParser::characters(const QString &ch) 0106 { 0107 Q_UNUSED(ch) 0108 return true; 0109 } 0110 0111 QList<Spectrum *> SpectrumParser::getSpectrums() const 0112 { 0113 return d->spectra; 0114 }