Mercurial > projects > qtd
comparison examples/widgets/calculator/calculator.d @ 210:3ea0efe4d31e
fixes for d2.
author | SokoL_SD |
---|---|
date | Tue, 14 Jul 2009 11:25:43 +0000 |
parents | e78566595089 |
children | 7ea67ec3cf29 |
comparison
equal
deleted
inserted
replaced
209:3f024154460c | 210:3ea0efe4d31e |
---|---|
39 ** | 39 ** |
40 ****************************************************************************/ | 40 ****************************************************************************/ |
41 | 41 |
42 module calculator; | 42 module calculator; |
43 | 43 |
44 import button; | |
44 import qt.gui.QDialog; | 45 import qt.gui.QDialog; |
45 import qt.gui.QGridLayout; | 46 import qt.gui.QGridLayout; |
46 import qt.gui.QLineEdit; | 47 import qt.gui.QLineEdit; |
47 import qt.gui.QFont; | 48 import qt.gui.QFont; |
48 | 49 |
49 import tango.math.Math : pow, sqrt; | 50 version(Tango) |
50 import Float = tango.text.convert.Float; | 51 { |
51 import Integer = tango.text.convert.Integer; | 52 import tango.math.Math; |
52 import tango.core.Array; | 53 import tango.math.Math : pow, sqrt; |
53 | 54 import Float = tango.text.convert.Float; |
54 import button; | 55 import Integer = tango.text.convert.Integer; |
55 | 56 import tango.core.Array; |
57 string ToString(int x) | |
58 { | |
59 return Integer.toString(x); | |
60 } | |
61 string ToString(double x, int c = 0) | |
62 { | |
63 return Float.toString(x, c); | |
64 } | |
65 int ToInt(string s) | |
66 { | |
67 return Integer.toInt(s); | |
68 } | |
69 float ToFloat(string s) | |
70 { | |
71 return Float.toFloat(s); | |
72 } | |
73 } | |
74 else | |
75 { | |
76 import std.math; | |
77 import std.conv; | |
78 import std.string; | |
79 string ToString(int x) | |
80 { | |
81 return std.string.format("%d", x); | |
82 } | |
83 string ToString(double x, int c = 0) | |
84 { | |
85 return std.string.format("%g", x); | |
86 } | |
87 int ToInt(string s) | |
88 { | |
89 return to!int(s); | |
90 } | |
91 float ToFloat(string s) | |
92 { | |
93 return to!float(s); | |
94 } | |
95 int find(string s, dchar c) | |
96 { | |
97 return indexOf(s,c); | |
98 } | |
99 } | |
56 | 100 |
57 class Calculator : public QDialog | 101 class Calculator : public QDialog |
58 { | 102 { |
59 | 103 |
60 public: | 104 public: |
71 display = new QLineEdit("0"); | 115 display = new QLineEdit("0"); |
72 display.setReadOnly(true); | 116 display.setReadOnly(true); |
73 display.setAlignment(Qt.AlignRight); | 117 display.setAlignment(Qt.AlignRight); |
74 display.setMaxLength(15); | 118 display.setMaxLength(15); |
75 | 119 |
76 QFont font = display.font(); | 120 auto font = new QFont(display.font()); |
77 font.setPointSize(font.pointSize() + 8); | 121 font.setPointSize(font.pointSize() + 8); |
78 display.setFont(font); | 122 display.setFont(font); |
79 | 123 |
80 for (int i = 0; i < NumDigitButtons; ++i) { | 124 for (int i = 0; i < NumDigitButtons; ++i) { |
81 digitButtons[i] = createButton(Integer.toString(i), &digitClicked); | 125 digitButtons[i] = createButton(ToString(i), &digitClicked); |
82 } | 126 } |
83 | 127 |
84 Button pointButton = createButton(tr("."), &pointClicked); | 128 Button pointButton = createButton(tr("."), &pointClicked); |
85 Button changeSignButton = createButton(tr("+/-"), &changeSignClicked); | 129 Button changeSignButton = createButton(tr("+/-"), &changeSignClicked); |
86 | 130 |
143 | 187 |
144 //private slots: | 188 //private slots: |
145 void digitClicked() | 189 void digitClicked() |
146 { | 190 { |
147 Button clickedButton = cast(Button) signalSender(); | 191 Button clickedButton = cast(Button) signalSender(); |
148 int digitValue = Integer.toInt(clickedButton.text); | 192 int digitValue = ToInt(clickedButton.text); |
149 if (display.text() == "0" && digitValue == 0.0) | 193 if (display.text() == "0" && digitValue == 0.0) |
150 return; | 194 return; |
151 | 195 |
152 if (waitingForOperand) { | 196 if (waitingForOperand) { |
153 display.clear(); | 197 display.clear(); |
154 waitingForOperand = false; | 198 waitingForOperand = false; |
155 } | 199 } |
156 display.setText(display.text() ~ Integer.toString(digitValue)); | 200 display.setText(display.text() ~ ToString(digitValue)); |
157 } | 201 } |
158 | 202 |
159 void unaryOperatorClicked() | 203 void unaryOperatorClicked() |
160 { | 204 { |
161 Button clickedButton = cast(Button) signalSender(); | 205 Button clickedButton = cast(Button) signalSender(); |
162 char[] clickedOperator = clickedButton.text(); | 206 string clickedOperator = clickedButton.text(); |
163 double operand = Float.toFloat(display.text); | 207 double operand = ToFloat(display.text); |
164 double result = 0.0; | 208 double result = 0.0; |
165 | 209 |
166 if (clickedOperator == tr("Sqrt")) { | 210 if (clickedOperator == tr("Sqrt")) { |
167 if (operand < 0.0) { | 211 if (operand < 0.0) { |
168 abortOperation(); | 212 abortOperation(); |
176 abortOperation(); | 220 abortOperation(); |
177 return; | 221 return; |
178 } | 222 } |
179 result = 1.0 / operand; | 223 result = 1.0 / operand; |
180 } | 224 } |
181 display.setText(Float.toString(result, 4)); | 225 display.setText(ToString(result, 4)); |
182 waitingForOperand = true; | 226 waitingForOperand = true; |
183 } | 227 } |
184 | 228 |
185 void additiveOperatorClicked() | 229 void additiveOperatorClicked() |
186 { | 230 { |
187 Button clickedButton = cast(Button) signalSender(); | 231 Button clickedButton = cast(Button) signalSender(); |
188 char[] clickedOperator = clickedButton.text(); | 232 string clickedOperator = clickedButton.text(); |
189 double operand = Float.toFloat(display.text); | 233 double operand = ToFloat(display.text); |
190 | 234 |
191 if (pendingMultiplicativeOperator.length) { | 235 if (pendingMultiplicativeOperator.length) { |
192 if (!calculate(operand, pendingMultiplicativeOperator)) { | 236 if (!calculate(operand, pendingMultiplicativeOperator)) { |
193 abortOperation(); | 237 abortOperation(); |
194 return; | 238 return; |
195 } | 239 } |
196 display.setText(Float.toString(factorSoFar, 4)); | 240 display.setText(ToString(factorSoFar, 4)); |
197 operand = factorSoFar; | 241 operand = factorSoFar; |
198 factorSoFar = 0.0; | 242 factorSoFar = 0.0; |
199 pendingMultiplicativeOperator = null; | 243 pendingMultiplicativeOperator = null; |
200 } | 244 } |
201 | 245 |
202 if (pendingAdditiveOperator.length) { | 246 if (pendingAdditiveOperator.length) { |
203 if (!calculate(operand, pendingAdditiveOperator)) { | 247 if (!calculate(operand, pendingAdditiveOperator)) { |
204 abortOperation(); | 248 abortOperation(); |
205 return; | 249 return; |
206 } | 250 } |
207 display.setText(Float.toString(sumSoFar, 4)); | 251 display.setText(ToString(sumSoFar, 4)); |
208 } else { | 252 } else { |
209 sumSoFar = operand; | 253 sumSoFar = operand; |
210 } | 254 } |
211 | 255 |
212 pendingAdditiveOperator = clickedOperator; | 256 pendingAdditiveOperator = clickedOperator; |
214 } | 258 } |
215 | 259 |
216 void multiplicativeOperatorClicked() | 260 void multiplicativeOperatorClicked() |
217 { | 261 { |
218 Button clickedButton = cast(Button) signalSender(); | 262 Button clickedButton = cast(Button) signalSender(); |
219 char[] clickedOperator = clickedButton.text(); | 263 string clickedOperator = clickedButton.text(); |
220 double operand = Float.toFloat(display.text); | 264 double operand = ToFloat(display.text); |
221 | 265 |
222 if (pendingMultiplicativeOperator.length) { | 266 if (pendingMultiplicativeOperator.length) { |
223 if (!calculate(operand, pendingMultiplicativeOperator)) { | 267 if (!calculate(operand, pendingMultiplicativeOperator)) { |
224 abortOperation(); | 268 abortOperation(); |
225 return; | 269 return; |
226 } | 270 } |
227 display.setText(Float.toString(factorSoFar, 4)); | 271 display.setText(ToString(factorSoFar/*, 4*/)); |
228 } else { | 272 } else { |
229 factorSoFar = operand; | 273 factorSoFar = operand; |
230 } | 274 } |
231 | 275 |
232 pendingMultiplicativeOperator = clickedOperator; | 276 pendingMultiplicativeOperator = clickedOperator; |
233 waitingForOperand = true; | 277 waitingForOperand = true; |
234 } | 278 } |
235 | 279 |
236 void equalClicked() | 280 void equalClicked() |
237 { | 281 { |
238 double operand = Float.toFloat(display.text); | 282 double operand = ToFloat(display.text); |
239 | 283 |
240 if (pendingMultiplicativeOperator.length) { | 284 if (pendingMultiplicativeOperator.length) { |
241 if (!calculate(operand, pendingMultiplicativeOperator)) { | 285 if (!calculate(operand, pendingMultiplicativeOperator)) { |
242 abortOperation(); | 286 abortOperation(); |
243 return; | 287 return; |
254 pendingAdditiveOperator = null; | 298 pendingAdditiveOperator = null; |
255 } else { | 299 } else { |
256 sumSoFar = operand; | 300 sumSoFar = operand; |
257 } | 301 } |
258 | 302 |
259 display.setText(Float.toString(sumSoFar, 4)); | 303 display.setText(ToString(sumSoFar, 4)); |
260 sumSoFar = 0.0; | 304 sumSoFar = 0.0; |
261 waitingForOperand = true; | 305 waitingForOperand = true; |
262 } | 306 } |
263 | 307 |
264 void pointClicked() | 308 void pointClicked() |
265 { | 309 { |
266 char[] text = display.text; | 310 string text = display.text; |
267 | 311 |
268 if (waitingForOperand) | 312 if (waitingForOperand) |
269 display.setText("0"); | 313 display.setText("0"); |
270 | 314 |
271 if (find(text, '.') >= text.length) | 315 if (find(text, '.') >= text.length) |
274 waitingForOperand = false; | 318 waitingForOperand = false; |
275 } | 319 } |
276 | 320 |
277 void changeSignClicked() | 321 void changeSignClicked() |
278 { | 322 { |
279 char[] text = display.text(); | 323 string text = display.text(); |
280 double value = Float.toFloat(text); | 324 double value = ToFloat(text); |
281 | 325 |
282 if (value > 0.0) { | 326 if (value > 0.0) { |
283 text = "-" ~ text; | 327 text = "-" ~ text; |
284 } else if (value < 0.0) { | 328 } else if (value < 0.0) { |
285 text = text[1..$]; | 329 text = text[1..$]; |
290 void backspaceClicked() | 334 void backspaceClicked() |
291 { | 335 { |
292 if (waitingForOperand) | 336 if (waitingForOperand) |
293 return; | 337 return; |
294 | 338 |
295 char[] text = display.text(); | 339 string text = display.text(); |
296 text = text[0..$-1]; | 340 text = text[0..$-1]; |
297 if (text.length == 0) { | 341 if (text.length == 0) { |
298 text = "0"; | 342 text = "0"; |
299 waitingForOperand = true; | 343 waitingForOperand = true; |
300 } | 344 } |
326 sumInMemory = 0.0; | 370 sumInMemory = 0.0; |
327 } | 371 } |
328 | 372 |
329 void readMemory() | 373 void readMemory() |
330 { | 374 { |
331 display.setText(Float.toString(sumInMemory, 4)); | 375 display.setText(ToString(sumInMemory, 4)); |
332 waitingForOperand = true; | 376 waitingForOperand = true; |
333 } | 377 } |
334 | 378 |
335 void setMemory() | 379 void setMemory() |
336 { | 380 { |
337 equalClicked(); | 381 equalClicked(); |
338 sumInMemory = Float.toFloat(display.text); | 382 sumInMemory = ToFloat(display.text); |
339 } | 383 } |
340 | 384 |
341 void addToMemory() | 385 void addToMemory() |
342 { | 386 { |
343 equalClicked(); | 387 equalClicked(); |
344 sumInMemory += Float.toFloat(display.text); | 388 sumInMemory += ToFloat(display.text); |
345 } | 389 } |
346 | 390 |
347 private: | 391 private: |
348 | 392 |
349 Button createButton(char[] text, void delegate() member) | 393 Button createButton(string text, void delegate() member) |
350 { | 394 { |
351 Button button = new Button(text); | 395 Button button = new Button(text); |
352 button.clicked.connect(member); | 396 button.clicked.connect(member); |
353 return button; | 397 return button; |
354 } | 398 } |
357 { | 401 { |
358 clearAll(); | 402 clearAll(); |
359 display.setText(tr("####")); | 403 display.setText(tr("####")); |
360 } | 404 } |
361 | 405 |
362 bool calculate(double rightOperand, char[] pendingOperator) | 406 bool calculate(double rightOperand, string pendingOperator) |
363 { | 407 { |
364 if (pendingOperator == tr("+")) { | 408 if (pendingOperator == tr("+")) { |
365 sumSoFar += rightOperand; | 409 sumSoFar += rightOperand; |
366 } else if (pendingOperator == tr("-")) { | 410 } else if (pendingOperator == tr("-")) { |
367 sumSoFar -= rightOperand; | 411 sumSoFar -= rightOperand; |
376 } | 420 } |
377 | 421 |
378 double sumInMemory; | 422 double sumInMemory; |
379 double sumSoFar; | 423 double sumSoFar; |
380 double factorSoFar; | 424 double factorSoFar; |
381 char[] pendingAdditiveOperator; | 425 string pendingAdditiveOperator; |
382 char[] pendingMultiplicativeOperator; | 426 string pendingMultiplicativeOperator; |
383 bool waitingForOperand; | 427 bool waitingForOperand; |
384 | 428 |
385 QLineEdit display; | 429 QLineEdit display; |
386 | 430 |
387 enum { NumDigitButtons = 10 }; | 431 enum { NumDigitButtons = 10 }; |