1 /***************************************************************************
2 * Copyright (C) 2003, 2004 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include "molcalc_impl.h"
24 #include <kpushbutton.h>
31 MolcalcImpl::MolcalcImpl(QWidget
*parent
, const char *name
, bool modal
)
32 : MolcalcDialog(parent
, name
, modal
)
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
);
47 updateData( buttonnumber
, REMOVE
);
50 void MolcalcImpl::updateData( int number
, KIND kind
)
53 //X Element *el = new Element( number );
54 //X if ( kind == ADD )
56 //X //kdDebug() << "ADD" << endl;
57 //X m_weight += el->weight();
58 //X m_elements.append( el );
60 //X else //TODO check if the element was in the list
62 //X //kdDebug() << "REMOVE" << endl;
64 //X QValueList<Element*>::const_iterator it = m_elements.begin( );
65 //X QValueList<Element*>::const_iterator itEnd = m_elements.end( );
67 //X //kdDebug() << "Try to remove Element " << el->elname() << endl;
69 //X bool removed = false;
70 //X while ( !removed && ( it != itEnd ))
72 //X if ( ( *it )->elname() == el->elname() )
74 //X m_elements.remove( *it );
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( );
96 for ( ; it
!= itEnd
; ++it
)
98 m_weight
+= ( *it
)->weight();
102 void MolcalcImpl::updateUI()
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() )
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( );
134 for ( ; it
!= itEnd
; ++it
)
136 if ( ( *it
)->elname() == ( *itNames
)->elname() )
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
);
151 resultComposition
->setText( composition( map
) );
155 resultWeight
->setText( i18n( "Molecular Weight: %1u" ).arg( m_weight
) );
158 QString
MolcalcImpl::composition( QMap
<Element
*,int> map
)
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() );
171 void MolcalcImpl::slotMinusToggled(bool on
)
173 on
? plusButton
->setOn( false ) : plusButton
->setOn( true );
176 #include "molcalc_impl.moc"