PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/simpleui.py

https://bitbucket.org/twright/c1000-intelligent-calculator
Python | 234 lines | 208 code | 6 blank | 20 comment | 0 complexity | 7026f876a8dc7177706ef9b897633204 MD5 | raw file
  1. #!/usr/bin/env python
  2. __author__ = 'Tom Wright <tom.tdw@gmail.com>'
  3. # Copyright 2012 Thomas Wright <tom.tdw@gmail.com>
  4. # This file is part of C1000 Intelligent Calculator.
  5. #
  6. # C1000 Intelligent Calculator is free software: you can redistribute it
  7. # and/or modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation, either version 3 of the License,
  9. # or (at your option) any later version.
  10. #
  11. # C1000 Intelligent Calculator is distributed in the hope that it will be
  12. # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  14. # Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # C1000 Intelligent Calculator. If not, see <http://www.gnu.org/licenses/>.
  18. # Standard modules
  19. from PyQt4 import QtCore, QtGui, uic
  20. import re
  21. # Project modules
  22. from calculator import Calculator
  23. import help
  24. class CalculatorForm(QtGui.QMainWindow):
  25. ''' The simple interface '''
  26. def __init__(self, parent=None):
  27. ''' Initialize the interface. '''
  28. super(type(self), self).__init__(parent)
  29. uic.loadUi('simpleui.ui', self)
  30. self.calc = Calculator()
  31. def _command(self):
  32. ''' Return the user's command. '''
  33. return str(self.input.text())
  34. def _append(self, text):
  35. ''' Append text to the command entry box. '''
  36. # Retrieve the current command
  37. cmd = self._command()
  38. if re.match(r'[ ()]', cmd[-1]):
  39. # If the command ends with a space just append text.
  40. self.input.setText(cmd + text)
  41. else:
  42. # Otherwise append a space followed by text.
  43. self.input.setText(cmd + ' ' + text)
  44. def _append_digit(self, digit):
  45. # Retrieve the current command
  46. cmd = self._command()
  47. if len(cmd) == 0:
  48. # If the command is of zero length, just replace it with
  49. # digit.
  50. self.input.setText(digit)
  51. elif re.match(r'^([^a-z][a-z]|.*[0-9\.^ (])$', self._comma):
  52. # If the commands ends with a digit or and opening bracket,
  53. # just append the digit.
  54. self.input.setText(cmd + digit)
  55. else:
  56. # Otherwise append a space followed by digit.
  57. self.input.setText(cmd + ' ' + digit)
  58. # Event handlers for the buttons
  59. @QtCore.pyqtSlot()
  60. def on_btn_help_clicked(self):
  61. help.help()
  62. @QtCore.pyqtSlot()
  63. def on_input_returnPressed(self):
  64. output = re.sub(r'^= ', '', self.calc.evaluate(self._command()))
  65. self.input.setText(output)
  66. @QtCore.pyqtSlot()
  67. def on_btn_eval_clicked(self):
  68. output = re.sub(r'^= ', '', self.calc.evaluate(self._command()))
  69. self.input.setText(output)
  70. @QtCore.pyqtSlot()
  71. def on_btn_0_clicked(self):
  72. self._append_digit('0')
  73. @QtCore.pyqtSlot()
  74. def on_btn_1_clicked(self):
  75. self._append_digit('1')
  76. @QtCore.pyqtSlot()
  77. def on_btn_2_clicked(self):
  78. self._append_digit('2')
  79. @QtCore.pyqtSlot()
  80. def on_btn_3_clicked(self):
  81. self._append_digit('3')
  82. @QtCore.pyqtSlot()
  83. def on_btn_4_clicked(self):
  84. self._append_digit('4')
  85. @QtCore.pyqtSlot()
  86. def on_btn_5_clicked(self):
  87. self._append_digit('5')
  88. @QtCore.pyqtSlot()
  89. def on_btn_6_clicked(self):
  90. self._append_digit('6')
  91. @QtCore.pyqtSlot()
  92. def on_btn_7_clicked(self):
  93. self._append_digit('7')
  94. @QtCore.pyqtSlot()
  95. def on_btn_8_clicked(self):
  96. self._append_digit('8')
  97. @QtCore.pyqtSlot()
  98. def on_btn_9_clicked(self):
  99. self._append_digit('9')
  100. @QtCore.pyqtSlot()
  101. def on_btn_mantissa_clicked(self):
  102. self._append_digit('.')
  103. @QtCore.pyqtSlot()
  104. def on_btn_x_clicked(self):
  105. self._append_digit('x')
  106. @QtCore.pyqtSlot()
  107. def on_btn_i_clicked(self):
  108. self._append_digit('i')
  109. @QtCore.pyqtSlot()
  110. def on_btn_add_clicked(self):
  111. self._append('+')
  112. @QtCore.pyqtSlot()
  113. def on_btn_sub_clicked(self):
  114. self._append('-')
  115. @QtCore.pyqtSlot()
  116. def on_btn_mul_clicked(self):
  117. self._append('*')
  118. @QtCore.pyqtSlot()
  119. def on_btn_div_clicked(self):
  120. self._append('/')
  121. @QtCore.pyqtSlot()
  122. def on_btn_index_clicked(self):
  123. self._append_digit('^')
  124. @QtCore.pyqtSlot()
  125. def on_btn_left_bracket_clicked(self):
  126. self._append('(')
  127. @QtCore.pyqtSlot()
  128. def on_btn_right_bracket_clicked(self):
  129. self._append_digit(')')
  130. @QtCore.pyqtSlot()
  131. def on_btn_logax_clicked(self):
  132. self._append('log(a, x)')
  133. @QtCore.pyqtSlot()
  134. def on_btn_log_clicked(self):
  135. self._append('log')
  136. @QtCore.pyqtSlot()
  137. def on_btn_ln_clicked(self):
  138. self._append('ln')
  139. @QtCore.pyqtSlot()
  140. def on_btn_del_clicked(self):
  141. self.input.setText('')
  142. @QtCore.pyqtSlot()
  143. def on_btn_sin_clicked(self):
  144. self._append('sin')
  145. @QtCore.pyqtSlot()
  146. def on_btn_arcsin_clicked(self):
  147. self._append('arcsin')
  148. @QtCore.pyqtSlot()
  149. def on_btn_cos_clicked(self):
  150. self._append('cos')
  151. @QtCore.pyqtSlot()
  152. def on_btn_arccos_clicked(self):
  153. self._append('arccos')
  154. @QtCore.pyqtSlot()
  155. def on_btn_tan_clicked(self):
  156. self._append('tan')
  157. @QtCore.pyqtSlot()
  158. def on_btn_arctan_clicked(self):
  159. self._append('arctan')
  160. @QtCore.pyqtSlot()
  161. def on_btn_pi_clicked(self):
  162. self._append('pi')
  163. @QtCore.pyqtSlot()
  164. def on_btn_e_clicked(self):
  165. self._append('e')
  166. @QtCore.pyqtSlot()
  167. def on_btn_clear_clicked(self):
  168. self.input.setText('')
  169. @QtCore.pyqtSlot()
  170. def on_btn_fact_clicked(self):
  171. self._append_digit('!')
  172. @QtCore.pyqtSlot()
  173. def on_btn_factors_clicked(self):
  174. self._append('factors')
  175. @QtCore.pyqtSlot()
  176. def on_btn_abs_clicked(self):
  177. self._append('|')
  178. if __name__ == '__main__':
  179. import sys
  180. app = QtGui.QApplication(sys.argv)
  181. calculator = CalculatorForm()
  182. calculator.show()
  183. sys.exit(app.exec_())