Skip to content

Commit

Permalink
add hwnd timer and reload anki
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewthe2 committed Jul 3, 2022
1 parent 1e85b83 commit 781c73d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/main/python/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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):
Expand All @@ -55,31 +59,31 @@ def __init__(self, parent):

# Window Capture
self.windows = []
self.selected_window = None
self.capture_window = CaptureWindow()

# Area Capture
self.snipping_widget = CaptureScreen()
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
self.models = []
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]
Expand All @@ -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):
Expand All @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions src/main/python/screenshot/hwnd_worker.py
Original file line number Diff line number Diff line change
@@ -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)

4 changes: 4 additions & 0 deletions src/main/python/ui/main_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 781c73d

Please sign in to comment.