• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kalzium

kalziumdataobject.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005, 2006, 2007 by Carsten Niehaus                     *
00003  *   cniehaus@kde.org                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.          *
00019  ***************************************************************************/
00020 
00021 #include "kalziumdataobject.h"
00022 
00023 #include <elementparser.h>
00024 #include <isotope.h>
00025 #include <isotopeparser.h>
00026 
00027 #include <QFile>
00028 #include <QVariant>
00029 #include <QSvgRenderer>
00030 #include <QPainter>
00031 
00032 #include <klocale.h>
00033 #include <kdebug.h>
00034 #include <kurl.h>
00035 #include <kstandarddirs.h>
00036 
00037 KalziumDataObject* KalziumDataObject::instance()
00038 {
00039     static KalziumDataObject kdo;
00040     return &kdo;
00041 }
00042 
00043 KalziumDataObject::KalziumDataObject()
00044     : m_search( 0 )
00045 {
00046     // reading elements
00047     ElementSaxParser * parser = new ElementSaxParser();
00048 
00049     QFile xmlFile( KStandardDirs::locate( "data", "libkdeedu/data/elements.xml" ) );
00050     QXmlInputSource source(&xmlFile);
00051     QXmlSimpleReader reader;
00052     
00053     reader.setContentHandler(parser);
00054     reader.parse(source);
00055 
00056     ElementList = parser->getElements();
00057 
00058     //we don't need parser anymore, let's free its memory
00059     delete parser;
00060 
00061     // reading isotopes
00062     IsotopeParser * isoparser = new IsotopeParser();
00063 
00064     QFile xmlIsoFile( KStandardDirs::locate( "data", "libkdeedu/data/isotopes.xml" ) );
00065     QXmlInputSource isosource(&xmlIsoFile);
00066     QXmlSimpleReader isoreader;
00067     
00068     isoreader.setContentHandler(isoparser);
00069     isoreader.parse(isosource);
00070 
00071     QList<Isotope*> isotopes = isoparser->getIsotopes();
00072 
00073     //we don't need isoparser anymore, let's free its memory
00074     delete isoparser;
00075 
00076     foreach( Isotope *iso, isotopes )
00077     {
00078         int num = iso->parentElementNumber();
00079         if ( m_isotopes.contains( num ) )
00080         {
00081             m_isotopes[num].append( iso );
00082         }
00083         else
00084         {
00085             QList<Isotope*> newlist;
00086             newlist.append( iso );
00087             m_isotopes.insert( num, newlist );
00088         }
00089     }
00090     
00091     // cache it
00092     m_numOfElements = ElementList.count();
00093             
00094     
00095     for ( int i = 0 ; i < m_numOfElements ; i++ )
00096         {
00097             //FIXME in case we ever get more than one theme we need
00098             //a settings-dialog where we can select the different iconsets...
00099             QString setname = "school";
00100 
00101             QPixmap pix( 40, 40 );
00102             pix.fill(Qt::transparent);
00103 
00104             QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
00105 
00106             QString filename = pathname + setname + '/' + QString::number( i+1 ) + ".svg";
00107 
00108             QSvgRenderer svgrenderer;
00109 
00110             QPainter p( &pix );
00111             if ( QFile::exists(filename) && svgrenderer.load(filename) ) {
00112                 svgrenderer.render( &p );
00113                 p.end();
00114 
00115                 PixmapList << pix;
00116             }
00117             else {
00118                 Element *e =  ElementList.at(i);
00119                 QString esymbol = e->dataAsString( ChemicalDataObject::symbol );
00120                 p.drawText(0,0,40,40, Qt::AlignCenter | Qt::TextWordWrap, esymbol );
00121                 p.end();
00122 
00123                 PixmapList << pix;
00124             }
00125         }
00126 
00127 }
00128 
00129 KalziumDataObject::~KalziumDataObject()
00130 {
00131     //Delete all elements
00132     qDeleteAll(ElementList);
00133 
00134     //Delete all isotopes
00135     QHashIterator<int, QList<Isotope*> > i(m_isotopes);
00136     while (i.hasNext()) {
00137         i.next();
00138         qDeleteAll( i.value());
00139     }
00140 }
00141 
00142 Element* KalziumDataObject::element( int number )
00143 {
00144     // checking that we are requesting a valid element
00145     if ( ( number <= 0 ) || ( number > m_numOfElements ) )
00146         return 0;
00147     return ElementList[ number-1 ];
00148 }
00149 
00150 QPixmap KalziumDataObject::pixmap( int number )
00151 {
00152     // checking that we are requesting a valid element
00153     if ( ( number <= 0 ) || ( number > m_numOfElements ) )
00154         return 0;
00155     return PixmapList[ number-1 ];
00156 }
00157 
00158 QList<Isotope*> KalziumDataObject::isotopes( Element * element )
00159 {
00160     return isotopes( element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt() );
00161 }
00162 
00163 QList<Isotope*> KalziumDataObject::isotopes( int number )
00164 {
00165     return m_isotopes.contains( number ) ? m_isotopes.value( number ) : QList<Isotope*>();
00166 }
00167 
00168 void KalziumDataObject::setSearch( Search *srch )
00169 {
00170     m_search = srch;
00171 }
00172 
00173 Search* KalziumDataObject::search() const
00174 {
00175     return m_search;
00176 }

kalzium

Skip menu "kalzium"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal