]> Git trees. - libqmvoc.git/commitdiff
* Reading from BlueObelisk is almost working
authorCarsten Niehaus <cniehaus@gmx.de>
Sat, 15 Oct 2005 12:11:03 +0000 (12:11 +0000)
committerCarsten Niehaus <cniehaus@gmx.de>
Sat, 15 Oct 2005 12:11:03 +0000 (12:11 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=470835

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

index 09697f8bb6658caa4005462aba8e5db05862dd3e..bd2a3d6431091c8128551d6f81639fb30fd8d977 100644 (file)
@@ -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
index 60a92c4ce33de5045ec7d0b5ae43c83abb569ef0..35268e067b89469b1b03f8688eaa4a2c17b59104 100644 (file)
@@ -23,6 +23,8 @@
 #include <QColor>
 #include <QList>
 
+#include <kdemacros.h>
+
 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 (file)
index 0000000..5722adb
--- /dev/null
@@ -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 <qdom.h>
+#include <QList>
+#include <QFile>
+#include <QStringList>
+
+#include <kdebug.h>
+#include <kurl.h>
+
+QList<Element*> ElementParser::loadAllElements( const QDomDocument& dataDocument2 )
+{
+       //only for testing!
+       QDomDocument dataDocument = dataDocument2;
+       
+       QList<Element*> 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 (file)
index 0000000..d0caa76
--- /dev/null
@@ -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 <QString>
+#include <QList>
+#include <QStringList>
+#include <qdom.h>
+
+class Element;
+
+/**
+ * @author Carsten Niehaus <cniehaus@kde.org>
+ */
+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<Element*> loadAllElements(const QDomDocument& dataDocument);
+};
+