summaryrefslogtreecommitdiffstats
path: root/examples/widgets/widgets/calculator
diff options
context:
space:
mode:
authorGabriel de Dietrich <gabriel.dietrich-de@nokia.com>2012-08-17 13:23:19 +0200
committerQt by Nokia <qt-info@nokia.com>2012-08-20 12:20:55 +0200
commit806dda08d685bc5f9ed71dfe8b61f21848d48066 (patch)
treea63533a1c4a335ae17adc105abb0ae4e62e2f26e /examples/widgets/widgets/calculator
parent9f942014e31842b512c3198de035d041c59f54a9 (diff)
Moving .qdoc files under examples/widgets/doc
Updated those .qdoc files to refer to the new relative examples emplacement. Images and snippets to be moved later. Also grouped all widgets related examples under widgets. Change-Id: Ib29696e2d8948524537f53e8dda88f9ee26a597f Reviewed-by: J-P Nurmi <j-p.nurmi@nokia.com>
Diffstat (limited to 'examples/widgets/widgets/calculator')
-rw-r--r--examples/widgets/widgets/calculator/button.cpp63
-rw-r--r--examples/widgets/widgets/calculator/button.h58
-rw-r--r--examples/widgets/widgets/calculator/calculator.cpp396
-rw-r--r--examples/widgets/widgets/calculator/calculator.desktop11
-rw-r--r--examples/widgets/widgets/calculator/calculator.h107
-rw-r--r--examples/widgets/widgets/calculator/calculator.pro14
-rw-r--r--examples/widgets/widgets/calculator/main.cpp51
-rw-r--r--examples/widgets/widgets/calculator/releasenotes.txt4
8 files changed, 704 insertions, 0 deletions
diff --git a/examples/widgets/widgets/calculator/button.cpp b/examples/widgets/widgets/calculator/button.cpp
new file mode 100644
index 0000000000..14c3ebe20d
--- /dev/null
+++ b/examples/widgets/widgets/calculator/button.cpp
@@ -0,0 +1,63 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+
+#include "button.h"
+
+//! [0]
+Button::Button(const QString &text, QWidget *parent)
+ : QToolButton(parent)
+{
+ setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
+ setText(text);
+}
+//! [0]
+
+//! [1]
+QSize Button::sizeHint() const
+//! [1] //! [2]
+{
+ QSize size = QToolButton::sizeHint();
+ size.rheight() += 20;
+ size.rwidth() = qMax(size.width(), size.height());
+ return size;
+}
+//! [2]
diff --git a/examples/widgets/widgets/calculator/button.h b/examples/widgets/widgets/calculator/button.h
new file mode 100644
index 0000000000..50874ea574
--- /dev/null
+++ b/examples/widgets/widgets/calculator/button.h
@@ -0,0 +1,58 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef BUTTON_H
+#define BUTTON_H
+
+#include <QToolButton>
+
+//! [0]
+class Button : public QToolButton
+{
+ Q_OBJECT
+
+public:
+ Button(const QString &text, QWidget *parent = 0);
+
+ QSize sizeHint() const;
+};
+//! [0]
+
+#endif
diff --git a/examples/widgets/widgets/calculator/calculator.cpp b/examples/widgets/widgets/calculator/calculator.cpp
new file mode 100644
index 0000000000..29503befc5
--- /dev/null
+++ b/examples/widgets/widgets/calculator/calculator.cpp
@@ -0,0 +1,396 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+
+#include <math.h>
+
+#include "button.h"
+#include "calculator.h"
+
+//! [0]
+Calculator::Calculator(QWidget *parent)
+ : QWidget(parent)
+{
+ sumInMemory = 0.0;
+ sumSoFar = 0.0;
+ factorSoFar = 0.0;
+ waitingForOperand = true;
+//! [0]
+
+//! [1]
+ display = new QLineEdit("0");
+//! [1] //! [2]
+ display->setReadOnly(true);
+ display->setAlignment(Qt::AlignRight);
+ display->setMaxLength(15);
+
+ QFont font = display->font();
+ font.setPointSize(font.pointSize() + 8);
+ display->setFont(font);
+//! [2]
+
+//! [4]
+ for (int i = 0; i < NumDigitButtons; ++i) {
+ digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
+ }
+
+ Button *pointButton = createButton(tr("."), SLOT(pointClicked()));
+ Button *changeSignButton = createButton(tr("\302\261"), SLOT(changeSignClicked()));
+
+ Button *backspaceButton = createButton(tr("Backspace"), SLOT(backspaceClicked()));
+ Button *clearButton = createButton(tr("Clear"), SLOT(clear()));
+ Button *clearAllButton = createButton(tr("Clear All"), SLOT(clearAll()));
+
+ Button *clearMemoryButton = createButton(tr("MC"), SLOT(clearMemory()));
+ Button *readMemoryButton = createButton(tr("MR"), SLOT(readMemory()));
+ Button *setMemoryButton = createButton(tr("MS"), SLOT(setMemory()));
+ Button *addToMemoryButton = createButton(tr("M+"), SLOT(addToMemory()));
+
+ Button *divisionButton = createButton(tr("\303\267"), SLOT(multiplicativeOperatorClicked()));
+ Button *timesButton = createButton(tr("\303\227"), SLOT(multiplicativeOperatorClicked()));
+ Button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked()));
+ Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
+
+ Button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked()));
+ Button *powerButton = createButton(tr("x\302\262"), SLOT(unaryOperatorClicked()));
+ Button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked()));
+ Button *equalButton = createButton(tr("="), SLOT(equalClicked()));
+//! [4]
+
+//! [5]
+ QGridLayout *mainLayout = new QGridLayout;
+//! [5] //! [6]
+ mainLayout->setSizeConstraint(QLayout::SetFixedSize);
+ mainLayout->addWidget(display, 0, 0, 1, 6);
+ mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
+ mainLayout->addWidget(clearButton, 1, 2, 1, 2);
+ mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
+
+ mainLayout->addWidget(clearMemoryButton, 2, 0);
+ mainLayout->addWidget(readMemoryButton, 3, 0);
+ mainLayout->addWidget(setMemoryButton, 4, 0);
+ mainLayout->addWidget(addToMemoryButton, 5, 0);
+
+ for (int i = 1; i < NumDigitButtons; ++i) {
+ int row = ((9 - i) / 3) + 2;
+ int column = ((i - 1) % 3) + 1;
+ mainLayout->addWidget(digitButtons[i], row, column);
+ }
+
+ mainLayout->addWidget(digitButtons[0], 5, 1);
+ mainLayout->addWidget(pointButton, 5, 2);
+ mainLayout->addWidget(changeSignButton, 5, 3);
+
+ mainLayout->addWidget(divisionButton, 2, 4);
+ mainLayout->addWidget(timesButton, 3, 4);
+ mainLayout->addWidget(minusButton, 4, 4);
+ mainLayout->addWidget(plusButton, 5, 4);
+
+ mainLayout->addWidget(squareRootButton, 2, 5);
+ mainLayout->addWidget(powerButton, 3, 5);
+ mainLayout->addWidget(reciprocalButton, 4, 5);
+ mainLayout->addWidget(equalButton, 5, 5);
+ setLayout(mainLayout);
+
+ setWindowTitle(tr("Calculator"));
+}
+//! [6]
+
+//! [7]
+void Calculator::digitClicked()
+{
+ Button *clickedButton = qobject_cast<Button *>(sender());
+ int digitValue = clickedButton->text().toInt();
+ if (display->text() == "0" && digitValue == 0.0)
+ return;
+
+ if (waitingForOperand) {
+ display->clear();
+ waitingForOperand = false;
+ }
+ display->setText(display->text() + QString::number(digitValue));
+}
+//! [7]
+
+//! [8]
+void Calculator::unaryOperatorClicked()
+//! [8] //! [9]
+{
+ Button *clickedButton = qobject_cast<Button *>(sender());
+ QString clickedOperator = clickedButton->text();
+ double operand = display->text().toDouble();
+ double result = 0.0;
+
+ if (clickedOperator == tr("Sqrt")) {
+ if (operand < 0.0) {
+ abortOperation();
+ return;
+ }
+ result = sqrt(operand);
+ } else if (clickedOperator == tr("x\302\262")) {
+ result = pow(operand, 2.0);
+ } else if (clickedOperator == tr("1/x")) {
+ if (operand == 0.0) {
+ abortOperation();
+ return;
+ }
+ result = 1.0 / operand;
+ }
+ display->setText(QString::number(result));
+ waitingForOperand = true;
+}
+//! [9]
+
+//! [10]
+void Calculator::additiveOperatorClicked()
+//! [10] //! [11]
+{
+ Button *clickedButton = qobject_cast<Button *>(sender());
+ QString clickedOperator = clickedButton->text();
+ double operand = display->text().toDouble();
+
+//! [11] //! [12]
+ if (!pendingMultiplicativeOperator.isEmpty()) {
+//! [12] //! [13]
+ if (!calculate(operand, pendingMultiplicativeOperator)) {
+ abortOperation();
+ return;
+ }
+ display->setText(QString::number(factorSoFar));
+ operand = factorSoFar;
+ factorSoFar = 0.0;
+ pendingMultiplicativeOperator.clear();
+ }
+
+//! [13] //! [14]
+ if (!pendingAdditiveOperator.isEmpty()) {
+//! [14] //! [15]
+ if (!calculate(operand, pendingAdditiveOperator)) {
+ abortOperation();
+ return;
+ }
+ display->setText(QString::number(sumSoFar));
+ } else {
+ sumSoFar = operand;
+ }
+
+//! [15] //! [16]
+ pendingAdditiveOperator = clickedOperator;
+//! [16] //! [17]
+ waitingForOperand = true;
+}
+//! [17]
+
+//! [18]
+void Calculator::multiplicativeOperatorClicked()
+{
+ Button *clickedButton = qobject_cast<Button *>(sender());
+ QString clickedOperator = clickedButton->text();
+ double operand = display->text().toDouble();
+
+ if (!pendingMultiplicativeOperator.isEmpty()) {
+ if (!calculate(operand, pendingMultiplicativeOperator)) {
+ abortOperation();
+ return;
+ }
+ display->setText(QString::number(factorSoFar));
+ } else {
+ factorSoFar = operand;
+ }
+
+ pendingMultiplicativeOperator = clickedOperator;
+ waitingForOperand = true;
+}
+//! [18]
+
+//! [20]
+void Calculator::equalClicked()
+{
+ double operand = display->text().toDouble();
+
+ if (!pendingMultiplicativeOperator.isEmpty()) {
+ if (!calculate(operand, pendingMultiplicativeOperator)) {
+ abortOperation();
+ return;
+ }
+ operand = factorSoFar;
+ factorSoFar = 0.0;
+ pendingMultiplicativeOperator.clear();
+ }
+ if (!pendingAdditiveOperator.isEmpty()) {
+ if (!calculate(operand, pendingAdditiveOperator)) {
+ abortOperation();
+ return;
+ }
+ pendingAdditiveOperator.clear();
+ } else {
+ sumSoFar = operand;
+ }
+
+ display->setText(QString::number(sumSoFar));
+ sumSoFar = 0.0;
+ waitingForOperand = true;
+}
+//! [20]
+
+//! [22]
+void Calculator::pointClicked()
+{
+ if (waitingForOperand)
+ display->setText("0");
+ if (!display->text().contains("."))
+ display->setText(display->text() + tr("."));
+ waitingForOperand = false;
+}
+//! [22]
+
+//! [24]
+void Calculator::changeSignClicked()
+{
+ QString text = display->text();
+ double value = text.toDouble();
+
+ if (value > 0.0) {
+ text.prepend(tr("-"));
+ } else if (value < 0.0) {
+ text.remove(0, 1);
+ }
+ display->setText(text);
+}
+//! [24]
+
+//! [26]
+void Calculator::backspaceClicked()
+{
+ if (waitingForOperand)
+ return;
+
+ QString text = display->text();
+ text.chop(1);
+ if (text.isEmpty()) {
+ text = "0";
+ waitingForOperand = true;
+ }
+ display->setText(text);
+}
+//! [26]
+
+//! [28]
+void Calculator::clear()
+{
+ if (waitingForOperand)
+ return;
+
+ display->setText("0");
+ waitingForOperand = true;
+}
+//! [28]
+
+//! [30]
+void Calculator::clearAll()
+{
+ sumSoFar = 0.0;
+ factorSoFar = 0.0;
+ pendingAdditiveOperator.clear();
+ pendingMultiplicativeOperator.clear();
+ display->setText("0");
+ waitingForOperand = true;
+}
+//! [30]
+
+//! [32]
+void Calculator::clearMemory()
+{
+ sumInMemory = 0.0;
+}
+
+void Calculator::readMemory()
+{
+ display->setText(QString::number(sumInMemory));
+ waitingForOperand = true;
+}
+
+void Calculator::setMemory()
+{
+ equalClicked();
+ sumInMemory = display->text().toDouble();
+}
+
+void Calculator::addToMemory()
+{
+ equalClicked();
+ sumInMemory += display->text().toDouble();
+}
+//! [32]
+//! [34]
+Button *Calculator::createButton(const QString &text, const char *member)
+{
+ Button *button = new Button(text);
+ connect(button, SIGNAL(clicked()), this, member);
+ return button;
+}
+//! [34]
+
+//! [36]
+void Calculator::abortOperation()
+{
+ clearAll();
+ display->setText(tr("####"));
+}
+//! [36]
+
+//! [38]
+bool Calculator::calculate(double rightOperand, const QString &pendingOperator)
+{
+ if (pendingOperator == tr("+")) {
+ sumSoFar += rightOperand;
+ } else if (pendingOperator == tr("-")) {
+ sumSoFar -= rightOperand;
+ } else if (pendingOperator == tr("\303\227")) {
+ factorSoFar *= rightOperand;
+ } else if (pendingOperator == tr("\303\267")) {
+ if (rightOperand == 0.0)
+ return false;
+ factorSoFar /= rightOperand;
+ }
+ return true;
+}
+//! [38]
diff --git a/examples/widgets/widgets/calculator/calculator.desktop b/examples/widgets/widgets/calculator/calculator.desktop
new file mode 100644
index 0000000000..d0ae81d5a9
--- /dev/null
+++ b/examples/widgets/widgets/calculator/calculator.desktop
@@ -0,0 +1,11 @@
+[Desktop Entry]
+Encoding=UTF-8
+Version=1.0
+Type=Application
+Terminal=false
+Name=Calculator
+Exec=/opt/usr/bin/calculator
+Icon=calculator
+X-Window-Icon=
+X-HildonDesk-ShowInToolbar=true
+X-Osso-Type=application/x-executable
diff --git a/examples/widgets/widgets/calculator/calculator.h b/examples/widgets/widgets/calculator/calculator.h
new file mode 100644
index 0000000000..6727d9b733
--- /dev/null
+++ b/examples/widgets/widgets/calculator/calculator.h
@@ -0,0 +1,107 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef CALCULATOR_H
+#define CALCULATOR_H
+
+#include <QWidget>
+
+QT_BEGIN_NAMESPACE
+class QLineEdit;
+QT_END_NAMESPACE
+class Button;
+
+//! [0]
+class Calculator : public QWidget
+{
+ Q_OBJECT
+
+public:
+ Calculator(QWidget *parent = 0);
+
+private slots:
+ void digitClicked();
+ void unaryOperatorClicked();
+ void additiveOperatorClicked();
+ void multiplicativeOperatorClicked();
+ void equalClicked();
+ void pointClicked();
+ void changeSignClicked();
+ void backspaceClicked();
+ void clear();
+ void clearAll();
+ void clearMemory();
+ void readMemory();
+ void setMemory();
+ void addToMemory();
+//! [0]
+
+//! [1]
+private:
+//! [1] //! [2]
+ Button *createButton(const QString &text, const char *member);
+ void abortOperation();
+ bool calculate(double rightOperand, const QString &pendingOperator);
+//! [2]
+
+//! [3]
+ double sumInMemory;
+//! [3] //! [4]
+ double sumSoFar;
+//! [4] //! [5]
+ double factorSoFar;
+//! [5] //! [6]
+ QString pendingAdditiveOperator;
+//! [6] //! [7]
+ QString pendingMultiplicativeOperator;
+//! [7] //! [8]
+ bool waitingForOperand;
+//! [8]
+
+//! [9]
+ QLineEdit *display;
+//! [9] //! [10]
+
+ enum { NumDigitButtons = 10 };
+ Button *digitButtons[NumDigitButtons];
+};
+//! [10]
+
+#endif
diff --git a/examples/widgets/widgets/calculator/calculator.pro b/examples/widgets/widgets/calculator/calculator.pro
new file mode 100644
index 0000000000..d237871e0d
--- /dev/null
+++ b/examples/widgets/widgets/calculator/calculator.pro
@@ -0,0 +1,14 @@
+HEADERS = button.h \
+ calculator.h
+SOURCES = button.cpp \
+ calculator.cpp \
+ main.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/widgets/calculator
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS calculator.pro
+sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/widgets/calculator
+INSTALLS += target sources
+
+QT += widgets
+
diff --git a/examples/widgets/widgets/calculator/main.cpp b/examples/widgets/widgets/calculator/main.cpp
new file mode 100644
index 0000000000..e4f591aabe
--- /dev/null
+++ b/examples/widgets/widgets/calculator/main.cpp
@@ -0,0 +1,51 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QApplication>
+
+#include "calculator.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+ Calculator calc;
+ calc.show();
+ return app.exec();
+}
diff --git a/examples/widgets/widgets/calculator/releasenotes.txt b/examples/widgets/widgets/calculator/releasenotes.txt
new file mode 100644
index 0000000000..053f651dba
--- /dev/null
+++ b/examples/widgets/widgets/calculator/releasenotes.txt
@@ -0,0 +1,4 @@
+Calculator
+=============
+
+Compared to the original example in the Qt SDK, the Calculator class (calculator.h) has been derived from QWidget instead of QDialog because in Maemo you cannot have any additional controls with dialogs. The mainLayout size constraint has been changed to SetNoConstraint to enable immediate scaling of the grid in Symbian. Screen definition for Symbian has been changed to showMaximized() to enable correct scaling of the application (see main.cpp). \ No newline at end of file