1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: calculator.cpp Example File (widgets/calculator/calculator.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">calculator.cpp Example File<br /><small><small>widgets/calculator/calculator.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
50
#include <math.h>
52
#include "button.h"
53
#include "calculator.h"
55
Calculator::Calculator(QWidget *parent)
61
waitingForOperand = true;
63
display = new QLineEdit("0");
64
display->setReadOnly(true);
65
display->setAlignment(Qt::AlignRight);
66
display->setMaxLength(15);
67
display->installEventFilter(this);
69
QFont font = display->font();
70
font.setPointSize(font.pointSize() + 8);
71
display->setFont(font);
73
QColor digitColor(150, 205, 205);
74
QColor backspaceColor(225, 185, 135);
75
QColor memoryColor(100, 155, 155);
76
QColor operatorColor(155, 175, 195);
78
for (int i = 0; i < NumDigitButtons; ++i) {
79
digitButtons[i] = createButton(QString::number(i), digitColor,
80
SLOT(digitClicked()));
83
pointButton = createButton(tr("."), digitColor, SLOT(pointClicked()));
84
changeSignButton = createButton(tr("�"), digitColor, SLOT(changeSignClicked()));
86
backspaceButton = createButton(tr("Backspace"), backspaceColor,
87
SLOT(backspaceClicked()));
88
clearButton = createButton(tr("Clear"), backspaceColor, SLOT(clear()));
89
clearAllButton = createButton(tr("Clear All"), backspaceColor.light(120),
92
clearMemoryButton = createButton(tr("MC"), memoryColor,
94
readMemoryButton = createButton(tr("MR"), memoryColor, SLOT(readMemory()));
95
setMemoryButton = createButton(tr("MS"), memoryColor, SLOT(setMemory()));
96
addToMemoryButton = createButton(tr("M+"), memoryColor,
99
divisionButton = createButton(tr("�"), operatorColor,
100
SLOT(multiplicativeOperatorClicked()));
101
timesButton = createButton(tr("�"), operatorColor,
102
SLOT(multiplicativeOperatorClicked()));
103
minusButton = createButton(tr("-"), operatorColor,
104
SLOT(additiveOperatorClicked()));
105
plusButton = createButton(tr("+"), operatorColor,
106
SLOT(additiveOperatorClicked()));
108
squareRootButton = createButton(tr("Sqrt"), operatorColor,
109
SLOT(unaryOperatorClicked()));
110
powerButton = createButton(tr("x�"), operatorColor,
111
SLOT(unaryOperatorClicked()));
112
reciprocalButton = createButton(tr("1/x"), operatorColor,
113
SLOT(unaryOperatorClicked()));
114
equalButton = createButton(tr("="), operatorColor.light(120),
115
SLOT(equalClicked()));
117
QGridLayout *mainLayout = new QGridLayout;
118
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
120
mainLayout->addWidget(display, 0, 0, 1, 6);
121
mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
122
mainLayout->addWidget(clearButton, 1, 2, 1, 2);
123
mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
125
mainLayout->addWidget(clearMemoryButton, 2, 0);
126
mainLayout->addWidget(readMemoryButton, 3, 0);
127
mainLayout->addWidget(setMemoryButton, 4, 0);
128
mainLayout->addWidget(addToMemoryButton, 5, 0);
130
for (int i = 1; i < NumDigitButtons; ++i) {
131
int row = ((9 - i) / 3) + 2;
132
int column = ((i - 1) % 3) + 1;
133
mainLayout->addWidget(digitButtons[i], row, column);
136
mainLayout->addWidget(digitButtons[0], 5, 1);
137
mainLayout->addWidget(pointButton, 5, 2);
138
mainLayout->addWidget(changeSignButton, 5, 3);
140
mainLayout->addWidget(divisionButton, 2, 4);
141
mainLayout->addWidget(timesButton, 3, 4);
142
mainLayout->addWidget(minusButton, 4, 4);
143
mainLayout->addWidget(plusButton, 5, 4);
145
mainLayout->addWidget(squareRootButton, 2, 5);
146
mainLayout->addWidget(powerButton, 3, 5);
147
mainLayout->addWidget(reciprocalButton, 4, 5);
148
mainLayout->addWidget(equalButton, 5, 5);
149
setLayout(mainLayout);
151
setWindowTitle(tr("Calculator"));
154
bool Calculator::eventFilter(QObject *target, QEvent *event)
156
if (target == display) {
157
if (event->type() == QEvent::MouseButtonPress
158
|| event->type() == QEvent::MouseButtonDblClick
159
|| event->type() == QEvent::MouseButtonRelease
160
|| event->type() == QEvent::ContextMenu) {
161
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
162
if (mouseEvent->buttons() & Qt::LeftButton) {
163
QPalette newPalette = palette();
164
newPalette.setColor(QPalette::Base,
165
display->palette().color(QPalette::Text));
166
newPalette.setColor(QPalette::Text,
167
display->palette().color(QPalette::Base));
168
display->setPalette(newPalette);
170
display->setPalette(palette());
175
return QDialog::eventFilter(target, event);
178
void Calculator::digitClicked()
180
Button *clickedButton = qobject_cast<Button *>(sender());
181
int digitValue = clickedButton->text().toInt();
182
if (display->text() == "0" && digitValue == 0.0)
185
if (waitingForOperand) {
187
waitingForOperand = false;
189
display->setText(display->text() + QString::number(digitValue));
192
void Calculator::unaryOperatorClicked()
194
Button *clickedButton = qobject_cast<Button *>(sender());
195
QString clickedOperator = clickedButton->text();
196
double operand = display->text().toDouble();
199
if (clickedOperator == tr("Sqrt")) {
200
if (operand < 0.0) {
204
result = sqrt(operand);
205
} else if (clickedOperator == tr("x�")) {
206
result = pow(operand, 2.0);
207
} else if (clickedOperator == tr("1/x")) {
208
if (operand == 0.0) {
212
result = 1.0 / operand;
214
display->setText(QString::number(result));
215
waitingForOperand = true;
218
void Calculator::additiveOperatorClicked()
220
Button *clickedButton = qobject_cast<Button *>(sender());
221
QString clickedOperator = clickedButton->text();
222
double operand = display->text().toDouble();
224
if (!pendingMultiplicativeOperator.isEmpty()) {
225
if (!calculate(operand, pendingMultiplicativeOperator)) {
229
display->setText(QString::number(factorSoFar));
230
operand = factorSoFar;
232
pendingMultiplicativeOperator.clear();
235
if (!pendingAdditiveOperator.isEmpty()) {
236
if (!calculate(operand, pendingAdditiveOperator)) {
240
display->setText(QString::number(sumSoFar));
245
pendingAdditiveOperator = clickedOperator;
246
waitingForOperand = true;
249
void Calculator::multiplicativeOperatorClicked()
251
Button *clickedButton = qobject_cast<Button *>(sender());
252
QString clickedOperator = clickedButton->text();
253
double operand = display->text().toDouble();
255
if (!pendingMultiplicativeOperator.isEmpty()) {
256
if (!calculate(operand, pendingMultiplicativeOperator)) {
260
display->setText(QString::number(factorSoFar));
262
factorSoFar = operand;
265
pendingMultiplicativeOperator = clickedOperator;
266
waitingForOperand = true;
269
void Calculator::equalClicked()
271
double operand = display->text().toDouble();
273
if (!pendingMultiplicativeOperator.isEmpty()) {
274
if (!calculate(operand, pendingMultiplicativeOperator)) {
278
operand = factorSoFar;
280
pendingMultiplicativeOperator.clear();
282
if (!pendingAdditiveOperator.isEmpty()) {
283
if (!calculate(operand, pendingAdditiveOperator)) {
287
pendingAdditiveOperator.clear();
292
display->setText(QString::number(sumSoFar));
294
waitingForOperand = true;
297
void Calculator::pointClicked()
299
if (waitingForOperand)
300
display->setText("0");
301
if (!display->text().contains("."))
302
display->setText(display->text() + tr("."));
303
waitingForOperand = false;
306
void Calculator::changeSignClicked()
308
QString text = display->text();
309
double value = text.toDouble();
311
if (value > 0.0) {
312
text.prepend(tr("-"));
313
} else if (value < 0.0) {
316
display->setText(text);
319
void Calculator::backspaceClicked()
321
if (waitingForOperand)
324
QString text = display->text();
326
if (text.isEmpty()) {
327
text = "0";
328
waitingForOperand = true;
330
display->setText(text);
333
void Calculator::clear()
335
if (waitingForOperand)
338
display->setText("0");
339
waitingForOperand = true;
342
void Calculator::clearAll()
346
pendingAdditiveOperator.clear();
347
pendingMultiplicativeOperator.clear();
348
display->setText("0");
349
waitingForOperand = true;
352
void Calculator::clearMemory()
357
void Calculator::readMemory()
359
display->setText(QString::number(sumInMemory));
360
waitingForOperand = true;
363
void Calculator::setMemory()
366
sumInMemory = display->text().toDouble();
369
void Calculator::addToMemory()
372
sumInMemory += display->text().toDouble();
375
Button *Calculator::createButton(const QString &text, const QColor &color,
378
Button *button = new Button(text, color);
379
connect(button, SIGNAL(clicked()), this, member);
383
void Calculator::abortOperation()
386
display->setText(tr("####"));
389
bool Calculator::calculate(double rightOperand, const QString &pendingOperator)
391
if (pendingOperator == tr("+")) {
392
sumSoFar += rightOperand;
393
} else if (pendingOperator == tr("-")) {
394
sumSoFar -= rightOperand;
395
} else if (pendingOperator == tr("�")) {
396
factorSoFar *= rightOperand;
397
} else if (pendingOperator == tr("�")) {
398
if (rightOperand == 0.0)
400
factorSoFar /= rightOperand;
404
<p /><address><hr /><div align="center">
405
<table width="100%" cellspacing="0" border="0"><tr class="address">
406
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
407
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
408
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
409
</tr></table></div></address></body>