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
* Copyright (C) 2009 by Kashyap. R. Puranik *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21
***************************************************************************/
23
#include "molcalcwidget.h"
28
#include "kalziumdataobject.h"
29
#include "kalziumutils.h"
34
#include <kiconloader.h>
36
#include <kpushbutton.h>
37
#include <klineedit.h>
38
#include <kstandarddirs.h>
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() ), this, SLOT( slotCalculate() ) );
58
ui.formulaEdit->setClearButtonShown(true);
61
if(!Prefs::addAlias()) {
68
if (Prefs::addAlias())
70
connect( ui.alias, SIGNAL(clicked()), this, SLOT( addAlias()));
71
QString shortForm, fullForm; // short form (symbol) and full form (expansion)
72
QList<QString> shortList, fullList; // Used to store the short and full forms
73
int i = 0; // loop counter
75
// Search in User defined aliases.
76
QString fileName = KStandardDirs::locate( "data", "libkdeedu/data/symbols2.csv");
79
// Check file validity
80
if (!(!file.open(QIODevice::ReadOnly | QIODevice::Text)))
82
kDebug() << fileName << " opened";
83
QTextStream in(&file);
84
// Get all shortForms and fullForms in the file.
85
// eg the short form and full form extracted from ("Me","CH3")
86
// are (Me) and (CH3) respectively
88
QString line = in.readLine();
89
shortForm = line.section(',', 0, 0);
90
shortForm.remove(QChar('\"'));
91
fullForm = line.section(',', 1, 1);
92
fullForm.remove(QChar('\"'));
93
shortList << shortForm;
97
int length = shortList.length();
98
ui.user_defined->setRowCount(length);
99
// Put all the aliases on to the table in the user interface
100
for( i = 0; i < length; i ++) {
101
shortForm = shortList.takeFirst ();
102
fullForm = fullList.takeFirst ();
103
ui.user_defined->setItem((int)i, 0, new QTableWidgetItem
104
(i18n("%1",shortForm + " : " + fullForm)));
112
kDebug() << fileName << " could not be opened!";
115
// Find the system defined aliases
117
fileName = KStandardDirs::locate( "data", "libkdeedu/data/symbols.csv");
118
QFile file2(fileName);
122
// Check file validity
123
if (!(!file2.open(QIODevice::ReadOnly | QIODevice::Text)))
125
kDebug() << fileName << " opened";
126
QTextStream in(&file2);
128
// Get all shortForms and fullForms in the file.
129
while (!in.atEnd()) {
130
QString line = in.readLine();
131
shortForm = line.section(',', 0, 0);
132
shortForm.remove(QChar('\"'));
133
fullForm = line.section(',', 1, 1);
134
fullForm.remove(QChar('\"'));
135
shortList << shortForm;
136
fullList << fullForm;
138
int length = shortList.length();
139
ui.pre_defined->setRowCount(length);
141
// Put all the aliases on to the table in the user interface
142
for( i = 0; i < length; i ++) {
143
shortForm = shortList.takeFirst ();
144
fullForm = fullList.takeFirst ();
145
ui.pre_defined->setItem((int)i, 0, new QTableWidgetItem
146
(i18n("%1",shortForm + " : " + fullForm)));
151
kDebug() << fileName << " could not be opened!";
156
MolcalcWidget::~MolcalcWidget()
162
void MolcalcWidget::clear()
166
m_elementMap.clear();
168
//stop the selection in the periodic table
169
KalziumDataObject::instance()->search()->resetSearch();
171
// Clear the widgets.
172
ui.resultLabel->clear();
173
ui.resultMass->clear();
174
ui.resultValue->hide();
176
ui.resultComposition->setText( i18n("Enter a formula in the\nwidget above and\nclick on 'Calc'.\nE.g. #Et#OH") );
178
ui.resultMass->setToolTip( QString() );
179
ui.resultComposition->setToolTip( QString() );
180
ui.resultLabel->setToolTip( QString() );
184
void MolcalcWidget::updateUI()
186
kDebug() << "MolcalcWidget::updateUI()";
189
kDebug() << "m_validInput == true";
191
// The complexString stores the whole molecule like this:
192
// 1 Seaborgium. Cumulative Mass: 263.119 u (39.2564 %)
193
QString complexString;
198
int i = 0; // counter
199
int rows = m_elementMap.elements().count(); // number of columns
200
ui.table->setRowCount(rows);
202
foreach (ElementCount * count , m_elementMap.map()) {
203
// Update the resultLabel
204
mass = count->element()->dataAsVariant( ChemicalDataObject::mass ).toDouble();
206
ui.table->setItem((int)i, 0, new QTableWidgetItem
207
(i18n("%1", count->element()->dataAsString( ChemicalDataObject::name))));
208
ui.table->setItem((int)i, 1, new QTableWidgetItem
209
(i18n("%1", count->count())));
210
ui.table->setItem((int)i, 2, new QTableWidgetItem
211
(i18n("%1", count->element()->dataAsString( ChemicalDataObject::mass))));
212
ui.table->setItem((int)i, 3, new QTableWidgetItem
213
(i18n("%1", mass * count->count())));
214
ui.table->setItem((int)i, 4, new QTableWidgetItem
215
(i18n("%1", mass * count->count()/ m_mass *100)));
221
rows = m_aliasList.count();
222
ui.alias_list->setRowCount(rows);
223
foreach (QString alias, m_aliasList) {
224
ui.alias_list->setItem((int)i++, 0, new QTableWidgetItem(alias));
229
ui.resultComposition->setText( compositionString(m_elementMap) );
232
ui.resultMass->setText( i18n( "Molecular mass: ") );
234
ui.resultValue->setText( QString::number(m_mass) + " u" );
235
ui.resultValue->show();
236
ui.resultMass->setToolTip( complexString );
237
ui.resultComposition->setToolTip( complexString );
241
//select the elements in the table
242
QList<Element*> list = m_elementMap.elements();
243
KalziumDataObject::instance()->findElements( list );
246
else{//the input was invalid, so tell this the user
247
kDebug() << "m_validInput == false";
248
ui.resultComposition->setText( i18n( "Invalid input" ) );
249
ui.resultLabel->setText( QString() );
250
ui.resultMass->setText( QString() );
252
ui.resultMass->setToolTip( i18n( "Invalid input" ) );
253
ui.resultComposition->setToolTip( i18n( "Invalid input" ) );
254
ui.resultLabel->setToolTip( i18n( "Invalid input" ) );
258
QString MolcalcWidget::compositionString( ElementCountMap &_map )
262
foreach (ElementCount * count, _map.map()) {
263
str += i18n( "%1<sub>%2</sub> " ,
264
count->element()->dataAsString( ChemicalDataObject::symbol ) ,
272
// ----------------------------------------------------------------
276
void MolcalcWidget::slotCalculate()
278
kDebug() << "MolcalcWidget::slotCalcButtonClicked()";
279
QString molecule = ui.formulaEdit->text();
281
// Parse the molecule, and at the same time calculate the total
282
// mass, and the composition of it.
283
if ( !molecule.isEmpty() )
285
m_validInput = m_parser->weight(molecule, &m_mass, &m_elementMap);
286
m_aliasList = m_parser->aliasList();
288
kDebug() << "done calculating.";
293
void MolcalcWidget::keyPressEvent(QKeyEvent * /* e */)
298
void MolcalcWidget::addAlias()
300
QString shortForm = ui.shortForm->text();
301
QString fullForm = ui.fullForm ->text();
303
// Validate the alias
307
ui.aliasMessage->setText("");
308
if ( shortForm.length() < 2)
310
ui.aliasMessage->setText(i18n
311
("Symbol should consist of two or more letters."));
315
if ( m_parser->weight(shortForm, & x , & y))
317
ui.aliasMessage->setText(i18n
318
("Symbol already being used"));
322
if (fullForm.isEmpty() || ! m_parser->weight(fullForm, & x, & y))
324
ui.aliasMessage->setText(i18n
325
("Expansion is invalid, please specify a valid expansion"));
329
// Open the file to write
330
QString fileName = KStandardDirs::locate( "data", "libkdeedu/data/symbols2.csv");
331
QFile file(fileName);
333
if (!(!file.open(QIODevice::WriteOnly| QIODevice::Append | QIODevice::Text)))
335
QTextStream out(&file);
336
out << "\"" + shortForm + "\",\"" + fullForm + "\"\n";
337
kDebug() << fileName << "is the file.";
338
kDebug() << "\"" + shortForm + "\",\"" + fullForm + "\"\n";
339
ui.aliasMessage->setText(i18n("done!"));
344
ui.aliasMessage->setText((i18n
345
("Unable to find the user defined alias file."))+fileName);
350
void MolcalcWidget::hideExtra()
353
ui.tabWidget->removeTab(1);
355
#include "molcalcwidget.moc"