Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / kalziumdataobject.cpp
blobd15485adbb212d3ebe2c8292afcef448eaf9edc7
1 /***************************************************************************
2 * Copyright (C) 2005, 2006, 2007 by Carsten Niehaus *
3 * cniehaus@kde.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "kalziumdataobject.h"
23 #include <elementparser.h>
24 #include <isotope.h>
25 #include <isotopeparser.h>
26 #include <spectrumparser.h>
28 #include <QFile>
29 #include <QVariant>
30 #include <QSvgRenderer>
31 #include <QPainter>
33 #include <klocale.h>
34 #include <kdebug.h>
35 #include <kurl.h>
36 #include <kstandarddirs.h>
37 #include <kpixmapcache.h>
39 KalziumDataObject* KalziumDataObject::instance()
41 static KalziumDataObject kdo;
42 return &kdo;
45 KalziumDataObject::KalziumDataObject()
46 : m_search( 0 )
48 // reading elements
49 ElementSaxParser * parser = new ElementSaxParser();
51 QFile xmlFile( KStandardDirs::locate( "data", "libkdeedu/data/elements.xml" ) );
52 QXmlInputSource source(&xmlFile);
53 QXmlSimpleReader reader;
55 reader.setContentHandler(parser);
56 reader.parse(source);
58 ElementList = parser->getElements();
60 //we don't need parser anymore, let's free its memory
61 delete parser;
63 //read the spectra
64 SpectrumParser * spectrumparser = new SpectrumParser();
66 QFile xmlSpFile( KStandardDirs::locate( "data", "libkdeedu/data/spectra.xml" ) );
67 QXmlInputSource spsource(&xmlSpFile);
68 QXmlSimpleReader sp_reader;
70 sp_reader.setContentHandler(spectrumparser);
71 sp_reader.parse(spsource);
73 m_spectra = spectrumparser->getSpectrums();
75 //we don't need spectrumparser anymore, let's free its memory
76 delete spectrumparser;
78 // reading isotopes
79 IsotopeParser * isoparser = new IsotopeParser();
81 QFile xmlIsoFile( KStandardDirs::locate( "data", "libkdeedu/data/isotopes.xml" ) );
82 QXmlInputSource isosource(&xmlIsoFile);
83 QXmlSimpleReader isoreader;
85 isoreader.setContentHandler(isoparser);
86 isoreader.parse(isosource);
88 QList<Isotope*> isotopes = isoparser->getIsotopes();
90 //we don't need isoparser anymore, let's free its memory
91 delete isoparser;
93 foreach( Isotope *iso, isotopes )
95 int num = iso->parentElementNumber();
96 if ( m_isotopes.contains( num ) )
98 m_isotopes[num].append( iso );
100 else
102 QList<Isotope*> newlist;
103 newlist.append( iso );
104 m_isotopes.insert( num, newlist );
108 // cache it
109 m_numOfElements = ElementList.count();
111 KPixmapCache cache("kalzium");
113 for ( int i = 0 ; i < m_numOfElements ; i++ )
115 //FIXME in case we ever get more than one theme we need
116 //a settings-dialog where we can select the different iconsets...
117 QString setname = "school";
119 QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
121 QString filename = pathname + setname + '/' + QString::number( i+1 ) + ".svg";
123 QPixmap pix = cache.loadFromSvg( filename, QSize( 40, 40 ) );
124 if ( pix.isNull() ) {
125 pix = QPixmap( 40, 40 );
126 pix.fill(Qt::transparent);
128 QPainter p( &pix );
129 Element *e = ElementList.at(i);
130 QString esymbol = e->dataAsString( ChemicalDataObject::symbol );
131 p.drawText(0,0,40,40, Qt::AlignCenter | Qt::TextWordWrap, esymbol );
132 p.end();
134 PixmapList << pix;
139 KalziumDataObject::~KalziumDataObject()
141 //Delete all elements
142 qDeleteAll(ElementList);
144 //Delete all isotopes
145 QHashIterator<int, QList<Isotope*> > i(m_isotopes);
146 while (i.hasNext()) {
147 i.next();
148 qDeleteAll( i.value());
152 Element* KalziumDataObject::element( int number )
154 // checking that we are requesting a valid element
155 if ( ( number <= 0 ) || ( number > m_numOfElements ) )
156 return 0;
157 return ElementList[ number-1 ];
160 QPixmap KalziumDataObject::pixmap( int number )
162 // checking that we are requesting a valid element
163 if ( ( number <= 0 ) || ( number > m_numOfElements ) )
164 return 0;
165 return PixmapList[ number-1 ];
168 QList<Isotope*> KalziumDataObject::isotopes( Element * element )
170 return isotopes( element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt() );
173 QList<Isotope*> KalziumDataObject::isotopes( int number )
175 return m_isotopes.contains( number ) ? m_isotopes.value( number ) : QList<Isotope*>();
178 Spectrum * KalziumDataObject::spectrum( int number )
180 foreach (Spectrum * s, m_spectra ) {
181 if (s->parentElementNumber() == number ) {
182 return s;
186 return 0;
190 void KalziumDataObject::setSearch( Search *srch )
192 m_search = srch;
195 Search* KalziumDataObject::search() const
197 return m_search;