From 68aff48be6b0d6e9f5764bb5b00701e4969065cd Mon Sep 17 00:00:00 2001 From: Chidi Williams Date: Sat, 22 Oct 2022 22:02:03 +0100 Subject: [PATCH] Shut down file transcription gracefully on window close (#119) --- gui.py | 28 +++++++++++++++------------- transcriber.py | 9 +++++++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/gui.py b/gui.py index ee6356d7..5ce71615 100644 --- a/gui.py +++ b/gui.py @@ -325,6 +325,7 @@ class FileTranscriberWidget(QWidget): model_download_progress_dialog: Optional[DownloadModelProgressDialog] = None transcriber_progress_dialog: Optional[TranscriberProgressDialog] = None transcribe_progress = pyqtSignal(tuple) + file_transcriber: Optional[FileTranscriber] = None def __init__(self, file_path: str, parent: Optional[QWidget]) -> None: super().__init__(parent) @@ -393,10 +394,6 @@ class FileTranscriberWidget(QWidget): self.selected_output_format = format def on_click_run(self): - base_path = getattr(sys, '_MEIPASS', os.path.dirname( - os.path.abspath(__file__))) - logging.debug(base_path) - default_path = FileTranscriber.get_default_output_file_path( task=self.selected_task, input_file_path=self.file_path, output_format=self.selected_output_format) @@ -452,7 +449,7 @@ class FileTranscriberWidget(QWidget): (current_size, total_size) = progress if current_size == total_size: - self.end_transcription() + self.reset_transcription() return # In the middle of transcription... @@ -462,18 +459,19 @@ class FileTranscriberWidget(QWidget): self.transcriber_progress_dialog = TranscriberProgressDialog( file_path=self.file_path, total_size=total_size, parent=self) self.transcriber_progress_dialog.canceled.connect( - self.on_cancel_transcriber_progress_dialog) + self.stop_transcription) # Update the progress of the dialog unless it has # been canceled before this progress update arrived if self.transcriber_progress_dialog != None and self.transcriber_progress_dialog.wasCanceled() == False: self.transcriber_progress_dialog.update_progress(current_size) - def on_cancel_transcriber_progress_dialog(self): - self.file_transcriber.stop() - self.end_transcription() + def stop_transcription(self): + if self.file_transcriber != None: + self.file_transcriber.stop() + self.reset_transcription() - def end_transcription(self): + def reset_transcription(self): self.run_button.setDisabled(False) if self.transcriber_progress_dialog != None: self.transcriber_progress_dialog.destroy() @@ -718,9 +716,13 @@ class FileTranscriberMainWindow(MainWindow): super().__init__(title=get_short_file_path( file_path), w=400, h=210, parent=parent, *args) - central_widget = FileTranscriberWidget(file_path, self) - central_widget.setContentsMargins(10, 10, 10, 10) - self.setCentralWidget(central_widget) + self.central_widget = FileTranscriberWidget(file_path, self) + self.central_widget.setContentsMargins(10, 10, 10, 10) + self.setCentralWidget(self.central_widget) + + def closeEvent(self, event: QCloseEvent) -> None: + self.central_widget.stop_transcription() + return super().closeEvent(event) class Application(QApplication): diff --git a/transcriber.py b/transcriber.py index 6f4d6809..40119613 100644 --- a/transcriber.py +++ b/transcriber.py @@ -141,9 +141,9 @@ class RecordingTranscriber: self.is_running = False if self.current_thread != None: - logging.debug('Waiting for processing thread to terminate') + logging.debug('Waiting for recording thread to terminate') self.current_thread.join() - logging.debug('Processing thread terminated') + logging.debug('Recording thread terminated') def more_data(fd: int): @@ -315,6 +315,11 @@ class FileTranscriber: def stop(self): self.stopped = True + if self.current_thread != None: + logging.debug('Waiting for file transcription thread to terminate') + self.current_thread.join() + logging.debug('File transcription thread terminated') + def check_stopped(self): return self.stopped