|
| 1 | +import sys |
| 2 | +import time |
| 3 | + |
| 4 | +from PyQt6.QtCore import Qt, QTimer, QMimeData |
| 5 | +from PyQt6.QtGui import QTextCursor, QFont, QDropEvent |
| 6 | +from PyQt6.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QVBoxLayout, QTabWidget, QLabel, QWidget, \ |
| 7 | + QPlainTextEdit |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +# Custom Controls |
| 12 | +class DropLabel(QLabel): |
| 13 | + def __init__(self, *args, **kwargs): |
| 14 | + super().__init__(*args, **kwargs) |
| 15 | + self.setStyleSheet("border: 2px dashed grey;") |
| 16 | + self.on_drop = lambda text:print(text) |
| 17 | + |
| 18 | + def set_drop_func(self, func:callable): |
| 19 | + if func.__code__.co_argcount != 1: |
| 20 | + print(f"Set drop func fail!\nThe arg_count of {func.__name__} must be 1, but its {func.__code__.co_argcount}") |
| 21 | + self.on_drop = func |
| 22 | + |
| 23 | + def dragEnterEvent(self, event): |
| 24 | + event.accept() |
| 25 | + |
| 26 | + def dropEvent(self, event:QDropEvent): |
| 27 | + self.on_drop(event.mimeData()) |
| 28 | + event.accept() |
| 29 | + |
| 30 | + def mousePressEvent(self, event): |
| 31 | + if event.button() == Qt.MouseButton.LeftButton: |
| 32 | + print("Left button pressed") |
| 33 | + elif event.button() == Qt.MouseButton.RightButton: |
| 34 | + print("Right button pressed") |
| 35 | + |
| 36 | +# Main Class |
| 37 | +class Tool(QMainWindow): |
| 38 | + def __init__(self): |
| 39 | + super(Tool, self).__init__() |
| 40 | + self.log_output = None |
| 41 | + self.timers = set() |
| 42 | + self.setWindowTitle('MIO-KITCHEN') |
| 43 | + self.main_layout = QHBoxLayout() |
| 44 | + self.log_area = QVBoxLayout() |
| 45 | + self.func_area = QVBoxLayout() |
| 46 | + self.main_layout.addLayout(self.log_area) |
| 47 | + self.main_layout.addLayout(self.func_area) |
| 48 | + self.log_area_content() |
| 49 | + self.func_area_content() |
| 50 | + self.start_timers() |
| 51 | + widget = QWidget() |
| 52 | + widget.setLayout(self.main_layout) |
| 53 | + self.setCentralWidget(widget) |
| 54 | + |
| 55 | + def log_area_content(self): |
| 56 | + time_show = QLabel('MIO-KITCHEN') |
| 57 | + ft = QFont() |
| 58 | + ft.setPointSize(18) |
| 59 | + ft.setBold(True) |
| 60 | + time_show.setFont(ft) |
| 61 | + if not self.log_output: |
| 62 | + self.log_output = QPlainTextEdit() |
| 63 | + drag_and_drop = DropLabel("Drop URL / Path / FileName Here.") |
| 64 | + drag_and_drop.setMinimumHeight(80) |
| 65 | + drag_and_drop.setMinimumWidth(240) |
| 66 | + drag_and_drop.set_drop_func(lambda :...) |
| 67 | + self.log_area.addWidget(time_show) |
| 68 | + self.log_area.addWidget(drag_and_drop) |
| 69 | + drag_and_drop.setAlignment(Qt.AlignmentFlag.AlignCenter) |
| 70 | + time_show.setAlignment(Qt.AlignmentFlag.AlignCenter) |
| 71 | + drag_and_drop.setAcceptDrops(True) |
| 72 | + self.log_area.addWidget(self.log_output) |
| 73 | + show_timer = QTimer(self) |
| 74 | + show_timer.timeout.connect(lambda: time_show.setText(time.strftime("%H:%M:%S"))) |
| 75 | + self.timers.add(show_timer) |
| 76 | + |
| 77 | + def func_area_content(self): |
| 78 | + tabs = QTabWidget() |
| 79 | + self.func_area.addWidget( |
| 80 | + tabs |
| 81 | + ) |
| 82 | + tabs.setMovable(True) |
| 83 | + |
| 84 | + for n, color in enumerate(["red", "green", "blue", "yellow"]): |
| 85 | + tabs.addTab(QLabel(color), str(n)) |
| 86 | + |
| 87 | + def start_timers(self): |
| 88 | + for i in self.timers: |
| 89 | + i.start(1000) |
| 90 | + |
| 91 | + |
| 92 | +class StdoutRedirector: |
| 93 | + def __init__(self, text_widget: QPlainTextEdit, is_error=False): |
| 94 | + self.text_space = text_widget |
| 95 | + self.flush = ... |
| 96 | + self.error = '' |
| 97 | + self.is_error = is_error |
| 98 | + |
| 99 | + def write(self, string): |
| 100 | + if self.is_error: |
| 101 | + self.error += string |
| 102 | + print(string) |
| 103 | + self.text_space.insertPlainText(string) |
| 104 | + self.text_space.moveCursor(QTextCursor.MoveOperation.End) |
| 105 | + |
| 106 | + |
| 107 | +def init(args: list): |
| 108 | + if args: |
| 109 | + print(args) |
| 110 | + app = QApplication(sys.argv) |
| 111 | + window = Tool() |
| 112 | + sys.stdout = StdoutRedirector(window.log_output) |
| 113 | + sys.stderr = StdoutRedirector(window.log_output, is_error=True) |
| 114 | + window.show() |
| 115 | + app.exec() |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + init([]) |
0 commit comments