Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / molcalcwidget.cpp
blob7a4b2919d1b7a5e931b24b279d919a0be6f5c9f8
1 /***************************************************************************
2 * Copyright (C) 2003-2005, 2006 by Carsten Niehaus, cniehaus@kde.org *
3 * Copyright (C) 2005 by Inge Wallin, inge@lysator.liu.se *
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 ***************************************************************************/
22 #include "molcalcwidget.h"
24 //libscience
25 #include <element.h>
27 #include "kalziumdataobject.h"
28 #include "kalziumutils.h"
29 #include "search.h"
31 #include <kdebug.h>
32 #include <kiconloader.h>
33 #include <klocale.h>
34 #include <kpushbutton.h>
35 #include <klineedit.h>
37 #include <QApplication>
38 #include <QLabel>
39 #include <QLayout>
40 #include <QToolButton>
41 #include <QTimer>
42 #include <QKeyEvent>
44 MolcalcWidget::MolcalcWidget( QWidget *parent )
45 : QWidget( parent )
47 m_parser = new MoleculeParser( KalziumDataObject::instance()->ElementList );
49 m_timer = new QTimer(this);
50 m_timer->setSingleShot( true );
52 ui.setupUi( this );
54 connect( ui.calcButton, SIGNAL( clicked() ), this, SLOT( slotCalculate() ) );
55 connect( ui.formulaEdit, SIGNAL( returnPressed() ), this, SLOT( slotCalculate() ) );
56 connect( m_timer, SIGNAL( timeout() ),
57 this, SLOT( slotCalculate() ) );
59 ui.formulaEdit->setClearButtonShown(true);
61 clear();
65 MolcalcWidget::~MolcalcWidget()
67 delete m_parser;
71 void MolcalcWidget::clear()
73 // Clear the data.
74 m_mass = 0;
75 m_elementMap.clear();
77 //stop the selection in the periodic table
78 KalziumDataObject::instance()->search()->resetSearch();
80 // Clear the widgets.
81 ui.resultLabel->setText( "" );
82 ui.resultMass->setText( "" );
84 ui.resultComposition->setText( i18n("To start, enter\na formula in the\nwidget above and\nclick on 'Calc'.") );
86 ui.resultMass->setToolTip( QString() );
87 ui.resultComposition->setToolTip( QString() );
88 ui.resultLabel->setToolTip( QString() );
92 void MolcalcWidget::updateUI()
94 kDebug() << "MolcalcWidget::updateUI()";
96 if ( m_validInput ){
97 kDebug() << "m_validInput == true";
98 QString str;
100 // The complexString stores the whole molecule like this:
101 // 1 Seaborgium. Cumulative Mass: 263.119 u (39.2564 %)
102 QString complexString;
104 // Create the list of elements making up the molecule
105 foreach (ElementCount * count , m_elementMap.map()) {
106 // Update the resultLabel
107 str += i18nc( "For example: \"1 Carbon\" or \"3 Oxygen\"", "%1 %2\n" ,
108 count->count() ,
109 count->element()->dataAsString( ChemicalDataObject::name) );
111 ui.resultLabel->setText( str );
113 // The composition
114 ui.resultComposition->setText( compositionString(m_elementMap) );
116 // The mass
117 ui.resultMass->setText( i18n( "Molecular mass: %1 u", m_mass ) );
119 ui.resultMass->setToolTip( complexString );
120 ui.resultComposition->setToolTip( complexString );
121 ui.resultLabel->setToolTip( complexString );
123 #if 0
124 // FIXME
125 //select the elements in the table
126 QList<Element*> list = m_elementMap.elements();
127 KalziumDataObject::instance()->findElements( list );
128 #endif
130 else{//the input was invalid, so tell this the user
131 kDebug() << "m_validInput == false";
132 ui.resultComposition->setText( i18n( "Invalid input" ) );
133 ui.resultLabel->setText( QString() );
134 ui.resultMass->setText( QString() );
136 ui.resultMass->setToolTip( i18n( "Invalid input" ) );
137 ui.resultComposition->setToolTip( i18n( "Invalid input" ) );
138 ui.resultLabel->setToolTip( i18n( "Invalid input" ) );
142 QString MolcalcWidget::compositionString( ElementCountMap &_map )
144 QString str;
146 foreach (ElementCount * count, _map.map()) {
147 str += i18n( "%1<sub>%2</sub> " ,
148 count->element()->dataAsString( ChemicalDataObject::symbol ) ,
149 count->count() );
152 return str;
156 // ----------------------------------------------------------------
157 // slots
160 void MolcalcWidget::slotCalculate()
162 kDebug() << "MolcalcWidget::slotCalcButtonClicked()";
163 QString molecule = ui.formulaEdit->text();
165 // Parse the molecule, and at the same time calculate the total
166 // mass, and the composition of it.
167 if ( !molecule.isEmpty() )
168 m_validInput = m_parser->weight(molecule, &m_mass, &m_elementMap);
170 updateUI();
173 void MolcalcWidget::keyPressEvent(QKeyEvent * /* e */)
175 m_timer->start(1000);
180 #include "molcalcwidget.moc"