~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to libscience/isotopeparser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
copyright            : (C) 2005-2008 by Carsten Niehaus
 
3
email                : cniehaus@kde.org
 
4
 ***************************************************************************/
 
5
/***************************************************************************
 
6
 *                                                                         *
 
7
 *   This program is free software; you can redistribute it and/or modify  *
 
8
 *   it under the terms of the GNU General Public License as published by  *
 
9
 *   the Free Software Foundation; either version 2 of the License, or     *
 
10
 *   (at your option) any later version.                                   *
 
11
 *                                                                         *
 
12
 ***************************************************************************/
 
13
#include "isotopeparser.h"
 
14
 
 
15
#include "chemicaldataobject.h"
 
16
#include "isotope.h"
 
17
#include <kunitconversion/converter.h>
 
18
#include <QDebug>
 
19
 
 
20
 
 
21
class IsotopeParser::Private
 
22
{
 
23
public:
 
24
        Private()
 
25
        :       currentUnit(KUnitConversion::NoUnit),
 
26
        currentErrorValue(QVariant()),
 
27
        currentElementSymbol(QString()),
 
28
        currentIsotope(0),
 
29
        inIsotope(false),
 
30
        inElement(false),
 
31
        inAtomicNumber(false),
 
32
        inExactMass(false),
 
33
        inSpin(false),
 
34
        inMagMoment(false),
 
35
        inHalfLife(false),
 
36
        inAlphaDecayLikeliness(false),
 
37
        inAlphaDecay(false),
 
38
        inBetaplusDecayLikeliness(false),
 
39
        inBetaplusDecay(false),
 
40
        inBetaminusDecayLikeliness(false),
 
41
        inBetaminusDecay(false),
 
42
        inECDecayLikeliness(false),     
 
43
        inECDecay(false),
 
44
        inAbundance(false)
 
45
        {
 
46
        }
 
47
 
 
48
  ~Private(){
 
49
    delete currentIsotope;
 
50
    //qDeleteAll(isotopes);
 
51
  }
 
52
 
 
53
        ChemicalDataObject currentDataObject;
 
54
        int currentUnit;
 
55
        QVariant currentErrorValue;
 
56
        QString currentElementSymbol;
 
57
        Isotope* currentIsotope;
 
58
        
 
59
        QList<Isotope*> isotopes;
 
60
        
 
61
        bool inIsotope;
 
62
        bool inElement;
 
63
        bool inAtomicNumber;
 
64
        bool inExactMass;
 
65
        bool inSpin;
 
66
        bool inMagMoment;
 
67
        bool inHalfLife;
 
68
        bool inAlphaDecayLikeliness;
 
69
        bool inAlphaDecay;
 
70
        bool inBetaplusDecayLikeliness;
 
71
        bool inBetaplusDecay;
 
72
        bool inBetaminusDecayLikeliness;
 
73
        bool inBetaminusDecay;
 
74
        bool inECDecayLikeliness;
 
75
        bool inECDecay;
 
76
        bool inAbundance;
 
77
};
 
78
 
 
79
IsotopeParser::IsotopeParser()
 
80
        : QXmlDefaultHandler(), d( new Private )
 
81
{
 
82
}
 
83
 
 
84
IsotopeParser::~IsotopeParser()
 
85
{
 
86
        delete d;
 
87
}
 
88
 
 
89
bool IsotopeParser::startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs)
 
