|
| 1 | +from PySide6.QtCore import QTimer |
| 2 | +from PySide6.QtGui import QIntValidator, QKeyEvent, Qt |
| 3 | +from PySide6.QtWidgets import ( |
| 4 | + QWidget, QLineEdit, QComboBox, QPushButton, QVBoxLayout, QLabel, |
| 5 | + QGridLayout, QHBoxLayout, QRadioButton, QButtonGroup, QMessageBox |
| 6 | +) |
| 7 | + |
| 8 | +from je_auto_control.gui.language_wrapper.multi_language_wrapper import language_wrapper |
| 9 | +from je_auto_control.utils.executor.action_executor import execute_action |
| 10 | +from je_auto_control.wrapper.auto_control_keyboard import type_keyboard |
| 11 | +from je_auto_control.wrapper.auto_control_mouse import click_mouse |
| 12 | +from je_auto_control.wrapper.auto_control_record import record, stop_record |
| 13 | +from je_auto_control.wrapper.platform_wrapper import keyboard_keys_table, mouse_keys_table |
| 14 | + |
| 15 | + |
| 16 | +class AutoControlGUIWidget(QWidget): |
| 17 | + |
| 18 | + def __init__(self, parent=None): |
| 19 | + super().__init__(parent) |
| 20 | + |
| 21 | + main_layout = QVBoxLayout() |
| 22 | + |
| 23 | + # Grid for input fields |
| 24 | + grid = QGridLayout() |
| 25 | + |
| 26 | + # Interval time |
| 27 | + grid.addWidget(QLabel(language_wrapper.language_word_dict.get("interval_time")), 0, 0) |
| 28 | + self.interval_input = QLineEdit() |
| 29 | + self.interval_input.setValidator(QIntValidator()) |
| 30 | + grid.addWidget(self.interval_input, 0, 1) |
| 31 | + |
| 32 | + # Cursor X/Y |
| 33 | + grid.addWidget(QLabel(language_wrapper.language_word_dict.get("cursor_x")), 2, 0) |
| 34 | + self.cursor_x_input = QLineEdit() |
| 35 | + self.cursor_x_input.setValidator(QIntValidator()) |
| 36 | + grid.addWidget(self.cursor_x_input, 2, 1) |
| 37 | + |
| 38 | + grid.addWidget(QLabel(language_wrapper.language_word_dict.get("cursor_y")), 3, 0) |
| 39 | + self.cursor_y_input = QLineEdit() |
| 40 | + self.cursor_y_input.setValidator(QIntValidator()) |
| 41 | + grid.addWidget(self.cursor_y_input, 3, 1) |
| 42 | + |
| 43 | + # Mouse button |
| 44 | + grid.addWidget(QLabel(language_wrapper.language_word_dict.get("mouse_button")), 4, 0) |
| 45 | + self.mouse_button_combo = QComboBox() |
| 46 | + self.mouse_button_combo.addItems(mouse_keys_table) |
| 47 | + grid.addWidget(self.mouse_button_combo, 4, 1) |
| 48 | + |
| 49 | + # Keyboard button |
| 50 | + grid.addWidget(QLabel(language_wrapper.language_word_dict.get("keyboard_button")), 5, 0) |
| 51 | + self.keyboard_button_combo = QComboBox() |
| 52 | + self.keyboard_button_combo.addItems(keyboard_keys_table.keys()) |
| 53 | + grid.addWidget(self.keyboard_button_combo, 5, 1) |
| 54 | + |
| 55 | + # Click type |
| 56 | + grid.addWidget(QLabel(language_wrapper.language_word_dict.get("click_type")), 6, 0) |
| 57 | + self.click_type_combo = QComboBox() |
| 58 | + self.click_type_combo.addItems(["Single Click", "Double Click"]) |
| 59 | + grid.addWidget(self.click_type_combo, 6, 1) |
| 60 | + |
| 61 | + # Input method selection |
| 62 | + grid.addWidget(QLabel(language_wrapper.language_word_dict.get("input_method")), 7, 0) |
| 63 | + self.mouse_radio = QRadioButton(language_wrapper.language_word_dict.get("mouse_radio")) |
| 64 | + self.keyboard_radio = QRadioButton(language_wrapper.language_word_dict.get("keyboard_radio")) |
| 65 | + self.mouse_radio.setChecked(True) |
| 66 | + self.input_method_group = QButtonGroup() |
| 67 | + self.input_method_group.addButton(self.mouse_radio) |
| 68 | + self.input_method_group.addButton(self.keyboard_radio) |
| 69 | + grid.addWidget(self.mouse_radio, 7, 1) |
| 70 | + grid.addWidget(self.keyboard_radio, 7, 2) |
| 71 | + |
| 72 | + main_layout.addLayout(grid) |
| 73 | + |
| 74 | + # Repeat options |
| 75 | + repeat_layout = QHBoxLayout() |
| 76 | + self.repeat_until_stopped = QRadioButton(language_wrapper.language_word_dict.get("repeat_until_stopped_radio")) |
| 77 | + self.repeat_count_times = QRadioButton(language_wrapper.language_word_dict.get("repeat_radio")) |
| 78 | + self.repeat_count_input = QLineEdit() |
| 79 | + self.repeat_count_input.setValidator(QIntValidator()) |
| 80 | + self.repeat_count_input.setPlaceholderText(language_wrapper.language_word_dict.get("times")) |
| 81 | + repeat_group = QButtonGroup() |
| 82 | + repeat_group.addButton(self.repeat_until_stopped) |
| 83 | + repeat_group.addButton(self.repeat_count_times) |
| 84 | + self.repeat_until_stopped.setChecked(True) |
| 85 | + self.repeat_count = 0 |
| 86 | + self.repeat_max = 0 |
| 87 | + |
| 88 | + repeat_layout.addWidget(self.repeat_until_stopped) |
| 89 | + repeat_layout.addWidget(self.repeat_count_times) |
| 90 | + repeat_layout.addWidget(self.repeat_count_input) |
| 91 | + main_layout.addLayout(repeat_layout) |
| 92 | + |
| 93 | + # Start/Stop buttons |
| 94 | + button_layout = QHBoxLayout() |
| 95 | + self.start_button = QPushButton(language_wrapper.language_word_dict.get("start")) |
| 96 | + self.start_button.clicked.connect(self.start_autocontrol) |
| 97 | + self.stop_button = QPushButton(language_wrapper.language_word_dict.get("stop")) |
| 98 | + self.stop_button.clicked.connect(self.stop_autocontrol) |
| 99 | + button_layout.addWidget(self.start_button) |
| 100 | + button_layout.addWidget(self.stop_button) |
| 101 | + main_layout.addLayout(button_layout) |
| 102 | + |
| 103 | + # Timer |
| 104 | + self.start_autocontrol_timer = QTimer() |
| 105 | + |
| 106 | + # Connect input method toggle |
| 107 | + self.mouse_radio.toggled.connect(self.update_input_mode) |
| 108 | + self.keyboard_radio.toggled.connect(self.update_input_mode) |
| 109 | + self.update_input_mode() |
| 110 | + |
| 111 | + self.setLayout(main_layout) |
| 112 | + |
| 113 | + def update_input_mode(self): |
| 114 | + use_mouse = self.mouse_radio.isChecked() |
| 115 | + self.cursor_x_input.setEnabled(use_mouse) |
| 116 | + self.cursor_y_input.setEnabled(use_mouse) |
| 117 | + self.mouse_button_combo.setEnabled(use_mouse) |
| 118 | + self.keyboard_button_combo.setEnabled(not use_mouse) |
| 119 | + |
| 120 | + def start_autocontrol(self): |
| 121 | + self.start_autocontrol_timer.setInterval(int(self.interval_input.text())) |
| 122 | + self.start_autocontrol_timer.timeout.connect(lambda: self.start_timer_function()) |
| 123 | + self.start_autocontrol_timer.start() |
| 124 | + self.repeat_max = int(self.repeat_count_input.text()) |
| 125 | + |
| 126 | + def start_timer_function(self): |
| 127 | + if self.repeat_until_stopped.isChecked(): |
| 128 | + self.trigger_autocontrol_function() |
| 129 | + elif self.repeat_count_times.isChecked(): |
| 130 | + self.repeat_count += 1 |
| 131 | + if self.repeat_count < self.repeat_max: |
| 132 | + self.trigger_autocontrol_function() |
| 133 | + else: |
| 134 | + self.repeat_count = 0 |
| 135 | + self.repeat_max = 0 |
| 136 | + self.start_autocontrol_timer.stop() |
| 137 | + |
| 138 | + def trigger_autocontrol_function(self): |
| 139 | + click_type = self.click_type_combo.currentText() |
| 140 | + if self.mouse_radio.isChecked(): |
| 141 | + trigger_function = click_mouse |
| 142 | + button = self.mouse_button_combo.currentText() |
| 143 | + x = int(self.cursor_x_input.text()) |
| 144 | + y = int(self.cursor_y_input.text()) |
| 145 | + if click_type == "Single Click": |
| 146 | + trigger_function(mouse_keycode=button, x=x, y=y) |
| 147 | + elif click_type == "Double Click": |
| 148 | + trigger_function(mouse_keycode=button, x=x, y=y) |
| 149 | + trigger_function(mouse_keycode=button, x=x, y=y) |
| 150 | + elif self.keyboard_radio.isChecked(): |
| 151 | + trigger_function = type_keyboard |
| 152 | + button = self.keyboard_button_combo.currentText() |
| 153 | + if click_type == "Single Click": |
| 154 | + trigger_function(keycode=button) |
| 155 | + elif click_type == "Double Click": |
| 156 | + trigger_function(keycode=button) |
| 157 | + trigger_function(keycode=button) |
| 158 | + |
| 159 | + def stop_autocontrol(self): |
| 160 | + self.start_autocontrol_timer.stop() |
| 161 | + |
| 162 | + |
| 163 | + def keyPressEvent(self, event: QKeyEvent): |
| 164 | + if event.modifiers() == Qt.KeyboardModifier.ControlModifier and event.key() == Qt.Key.Key_4: |
| 165 | + self.start_autocontrol_timer.stop() |
| 166 | + else: |
| 167 | + super().keyPressEvent(event) |
0 commit comments