moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kalzium / src / molcalc_impl.cpp
blob0a0d9465a817eb48e28721acd68f1dde2e2e1612
1 /***************************************************************************
2 * Copyright (C) 2003, 2004 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include "molcalc_impl.h"
22 #include <kdebug.h>
23 #include <klocale.h>
24 #include <kpushbutton.h>
26 #include "element.h"
28 #include <qlabel.h>
29 #include <qmap.h>
31 MolcalcImpl::MolcalcImpl(QWidget *parent, const char *name, bool modal )
32 : MolcalcDialog(parent, name, modal)
34 m_weight = 0;
36 connect( plusButton, SIGNAL( toggled(bool) ), this, SLOT( slotPlusToggled(bool) ) );
37 connect( minusButton, SIGNAL( toggled(bool) ), this, SLOT( slotMinusToggled(bool) ) );
40 void MolcalcImpl::slotButtonClicked( int buttonnumber )
42 //kdDebug() << "slotButtonClicked( int buttonnumber ) ... " << buttonnumber << endl;
44 if ( plusButton->isOn() )
45 updateData( buttonnumber, ADD );
46 else
47 updateData( buttonnumber, REMOVE );
50 void MolcalcImpl::updateData( int number, KIND kind )
52 //TODO XML
53 //X Element *el = new Element( number );
54 //X if ( kind == ADD )
55 //X {
56 //X //kdDebug() << "ADD" << endl;
57 //X m_weight += el->weight();
58 //X m_elements.append( el );
59 //X }
60 //X else //TODO check if the element was in the list
61 //X {
62 //X //kdDebug() << "REMOVE" << endl;
63 //X
64 //X QValueList<Element*>::const_iterator it = m_elements.begin( );
65 //X QValueList<Element*>::const_iterator itEnd = m_elements.end( );
66 //X
67 //X //kdDebug() << "Try to remove Element " << el->elname() << endl;
68 //X
69 //X bool removed = false;
70 //X while ( !removed && ( it != itEnd ))
71 //X {
72 //X if ( ( *it )->elname() == el->elname() )
73 //X {
74 //X m_elements.remove( *it );
75 //X removed = true;
76 //X }
77 //X it++;
78 //X }
79 //X }
80 //X updateUI();
83 void MolcalcImpl::slotPlusToggled(bool on)
85 on ? minusButton->setOn( false ) : minusButton->setOn( true );
88 void MolcalcImpl::recalculate()
90 //kdDebug() << "# of elements: " << m_elements.count() << endl;
91 QValueList<Element*>::const_iterator it = m_elements.begin( );
92 const QValueList<Element*>::const_iterator itEnd = m_elements.end( );
94 m_weight = 0.0;
96 for ( ; it != itEnd ; ++it )
98 m_weight += ( *it )->weight();
102 void MolcalcImpl::updateUI()
104 QString str;
106 //the elements
107 QMap<Element*, int> map;
109 QValueList<Element*>::const_iterator it = m_elements.begin( );
110 const QValueList<Element*>::const_iterator itEnd = m_elements.end( );
112 QValueList<Element*> differentElements;
113 QValueList<Element*>::const_iterator itNames;
115 for ( ; it != itEnd ; ++it )
116 {//get the different elements in the molecule
117 bool contains = false;
118 for ( itNames = differentElements.begin(); itNames != differentElements.end() ; ++itNames )
120 if ( ( *it )->elname() == ( *itNames )->elname() )
121 contains = true;
123 if ( !contains )
124 differentElements.append( *it );
127 itNames = differentElements.begin( );
129 for ( ; itNames != differentElements.end() ; ++itNames )
130 {//count the different elements (write the result in a QMap)
131 it = m_elements.begin( );
133 int count = 0;
134 for ( ; it != itEnd ; ++it )
136 if ( ( *it )->elname() == ( *itNames )->elname() )
137 count++;
139 map[ *itNames ] = count;
142 QMap<Element*, int>::Iterator itMap;
143 for ( itMap = map.begin(); itMap != map.end(); ++itMap )
144 {//update the resultLabel
145 str += i18n( "%1 %2. Cumulative Weight: %3 u (%4 %)\n" ).arg( itMap.data() ).arg( i18n( itMap.key()->elname().utf8() ) ).arg( itMap.data() * itMap.key()->weight() ).arg((( itMap.data() * itMap.key()->weight() )/m_weight )*100);
148 resultLabel->setText( str );
150 //the composition
151 resultComposition->setText( composition( map ) );
153 //the weight
154 recalculate();
155 resultWeight->setText( i18n( "Molecular Weight: %1u" ).arg( m_weight ) );
158 QString MolcalcImpl::composition( QMap<Element*,int> map )
160 QString str;
162 QMap<Element*, int>::Iterator itMap;
163 for ( itMap = map.begin(); itMap != map.end(); ++itMap )
165 str += i18n( "%1<sub>%2</sub>" ).arg( itMap.key()->symbol() ).arg( itMap.data() );
168 return str;
171 void MolcalcImpl::slotMinusToggled(bool on)
173 on ? plusButton->setOn( false ) : plusButton->setOn( true );
176 #include "molcalc_impl.moc"