]> Git trees. - libqmvoc.git/commitdiff
* Finish ChemicalDataObject
authorCarsten Niehaus <cniehaus@gmx.de>
Tue, 18 Oct 2005 12:10:49 +0000 (12:10 +0000)
committerCarsten Niehaus <cniehaus@gmx.de>
Tue, 18 Oct 2005 12:10:49 +0000 (12:10 +0000)
* tests adopted to it
* svn:ignore

svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=471676

libscience/Makefile.am
libscience/chemicaldataobject.cpp [new file with mode: 0644]
libscience/chemicaldataobject.h [new file with mode: 0644]
libscience/element.cpp
libscience/element.h
libscience/elementparser.cpp
libscience/elementparser.h
libscience/tests/xmlreadingtest.cpp

index bd2a3d6431091c8128551d6f81639fb30fd8d977..879fdd7df1090f9d834d5e333a286ad0740fdcc8 100644 (file)
@@ -10,7 +10,8 @@ libscience_la_SOURCES = \
        isotope.cpp \
        spectrumparser.cpp \
        elementparser.cpp \
-       tempunit.cpp
+       tempunit.cpp \
+       chemicaldataobject.cpp
 
 libscience_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0
 libscience_la_LIBADD = $(LIB_KDEUI) 
diff --git a/libscience/chemicaldataobject.cpp b/libscience/chemicaldataobject.cpp
new file mode 100644 (file)
index 0000000..96c2c4b
--- /dev/null
@@ -0,0 +1,35 @@
+/***************************************************************************
+ *   Copyright (C) 2005 by Carsten Niehaus                                 *
+ *   cniehaus@kde.org                                                      *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.          *
+ ***************************************************************************/
+#include "chemicaldataobject.h"
+
+ChemicalDataObject::ChemicalDataObject( QVariant v, BlueObelisk type )
+{
+       m_value = v;
+       m_type = type;
+};
+
+ChemicalDataObject::ChemicalDataObject() 
+{
+}
+
+QString ChemicalDataObject::valueAsString()
+{
+       return m_value.toString();
+}
diff --git a/libscience/chemicaldataobject.h b/libscience/chemicaldataobject.h
new file mode 100644 (file)
index 0000000..e5d3257
--- /dev/null
@@ -0,0 +1,132 @@
+#ifndef CHEMICALDATAOBJECT_H
+#define CHEMICALDATAOBJECT_H
+/***************************************************************************
+ *   Copyright (C) 2005 by Carsten Niehaus                                 *
+ *   cniehaus@kde.org                                                      *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.          *
+ ***************************************************************************/
+
+#include <QVariant>
+#include <QString>
+
+/**
+ * A ChemicalDataObject is an object which contains information about 
+ * a chemical element. This can for example be a boilingpoint. The information
+ * is stored in a QVariant.
+ * This class supports the CML-format defined by the BlueObelisk-Project.
+ *
+ * @author Carsten Niehaus <cniehaus@kde.org
+ */
+class ChemicalDataObject
+{
+       public:
+               /**
+                * The BlueObelisk-project defines in their xml-file the dataset
+                * with the names of the enum plus 'bo:'. So for symbol
+                * it is 'bo:symbol'. To avoid confusion I will choose the very
+                * same naming
+                */
+               enum BlueObelisk
+               {
+                       atomicNumber = 0 /**< The atomic number of the element */,
+                       symbol/**< the symbol of the element */,
+                       name/**< The IUPAC-name of the element */,
+                       mass/**< # IUPAC Official Masses */,
+                       exactMass/**< exact masses of the most common isotopes for each element */,
+                       ionization/**< First inizationenergy */,
+                       electronAffinity/**< the electronaffinity of the element */,
+                       electronegativityPauling/**< the electronegativity in the definition of Pauling*/,
+                       radiusCovalent/**< the covalent radius */,
+                       radiusVDW/**< the van der Waals radius */
+               };
+               
+               /**
+                * public constructor
+                */
+               ChemicalDataObject();
+
+               /**
+                * public constructor
+                * @param v the data of the object
+                * @param type the type of the data
+                */
+               ChemicalDataObject(QVariant v, 
+                               BlueObelisk type);
+
+               /**
+                * set the data of this object to @p v
+                */
+               void setData( QVariant v ){
+                       m_value = v;
+               }
+               
+               /**
+                * public destructor
+                */
+               ~ChemicalDataObject();
+
+               /**
+                * Every ChemicalDataObject contains one data. For example a
+                * integer value which represents the boilingpoint. This method
+                * returns the value as a QString
+                * 
+                * For bool, the returned string will be "false" or "true"
+                * For a QString, the QString will be returned
+                * For a int or double, the value will be returned as a QString
+                *
+                * @return the value as a QString.
+                */
+               QString valueAsString();
+               
+               /**
+                * Every ChemicalDataObject contains one data. For example a
+                * integer value which represents the boilingpoint. This method
+                * returns the value as a QVariant
+                *
+                * @return the value as a QVariant.
+                */
+               QVariant value() const{
+                       return m_value;
+               }
+
+               /**
+                * @return the type of dataset of this object
+                */
+               BlueObelisk type() const{
+                       return m_type;
+               }
+
+               /**
+                * @param type the type of this object
+                */
+               void setType( BlueObelisk type ){
+                       m_type = type;
+               }
+
+               /**
+                * @overload
+                */
+               void setType( int type ){
+                       m_type = ( BlueObelisk ) type;
+               }
+
+       private:
+               QVariant m_value;
+               BlueObelisk m_type;
+};
+
+#endif // CHEMICALDATAOBJECT_H
index f7cc6a87fc841066e957f247e5cd4e5a8c8fd67a..22b5f4d940750b40eea16ceec29321da32b0f25f 100644 (file)
@@ -22,6 +22,7 @@
 #include "spectrum.h"
 #include "isotope.h"
 #include "tempunit.h"
