~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to src/molcalcwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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                        *
 
5
 *                                                                         *
 
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.                                   *
 
10
 *                                                                         *
 
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.                          *
 
15
 *                                                                         *
 
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.         *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
#include "molcalcwidget.h"
 
24
 
 
25
//libscience
 
26
#include <element.h>
 
27
 
 
28
#include "kalziumdataobject.h"
 
29
#include "kalziumutils.h"
 
30
#include "search.h"
 
31
#include "prefs.h"
 
32
 
 
33
#include <kdebug.h>
 
34
#include <kiconloader.h>
 
35
#include <klocale.h>
 
36
#include <kpushbutton.h>
 
37
#include <klineedit.h>
 
38
#include <kstandarddirs.h>
 
39
 
 
40
#include <QTimer>
 
41
#include <QKeyEvent>
 
42
#include <QFile>
 
43
 
 
44
MolcalcWidget::MolcalcWidget( QWidget *parent )
 
45
    : QWidget( parent )
 
46
{
 
47
    m_parser = new MoleculeParser( KalziumDataObject::instance()->ElementList );
 
48
 
 
49
    m_timer = new QTimer(this);
 
50
    m_timer->setSingleShot( true );
 
51
        
 
52
    ui.setupUi( this );
 
53
        
 
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() ) );
 
57
 
 
58
    ui.formulaEdit->setClearButtonShown(true);
 
59
 
 
60
    clear();
 
61
        if(!Prefs::addAlias()) {
 
62
                hideExtra();
 
63
                ui.details->show();
 
64
        }
 
65
        if(!Prefs::alias())
 
66
                ui.details->hide();
 
67
 
 
68
        if (Prefs::addAlias())
 
69
        {
 
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
 
74
                
 
75
                // Search in User defined aliases.
 
76
                QString fileName = KStandardDirs::locate( "data", "libkdeedu/data/symbols2.csv");
 
77
                QFile file(fileName);    
 
78
 
 
79
                // Check file validity
 
80
                if (!(!file.open(QIODevice::ReadOnly | QIODevice::Text)))
 
81
                {
 
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
 
87
                    while (!in.atEnd()) {
 
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;
 
94
                                fullList << fullForm;
 
95
                    }
 
96
                    
 
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)));
 
105
                        }
 
106
                        
 
107
                                
 
108
                        
 
109
                }
 
110
                else
 
111
                {
 
112
                    kDebug() << fileName << " could not be opened!";
 
113
                }
 
114
 
 
115
                // Find the system defined aliases
 
116
                // Open the file
 
117
                fileName = KStandardDirs::locate( "data", "libkdeedu/data/symbols.csv");
 
118
                QFile file2(fileName);
 
119
                shortList.clear();
 
120
                fullList.clear();
 
121
 
 
122
                    // Check file validity
 
123
                if (!(!file2.open(QIODevice::ReadOnly | QIODevice::Text)))
 
124
                {
 
125
                        kDebug() << fileName << " opened";
 
126
                    QTextStream in(&file2);
 
127
 
 
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;
 
137
                    }
 
138
                    int length = shortList.length();
 
139
                    ui.pre_defined->setRowCount(length);
 
140
                    
 
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)));
 
147
                        }
 
148
                }
 
149
                else
 
150
                {
 
151
                    kDebug() << fileName << " could not be opened!";
 
152
                }
 
153
        }
 
154
}
 
155
 
 
156
MolcalcWidget::~MolcalcWidget()
 
157
{
 
158
        delete m_parser;
 
159
}
 
160
 
 
161
 
 
162
void MolcalcWidget::clear()
 
163
{
 
164
        // Clear the data.
 
165
        m_mass = 0;
 
166
        m_elementMap.clear();
 
167
 
 
168
        //stop the selection in the periodic table
 
169
        KalziumDataObject::instance()->search()->resetSearch();
 
170
 
 
171
        // Clear the widgets.
 
172
        ui.resultLabel->clear();
 
173
        ui.resultMass->clear();
 
174
        ui.resultValue->hide();
 
175
        
 
176
        ui.resultComposition->setText( i18n("Enter a formula in the\nwidget above and\nclick on 'Calc'.\nE.g. #Et#OH") );
 
177
 
 
178
        ui.resultMass->setToolTip( QString() );
 
179
        ui.resultComposition->setToolTip( QString() );
 
180
        ui.resultLabel->setToolTip( QString() );
 
181
}
 
182
 
 
183
 
 
184
void MolcalcWidget::updateUI()
 
185
{
 
186
    kDebug() << "MolcalcWidget::updateUI()";
 
187
 
 
188
    if ( m_validInput ){
 
189
        kDebug() << "m_validInput == true";
 
190
 
 
191
        // The complexString stores the whole molecule like this:
 
192
        // 1 Seaborgium. Cumulative Mass: 263.119 u (39.2564 %)
 
193
        QString complexString;
 
194
        if (Prefs::alias())
 
195
            {
 
196
                    double mass;
 
197
                    QString str;
 
198
                    int i = 0;                                          // counter
 
199
                    int rows = m_elementMap.elements().count();         // number of columns
 
200
                    ui.table->setRowCount(rows);
 
201
                
 
202
                    foreach (ElementCount * count , m_elementMap.map()) {
 
203
                        // Update the resultLabel
 
204
                        mass = count->element()->dataAsVariant( ChemicalDataObject::mass ).toDouble();
 
205
 
 
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)));
 
216
 
 
217
                        i++;
 
218
                    }
 
