Changeset 561 for trunk/examples/script/calculator
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/examples/script/calculator/calculator.js
r2 r561 1 Function.prototype.bind = function() { 2 var func = this; 3 var thisObject = arguments[0]; 4 var args = Array.prototype.slice.call(arguments, 1); 5 return function() { 6 return func.apply(thisObject, args); 7 } 8 } 9 1 10 //! [0] 2 11 function Calculator(ui) … … 4 13 this.ui = ui; 5 14 6 this.pendingAdditiveOperator = "";7 this.pendingMultiplicativeOperator = "";15 this.pendingAdditiveOperator = Calculator.NO_OPERATOR; 16 this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR; 8 17 this.sumInMemory = 0; 9 18 this.sumSoFar = 0; … … 14 23 display.text = "0"; 15 24 16 zeroButton.clicked.connect(this , this.digitClicked);17 oneButton.clicked.connect(this , "digitClicked");18 twoButton.clicked.connect(this , "digitClicked");19 threeButton.clicked.connect(this , "digitClicked");20 fourButton.clicked.connect(this , "digitClicked");21 fiveButton.clicked.connect(this , "digitClicked");22 sixButton.clicked.connect(this , "digitClicked");23 sevenButton.clicked.connect(this , "digitClicked");24 eightButton.clicked.connect(this , "digitClicked");25 nineButton.clicked.connect(this , "digitClicked");25 zeroButton.clicked.connect(this.digitClicked.bind(this, 0)); 26 oneButton.clicked.connect(this.digitClicked.bind(this, 1)); 27 twoButton.clicked.connect(this.digitClicked.bind(this, 2)); 28 threeButton.clicked.connect(this.digitClicked.bind(this, 3)); 29 fourButton.clicked.connect(this.digitClicked.bind(this, 4)); 30 fiveButton.clicked.connect(this.digitClicked.bind(this, 5)); 31 sixButton.clicked.connect(this.digitClicked.bind(this, 6)); 32 sevenButton.clicked.connect(this.digitClicked.bind(this, 7)); 33 eightButton.clicked.connect(this.digitClicked.bind(this, 8)); 34 nineButton.clicked.connect(this.digitClicked.bind(this, 9)); 26 35 27 36 pointButton.clicked.connect(this, "pointClicked"); … … 37 46 addToMemoryButton.clicked.connect(this, "addToMemory"); 38 47 39 divisionButton.clicked.connect(this , "multiplicativeOperatorClicked");40 timesButton.clicked.connect(this , "multiplicativeOperatorClicked");41 minusButton.clicked.connect(this , "additiveOperatorClicked");42 plusButton.clicked.connect(this , "additiveOperatorClicked");43 44 squareRootButton.clicked.connect(this , "unaryOperatorClicked");45 powerButton.clicked.connect(this , "unaryOperatorClicked");46 reciprocalButton.clicked.connect(this , "unaryOperatorClicked");48 divisionButton.clicked.connect(this.multiplicativeOperatorClicked.bind(this, Calculator.DIVISION_OPERATOR)); 49 timesButton.clicked.connect(this.multiplicativeOperatorClicked.bind(this, Calculator.TIMES_OPERATOR)); 50 minusButton.clicked.connect(this.additiveOperatorClicked.bind(this, Calculator.MINUS_OPERATOR)); 51 plusButton.clicked.connect(this.additiveOperatorClicked.bind(this, Calculator.PLUS_OPERATOR)); 52 53 squareRootButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.SQUARE_OPERATOR)); 54 powerButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.POWER_OPERATOR)); 55 reciprocalButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.RECIPROCAL_OPERATOR)); 47 56 equalButton.clicked.connect(this, "equalClicked"); 48 57 } 49 58 } 50 59 //! [0] 60 61 Calculator.NO_OPERATOR = 0; 62 Calculator.SQUARE_OPERATOR = 1; 63 Calculator.POWER_OPERATOR = 2; 64 Calculator.RECIPROCAL_OPERATOR = 3; 65 Calculator.DIVISION_OPERATOR = 4; 66 Calculator.TIMES_OPERATOR = 5; 67 Calculator.MINUS_OPERATOR = 6; 68 Calculator.PLUS_OPERATOR = 7; 51 69 52 70 Calculator.prototype.abortOperation = function() … … 58 76 Calculator.prototype.calculate = function(rightOperand, pendingOperator) 59 77 { 60 if (pendingOperator == "+") {78 if (pendingOperator == Calculator.PLUS_OPERATOR) { 61 79 this.sumSoFar += rightOperand; 62 } else if (pendingOperator == "-") {80 } else if (pendingOperator == Calculator.MINUS_OPERATOR) { 63 81 this.sumSoFar -= rightOperand; 64 } else if (pendingOperator == "*") {82 } else if (pendingOperator == Calculator.TIMES_OPERATOR) { 65 83 this.factorSoFar *= rightOperand; 66 } else if (pendingOperator == "/") {84 } else if (pendingOperator == Calculator.DIVISION_OPERATOR) { 67 85 if (rightOperand == 0) 68 return false;86 return false; 69 87 this.factorSoFar /= rightOperand; 70 88 } … … 73 91 74 92 //! [1] 75 Calculator.prototype.digitClicked = function() 76 { 77 var digitValue = __qt_sender__.text - 0; 93 Calculator.prototype.digitClicked = function(digitValue) 94 { 78 95 if ((digitValue == 0) && (this.ui.display.text == "0")) 79 96 return; … … 86 103 //! [1] 87 104 88 Calculator.prototype.unaryOperatorClicked = function( )105 Calculator.prototype.unaryOperatorClicked = function(op) 89 106 { 90 107 var operand = this.ui.display.text - 0; 91 108 var result = 0; 92 if ( __qt_sender__.text == "Sqrt") {109 if (op == Calculator.SQUARE_OPERATOR) { 93 110 if (operand < 0) { 94 111 this.abortOperation(); … … 96 113 } 97 114 result = Math.sqrt(operand); 98 } else if ( __qt_sender__.text == "x^2") {115 } else if (op == Calculator.POWER_OPERATOR) { 99 116 result = Math.pow(operand, 2); 100 } else if ( __qt_sender__.text == "1/x") {117 } else if (op == Calculator.RECIPROCAL_OPERATOR) { 101 118 if (operand == 0.0) { 102 119 this.abortOperation(); … … 109 126 } 110 127 111 Calculator.prototype.additiveOperatorClicked = function( )112 { 113 var operand = this.ui.display.text - 0; 114 115 if (this.pendingMultiplicativeOperator .length != 0) {128 Calculator.prototype.additiveOperatorClicked = function(op) 129 { 130 var operand = this.ui.display.text - 0; 131 132 if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) { 116 133 if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { 117 134 this.abortOperation(); … … 121 138 operand = this.factorSoFar; 122 139 this.factorSoFar = 0; 123 this.pendingMultiplicativeOperator = "";124 } 125 126 if (this.pendingAdditiveOperator .length != 0) {140 this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR; 141 } 142 143 if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) { 127 144 if (!this.calculate(operand, this.pendingAdditiveOperator)) { 128 145 this.abortOperation(); … … 134 151 } 135 152 136 this.pendingAdditiveOperator = __qt_sender__.text;137 this.waitingForOperand = true; 138 } 139 140 Calculator.prototype.multiplicativeOperatorClicked = function( )141 { 142 var operand = this.ui.display.text - 0; 143 144 if (this.pendingMultiplicativeOperator .length != 0) {153 this.pendingAdditiveOperator = op; 154 this.waitingForOperand = true; 155 } 156 157 Calculator.prototype.multiplicativeOperatorClicked = function(op) 158 { 159 var operand = this.ui.display.text - 0; 160 161 if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) { 145 162 if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { 146 163 this.abortOperation(); … … 152 169 } 153 170 154 this.pendingMultiplicativeOperator = __qt_sender__.text;171 this.pendingMultiplicativeOperator = op; 155 172 this.waitingForOperand = true; 156 173 } … … 160 177 var operand = this.ui.display.text - 0; 161 178 162 if (this.pendingMultiplicativeOperator .length != 0) {179 if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) { 163 180 if (!this.calculate(operand, this.pendingMultiplicativeOperator)) { 164 181 this.abortOperation(); … … 167 184 operand = this.factorSoFar; 168 185 this.factorSoFar = 0.0; 169 this.pendingMultiplicativeOperator = "";170 } 171 if (this.pendingAdditiveOperator .length != 0) {186 this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR; 187 } 188 if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) { 172 189 if (!this.calculate(operand, this.pendingAdditiveOperator)) { 173 190 this.abortOperation(); 174 191 return; 175 192 } 176 this.pendingAdditiveOperator = "";193 this.pendingAdditiveOperator = Calculator.NO_OPERATOR; 177 194 } else { 178 195 this.sumSoFar = operand; … … 235 252 this.sumSoFar = 0.0; 236 253 this.factorSoFar = 0.0; 237 this.pendingAdditiveOperator = "";238 this.pendingMultiplicativeOperator = "";254 this.pendingAdditiveOperator = Calculator.NO_OPERATOR; 255 this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR; 239 256 this.ui.display.text = "0"; 240 257 this.waitingForOperand = true; -
trunk/examples/script/calculator/calculator.pro
r2 r561 11 11 sources.path = $$[QT_INSTALL_EXAMPLES]/script/calculator 12 12 INSTALLS += target sources 13 14 symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) -
trunk/examples/script/calculator/main.cpp
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information (qt-info@nokia.com) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation (qt-info@nokia.com) 5 6 ** 6 7 ** This file is part of the examples of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you have questions regarding the use of this file, please contact 37 ** Nokia at qt-info@nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 61 61 //! [0a] 62 62 63 #if ndef QT_NO_SCRIPTTOOLS63 #if !defined(QT_NO_SCRIPTTOOLS) 64 64 QScriptEngineDebugger debugger; 65 65 debugger.attachTo(&engine); … … 90 90 //! [2] 91 91 92 #if ndef QT_NO_SCRIPTTOOLS92 #if !defined(QT_NO_SCRIPTTOOLS) 93 93 QLineEdit *display = qFindChild<QLineEdit*>(ui, "display"); 94 94 QObject::connect(display, SIGNAL(returnPressed()),
Note:
See TracChangeset
for help on using the changeset viewer.