1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
34
#include "calculator.h"
36
Calculator::Calculator(QWidget *parent)
42
waitingForOperand = true;
44
display = new QLineEdit("0");
45
display->setReadOnly(true);
46
display->setAlignment(Qt::AlignRight);
47
display->setMaxLength(15);
48
display->installEventFilter(this);
50
QFont font = display->font();
51
font.setPointSize(font.pointSize() + 8);
52
display->setFont(font);
54
QColor digitColor(150, 205, 205);
55
QColor backspaceColor(225, 185, 135);
56
QColor memoryColor(100, 155, 155);
57
QColor operatorColor(155, 175, 195);
59
for (int i = 0; i < NumDigitButtons; ++i) {
60
digitButtons[i] = createButton(QString::number(i), digitColor,
61
SLOT(digitClicked()));
64
pointButton = createButton(tr("."), digitColor, SLOT(pointClicked()));
65
changeSignButton = createButton(tr("�"), digitColor, SLOT(changeSignClicked()));
67
backspaceButton = createButton(tr("Backspace"), backspaceColor,
68
SLOT(backspaceClicked()));
69
clearButton = createButton(tr("Clear"), backspaceColor, SLOT(clear()));
70
clearAllButton = createButton(tr("Clear All"), backspaceColor.light(120),
73
clearMemoryButton = createButton(tr("MC"), memoryColor,
75
readMemoryButton = createButton(tr("MR"), memoryColor, SLOT(readMemory()));
76
setMemoryButton = createButton(tr("MS"), memoryColor, SLOT(setMemory()));
77
addToMemoryButton = createButton(tr("M+"), memoryColor,
80
divisionButton = createButton(tr("�"), operatorColor,
81
SLOT(multiplicativeOperatorClicked()));
82
timesButton = createButton(tr("�"), operatorColor,
83
SLOT(multiplicativeOperatorClicked()));
84
minusButton = createButton(tr("-"), operatorColor,
85
SLOT(additiveOperatorClicked()));
86
plusButton = createButton(tr("+"), operatorColor,
87
SLOT(additiveOperatorClicked()));
89
squareRootButton = createButton(tr("Sqrt"), operatorColor,
90
SLOT(unaryOperatorClicked()));
91
powerButton = createButton(tr("x�"), operatorColor,
92
SLOT(unaryOperatorClicked()));
93
reciprocalButton = createButton(tr("1/x"), operatorColor,
94
SLOT(unaryOperatorClicked()));
95
equalButton = createButton(tr("="), operatorColor.light(120),
96
SLOT(equalClicked()));
98
QGridLayout *mainLayout = new QGridLayout;
99
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
101
mainLayout->addWidget(display, 0, 0, 1, 6);
102
mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
103
mainLayout->addWidget(clearButton, 1, 2, 1, 2);
104
mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
106
mainLayout->addWidget(clearMemoryButton, 2, 0);
107
mainLayout->addWidget(readMemoryButton, 3, 0);
108
mainLayout->addWidget(setMemoryButton, 4, 0);
109
mainLayout->addWidget(addToMemoryButton, 5, 0);
111
for (int i = 1; i < NumDigitButtons; ++i) {
112
int row = ((9 - i) / 3) + 2;
113
int column = ((i - 1) % 3) + 1;
114
mainLayout->addWidget(digitButtons[i], row, column);
117
mainLayout->addWidget(digitButtons[0], 5, 1);
118
mainLayout->addWidget(pointButton, 5, 2);
119
mainLayout->addWidget(changeSignButton, 5, 3);
121
mainLayout->addWidget(divisionButton, 2, 4);
122
mainLayout->addWidget(timesButton, 3, 4);
123
mainLayout->addWidget(minusButton, 4, 4);
124
mainLayout->addWidget(plusButton, 5, 4);
126
mainLayout->addWidget(squareRootButton, 2, 5);
127
mainLayout->addWidget(powerButton, 3, 5);
128
mainLayout->addWidget(reciprocalButton, 4, 5);
129
mainLayout->addWidget(equalButton, 5, 5);
130
setLayout(mainLayout);
132
setWindowTitle(tr("Calculator"));
135
bool Calculator::eventFilter(QObject *target, QEvent *event)
137
if (target == display) {
138
if (event->type() == QEvent::MouseButtonPress
139
|| event->type() == QEvent::MouseButtonDblClick
140
|| event->type() == QEvent::MouseButtonRelease
141
|| event->type() == QEvent::ContextMenu) {
142
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
143
if (mouseEvent->buttons() & Qt::LeftButton) {
144
QPalette newPalette = palette();
145
newPalette.setColor(QPalette::Base,
146
display->palette().color(QPalette::Text));
147
newPalette.setColor(QPalette::Text,
148
display->palette().color(QPalette::Base));
149
display->setPalette(newPalette);
151
display->setPalette(palette());
156
return QDialog::eventFilter(target, event);
159
void Calculator::digitClicked()
161
Button *clickedButton = qobject_cast<Button *>(sender());
162
int digitValue = clickedButton->text().toInt();
163
if (display->text() == "0" && digitValue == 0.0)
166
if (waitingForOperand) {
168
waitingForOperand = false;
170
display->setText(display->text() + QString::number(digitValue));
173
void Calculator::unaryOperatorClicked()
175
Button *clickedButton = qobject_cast<Button *>(sender());
176
QString clickedOperator = clickedButton->text();
177
double operand = display->text().toDouble();
180
if (clickedOperator == tr("Sqrt")) {
185
result = sqrt(operand);
186
} else if (clickedOperator == tr("x�")) {
187
result = pow(operand, 2.0);
188
} else if (clickedOperator == tr("1/x")) {
189
if (operand == 0.0) {
193
result = 1.0 / operand;
195
display->setText(QString::number(result));
196
waitingForOperand = true;
199
void Calculator::additiveOperatorClicked()
201
Button *clickedButton = qobject_cast<Button *>(sender());
202
QString clickedOperator = clickedButton->text();
203
double operand = display->text().toDouble();
205
if (!pendingMultiplicativeOperator.isEmpty()) {
206
if (!calculate(operand, pendingMultiplicativeOperator)) {
210
display->setText(QString::number(factorSoFar));
211
operand = factorSoFar;
213
pendingMultiplicativeOperator.clear();
216
if (!pendingAdditiveOperator.isEmpty()) {
217
if (!calculate(operand, pendingAdditiveOperator)) {
221
display->setText(QString::number(sumSoFar));
226
pendingAdditiveOperator = clickedOperator;
227
waitingForOperand = true;
230
void Calculator::multiplicativeOperatorClicked()
232
Button *clickedButton = qobject_cast<Button *>(sender());
233
QString clickedOperator = clickedButton->text();
234
double operand = display->text().toDouble();
236
if (!pendingMultiplicativeOperator.isEmpty()) {
237
if (!calculate(operand, pendingMultiplicativeOperator)) {
241
display->setText(QString::number(factorSoFar));
243
factorSoFar = operand;
246
pendingMultiplicativeOperator = clickedOperator;
247
waitingForOperand = true;
250
void Calculator::equalClicked()
252
double operand = display->text().toDouble();
254
if (!pendingMultiplicativeOperator.isEmpty()) {
255
if (!calculate(operand, pendingMultiplicativeOperator)) {
259
operand = factorSoFar;
261
pendingMultiplicativeOperator.clear();
263
if (!pendingAdditiveOperator.isEmpty()) {
264
if (!calculate(operand, pendingAdditiveOperator)) {
268
pendingAdditiveOperator.clear();
273
display->setText(QString::number(sumSoFar));
275
waitingForOperand = true;
278
void Calculator::pointClicked()
280
if (waitingForOperand)
281
display->setText("0");
282
if (!display->text().contains("."))
283
display->setText(display->text() + tr("."));
284
waitingForOperand = false;
287
void Calculator::changeSignClicked()
289
QString text = display->text();
290
double value = text.toDouble();
293
text.prepend(tr("-"));
294
} else if (value < 0.0) {
297
display->setText(text);
300
void Calculator::backspaceClicked()
302
if (waitingForOperand)
305
QString text = display->text();
307
if (text.isEmpty()) {
309
waitingForOperand = true;
311
display->setText(text);
314
void Calculator::clear()
316
if (waitingForOperand)
319
display->setText("0");
320
waitingForOperand = true;
323
void Calculator::clearAll()
327
pendingAdditiveOperator.clear();
328
pendingMultiplicativeOperator.clear();
329
display->setText("0");
330
waitingForOperand = true;
333
void Calculator::clearMemory()
338
void Calculator::readMemory()
340
display->setText(QString::number(sumInMemory));
341
waitingForOperand = true;
344
void Calculator::setMemory()
347
sumInMemory = display->text().toDouble();
350
void Calculator::addToMemory()
353
sumInMemory += display->text().toDouble();
356
Button *Calculator::createButton(const QString &text, const QColor &color,
359
Button *button = new Button(text, color);
360
connect(button, SIGNAL(clicked()), this, member);
364
void Calculator::abortOperation()
367
display->setText(tr("####"));
370
bool Calculator::calculate(double rightOperand, const QString &pendingOperator)
372
if (pendingOperator == tr("+")) {
373
sumSoFar += rightOperand;
374
} else if (pendingOperator == tr("-")) {
375
sumSoFar -= rightOperand;
376
} else if (pendingOperator == tr("�")) {
377
factorSoFar *= rightOperand;
378
} else if (pendingOperator == tr("�")) {
379
if (rightOperand == 0.0)
381
factorSoFar /= rightOperand;