1
/***************************************************************************
2
* Copyright (C) 2005, 2006, 2007 by Carsten Niehaus *
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. *
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. *
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>
25
#include <isotopeparser.h>
26
#include <spectrumparser.h>
34
#include <kstandarddirs.h>
35
#include <kpixmapcache.h>
37
#include <kunitconversion/converter.h>
39
struct StaticKalziumDataObject
41
KalziumDataObject kdo;
44
K_GLOBAL_STATIC( StaticKalziumDataObject, s_kdo )
46
KalziumDataObject* KalziumDataObject::instance()
51
KalziumDataObject::KalziumDataObject()
55
ElementSaxParser * parser = new ElementSaxParser();
57
QFile xmlFile( KStandardDirs::locate( "data", "libkdeedu/data/elements.xml" ) );
58
QXmlInputSource source(&xmlFile);
59
QXmlSimpleReader reader;
61
reader.setContentHandler(parser);
64
ElementList = parser->getElements();
66
//we don't need parser anymore, let's free its memory
70
SpectrumParser * spectrumparser = new SpectrumParser();
72
QFile xmlSpFile( KStandardDirs::locate( "data", "libkdeedu/data/spectra.xml" ) );
73
QXmlInputSource spsource(&xmlSpFile);
74
QXmlSimpleReader sp_reader;
76
sp_reader.setContentHandler(spectrumparser);
77
sp_reader.parse(spsource);
79
m_spectra = spectrumparser->getSpectrums();
81
//we don't need spectrumparser anymore, let's free its memory
82
delete spectrumparser;
85
IsotopeParser * isoparser = new IsotopeParser();
87
QFile xmlIsoFile( KStandardDirs::locate( "data", "libkdeedu/data/isotopes.xml" ) );
88
QXmlInputSource isosource(&xmlIsoFile);
89
QXmlSimpleReader isoreader;
91
isoreader.setContentHandler(isoparser);
92
isoreader.parse(isosource);
94
QList<Isotope*> isotopes = isoparser->getIsotopes();
96
//we don't need isoparser anymore, let's free its memory
99
foreach( Isotope *iso, isotopes )
101
int num = iso->parentElementNumber();
102
if ( m_isotopes.contains( num ) ) {
103
m_isotopes[num].append( iso );
105
QList<Isotope*> newlist;
106
newlist.append( iso );
107
m_isotopes.insert( num, newlist );
112
m_numOfElements = ElementList.count();
114
qAddPostRoutine( KalziumDataObject::cleanup );
117
KalziumDataObject::~KalziumDataObject()
119
//Delete all elements
120
qDeleteAll(ElementList);
122
//Delete all isotopes
123
QHashIterator<int, QList<Isotope*> > i(m_isotopes);
124
while (i.hasNext()) {
126
qDeleteAll( i.value());
130
Element* KalziumDataObject::element( int number )
132
// checking that we are requesting a valid element
133
if ( ( number <= 0 ) || ( number > m_numOfElements ) )
135
return ElementList[ number-1 ];
138
QString KalziumDataObject::unitAsString( const int unit ) const
140
return KUnitConversion::Converter().unit( unit ).data()->symbol();
143
QPixmap KalziumDataObject::pixmap( int number )
145
// checking that we are requesting a valid element
146
if ( ( number <= 0 ) || ( number > m_numOfElements ) )
148
if ( PixmapList.isEmpty() )
150
return PixmapList[ number-1 ];
153
QList<Isotope*> KalziumDataObject::isotopes( Element * element )
155
return isotopes( element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt() );
158
QList<Isotope*> KalziumDataObject::isotopes( int number )
160
return m_isotopes.contains( number ) ? m_isotopes.value( number ) : QList<Isotope*>();
163
Spectrum * KalziumDataObject::spectrum( int number )
165
foreach (Spectrum * s, m_spectra ) {
166
if (s->parentElementNumber() == number ) {
175
void KalziumDataObject::setSearch( Search *srch )
180
Search* KalziumDataObject::search() const
185
void KalziumDataObject::cleanup()
187
KalziumDataObject::instance()->cleanPixmaps();
190
void KalziumDataObject::loadIconSet()
192
KPixmapCache cache("kalzium");
193
//FIXME in case we ever get more than one theme we need
194
//a settings-dialog where we can select the different iconsets...
195
const QString setname = "school";
196
const QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
198
for ( int i = 0 ; i < m_numOfElements ; i++ )
200
QString filename = pathname + setname + '/' + QString::number( i+1 ) + ".svg";
202
QPixmap pix = cache.loadFromSvg( filename, QSize( 40, 40 ) );
203
if ( pix.isNull() ) {
204
pix = QPixmap( 40, 40 );
205
pix.fill(Qt::transparent);
208
Element *e = ElementList.at(i);
209
QString esymbol = e->dataAsString( ChemicalDataObject::symbol );
210
p.drawText(0,0,40,40, Qt::AlignCenter | Qt::TextWordWrap, esymbol );
217
void KalziumDataObject::cleanPixmaps()