From 781c73de3dbd69c663eae6af1e9e0da6f4baa115 Mon Sep 17 00:00:00 2001 From: Mathew Chan Date: Sun, 3 Jul 2022 11:34:25 +0800 Subject: [PATCH] add hwnd timer and reload anki --- src/main/python/main.py | 51 +++++++++++++---------- src/main/python/screenshot/hwnd_worker.py | 22 ++++++++++ src/main/python/ui/main_ui.py | 4 ++ 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/main/python/screenshot/hwnd_worker.py diff --git a/src/main/python/main.py b/src/main/python/main.py index a5c65c5..c742f04 100644 --- a/src/main/python/main.py +++ b/src/main/python/main.py @@ -1,12 +1,12 @@ import sys from fbs_runtime.application_context.PyQt5 import ApplicationContext from PyQt5.QtWidgets import QMainWindow, QWidget -from screenshot.hwnd_manager import HWNDManager from anki.anki_connect import AnkiConnect from call_handler import CallHandler from screenshot.capture_window import CaptureWindow from screenshot.capture_screen import CaptureScreen from screenshot import Capture_Mode +from screenshot.hwnd_worker import HWNDWorker from threading import Thread from ui.main_ui import UIMain from game2text.ocr import OCR, OCR_Engine, paddle_models_path @@ -17,21 +17,23 @@ def __init__(self, appctxt): super().__init__() self.setGeometry(500, 500, 400, 400) self.setWindowTitle("Game2Text Lightning") - self.HWNDManager = HWNDManager() + self.hwnd_worker = HWNDWorker(interval=1000) self.anki_connect = AnkiConnect(anki_models_path = appctxt.get_resource('anki/user_models.yaml')) self.ocr = OCR(appctxt.get_resource(paddle_models_path), OCR_Engine.PADDLE_OCR) self.call_handler = CallHandler(appctxt, self.anki_connect) self.control_panel = ControlPanel(self) self.setCentralWidget(self.control_panel) + # Windows - window_thread = Thread(target = self.fetch_windows) - window_thread.start() + self.hwnd_worker.hwnd_signal.connect(self.on_receive_window_titles) + self.hwnd_worker.start() # Anki - model_thread = Thread(target = self.fetch_models) - deck_thread = Thread(target = self.fetch_decks) - model_thread.start() - deck_thread.start() + self.load_anki() + + def on_receive_window_titles(self, windows): + self.windows = windows + self.control_panel.set_windows(self.windows) def fetch_models(self): self.models = self.anki_connect.fetch_anki_models() @@ -41,12 +43,14 @@ def fetch_decks(self): self.decks = self.anki_connect.fetch_anki_decks() self.control_panel.update_deck_options(self.decks) - def fetch_windows(self): - self.windows = self.HWNDManager.get_hwnd_titles() - self.control_panel.set_windows(self.windows) - def capture(self): return self.control_panel.get_capture() + + def load_anki(self): + model_thread = Thread(target = self.fetch_models) + deck_thread = Thread(target = self.fetch_decks) + model_thread.start() + deck_thread.start() class ControlPanel(QWidget, UIMain): def __init__(self, parent): @@ -55,6 +59,7 @@ def __init__(self, parent): # Window Capture self.windows = [] + self.selected_window = None self.capture_window = CaptureWindow() # Area Capture @@ -62,16 +67,16 @@ def __init__(self, parent): self.snipping_widget.onSnippingCompleted = self.on_snipping_completed self.snipped_capture = None - self.call_handler = parent.call_handler # handle calls between web and python - self.game2text = Game2Text(parent.ocr, parent.capture, self.call_handler) + self.game2text = Game2Text(parent.ocr, parent.capture, parent.call_handler) self.running_ocr = False self.capture_mode = Capture_Mode.WINDOW self.captureComboBox.currentIndexChanged.connect(self.select_capture_mode) - self.captureWindowComboBox.currentIndexChanged.connect(self.select_window) + self.captureWindowComboBox.activated.connect(self.select_window) self.modelComboBox.currentIndexChanged.connect(self.select_model) self.selectRegionButton.clicked.connect(self.select_area) self.start_button.clicked.connect(self.toggle_ocr) + self.reloadAnkiButton.clicked.connect(parent.load_anki) # Anki Settings self.anki_connect = parent.anki_connect @@ -79,7 +84,6 @@ def __init__(self, parent): self.selected_model = None self.tableFields.on_change = self.on_anki_options_update - def select_model(self, index): if self.models: self.selected_model = self.models[index] @@ -88,7 +92,6 @@ def select_model(self, index): field_value_map = self.anki_connect.get_field_value_map(self.selected_model.model_name) self.tableFields.setData(fields, field_value_map) self.tableFields.show() - # self.call_handler.set_model(self.selected_model.model_name) self.anki_connect.set_model(self.selected_model.model_name) def update_model_options(self, options): @@ -105,16 +108,22 @@ def set_models(self, models): def set_windows(self, windows): self.windows = windows - for window in windows: - self.captureWindowComboBox.addItem(window) - + self.captureWindowComboBox.clear() + new_selected_index = -1 + for index, window in enumerate(windows): + self.captureWindowComboBox.addItem(window) + if window == self.selected_window: + new_selected_index = index + self.captureWindowComboBox.setCurrentIndex(new_selected_index) + def select_capture_mode(self, index): self.capture_mode = Capture_Mode(index) def select_window(self, index): if self.windows: self.start_button.setEnabled(True) - self.capture_window.setWindowTitle(self.windows[index]) + self.selected_window = self.windows[index] + self.capture_window.setWindowTitle(self.selected_window) def get_capture(self): if self.capture_mode == Capture_Mode.WINDOW: diff --git a/src/main/python/screenshot/hwnd_worker.py b/src/main/python/screenshot/hwnd_worker.py new file mode 100644 index 0000000..c67fe5e --- /dev/null +++ b/src/main/python/screenshot/hwnd_worker.py @@ -0,0 +1,22 @@ +from PyQt5.QtCore import QThread, QTimer, pyqtSignal, QEventLoop +from .hwnd_manager import HWNDManager + +class HWNDWorker(QThread): + hwnd_signal = pyqtSignal(list) + def __init__(self, interval): + super(QThread, self).__init__() + self.hwnd_manager = HWNDManager() + self.interval = interval + self.timer = QTimer() + self.timer.moveToThread(self) + self.timer.timeout.connect(self.emit_hwnd) + + def run(self): + self.timer.start(self.interval) + loop = QEventLoop() + loop.exec_() + + def emit_hwnd(self): + window_titles = self.hwnd_manager.get_hwnd_titles() + self.hwnd_signal.emit(window_titles) + \ No newline at end of file diff --git a/src/main/python/ui/main_ui.py b/src/main/python/ui/main_ui.py index 8cff63e..981b757 100644 --- a/src/main/python/ui/main_ui.py +++ b/src/main/python/ui/main_ui.py @@ -70,10 +70,14 @@ def setupUi(self, parent): self.tableFields = AnkiFieldTable([], None, 0, 2) self.tableFields.hide() + # reload anki button + self.reloadAnkiButton = QPushButton("Reload") + # First Tab self.tab1.layout = QVBoxLayout() self.tab1.layout.addLayout(self.modelDeckLayout) self.tab1.layout.addWidget(self.tableFields) + self.tab1.layout.addWidget(self.reloadAnkiButton) self.tab1.layout.addWidget(QLabel('Make sure AnkiConnect is installed, and Anki is open.')) self.tab1.setLayout(self.tab1.layout)