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

Last change on this file since 158 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include <math.h>
45
46#include "button.h"
47#include "calculator.h"
48
49//! [0]
50Calculator::Calculator(QWidget *parent)
51 : QDialog(parent)
52{
53 sumInMemory = 0.0;
54 sumSoFar = 0.0;
55 factorSoFar = 0.0;
56 waitingForOperand = true;
57//! [0]
58
59//! [1]
60 display = new QLineEdit("0");
61//! [1] //! [2]
62 display->setReadOnly(true);
63 display->setAlignment(Qt::AlignRight);
64 display->setMaxLength(15);
65
66 QFont font = display->font();
67 font.setPointSize(font.pointSize() + 8);
68 display->setFont(font);
69//! [2]
70
71//! [4]
72 for (int i = 0; i < NumDigitButtons; ++i) {
73 digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
74 }
75
76 Button *pointButton = createButton(tr("."), SLOT(pointClicked()));
77 Button *changeSignButton = createButton(tr("\261"), SLOT(changeSignClicked()));
78
79 Button *backspaceButton = createButton(tr("Backspace"), SLOT(backspaceClicked()));
80 Button *clearButton = createButton(tr("Clear"), SLOT(clear()));
81 Button *clearAllButton = createButton(tr("Clear All"), SLOT(clearAll()));
82
83 Button *clearMemoryButton = createButton(tr("MC"), SLOT(clearMemory()));
84 Button *readMemoryButton = createButton(tr("MR"), SLOT(readMemory()));
85 Button *setMemoryButton = createButton(tr("MS"), SLOT(setMemory()));
86 Button *addToMemoryButton = createButton(tr("M+"), SLOT(addToMemory()));
87
88 Button *divisionButton = createButton(tr("\367"), SLOT(multiplicativeOperatorClicked()));
89 Button *timesButton = createButton(tr("\327"), SLOT(multiplicativeOperatorClicked()));
90 Button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked()));
91 Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
92
93 Button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked()));
94 Button *powerButton = createButton(tr("x\262"), SLOT(unaryOperatorClicked()));
95 Button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked()));
96 Button *equalButton = createButton(tr("="), SLOT(equalClicked()));
97//! [4]
98
99//! [5]
100 QGridLayout *mainLayout = new QGridLayout;
101//! [5] //! [6]
102 mainLayout->setSizeConstraint(QLayout::SetFixedSize);
103
104 mainLayout->addWidget(display, 0, 0, 1, 6);
105 mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
106 mainLayout->addWidget(clearButton, 1, 2, 1, 2);
107 mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
108
109 mainLayout->addWidget(clearMemoryButton, 2, 0);
110 mainLayout->addWidget(readMemoryButton, 3, 0);
111 mainLayout->addWidget(setMemoryButton, 4, 0);
112 mainLayout->addWidget(addToMemoryButton, 5, 0);
113
114 for (int i = 1; i < NumDigitButtons; ++i) {
115 int row = ((9 - i) / 3) + 2;
116 int column = ((i - 1) % 3) + 1;
117 mainLayout->addWidget(digitButtons[i], row, column);
118 }