diff options
Diffstat (limited to 'examples/script/calculator')
-rw-r--r-- | examples/script/calculator/calculator.js | 264 | ||||
-rw-r--r-- | examples/script/calculator/calculator.pro | 12 | ||||
-rw-r--r-- | examples/script/calculator/calculator.qrc | 6 | ||||
-rw-r--r-- | examples/script/calculator/calculator.ui | 406 | ||||
-rw-r--r-- | examples/script/calculator/main.cpp | 101 |
5 files changed, 789 insertions, 0 deletions
diff --git a/examples/script/calculator/calculator.js b/examples/script/calculator/calculator.js new file mode 100644 index 0000000..62c2cba --- /dev/null +++ b/examples/script/calculator/calculator.js @@ -0,0 +1,264 @@ +//! [0] +function Calculator(ui) +{ + this.ui = ui; + + this.pendingAdditiveOperator = ""; + this.pendingMultiplicativeOperator = ""; + this.sumInMemory = 0; + this.sumSoFar = 0; + this.factorSoFar = 0; + this.waitingForOperand = true; + + with (ui) { + display.text = "0"; + + zeroButton.clicked.connect(this, this.digitClicked); + oneButton.clicked.connect(this, "digitClicked"); + twoButton.clicked.connect(this, "digitClicked"); + threeButton.clicked.connect(this, "digitClicked"); + fourButton.clicked.connect(this, "digitClicked"); + fiveButton.clicked.connect(this, "digitClicked"); + sixButton.clicked.connect(this, "digitClicked"); + sevenButton.clicked.connect(this, "digitClicked"); + eightButton.clicked.connect(this, "digitClicked"); + nineButton.clicked.connect(this, "digitClicked"); + + pointButton.clicked.connect(this, "pointClicked"); + changeSignButton.clicked.connect(this, "changeSignClicked"); + + backspaceButton.clicked.connect(this, "backspaceClicked"); + clearButton.clicked.connect(this, "clear"); + clearAllButton.clicked.connect(this, "clearAll"); + + clearMemoryButton.clicked.connect(this, "clearMemory"); + readMemoryButton.clicked.connect(this, "readMemory"); + setMemoryButton.clicked.connect(this, "setMemory"); + addToMemoryButton.clicked.connect(this, "addToMemory"); + + divisionButton.clicked.connect(this, "multiplicativeOperatorClicked"); + timesButton.clicked.connect(this, "multiplicativeOperatorClicked"); + minusButton.clicked.connect(this, "additiveOperatorClicked"); + plusButton.clicked.connect(this, "additiveOperatorClicked"); + + squareRootButton.clicked.connect(this, "unaryOperatorClicked"); + powerButton.clicked.connect(this, "unaryOperatorClicked"); + reciprocalButton.clicked.connect(this, "unaryOperatorClicked"); + equalButton.clicked.connect(this, "equalClicked"); + } +} +//! [0] + +Calculator.prototype.abortOperation = function() +{ + this.clearAll(); + this.ui.display.text = "####"; +} + +Calculator.prototype.calculate = function(rightOperand, pendingOperator) +{ + if (pendingOperator == "+") { + this.sumSoFar += rightOperand; + } else if (pendingOperator == "-") { + this.sumSoFar -= rightOperand; + } else if (pendingOperator == "*") { + this.factorSoFar *= rightOperand; + } else if (pendingOperator == "/") { + if (rightOperand == 0) + return false; + this.factorSoFar /= rightOperand; + } + return true; +} + +//! [1] +Calculator.prototype.digitClicked = function() +{ + var digitValue = __qt_sender__.text - 0; + if ((digitValue == 0) && (this.ui.display.text == "0")) + return; + if (this.waitingForOperand) { + this.ui.display.clear(); + this.waitingForOperand = false; + } + this.ui.display.text += digitValue; +} +//! [1] + +Calculator.prototype.unaryOperatorClicked = function() +{ + var operand = this.ui.display.text - 0; + var result = 0; + if (__qt_sender__.text == "Sqrt") { + if (operand < 0) { + this.abortOperation(); + return; + } + result = Math.sqrt(operand); + } else if (__qt_sender__.text == "x^2") { + result = Math.pow(operand, 2); + } else if (__qt_sender__.text == "1/x") { + if (operand == 0.0) { + this.abortOperation(); + return; + } + result = 1 / operand; + } + this.ui.display.text = result + ""; + this.waitingForOperand = true; +} + +Calculator.prototype.additiveOperatorClicked = function() +{ + var operand = this.ui.display.text - 0; + + if (this.pendingMultiplicativeOperator.length != 0) { + if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { + this.abortOperation(); + return; + } + this.ui.display.text = this.factorSoFar + ""; + operand = this.factorSoFar; + this.factorSoFar = 0; + this.pendingMultiplicativeOperator = ""; + } + + if (this.pendingAdditiveOperator.length != 0) { + if (!this.calculate(operand, this.pendingAdditiveOperator)) { + this.abortOperation(); + return; + } + this.ui.display.text = this.sumSoFar + ""; + } else { + this.sumSoFar = operand; + } + + this.pendingAdditiveOperator = __qt_sender__.text; + this.waitingForOperand = true; +} + +Calculator.prototype.multiplicativeOperatorClicked = function() +{ + var operand = this.ui.display.text - 0; + + if (this.pendingMultiplicativeOperator.length != 0) { + if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { + this.abortOperation(); + return; + } + this.ui.display.text = this.factorSoFar + ""; + } else { + this.factorSoFar = operand; + } + + this.pendingMultiplicativeOperator = __qt_sender__.text; + this.waitingForOperand = true; +} + +Calculator.prototype.equalClicked = function() +{ + var operand = this.ui.display.text - 0; + + if (this.pendingMultiplicativeOperator.length != 0) { + if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { + this.abortOperation(); + return; + } + operand = this.factorSoFar; + this.factorSoFar = 0.0; + this.pendingMultiplicativeOperator = ""; + } + if (this.pendingAdditiveOperator.length != 0) { + if (!this.calculate(operand, this.pendingAdditiveOperator)) { + this.abortOperation(); + return; + } + this.pendingAdditiveOperator = ""; + } else { + this.sumSoFar = operand; + } + + this.ui.display.text = this.sumSoFar + ""; + this.sumSoFar = 0.0; + this.waitingForOperand = true; +} + +Calculator.prototype.pointClicked = function() +{ + if (this.waitingForOperand) + this.ui.display.text = "0"; + if (this.ui.display.text.indexOf(".") == -1) + this.ui.display.text += "."; + this.waitingForOperand = false; +} + +//! [2] +Calculator.prototype.changeSignClicked = function() +{ + var text = this.ui.display.text; + var value = text - 0; + + if (value > 0) { + text = "-" + text; + } else if (value < 0) { + text = text.slice(1); + } + this.ui.display.text = text; +} +//! [2] + +Calculator.prototype.backspaceClicked = function() +{ + if (this.waitingForOperand) + return; + + var text = this.ui.display.text; + text = text.slice(0, -1); + if (text.length == 0) { + text = "0"; + this.waitingForOperand = true; + } + this.ui.display.text = text; +} + +Calculator.prototype.clear = function() +{ + if (this.waitingForOperand) + return; + + this.ui.display.text = "0"; + this.waitingForOperand = true; +} + +Calculator.prototype.clearAll = function() +{ + this.sumSoFar = 0.0; + this.factorSoFar = 0.0; + this.pendingAdditiveOperator = ""; + this.pendingMultiplicativeOperator = ""; + this.ui.display.text = "0"; + this.waitingForOperand = true; +} + +Calculator.prototype.clearMemory = function() +{ + this.sumInMemory = 0.0; +} + +Calculator.prototype.readMemory = function() +{ + this.ui.display.text = this.sumInMemory + ""; + this.waitingForOperand = true; +} + +Calculator.prototype.setMemory = function() +{ + this.equalClicked(); + this.sumInMemory = this.ui.display.text - 0; +} + +Calculator.prototype.addToMemory = function() +{ + this.equalClicked(); + this.sumInMemory += this.ui.display.text - 0; +} diff --git a/examples/script/calculator/calculator.pro b/examples/script/calculator/calculator.pro new file mode 100644 index 0000000..226d5f4 --- /dev/null +++ b/examples/script/calculator/calculator.pro @@ -0,0 +1,12 @@ +QT += script +CONFIG += uitools +RESOURCES += calculator.qrc +SOURCES += main.cpp + +contains(QT_CONFIG, scripttools): QT += scripttools + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/script/calculator +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS *.pro *.js *.ui +sources.path = $$[QT_INSTALL_EXAMPLES]/script/calculator +INSTALLS += target sources diff --git a/examples/script/calculator/calculator.qrc b/examples/script/calculator/calculator.qrc new file mode 100644 index 0000000..afa6686 --- /dev/null +++ b/examples/script/calculator/calculator.qrc @@ -0,0 +1,6 @@ +<RCC> + <qresource prefix="/" > + <file>calculator.js</file> + <file>calculator.ui</file> + </qresource> +</RCC> diff --git a/examples/script/calculator/calculator.ui b/examples/script/calculator/calculator.ui new file mode 100644 index 0000000..bb519ba --- /dev/null +++ b/examples/script/calculator/calculator.ui @@ -0,0 +1,406 @@ +<ui version="4.0" > + <class>Calculator</class> + <widget class="QWidget" name="Calculator" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>314</width> + <height>301</height> + </rect> + </property> + <property name="sizePolicy" > + <sizepolicy vsizetype="Fixed" hsizetype="Fixed" > + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize" > + <size> + <width>314</width> + <height>301</height> + </size> + </property> + <property name="maximumSize" > + <size> + <width>314</width> + <height>301</height> + </size> + </property> + <property name="windowTitle" > + <string>Calculator</string> + </property> + <widget class="QToolButton" name="backspaceButton" > + <property name="geometry" > + <rect> + <x>10</x> + <y>50</y> + <width>91</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>Backspace</string> + </property> + </widget> + <widget class="QToolButton" name="clearButton" > + <property name="geometry" > + <rect> + <x>110</x> + <y>50</y> + <width>91</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>Clear</string> + </property> + </widget> + <widget class="QToolButton" name="clearAllButton" > + <property name="geometry" > + <rect> + <x>210</x> + <y>50</y> + <width>91</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>Clear All</string> + </property> + </widget> + <widget class="QToolButton" name="clearMemoryButton" > + <property name="geometry" > + <rect> + <x>10</x> + <y>100</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>MC</string> + </property> + </widget> + <widget class="QToolButton" name="readMemoryButton" > + <property name="geometry" > + <rect> + <x>10</x> + <y>150</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>MR</string> + </property> + </widget> + <widget class="QToolButton" name="setMemoryButton" > + <property name="geometry" > + <rect> + <x>10</x> + <y>200</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>MS</string> + </property> + </widget> + <widget class="QToolButton" name="addToMemoryButton" > + <property name="geometry" > + <rect> + <x>10</x> + <y>250</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>M+</string> + </property> + </widget> + <widget class="QToolButton" name="sevenButton" > + <property name="geometry" > + <rect> + <x>60</x> + <y>100</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>7</string> + </property> + </widget> + <widget class="QToolButton" name="eightButton" > + <property name="geometry" > + <rect> + <x>110</x> + <y>100</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>8</string> + </property> + </widget> + <widget class="QToolButton" name="nineButton" > + <property name="geometry" > + <rect> + <x>160</x> + <y>100</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>9</string> + </property> + </widget> + <widget class="QToolButton" name="fourButton" > + <property name="geometry" > + <rect> + <x>60</x> + <y>150</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>4</string> + </property> + </widget> + <widget class="QToolButton" name="fiveButton" > + <property name="geometry" > + <rect> + <x>110</x> + <y>150</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>5</string> + </property> + </widget> + <widget class="QToolButton" name="sixButton" > + <property name="geometry" > + <rect> + <x>160</x> + <y>150</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>6</string> + </property> + </widget> + <widget class="QToolButton" name="oneButton" > + <property name="geometry" > + <rect> + <x>60</x> + <y>200</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>1</string> + </property> + </widget> + <widget class="QToolButton" name="twoButton" > + <property name="geometry" > + <rect> + <x>110</x> + <y>200</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>2</string> + </property> + </widget> + <widget class="QToolButton" name="threeButton" > + <property name="geometry" > + <rect> + <x>160</x> + <y>200</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>3</string> + </property> + </widget> + <widget class="QToolButton" name="zeroButton" > + <property name="geometry" > + <rect> + <x>60</x> + <y>250</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>0</string> + </property> + </widget> + <widget class="QToolButton" name="pointButton" > + <property name="geometry" > + <rect> + <x>110</x> + <y>250</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>.</string> + </property> + </widget> + <widget class="QToolButton" name="changeSignButton" > + <property name="geometry" > + <rect> + <x>160</x> + <y>250</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>+-</string> + </property> + </widget> + <widget class="QToolButton" name="plusButton" > + <property name="geometry" > + <rect> + <x>210</x> + <y>250</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>+</string> + </property> + </widget> + <widget class="QToolButton" name="divisionButton" > + <property name="geometry" > + <rect> + <x>210</x> + <y>100</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>/</string> + </property> + </widget> + <widget class="QToolButton" name="timesButton" > + <property name="geometry" > + <rect> + <x>210</x> + <y>150</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>*</string> + </property> + </widget> + <widget class="QToolButton" name="minusButton" > + <property name="geometry" > + <rect> + <x>210</x> + <y>200</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>-</string> + </property> + </widget> + <widget class="QToolButton" name="squareRootButton" > + <property name="geometry" > + <rect> + <x>260</x> + <y>100</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>Sqrt</string> + </property> + </widget> + <widget class="QToolButton" name="powerButton" > + <property name="geometry" > + <rect> + <x>260</x> + <y>150</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>x^2</string> + </property> + </widget> + <widget class="QToolButton" name="reciprocalButton" > + <property name="geometry" > + <rect> + <x>260</x> + <y>200</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>1/x</string> + </property> + </widget> + <widget class="QToolButton" name="equalButton" > + <property name="geometry" > + <rect> + <x>260</x> + <y>250</y> + <width>41</width> + <height>41</height> + </rect> + </property> + <property name="text" > + <string>=</string> + </property> + </widget> + <widget class="QLineEdit" name="display" > + <property name="geometry" > + <rect> + <x>10</x> + <y>10</y> + <width>291</width> + <height>31</height> + </rect> + </property> + <property name="maxLength" > + <number>15</number> + </property> + <property name="alignment" > + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + <property name="readOnly" > + <bool>true</bool> + </property> + </widget> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/script/calculator/main.cpp b/examples/script/calculator/main.cpp new file mode 100644 index 0000000..4608aa5 --- /dev/null +++ b/examples/script/calculator/main.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include <QUiLoader> +#include <QtScript> +#include <QWidget> +#include <QFile> +#include <QMainWindow> +#include <QLineEdit> + +#ifndef QT_NO_SCRIPTTOOLS +#include <QScriptEngineDebugger> +#endif + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(calculator); + + QApplication app(argc, argv); +//! [0a] + QScriptEngine engine; +//! [0a] + +#ifndef QT_NO_SCRIPTTOOLS + QScriptEngineDebugger debugger; + debugger.attachTo(&engine); + QMainWindow *debugWindow = debugger.standardWindow(); + debugWindow->resize(1024, 640); +#endif + +//! [0b] + QString scriptFileName(":/calculator.js"); + QFile scriptFile(scriptFileName); + scriptFile.open(QIODevice::ReadOnly); + engine.evaluate(scriptFile.readAll(), scriptFileName); + scriptFile.close(); +//! [0b] + +//! [1] + QUiLoader loader; + QFile uiFile(":/calculator.ui"); + uiFile.open(QIODevice::ReadOnly); + QWidget *ui = loader.load(&uiFile); + uiFile.close(); +//! [1] + +//! [2] + QScriptValue ctor = engine.evaluate("Calculator"); + QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership); + QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi); +//! [2] + +#ifndef QT_NO_SCRIPTTOOLS + QLineEdit *display = qFindChild<QLineEdit*>(ui, "display"); + QObject::connect(display, SIGNAL(returnPressed()), + debugWindow, SLOT(show())); +#endif +//! [3] + ui->show(); + return app.exec(); +//! [3] +} |