]> Git trees. - libqmvoc.git/commitdiff
* Add support for units
authorCarsten Niehaus <cniehaus@gmx.de>
Sun, 23 Oct 2005 11:59:34 +0000 (11:59 +0000)
committerCarsten Niehaus <cniehaus@gmx.de>
Sun, 23 Oct 2005 11:59:34 +0000 (11:59 +0000)
* Add the period

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

libscience/chemicaldataobject.cpp
libscience/chemicaldataobject.h
libscience/elementparser.cpp
libscience/elementparser.h
libscience/tests/xmlreadingtest.cpp

index 744dc76e4d59d72d169921a06a2c8617809291c9..56d0239948a05c8aea97c69930babcecb68c94cd 100644 (file)
@@ -25,11 +25,13 @@ ChemicalDataObject::ChemicalDataObject( QVariant v, BlueObelisk type )
 {
        m_value = v;
        m_type = type;
+       m_unit = ChemicalDataObject::noUnit;
 };
 
 ChemicalDataObject::ChemicalDataObject() 
 {
        m_value = QVariant();
+       m_unit = ChemicalDataObject::noUnit;
 }
 
 QString ChemicalDataObject::valueAsString()
@@ -119,10 +121,44 @@ QString ChemicalDataObject::dictRef()
                case periodTableBlock:
                        botype = "periodTableBlock";
                        break;
+               case nameOrigin:
+                       botype = "nameOrigin";
+                       break;
+               case orbit:
+                       botype = "orbit";
+                       break;
+               case period:
+                       botype = "period";
+                       break;
        }
        
        botype = botype.prepend( "bo:" );
        
        return botype;
 }
+
+QString ChemicalDataObject::unitAsString()
+{
+       QString bounit;
+       switch ( m_unit ){
+               case kelvin:
+                       bounit = "kelvin";
+                       break;
+               case nm:
+                       bounit = "nm";
+                       break;
+               case pm:
+                       bounit = "pm";
+                       break;
+               case ev:
+                       bounit = "ev";
+                       break;
+               case noUnit:
+                       return "noUnit";
+       }
+       
+       bounit = bounit.prepend( "bo:" );
+       
+       return bounit;
+}
        
index ebe67a22bc36c54fada03a54d27878b3cb262b45..cb606fab5b07dcef73358eeab5ff0641f2401bd1 100644 (file)
@@ -55,7 +55,22 @@ class ChemicalDataObject
                        meltingpoint,
                        boilingpoint,
                        periodTableBlock,
-                       nameOrigin
+                       nameOrigin,
+                       orbit,
+                       period
+               };
+
+               /**
+                * The BlueObelisk-project defines in their XML file the dataset
+                * with the units in the namespace "bo". 
+                */
+               enum BlueObeliskUnit
+               {
+                       kelvin = 0/**< Degree Kelvin */,
+                       ev/**< electron volt */,
+                       nm/**< nanometer */,
+                       pm/**< picometer */,
+                       noUnit/**< no unit */
                };
 
                /**
@@ -153,10 +168,28 @@ class ChemicalDataObject
                 * identifier. For example, for the mass it is "bo:mass"
                 */
                QString dictRef();
+
+               /**
+                * @return the unit of the object as a QString. For example kelvin 
+                * will be returned as "bo:kelvin"
+                */
+               QString unitAsString();
+
+               /**
+                * @return the unit of the object
+                */
+               BlueObeliskUnit unit() const{
+                       return m_unit;
+               }
+
+               void setUnit( BlueObeliskUnit unit ){
+                       m_unit = unit;
+               }
                
        private:
                QVariant m_value;
                BlueObelisk m_type;
+               BlueObeliskUnit m_unit;
 };
 
 #endif // CHEMICALDATAOBJECT_H
index ecf252e9e502b8e002063aab81fec73534f3019d..a4a3c8afdd1a71197ab18a8d0a804e149a5592db 100644 (file)
@@ -35,7 +35,8 @@ ElementSaxParser::ElementSaxParser()
        inBoilingPoint_(false),
        inMeltingPoint_(false),
        inPeriodTableBlock_(false),
-       inNameOrigin_(false)
+       inNameOrigin_(false),
+       inPeriod_(false)
 {
 }
 
@@ -75,6 +76,8 @@ bool ElementSaxParser::startElement(const QString&, const QString &localName, co
                                inPeriodTableBlock_ = true;
                        else if (attrs.value(i) == "bo:nameOrigin")
                                inNameOrigin_ = true;
+                       else if (attrs.value(i) == "bo:period")
+                               inPeriod_ = true;
                }
        }
        return true;
@@ -169,6 +172,11 @@ bool ElementSaxParser::characters(const QString &ch)
                type = ChemicalDataObject::nameOrigin; 
                inNameOrigin_ = false;
        }
+       else if (inPeriod_) {
+               value = ch.toInt();
+               type = ChemicalDataObject::period; 
+               inPeriod_ = false;
+       }
        else//it is a non known value. Do not create a wrong object but return
                return true;
 
index afcb922039abcdcd35ec0580994173785d47c6a6..58ed4e2d226fa0a24c5284d61b9288de6b78fcb5 100644 (file)
@@ -48,6 +48,7 @@ class ElementSaxParser : public QXmlDefaultHandler
                         inBoilingPoint_,
                         inMeltingPoint_,
                         inPeriodTableBlock_,
-                        inNameOrigin_;
+                        inNameOrigin_,
+                        inPeriod_;
 };
 #endif // ELEMENTPARSER_H
index d36662c66d88b6f7b4200754f4c6ba55deca5087..a0e77df4b7e298c78f99e7cb68cf070204ef4825 100644 (file)
@@ -41,7 +41,12 @@ int main(int argc, char *argv[])
                        //Test: give me all data available
                        foreach( ChemicalDataObject*o, list ){
                                if ( o )
-                                       kdDebug() << "Name: " << o->dictRef() << " " << o->valueAsString() << endl;
+                               {
+                                       QString unit = o->unitAsString();
+                                       if ( unit == "bo:noUnit" )
+                                               unit = "";
+                                       kdDebug() << "Name: " << o->dictRef() << " " << o->valueAsString()  <<" "  << unit << endl;
+                               }
                        }
                }