-rw-r--r-- | noncore/apps/oxygen/calcdlg.ui | 203 | ||||
-rw-r--r-- | noncore/apps/oxygen/calcdlgui.cpp | 2 | ||||
-rw-r--r-- | noncore/apps/oxygen/kmolcalc.cpp | 40 | ||||
-rw-r--r-- | noncore/apps/oxygen/kmolcalc.h | 5 |
4 files changed, 99 insertions, 151 deletions
diff --git a/noncore/apps/oxygen/calcdlg.ui b/noncore/apps/oxygen/calcdlg.ui index e0b19f9..5b0c53e 100644 --- a/noncore/apps/oxygen/calcdlg.ui +++ b/noncore/apps/oxygen/calcdlg.ui | |||
@@ -1,243 +1,230 @@ | |||
1 | <!DOCTYPE UI><UI> | 1 | <!DOCTYPE UI><UI> |
2 | <class>CalcDlg</class> | 2 | <class>CalcDlg</class> |
3 | <widget> | 3 | <widget> |
4 | <class>QWidget</class> | 4 | <class>QWidget</class> |
5 | <property stdset="1"> | 5 | <property stdset="1"> |
6 | <name>name</name> | 6 | <name>name</name> |
7 | <cstring>CalcDlg</cstring> | 7 | <cstring>CalcDlg</cstring> |
8 | </property> | 8 | </property> |
9 | <property stdset="1"> | 9 | <property stdset="1"> |
10 | <name>geometry</name> | 10 | <name>geometry</name> |
11 | <rect> | 11 | <rect> |
12 | <x>0</x> | 12 | <x>0</x> |
13 | <y>0</y> | 13 | <y>0</y> |
14 | <width>218</width> | 14 | <width>275</width> |
15 | <height>274</height> | 15 | <height>357</height> |
16 | </rect> | 16 | </rect> |
17 | </property> | 17 | </property> |
18 | <property stdset="1"> | 18 | <property stdset="1"> |
19 | <name>caption</name> | 19 | <name>caption</name> |
20 | <string>Network Time</string> | 20 | <string>Network Time</string> |
21 | </property> | 21 | </property> |
22 | <property> | 22 | <property> |
23 | <name>layoutMargin</name> | 23 | <name>layoutMargin</name> |
24 | </property> | 24 | </property> |
25 | <property> | 25 | <property> |
26 | <name>layoutSpacing</name> | 26 | <name>layoutSpacing</name> |
27 | </property> | 27 | </property> |
28 | <widget> | 28 | <widget> |
29 | <class>QTabWidget</class> | 29 | <class>QTabWidget</class> |
30 | <property stdset="1"> | 30 | <property stdset="1"> |
31 | <name>name</name> | 31 | <name>name</name> |
32 | <cstring>TabWidgetMain</cstring> | 32 | <cstring>TabWidgetMain</cstring> |
33 | </property> | 33 | </property> |
34 | <property stdset="1"> | 34 | <property stdset="1"> |
35 | <name>geometry</name> | 35 | <name>geometry</name> |
36 | <rect> | 36 | <rect> |
37 | <x>10</x> | 37 | <x>0</x> |
38 | <y>0</y> | 38 | <y>0</y> |
39 | <width>200</width> | 39 | <width>270</width> |
40 | <height>260</height> | 40 | <height>350</height> |
41 | </rect> | 41 | </rect> |
42 | </property> | 42 | </property> |
43 | <property> | 43 | <property> |
44 | <name>layoutMargin</name> | 44 | <name>layoutMargin</name> |
45 | </property> | 45 | </property> |
46 | <property> | 46 | <property> |
47 | <name>layoutSpacing</name> | 47 | <name>layoutSpacing</name> |
48 | </property> | 48 | </property> |
49 | <widget> | 49 | <widget> |
50 | <class>QWidget</class> | 50 | <class>QWidget</class> |
51 | <property stdset="1"> | 51 | <property stdset="1"> |
52 | <name>name</name> | 52 | <name>name</name> |
53 | <cstring>tabMain</cstring> | 53 | <cstring>tabMain</cstring> |
54 | </property> | 54 | </property> |
55 | <attribute> | 55 | <attribute> |
56 | <name>title</name> | 56 | <name>title</name> |
57 | <string>Molweight</string> | 57 | <string>Molweight</string> |
58 | </attribute> | 58 | </attribute> |
59 | <widget> | 59 | <widget> |
60 | <class>QFrame</class> | 60 | <class>QFrame</class> |
61 | <property stdset="1"> | 61 | <property stdset="1"> |
62 | <name>name</name> | 62 | <name>name</name> |
63 | <cstring>FrameSystemTime</cstring> | 63 | <cstring>FrameSystemTime</cstring> |
64 | </property> | 64 | </property> |
65 | <property stdset="1"> | 65 | <property stdset="1"> |
66 | <name>geometry</name> | 66 | <name>geometry</name> |
67 | <rect> | 67 | <rect> |
68 | <x>2</x> | 68 | <x>0</x> |
69 | <y>2</y> | 69 | <y>0</y> |
70 | <width>180</width> | 70 | <width>260</width> |
71 | <height>220</height> | 71 | <height>320</height> |
72 | </rect> | 72 | </rect> |
73 | </property> | 73 | </property> |
74 | <property stdset="1"> | 74 | <property stdset="1"> |
75 | <name>frameShape</name> | 75 | <name>frameShape</name> |
76 | <enum>StyledPanel</enum> | 76 | <enum>StyledPanel</enum> |
77 | </property> | 77 | </property> |
78 | <property stdset="1"> | 78 | <property stdset="1"> |
79 | <name>frameShadow</name> | 79 | <name>frameShadow</name> |
80 | <enum>Raised</enum> | 80 | <enum>Raised</enum> |
81 | </property> | 81 | </property> |
82 | <widget> | 82 | <widget> |
83 | <class>QLabel</class> | ||
84 | <property stdset="1"> | ||
85 | <name>name</name> | ||
86 | <cstring>TextLabel1_4</cstring> | ||
87 | </property> | ||
88 | <property stdset="1"> | ||
89 | <name>geometry</name> | ||
90 | <rect> | ||
91 | <x>1</x> | ||
92 | <y>1</y> | ||
93 | <width>298</width> | ||
94 | <height>16</height> | ||
95 | </rect> | ||
96 | </property> | ||
97 | <property stdset="1"> | ||
98 | <name>text</name> | ||
99 | <string>Formula:</string> | ||
100 | </property> | ||
101 | </widget> | ||
102 | <widget> | ||
103 | <class>QLayoutWidget</class> | 83 | <class>QLayoutWidget</class> |
104 | <property stdset="1"> | 84 | <property stdset="1"> |
105 | <name>name</name> | 85 | <name>name</name> |
106 | <cstring>Layout7</cstring> | 86 | <cstring>Layout7</cstring> |
107 | </property> | 87 | </property> |
108 | <property stdset="1"> | 88 | <property stdset="1"> |
109 | <name>geometry</name> | 89 | <name>geometry</name> |
110 | <rect> | 90 | <rect> |
111 | <x>0</x> | 91 | <x>0</x> |
112 | <y>50</y> | 92 | <y>0</y> |
113 | <width>175</width> | 93 | <width>260</width> |
114 | <height>30</height> | 94 | <height>320</height> |
115 | </rect> | 95 | </rect> |
116 | </property> | 96 | </property> |
117 | <hbox> | 97 | <vbox> |
118 | <property stdset="1"> | 98 | <property stdset="1"> |
119 | <name>margin</name> | 99 | <name>margin</name> |
120 | <number>0</number> | 100 | <number>0</number> |
121 | </property> | 101 | </property> |
122 | <property stdset="1"> | 102 | <property stdset="1"> |
123 | <name>spacing</name> | 103 | <name>spacing</name> |
124 | <number>6</number> | 104 | <number>6</number> |
125 | </property> | 105 | </property> |
126 | <widget> | 106 | <widget> |
127 | <class>QPushButton</class> | 107 | <class>QLabel</class> |
128 | <property stdset="1"> | 108 | <property stdset="1"> |
129 | <name>name</name> | 109 | <name>name</name> |
130 | <cstring>calculate</cstring> | 110 | <cstring>TextLabel1_4</cstring> |
131 | </property> | 111 | </property> |
132 | <property stdset="1"> | 112 | <property stdset="1"> |
133 | <name>text</name> | 113 | <name>text</name> |
134 | <string>Calculate</string> | 114 | <string>Formula:</string> |
115 | </property> | ||
116 | </widget> | ||
117 | <widget> | ||
118 | <class>QLineEdit</class> | ||
119 | <property stdset="1"> | ||
120 | <name>name</name> | ||
121 | <cstring>formula</cstring> | ||
122 | </property> | ||
123 | </widget> | ||
124 | <widget> | ||
125 | <class>QLayoutWidget</class> | ||
126 | <property stdset="1"> | ||
127 | <name>name</name> | ||
128 | <cstring>Layout7</cstring> | ||
135 | </property> | 129 | </property> |
130 | <hbox> | ||
131 | <property stdset="1"> | ||
132 | <name>margin</name> | ||
133 | <number>0</number> | ||
134 | </property> | ||
135 | <property stdset="1"> | ||
136 | <name>spacing</name> | ||
137 | <number>6</number> | ||
138 | </property> | ||
139 | <widget> | ||
140 | <class>QPushButton</class> | ||
141 | <property stdset="1"> | ||
142 | <name>name</name> | ||
143 | <cstring>calculate</cstring> | ||
144 | </property> | ||
145 | <property stdset="1"> | ||
146 | <name>text</name> | ||
147 | <string>Calculate</string> | ||
148 | </property> | ||
149 | </widget> | ||
150 | <widget> | ||
151 | <class>QPushButton</class> | ||
152 | <property stdset="1"> | ||
153 | <name>name</name> | ||
154 | <cstring>clear_fields</cstring> | ||
155 | </property> | ||
156 | <property stdset="1"> | ||
157 | <name>text</name> | ||
158 | <string>Clear</string> | ||
159 | </property> | ||
160 | </widget> | ||
161 | </hbox> | ||
136 | </widget> | 162 | </widget> |
137 | <widget> | 163 | <widget> |
138 | <class>QPushButton</class> | 164 | <class>QLabel</class> |
139 | <property stdset="1"> | 165 | <property stdset="1"> |
140 | <name>name</name> | 166 | <name>name</name> |
141 | <cstring>clear_fields</cstring> | 167 | <cstring>TextLabel2_4</cstring> |
142 | </property> | 168 | </property> |
143 | <property stdset="1"> | 169 | <property stdset="1"> |
144 | <name>text</name> | 170 | <name>text</name> |
145 | <string>Clear</string> | 171 | <string>Result:</string> |
146 | </property> | 172 | </property> |
147 | </widget> | 173 | </widget> |
148 | </hbox> | 174 | <widget> |
149 | </widget> | 175 | <class>QLineEdit</class> |
150 | <widget> | 176 | <property stdset="1"> |
151 | <class>QLineEdit</class> | 177 | <name>name</name> |
152 | <property stdset="1"> | 178 | <cstring>result</cstring> |
153 | <name>name</name> | 179 | </property> |
154 | <cstring>formula</cstring> | 180 | </widget> |
155 | </property> | 181 | <widget> |
156 | <property stdset="1"> | 182 | <class>QLabel</class> |
157 | <name>geometry</name> | 183 | <property stdset="1"> |
158 | <rect> | 184 | <name>name</name> |
159 | <x>0</x> | 185 | <cstring>TextLabel1</cstring> |
160 | <y>20</y> | 186 | </property> |
161 | <width>170</width> | 187 | <property stdset="1"> |
162 | <height>22</height> | 188 | <name>text</name> |
163 | </rect> | 189 | <string>Elemental composition (%):</string> |
164 | </property> | 190 | </property> |
165 | </widget> | 191 | </widget> |
166 | <widget> | 192 | <widget> |
167 | <class>QMultiLineEdit</class> | 193 | <class>QMultiLineEdit</class> |
168 | <property stdset="1"> | 194 | <property stdset="1"> |
169 | <name>name</name> | 195 | <name>name</name> |
170 | <cstring>anal_display</cstring> | 196 | <cstring>anal_display</cstring> |
171 | </property> | 197 | </property> |
172 | <property stdset="1"> | 198 | <property stdset="1"> |
173 | <name>geometry</name> | 199 | <name>readOnly</name> |
174 | <rect> | 200 | <bool>true</bool> |
175 | <x>0</x> | 201 | </property> |
176 | <y>130</y> | 202 | </widget> |
177 | <width>170</width> | 203 | </vbox> |
178 | <height>80</height> | ||
179 | </rect> | ||
180 | </property> | ||
181 | </widget> | ||
182 | <widget> | ||
183 | <class>QLineEdit</class> | ||
184 | <property stdset="1"> | ||
185 | <name>name</name> | ||
186 | <cstring>result</cstring> | ||
187 | </property> | ||
188 | <property stdset="1"> | ||
189 | <name>geometry</name> | ||
190 | <rect> | ||
191 | <x>0</x> | ||
192 | <y>100</y> | ||
193 | <width>170</width> | ||
194 | <height>22</height> | ||
195 | </rect> | ||
196 | </property> | ||
197 | </widget> | ||
198 | <widget> | ||
199 | <class>QLabel</class> | ||
200 | <property stdset="1"> | ||
201 | <name>name</name> | ||
202 | <cstring>TextLabel2_4</cstring> | ||
203 | </property> | ||
204 | <property stdset="1"> | ||
205 | <name>geometry</name> | ||
206 | <rect> | ||
207 | <x>1</x> | ||
208 | <y>86</y> | ||
209 | <width>298</width> | ||
210 | <height>16</height> | ||
211 | </rect> | ||
212 | </property> | ||
213 | <property stdset="1"> | ||
214 | <name>text</name> | ||
215 | <string>Result:</string> | ||
216 | </property> | ||
217 | </widget> | 204 | </widget> |
218 | </widget> | 205 | </widget> |
219 | </widget> | 206 | </widget> |
220 | <widget> | 207 | <widget> |
221 | <class>QWidget</class> | 208 | <class>QWidget</class> |
222 | <property stdset="1"> | 209 | <property stdset="1"> |
223 | <name>name</name> | 210 | <name>name</name> |
224 | <cstring>tabNtp</cstring> | 211 | <cstring>tabNtp</cstring> |
225 | </property> | 212 | </property> |
226 | <attribute> | 213 | <attribute> |
227 | <name>title</name> | 214 | <name>title</name> |
228 | <string>Nernst</string> | 215 | <string>Nernst</string> |
229 | </attribute> | 216 | </attribute> |
230 | <grid> | 217 | <grid> |
231 | <property stdset="1"> | 218 | <property stdset="1"> |
232 | <name>margin</name> | 219 | <name>margin</name> |
233 | <number>1</number> | 220 | <number>1</number> |
234 | </property> | 221 | </property> |
235 | <property stdset="1"> | 222 | <property stdset="1"> |
236 | <name>spacing</name> | 223 | <name>spacing</name> |
237 | <number>2</number> | 224 | <number>2</number> |
238 | </property> | 225 | </property> |
239 | </grid> | 226 | </grid> |
240 | </widget> | 227 | </widget> |
241 | </widget> | 228 | </widget> |
242 | </widget> | 229 | </widget> |
243 | </UI> | 230 | </UI> |
diff --git a/noncore/apps/oxygen/calcdlgui.cpp b/noncore/apps/oxygen/calcdlgui.cpp index e9b40d3..e40e2c7 100644 --- a/noncore/apps/oxygen/calcdlgui.cpp +++ b/noncore/apps/oxygen/calcdlgui.cpp | |||
@@ -1,55 +1,57 @@ | |||
1 | /*************************************************************************** | 1 | /*************************************************************************** |
2 | * * | 2 | * * |
3 | * This program is free software; you can redistribute it and/or modify * | 3 | * This program is free software; you can redistribute it and/or modify * |
4 | * it under the terms of the GNU General Public License as published by * | 4 | * it under the terms of the GNU General Public License as published by * |
5 | * the Free Software Foundation; either version 2 of the License, or * | 5 | * the Free Software Foundation; either version 2 of the License, or * |
6 | * ( at your option ) any later version. * | 6 | * ( at your option ) any later version. * |
7 | * * | 7 | * * |
8 | **************************************************************************/ | 8 | **************************************************************************/ |
9 | #include "oxygen.h" | 9 | #include "oxygen.h" |
10 | 10 | ||
11 | #include "calcdlgui.h" | 11 | #include "calcdlgui.h" |
12 | #include "kmolcalc.h" | 12 | #include "kmolcalc.h" |
13 | #include <qlineedit.h> | 13 | #include <qlineedit.h> |
14 | #include <qmultilineedit.h> | 14 | #include <qmultilineedit.h> |
15 | #include <qpushbutton.h> | 15 | #include <qpushbutton.h> |
16 | 16 | ||
17 | calcDlgUI::calcDlgUI() : CalcDlg() | 17 | calcDlgUI::calcDlgUI() : CalcDlg() |
18 | { | 18 | { |
19 | this->showMaximized(); | ||
19 | kmolcalc = new KMolCalc; | 20 | kmolcalc = new KMolCalc; |
20 | connect( calculate, SIGNAL( clicked() ), this, SLOT( calc() ) ); | 21 | connect( calculate, SIGNAL( clicked() ), this, SLOT( calc() ) ); |
21 | connect( clear_fields, SIGNAL( clicked() ), this, SLOT( clear() ) ); | 22 | connect( clear_fields, SIGNAL( clicked() ), this, SLOT( clear() ) ); |
23 | result->setReadOnly( true ); | ||
22 | } | 24 | } |
23 | 25 | ||
24 | void calcDlgUI::calc() | 26 | void calcDlgUI::calc() |
25 | { | 27 | { |
26 | QString compound( formula->text() ); | 28 | QString compound( formula->text() ); |
27 | if ( compound.isEmpty() ) { | 29 | if ( compound.isEmpty() ) { |
28 | clear(); | 30 | clear(); |
29 | return; | 31 | return; |
30 | } | 32 | } |
31 | QString errors( kmolcalc->readFormula( compound ) ); | 33 | QString errors( kmolcalc->readFormula( compound ) ); |
32 | QString mw, ea; | 34 | QString mw, ea; |
33 | double weight = kmolcalc->getWeight(); | 35 | double weight = kmolcalc->getWeight(); |
34 | if ( errors == "OK" ) { | 36 | if ( errors == "OK" ) { |
35 | mw.setNum( weight ); | 37 | mw.setNum( weight ); |
36 | ea = kmolcalc->getEmpFormula() + " :\n" + kmolcalc->getEA(); | 38 | ea = kmolcalc->getEmpFormula() + " :\n" + kmolcalc->getEA(); |
37 | } else { | 39 | } else { |
38 | mw = "???"; | 40 | mw = "???"; |
39 | ea = tr( "ERROR: \n" ) + errors + "\n"; | 41 | ea = tr( "ERROR: \n" ) + errors + "\n"; |
40 | } | 42 | } |
41 | result->setText( mw ); | 43 | result->setText( mw ); |
42 | anal_display->setText( ea ); | 44 | anal_display->setText( ea ); |
43 | } | 45 | } |
44 | 46 | ||
45 | /** | 47 | /** |
46 | * * Clear all text entry / result fields. | 48 | * * Clear all text entry / result fields. |
47 | * */ | 49 | * */ |
48 | void calcDlgUI::clear() | 50 | void calcDlgUI::clear() |
49 | { | 51 | { |
50 | formula->clear(); | 52 | formula->clear(); |
51 | result->clear(); | 53 | result->clear(); |
52 | anal_display->clear(); | 54 | anal_display->clear(); |
53 | } | 55 | } |
54 | 56 | ||
55 | 57 | ||
diff --git a/noncore/apps/oxygen/kmolcalc.cpp b/noncore/apps/oxygen/kmolcalc.cpp index 7257c4a..0ba52da 100644 --- a/noncore/apps/oxygen/kmolcalc.cpp +++ b/noncore/apps/oxygen/kmolcalc.cpp | |||
@@ -1,235 +1,199 @@ | |||
1 | /* | 1 | /* |
2 | * kmolcalc.cpp | 2 | * kmolcalc.cpp |
3 | * | 3 | * |
4 | * Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com> | 4 | * Copyright (C) 2000,2001 Tomislav Gountchev <tomi@idiom.com> |
5 | * Copyright (C) 2002 Carsten Niehaus <cniehaus@handhelds.org> | 5 | * Copyright (C) 2002 Carsten Niehaus <cniehaus@handhelds.org> |
6 | */ | 6 | */ |
7 | 7 | ||
8 | /** | 8 | /** |
9 | * KMOLCALC is the calculation engine. It knows about a hashtable of user defined atomic | 9 | * KMOLCALC is the calculation engine. It knows about a hashtable of user defined atomic |
10 | * weights and group definitions ELSTABLE, and the currently processed formula, stored | 10 | * weights and group definitions ELSTABLE, and the currently processed formula, stored |
11 | * as a list of elements and their coefficients, ELEMENTS. | 11 | * as a list of elements and their coefficients, ELEMENTS. |
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include "kmolcalc.h" | 14 | #include "kmolcalc.h" |
15 | #include <qdict.h> | 15 | #include <qdict.h> |
16 | #include <qdir.h> | 16 | #include <qdir.h> |
17 | #include <qfile.h> | 17 | #include <qfile.h> |
18 | #include <iostream.h> | 18 | #include <iostream.h> |
19 | 19 | ||
20 | 20 | ||
21 | /** | 21 | /** |
22 | * Construct a new calculator object. | 22 | * Construct a new calculator object. |
23 | */ | 23 | */ |
24 | KMolCalc::KMolCalc() { | 24 | KMolCalc::KMolCalc() { |
25 | elements = new ElementList; | 25 | elements = new ElementList; |
26 | elstable = NULL; | 26 | elstable = NULL; |
27 | readElstable(); | 27 | readElstable(); |
28 | } | 28 | } |
29 | 29 | ||
30 | KMolCalc::~KMolCalc() { | 30 | KMolCalc::~KMolCalc() { |
31 | delete elements; | 31 | delete elements; |
32 | } | 32 | } |
33 | 33 | ||
34 | void KMolCalc::readElstable() { | 34 | void KMolCalc::readElstable() { |
35 | weight = -1; // not calculated yet | 35 | weight = -1; // not calculated yet |
36 | if (elstable) delete elstable; | 36 | if (elstable) delete elstable; |
37 | elstable = new QDict<SubUnit> (197, TRUE); | 37 | elstable = new QDict<SubUnit> (197, TRUE); |
38 | elstable->setAutoDelete(TRUE); | 38 | elstable->setAutoDelete(TRUE); |
39 | QStringList files = "/home/opie/opie/noncore/apps/oxigen/kmolweights"; | 39 | mwfile = "/home/opie/opie/noncore/apps/oxygen/kmolweights"; |
40 | mwfile = "/home/opie/opie/noncore/apps/oxigen/kmolweights"; | ||
41 | QFile f(mwfile); | 40 | QFile f(mwfile); |
42 | QString* latest_f = &mwfile; | ||
43 | for (uint i=0; i<files.count(); i++) { | ||
44 | if (QFileInfo(QFile(files[i])).lastModified() > QFileInfo(QFile(*latest_f)).lastModified()) { | ||
45 | latest_f = &files[i]; | ||
46 | } | ||
47 | } | ||
48 | QFile lf(*latest_f); | ||
49 | if (f.exists()) readMwfile(f); | 41 | if (f.exists()) readMwfile(f); |
50 | if (!f.exists()) { | ||
51 | readMwfile(lf); | ||
52 | writeElstable(); | ||
53 | } else if (QFileInfo(f).lastModified() < QFileInfo(lf).lastModified()) { | ||
54 | // announce | ||
55 | QMessageBox::information | ||
56 | (0, "Warning:", "Found new global Mw file.\nLocal definitions will be updated.", QMessageBox::Ok); | ||
57 | readMwfile(lf); | ||
58 | writeElstable(); | ||
59 | } | ||
60 | |||
61 | } | 42 | } |
62 | 43 | ||
63 | 44 | ||
64 | /** | 45 | /** |
65 | * Parse a string S and construct the ElementList this->ELEMENTS, representing the | 46 | * Parse a string S and construct the ElementList this->ELEMENTS, representing the |
66 | * composition of S. Returns 0 if successful, or an error code (currently -1) if | 47 | * composition of S. Returns 0 if successful, or an error code (currently -1) if |
67 | * parsing failed. | 48 | * parsing failed. |
68 | * The elements is S must be valid element or group symbols, as stored in this->ELSTABLE. | 49 | * The elements is S must be valid element or group symbols, as stored in this->ELSTABLE. |
69 | * See help files for correct formula syntax. | 50 | * See help files for correct formula syntax. |
70 | */ | 51 | */ |
71 | QString KMolCalc::readFormula(const QString& s) { | 52 | QString KMolCalc::readFormula(const QString& s) { |
72 | weight = -1; | 53 | weight = -1; |
73 | if (elements) delete elements; | 54 | if (elements) delete elements; |
74 | elements = new ElementList; | 55 | elements = new ElementList; |
75 | return KMolCalc::readGroup(s, elements); | 56 | return KMolCalc::readGroup(s, elements); |
76 | } | 57 | } |
77 | 58 | ||
78 | // read a formula group recursively. Called by readFormula. | 59 | // read a formula group recursively. Called by readFormula. |
79 | QString KMolCalc::readGroup(const QString& s, ElementList* els) { | 60 | QString KMolCalc::readGroup(const QString& s, ElementList* els) { |
80 | if (s.isEmpty()) return QString ("Enter a formula."); //ERROR | 61 | if (s.isEmpty()) return QString ("Enter a formula."); //ERROR |
81 | int sl = s.length(); | 62 | int sl = s.length(); |
82 | int i = 0; | 63 | int i = 0; |
83 | QString errors ("OK"); | 64 | QString errors ("OK"); |
84 | bool ok = TRUE; | 65 | bool ok = TRUE; |
85 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; | 66 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; |
86 | double prefix = (i == 0 ? 1 : s.left(i).toDouble(&ok)); | 67 | double prefix = (i == 0 ? 1 : s.left(i).toDouble(&ok)); |
87 | if (! ok || i == sl || prefix == 0) return QString ("Bad formula."); // ERROR | 68 | if (! ok || i == sl || prefix == 0) return QString ("Bad formula."); // ERROR |
88 | ElementList* elstemp = new ElementList; | 69 | ElementList* elstemp = new ElementList; |
89 | while (i < sl) { | 70 | while (i < sl) { |
90 | int j = i; | 71 | int j = i; |
91 | if (s[i] == '(') { | 72 | if (s[i] == '(') { |
92 | ElementList* inner = new ElementList; | 73 | ElementList* inner = new ElementList; |
93 | int level = 1; // count levels of nested ( ). | 74 | int level = 1; // count levels of nested ( ). |
94 | while (1) { | 75 | while (1) { |
95 | if (i++ == sl) { | 76 | if (i++ == sl) { |
96 | delete inner; | 77 | delete inner; |
97 | delete elstemp; | 78 | delete elstemp; |
98 | return QString ("Bad formula."); //ERROR | 79 | return QString ("Bad formula."); //ERROR |
99 | } | 80 | } |
100 | if (s[i] == '(') level++; | 81 | if (s[i] == '(') level++; |
101 | if (s[i] == ')') level--; | 82 | if (s[i] == ')') level--; |
102 | if (level == 0) break; | 83 | if (level == 0) break; |
103 | } | 84 | } |
104 | errors = KMolCalc::readGroup(s.mid(j+1, i-j-1), inner); | 85 | errors = KMolCalc::readGroup(s.mid(j+1, i-j-1), inner); |
105 | j = ++i; | 86 | j = ++i; |
106 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; | 87 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; |
107 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); | 88 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); |
108 | if (! ok || suffix == 0) { | 89 | if (! ok || suffix == 0) { |
109 | delete inner; | 90 | delete inner; |
110 | delete elstemp; | 91 | delete elstemp; |
111 | return QString ("Bad formula."); // ERROR | 92 | return QString ("Bad formula."); // ERROR |
112 | } | 93 | } |
113 | inner->addTo(*elstemp, suffix); | 94 | inner->addTo(*elstemp, suffix); |
114 | delete inner; | 95 | delete inner; |
115 | inner = NULL; | 96 | inner = NULL; |
116 | } else if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) { | 97 | } else if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) { |
117 | while (++i < sl && ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '*' || | 98 | while (++i < sl && ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '*' || |
118 | s[i] == '\'')); | 99 | s[i] == '\'')); |
119 | QString elname = s.mid(j, i-j); | 100 | QString elname = s.mid(j, i-j); |
120 | j = i; | 101 | j = i; |
121 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; | 102 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; |
122 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); | 103 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); |
123 | if (! ok || suffix == 0) { | 104 | if (! ok || suffix == 0) { |
124 | delete elstemp; | 105 | delete elstemp; |
125 | return QString ("Bad formula."); // ERROR | 106 | return QString ("Bad formula."); // ERROR |
126 | } | 107 | } |
127 | SubUnit* group = elstable->find(elname); | 108 | SubUnit* group = elstable->find(elname); |
128 | if (group == 0) { | 109 | if (group == 0) { |
129 | delete elstemp; | 110 | delete elstemp; |
130 | return QString ("Undefined symbol: ") + elname; //ERROR | 111 | return QString ("Undefined symbol: ") + elname; //ERROR |
131 | } | 112 | } |
132 | group->addTo(*elstemp, suffix); | 113 | group->addTo(*elstemp, suffix); |
133 | } else if (s[i] == '+') { | 114 | } else if (s[i] == '+') { |
134 | if (elstemp->isEmpty()) { | 115 | if (elstemp->isEmpty()) { |
135 | delete elstemp; | 116 | delete elstemp; |
136 | return QString ("Bad formula."); //ERROR | 117 | return QString ("Bad formula."); //ERROR |
137 | } | 118 | } |
138 | elstemp->addTo(*els, prefix); | 119 | elstemp->addTo(*els, prefix); |
139 | delete elstemp; | 120 | delete elstemp; |
140 | errors = KMolCalc::readGroup(s.mid(i+1, sl-i-1), els); | 121 | errors = KMolCalc::readGroup(s.mid(i+1, sl-i-1), els); |
141 | return errors; | 122 | return errors; |
142 | } else { | 123 | } else { |
143 | delete elstemp; | 124 | delete elstemp; |
144 | return QString ("Bad formula."); //ERROR | 125 | return QString ("Bad formula."); //ERROR |
145 | } | 126 | } |
146 | } | 127 | } |
147 | elstemp->addTo(*els, prefix); | 128 | elstemp->addTo(*els, prefix); |
148 | delete elstemp; | 129 | delete elstemp; |
149 | return errors; | 130 | return errors; |
150 | } | 131 | } |
151 | 132 | ||
152 | /** | 133 | /** |
153 | * Calculate and return the molecular weight of the current chemical formula. | 134 | * Calculate and return the molecular weight of the current chemical formula. |
154 | */ | 135 | */ |
155 | double KMolCalc::getWeight() { | 136 | double KMolCalc::getWeight() { |
156 | if (weight == -1) weight = elements->getWeight(elstable); | 137 | if (weight == -1) weight = elements->getWeight(elstable); |
157 | return weight; | 138 | return weight; |
158 | } | 139 | } |
159 | 140 | ||
160 | /** | 141 | /** |
161 | * Return the elemental composition of the current formula, as a string of tab-separated | 142 | * Return the elemental composition of the current formula, as a string of tab-separated |
162 | * element - percentage pairs, separated by newlines. | 143 | * element - percentage pairs, separated by newlines. |
163 | */ | 144 | */ |
164 | QString KMolCalc::getEA() { | 145 | QString KMolCalc::getEA() { |
165 | if (weight == -1) weight = elements->getWeight(elstable); | 146 | if (weight == -1) weight = elements->getWeight(elstable); |
166 | if (weight == -1) return QString("ERROR: Couldn't get Mw..."); // ERROR | 147 | if (weight == -1) return QString("ERROR: Couldn't get Mw..."); // ERROR |
167 | return elements->getEA(elstable, weight); | 148 | return elements->getEA(elstable, weight); |
168 | } | 149 | } |
169 | 150 | ||
170 | /** | 151 | /** |
171 | * Return the empirical formula of the current compound as a QString. | 152 | * Return the empirical formula of the current compound as a QString. |
172 | */ | 153 | */ |
173 | QString KMolCalc::getEmpFormula() { | 154 | QString KMolCalc::getEmpFormula() { |
174 | return elements->getEmpFormula(); | 155 | return elements->getEmpFormula(); |
175 | } | 156 | } |
176 | 157 | ||
177 | // Read the element definition file. | 158 | // Read the element definition file. |
178 | void KMolCalc::readMwfile(QFile& f) { | 159 | void KMolCalc::readMwfile(QFile& f) { |
179 | if (! f.open(IO_ReadOnly)) return; //ERROR | 160 | if (! f.open(IO_ReadOnly)) return; //ERROR |
180 | QTextStream fs (&f); | 161 | QTextStream fs (&f); |
181 | QString line; | 162 | QString line; |
182 | while (! fs.eof()) { | 163 | while (! fs.eof()) { |
183 | line = fs.readLine(); | 164 | line = fs.readLine(); |
184 | SubUnit* s = SubUnit::makeSubUnit(line); | 165 | SubUnit* s = SubUnit::makeSubUnit(line); |
185 | elstable->replace(s->getName(), s); | 166 | elstable->replace(s->getName(), s); |
186 | } | 167 | } |
187 | f.close(); | 168 | f.close(); |
188 | } | 169 | } |
189 | 170 | ||
190 | /** | 171 | /** |
191 | * Save the element definitions file. | ||
192 | */ | ||
193 | void KMolCalc::writeElstable() { | ||
194 | QFile f(mwfile); | ||
195 | if (! f.open(IO_WriteOnly)) return; //ERROR | ||
196 | QTextStream fs (&f); | ||
197 | QString line; | ||
198 | QDictIterator<SubUnit> it(*elstable); | ||
199 | while (it.current()) { | ||
200 | it.current()->writeOut(line); | ||
201 | fs << line << endl; | ||
202 | ++it; | ||
203 | } | ||
204 | f.close(); | ||
205 | } | ||
206 | |||
207 | /** | ||
208 | * Remove a group or element definition from ELSTABLE. | 172 | * Remove a group or element definition from ELSTABLE. |
209 | */ | 173 | */ |
210 | void KMolCalc::undefineGroup (const QString& name) { | 174 | void KMolCalc::undefineGroup (const QString& name) { |
211 | elstable->remove (name); | 175 | elstable->remove (name); |
212 | } | 176 | } |
213 | 177 | ||
214 | /** | 178 | /** |
215 | * Add a new element name - atomic weight record to the ELSTABLE hashtable. Assumes | 179 | * Add a new element name - atomic weight record to the ELSTABLE hashtable. Assumes |
216 | * NAME has valid syntax. | 180 | * NAME has valid syntax. |
217 | 181 | ||
218 | */ | 182 | */ |
219 | void KMolCalc::defineElement (const QString& name, double weight) { | 183 | void KMolCalc::defineElement (const QString& name, double weight) { |
220 | Element* el = new Element(name, weight); | 184 | Element* el = new Element(name, weight); |
221 | elstable->replace(name, el); | 185 | elstable->replace(name, el); |
222 | } | 186 | } |
223 | 187 | ||
224 | /** | 188 | /** |
225 | * Add a new group definition to the ELSTABLE. Returns 0 if OK, -1 if parsing FORMULA | 189 | * Add a new group definition to the ELSTABLE. Returns 0 if OK, -1 if parsing FORMULA |
226 | * fails. Assumes the syntax of grpname is correct. | 190 | * fails. Assumes the syntax of grpname is correct. |
227 | */ | 191 | */ |
228 | QString KMolCalc::defineGroup (const QString& grpname, const QString& formula) { | 192 | QString KMolCalc::defineGroup (const QString& grpname, const QString& formula) { |
229 | ElementList* els = new ElementList(grpname); | 193 | ElementList* els = new ElementList(grpname); |
230 | QString error = readGroup(formula, els); | 194 | QString error = readGroup(formula, els); |
231 | if (error != "OK") return error; | 195 | if (error != "OK") return error; |
232 | if (els->contains(grpname)) return QString("Can't define a group recursively!\n"); | 196 | if (els->contains(grpname)) return QString("Can't define a group recursively!\n"); |
233 | elstable->replace(grpname, els); | 197 | elstable->replace(grpname, els); |
234 | return QString("OK"); | 198 | return QString("OK"); |
235 | } | 199 | } |
diff --git a/noncore/apps/oxygen/kmolcalc.h b/noncore/apps/oxygen/kmolcalc.h index c3e02f3..c589133 100644 --- a/noncore/apps/oxygen/kmolcalc.h +++ b/noncore/apps/oxygen/kmolcalc.h | |||
@@ -1,110 +1,105 @@ | |||
1 | /* | 1 | /* |
2 | * kmolcalc.h | 2 | * kmolcalc.h |
3 | * | 3 | * |
4 | * Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com> | 4 | * Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com> |
5 | */ | 5 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #ifndef KMOLCALC_H | 8 | #ifndef KMOLCALC_H |
9 | #define KMOLCALC_H | 9 | #define KMOLCALC_H |
10 | 10 | ||
11 | #ifdef HAVE_CONFIG_H | 11 | #ifdef HAVE_CONFIG_H |
12 | #include <config.h> | 12 | #include <config.h> |
13 | #endif | 13 | #endif |
14 | 14 | ||
15 | 15 | ||
16 | #include "kmolelements.h" | 16 | #include "kmolelements.h" |
17 | #include <qdict.h> | 17 | #include <qdict.h> |
18 | #include <qlist.h> | 18 | #include <qlist.h> |
19 | #include <qstring.h> | 19 | #include <qstring.h> |
20 | #include <qfile.h> | 20 | #include <qfile.h> |
21 | #include <qfileinfo.h> | 21 | #include <qfileinfo.h> |
22 | #include <qmessagebox.h> | 22 | #include <qmessagebox.h> |
23 | #include <qtextstream.h> | 23 | #include <qtextstream.h> |
24 | 24 | ||
25 | 25 | ||
26 | /** | 26 | /** |
27 | * KMOLCALC is the calculation engine. It knows about a hashtable of user defined atomic | 27 | * KMOLCALC is the calculation engine. It knows about a hashtable of user defined atomic |
28 | * weights and group definitions ELSTABLE, and the currently processed formula, stored | 28 | * weights and group definitions ELSTABLE, and the currently processed formula, stored |
29 | * as a list of elements and their coefficients, ELEMENTS. | 29 | * as a list of elements and their coefficients, ELEMENTS. |
30 | */ | 30 | */ |
31 | class KMolCalc { | 31 | class KMolCalc { |
32 | 32 | ||
33 | public: | 33 | public: |
34 | 34 | ||
35 | KMolCalc(); | 35 | KMolCalc(); |
36 | ~KMolCalc(); | 36 | ~KMolCalc(); |
37 | 37 | ||
38 | /** | 38 | /** |
39 | * Parse a string S and construct the ElementList ELEMENTS, representing the composition | 39 | * Parse a string S and construct the ElementList ELEMENTS, representing the composition |
40 | * of S. Returns "OK" if successful, or an error message if parsing failed. | 40 | * of S. Returns "OK" if successful, or an error message if parsing failed. |
41 | * The elements is S must be valid element or group symbols, as stored in ELSTABLE. | 41 | * The elements is S must be valid element or group symbols, as stored in ELSTABLE. |
42 | * See help files for correct formula syntax. | 42 | * See help files for correct formula syntax. |
43 | */ | 43 | */ |
44 | QString readFormula(const QString& s); | 44 | QString readFormula(const QString& s); |
45 | 45 | ||
46 | /** | 46 | /** |
47 | * Calculate and return the molecular weight of the current chemical formula. | 47 | * Calculate and return the molecular weight of the current chemical formula. |
48 | */ | 48 | */ |
49 | double getWeight(); | 49 | double getWeight(); |
50 | 50 | ||
51 | /** | 51 | /** |
52 | * Return the elemental composition of the current formula, as a string of tab-separated | 52 | * Return the elemental composition of the current formula, as a string of tab-separated |
53 | * element - percentage pairs, separated by newlines. | 53 | * element - percentage pairs, separated by newlines. |
54 | */ | 54 | */ |
55 | QString getEA(); | 55 | QString getEA(); |
56 | 56 | ||
57 | 57 | ||
58 | /** | 58 | /** |
59 | * Return the empirical formula of the current compound as a QString. | 59 | * Return the empirical formula of the current compound as a QString. |
60 | */ | 60 | */ |
61 | QString getEmpFormula(); | 61 | QString getEmpFormula(); |
62 | 62 | ||
63 | /** | 63 | /** |
64 | * Add a new element name - atomic weight record to the ELSTABLE hashtable. | 64 | * Add a new element name - atomic weight record to the ELSTABLE hashtable. |
65 | * Assumes NAME has valid syntax. | 65 | * Assumes NAME has valid syntax. |
66 | */ | 66 | */ |
67 | void defineElement(const QString& name, double weight); | 67 | void defineElement(const QString& name, double weight); |
68 | 68 | ||
69 | /** | 69 | /** |
70 | * Add a new group definition to the ELSTABLE. Returns "OK" if OK, error message | 70 | * Add a new group definition to the ELSTABLE. Returns "OK" if OK, error message |
71 | * if parsing FORMULA fails. Assumes the syntax of NAME is correct. | 71 | * if parsing FORMULA fails. Assumes the syntax of NAME is correct. |
72 | */ | 72 | */ |
73 | QString defineGroup(const QString& name, const QString& formula); | 73 | QString defineGroup(const QString& name, const QString& formula); |
74 | 74 | ||
75 | /** | 75 | /** |
76 | * Remove a group or element definition from ELSTABLE. | 76 | * Remove a group or element definition from ELSTABLE. |
77 | */ | 77 | */ |
78 | void undefineGroup(const QString& name); | 78 | void undefineGroup(const QString& name); |
79 | 79 | ||
80 | /** | 80 | /** |
81 | * Save the element definitions file. | ||
82 | */ | ||
83 | void writeElstable(); | ||
84 | |||
85 | /** | ||
86 | * Read the element definitions file and construct ELSTABLE. | 81 | * Read the element definitions file and construct ELSTABLE. |
87 | */ | 82 | */ |
88 | void readElstable(); | 83 | void readElstable(); |
89 | 84 | ||
90 | /** | 85 | /** |
91 | * The element dictionary. | 86 | * The element dictionary. |
92 | */ | 87 | */ |
93 | QDict<SubUnit>* elstable; | 88 | QDict<SubUnit>* elstable; |
94 | 89 | ||
95 | QString mwfile; | 90 | QString mwfile; |
96 | 91 | ||
97 | private: | 92 | private: |
98 | double weight; | 93 | double weight; |
99 | 94 | ||
100 | QString readGroup(const QString& s, ElementList* els); | 95 | QString readGroup(const QString& s, ElementList* els); |
101 | void readMwfile(QFile& f); | 96 | void readMwfile(QFile& f); |
102 | ElementList* elements; | 97 | ElementList* elements; |
103 | }; | 98 | }; |
104 | 99 | ||
105 | #endif | 100 | #endif |
106 | 101 | ||
107 | 102 | ||
108 | 103 | ||
109 | 104 | ||
110 | 105 | ||