Polished up GUI for processing :D

This commit is contained in:
oitsjustjose 2025-03-22 16:15:11 -05:00
commit 532d800ffd
3 changed files with 219 additions and 61 deletions

View file

@ -1,76 +1,229 @@
import sys
from pathlib import Path
from typing import Callable
from PyQt6.QtCore import QPoint, Qt
from PyQt6.QtCore import QThread
from PyQt6.QtWidgets import (
QApplication,
QFileDialog,
QHBoxLayout,
QLabel,
QMainWindow,
QPushButton,
QTextEdit,
QVBoxLayout,
QWidget,
)
def __make_window() -> QMainWindow:
"""Automatically creates a window centered to the current screen
Returns:
QMainWindow: The created and centered window
"""
window = QMainWindow()
window.setWindowTitle("Drum Track Converter by oitsjustjose")
window.setFixedSize(640, 400)
frame_geo = window.frameGeometry()
center = window.screen().availableGeometry().center()
frame_geo.moveCenter(center)
window.move(frame_geo.topLeft())
return window
from src.messaging import MessageInterface
from src.processor import FolderProcessor
def __create_label(parent: QWidget, text: str, pos: QPoint) -> QLabel:
"""Creates a label in a single call that is horizontally centered and automatically sized, with the provided text and pos.
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
Args:
parent (QWidget): The parent widget
text (str): The text to render
pos (QPoint): The position to render the label at
# Widgets that we'll want to set throughout the runtime
self.start_button: QPushButton = None
self.logging_region: QTextEdit = None
Returns:
QLabel: The label created
"""
label = QLabel(text, parent, alignment=Qt.AlignmentFlag.AlignHCenter)
label.adjustSize()
label.move(pos)
return label
self.setWindowTitle("Drum Track Converter by oitsjustjose")
self.__setup_layout()
self.__center()
# Variables for the actual processing flow
self.input_dir: str = ""
self.output_dir: str = ""
self._thread: QThread = None
def __center(self) -> None:
"""Centers the window to the middle of the Screen"""
# Center window
frame_geo = self.frameGeometry()
center = self.screen().availableGeometry().center()
frame_geo.moveCenter(center)
self.move(frame_geo.topLeft())
def __setup_layout(self):
"""Sets up the main layout of the window, including children"""
self.layout = QVBoxLayout()
self.__setup_children()
widget = QWidget()
widget.setLayout(self.layout)
self.setCentralWidget(widget)
def __setup_children(self):
"""Sets up the children of the main window element using [nested] HBox and VBox layouts"""
def create_io_button_group(
self: MainWindow, label_text: str, on_click: Callable
) -> QWidget:
"""Re-usable template to create an input or output button group
This group contains a label and a clickable button, which calls the
on_click callable passed in when clicked.
Args:
self (MainWindow): The parent object
label_text (str): The label to place above the text
on_click (Callable): The method to fire when the included button is clicked
Returns:
QWidget: _description_
"""
vbox = QWidget()
layout = QVBoxLayout()
label = self.__create_label(label_text)
button = QPushButton("Choose Folder...")
button.clicked.connect(on_click)
layout.addWidget(label)
layout.addWidget(button)
vbox.setLayout(layout)
return vbox
# Input and Output directory items
io_btns = QWidget()
io_btns_layout = QHBoxLayout()
io_btns_layout.addWidget(
create_io_button_group(self, "Select Input Folder", self.on_input_clicked)
)
io_btns_layout.addWidget(
create_io_button_group(self, "Select Output Folder", self.on_output_clicked)
)
io_btns.setLayout(io_btns_layout)
# Start Button
self.start_button = QPushButton("Start")
self.start_button.setEnabled(False)
self.start_button.adjustSize()
self.start_button.clicked.connect(self.on_start_clicked)
# Logging region appears automatically
self.logging_region = QTextEdit()
self.logging_region.setReadOnly(True)
self.logging_region.setVisible(False)
self.logging_region.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
self.layout.addWidget(io_btns)
self.layout.addWidget(self.start_button)
self.layout.addWidget(self.logging_region)
def __create_label(self, text: str) -> QLabel:
"""Creates a label in a single call that is horizontally centered and automatically sized, with the provided text and pos.
Args:
parent (QWidget): The parent widget
text (str): The text to render
pos (QPoint): The position to render the label at
Returns:
QLabel: The label created
"""
label = QLabel(text)
label.adjustSize()
return label
def on_input_clicked(self):
"""Sets the input directory from a FileDialog, enables the start button if conditions are met"""
self.input_dir = QFileDialog.getExistingDirectory(None, "Select Input Folder")
enabled = bool(self.input_dir and self.output_dir)
self.start_button.setEnabled(enabled)
def on_output_clicked(self):
"""Sets the output directory from a FileDialog, enables the start button if conditions are met"""
self.output_dir = QFileDialog.getExistingDirectory(None, "Select Input Folder")
enabled = bool(self.input_dir and self.output_dir)
self.start_button.setEnabled(enabled)
def on_start_clicked(self):
"""Passes through basic assertions, spins up thread to process files from the input and output params"""
assert bool(self.input_dir and self.output_dir)
# Only join once we're good to go again, there's really no harm in keeping the other thread around
if self._thread and not self._thread.isRunning():
self._thread.join()
self._thread = None
self.start_button.setEnabled(False)
self.start_button.setText("Processing...")
self.logging_region.setVisible(True)
self._thread = OffThreadProcessor(self)
self._thread.start()
def stop_thread(self):
"""Stops the working thread (if it's been started) at its soonest convenience..."""
if not self._thread:
return
if self._thread.isRunning():
self._thread.requestInterruption()
self._thread.quit()
self._thread.wait()
self._thread.deleteLater()
class OffThreadProcessor(QThread):
def __init__(self, parent: MainWindow):
super().__init__(parent)
self.running = False
self.main_window: MainWindow = parent
self.gui_output = GuiOutput(self.main_window)
self.processor = FolderProcessor(
parent.input_dir, parent.output_dir, self.gui_output
)
def run(self) -> None:
self.running = True
self.processor.process_directory()
# All of this occurs here instead of on_start_clicked to keep the main GUI from being blocked on the join
self.main_window.logging_region.append("✅ Done!")
self.main_window.start_button.setText("Start")
self.main_window.start_button.setEnabled(True)
self.running = False
def isRunning(self):
return self.running
def requestInterruption(self):
self.processor.quit()
return super().requestInterruption()
class GuiOutput(MessageInterface):
def __init__(self, window: MainWindow):
self.w: MainWindow = window
def info(self, input):
if not self.w.logging_region:
return
self.w.logging_region.append(f"ℹ️ {input}")
def warning(self, input):
if not self.w.logging_region:
return
self.w.logging_region.append(f"⚠️ {input}")
def error(self, input):
if not self.w.logging_region:
return
self.w.logging_region.append(f"‼️ {input}")
def main() -> None:
# Values for the actual methods called later down
input_directory: str = ""
"""
Creates a QApplication, main window and shows the window
When the app is closed, the exit code is stored for sys.exit() while
in the meantime the off-thread processing (if any) is stopped.
"""
app = QApplication(sys.argv)
window = __make_window()
__create_label(window, "Select Input Directory", QPoint(36, 20))
input_dir_button = QPushButton("Choose Folder...", window)
input_dir_button.move(QPoint(40, 40))
# Event triggers start here so we can access the above variables within event handlers
def input_dir_button_clicked() -> None:
input_directory = QFileDialog.getExistingDirectory(None, "Select Input Folder")
path = Path(input_directory).name
input_dir_button.setText(path)
input_dir_button.adjustSize()
# Event trigger hookups
input_dir_button.clicked.connect(input_dir_button_clicked)
window = MainWindow()
window.show()
sys.exit(app.exec())
exit_code = app.exec()
window.stop_thread()
sys.exit(exit_code)
if __name__ == "__main__":

View file

@ -89,8 +89,3 @@ class CliOutput(MessageInterface):
def error(self, input):
self.logger.error(f"{input}")
class GuiOutput(MessageInterface):
# TODO: this is a stub
pass

View file

@ -23,6 +23,11 @@ class FolderProcessor:
self.input_dir: str = input_dir
self.output_dir: str = output_dir
self.msg_interface: MessageInterface = msg_interface
self.should_stop = False
def quit(self) -> None:
"""Tells the ongoing process to quit on next iteration"""
self.should_stop = True
def process_directory(self) -> None:
"""
@ -76,6 +81,11 @@ class FolderProcessor:
f"Done processing {original_path.name} and relocated it to {file_dest.relative_to(cur_dir)}!"
)
if self.should_stop:
break
if self.should_stop:
break
# Remove the model output since we don't need it anymore
if os.path.exists(MODEL_NAME):
shutil.rmtree(MODEL_NAME)