author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include <math.h> |
|
44 |
||
45 |
#include "button.h" |
|
46 |
#include "calculator.h" |
|
47 |
||
48 |
//! [0] |
|
49 |
Calculator::Calculator(QWidget *parent) |
|
50 |
: QDialog(parent) |
|
51 |
{ |
|
52 |
sumInMemory = 0.0; |
|
53 |
sumSoFar = 0.0; |
|
54 |
factorSoFar = 0.0; |
|
55 |
waitingForOperand = true; |
|
56 |
//! [0] |
|
57 |
||
58 |
//! [1] |
|
59 |
display = new QLineEdit("0"); |
|
60 |
//! [1] //! [2] |
|
61 |
display->setReadOnly(true); |
|
62 |
display->setAlignment(Qt::AlignRight); |
|
63 |
display->setMaxLength(15); |
|
64 |
||
65 |
QFont font = display->font(); |
|
66 |
font.setPointSize(font.pointSize() + 8); |
|
67 |
display->setFont(font); |
|
68 |
//! [2] |
|
69 |
||
70 |
//! [4] |
|
71 |
for (int i = 0; i < NumDigitButtons; ++i) { |
|
72 |
digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked())); |
|
73 |
} |
|
74 |
||
75 |
Button *pointButton = createButton(tr("."), SLOT(pointClicked())); |
|
76 |
Button *changeSignButton = createButton(tr("\261"), SLOT(changeSignClicked())); |
|
77 |
||
78 |
Button *backspaceButton = createButton(tr("Backspace"), SLOT(backspaceClicked())); |
|
79 |
Button *clearButton = createButton(tr("Clear"), SLOT(clear())); |
|
80 |
Button *clearAllButton = createButton(tr("Clear All"), SLOT(clearAll())); |
|
81 |
||
82 |
Button *clearMemoryButton = createButton(tr("MC"), SLOT(clearMemory())); |
|
83 |
Button *readMemoryButton = createButton(tr("MR"), SLOT(readMemory())); |
|
84 |
Button *setMemoryButton = createButton(tr("MS"), SLOT(setMemory())); |
|
85 |
Button *addToMemoryButton = createButton(tr("M+"), SLOT(addToMemory())); |
|
86 |
||
87 |
Button *divisionButton = createButton(tr("\367"), SLOT(multiplicativeOperatorClicked())); |
|
88 |
Button *timesButton = createButton(tr("\327"), SLOT(multiplicativeOperatorClicked())); |
|
89 |
Button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked())); |
|
90 |
Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked())); |
|
91 |
||
92 |
Button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked())); |
|
93 |
Button *powerButton = createButton(tr("x\262"), SLOT(unaryOperatorClicked())); |
|
94 |
Button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked())); |
|
95 |
Button *equalButton = createButton(tr("="), SLOT(equalClicked())); |
|
96 |
//! [4] |
|
97 |
||
98 |
//! [5] |
|
99 |
QGridLayout *mainLayout = new QGridLayout; |
|
100 |
//! [5] //! [6] |
|
101 |
mainLayout->setSizeConstraint(QLayout::SetFixedSize); |
|
102 |
||
103 |
mainLayout->addWidget(display, 0, 0, 1, 6); |
|
104 |
mainLayout->addWidget(backspaceButton, 1, 0, 1, 2); |
|
105 |
mainLayout->addWidget(clearButton, 1, 2, 1, 2); |
|
106 |
mainLayout->addWidget(clearAllButton, 1, 4, 1, 2); |
|
107 |
||
108 |
mainLayout->addWidget(clearMemoryButton, 2, 0); |
|
109 |
mainLayout->addWidget(readMemoryButton, 3, 0); |
|
110 |
mainLayout->addWidget(setMemoryButton, 4, 0); |
|
111 |
mainLayout->addWidget(addToMemoryButton, 5, 0); |
|
112 |
||
113 |
for (int i = 1; i < NumDigitButtons; ++i) { |
|
114 |
int row = ((9 - i) / 3) + 2; |
|
115 |
int column = ((i - 1) % 3) + 1; |
|
116 |
mainLayout->addWidget(digitButtons[i], row, column); |
|
117 |
} |
|
118 |
||
119 |
mainLayout->addWidget(digitButtons[0], 5, 1); |
|
120 |
mainLayout->addWidget(pointButton, 5, 2); |
|
121 |
mainLayout->addWidget(changeSignButton, 5, 3); |
|
122 |
||
123 |
mainLayout->addWidget(divisionButton, 2, 4); |
|
124 |
mainLayout->addWidget(timesButton, 3, 4); |
|
125 |
mainLayout->addWidget(minusButton, 4, 4); |
|
126 |
mainLayout->addWidget(plusButton, 5, 4); |
|
127 |
||
128 |
mainLayout->addWidget(squareRootButton, 2, 5); |
|
129 |
mainLayout->addWidget(powerButton, 3, 5); |
|
130 |
mainLayout->addWidget(reciprocalButton, 4, 5); |
|
131 |
mainLayout->addWidget(equalButton, 5, 5); |
|
132 |
setLayout(mainLayout); |
|
133 |
||
134 |
setWindowTitle(tr("Calculator")); |
|
135 |
} |
|
136 |
//! [6] |
|
137 |
||
138 |
//! [7] |
|
139 |
void Calculator::digitClicked() |
|
140 |
{ |
|
141 |
Button *clickedButton = qobject_cast<Button *>(sender()); |
|
142 |
int digitValue = clickedButton->text().toInt(); |
|
143 |
if (display->text() == "0" && digitValue == 0.0) |
|
144 |
return; |
|
145 |
||
146 |
if (waitingForOperand) { |
|
147 |
display->clear(); |
|
148 |
waitingForOperand = false; |
|
149 |
} |
|
150 |
display->setText(display->text() + QString::number(digitValue)); |
|
151 |
} |
|
152 |
//! [7] |
|
153 |
||
154 |
//! [8] |
|
155 |
void Calculator::unaryOperatorClicked() |
|
156 |
//! [8] //! [9] |
|
157 |
{ |
|
158 |
Button *clickedButton = qobject_cast<Button *>(sender()); |
|
159 |
QString clickedOperator = clickedButton->text(); |
|
160 |
double operand = display->text().toDouble(); |
|
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\262")) { |
|
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 |
} |
|
178 |
display->setText(QString::number(result)); |
|
179 |
waitingForOperand = true; |
|
180 |
} |
|
181 |
//! [9] |
|
182 |
||
183 |
//! [10] |
|
184 |
void Calculator::additiveOperatorClicked() |
|
185 |
//! [10] //! [11] |
|
186 |
{ |
|
187 |
Button *clickedButton = qobject_cast<Button *>(sender()); |
|
188 |
QString clickedOperator = clickedButton->text(); |
|
189 |
double operand = display->text().toDouble(); |
|
190 |
||
191 |
//! [11] //! [12] |
|
192 |
if (!pendingMultiplicativeOperator.isEmpty()) { |
|
193 |
//! [12] //! [13] |
|
194 |
if (!calculate(operand, pendingMultiplicativeOperator)) { |
|
195 |
abortOperation(); |
|
196 |
return; |
|
197 |
} |
|
198 |
display->setText(QString::number(factorSoFar)); |
|
199 |
operand = factorSoFar; |
|
200 |
factorSoFar = 0.0; |
|
201 |
pendingMultiplicativeOperator.clear(); |
|
202 |
} |
|
203 |
||
204 |
//! [13] //! [14] |
|
205 |
if (!pendingAdditiveOperator.isEmpty()) { |
|
206 |
//! [14] //! [15] |
|
207 |
if (!calculate(operand, pendingAdditiveOperator)) { |
|
208 |
abortOperation(); |
|
209 |
return; |
|
210 |
} |
|
211 |
display->setText(QString::number(sumSoFar)); |
|
212 |
} else { |
|
213 |
sumSoFar = operand; |
|
214 |
} |
|
215 |
||
216 |
//! [15] //! [16] |
|
217 |
pendingAdditiveOperator = clickedOperator; |
|
218 |
//! [16] //! [17] |
|
219 |
waitingForOperand = true; |
|
220 |
} |
|
221 |
//! [17] |
|
222 |
||
223 |
//! [18] |
|
224 |
void Calculator::multiplicativeOperatorClicked() |
|
225 |
{ |
|
226 |
Button *clickedButton = qobject_cast<Button *>(sender()); |
|
227 |
QString clickedOperator = clickedButton->text(); |
|
228 |
double operand = display->text().toDouble(); |
|
229 |
||
230 |
if (!pendingMultiplicativeOperator.isEmpty()) { |
|
231 |
if (!calculate(operand, pendingMultiplicativeOperator)) { |
|
232 |
abortOperation(); |
|
233 |
return; |
|
234 |
} |
|
235 |
display->setText(QString::number(factorSoFar)); |
|
236 |
} else { |
|
237 |
factorSoFar = operand; |
|
238 |
} |
|
239 |
||
240 |
pendingMultiplicativeOperator = clickedOperator; |
|
241 |
waitingForOperand = true; |
|
242 |
} |
|
243 |
//! [18] |
|
244 |
||
245 |
//! [20] |
|
246 |
void Calculator::equalClicked() |
|
247 |
{ |
|
248 |
double operand = display->text().toDouble(); |
|
249 |
||
250 |
if (!pendingMultiplicativeOperator.isEmpty()) { |
|
251 |
if (!calculate(operand, pendingMultiplicativeOperator)) { |
|
252 |
abortOperation(); |
|
253 |
return; |
|
254 |
} |
|
255 |
operand = factorSoFar; |
|
256 |
factorSoFar = 0.0; |
|
257 |
pendingMultiplicativeOperator.clear(); |
|
258 |
} |
|
259 |
if (!pendingAdditiveOperator.isEmpty()) { |
|
260 |
if (!calculate(operand, pendingAdditiveOperator)) { |
|
261 |
abortOperation(); |
|
262 |
return; |
|
263 |
} |
|
264 |
pendingAdditiveOperator.clear(); |
|
265 |
} else { |
|
266 |
sumSoFar = operand; |
|
267 |
} |
|
268 |
||
269 |
display->setText(QString::number(sumSoFar)); |
|
270 |
sumSoFar = 0.0; |
|
271 |
waitingForOperand = true; |
|
272 |
} |
|
273 |
//! [20] |
|
274 |
||
275 |
//! [22] |
|
276 |
void Calculator::pointClicked() |
|
277 |
{ |
|
278 |
if (waitingForOperand) |
|
279 |
display->setText("0"); |
|
280 |
if (!display->text().contains(".")) |
|
281 |
display->setText(display->text() + tr(".")); |
|
282 |
waitingForOperand = false; |
|
283 |
} |
|
284 |
//! [22] |
|
285 |
||
286 |
//! [24] |
|
287 |
void Calculator::changeSignClicked() |
|
288 |
{ |
|
289 |
QString text = display->text(); |
|
290 |
double value = text.toDouble(); |
|
291 |
||
292 |
if (value > 0.0) { |
|
293 |
text.prepend(tr("-")); |
|
294 |
} else if (value < 0.0) { |
|
295 |
text.remove(0, 1); |
|
296 |
} |
|
297 |
display->setText(text); |
|
298 |
} |
|
299 |
//! [24] |
|
300 |
||
301 |
//! [26] |
|
302 |
void Calculator::backspaceClicked() |
|
303 |
{ |
|
304 |
if (waitingForOperand) |
|
305 |
return; |
|
306 |
||
307 |
QString text = display->text(); |
|
308 |
text.chop(1); |
|
309 |
if (text.isEmpty()) { |
|
310 |
text = "0"; |
|
311 |
waitingForOperand = true; |
|
312 |
} |
|
313 |
display->setText(text); |
|
314 |
} |
|
315 |
//! [26] |
|
316 |
||
317 |
//! [28] |
|
318 |
void Calculator::clear() |
|
319 |
{ |
|
320 |
if (waitingForOperand) |
|
321 |
return; |
|
322 |
||
323 |
display->setText("0"); |
|
324 |
waitingForOperand = true; |
|
325 |
} |
|
326 |
//! [28] |
|
327 |
||
328 |
//! [30] |
|
329 |
void Calculator::clearAll() |
|
330 |
{ |
|
331 |
sumSoFar = 0.0; |
|
332 |
factorSoFar = 0.0; |
|
333 |
pendingAdditiveOperator.clear(); |
|
334 |
pendingMultiplicativeOperator.clear(); |
|
335 |
display->setText("0"); |
|
336 |
waitingForOperand = true; |
|
337 |
} |
|
338 |
//! [30] |
|
339 |
||
340 |
//! [32] |
|
341 |
void Calculator::clearMemory() |
|
342 |
{ |
|
343 |
sumInMemory = 0.0; |
|
344 |
} |
|
345 |
||
346 |
void Calculator::readMemory() |
|
347 |
{ |
|
348 |
display->setText(QString::number(sumInMemory)); |
|
349 |
waitingForOperand = true; |
|
350 |
} |
|
351 |
||
352 |
void Calculator::setMemory() |
|
353 |
{ |
|
354 |
equalClicked(); |
|
355 |
sumInMemory = display->text().toDouble(); |
|
356 |
} |
|
357 |
||
358 |
void Calculator::addToMemory() |
|
359 |
{ |
|
360 |
equalClicked(); |
|
361 |
sumInMemory += display->text().toDouble(); |
|
362 |
} |
|
363 |
//! [32] |
|
364 |
//! [34] |
|
365 |
Button *Calculator::createButton(const QString &text, const char *member) |
|
366 |
{ |
|
367 |
Button *button = new Button(text); |
|
368 |
connect(button, SIGNAL(clicked()), this, member); |
|
369 |
return button; |
|
370 |
} |
|
371 |
//! [34] |
|
372 |
||
373 |
//! [36] |
|
374 |
void Calculator::abortOperation() |
|
375 |
{ |
|
376 |
clearAll(); |
|
377 |
display->setText(tr("####")); |
|
378 |
} |
|
379 |
//! [36] |
|
380 |
||
381 |
//! [38] |
|
382 |
bool Calculator::calculate(double rightOperand, const QString &pendingOperator) |
|
383 |
{ |
|
384 |
if (pendingOperator == tr("+")) { |
|
385 |
sumSoFar += rightOperand; |
|
386 |
} else if (pendingOperator == tr("-")) { |
|
387 |
sumSoFar -= rightOperand; |
|
388 |
} else if (pendingOperator == tr("\327")) { |
|
389 |
factorSoFar *= rightOperand; |
|
390 |
} else if (pendingOperator == tr("\367")) { |
|
391 |
if (rightOperand == 0.0) |
|
392 |
return false; |
|
393 |
factorSoFar /= rightOperand; |
|
394 |
} |
|
395 |
return true; |
|
396 |
} |
|
397 |
//! [38] |