219
                    // The alias list
 
220
                    i = 0;
 
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));
 
225
                }
 
226
                }
 
227
                
 
228
                // The composition
 
229
        ui.resultComposition->setText( compositionString(m_elementMap) );
 
230
 
 
231
        // The mass
 
232
        ui.resultMass->setText( i18n( "Molecular mass: ") );
 
233
        
 
234
        ui.resultValue->setText( QString::number(m_mass) + " u" ); 
 
235
            ui.resultValue->show();
 
236
        ui.resultMass->setToolTip(  complexString );
 
237
        ui.resultComposition->setToolTip( complexString );
 
238
 
 
239
#if 0
 
240
        // FIXME
 
241
        //select the elements in the table
 
242
        QList<Element*> list = m_elementMap.elements();
 
243
        KalziumDataObject::instance()->findElements( list );
 
244
#endif
 
245
    }
 
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() );
 
251
 
 
252
        ui.resultMass->setToolTip(        i18n( "Invalid input" ) );
 
253
        ui.resultComposition->setToolTip( i18n( "Invalid input" ) );
 
254
        ui.resultLabel->setToolTip(       i18n( "Invalid input" ) );
 
255
    }
 
256
}
 
257
 
 
258
QString MolcalcWidget::compositionString( ElementCountMap &_map )
 
259
{
 
260
        QString  str;
 
261
 
 
262
  foreach (ElementCount * count, _map.map()) {
 
263
      str += i18n( "%1<sub>%2</sub> " ,
 
264
              count->element()->dataAsString( ChemicalDataObject::symbol ) ,
 
265
              count->count() );
 
266
  }
 
267
 
 
268
        return str;
 
269
}
 
270
 
 
271
 
 
272
// ----------------------------------------------------------------
 
273
//                            slots
 
274
 
 
275
 
 
276
void MolcalcWidget::slotCalculate()
 
277
{
 
278
    kDebug() << "MolcalcWidget::slotCalcButtonClicked()";
 
279
        QString  molecule = ui.formulaEdit->text();
 
280
 
 
281
        // Parse the molecule, and at the same time calculate the total
 
282
        // mass, and the composition of it.
 
283
        if ( !molecule.isEmpty() )
 
284
        {
 
285
            m_validInput = m_parser->weight(molecule, &m_mass, &m_elementMap);
 
286
            m_aliasList = m_parser->aliasList();
 
287
        }
 
288
        kDebug() << "done calculating.";
 
289
 
 
290
        updateUI();
 
291
}
 
292
 
 
293
void MolcalcWidget::keyPressEvent(QKeyEvent * /* e */)
 
294
{
 
295
    m_timer->start(500);
 
296
}
 
297
 
 
298
void MolcalcWidget::addAlias()
 
299
{
 
300
        QString shortForm = ui.shortForm->text();
 
301
        QString fullForm  = ui.fullForm ->text();
 
302
        
 
303
    // Validate the alias
 
304
        double x;
 
305
        ElementCountMap y;
 
306
        
 
307
        ui.aliasMessage->setText("");
 
308
        if ( shortForm.length() < 2)
 
309
        {
 
310
                ui.aliasMessage->setText(i18n
 
311
                ("Symbol should consist of two or more letters."));
 
312
                return;
 
313
        }
 
314
        
 
315
        if ( m_parser->weight(shortForm, & x , & y))
 
316
        {
 
317
                ui.aliasMessage->setText(i18n
 
318
                ("Symbol already being used"));
 
319
                return;
 
320
        }
 
321
        
 
322
        if (fullForm.isEmpty() || ! m_parser->weight(fullForm, & x, & y))
 
323
        {
 
324
                ui.aliasMessage->setText(i18n
 
325
                ("Expansion is invalid, please specify a valid expansion"));
 
326
                return;
 
327
        }
 
328
        
 
329
        // Open the file to write
 
330
        QString fileName = KStandardDirs::locate( "data", "libkdeedu/data/symbols2.csv");
 
331
        QFile file(fileName);
 
332
        
 
333
        if (!(!file.open(QIODevice::WriteOnly| QIODevice::Append | QIODevice::Text)))
 
334
    {
 
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!"));
 
340
                return;
 
341
    }
 
342
    else
 
343
    {
 
344
        ui.aliasMessage->setText((i18n
 
345
                ("Unable to find the user defined alias file."))+fileName);
 
346
                return;
 
347
    }
 
348
}
 
349
 
 
350
void MolcalcWidget::hideExtra()
 
351
{
 
352
        ui.details->hide();
 
353
        ui.tabWidget->removeTab(1);
 
354
}
 
355
#include "molcalcwidget.moc"