+#include "chemicaldataobject.h"
 
 #include <kdebug.h>
 #include <klocale.h>
@@ -37,6 +38,22 @@ Element::Element()
        m_abundance = 0;
 }
 
+QVariant Element::data(ChemicalDataObject::BlueObelisk type)
+{
+       foreach( ChemicalDataObject*o, dataList ) {
+               if ( o->type() == type )
+                       return o->value();
+       }
+}
+
+QString Element::dataAsString(ChemicalDataObject::BlueObelisk type)
+{
+       foreach( ChemicalDataObject*o, dataList ) {
+               if ( o->type() == type )
+                       return o->valueAsString();
+       }
+}
+
 Isotope* Element::isotopeByNucleons( int numberOfNucleons )
 {
        QList<Isotope*>::ConstIterator it = m_isotopeList.begin();
@@ -268,18 +285,3 @@ double Element::radius( RADIUSTYPE type )
        return 0.0;
 }
 
-
-ChemicalDataObject::ChemicalDataObject( QVariant v, BlueObelisk type )
-{
-       m_value = v;
-       m_type = type;
-};
-
-ChemicalDataObject::ChemicalDataObject() 
-{
-}
-
-QString ChemicalDataObject::valueAsString()
-{
-       return m_value.toString();
-}
index 0c88c40e5ac20583549e6b87c1d0ca51d8ba4884..cbe682b841b0b5fb7a684addec79118a37192da2 100644 (file)
 #include <QList>
 #include <QVariant>
 
-#include <kdemacros.h>
+#include "chemicaldataobject.h"
 
-class Element;
 class QDomDocument;
 class QPainter;
 class QPoint;
 class QFont;
 class QRect;
+
 class Spectrum;
 class Isotope;
+class Element;
+class ChemicalDataObject;
 
 struct coordinate;
 
@@ -429,6 +431,16 @@ class Element{
                        return m_Color; 
                }
 
