~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/widgets-calculator-calculator-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
Import upstream version 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
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>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
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.
 
29
**
 
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.
 
34
**
 
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.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
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.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
 
 
50
    #include &lt;math.h&gt;
 
51
 
 
52
    #include &quot;button.h&quot;
 
53
    #include &quot;calculator.h&quot;
 
54
 
 
55
    Calculator::Calculator(QWidget *parent)
 
56
        : QDialog(parent)
 
57
    {
 
58
        sumInMemory = 0.0;
 
59
        sumSoFar = 0.0;
 
60
        factorSoFar = 0.0;
 
61
        waitingForOperand = true;
 
62
 
 
63
        display = new QLineEdit(&quot;0&quot;);
 
64
        display-&gt;setReadOnly(true);
 
65
        display-&gt;setAlignment(Qt::AlignRight);
 
66
        display-&gt;setMaxLength(15);
 
67
        display-&gt;installEventFilter(this);
 
68
 
 
69
        QFont font = display-&gt;font();
 
70
        font.setPointSize(font.pointSize() + 8);
 
71
        display-&gt;setFont(font);
 
72
 
 
73
        QColor digitColor(150, 205, 205);
 
74
        QColor backspaceColor(225, 185, 135);
 
75
        QColor memoryColor(100, 155, 155);
 
76
        QColor operatorColor(155, 175, 195);
 
77
 
 
78
        for (int i = 0; i &lt; NumDigitButtons; ++i) {
 
79
            digitButtons[i] = createButton(QString::number(i), digitColor,
 
80
                                           SLOT(digitClicked()));
 
81
        }
 
82
 
 
83
        pointButton = createButton(tr(&quot;.&quot;), digitColor, SLOT(pointClicked()));
 
84
        changeSignButton = createButton(tr(&quot;�&quot;), digitColor, SLOT(changeSignClicked()));
 
85
 
 
86
        backspaceButton = createButton(tr(&quot;Backspace&quot;), backspaceColor,
 
87
                                       SLOT(backspaceClicked()));
 
88
        clearButton = createButton(tr(&quot;Clear&quot;), backspaceColor, SLOT(clear()));
 
89
        clearAllButton = createButton(tr(&quot;Clear All&quot;), backspaceColor.light(120),
 
90
                                      SLOT(clearAll()));
 
91
 
 
92
        clearMemoryButton = createButton(tr(&quot;MC&quot;), memoryColor,
 
93
                                         SLOT(clearMemory()));
 
94
        readMemoryButton = createButton(tr(&quot;MR&quot;), memoryColor, SLOT(readMemory()));
 
95
        setMemoryButton = createButton(tr(&quot;MS&quot;), memoryColor, SLOT(setMemory()));
 
96
        addToMemoryButton = createButton(tr(&quot;M+&quot;), memoryColor,
 
97
                                         SLOT(addToMemory()));
 
98
 
 
99
        divisionButton = createButton(tr(&quot;�&quot;), operatorColor,
 
100
                                      SLOT(multiplicativeOperatorClicked()));
 
101
        timesButton = createButton(tr(&quot;�&quot;), operatorColor,
 
102
                                   SLOT(multiplicativeOperatorClicked()));
 
103
        minusButton = createButton(tr(&quot;-&quot;), operatorColor,
 
104
                                   SLOT(additiveOperatorClicked()));
 
105
        plusButton = createButton(tr(&quot;+&quot;), operatorColor,
 
106
                                  SLOT(additiveOperatorClicked()));
 
107
 
 
108
        squareRootButton = createButton(tr(&quot;Sqrt&quot;), operatorColor,
 
109
                                        SLOT(unaryOperatorClicked()));
 
110
        powerButton = createButton(tr(&quot;x�&quot;), operatorColor,
 
111
                                   SLOT(unaryOperatorClicked()));
 
112
        reciprocalButton = createButton(tr(&quot;1/x&quot;), operatorColor,
 
113
                                        SLOT(unaryOperatorClicked()));
 
114
        equalButton = createButton(tr(&quot;=&quot;), operatorColor.light(120),
 
115
                                   SLOT(equalClicked()));
 
116
 
 
117
        QGridLayout *mainLayout = new QGridLayout;
 
118
        mainLayout-&gt;setSizeConstraint(QLayout::SetFixedSize);
 
119
 
 
120
        mainLayout-&gt;addWidget(display, 0, 0, 1, 6);
 
121
        mainLayout-&gt;addWidget(backspaceButton, 1, 0, 1, 2);
 
122
        mainLayout-&gt;addWidget(clearButton, 1, 2, 1, 2);
 
123
        mainLayout-&gt;addWidget(clearAllButton, 1, 4, 1, 2);
 
124
 
 
125
        mainLayout-&gt;addWidget(clearMemoryButton, 2, 0);
 
126
        mainLayout-&gt;addWidget(readMemoryButton, 3, 0);
 
127
        mainLayout-&gt;addWidget(setMemoryButton, 4, 0);
 
128
        mainLayout-&gt;addWidget(addToMemoryButton, 5, 0);
 
129
 
 
130
        for (int i = 1; i &lt; NumDigitButtons; ++i) {
 
131
            int row = ((9 - i) / 3) + 2;
 
132
            int column = ((i - 1) % 3) + 1;
 
133
            mainLayout-&gt;addWidget(digitButtons[i], row, column);
 
134
        }
 
135
 
 
136
        mainLayout-&gt;addWidget(digitButtons[0], 5, 1);
 
137
        mainLayout-&gt;addWidget(pointButton, 5, 2);
 
138
        mainLayout-&gt;addWidget(changeSignButton, 5, 3);
 
139
 
 
140
        mainLayout-&gt;addWidget(divisionButton, 2, 4);
 
141
        mainLayout-&gt;addWidget(timesButton, 3, 4);
 
142
        mainLayout-&gt;addWidget(minusButton, 4, 4);
 
143
        mainLayout-&gt;addWidget(plusButton, 5, 4);
 
144
 
 
145
        mainLayout-&gt;addWidget(squareRootButton, 2, 5);
 
146
        mainLayout-&gt;addWidget(powerButton, 3, 5);
 
147
        mainLayout-&gt;addWidget(reciprocalButton, 4, 5);
 
148
        mainLayout-&gt;addWidget(equalButton, 5, 5);
 
149
        setLayout(mainLayout);
 
150
 
 
151
        setWindowTitle(tr(&quot;Calculator&quot;));
 
152
    }
 
