Removed CONFIG+=parallel_test from suspected parallel-unsafe tests
[profile/ivi/qtdeclarative.git] / examples / demos / calculator / content / calculator.js
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 var curVal = 0
42 var memory = 0
43 var lastOp = ""
44 var timer = 0
45
46 function disabled(op) {
47     if (op == "." && display.text.toString().search(/\./) != -1) {
48         return true
49     } else if (op == window.squareRoot &&  display.text.toString().search(/-/) != -1) {
50         return true
51     } else {
52         return false
53     }
54 }
55
56 function doOperation(op) {
57     if (op == '*')//Keyboard Aliases
58         op = window.multiplication;
59     if (op == '/')
60         op = window.division;
61     if (disabled(op)) {
62         return
63     }
64
65     if (op.toString().length==1 && ((op >= "0" && op <= "9") || op==".") ) {
66         if (display.text.toString().length >= 14)
67             return; // No arbitrary length numbers
68         if (lastOp.toString().length == 1 && ((lastOp >= "0" && lastOp <= "9") || lastOp == ".") ) {
69             display.text = display.text + op.toString()
70         } else {
71             display.text = op
72         }
73         lastOp = op
74         return
75     }
76     lastOp = op
77
78     if (display.currentOperation.text == "+") {
79         display.text = Number(display.text.valueOf()) + Number(curVal.valueOf())
80     } else if (display.currentOperation.text == "-") {
81         display.text = Number(curVal) - Number(display.text.valueOf())
82     } else if (display.currentOperation.text == window.multiplication) {
83         display.text = Number(curVal) * Number(display.text.valueOf())
84     } else if (display.currentOperation.text == window.division) {
85         display.text = Number(Number(curVal) / Number(display.text.valueOf())).toString()
86     } else if (display.currentOperation.text == "=") {
87     }
88
89     if (op == "+" || op == "-" || op == window.multiplication || op == window.division) {
90         display.currentOperation.text = op
91         curVal = display.text.valueOf()
92         return
93     }
94
95     curVal = 0
96     display.currentOperation.text = ""
97
98     if (op == "1/x") {
99         display.text = (1 / display.text.valueOf()).toString()
100     } else if (op == "x^2") {
101         display.text = (display.text.valueOf() * display.text.valueOf()).toString()
102     } else if (op == "Abs") {
103         display.text = (Math.abs(display.text.valueOf())).toString()
104     } else if (op == "Int") {
105         display.text = (Math.floor(display.text.valueOf())).toString()
106     } else if (op == window.plusminus) {
107         display.text = (display.text.valueOf() * -1).toString()
108     } else if (op == window.squareRoot) {
109         display.text = (Math.sqrt(display.text.valueOf())).toString()
110     } else if (op == "mc") {
111         memory = 0;
112     } else if (op == "m+") {
113         memory += display.text.valueOf()
114     } else if (op == "mr") {
115         display.text = memory.toString()
116     } else if (op == "m-") {
117         memory = display.text.valueOf()
118     } else if (op == window.leftArrow) {
119         display.text = display.text.toString().slice(0, -1)
120         if (display.text.length == 0) {
121             display.text = "0"
122         }
123     } else if (op == "Off") {
124         Qt.quit();
125     } else if (op == "C") {
126         display.text = "0"
127     } else if (op == "AC") {
128         curVal = 0
129         memory = 0
130         lastOp = ""
131         display.text ="0"
132     }
133 }
134