+               QList<ChemicalDataObject*> dataList;
+
+               void addData( ChemicalDataObject*o ){
+                       dataList.append( o );
+               }
+
+               QVariant data( ChemicalDataObject::BlueObelisk type );
+               
+               QString dataAsString( ChemicalDataObject::BlueObelisk type );
+
        private:
                /**
                 * the integer num represents the number of the element
@@ -483,105 +495,4 @@ class Element{
                doubleList m_ionenergies;
 };
 
-/**
- * @author Carsten Niehaus <cniehaus@kde.org
- */
-class ChemicalDataObject
-{
-       public:
-               /**
-                * The BlueObelisk-project defines in their xml-file the dataset
-                * with the names of the enum plus 'bo:'. So for symbol
-                * it is 'bo:symbol'. To avoid confusion I will choose the very
-                * same naming
-                */
-               enum BlueObelisk
-               {
-                       atomicNumber = 0 /**< The atomic number of the element */,
-                       symbol/**< the symbol of the element */,
-                       name/**< The IUPAC-name of the element */,
-                       mass/**< # IUPAC Official Masses */,
-                       exactMass/**< exact masses of the most common isotopes for each element */,
-                       ionization/**< First inizationenergy */,
-                       electronAffinity/**< the electronaffinity of the element */,
-                       electronegativityPauling/**< the electronegativity in the definition of Pauling*/,
-                       radiusCovalent/**< the covalent radius */,
-                       radiusVDW/**< the van der Waals radius */
-               };
-               
-               /**
-                * public constructor
-                */
-               ChemicalDataObject();
-
-               /**
-                * public constructor
-                * @param v the data of the object
-                * @param type the type of the data
-                */
-               ChemicalDataObject(QVariant v, 
-                               BlueObelisk type);
-
-               /**
-                * set the data of this object to @p v
-                */
-               void setData( QVariant *v ){
-                       m_value = v;
-               }
-               
-               /**
-                * public destructor
-                */
-               ~ChemicalDataObject();
-
-               /**
-                * Every ChemicalDataObject contains one data. For example a
-                * integer value which represents the boilingpoint. This method
-                * returns the value as a QString
-                * 
-                * For bool, the returned string will be "false" or "true"
-                * For a QString, the QString will be returned
-                * For a int or double, the value will be returned as a QString
-                *
-                * @return the value as a QString.
-                */
-               QString valueAsString();
-               
-               /**
-                * Every ChemicalDataObject contains one data. For example a
-                * integer value which represents the boilingpoint. This method
-                * returns the value as a QVariant
-                *
-                * @return the value as a QVariant.
-                */
-               QVariant value() const{
-                       return m_value;
-               }
-
-               /**
-                * @return the type of dataset of this object
-                */
-               BlueObelisk type() const{
-                       return m_type;
-               }
-
-               /**
-                * @param type the type of this object
-                */
-               void setType( BlueObelisk type ){
-                       m_type = type;
-               }
-
-               /**
-                * @overload
-                */
-               void setType( int type ){
-                       m_type = ( BlueObelisk ) type;
-               }
-
-       private:
-               QVariant m_value;
-               BlueObelisk m_type;
-};
-
 #endif
index 59ebcb3f61c06a4e6490cb9cc4bc00af1820a876..2580f2b9750671b3b138694ccf4bf9f434829ae1 100644 (file)
@@ -25,6 +25,7 @@ ElementSaxParser::ElementSaxParser()
        inElement_(false), 
        inName_(false), 
        inMass_( false ),
