AbacusNG/main.py

75 lines
2.6 KiB
Python

import sys
from PySide6.QtGui import QStandardItemModel, QStandardItem
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QFileDialog
from PySide6.QtCore import QFile, QIODevice, QModelIndex, QAbstractTableModel
from abacus import Abacus, AbacusDataModel, AbacusEmptyAssignmentException
class AbacusGUI:
def __init__(self):
self._abacus = Abacus()
self._history = []
self._app = QApplication(sys.argv)
ui_file_name = "gui.ui"
ui_file = QFile(ui_file_name)
if not ui_file.open(QIODevice.ReadOnly):
print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
sys.exit(-1)
loader = QUiLoader()
self._window = loader.load(ui_file)
ui_file.close()
if not self._window:
print(loader.errorString())
sys.exit(-1)
self._window.show()
self._window.but_enter.clicked.connect(self._berechne)
self._window.le_input.returnPressed.connect(self._berechne)
self._history_model = QStandardItemModel()
self._window.lv_history.setModel(self._history_model)
self._var_model = QStandardItemModel()
self._func_model = QStandardItemModel()
self._window.lv_variable.setModel(self._var_model)
self._window.lv_func.setModel(self._func_model)
self._check_models()
sys.exit(self._app.exec())
def _berechne(self):
input_str = self._window.le_input.text()
try:
self._abacus.process_input(input_str)
except AbacusEmptyAssignmentException as e:
print(e)
return
result = self._abacus.process_input(input_str)
if result:
adm = AbacusDataModel(*result)
self._history.append(adm)
self._history.append(result)
# item = QStandardItem('Halllo')
item = QStandardItem(str(result))
self._history_model.appendRow(item)
self._abacus.set_var('result', result[3])
self._window.le_input.setText(str(result[3]))
else:
self._window.le_input.setText('')
self._check_models()
def _check_models(self):
self._var_model.clear()
self._func_model.clear()
for k, v in self._abacus.get_vars().items():
item = QStandardItem(k+' = '+str(v))
self._var_model.appendRow(item)
# for k, v in self._abacus_core.get_funcs().items():
# item = QStandardItem(k+' = '+str(v))
# self._func_model.appendRow(item)
if __name__ == "__main__":
ag = AbacusGUI()