/simpleui.py
Python | 234 lines | 208 code | 6 blank | 20 comment | 0 complexity | 7026f876a8dc7177706ef9b897633204 MD5 | raw file
- #!/usr/bin/env python
- __author__ = 'Tom Wright <tom.tdw@gmail.com>'
- # Copyright 2012 Thomas Wright <tom.tdw@gmail.com>
- # This file is part of C1000 Intelligent Calculator.
- #
- # C1000 Intelligent Calculator is free software: you can redistribute it
- # and/or modify it under the terms of the GNU General Public License as
- # published by the Free Software Foundation, either version 3 of the License,
- # or (at your option) any later version.
- #
- # C1000 Intelligent Calculator is distributed in the hope that it will be
- # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
- # Public License for more details.
- #
- # You should have received a copy of the GNU General Public License along with
- # C1000 Intelligent Calculator. If not, see <http://www.gnu.org/licenses/>.
- # Standard modules
- from PyQt4 import QtCore, QtGui, uic
- import re
- # Project modules
- from calculator import Calculator
- import help
- class CalculatorForm(QtGui.QMainWindow):
- ''' The simple interface '''
- def __init__(self, parent=None):
- ''' Initialize the interface. '''
- super(type(self), self).__init__(parent)
- uic.loadUi('simpleui.ui', self)
- self.calc = Calculator()
- def _command(self):
- ''' Return the user's command. '''
- return str(self.input.text())
- def _append(self, text):
- ''' Append text to the command entry box. '''
- # Retrieve the current command
- cmd = self._command()
- if re.match(r'[ ()]', cmd[-1]):
- # If the command ends with a space just append text.
- self.input.setText(cmd + text)
- else:
- # Otherwise append a space followed by text.
- self.input.setText(cmd + ' ' + text)
- def _append_digit(self, digit):
- # Retrieve the current command
- cmd = self._command()
- if len(cmd) == 0:
- # If the command is of zero length, just replace it with
- # digit.
- self.input.setText(digit)
- elif re.match(r'^([^a-z][a-z]|.*[0-9\.^ (])$', self._comma):
- # If the commands ends with a digit or and opening bracket,
- # just append the digit.
- self.input.setText(cmd + digit)
- else:
- # Otherwise append a space followed by digit.
- self.input.setText(cmd + ' ' + digit)
- # Event handlers for the buttons
- @QtCore.pyqtSlot()
- def on_btn_help_clicked(self):
- help.help()
- @QtCore.pyqtSlot()
- def on_input_returnPressed(self):
- output = re.sub(r'^= ', '', self.calc.evaluate(self._command()))
- self.input.setText(output)
- @QtCore.pyqtSlot()
- def on_btn_eval_clicked(self):
- output = re.sub(r'^= ', '', self.calc.evaluate(self._command()))
- self.input.setText(output)
- @QtCore.pyqtSlot()
- def on_btn_0_clicked(self):
- self._append_digit('0')
- @QtCore.pyqtSlot()
- def on_btn_1_clicked(self):
- self._append_digit('1')
- @QtCore.pyqtSlot()
- def on_btn_2_clicked(self):
- self._append_digit('2')
- @QtCore.pyqtSlot()
- def on_btn_3_clicked(self):
- self._append_digit('3')
- @QtCore.pyqtSlot()
- def on_btn_4_clicked(self):
- self._append_digit('4')
- @QtCore.pyqtSlot()
- def on_btn_5_clicked(self):
- self._append_digit('5')
- @QtCore.pyqtSlot()
- def on_btn_6_clicked(self):
- self._append_digit('6')
- @QtCore.pyqtSlot()
- def on_btn_7_clicked(self):
- self._append_digit('7')
- @QtCore.pyqtSlot()
- def on_btn_8_clicked(self):
- self._append_digit('8')
- @QtCore.pyqtSlot()
- def on_btn_9_clicked(self):
- self._append_digit('9')
- @QtCore.pyqtSlot()
- def on_btn_mantissa_clicked(self):
- self._append_digit('.')
- @QtCore.pyqtSlot()
- def on_btn_x_clicked(self):
- self._append_digit('x')
- @QtCore.pyqtSlot()
- def on_btn_i_clicked(self):
- self._append_digit('i')
- @QtCore.pyqtSlot()
- def on_btn_add_clicked(self):
- self._append('+')
- @QtCore.pyqtSlot()
- def on_btn_sub_clicked(self):
- self._append('-')
- @QtCore.pyqtSlot()
- def on_btn_mul_clicked(self):
- self._append('*')
- @QtCore.pyqtSlot()
- def on_btn_div_clicked(self):
- self._append('/')
- @QtCore.pyqtSlot()
- def on_btn_index_clicked(self):
- self._append_digit('^')
- @QtCore.pyqtSlot()
- def on_btn_left_bracket_clicked(self):
- self._append('(')
- @QtCore.pyqtSlot()
- def on_btn_right_bracket_clicked(self):
- self._append_digit(')')
- @QtCore.pyqtSlot()
- def on_btn_logax_clicked(self):
- self._append('log(a, x)')
- @QtCore.pyqtSlot()
- def on_btn_log_clicked(self):
- self._append('log')
- @QtCore.pyqtSlot()
- def on_btn_ln_clicked(self):
- self._append('ln')
- @QtCore.pyqtSlot()
- def on_btn_del_clicked(self):
- self.input.setText('')
- @QtCore.pyqtSlot()
- def on_btn_sin_clicked(self):
- self._append('sin')
- @QtCore.pyqtSlot()
- def on_btn_arcsin_clicked(self):
- self._append('arcsin')
- @QtCore.pyqtSlot()
- def on_btn_cos_clicked(self):
- self._append('cos')
- @QtCore.pyqtSlot()
- def on_btn_arccos_clicked(self):
- self._append('arccos')
- @QtCore.pyqtSlot()
- def on_btn_tan_clicked(self):
- self._append('tan')
- @QtCore.pyqtSlot()
- def on_btn_arctan_clicked(self):
- self._append('arctan')
- @QtCore.pyqtSlot()
- def on_btn_pi_clicked(self):
- self._append('pi')
- @QtCore.pyqtSlot()
- def on_btn_e_clicked(self):
- self._append('e')
- @QtCore.pyqtSlot()
- def on_btn_clear_clicked(self):
- self.input.setText('')
- @QtCore.pyqtSlot()
- def on_btn_fact_clicked(self):
- self._append_digit('!')
- @QtCore.pyqtSlot()
- def on_btn_factors_clicked(self):
- self._append('factors')
- @QtCore.pyqtSlot()
- def on_btn_abs_clicked(self):
- self._append('|')
- if __name__ == '__main__':
- import sys
- app = QtGui.QApplication(sys.argv)
- calculator = CalculatorForm()
- calculator.show()
- sys.exit(app.exec_())