From aa84ffb06512ae2ca9cf0e75a2efa02185949861 Mon Sep 17 00:00:00 2001 From: Carsten Niehaus Date: Sat, 15 Oct 2005 12:11:03 +0000 Subject: [PATCH] * Reading from BlueObelisk is almost working svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=470835 --- libscience/Makefile.am | 1 + libscience/element.h | 8 ++- libscience/elementparser.cpp | 115 +++++++++++++++++++++++++++++++++++ libscience/elementparser.h | 44 ++++++++++++++ 4 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 libscience/elementparser.cpp create mode 100644 libscience/elementparser.h diff --git a/libscience/Makefile.am b/libscience/Makefile.am index 09697f8..bd2a3d6 100644 --- a/libscience/Makefile.am +++ b/libscience/Makefile.am @@ -9,6 +9,7 @@ libscience_la_SOURCES = \ spectrum.cpp \ isotope.cpp \ spectrumparser.cpp \ + elementparser.cpp \ tempunit.cpp libscience_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0 diff --git a/libscience/element.h b/libscience/element.h index 60a92c4..35268e0 100644 --- a/libscience/element.h +++ b/libscience/element.h @@ -23,6 +23,8 @@ #include #include +#include + class Element; class QDomDocument; class QPainter; @@ -260,7 +262,11 @@ class Element{ /** * @return the name of the element */ - QString elname() const { + QString elname() const KDE_DEPRECATED{ + return elementName(); + } + + QString elementName() const{ return m_name; } diff --git a/libscience/elementparser.cpp b/libscience/elementparser.cpp new file mode 100644 index 0000000..5722adb --- /dev/null +++ b/libscience/elementparser.cpp @@ -0,0 +1,115 @@ +/*************************************************************************** +copyright : (C) 2005 by Carsten Niehaus +email : 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. * + * * + ***************************************************************************/ +#include "elementparser.h" +#include "element.h" + +#include +#include +#include +#include + +#include +#include + +QList ElementParser::loadAllElements( const QDomDocument& dataDocument2 ) +{ + //only for testing! + QDomDocument dataDocument = dataDocument2; + + QList elementList; + + KURL url; + url.setPath("/home/carsten/svn/trunk/KDE/kdeedu/libkdeedu/libscience/" ); + url.setFileName( "elements.xml" ); + QFile layoutFile( url.path() ); + + if (!layoutFile.open(IO_ReadOnly)) + { + kdDebug() << "layoutfile IO-error" << endl; + } + + // Check if the document is well-formed + if (!dataDocument.setContent(&layoutFile)) + { + kdDebug() << "wrong xml" << endl; + layoutFile.close(); + } + layoutFile.close(); + + QStringList elementSymbols = loadElementSymbols(dataDocument); + + foreach(QString symbol, elementSymbols) + { + Element *e = loadElement( symbol, dataDocument ); + if ( e ) + elementList.append( e ); + } + + kdDebug() << elementList.count() << endl; + + return elementList; +} + +QStringList ElementParser::loadElementSymbols( const QDomDocument& dataDocument ) +{ + QStringList symbolList; + + //xml-reading + QDomNodeList elementNodes = dataDocument.elementsByTagName( "elementType" ); + + const uint count = elementNodes.count(); + + for ( uint i = 0; i < count; ++i ) + { + QString symbol = elementNodes.item( i ).toElement().attribute("id"); + symbolList.append( symbol ); + + //for debugging! + loadElement( symbol, dataDocument ); + } + + return symbolList; +} + +Element* ElementParser::loadElement( const QString& symbol, const QDomDocument& dataDocument ) +{ + QDomNodeList elementNodes = dataDocument.elementsByTagName( "elementType" ); + const uint count = elementNodes.count(); + + for ( uint i = 0; i < count; ++i ) + { + QDomElement currentElement = elementNodes.item( i ).toElement(); + QString currentSymbol = currentElement.attribute("id"); + + if ( currentSymbol == symbol ) + return loadElement( currentElement ); + } + + //the element was not found... + return 0; +} + +Element* ElementParser::loadElement( const QDomElement& element ) +{ + Element *e = new Element(); + + QString symbol = element.attribute( "id" ); + + QDomNodeList scalarList = element.elementsByTagName( "scalar" ); + + e->setSymbol( symbol ); + + kdDebug() << "scalarList::count of element " << e->symbol() << ": " << scalarList.count() << endl; + + return e; +} diff --git a/libscience/elementparser.h b/libscience/elementparser.h new file mode 100644 index 0000000..d0caa76 --- /dev/null +++ b/libscience/elementparser.h @@ -0,0 +1,44 @@ +/*************************************************************************** + copyright : (C) 2005 by Carsten Niehaus + email : 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. * + * * + ***************************************************************************/ + +#include +#include +#include +#include + +class Element; + +/** + * @author Carsten Niehaus + */ +class ElementParser +{ + public: + /** + * @return the Element with the symbol symbol + */ + static Element* loadElement( const QString& symbol, const QDomDocument& dataDocument ); + + static Element* loadElement( const QDomElement& element ); + + /** + * @return a QStringList with the symbols of all known elements + */ + static QStringList loadElementSymbols(const QDomDocument& dataDocument); + + /** + * @return all chemical elements in the xml-file + */ + static QList loadAllElements(const QDomDocument& dataDocument); +}; + -- 2.47.3