153
 
 
154
    bool Calculator::eventFilter(QObject *target, QEvent *event)
 
155
    {
 
156
        if (target == display) {
 
157
            if (event-&gt;type() == QEvent::MouseButtonPress
 
158
                    || event-&gt;type() == QEvent::MouseButtonDblClick
 
159
                    || event-&gt;type() == QEvent::MouseButtonRelease
 
160
                    || event-&gt;type() == QEvent::ContextMenu) {
 
161
                QMouseEvent *mouseEvent = static_cast&lt;QMouseEvent *&gt;(event);
 
162
                if (mouseEvent-&gt;buttons() &amp; Qt::LeftButton) {
 
163
                    QPalette newPalette = palette();
 
164
                    newPalette.setColor(QPalette::Base,
 
165
                                        display-&gt;palette().color(QPalette::Text));
 
166
                    newPalette.setColor(QPalette::Text,
 
167
                                        display-&gt;palette().color(QPalette::Base));
 
168
                    display-&gt;setPalette(newPalette);
 
169
                } else {
 
170
                    display-&gt;setPalette(palette());
 
171
                }
 
172
                return true;
 
173
            }
 
174
        }
 
175
        return QDialog::eventFilter(target, event);
 
176
    }
 
177
 
 
178
    void Calculator::digitClicked()
 
179
    {
 
180
        Button *clickedButton = qobject_cast&lt;Button *&gt;(sender());
 
181
        int digitValue = clickedButton-&gt;text().toInt();
 
182
        if (display-&gt;text() == &quot;0&quot; &amp;&amp; digitValue == 0.0)
 
183
            return;
 
184
 
 
185
        if (waitingForOperand) {
 
186
            display-&gt;clear();
 
187
            waitingForOperand = false;
 
188
        }
 
189
        display-&gt;setText(display-&gt;text() + QString::number(digitValue));
 
190
    }
 
