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 ***************************************************************************/
20
21#include "kalziumdataobject.h"
22
23#include <elementparser.h>
24#include <isotope.h>
25#include <isotopeparser.h>
26#include <spectrumparser.h>
27
28#include <QFile>
29#include <QVariant>
30#include <QSvgRenderer>
31#include <QPainter>
32
33#include <klocale.h>
34#include <kdebug.h>
35#include <kurl.h>
36#include <kstandarddirs.h>
37#include <kpixmapcache.h>
38#include <QXmlReader>
39
40KalziumDataObject* KalziumDataObject::instance()
41{
42 static KalziumDataObject kdo;
43 return &kdo;
44}
45
46KalziumDataObject::KalziumDataObject()
47 : m_search( 0 )
48{
49 // reading elements
50 ElementSaxParser * parser = new ElementSaxParser();
51
52 QFile xmlFile( KStandardDirs::locate( "data", "libkdeedu/data/elements.xml" ) );
53 QXmlInputSource source(&xmlFile);
54 QXmlSimpleReader reader;
55
56 reader.setContentHandler(parser);
57 reader.parse(source);
58
59 ElementList = parser->getElements();
60
61 //we don't need parser anymore, let's free its memory
62 delete parser;
63
64 //read the spectra
65 SpectrumParser * spectrumparser = new SpectrumParser();
66
67 QFile xmlSpFile( KStandardDirs::locate( "data", "libkdeedu/data/spectra.xml" ) );
68 QXmlInputSource spsource(&xmlSpFile);
69 QXmlSimpleReader sp_reader;
70
71 sp_reader.setContentHandler(spectrumparser);
72 sp_reader.parse(spsource);
73
74 m_spectra = spectrumparser->getSpectrums();
75
76 //we don't need spectrumparser anymore, let's free its memory
77 delete spectrumparser;
78
79 // reading isotopes
80 IsotopeParser * isoparser = new IsotopeParser();
81
82 QFile xmlIsoFile( KStandardDirs::locate( "data", "libkdeedu/data/isotopes.xml" ) );
83 QXmlInputSource isosource(&xmlIsoFile);
84 QXmlSimpleReader isoreader;
85
86 isoreader.setContentHandler(isoparser);
87 isoreader.parse(isosource);
88
89 QList<Isotope*> isotopes = isoparser->getIsotopes();
90
91 //we don't need isoparser anymore, let's free its memory
92 delete isoparser;
93
94 foreach( Isotope *iso, isotopes )
95 {
96 int num = iso->parentElementNumber();
97 if ( m_isotopes.contains( num ) )
98 {
99 m_isotopes[num].append( iso );
100 }
101 else
102 {
103 QList<Isotope*> newlist;
104 newlist.append( iso );
105 m_isotopes.insert( num, newlist );
106 }
107 }
108
109 // cache it
110 m_numOfElements = ElementList.count();
111
112 KPixmapCache cache("kalzium");
113
114 for ( int i = 0 ; i < m_numOfElements ; i++ )
115 {
116 //FIXME in case we ever get more than one theme we need
117 //a settings-dialog where we can select the different iconsets...
118 QString setname = "school";
119
120 QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
121
122 QString filename = pathname + setname + '/' + QString::number( i+1 ) + ".svg";
123
124 QPixmap pix = cache.loadFromSvg( filename, QSize( 40, 40 ) );
125 if ( pix.isNull() ) {
126 pix = QPixmap( 40, 40 );
127 pix.fill(Qt::transparent);
128
129 QPainter p( &pix );
130 Element *e = ElementList.at(i);
131 QString esymbol = e->dataAsString( ChemicalDataObject::symbol );
132 p.drawText(0,0,40,40, Qt::AlignCenter | Qt::TextWordWrap, esymbol );
133 p.end();
134 }
135 PixmapList << pix;
136 }
137
138}
139
140KalziumDataObject::~KalziumDataObject()
141{
142 //Delete all elements
143 qDeleteAll(ElementList);
144
145 //Delete all isotopes
146 QHashIterator<int, QList<Isotope*> > i(m_isotopes);
147 while (i.hasNext()) {
148 i.next();
149 qDeleteAll( i.value());
150 }
151}
152
153Element* KalziumDataObject::element( int number )
154{
155 // checking that we are requesting a valid element
156 if ( ( number <= 0 ) || ( number > m_numOfElements ) )
157 return 0;
158 return ElementList[ number-1 ];
159}
160
161QPixmap KalziumDataObject::pixmap( int number )
162{
163 // checking that we are requesting a valid element
164 if ( ( number <= 0 ) || ( number > m_numOfElements ) )
165 return 0;
166 return PixmapList[ number-1 ];
167}
168
169QList<Isotope*> KalziumDataObject::isotopes( Element * element )
170{
171 return isotopes( element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt() );
172}
173
174QList<Isotope*> KalziumDataObject::isotopes( int number )
175{
176 return m_isotopes.contains( number ) ? m_isotopes.value( number ) : QList<Isotope*>();
177}
178
179Spectrum * KalziumDataObject::spectrum( int number )
180{
181 foreach (Spectrum * s, m_spectra ) {
182 if (s->parentElementNumber() == number ) {
183 return s;
184 }
185 }
186
187 return 0;
188}
189
190
191void KalziumDataObject::setSearch( Search *srch )
192{
193 m_search = srch;
194}
195
196Search* KalziumDataObject::search() const
197{
198 return m_search;
199}
200