Mercurial > projects > qtd
annotate examples/widgets/calculator/calculator.d @ 350:31520b2c0b3c
Removed dependency on parent trait and stringof
author | Max Samukha <maxter@spambox.com> |
---|---|
date | Thu, 20 May 2010 15:49:08 +0300 |
parents | 7a3c43424dca |
children |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | |
4 ** Contact: Qt Software Information (qt-info@nokia.com) | |
5 ** | |
6 ** This file is part of the example classes of the Qt Toolkit. | |
7 ** | |
8 ** $QT_BEGIN_LICENSE:LGPL$ | |
9 ** Commercial Usage | |
10 ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 ** accordance with the Qt Commercial License Agreement provided with the | |
12 ** Software or, alternatively, in accordance with the terms contained in | |
13 ** a written agreement between you and Nokia. | |
14 ** | |
15 ** GNU Lesser General Public License Usage | |
16 ** Alternatively, this file may be used under the terms of the GNU Lesser | |
17 ** General Public License version 2.1 as published by the Free Software | |
18 ** Foundation and appearing in the file LICENSE.LGPL included in the | |
19 ** packaging of this file. Please review the following information to | |
20 ** ensure the GNU Lesser General Public License version 2.1 requirements | |
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | |
22 ** | |
23 ** In addition, as a special exception, Nokia gives you certain | |
24 ** additional rights. These rights are described in the Nokia Qt LGPL | |
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | |
26 ** package. | |
27 ** | |
28 ** GNU General Public License Usage | |
29 ** Alternatively, this file may be used under the terms of the GNU | |
30 ** General Public License version 3.0 as published by the Free Software | |
31 ** Foundation and appearing in the file LICENSE.GPL included in the | |
32 ** packaging of this file. Please review the following information to | |
33 ** ensure the GNU General Public License version 3.0 requirements will be | |
34 ** met: http://www.gnu.org/copyleft/gpl.html. | |
35 ** | |
36 ** If you are unsure which license is appropriate for your use, please | |
37 ** contact the sales department at qt-sales@nokia.com. | |
38 ** $QT_END_LICENSE$ | |
39 ** | |
40 ****************************************************************************/ | |
41 | |
42 module calculator; | |
43 | |
210 | 44 import button; |
1 | 45 import qt.gui.QDialog; |
46 import qt.gui.QGridLayout; | |
47 import qt.gui.QLineEdit; | |
48 import qt.gui.QFont; | |
49 | |
215 | 50 import std.math; |
51 import std.conv; | |
350
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
52 import std.string : format, indexOf; |
1 | 53 |
54 class Calculator : public QDialog | |
55 { | |
350
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
56 |
1 | 57 public: |
58 this(QWidget parent = null) | |
59 { | |
60 super(parent); | |
61 | |
62 sumInMemory = 0.0; | |
63 sumSoFar = 0.0; | |
64 factorSoFar = 0.0; | |
65 waitingForOperand = true; | |
66 | |
67 display = new QLineEdit("0"); | |
68 display.setReadOnly(true); | |
69 display.setAlignment(Qt.AlignRight); | |
70 display.setMaxLength(15); | |
71 | |
350
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
72 auto font = new QFont(display.font()); |
1 | 73 font.setPointSize(font.pointSize() + 8); |
74 display.setFont(font); | |
75 | |
76 for (int i = 0; i < NumDigitButtons; ++i) { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
77 digitButtons[i] = createButton(format("%d", i), "digitClicked"); |
1 | 78 } |
79 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
80 Button pointButton = createButton(tr("."), "pointClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
81 Button changeSignButton = createButton(tr("+/-"), "changeSignClicked"); |
1 | 82 |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
83 Button backspaceButton = createButton(tr("Backspace"), "backspaceClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
84 Button clearButton = createButton(tr("Clear"), "clear"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
85 Button clearAllButton = createButton(tr("Clear All"), "clearAll"); |
1 | 86 |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
87 Button clearMemoryButton = createButton(tr("MC"), "clearMemory"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
88 Button readMemoryButton = createButton(tr("MR"), "readMemory"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
89 Button setMemoryButton = createButton(tr("MS"), "setMemory"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
90 Button addToMemoryButton = createButton(tr("M+"), "addToMemory"); |
1 | 91 |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
92 Button divisionButton = createButton(tr("/"), "multiplicativeOperatorClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
93 Button timesButton = createButton(tr("*"), "multiplicativeOperatorClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
94 Button minusButton = createButton(tr("-"), "additiveOperatorClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
95 Button plusButton = createButton(tr("+"), "additiveOperatorClicked"); |
1 | 96 |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
97 Button squareRootButton = createButton(tr("Sqrt"), "unaryOperatorClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
98 Button powerButton = createButton(tr("x^2"), "unaryOperatorClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
99 Button reciprocalButton = createButton(tr("1/x"), "unaryOperatorClicked"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
100 Button equalButton = createButton(tr("="), "equalClicked"); |
1 | 101 |
102 QGridLayout mainLayout = new QGridLayout(); | |
103 | |
104 mainLayout.setSizeConstraint(QLayout.SetFixedSize); | |
105 | |
106 mainLayout.addWidget(display, 0, 0, 1, 6); | |
107 mainLayout.addWidget(backspaceButton, 1, 0, 1, 2); | |
108 mainLayout.addWidget(clearButton, 1, 2, 1, 2); | |
109 mainLayout.addWidget(clearAllButton, 1, 4, 1, 2); | |
110 | |
111 mainLayout.addWidget(clearMemoryButton, 2, 0); | |
112 mainLayout.addWidget(readMemoryButton, 3, 0); | |
113 mainLayout.addWidget(setMemoryButton, 4, 0); | |
114 mainLayout.addWidget(addToMemoryButton, 5, 0); | |
115 | |
116 for (int i = 1; i < NumDigitButtons; ++i) { | |
117 int row = ((9 - i) / 3) + 2; | |
118 int column = ((i - 1) % 3) + 1; | |
119 mainLayout.addWidget(digitButtons[i], row, column); | |
120 } | |
121 | |
122 mainLayout.addWidget(digitButtons[0], 5, 1); | |
123 mainLayout.addWidget(pointButton, 5, 2); | |
124 mainLayout.addWidget(changeSignButton, 5, 3); | |
125 | |
126 mainLayout.addWidget(divisionButton, 2, 4); | |
127 mainLayout.addWidget(timesButton, 3, 4); | |
128 mainLayout.addWidget(minusButton, 4, 4); | |
129 mainLayout.addWidget(plusButton, 5, 4); | |
130 | |
131 mainLayout.addWidget(squareRootButton, 2, 5); | |
132 mainLayout.addWidget(powerButton, 3, 5); | |
133 mainLayout.addWidget(reciprocalButton, 4, 5); | |
134 mainLayout.addWidget(equalButton, 5, 5); | |
135 setLayout(mainLayout); | |
136 | |
137 setWindowTitle(tr("Calculator")); | |
138 } | |
139 | |
140 //private slots: | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
141 void slot_digitClicked() |
1 | 142 { |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
143 Button clickedButton = cast(Button) sender(); |
215 | 144 int digitValue = to!int(clickedButton.text); |
1 | 145 if (display.text() == "0" && digitValue == 0.0) |
146 return; | |
147 | |
148 if (waitingForOperand) { | |
149 display.clear(); | |
150 waitingForOperand = false; | |
151 } | |
350
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
152 |
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
153 display.setText(display.text() ~ format("%s", digitValue)); |
1 | 154 } |
155 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
156 void slot_unaryOperatorClicked() |
1 | 157 { |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
158 Button clickedButton = cast(Button) sender(); |
210 | 159 string clickedOperator = clickedButton.text(); |
215 | 160 double operand = to!float(display.text); |
1 | 161 double result = 0.0; |
162 | |
163 if (clickedOperator == tr("Sqrt")) { | |
164 if (operand < 0.0) { | |
165 abortOperation(); | |
166 return; | |
167 } | |
168 result = sqrt(operand); | |
169 } else if (clickedOperator == tr("x^2")) { | |
170 result = pow(operand, 2.0); | |
171 } else if (clickedOperator == tr("1/x")) { | |
172 if (operand == 0.0) { | |
173 abortOperation(); | |
174 return; | |
175 } | |
176 result = 1.0 / operand; | |
177 } | |
215 | 178 display.setText(format("%g", result)); |
1 | 179 waitingForOperand = true; |
180 } | |
181 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
182 void slot_additiveOperatorClicked() |
1 | 183 { |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
184 Button clickedButton = cast(Button) sender(); |
210 | 185 string clickedOperator = clickedButton.text(); |
215 | 186 double operand = to!float(display.text); |
1 | 187 |
188 if (pendingMultiplicativeOperator.length) { | |
189 if (!calculate(operand, pendingMultiplicativeOperator)) { | |
190 abortOperation(); | |
191 return; | |
192 } | |
215 | 193 display.setText(format("%g", factorSoFar)); |
1 | 194 operand = factorSoFar; |
195 factorSoFar = 0.0; | |
196 pendingMultiplicativeOperator = null; | |
197 } | |
198 | |
199 if (pendingAdditiveOperator.length) { | |
200 if (!calculate(operand, pendingAdditiveOperator)) { | |
201 abortOperation(); | |
202 return; | |
203 } | |
215 | 204 display.setText(format("%g", sumSoFar)); |
1 | 205 } else { |
206 sumSoFar = operand; | |
207 } | |
208 | |
209 pendingAdditiveOperator = clickedOperator; | |
210 waitingForOperand = true; | |
211 } | |
212 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
213 void slot_multiplicativeOperatorClicked() |
1 | 214 { |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
215 Button clickedButton = cast(Button) sender(); |
210 | 216 string clickedOperator = clickedButton.text(); |
215 | 217 double operand = to!float(display.text); |
1 | 218 |
219 if (pendingMultiplicativeOperator.length) { | |
220 if (!calculate(operand, pendingMultiplicativeOperator)) { | |
221 abortOperation(); | |
222 return; | |
223 } | |
215 | 224 display.setText(format("%g", factorSoFar)); |
1 | 225 } else { |
226 factorSoFar = operand; | |
227 } | |
228 | |
229 pendingMultiplicativeOperator = clickedOperator; | |
230 waitingForOperand = true; | |
231 } | |
232 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
233 void slot_equalClicked() |
1 | 234 { |
215 | 235 double operand = to!float(display.text); |
1 | 236 |
237 if (pendingMultiplicativeOperator.length) { | |
238 if (!calculate(operand, pendingMultiplicativeOperator)) { | |
239 abortOperation(); | |
240 return; | |
241 } | |
242 operand = factorSoFar; | |
243 factorSoFar = 0.0; | |
244 pendingMultiplicativeOperator = null; | |
245 } | |
246 if (pendingAdditiveOperator.length) { | |
247 if (!calculate(operand, pendingAdditiveOperator)) { | |
248 abortOperation(); | |
249 return; | |
250 } | |
251 pendingAdditiveOperator = null; | |
252 } else { | |
253 sumSoFar = operand; | |
254 } | |
255 | |
215 | 256 display.setText(format("%g", sumSoFar)); |
1 | 257 sumSoFar = 0.0; |
258 waitingForOperand = true; | |
259 } | |
260 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
261 void slot_pointClicked() |
1 | 262 { |
210 | 263 string text = display.text; |
1 | 264 |
265 if (waitingForOperand) | |
266 display.setText("0"); | |
267 | |
215 | 268 if (indexOf(text, '.') >= text.length) |
1 | 269 display.setText(text ~ tr(".")); |
350
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
270 |
1 | 271 waitingForOperand = false; |
272 } | |
273 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
274 void slot_changeSignClicked() |
1 | 275 { |
210 | 276 string text = display.text(); |
215 | 277 double value = to!float(text); |
1 | 278 |
279 if (value > 0.0) { | |
280 text = "-" ~ text; | |
281 } else if (value < 0.0) { | |
282 text = text[1..$]; | |
283 } | |
284 display.setText(text); | |
285 } | |
286 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
287 void slot_backspaceClicked() |
1 | 288 { |
289 if (waitingForOperand) | |
290 return; | |
291 | |
210 | 292 string text = display.text(); |
1 | 293 text = text[0..$-1]; |
294 if (text.length == 0) { | |
295 text = "0"; | |
296 waitingForOperand = true; | |
297 } | |
298 display.setText(text); | |
299 } | |
300 | |
301 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
302 void slot_clear() |
1 | 303 { |
304 if (waitingForOperand) | |
305 return; | |
306 | |
307 display.setText("0"); | |
308 waitingForOperand = true; | |
309 } | |
310 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
311 void slot_clearAll() |
1 | 312 { |
313 sumSoFar = 0.0; | |
314 factorSoFar = 0.0; | |
315 pendingAdditiveOperator = null; | |
316 pendingMultiplicativeOperator = null; | |
317 display.setText("0"); | |
318 waitingForOperand = true; | |
319 } | |
320 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
321 void slot_clearMemory() |
1 | 322 { |
323 sumInMemory = 0.0; | |
324 } | |
325 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
326 void slot_readMemory() |
1 | 327 { |
215 | 328 display.setText(format("%g", sumInMemory)); |
1 | 329 waitingForOperand = true; |
330 } | |
331 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
332 void slot_setMemory() |
1 | 333 { |
334 equalClicked(); | |
215 | 335 sumInMemory = to!float(display.text); |
1 | 336 } |
337 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
338 void slot_addToMemory() |
1 | 339 { |
340 equalClicked(); | |
215 | 341 sumInMemory += to!float(display.text); |
1 | 342 } |
343 | |
344 private: | |
345 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
346 Button createButton(string text, string member) |
1 | 347 { |
348 Button button = new Button(text); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
349 connect(button, "clicked", this, member); |
1 | 350 return button; |
351 } | |
352 | |
353 void abortOperation() | |
354 { | |
355 clearAll(); | |
356 display.setText(tr("####")); | |
357 } | |
358 | |
210 | 359 bool calculate(double rightOperand, string pendingOperator) |
1 | 360 { |
361 if (pendingOperator == tr("+")) { | |
362 sumSoFar += rightOperand; | |
363 } else if (pendingOperator == tr("-")) { | |
364 sumSoFar -= rightOperand; | |
365 } else if (pendingOperator == tr("*")) { | |
366 factorSoFar *= rightOperand; | |
367 } else if (pendingOperator == tr("/")) { | |
368 if (rightOperand == 0.0) | |
369 return false; | |
370 factorSoFar /= rightOperand; | |
371 } | |
372 return true; | |
373 } | |
374 | |
375 double sumInMemory; | |
376 double sumSoFar; | |
377 double factorSoFar; | |
210 | 378 string pendingAdditiveOperator; |
379 string pendingMultiplicativeOperator; | |
1 | 380 bool waitingForOperand; |
381 | |
382 QLineEdit display; | |
383 | |
384 enum { NumDigitButtons = 10 }; | |
385 Button[NumDigitButtons] digitButtons; | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
386 |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
387 mixin Q_OBJECT; |
1 | 388 } |