~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to src/kalziumdataobject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <QPainter>
 
30
 
 
31
#include <klocale.h>
 
32
#include <kdebug.h>
 
33
#include <kurl.h>
 
34
#include <kstandarddirs.h>
 
35
#include <kpixmapcache.h>
 
36
#include <kglobal.h>
 
37
#include <kunitconversion/converter.h>
 
38
 
 
39
struct StaticKalziumDataObject
 
40
{
 
41
    KalziumDataObject kdo;
 
42
};
 
43
 
 
44
K_GLOBAL_STATIC( StaticKalziumDataObject, s_kdo )
 
45
 
 
46
KalziumDataObject* KalziumDataObject::instance()
 
47
{
 
48
    return &s_kdo->kdo;
 
49
}
 
50
 
 
51
KalziumDataObject::KalziumDataObject()
 
52
        : m_search( 0 )
 
53
{
 
54
    // reading elements
 
55
    ElementSaxParser * parser = new ElementSaxParser();
 
56
 
 
57
    QFile xmlFile( KStandardDirs::locate( "data", "libkdeedu/data/elements.xml" ) );
 
58
    QXmlInputSource source(&xmlFile);
 
59
    QXmlSimpleReader reader;
 
60
 
 
61
    reader.setContentHandler(parser);
 
62
    reader.parse(source);
 
63
 
 
64
    ElementList = parser->getElements();
 
65
 
 
66
    //we don't need parser anymore, let's free its memory
 
67
    delete parser;
 
68
 
 
69
    //read the spectra
 
70
    SpectrumParser * spectrumparser = new SpectrumParser();
 
71
 
 
72
    QFile xmlSpFile( KStandardDirs::locate( "data", "libkdeedu/data/spectra.xml" ) );
 
73
    QXmlInputSource spsource(&xmlSpFile);
 
74
    QXmlSimpleReader sp_reader;
 
75
 
 
76
    sp_reader.setContentHandler(spectrumparser);
 
77
    sp_reader.parse(spsource);
 
78
 
 
79
    m_spectra = spectrumparser->getSpectrums();
 
80
 
 
81
    //we don't need spectrumparser anymore, let's free its memory
 
82
    delete spectrumparser;
 
83
 
 
84
    // reading isotopes
 
85
    IsotopeParser * isoparser = new IsotopeParser();
 
86
 
 
87
    QFile xmlIsoFile( KStandardDirs::locate( "data", "libkdeedu/data/isotopes.xml" ) );
 
88
    QXmlInputSource isosource(&xmlIsoFile);
 
89
    QXmlSimpleReader isoreader;
 
90
 
 
91
    isoreader.setContentHandler(isoparser);
 
92
    isoreader.parse(isosource);
 
93
 
 
94
    QList<Isotope*> isotopes = isoparser->getIsotopes();
 
95
 
 
96
    //we don't need isoparser anymore, let's free its memory
 
97
    delete isoparser;
 
98
 
 
99
    foreach( Isotope *iso, isotopes )
 
100
    {
 
101
        int num = iso->parentElementNumber();
 
102
        if ( m_isotopes.contains( num ) ) {
 
103
            m_isotopes[num].append( iso );
 
104
        } else {
 
105
            QList<Isotope*> newlist;
 
106
            newlist.append( iso );
 
107
            m_isotopes.insert( num, newlist );
 
108
        }
 
109
    }
 
110
 
 
111
    // cache it
 
112
    m_numOfElements = ElementList.count();
 
113
 
 
114
    qAddPostRoutine( KalziumDataObject::cleanup );
 
115
}
 
116
 
 
117
KalziumDataObject::~KalziumDataObject()
 
118
{
 
119
    //Delete all elements
 
120
    qDeleteAll(ElementList);
 
121
 
 
122
    //Delete all isotopes
 
123
    QHashIterator<int, QList<Isotope*> > i(m_isotopes);
 
124
    while (i.hasNext()) {
 
125
        i.next();
 
126
        qDeleteAll( i.value());
 
127
    }
 
128
}
 
129
 
 
130
Element* KalziumDataObject::element( int number )
 
131
{
 
132
    // checking that we are requesting a valid element
 
133
    if ( ( number <= 0 ) || ( number > m_numOfElements ) )
 
134
        return 0;
 
135
    return ElementList[ number-1 ];
 
136
}
 
137
 
 
138
QString KalziumDataObject::unitAsString( const int unit ) const
 
139
{
 
140
    return KUnitConversion::Converter().unit( unit ).data()->symbol();
 
141
}
 
142
 
 
143
QPixmap KalziumDataObject::pixmap( int number )
 
144
{
 
145
    // checking that we are requesting a valid element
 
146
    if ( ( number <= 0 ) || ( number > m_numOfElements ) )
 
147
        return 0;
 
148
    if ( PixmapList.isEmpty() )
 
149
        loadIconSet();
 
150
    return PixmapList[ number-1 ];
 
151
}
 
152
 
 
153
QList<Isotope*> KalziumDataObject::isotopes( Element * element )
 
154
{
 
155
    return isotopes( element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt() );
 
156
}
 
157
 
 
158
QList<Isotope*> KalziumDataObject::isotopes( int number )
 
159
{
 
160
    return m_isotopes.contains( number ) ? m_isotopes.value( number ) : QList<Isotope*>();
 
161
}
 
162
 
 
163
Spectrum * KalziumDataObject::spectrum( int number )
 
164
{
 
165
    foreach (Spectrum * s, m_spectra ) {
 
166
        if (s->parentElementNumber() == number ) {
 
167
            return s;
 
168
        }
 
169
    }
 
170
 
 
171
    return 0;
 
172
}
 
173
 
 
174
 
 
175
void KalziumDataObject::setSearch( Search *srch )
 
176
{
 
177
    m_search = srch;
 
178
}
 
179
 
 
180
Search* KalziumDataObject::search() const
 
181
{
 
182
    return m_search;
 
183
}
 
184
 
 
185
void KalziumDataObject::cleanup()
 
186
{
 
187
    KalziumDataObject::instance()->cleanPixmaps();
 
188
}
 
189
 
 
190
void KalziumDataObject::loadIconSet()
 
191
{
 
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/";
 
197
 
 
198
    for ( int i = 0 ; i < m_numOfElements ; i++ )
 
199
    {
 
200
        QString filename = pathname + setname + '/' + QString::number( i+1 ) + ".svg";
 
201
 
 
202
        QPixmap pix = cache.loadFromSvg( filename, QSize( 40, 40 ) );
 
203
        if ( pix.isNull() ) {
 
204
            pix = QPixmap( 40, 40 );
 
205
            pix.fill(Qt::transparent);
 
206
 
 
207
            QPainter p( &pix );
 
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 );
 
211
            p.end();
 
212
        }
 
213
        PixmapList << pix;
 
214
    }
 
215
}
 
216
 
 
217
void KalziumDataObject::cleanPixmaps()
 
218
{
 
219
    PixmapList.clear();
 
220
}