aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/demos/calqlatr/content/calculator.js
diff options
context:
space:
mode:
authorTopi Reinio <topi.reinio@digia.com>2014-09-24 14:37:46 +0200
committerTopi Reiniö <topi.reinio@digia.com>2014-09-30 12:37:28 +0200
commitf83d12e0c27ad76d98d66a663140a09698b11d37 (patch)
tree6ce1e66cd651c8b325f5a6790ea036e7bbe62c47 /examples/quick/demos/calqlatr/content/calculator.js
parentfccf0e29120f64a8b9b2a15b33971a648ab190e7 (diff)
Update the Calqltr demo visuals and engine logic
- Add logic for displaying the calculation result in the best available precision, determined by the display width - Display 'ERROR' when the result cannot be displayed - Animate the number pad button colors to react to presses and visually disable them when pressing the button has no effect - Fix issues in calculator.js logic - Update documentation accordingly Task-number: QTBUG-41253 Change-Id: Ibed7b8218ea4cd074b8f9b90d9bb4e3ea6b25ba2 Reviewed-by: Johanna Äijälä <johanna.aijala@digia.com> Reviewed-by: Leena Miettinen <riitta-leena.miettinen@digia.com> Reviewed-by: Venugopal Shivashankar <venugopal.shivashankar@digia.com>
Diffstat (limited to 'examples/quick/demos/calqlatr/content/calculator.js')
-rw-r--r--examples/quick/demos/calqlatr/content/calculator.js43
1 files changed, 22 insertions, 21 deletions
diff --git a/examples/quick/demos/calqlatr/content/calculator.js b/examples/quick/demos/calqlatr/content/calculator.js
index 906cb9d3b7..841ba9414c 100644
--- a/examples/quick/demos/calqlatr/content/calculator.js
+++ b/examples/quick/demos/calqlatr/content/calculator.js
@@ -38,7 +38,6 @@
**
****************************************************************************/
-
var curVal = 0
var memory = 0
var lastOp = ""
@@ -46,9 +45,13 @@ var previousOperator = ""
var digits = ""
function disabled(op) {
- if (op == "." && digits.toString().search(/\./) != -1) {
+ if (digits == "" && !((op >= "0" && op <= "9") || op == "."))
+ return true
+ else if (op == '=' && previousOperator.length != 1)
return true
- } else if (op == window.squareRoot && digits.toString().search(/-/) != -1) {
+ else if (op == "." && digits.toString().search(/\./) != -1) {
+ return true
+ } else if (op == "√" && digits.toString().search(/-/) != -1) {
return true
} else {
return false
@@ -59,7 +62,7 @@ function digitPressed(op)
{
if (disabled(op))
return
- if (digits.toString().length >= 8)
+ if (digits.toString().length >= display.maxDigits)
return
if (lastOp.toString().length == 1 && ((lastOp >= "0" && lastOp <= "9") || lastOp == ".") ) {
digits = digits + op.toString()
@@ -77,6 +80,12 @@ function operatorPressed(op)
return
lastOp = op
+ if (op == "±") {
+ digits = Number(digits.valueOf() * -1)
+ display.setDigit(display.displayNumber(digits))
+ return
+ }
+
if (previousOperator == "+") {
digits = Number(digits.valueOf()) + Number(curVal.valueOf())
} else if (previousOperator == "−") {
@@ -85,7 +94,6 @@ function operatorPressed(op)
digits = Number(curVal) * Number(digits.valueOf())
} else if (previousOperator == "÷") {
digits = Number(curVal) / Number(digits.valueOf())
- } else if (previousOperator == "=") {
}
if (op == "+" || op == "−" || op == "×" || op == "÷") {
@@ -97,10 +105,7 @@ function operatorPressed(op)
}
if (op == "=") {
- if (digits.toString().length >= 9)
- digits = digits.toExponential(2)
-
- display.newLine("=", digits.toString())
+ display.newLine("=", digits.valueOf())
}
curVal = 0
@@ -114,12 +119,9 @@ function operatorPressed(op)
digits = (Math.abs(digits.valueOf())).toString()
} else if (op == "Int") {
digits = (Math.floor(digits.valueOf())).toString()
- } else if (op == "±") {
- digits = (digits.valueOf() * -1).toString()
- display.clear()
- display.appendDigit(digits)
} else if (op == "√") {
- digits = (Math.sqrt(digits.valueOf())).toString()
+ digits = Number(Math.sqrt(digits.valueOf()))
+ display.newLine("√", digits.valueOf())
} else if (op == "mc") {
memory = 0;
} else if (op == "m+") {
@@ -134,17 +136,16 @@ function operatorPressed(op)
display.appendDigit(digits)
} else if (op == "Off") {
Qt.quit();
- } else if (op == "C") {
+ }
+
+ // Reset the state on 'C' operator or after
+ // an error occurred
+ if (op == "C" || display.isError) {
display.clear()
curVal = 0
memory = 0
lastOp = ""
- digits ="0"
- } else if (op == "AC") {
- curVal = 0
- memory = 0
- lastOp = ""
- digits ="0"
+ digits = ""
}
}