summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-06-30 16:03:36 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-06-30 16:03:36 (GMT)
commitfa5e77e0dd45324d60f7dfacfdcaaf0951d4a29d (patch)
tree6b68bec690ab6e486f8cadd2474808675340b53e
parent83a1e1164468f8bf7ad87b67f25becc52eabfa01 (diff)
downloadQt-fa5e77e0dd45324d60f7dfacfdcaaf0951d4a29d.zip
Qt-fa5e77e0dd45324d60f7dfacfdcaaf0951d4a29d.tar.gz
Qt-fa5e77e0dd45324d60f7dfacfdcaaf0951d4a29d.tar.bz2
make the calculator example use only standard JS
__qt_sender__ is evil. Gah.
-rw-r--r--examples/script/calculator/calculator.js107
1 files changed, 62 insertions, 45 deletions
diff --git a/examples/script/calculator/calculator.js b/examples/script/calculator/calculator.js
index 62c2cba..ac3c1b6 100644
--- a/examples/script/calculator/calculator.js
+++ b/examples/script/calculator/calculator.js
@@ -1,10 +1,19 @@
+Function.prototype.bind = function() {
+ var func = this;
+ var thisObject = arguments[0];
+ var args = Array.prototype.slice.call(arguments, 1);
+ return function() {
+ return func.apply(thisObject, args);
+ }
+}
+
//! [0]
function Calculator(ui)
{
this.ui = ui;
- this.pendingAdditiveOperator = "";
- this.pendingMultiplicativeOperator = "";
+ this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
+ this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
this.sumInMemory = 0;
this.sumSoFar = 0;
this.factorSoFar = 0;
@@ -13,16 +22,16 @@ function Calculator(ui)
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");
+ zeroButton.clicked.connect(this.digitClicked.bind(this, 0));
+ oneButton.clicked.connect(this.digitClicked.bind(this, 1));
+ twoButton.clicked.connect(this.digitClicked.bind(this, 2));
+ threeButton.clicked.connect(this.digitClicked.bind(this, 3));
+ fourButton.clicked.connect(this.digitClicked.bind(this, 4));
+ fiveButton.clicked.connect(this.digitClicked.bind(this, 5));
+ sixButton.clicked.connect(this.digitClicked.bind(this, 6));
+ sevenButton.clicked.connect(this.digitClicked.bind(this, 7));
+ eightButton.clicked.connect(this.digitClicked.bind(this, 8));
+ nineButton.clicked.connect(this.digitClicked.bind(this, 9));
pointButton.clicked.connect(this, "pointClicked");
changeSignButton.clicked.connect(this, "changeSignClicked");
@@ -36,19 +45,28 @@ function Calculator(ui)
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");
+ divisionButton.clicked.connect(this.multiplicativeOperatorClicked.bind(this, Calculator.DIVISION_OPERATOR));
+ timesButton.clicked.connect(this.multiplicativeOperatorClicked.bind(this, Calculator.TIMES_OPERATOR));
+ minusButton.clicked.connect(this.additiveOperatorClicked.bind(this, Calculator.MINUS_OPERATOR));
+ plusButton.clicked.connect(this.additiveOperatorClicked.bind(this, Calculator.PLUS_OPERATOR));
+
+ squareRootButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.SQUARE_OPERATOR));
+ powerButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.POWER_OPERATOR));
+ reciprocalButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.RECIPROCAL_OPERATOR));
equalButton.clicked.connect(this, "equalClicked");
}
}
//! [0]
+Calculator.NO_OPERATOR = 0;
+Calculator.SQUARE_OPERATOR = 1;
+Calculator.POWER_OPERATOR = 2;
+Calculator.RECIPROCAL_OPERATOR = 3;
+Calculator.DIVISION_OPERATOR = 4;
+Calculator.TIMES_OPERATOR = 5;
+Calculator.MINUS_OPERATOR = 6;
+Calculator.PLUS_OPERATOR = 7;
+
Calculator.prototype.abortOperation = function()
{
this.clearAll();
@@ -57,24 +75,23 @@ Calculator.prototype.abortOperation = function()
Calculator.prototype.calculate = function(rightOperand, pendingOperator)
{
- if (pendingOperator == "+") {
+ if (pendingOperator == Calculator.PLUS_OPERATOR) {
this.sumSoFar += rightOperand;
- } else if (pendingOperator == "-") {
+ } else if (pendingOperator == Calculator.MINUS_OPERATOR) {
this.sumSoFar -= rightOperand;
- } else if (pendingOperator == "*") {
+ } else if (pendingOperator == Calculator.TIMES_OPERATOR) {
this.factorSoFar *= rightOperand;
- } else if (pendingOperator == "/") {
+ } else if (pendingOperator == Calculator.DIVISION_OPERATOR) {
if (rightOperand == 0)
- return false;
+ return false;
this.factorSoFar /= rightOperand;
}
return true;
}
//! [1]
-Calculator.prototype.digitClicked = function()
+Calculator.prototype.digitClicked = function(digitValue)
{
- var digitValue = __qt_sender__.text - 0;
if ((digitValue == 0) && (this.ui.display.text == "0"))
return;
if (this.waitingForOperand) {
@@ -85,19 +102,19 @@ Calculator.prototype.digitClicked = function()
}
//! [1]
-Calculator.prototype.unaryOperatorClicked = function()
+Calculator.prototype.unaryOperatorClicked = function(op)
{
var operand = this.ui.display.text - 0;
var result = 0;
- if (__qt_sender__.text == "Sqrt") {
+ if (op == Calculator.SQUARE_OPERATOR) {
if (operand < 0) {
this.abortOperation();
return;
}
result = Math.sqrt(operand);
- } else if (__qt_sender__.text == "x^2") {
+ } else if (op == Calculator.POWER_OPERATOR) {
result = Math.pow(operand, 2);
- } else if (__qt_sender__.text == "1/x") {
+ } else if (op == Calculator.RECIPROCAL_OPERATOR) {
if (operand == 0.0) {
this.abortOperation();
return;
@@ -108,11 +125,11 @@ Calculator.prototype.unaryOperatorClicked = function()
this.waitingForOperand = true;
}
-Calculator.prototype.additiveOperatorClicked = function()
+Calculator.prototype.additiveOperatorClicked = function(op)
{
var operand = this.ui.display.text - 0;
- if (this.pendingMultiplicativeOperator.length != 0) {
+ if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
this.abortOperation();
return;
@@ -120,10 +137,10 @@ Calculator.prototype.additiveOperatorClicked = function()
this.ui.display.text = this.factorSoFar + "";
operand = this.factorSoFar;
this.factorSoFar = 0;
- this.pendingMultiplicativeOperator = "";
+ this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
}
- if (this.pendingAdditiveOperator.length != 0) {
+ if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) {
if (!this.calculate(operand, this.pendingAdditiveOperator)) {
this.abortOperation();
return;
@@ -133,15 +150,15 @@ Calculator.prototype.additiveOperatorClicked = function()
this.sumSoFar = operand;
}
- this.pendingAdditiveOperator = __qt_sender__.text;
+ this.pendingAdditiveOperator = op;
this.waitingForOperand = true;
}
-Calculator.prototype.multiplicativeOperatorClicked = function()
+Calculator.prototype.multiplicativeOperatorClicked = function(op)
{
var operand = this.ui.display.text - 0;
- if (this.pendingMultiplicativeOperator.length != 0) {
+ if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
this.abortOperation();
return;
@@ -151,7 +168,7 @@ Calculator.prototype.multiplicativeOperatorClicked = function()
this.factorSoFar = operand;
}
- this.pendingMultiplicativeOperator = __qt_sender__.text;
+ this.pendingMultiplicativeOperator = op;
this.waitingForOperand = true;
}
@@ -159,21 +176,21 @@ Calculator.prototype.equalClicked = function()
{
var operand = this.ui.display.text - 0;
- if (this.pendingMultiplicativeOperator.length != 0) {
+ if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
this.abortOperation();
return;
}
operand = this.factorSoFar;
this.factorSoFar = 0.0;
- this.pendingMultiplicativeOperator = "";
+ this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
}
- if (this.pendingAdditiveOperator.length != 0) {
+ if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) {
if (!this.calculate(operand, this.pendingAdditiveOperator)) {
this.abortOperation();
return;
}
- this.pendingAdditiveOperator = "";
+ this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
} else {
this.sumSoFar = operand;
}
@@ -234,8 +251,8 @@ Calculator.prototype.clearAll = function()
{
this.sumSoFar = 0.0;
this.factorSoFar = 0.0;
- this.pendingAdditiveOperator = "";
- this.pendingMultiplicativeOperator = "";
+ this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
+ this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
this.ui.display.text = "0";
this.waitingForOperand = true;
}