comparison examples/widgets/calculator/calculator.d @ 215:8aaa84d48451

Improve examples.
author SokoL_SD
date Tue, 14 Jul 2009 15:25:45 +0000
parents 7ea67ec3cf29
children 256ab6cb8e85
comparison
equal deleted inserted replaced
214:11f1760d1700 215:8aaa84d48451
45 import qt.gui.QDialog; 45 import qt.gui.QDialog;
46 import qt.gui.QGridLayout; 46 import qt.gui.QGridLayout;
47 import qt.gui.QLineEdit; 47 import qt.gui.QLineEdit;
48 import qt.gui.QFont; 48 import qt.gui.QFont;
49 49
50 version(Tango) 50 import std.math;
51 { 51 import std.conv;
52 import tango.math.Math; 52 import std.string;
53 import tango.math.Math : pow, sqrt;
54 import Float = tango.text.convert.Float;
55 import Integer = tango.text.convert.Integer;
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 }
100 53
101 class Calculator : public QDialog 54 class Calculator : public QDialog
102 { 55 {
103 56
104 public: 57 public:
120 auto font = new QFont(display.font()); 73 auto font = new QFont(display.font());
121 font.setPointSize(font.pointSize() + 8); 74 font.setPointSize(font.pointSize() + 8);
122 display.setFont(font); 75 display.setFont(font);
123 76
124 for (int i = 0; i < NumDigitButtons; ++i) { 77 for (int i = 0; i < NumDigitButtons; ++i) {
125 digitButtons[i] = createButton(ToString(i), &digitClicked); 78 digitButtons[i] = createButton(format("%d", i), &digitClicked);
126 } 79 }
127 80
128 Button pointButton = createButton(tr("."), &pointClicked); 81 Button pointButton = createButton(tr("."), &pointClicked);
129 Button changeSignButton = createButton(tr("+/-"), &changeSignClicked); 82 Button changeSignButton = createButton(tr("+/-"), &changeSignClicked);
130 83
187 140
188 //private slots: 141 //private slots:
189 void digitClicked() 142 void digitClicked()
190 { 143 {
191 Button clickedButton = cast(Button) signalSender(); 144 Button clickedButton = cast(Button) signalSender();
192 int digitValue = ToInt(clickedButton.text); 145 int digitValue = to!int(clickedButton.text);
193 if (display.text() == "0" && digitValue == 0.0) 146 if (display.text() == "0" && digitValue == 0.0)
194 return; 147 return;
195 148
196 if (waitingForOperand) { 149 if (waitingForOperand) {
197 display.clear(); 150 display.clear();
198 waitingForOperand = false; 151 waitingForOperand = false;
199 } 152 }
200 display.setText(display.text() ~ ToString(digitValue)); 153 display.setText(display.text() ~ format("%g", digitValue));
201 } 154 }
202 155
203 void unaryOperatorClicked() 156 void unaryOperatorClicked()
204 { 157 {
205 Button clickedButton = cast(Button) signalSender(); 158 Button clickedButton = cast(Button) signalSender();
206 string clickedOperator = clickedButton.text(); 159 string clickedOperator = clickedButton.text();
207 double operand = ToFloat(display.text); 160 double operand = to!float(display.text);
208 double result = 0.0; 161 double result = 0.0;
209 162
210 if (clickedOperator == tr("Sqrt")) { 163 if (clickedOperator == tr("Sqrt")) {
211 if (operand < 0.0) { 164 if (operand < 0.0) {
212 abortOperation(); 165 abortOperation();
220 abortOperation(); 173 abortOperation();
221 return; 174 return;
222 } 175 }
223 result = 1.0 / operand; 176 result = 1.0 / operand;
224 } 177 }
225 display.setText(ToString(result, 4)); 178 display.setText(format("%g", result));
226 waitingForOperand = true; 179 waitingForOperand = true;
227 } 180 }
228 181
229 void additiveOperatorClicked() 182 void additiveOperatorClicked()
230 { 183 {
231 Button clickedButton = cast(Button) signalSender(); 184 Button clickedButton = cast(Button) signalSender();
232 string clickedOperator = clickedButton.text(); 185 string clickedOperator = clickedButton.text();
233 double operand = ToFloat(display.text); 186 double operand = to!float(display.text);
234 187
235 if (pendingMultiplicativeOperator.length) { 188 if (pendingMultiplicativeOperator.length) {
236 if (!calculate(operand, pendingMultiplicativeOperator)) { 189 if (!calculate(operand, pendingMultiplicativeOperator)) {
237 abortOperation(); 190 abortOperation();
238 return; 191 return;
239 } 192 }
240 display.setText(ToString(factorSoFar, 4)); 193 display.setText(format("%g", factorSoFar));
241 operand = factorSoFar; 194 operand = factorSoFar;
242 factorSoFar = 0.0; 195 factorSoFar = 0.0;
243 pendingMultiplicativeOperator = null; 196 pendingMultiplicativeOperator = null;
244 } 197 }
245 198
246 if (pendingAdditiveOperator.length) { 199 if (pendingAdditiveOperator.length) {
247 if (!calculate(operand, pendingAdditiveOperator)) { 200 if (!calculate(operand, pendingAdditiveOperator)) {
248 abortOperation(); 201 abortOperation();
249 return; 202 return;
250 } 203 }
251 display.setText(ToString(sumSoFar, 4)); 204 display.setText(format("%g", sumSoFar));
252 } else { 205 } else {
253 sumSoFar = operand; 206 sumSoFar = operand;
254 } 207 }
255 208
256 pendingAdditiveOperator = clickedOperator; 209 pendingAdditiveOperator = clickedOperator;
259 212
260 void multiplicativeOperatorClicked() 213 void multiplicativeOperatorClicked()
261 { 214 {
262 Button clickedButton = cast(Button) signalSender(); 215 Button clickedButton = cast(Button) signalSender();
263 string clickedOperator = clickedButton.text(); 216 string clickedOperator = clickedButton.text();
264 double operand = ToFloat(display.text); 217 double operand = to!float(display.text);
265 218
266 if (pendingMultiplicativeOperator.length) { 219 if (pendingMultiplicativeOperator.length) {
267 if (!calculate(operand, pendingMultiplicativeOperator)) { 220 if (!calculate(operand, pendingMultiplicativeOperator)) {
268 abortOperation(); 221 abortOperation();
269 return; 222 return;
270 } 223 }
271 display.setText(ToString(factorSoFar/*, 4*/)); 224 display.setText(format("%g", factorSoFar));
272 } else { 225 } else {
273 factorSoFar = operand; 226 factorSoFar = operand;
274 } 227 }
275 228
276 pendingMultiplicativeOperator = clickedOperator; 229 pendingMultiplicativeOperator = clickedOperator;
277 waitingForOperand = true; 230 waitingForOperand = true;
278 } 231 }
279 232
280 void equalClicked() 233 void equalClicked()
281 { 234 {
282 double operand = ToFloat(display.text); 235 double operand = to!float(display.text);
283 236
284 if (pendingMultiplicativeOperator.length) { 237 if (pendingMultiplicativeOperator.length) {
285 if (!calculate(operand, pendingMultiplicativeOperator)) { 238 if (!calculate(operand, pendingMultiplicativeOperator)) {
286 abortOperation(); 239 abortOperation();
287 return; 240 return;
298 pendingAdditiveOperator = null; 251 pendingAdditiveOperator = null;
299 } else { 252 } else {
300 sumSoFar = operand; 253 sumSoFar = operand;
301 } 254 }
302 255
303 display.setText(ToString(sumSoFar, 4)); 256 display.setText(format("%g", sumSoFar));
304 sumSoFar = 0.0; 257 sumSoFar = 0.0;
305 waitingForOperand = true; 258 waitingForOperand = true;
306 } 259 }
307 260
308 void pointClicked() 261 void pointClicked()
310 string text = display.text; 263 string text = display.text;
311 264
312 if (waitingForOperand) 265 if (waitingForOperand)
313 display.setText("0"); 266 display.setText("0");
314 267
315 if (find(text, '.') >= text.length) 268 if (indexOf(text, '.') >= text.length)
316 display.setText(text ~ tr(".")); 269 display.setText(text ~ tr("."));
317 270
318 waitingForOperand = false; 271 waitingForOperand = false;
319 } 272 }
320 273
321 void changeSignClicked() 274 void changeSignClicked()
322 { 275 {
323 string text = display.text(); 276 string text = display.text();
324 double value = ToFloat(text); 277 double value = to!float(text);
325 278
326 if (value > 0.0) { 279 if (value > 0.0) {
327 text = "-" ~ text; 280 text = "-" ~ text;
328 } else if (value < 0.0) { 281 } else if (value < 0.0) {
329 text = text[1..$]; 282 text = text[1..$];
370 sumInMemory = 0.0; 323 sumInMemory = 0.0;
371 } 324 }
372 325
373 void readMemory() 326 void readMemory()
374 { 327 {
375 display.setText(ToString(sumInMemory, 4)); 328 display.setText(format("%g", sumInMemory));
376 waitingForOperand = true; 329 waitingForOperand = true;
377 } 330 }
378 331
379 void setMemory() 332 void setMemory()
380 { 333 {
381 equalClicked(); 334 equalClicked();
382 sumInMemory = ToFloat(display.text); 335 sumInMemory = to!float(display.text);
383 } 336 }
384 337
385 void addToMemory() 338 void addToMemory()
386 { 339 {
387 equalClicked(); 340 equalClicked();
388 sumInMemory += ToFloat(display.text); 341 sumInMemory += to!float(display.text);
389 } 342 }
390 343
391 private: 344 private:
392 345
393 Button createButton(string text, void delegate() member) 346 Button createButton(string text, void delegate() member)