Package Camelot :: Package camelot :: Package view :: Package controls :: Module calculator |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.calculator

      1  from camelot.view.art import Icon
      2  from camelot.core.utils import ugettext as _
      3

      4  from PyQt4 import QtGui
      5  from PyQt4 import QtCore
      6

      7 -class Calculator(QtGui.QDialog):
      8 -    def __init__(self, parent=None):
      9          QtGui.QDialog.__init__(self, parent)
     10          mainLayout = QtGui.QVBoxLayout()
     11          topLeftLayout = QtGui.QVBoxLayout()
     12          topRightLayout = QtGui.QHBoxLayout()
     13          bottomRightLayout = QtGui.QHBoxLayout()
     14          bottomLayout = QtGui.QGridLayout()
     15

     16          self.setWindowTitle(_('Calculator'))
     17          self.input = QtGui.QLineEdit(self)
     18          self.connect(self.input, QtCore.SIGNAL('textEdited(const QString&)'), self.Calculate)
     19          #self.connect(self.input, QtCore.SIGNAL('returnPressed()'), self.ShowCalculate)

     20

     21

     22

     23          #BUTTONS---

     24

     25          self.equals = QtGui.QPushButton('=', self)
     26          self.discount = QtGui.QPushButton('Discount', self)
     27          self.save = QtGui.QPushButton('&Save', self)
     28          self.cancel = QtGui.QPushButton('Cancel', self)
     29

     30

     31

     32          self.zero = QtGui.QPushButton('0', self)
     33          self.one = QtGui.QPushButton('1', self)
     34          self.two = QtGui.QPushButton('2', self)
     35          self.three = QtGui.QPushButton('3', self)
     36          self.four = QtGui.QPushButton('4', self)
     37          self.five = QtGui.QPushButton('5', self)
     38          self.six = QtGui.QPushButton('6', self)
     39          self.seven = QtGui.QPushButton('7', self)
     40          self.eight = QtGui.QPushButton('8', self)
     41          self.nine = QtGui.QPushButton('9', self)
     42

     43          self.clear = QtGui.QPushButton('&Clear', self)
     44

     45          self.backspace = QtGui.QToolButton()
     46          icon = Icon('tango/16x16/actions/go-previous.png').getQIcon()
     47          self.backspace.setIcon(icon)
     48          self.backspace.setAutoRaise(True)
     49

     50          self.plus = QtGui.QPushButton('+', self)
     51          self.min = QtGui.QPushButton('-', self)
     52          self.multiply = QtGui.QPushButton('x', self)
     53          self.devide = QtGui.QPushButton('/', self)
     54          self.comma = QtGui.QPushButton(',', self)
     55

     56

     57          #Button-Connects---

     58

     59          self.connect(self.equals, QtCore.SIGNAL('clicked()'), self.ShowCalculate)
     60          self.connect(self.zero, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(0))
     61          self.connect(self.one, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(1))
     62          self.connect(self.two, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(2))
     63          self.connect(self.three, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(3))
     64          self.connect(self.four, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(4))
     65          self.connect(self.five, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(5))
     66          self.connect(self.six, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(6))
     67          self.connect(self.seven, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(7))
     68          self.connect(self.eight, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(8))
     69          self.connect(self.nine, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick(9))
     70          self.connect(self.plus, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick('+'))
     71          self.connect(self.min, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick('-'))
     72          self.connect(self.multiply, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick('*'))
     73          self.connect(self.devide, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick('/'))
     74          self.connect(self.clear, QtCore.SIGNAL('clicked()'), self.clearInput)
     75          self.connect(self.backspace, QtCore.SIGNAL('clicked()'), lambda:self.input.backspace())
     76          self.connect(self.comma, QtCore.SIGNAL('clicked()'), lambda:self.buttonClick('.'))
     77          self.connect(self.cancel, QtCore.SIGNAL('clicked()'), lambda:self.close())
     78          self.connect(self.save, QtCore.SIGNAL('clicked()'), self.SaveValue)
     79          self.connect(self.discount, QtCore.SIGNAL('clicked()'), self.discountClick)
     80

     81

     82

     83

     84

     85          self.output = QtGui.QLabel(self)
     86          #self.output.move(3, 8)

     87

     88

     89          mainLayout.addLayout(topLeftLayout)
     90          topLeftLayout.addLayout(topRightLayout)
     91          topRightLayout.addWidget(self.input)
     92          topRightLayout.addWidget(self.backspace)
     93          topLeftLayout.addWidget(self.output)
     94          topLeftLayout.addLayout(bottomRightLayout)
     95          bottomRightLayout.addWidget(self.equals)
     96          bottomRightLayout.addWidget(self.discount)
     97          bottomRightLayout.addWidget(self.save)
     98          bottomRightLayout.addWidget(self.cancel)
     99          mainLayout.addLayout(bottomLayout)
    100          bottomLayout.addWidget(self.one, 0,0)
    101          bottomLayout.addWidget(self.two, 0,1)
    102          bottomLayout.addWidget(self.three, 0,2)
    103          bottomLayout.addWidget(self.plus, 0,3)
    104          bottomLayout.addWidget(self.four, 1,0)
    105          bottomLayout.addWidget(self.five, 1,1)
    106          bottomLayout.addWidget(self.six, 1,2)
    107          bottomLayout.addWidget(self.min, 1,3)
    108          bottomLayout.addWidget(self.seven, 2,0)
    109          bottomLayout.addWidget(self.eight, 2,1)
    110          bottomLayout.addWidget(self.nine, 2,2)
    111          bottomLayout.addWidget(self.multiply, 2,3)
    112          bottomLayout.addWidget(self.clear, 3,0)
    113          bottomLayout.addWidget(self.zero, 3,1)
    114          bottomLayout.addWidget(self.comma, 3,2)
    115          bottomLayout.addWidget(self.devide, 3,3)
    116          self.setLayout(mainLayout)
    117

    118

    119

    120

    121

    122

    123

    124

    125 -    def keyPressEvent(self, event):
    126          #QtGui.QWidget.keyPressEvent(self, event)

    127

    128          key = event.key()
    129          if key == QtCore.Qt.Key_S:
    130              self.SaveValue()
    131              return
    132          else:
    133              QtGui.QDialog.keyPressEvent(self, event)
    134

    135

    136

    137 -    def SaveValue(self):
    138          self.emit(QtCore.SIGNAL('calculationFinished'), str(self.output.text()))
    139

    140          self.close()
    141          return
    142

    143

    144 -    def setValue(self, value):
    145          value = str(value).replace(',', '.')
    146          self.input.setText(str(value))
    147          self.Calculate()
    148

    149

    150 -    def Calculate(self):
    151          input = str(self.input.text())
    152          if not input:
    153              self.output.setText('')
    154              return
    155

    156          try:
    157              self.output.setText(str(eval(input)))
    158          except SyntaxError:
    159              pass
    160          except NameError:
    161              self.output.setText('Please input numeric characters')
    162              pass
    163

    164 -    def ShowCalculate(self):
    165          input = str(self.input.text())
    166          if not input:
    167              self.output.setText('')
    168              return
    169

    170

    171          if input == str(self.output.text()):
    172              reply = QtGui.QMessageBox.question(self, 'Message',

    173                                                 "Do you want to Save and Quit?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
    174

    175              if reply == QtGui.QMessageBox.Yes:
    176                  self.SaveValue()
    177

    178

    179          try:
    180              self.input.setText(str(eval(input)))
    181          except SyntaxError:
    182              self.output.setText('Invalid input')
    183              pass
    184          except NameError:
    185              self.output.setText('Please input numeric characters')
    186              pass
    187

    188 -    def buttonClick(self, event):
    189          self.input.setText(str(self.input.text()) + str(event))
    190          self.Calculate()
    191          self.input.setFocus()
    192

    193

    194

    195 -    def discountClick(self):
    196          input = str(self.input.text())
    197          if not input:
    198              return
    199

    200          #self.input.setText(str(eval(self.output.text())))

    201

    202          text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter percentage')
    203

    204          if ok:
    205              percentage = 1.00 + ((eval(str(text)) / 100.00))
    206              newInput = eval(str(self.input.text())) * percentage
    207              self.input.setText(str(newInput))
    208              self.Calculate()
    209              self.input.setFocus()
    210              return
    211

    212

    213 -    def clearInput(self):
    214          self.input.setText('')
    215          self.Calculate()
    216          self.input.setFocus()
    217