summaryrefslogtreecommitdiffstats
path: root/kalzium/src/molcalcwidget.cpp
blob: 10ad8246c3599c32e911bd957dbb2f93eb52ed25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/***************************************************************************
 *   Copyright (C) 2003-2005 by Carsten Niehaus, cniehaus@kde.org          *
 *   Copyright (C) 2005      by Inge Wallin,     inge@lysator.liu.se       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.          *
 *                                                                         *
 ***************************************************************************/


#include "molcalcwidget.h"

#include "kalziumdataobject.h"
#include "molcalcwidgetbase.h"
#include "element.h"
#include "kalziumutils.h"
#include "parser.h"
#include "moleculeparser.h"

#include <kaction.h>
#include <kdebug.h>
#include <klocale.h>
#include <kpushbutton.h>
#include <klineedit.h>
#include <ktoolbar.h>

#include <tqlabel.h>
#include <layout.h>
#include <tqtooltip.h>


MolcalcWidget::MolcalcWidget( TQWidget *parent, const char *name )
    : MolcalcWidgetBase( parent, name )
{
	clear();
}


void MolcalcWidget::clear()
{
	// Clear the data.
	m_mass = 0;
	m_elementMap.clear();

	// Clear the widgets.
	resultLabel->setText( "" );
	resultMass->setText( "" );
	
	resultComposition->setText( i18n("To start, enter\na formula in the\nwidget above and\nclick on 'Calc'.") );

	TQToolTip::remove( resultMass );
	TQToolTip::remove( resultComposition );
	TQToolTip::remove( resultLabel );
}


void MolcalcWidget::updateUI()
{
	if ( m_validInput ){

		TQString str;

		// The complexString stores the whole molecule like this:
		// 1 Seaborgium. Cumulative Mass: 263.119 u (39.2564 %)
		TQString complexString;

		// Create the list of elements making up the molecule
		ElementCountMap::Iterator  it    = m_elementMap.begin();
		ElementCountMap::Iterator  itEnd = m_elementMap.end();
		for ( ; it != itEnd; ++it ) {
			// Update the resultLabel
			str += i18n( "For example: \"1 Carbon\" or \"3 Oxygen\"", "%1 %2\n" )
				.arg( (*it)->count() )
				.arg( (*it)->element()->elname() );

			complexString
				+= i18n( "For example: 1 Seaborgium. Cumulative Mass: 263.119 u (39.25%)",
						"%1 %2. Cumulative Mass: %3 u (%4%)\n" )
				.arg( (*it)->count() )
				.arg( (*it)->element()->elname() )
				.arg( (*it)->count() * (*it)->element()->mass() )
				.arg( KalziumUtils::strippedValue( (( (*it)->count() * (*it)->element()->mass() )
								/ m_mass ) * 100 ) );
		}
		resultLabel->setText( str );

		// The composition
		resultComposition->setText( compositionString(m_elementMap) );

		// The mass
		resultMass->setText( i18n( "Molecular mass: %1 u" ).arg( m_mass ) );

		TQToolTip::add( resultMass,        complexString );
		TQToolTip::add( resultComposition, complexString );
		TQToolTip::add( resultLabel,       complexString );
	}
	else{//the input was invalid, so tell this the user
		resultComposition->setText( i18n( "Invalid input" ) );
		resultLabel->setText( TQString() );
		resultMass->setText( TQString() );
		
		TQToolTip::add( resultMass,        i18n( "Invalid input" ) );
		TQToolTip::add( resultComposition, i18n( "Invalid input" ) );
		TQToolTip::add( resultLabel,       i18n( "Invalid input" ) );
	}
}


TQString MolcalcWidget::compositionString( ElementCountMap &_map )
{
	TQString  str;

	ElementCountMap::Iterator  it    = _map.begin();
	ElementCountMap::Iterator  itEnd = _map.end();
	for (; it != itEnd; ++it) {
		str += i18n( "%1<sub>%2</sub> " )
			.arg( (*it)->element()->symbol() )
			.arg( (*it)->count() );
	}

	return str;
}


// ----------------------------------------------------------------
//                            slots


void MolcalcWidget::slotCalcButtonClicked()
{
	TQString  molecule = formulaEdit->text();

	// Parse the molecule, and at the same time calculate the total
	// mass, and the composition of it.
	m_validInput = m_parser.weight(molecule, &m_mass, &m_elementMap);
	
	updateUI();
}


#include "molcalcwidget.moc"