source: trunk/examples/widgets/calculator/calculator.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 11.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
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**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
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]
49Calculator::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]
139void 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]
155void 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]
184void 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]
224void 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]
246void 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]
276void 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]
287void 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]
302void 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]
318void Calculator::clear()
319{
320 if (waitingForOperand)
321 return;
322
323 display->setText("0");
324 waitingForOperand = true;
325}
326//! [28]
327
328//! [30]
329void 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]
341void Calculator::clearMemory()
342{
343 sumInMemory = 0.0;
344}
345
346void Calculator::readMemory()
347{
348 display->setText(QString::number(sumInMemory));
349 waitingForOperand = true;
350}
351
352void Calculator::setMemory()
353{
354 equalClicked();
355 sumInMemory = display->text().toDouble();
356}
357
358void Calculator::addToMemory()
359{
360 equalClicked();
361 sumInMemory += display->text().toDouble();
362}
363//! [32]
364//! [34]
365Button *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]
374void Calculator::abortOperation()
375{
376 clearAll();
377 display->setText(tr("####"));
378}
379//! [36]
380
381//! [38]
382bool 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]
Note: See TracBrowser for help on using the repository browser.