191
 
 
192
    void Calculator::unaryOperatorClicked()
 
193
    {
 
194
        Button *clickedButton = qobject_cast&lt;Button *&gt;(sender());
 
195
        QString clickedOperator = clickedButton-&gt;text();
 
196
        double operand = display-&gt;text().toDouble();
 
197
        double result;
 
198
 
 
199
        if (clickedOperator == tr(&quot;Sqrt&quot;)) {
 
200
            if (operand &lt; 0.0) {
 
201
                abortOperation();
 
202
                return;
 
203
            }
 
204
            result = sqrt(operand);
 
205
        } else if (clickedOperator == tr(&quot;x�&quot;)) {
 
206
            result = pow(operand, 2.0);
 
207
        } else if (clickedOperator == tr(&quot;1/x&quot;)) {
 
208
            if (operand == 0.0) {
 
209
                abortOperation();
 
210
                return;
 
211
            }
 
212
            result = 1.0 / operand;
 
213
        }
 
214
        display-&gt;setText(QString::number(result));
 
215
        waitingForOperand = true;
 
216
    }
 
217
 
 
218
    void Calculator::additiveOperatorClicked()
 
219
    {
 
220
        Button *clickedButton = qobject_cast&lt;Button *&gt;(sender());
 
221
        QString clickedOperator = clickedButton-&gt;text();
 
222
        double operand = display-&gt;text().toDouble();
 
223
 
 
224
        if (!pendingMultiplicativeOperator.isEmpty()) {
 
225
            if (!calculate(operand, pendingMultiplicativeOperator)) {
 
226
                abortOperation();
 
227
                return;
 
228
            }
 
229
            display-&gt;setText(QString::number(factorSoFar));
 
230
            operand = factorSoFar;
 
231
            factorSoFar = 0.0;
 
232
            pendingMultiplicativeOperator.clear();
 
233
        }
 
234
 
 
235
        if (!pendingAdditiveOperator.isEmpty()) {
 
236
            if (!calculate(operand, pendingAdditiveOperator)) {
 
237
                abortOperation();
 
238
                return;
 
239
            }
 
240
            display-&gt;setText(QString::number(sumSoFar));
 
241
        } else {
 
242
            sumSoFar = operand;
 
243
        }
 
244
 
 
245
        pendingAdditiveOperator = clickedOperator;
 
246
        waitingForOperand = true;
 
247
    }
 
248
 
 
249
    void Calculator::multiplicativeOperatorClicked()
 
250
    {
 
251
        Button *clickedButton = qobject_cast&lt;Button *&gt;(sender());
 
252
        QString clickedOperator = clickedButton-&gt;text();
 
253
        double operand = display-&gt;text().toDouble();
 
254
 
 
255
        if (!pendingMultiplicativeOperator.isEmpty()) {
 
256
            if (!calculate(operand, pendingMultiplicativeOperator)) {
 
257
                abortOperation();
 
258
                return;
 
259
            }
 
260
            display-&gt;setText(QString::number(factorSoFar));
 
261
        } else {
 
262
            factorSoFar = operand;
 
263
        }
 
264
 
 
265
        pendingMultiplicativeOperator = clickedOperator;
 
266
        waitingForOperand = true;
 
267
    }
 
268
 
 
269
    void Calculator::equalClicked()
 
270
    {
 
271
        double operand = display-&gt;text().toDouble();
 
272
 
 
273
        if (!pendingMultiplicativeOperator.isEmpty()) {
 
274
            if (!calculate(operand, pendingMultiplicativeOperator)) {
 
275
                abortOperation();
 
276
                return;
 
277
            }
 
278
            operand = factorSoFar;
 
279
            factorSoFar = 0.0;
 
280
            pendingMultiplicativeOperator.clear();
 
281
        }
 
282
        if (!pendingAdditiveOperator.isEmpty()) {
 
283
            if (!calculate(operand, pendingAdditiveOperator)) {
 
284
                abortOperation();
 
285
                return;
 
286
            }
 
287
            pendingAdditiveOperator.clear();
 
288
        } else {
 
289
            sumSoFar = operand;
 
290
        }
 
291
 
 
292
        display-&gt;setText(QString::number(sumSoFar));
 
293
        sumSoFar = 0.0;
 
294
        waitingForOperand = true;
 
295
    }
 