90
{
 
91
    if (localName == "isotopeList") 
 
92
    {
 
93
        d->inElement = true;
 
94
 
 
95
        //now save the symbol of the current element
 
96
        for (int i = 0; i < attrs.length(); ++i) 
 
97
        {
 
98
            if ( attrs.localName( i ) == "id" )
 
99
                d->currentElementSymbol = attrs.value( i );
 
100
 
 
101
        }
 
102
    } else if ( d->inElement && localName == "isotope") 
 
103
    {
 
104
        d->currentIsotope = new Isotope();
 
105
        d->currentIsotope->addData( ChemicalDataObject( QVariant( d->currentElementSymbol ), ChemicalDataObject::symbol ) );
 
106
        d->inIsotope = true;
 
107
        for (int i = 0; i < attrs.length(); ++i) 
 
108
        {
 
109
            if ( attrs.localName( i ) == "number" )
 
110
            {
 
111
                d->currentIsotope->setNucleons( attrs.value( i ).toInt() );
 
112
            }
 
113
        }
 
114
    } else if (d->inIsotope && localName == "scalar")
 
115
    {
 
116
        for (int i = 0; i < attrs.length(); ++i) 
 
117
        {
 
118
            if ( attrs.localName( i ) == "errorValue" )
 
119
            {
 
120
                d->currentErrorValue = QVariant( attrs.value( i ) );
 
121
                continue;
 
122
            }
 
123
 
 
124
            if (attrs.value(i) == "bo:atomicNumber")
 
125
                d->inAtomicNumber = true;
 
126
            else if (attrs.value(i) == "bo:exactMass")
 
127
                d->inExactMass = true;
 
128
            else if (attrs.value(i) == "bo:halfLife"){
 
129
                for (int i = 0; i < attrs.length(); ++i) 
 
130
                {
 
131
                    if (attrs.localName(i) == "units") {
 
132
                        if ( attrs.value(i) == "siUnits:s" ) {
 
133
                            d->currentUnit = KUnitConversion::Second;
 
134
                        } else if ( attrs.value(i) == "units:y" ) {
 
135
                            d->currentUnit = KUnitConversion::Year;
 
136
                        } else {
 
137
                            d->currentUnit = KUnitConversion::NoUnit;
 
138
                        }
 
139
                    }
 
140
                }
 
141
 
 
142
                d->currentDataObject.setUnit( d->currentUnit );
 
143
                d->inHalfLife = true;
 
144
            }
 
145
            else if (attrs.value(i) == "bo:alphaDecay")
 
146
                d->inAlphaDecay = true;
 
147
            else if (attrs.value(i) == "bo:alphaDecayLikeliness")
 
148
                d->inAlphaDecayLikeliness = true;
 
149
            else if (attrs.value(i) == "bo:ecDecay")
 
150
                d->inECDecay = true;
 
151
            else if (attrs.value(i) == "bo:ecDecayLikeliness")
 
152
                d->inECDecayLikeliness = true;
 
153
            else if (attrs.value(i) == "bo:betaminusDecay")
 
154
                d->inBetaminusDecay = true;
 
155
            else if (attrs.value(i) == "bo:betaminusDecayLikeliness")
 
156
                d->inBetaminusDecayLikeliness = true;
 
157
            else if (attrs.value(i) == "bo:betaplusDecay")
 
158
                d->inBetaplusDecay = true;
 
159
            else if (attrs.value(i) == "bo:betaplusDecayLikeliness")
 
160
                d->inBetaplusDecayLikeliness = true;
 
161
            else if (attrs.value(i) == "bo:spin")
 
162
                d->inSpin = true;
 
163
            else if (attrs.value(i) == "bo:magneticMoment")
 
164
                d->inMagMoment = true;
 
165
            else if (attrs.value(i) == "bo:relativeAbundance")
 
166
                d->inAbundance = true;
 
167
        } 
 
168
    }
 
169
    return true;
 
170
}
 
171
 
 
172
bool IsotopeParser::endElement( const QString&, const QString& localName, const QString& )
 
173
{
 
174
        if ( localName == "isotope" )
 
175
        {
 
176
                d->isotopes.append(d->currentIsotope);
 
177
                
 
178
                d->currentIsotope = 0;
 
179
                d->inIsotope = false;
 
180
        }
 
181
        else if ( localName == "isotopeList" )
 
182
        {//a new list of isotopes start...
 
183
            d->inElement = false;
 
184
        }
 
185
 
 
186
        return true;
 
187
}
 
188
 
 
189
bool IsotopeParser::characters(const QString &ch)
 