+       inExactMass_( false ),
        inAtomicNumber_(false), 
        inSymbol_( false )
 {
@@ -37,8 +38,11 @@ bool ElementSaxParser::startElement(const QString&, const QString &localName, co
                inElement_ = true;
        } else if (inElement_ && localName == "scalar") {
                for (int i = 0; i < attrs.length(); ++i) {
+
                        if (attrs.value(i) == "bo:name")
                                inName_ = true;
+                       if (attrs.value(i) == "bo:exactMass")
+                               inExactMass_ = true;
                        if (attrs.value(i) == "bo:mass")
                                inMass_ = true;
                        if (attrs.value(i) == "bo:atomicNumber")
@@ -55,6 +59,8 @@ bool ElementSaxParser::endElement (  const QString & namespaceURI, const QString
        if ( localName == "elementType" )
        {
                elements_.append(currentElement_);
+//X            if ( currentElement_ )
+//X                    kdDebug() << "Number of ChemicalDataObject: " << currentElement_->dataList.count() << endl;
                currentElement_ = 0;
                inElement_ = false;
        }
@@ -63,23 +69,44 @@ bool ElementSaxParser::endElement (  const QString & namespaceURI, const QString
 
 bool ElementSaxParser::characters(const QString &ch)
 {
+       ChemicalDataObject *dataobject = new ChemicalDataObject();
+       ChemicalDataObject::BlueObelisk type;
+       QVariant value;
+
        if (inName_) {
-               currentElement_->setName(ch);
+               value = ch;
+               type = ChemicalDataObject::name; 
                inName_ = false;
        }
-       if ( inMass_ ){
-               currentElement_->setMass( ch.toDouble() );
+       else if ( inMass_ ){
+               value = ch.toDouble();
+               type = ChemicalDataObject::mass; 
                inMass_ = false;
        }
-       if (inSymbol_) {
-               currentElement_->setSymbol(ch);
+       else if ( inExactMass_ ){
+               value = ch.toDouble();
+               type = ChemicalDataObject::exactMass; 
+               inExactMass_ = false;
+       }
+       else if (inSymbol_) {
+               value = ch;
+               type = ChemicalDataObject::symbol; 
                inSymbol_ = false;
        }
-       if (inAtomicNumber_) {
-               currentElement_->setNumber(ch.toInt());
+       else if (inAtomicNumber_) {
+               value = ch.toInt();
+               type = ChemicalDataObject::atomicNumber; 
                inAtomicNumber_ = false;
        }
 
+       dataobject->setData( value );
+       dataobject->setType( type );
+
+//X    kdDebug() << dataobject->valueAsString() << endl;
+
+       if ( currentElement_ )
+               currentElement_->addData( dataobject );
+
        return true;
 }
 
index af1aa5b1b132e73cd271db06685e5fe031450405..27561ee1881e3c3114c41d591c44bbd943ca16dc 100644 (file)
@@ -37,6 +37,7 @@ class ElementSaxParser : public QXmlDefaultHandler
                bool inElement_;
                bool inName_,
                         inMass_,
+                        inExactMass_,
                         inAtomicNumber_,
                         inSymbol_;
 };
index 0badd105ef0cb957e1d8ac7877372114e50f9c58..9aa3af065ac81cb893c0ad39e4cc215ce7c0c621 100644 (file)
@@ -16,6 +16,7 @@ int main(int argc, char *argv[])
        QFile xmlFile(argv[1]);
        QXmlInputSource source(xmlFile);
        QXmlSimpleReader reader;
+
        reader.setContentHandler(parser);
        reader.parse(source);
 
@@ -23,10 +24,19 @@ int main(int argc, char *argv[])
 
        foreach( Element* e, v ){
                if ( e )
-                       kdDebug() << "(" << e->number() << ", " <<
-                                      e->elementName() << ", " << e->symbol() << ") " << 
-                               ", mass: " << e->mass() << 
-                               endl;
+               {
+                       QList<ChemicalDataObject*> list = e->dataList;
+
+                       //Give me the name of the element
+                       kdDebug() << "Name: " << e->dataAsString( ChemicalDataObject::name ) << endl;
+                       
+//X                    //give me all you have
+//X                    foreach( ChemicalDataObject*o, list ){
+//X                            if ( o )
+//X                                    kdDebug() << o->valueAsString() << endl;
+//X                    }
+               }
+
        }
 
        return 0;