From 98f6a88fe687c1d4c1e747a5666f038358df8341 Mon Sep 17 00:00:00 2001 From: Maximilian Zettler Date: Tue, 15 Mar 2022 10:24:25 +0100 Subject: [PATCH] cleanup gui code --- timebot/app.py | 4 +-- timebot/gui.py | 93 +++++++++++++++++++++++++------------------------- 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/timebot/app.py b/timebot/app.py index 190cfe2..c32400c 100644 --- a/timebot/app.py +++ b/timebot/app.py @@ -107,10 +107,10 @@ def run(): print(tb.status()) elif args.subparser_name == "gui": from PyQt5.QtWidgets import QApplication - from timebot.gui import TimebotMain + from timebot.gui import TimebotMainWindow signal.signal(signal.SIGINT, lambda *_args: QApplication.quit()) app = QApplication(sys.argv) - main_window = TimebotMain(timebot=tb) + main_window = TimebotMainWindow(timebot=tb) main_window.show() sys.exit(app.exec_()) else: diff --git a/timebot/gui.py b/timebot/gui.py index 2e5ef57..5e0dba2 100644 --- a/timebot/gui.py +++ b/timebot/gui.py @@ -10,30 +10,6 @@ from timebot.timebot import TimeBot, TimebotObtainPasswordError package_logger = logging.getLogger(__name__) -class TimebotApiWorker(QObject): - status_msg = pyqtSignal(object) - hours_present_msg = pyqtSignal(object) - error = pyqtSignal(object) - obtain_password = pyqtSignal() - finished = pyqtSignal() - - def __init__(self, timebot: TimeBot): - super().__init__() - self.timebot = timebot - - def run(self): - try: - hours_present = self.timebot.get_hours_present() - hours_present = hours_present - datetime.timedelta(microseconds=hours_present.microseconds) - self.hours_present_msg.emit(hours_present) - self.status_msg.emit(self.timebot.status()) - except TimebotObtainPasswordError as e: - self.obtain_password.emit() - except Exception as e: - self.error.emit(e) - self.finished.emit() - - class HoursPresentLabelArea(QWidget): def __init__(self, parent=None): super().__init__(parent) @@ -50,7 +26,7 @@ class HoursPresentLabelArea(QWidget): self.setLayout(layout) -class TimebotMain(QWidget): +class TimebotMainWindow(QWidget): def __init__(self, timebot: TimeBot, parent=None): super().__init__(parent) @@ -97,15 +73,30 @@ class TimebotMain(QWidget): def update_main_window(self): self.main_window_info_thread = QThread() - self.main_window_info_worker = MainWindowInfoWorker() + self.main_window_info_worker = MainWindowInfoWorker(self) self.main_window_info_worker.moveToThread(self.main_window_info_thread) self.main_window_info_thread.started.connect(self.main_window_info_worker.run) self.main_window_info_worker.finished.connect(self.main_window_info_thread.quit) self.main_window_info_worker.finished.connect(self.main_window_info_worker.deleteLater) self.main_window_info_thread.finished.connect(self.main_window_info_thread.deleteLater) - self.main_window_info_worker.finished.connect(self.update_hours_present) self.main_window_info_thread.start() + def update_status(self): + if self.update_status_running: + return + self.status_thread = QThread() + self.status_worker = TimebotApiWorker(self, self.timebot) + self.status_worker.moveToThread(self.status_thread) + self.status_thread.started.connect(self.status_worker.run) + self.status_worker.finished.connect(self.status_thread.quit) + self.status_worker.finished.connect(self.status_worker.deleteLater) + self.status_thread.finished.connect(self.status_thread.deleteLater) + self.status_thread.finished.connect(self.update_status_finished) + self.status_worker.error.connect(lambda exp, *args: self.error_msg_show(str(exp))) + self.status_worker.obtain_password.connect(lambda *args: self.get_password(self.update_status)) + self.status_thread.start() + self.update_status_started() + def get_password(self, callback: callable = None): self.status_timer.stop() password, ok = self.password_dialog.getText(self, "Timebot Password", 'Enter your password:', @@ -117,7 +108,8 @@ class TimebotMain(QWidget): except Exception as e: self.error_msg_show(str(e)) self.timebot.mobatime_api.password = None - callback() + if callback is not None: + callback() self.status_timer.start(self.status_timer_time) def error_msg_show(self, error_text: str): @@ -143,29 +135,38 @@ class TimebotMain(QWidget): self.hours_present: datetime.timedelta = override elif self.hours_present > datetime.timedelta(seconds=self.main_window_timer_time / 100): self.hours_present = self.hours_present + datetime.timedelta(seconds=1) - self.text_box_hours_present_area.text_box_hours_present.setText(str(self.hours_present)) + hp = self.hours_present - datetime.timedelta(microseconds=self.hours_present.microseconds) + self.text_box_hours_present_area.text_box_hours_present.setText(str(hp)) - def update_status(self): - if self.update_status_running: - return - self.status_thread = QThread() - self.status_worker = TimebotApiWorker(self.timebot) - self.status_worker.moveToThread(self.status_thread) - self.status_thread.started.connect(self.status_worker.run) - self.status_worker.finished.connect(self.status_thread.quit) - self.status_worker.finished.connect(self.status_worker.deleteLater) - self.status_thread.finished.connect(self.status_thread.deleteLater) - self.status_thread.finished.connect(self.update_status_finished) - self.status_worker.status_msg.connect(self.text_box_status.setText) - self.status_worker.hours_present_msg.connect(lambda hp, *args: self.update_hours_present(override=hp)) - self.status_worker.error.connect(lambda exp, *args: self.error_msg_show(str(exp))) - self.status_worker.obtain_password.connect(lambda *args: self.get_password(self.update_status)) - self.status_thread.start() - self.update_status_started() + +class TimebotApiWorker(QObject): + error = pyqtSignal(object) + obtain_password = pyqtSignal() + finished = pyqtSignal() + + def __init__(self, tmw: TimebotMainWindow, timebot: TimeBot): + super().__init__() + self.tmw = tmw + self.timebot = timebot + + def run(self): + try: + self.tmw.update_hours_present(override=self.timebot.get_hours_present()) + self.tmw.text_box_status.setText(self.timebot.status()) + except TimebotObtainPasswordError: + self.obtain_password.emit() + except Exception as e: + self.error.emit(e) + self.finished.emit() class MainWindowInfoWorker(QObject): finished = pyqtSignal() + def __init__(self, tmw: TimebotMainWindow): + super().__init__() + self.tmw = tmw + def run(self): + self.tmw.update_hours_present() self.finished.emit()