190
{
 
191
        ChemicalDataObject::BlueObelisk type;
 
192
        QVariant value;
 
193
 
 
194
        if (d->inExactMass){
 
195
                value = ch.toDouble();
 
196
                type = ChemicalDataObject::exactMass; 
 
197
                d->inExactMass = false;
 
198
        }
 
199
        else if (d->inAtomicNumber) {
 
200
                value = ch.toInt();
 
201
                type = ChemicalDataObject::atomicNumber; 
 
202
                d->inAtomicNumber = false;
 
203
        }
 
204
        else if (d->inSpin) {
 
205
                value = ch;
 
206
                type = ChemicalDataObject::spin; 
 
207
                d->inSpin = false;
 
208
        }
 
209
        else if (d->inMagMoment) {
 
210
                value = ch;
 
211
                type = ChemicalDataObject::magneticMoment; 
 
212
                d->inMagMoment = false;
 
213
        }
 
214
        else if (d->inHalfLife) {
 
215
                value = ch.toDouble();
 
216
                type = ChemicalDataObject::halfLife; 
 
217
                d->inHalfLife = false;
 
218
        }
 
219
        else if (d->inAlphaDecay) {
 
220
                value = ch.toDouble();
 
221
                type = ChemicalDataObject::alphaDecay; 
 
222
                d->inAlphaDecay = false;
 
223
        }
 
224
        else if (d->inAlphaDecayLikeliness) {
 
225
                value = ch.toDouble();
 
226
                type = ChemicalDataObject::alphaDecayLikeliness; 
 
227
                d->inAlphaDecayLikeliness = false;
 
228
        }
 
229
        else if (d->inBetaplusDecay) {
 
230
                value = ch.toDouble();
 
231
                type = ChemicalDataObject::betaplusDecay; 
 
232
                d->inBetaplusDecay = false;
 
233
        }
 
234
        else if (d->inBetaplusDecayLikeliness) {
 
235
                value = ch.toDouble();
 
236
                type = ChemicalDataObject::betaplusDecayLikeliness; 
 
237
                d->inBetaplusDecayLikeliness = false;
 
238
        }
 
239
        else if (d->inBetaminusDecay) {
 
240
                value = ch.toDouble();
 
241
                type = ChemicalDataObject::betaminusDecay; 
 
242
                d->inBetaminusDecay = false;
 
243
        }
 
244
        else if (d->inBetaminusDecayLikeliness) {
 
245
                value = ch.toDouble();
 
246
                type = ChemicalDataObject::betaminusDecayLikeliness; 
 
247
                d->inBetaminusDecayLikeliness = false;
 
248
        }
 
249
        else if (d->inECDecayLikeliness) {
 
250
                value = ch.toDouble();
 
251
                type = ChemicalDataObject::ecDecayLikeliness; 
 
252
                d->inECDecayLikeliness = false;
 
253
        }
 
254
        else if (d->inECDecay) {
 
255
                value = ch.toDouble();
 
256
                type = ChemicalDataObject::ecDecay; 
 
257
                d->inECDecay = false;
 
258
        }
 
259
        else if (d->inAbundance){
 
260
                value = ch;
 
261
                type = ChemicalDataObject::relativeAbundance;
 
262
                d->inAbundance = false;
 
263
        }
 
264
        else//it is a non known value. Do not create a wrong object but return
 
265
                return true;
 
266
 
 
267
        if ( type == ChemicalDataObject::exactMass )
 
268
        {
 
269
                d->currentDataObject.setErrorValue( d->currentErrorValue );
 
270
        }
 
271
 
 
272
        d->currentDataObject.setData( value );
 
273
        d->currentDataObject.setType( type );
 
274
 
 
275
        if ( d->currentIsotope )
 
276
        {
 
277
                d->currentIsotope->addData( d->currentDataObject );
 
278
        }
 
279
 
 
280
        return true;
 
281
}
 
282
 
 
283
QList<Isotope*> IsotopeParser::getIsotopes()
 
284
{
 
285
        return d->isotopes;
 
286
}