1 /***************************************************************************
2 * Copyright (C) 2003-2005, 2006 by Carsten Niehaus, cniehaus@kde.org *
3 * Copyright (C) 2005 by Inge Wallin, inge@lysator.liu.se *
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. *
20 ***************************************************************************/
22 #include "molcalcwidget.h"
27 #include "kalziumdataobject.h"
28 #include "kalziumutils.h"
32 #include <kiconloader.h>
34 #include <kpushbutton.h>
35 #include <klineedit.h>
37 #include <QApplication>
40 #include <QToolButton>
44 MolcalcWidget::MolcalcWidget( QWidget
*parent
)
47 m_parser
= new MoleculeParser( KalziumDataObject::instance()->ElementList
);
49 m_timer
= new QTimer(this);
50 m_timer
->setSingleShot( true );
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);
65 MolcalcWidget::~MolcalcWidget()
71 void MolcalcWidget::clear()
77 //stop the selection in the periodic table
78 KalziumDataObject::instance()->search()->resetSearch();
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()";
97 kDebug() << "m_validInput == true";
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" ,
109 count
->element()->dataAsString( ChemicalDataObject::name
) );
111 ui
.resultLabel
->setText( str
);
114 ui
.resultComposition
->setText( compositionString(m_elementMap
) );
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
);
125 //select the elements in the table
126 QList
<Element
*> list
= m_elementMap
.elements();
127 KalziumDataObject::instance()->findElements( list
);
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
)
146 foreach (ElementCount
* count
, _map
.map()) {
147 str
+= i18n( "%1<sub>%2</sub> " ,
148 count
->element()->dataAsString( ChemicalDataObject::symbol
) ,
156 // ----------------------------------------------------------------
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
);
173 void MolcalcWidget::keyPressEvent(QKeyEvent
* /* e */)
175 m_timer
->start(1000);
180 #include "molcalcwidget.moc"