Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 5713

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: calculator.js Example File (script/calculator/calculator.js)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">calculator.js Example File<br /><span class="small-subtitle">script/calculator/calculator.js</span>
</h1>
<pre> 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);
     }
 }

 function Calculator(ui)
 {
     this.ui = ui;

     this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
     this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
     this.sumInMemory = 0;
     this.sumSoFar = 0;
     this.factorSoFar = 0;
     this.waitingForOperand = true;

     with (ui) {
         display.text = &quot;0&quot;;

         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, &quot;pointClicked&quot;);
         changeSignButton.clicked.connect(this, &quot;changeSignClicked&quot;);

         backspaceButton.clicked.connect(this, &quot;backspaceClicked&quot;);
         clearButton.clicked.connect(this, &quot;clear&quot;);
         clearAllButton.clicked.connect(this, &quot;clearAll&quot;);

         clearMemoryButton.clicked.connect(this, &quot;clearMemory&quot;);
         readMemoryButton.clicked.connect(this, &quot;readMemory&quot;);
         setMemoryButton.clicked.connect(this, &quot;setMemory&quot;);
         addToMemoryButton.clicked.connect(this, &quot;addToMemory&quot;);

         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, &quot;equalClicked&quot;);
     }
 }

 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();
     this.ui.display.text = &quot;####&quot;;
 }

 Calculator.prototype.calculate = function(rightOperand, pendingOperator)
 {
     if (pendingOperator == Calculator.PLUS_OPERATOR) {
         this.sumSoFar += rightOperand;
     } else if (pendingOperator == Calculator.MINUS_OPERATOR) {
         this.sumSoFar -= rightOperand;
     } else if (pendingOperator == Calculator.TIMES_OPERATOR) {
         this.factorSoFar *= rightOperand;
     } else if (pendingOperator == Calculator.DIVISION_OPERATOR) {
         if (rightOperand == 0)
             return false;
         this.factorSoFar /= rightOperand;
     }
     return true;
 }

 Calculator.prototype.digitClicked = function(digitValue)
 {
     if ((digitValue == 0) &amp;&amp; (this.ui.display.text == &quot;0&quot;))
         return;
     if (this.waitingForOperand) {
         this.ui.display.clear();
         this.waitingForOperand = false;
     }
     this.ui.display.text += digitValue;
 }

 Calculator.prototype.unaryOperatorClicked = function(op)
 {
     var operand = this.ui.display.text - 0;
     var result = 0;
     if (op == Calculator.SQUARE_OPERATOR) {
         if (operand &lt; 0) {
             this.abortOperation();
             return;
         }
         result = Math.sqrt(operand);
     } else if (op == Calculator.POWER_OPERATOR) {
         result = Math.pow(operand, 2);
     } else if (op == Calculator.RECIPROCAL_OPERATOR) {
         if (operand == 0.0) {
             this.abortOperation();
             return;
         }
         result = 1 / operand;
     }
     this.ui.display.text = result + &quot;&quot;;
     this.waitingForOperand = true;
 }

 Calculator.prototype.additiveOperatorClicked = function(op)
 {
     var operand = this.ui.display.text - 0;

     if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
         if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
             this.abortOperation();
             return;
         }
         this.ui.display.text = this.factorSoFar + &quot;&quot;;
         operand = this.factorSoFar;
         this.factorSoFar = 0;
         this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
     }

     if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) {
         if (!this.calculate(operand, this.pendingAdditiveOperator)) {
             this.abortOperation();
             return;
         }
         this.ui.display.text = this.sumSoFar + &quot;&quot;;
     } else {
         this.sumSoFar = operand;
     }

     this.pendingAdditiveOperator = op;
     this.waitingForOperand = true;
 }

 Calculator.prototype.multiplicativeOperatorClicked = function(op)
 {
     var operand = this.ui.display.text - 0;

     if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
         if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
             this.abortOperation();
             return;
         }
         this.ui.display.text = this.factorSoFar + &quot;&quot;;
     } else {
         this.factorSoFar = operand;
     }

     this.pendingMultiplicativeOperator = op;
     this.waitingForOperand = true;
 }

 Calculator.prototype.equalClicked = function()
 {
     var operand = this.ui.display.text - 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 = Calculator.NO_OPERATOR;
     }
     if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) {
         if (!this.calculate(operand, this.pendingAdditiveOperator)) {
             this.abortOperation();
             return;
         }
         this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
     } else {
         this.sumSoFar = operand;
     }

     this.ui.display.text = this.sumSoFar + &quot;&quot;;
     this.sumSoFar = 0.0;
     this.waitingForOperand = true;
 }

 Calculator.prototype.pointClicked = function()
 {
     if (this.waitingForOperand)
         this.ui.display.text = &quot;0&quot;;
     if (this.ui.display.text.indexOf(&quot;.&quot;) == -1)
         this.ui.display.text += &quot;.&quot;;
     this.waitingForOperand = false;
 }

 Calculator.prototype.changeSignClicked = function()
 {
     var text = this.ui.display.text;
     var value = text - 0;

     if (value &gt; 0) {
         text = &quot;-&quot; + text;
     } else if (value &lt; 0) {
         text = text.slice(1);
     }
     this.ui.display.text = text;
 }

 Calculator.prototype.backspaceClicked = function()
 {
     if (this.waitingForOperand)
         return;

     var text = this.ui.display.text;
     text = text.slice(0, -1);
     if (text.length == 0) {
         text = &quot;0&quot;;
         this.waitingForOperand = true;
     }
     this.ui.display.text = text;
 }

 Calculator.prototype.clear = function()
 {
     if (this.waitingForOperand)
         return;

     this.ui.display.text = &quot;0&quot;;
     this.waitingForOperand = true;
 }

 Calculator.prototype.clearAll = function()
 {
     this.sumSoFar = 0.0;
     this.factorSoFar = 0.0;
     this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
     this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
     this.ui.display.text = &quot;0&quot;;
     this.waitingForOperand = true;
 }

 Calculator.prototype.clearMemory = function()
 {
     this.sumInMemory = 0.0;
 }

 Calculator.prototype.readMemory = function()
 {
     this.ui.display.text = this.sumInMemory + &quot;&quot;;
     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;
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>