296
 
 
297
    void Calculator::pointClicked()
 
298
    {
 
299
        if (waitingForOperand)
 
300
            display-&gt;setText(&quot;0&quot;);
 
301
        if (!display-&gt;text().contains(&quot;.&quot;))
 
302
            display-&gt;setText(display-&gt;text() + tr(&quot;.&quot;));
 
303
        waitingForOperand = false;
 
304
    }
 
305
 
 
306
    void Calculator::changeSignClicked()
 
307
    {
 
308
        QString text = display-&gt;text();
 
309
        double value = text.toDouble();
 
310
 
 
311
        if (value &gt; 0.0) {
 
312
            text.prepend(tr(&quot;-&quot;));
 
313
        } else if (value &lt; 0.0) {
 
314
            text.remove(0, 1);
 
315
        }
 
316
        display-&gt;setText(text);
 
317
    }
 
318
 
 
319
    void Calculator::backspaceClicked()
 
320
    {
 
321
        if (waitingForOperand)
 
322
            return;
 
323
 
 
324
        QString text = display-&gt;text();
 
325
        text.chop(1);
 
326
        if (text.isEmpty()) {
 
327
            text = &quot;0&quot;;
 
328
            waitingForOperand = true;
 
329
        }
 
330
        display-&gt;setText(text);
 
331
    }
 
332
 
 
333
    void Calculator::clear()
 
334
    {
 
335
        if (waitingForOperand)
 
336
            return;
 
337
 
 
338
        display-&gt;setText(&quot;0&quot;);
 
339
        waitingForOperand = true;
 
340
    }
 
341
 
 
342
    void Calculator::clearAll()
 
343
    {
 
344
        sumSoFar = 0.0;
 
345
        factorSoFar = 0.0;
 
346
        pendingAdditiveOperator.clear();
 
347
        pendingMultiplicativeOperator.clear();
 
348
        display-&gt;setText(&quot;0&quot;);
 
349
        waitingForOperand = true;
 
350
    }
 
351
 
 
352
    void Calculator::clearMemory()
 
353
    {
 
354
        sumInMemory = 0.0;
 
355
    }
 
356
 
 
357
    void Calculator::readMemory()
 
358
    {
 
359
        display-&gt;setText(QString::number(sumInMemory));
 
360
        waitingForOperand = true;
 
361
    }
 
362
 
 
363
    void Calculator::setMemory()
 
364
    {
 
365
        equalClicked();
 
366
        sumInMemory = display-&gt;text().toDouble();
 
367
    }
 
368
 
 
369
    void Calculator::addToMemory()
 
370
    {
 
371
        equalClicked();
 
372
        sumInMemory += display-&gt;text().toDouble();
 
373
    }
 
374
 
 
375
    Button *Calculator::createButton(const QString &amp;text, const QColor &amp;color,
 
376
                                     const char *member)
 
377
    {
 
378
        Button *button = new Button(text, color);
 
379
        connect(button, SIGNAL(clicked()), this, member);
 
380
        return button;
 
381
    }
 
382
 
 
383
    void Calculator::abortOperation()
 
384
    {
 
385
        clearAll();
 
386
        display-&gt;setText(tr(&quot;####&quot;));
 
387
    }
 
388
 
 
389
    bool Calculator::calculate(double rightOperand, const QString &amp;pendingOperator)
 
390
    {
 
391
        if (pendingOperator == tr(&quot;+&quot;)) {
 
392
            sumSoFar += rightOperand;
 
393
        } else if (pendingOperator == tr(&quot;-&quot;)) {
 
394
            sumSoFar -= rightOperand;
 
395
        } else if (pendingOperator == tr(&quot;�&quot;)) {
 
396
            factorSoFar *= rightOperand;
 
397
        } else if (pendingOperator == tr(&quot;�&quot;)) {
 
398
            if (rightOperand == 0.0)
 
399
                return false;
 
400
            factorSoFar /= rightOperand;
 
401
        }
 
402
        return true;
 
403
    }</pre>
 
404
<p /><address><hr /><div align="center">
 
405
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
406
<td width="30%">Copyright &copy; 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>
 
410
</html>