-rw-r--r-- | noncore/tools/calculator/calculatorimpl.cpp | 24 | ||||
-rw-r--r-- | noncore/tools/calculator/calculatorimpl.h | 2 |
2 files changed, 21 insertions, 5 deletions
diff --git a/noncore/tools/calculator/calculatorimpl.cpp b/noncore/tools/calculator/calculatorimpl.cpp index 2f7d7ce..1b93c7f 100644 --- a/noncore/tools/calculator/calculatorimpl.cpp +++ b/noncore/tools/calculator/calculatorimpl.cpp | |||
@@ -1,44 +1,49 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | /* | ||
22 | * 01/14/2002 Charles-Edouard Ruault <ce@ruault.com> | ||
23 | * Added support for Temperature conversions. | ||
24 | */ | ||
25 | |||
21 | #include "calculatorimpl.h" | 26 | #include "calculatorimpl.h" |
22 | 27 | ||
23 | #include <qpe/resource.h> | 28 | #include <qpe/resource.h> |
24 | #include <qpe/qmath.h> | 29 | #include <qpe/qmath.h> |
25 | #include <qpe/qpeapplication.h> | 30 | #include <qpe/qpeapplication.h> |
26 | 31 | ||
27 | #include <qpushbutton.h> | 32 | #include <qpushbutton.h> |
28 | #include <qcombobox.h> | 33 | #include <qcombobox.h> |
29 | #include <qlabel.h> | 34 | #include <qlabel.h> |
30 | #include <qfont.h> | 35 | #include <qfont.h> |
31 | #include <qlayout.h> | 36 | #include <qlayout.h> |
32 | #include <qstringlist.h> | 37 | #include <qstringlist.h> |
33 | #include <qfile.h> | 38 | #include <qfile.h> |
34 | #include <qtextstream.h> | 39 | #include <qtextstream.h> |
35 | #include <qmessagebox.h> | 40 | #include <qmessagebox.h> |
36 | #include <math.h> | 41 | #include <math.h> |
37 | 42 | ||
38 | CalculatorImpl::CalculatorImpl( QWidget * parent, const char * name, | 43 | CalculatorImpl::CalculatorImpl( QWidget * parent, const char * name, |
39 | WFlags f ) | 44 | WFlags f ) |
40 | : Calculator( parent, name, f ) | 45 | : Calculator( parent, name, f ) |
41 | { | 46 | { |
42 | xtopowerofy = Resource::loadPixmap("xtopowerofy"); | 47 | xtopowerofy = Resource::loadPixmap("xtopowerofy"); |
43 | ythrootofx = Resource::loadPixmap("ythrootofx"); | 48 | ythrootofx = Resource::loadPixmap("ythrootofx"); |
44 | oneoverx = Resource::loadPixmap("oneoverx"); | 49 | oneoverx = Resource::loadPixmap("oneoverx"); |
@@ -113,67 +118,74 @@ CalculatorImpl::CalculatorImpl( QWidget * parent, const char * name, | |||
113 | ComboBoxFunction->insertItem(captions.last()); | 118 | ComboBoxFunction->insertItem(captions.last()); |
114 | 119 | ||
115 | // now add in the conversion modes | 120 | // now add in the conversion modes |
116 | // when the menu gets done, these should be in a submenu | 121 | // when the menu gets done, these should be in a submenu |
117 | QString tmp = QPEApplication::qpeDir(); | 122 | QString tmp = QPEApplication::qpeDir(); |
118 | tmp += "/etc/unit_conversion.dat"; | 123 | tmp += "/etc/unit_conversion.dat"; |
119 | QFile myfile(tmp); | 124 | QFile myfile(tmp); |
120 | if ( !myfile.open( IO_Translate | IO_ReadOnly ) ) { | 125 | if ( !myfile.open( IO_Translate | IO_ReadOnly ) ) { |
121 | // QMessageBox::warning(this, "Warning", "Data file\nunit_conversion.dat\nnot found\nNo conversion\nfeatures will\nbe available"); | 126 | // QMessageBox::warning(this, "Warning", "Data file\nunit_conversion.dat\nnot found\nNo conversion\nfeatures will\nbe available"); |
122 | // disable the f button if no conv file available | 127 | // disable the f button if no conv file available |
123 | ComboBoxFunction->setEnabled(FALSE); | 128 | ComboBoxFunction->setEnabled(FALSE); |
124 | } | 129 | } |
125 | else { | 130 | else { |
126 | QString line, line2; | 131 | QString line, line2; |
127 | QTextStream ts(&myfile); | 132 | QTextStream ts(&myfile); |
128 | 133 | ||
129 | // first pass, see how many conversion types there are in order to allocate for them | 134 | // first pass, see how many conversion types there are in order to allocate for them |
130 | while ( ! ts.eof() ) { | 135 | while ( ! ts.eof() ) { |
131 | line = ts.readLine(); | 136 | line = ts.readLine(); |
132 | if ( line.contains ("STARTTYPE" ) ) | 137 | if ( line.contains ("STARTTYPE" ) ) |
133 | conversion_mode_count++; | 138 | conversion_mode_count++; |
134 | } | 139 | } |
135 | 140 | ||
136 | entry_list = new double[conversion_mode_count*func_button_count]; | 141 | entry_list = new double[conversion_mode_count*func_button_count]; |
137 | 142 | preoffset_list = new double[conversion_mode_count*func_button_count]; | |
143 | postoffset_list = new double[conversion_mode_count*func_button_count]; | ||
138 | myfile.close(); | 144 | myfile.close(); |
139 | myfile.open( IO_Translate | IO_ReadOnly ); | 145 | myfile.open( IO_Translate | IO_ReadOnly ); |
140 | QTextStream ts2(&myfile); | 146 | QTextStream ts2(&myfile); |
141 | 147 | ||
142 | // second pass, read in values | 148 | // second pass, read in values |
143 | int x = 0; | 149 | int x = 0; |
144 | while ( ! ts2.eof() ) { | 150 | while ( ! ts2.eof() ) { |
145 | line = ts2.readLine(); | 151 | line = ts2.readLine(); |
146 | if ( line.contains("STARTTYPE") ) { | 152 | if ( line.contains("STARTTYPE") ) { |
147 | captions << line.remove(0,10); | 153 | captions << line.remove(0,10); |
148 | ComboBoxFunction->insertItem(captions.last()); | 154 | ComboBoxFunction->insertItem(captions.last()); |
149 | while ( !line.contains("ENDTYPE") ) { | 155 | while ( !line.contains("ENDTYPE") ) { |
150 | line = ts2.readLine(); | 156 | line = ts2.readLine(); |
151 | if ( line.contains("NAME") ) { | 157 | if ( line.contains("NAME") ) { |
152 | faces << line.remove(0,5); | 158 | faces << line.remove(0,5); |
153 | line2 = ts2.readLine(); | 159 | line2 = ts2.readLine(); |
154 | line2.remove(0,6); | 160 | line2.remove(0,6); |
155 | entry_list[x] = line2.toDouble(); | 161 | entry_list[x] = line2.toDouble(); |
162 | line2 = ts2.readLine(); | ||
163 | line2.remove(0,7); | ||
164 | preoffset_list[x] = line2.toDouble(); | ||
165 | line2 = ts2.readLine(); | ||
166 | line2.remove(0,8); | ||
167 | postoffset_list[x] = line2.toDouble(); | ||
156 | x++; | 168 | x++; |
157 | } | 169 | } |
158 | } | 170 | } |
159 | } | 171 | } |
160 | } | 172 | } |
161 | } | 173 | } |
162 | myfile.close(); | 174 | myfile.close(); |
163 | clear(); | 175 | clear(); |
164 | max_mode = pre_conv_modes_count + conversion_mode_count + post_conv_modes_count - 1; | 176 | max_mode = pre_conv_modes_count + conversion_mode_count + post_conv_modes_count - 1; |
165 | display_pixmap_faces(); | 177 | display_pixmap_faces(); |
166 | 178 | ||
167 | qApp->installEventFilter( this ); | 179 | qApp->installEventFilter( this ); |
168 | } | 180 | } |
169 | 181 | ||
170 | bool CalculatorImpl::eventFilter( QObject *o, QEvent *e ) | 182 | bool CalculatorImpl::eventFilter( QObject *o, QEvent *e ) |
171 | { | 183 | { |
172 | if ( e->type() == QEvent::KeyPress && state != sError ) { | 184 | if ( e->type() == QEvent::KeyPress && state != sError ) { |
173 | QKeyEvent *k = (QKeyEvent*)e; | 185 | QKeyEvent *k = (QKeyEvent*)e; |
174 | if ( k->key() >= Key_0 && k->key() <= Key_9 ) { | 186 | if ( k->key() >= Key_0 && k->key() <= Key_9 ) { |
175 | enterNumber( k->key() - Key_0 ); | 187 | enterNumber( k->key() - Key_0 ); |
176 | return true; | 188 | return true; |
177 | } else { | 189 | } else { |
178 | switch ( k->key() ) { | 190 | switch ( k->key() ) { |
179 | case Key_Equal: | 191 | case Key_Equal: |
@@ -199,58 +211,60 @@ bool CalculatorImpl::eventFilter( QObject *o, QEvent *e ) | |||
199 | return true; | 211 | return true; |
200 | case Key_ParenLeft: | 212 | case Key_ParenLeft: |
201 | if ( current_mode < pre_conv_modes_count ) | 213 | if ( current_mode < pre_conv_modes_count ) |
202 | execOp( oOpenBrace ); | 214 | execOp( oOpenBrace ); |
203 | return true; | 215 | return true; |
204 | case Key_ParenRight: | 216 | case Key_ParenRight: |
205 | if ( current_mode < pre_conv_modes_count ) | 217 | if ( current_mode < pre_conv_modes_count ) |
206 | execOp( oCloseBrace ); | 218 | execOp( oCloseBrace ); |
207 | return true; | 219 | return true; |
208 | default: | 220 | default: |
209 | break; | 221 | break; |
210 | } | 222 | } |
211 | } | 223 | } |
212 | } | 224 | } |
213 | return Calculator::eventFilter( o, e ); | 225 | return Calculator::eventFilter( o, e ); |
214 | } | 226 | } |
215 | 227 | ||
216 | void CalculatorImpl::do_convert(int button) { | 228 | void CalculatorImpl::do_convert(int button) { |
217 | if ( state == sError ) | 229 | if ( state == sError ) |
218 | return; | 230 | return; |
219 | if ( current_mode >= pre_conv_modes_count && current_mode <= (max_mode - post_conv_modes_count) && | 231 | if ( current_mode >= pre_conv_modes_count && current_mode <= (max_mode - post_conv_modes_count) && |
220 | button < changeable_func_button_count ) { | 232 | button < changeable_func_button_count ) { |
221 | if ( last_conversion > -1 ) { | 233 | if ( last_conversion > -1 ) { |
222 | if( state == sNewNumber ){ | 234 | if( state == sNewNumber ){ |
223 | acc = num | 235 | acc = (num+ preoffset_list[(current_mode - pre_conv_modes_count) * func_button_count + last_conversion]) |
224 | / (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + last_conversion]) | 236 | / (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + last_conversion]) |
225 | * (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + button]) ; | 237 | * (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + button]) |
238 | +postoffset_list[(current_mode - pre_conv_modes_count) * func_button_count + button]; | ||
226 | num = acc; | 239 | num = acc; |
227 | LCD->display( acc ); | 240 | LCD->display( acc ); |
228 | } else { | 241 | } else { |
229 | state = sNewNumber; | 242 | state = sNewNumber; |
230 | num = num | 243 | num = (num+ preoffset_list[(current_mode - pre_conv_modes_count) * func_button_count + last_conversion]) |
231 | / (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + last_conversion]) | 244 | / (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + last_conversion]) |
232 | * (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + button]) ; | 245 | * (entry_list[(current_mode - pre_conv_modes_count) * func_button_count + button]) |
246 | + postoffset_list[(current_mode - pre_conv_modes_count) * func_button_count + button];; | ||
233 | LCD->display( num ); | 247 | LCD->display( num ); |
234 | acc = num; | 248 | acc = num; |
235 | } | 249 | } |
236 | } | 250 | } |
237 | last_conversion = button; | 251 | last_conversion = button; |
238 | } | 252 | } |
239 | } | 253 | } |
240 | 254 | ||
241 | 255 | ||
242 | void CalculatorImpl::function_button(int mode){ | 256 | void CalculatorImpl::function_button(int mode){ |
243 | if ( state == sError ) | 257 | if ( state == sError ) |
244 | clear(); | 258 | clear(); |
245 | // dont need the next line when using a popup menu | 259 | // dont need the next line when using a popup menu |
246 | current_mode = mode; | 260 | current_mode = mode; |
247 | 261 | ||
248 | // reset the last conv | 262 | // reset the last conv |
249 | last_conversion = -1; | 263 | last_conversion = -1; |
250 | 264 | ||
251 | // set the caption | 265 | // set the caption |
252 | this->setCaption( captions[current_mode] ); | 266 | this->setCaption( captions[current_mode] ); |
253 | 267 | ||
254 | reset_conv(); | 268 | reset_conv(); |
255 | 269 | ||
256 | for ( int x = 0 ; x < changeable_func_button_count ; x++ ) { | 270 | for ( int x = 0 ; x < changeable_func_button_count ; x++ ) { |
diff --git a/noncore/tools/calculator/calculatorimpl.h b/noncore/tools/calculator/calculatorimpl.h index bfb726f..f0180c9 100644 --- a/noncore/tools/calculator/calculatorimpl.h +++ b/noncore/tools/calculator/calculatorimpl.h | |||
@@ -103,33 +103,35 @@ private: | |||
103 | 103 | ||
104 | void execOp( Operation i ); | 104 | void execOp( Operation i ); |
105 | double evalExpr( int op ); | 105 | double evalExpr( int op ); |
106 | QLabel * memMark; | 106 | QLabel * memMark; |
107 | QString fake; | 107 | QString fake; |
108 | 108 | ||
109 | // useful values for conversion stuff | 109 | // useful values for conversion stuff |
110 | int current_mode, max_mode, conversion_mode_count, last_conversion; | 110 | int current_mode, max_mode, conversion_mode_count, last_conversion; |
111 | 111 | ||
112 | // make adding new modes easier for ourselves | 112 | // make adding new modes easier for ourselves |
113 | static const int pre_conv_modes_count = 1; | 113 | static const int pre_conv_modes_count = 1; |
114 | static const int post_conv_modes_count = 0; | 114 | static const int post_conv_modes_count = 0; |
115 | 115 | ||
116 | // an array of pointers to the func buttons | 116 | // an array of pointers to the func buttons |
117 | static const int func_button_count = 12; | 117 | static const int func_button_count = 12; |
118 | // this is an abomination | 118 | // this is an abomination |
119 | static const int changeable_func_button_count = 10; | 119 | static const int changeable_func_button_count = 10; |
120 | QPushButton* func_buttons[func_button_count]; | 120 | QPushButton* func_buttons[func_button_count]; |
121 | 121 | ||
122 | QButtonGroup bgr_function, bgr_digits, bgr_std, bgr_command; | 122 | QButtonGroup bgr_function, bgr_digits, bgr_std, bgr_command; |
123 | QStringList faces, captions; | 123 | QStringList faces, captions; |
124 | 124 | ||
125 | // an array of doubles holding the conversion ratios | 125 | // an array of doubles holding the conversion ratios |
126 | double* entry_list; | 126 | double* entry_list; |
127 | double* preoffset_list; | ||
128 | double* postoffset_list; | ||
127 | 129 | ||
128 | QPixmap xtopowerofy; | 130 | QPixmap xtopowerofy; |
129 | QPixmap ythrootofx; | 131 | QPixmap ythrootofx; |
130 | QPixmap oneoverx; | 132 | QPixmap oneoverx; |
131 | 133 | ||
132 | void display_pixmap_faces(void); | 134 | void display_pixmap_faces(void); |
133 | }; | 135 | }; |
134 | 136 | ||
135 | #endif | 137 | #endif |