From 1c17b4e17fe9aaee14a0877e14d85b4e1e255e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Tue, 31 Dec 2024 21:35:42 +0800 Subject: [PATCH] . --- py/LunaTranslator/ocrengines/windowsocr.py | 100 ++++++++++++++++++--- 1 file changed, 90 insertions(+), 10 deletions(-) diff --git a/py/LunaTranslator/ocrengines/windowsocr.py b/py/LunaTranslator/ocrengines/windowsocr.py index 2c1f6ce9..ea0f44c2 100644 --- a/py/LunaTranslator/ocrengines/windowsocr.py +++ b/py/LunaTranslator/ocrengines/windowsocr.py @@ -1,29 +1,109 @@ import gobject -import winrtutils -from myutils.config import _TR, static_data +import winrtutils, windows, re +import subprocess +from myutils.config import _TR from myutils.utils import dynamiclink from ocrengines.baseocrclass import baseocr from qtsymbols import * from gui.dynalang import LPushButton, LLabel from myutils.utils import getlanguagespace +from gui.dynalang import LPushButton, LFormLayout, LLabel +from gui.usefulwidget import SuperCombo, getboxlayout +import threading, qtawesome + + +def getallsupports(): + _ = ( + subprocess.run( + "powershell Get-WindowsCapability -Online | Where-Object { $_.Name -Like 'Language.OCR*' }", + stdout=subprocess.PIPE, + ) + .stdout.decode() + .splitlines() + ) + langs = [] + langsinter = [] + for i in range(len(_)): + if _[i].startswith("Name"): + if "Installed" in _[i + 1]: + continue + langs.append(re.search("Language.OCR~~~(.*?)~", _[i]).groups()[0]) + langsinter.append(re.search("Language.OCR~~~(.*)", _[i]).group()) + return langs, langsinter + + +class _SuperCombo(SuperCombo): + setlist = pyqtSignal(list, list) + + def addItems(self, items, internals): + self.clear() + super().addItems(items, internals) + + +def loadlist(combo: _SuperCombo): + lang, inter = getallsupports() + combo.setlist.emit(lang, inter) + + +def installx(combo: _SuperCombo, btninstall): + if combo.currentIndex() == -1: + return + btninstall.setEnabled(False) + combo.setEnabled(False) + lang = combo.getIndexData(combo.currentIndex()) + subprocess.run( + "powershell $Capability = Get-WindowsCapability -Online | Where-Object {{ $_.Name -Like '{}' }} | Add-WindowsCapability -Online".format( + lang + ), + ) + lang, inter = getallsupports() + combo.setlist.emit(lang, inter) + btninstall.setEnabled(True) + combo.setEnabled(True) def question(): dialog = QWidget() - formLayout = QHBoxLayout() + formLayout = LFormLayout() formLayout.setContentsMargins(0, 0, 0, 0) dialog.setLayout(formLayout) supportlang = QLabel() supportlang.setText(", ".join([_[1] for _ in winrtutils.getlanguagelist()])) - btndownload = LPushButton("添加语言包") - btndownload.clicked.connect( - lambda: gobject.baseobject.openlink( - dynamiclink("{docs_server}/#/zh/useapis/ocrapi?id=windowsocr") + supportlang.setWordWrap(True) + formLayout.addRow("当前支持的语言", supportlang) + + lst = [] + if not windows.IsUserAnAdmin(): + nopri = LLabel("权限不足,请以管理员权限运行!") + lst.append(nopri) + btndownload = LPushButton("添加语言包") + btndownload.clicked.connect( + lambda: gobject.baseobject.openlink( + dynamiclink("{docs_server}/#/zh/useapis/ocrapi?id=windowsocr") + ) ) + lst.append(btndownload) + else: + combo = _SuperCombo() + combo.setlist.connect(combo.addItems) + btninstall = LPushButton("添加") + btninstall.clicked.connect( + lambda: threading.Thread(target=installx, args=(combo, btninstall)).start() + ) + lst.append(combo) + lst.append(btninstall) + threading.Thread(target=loadlist, args=(combo,)).start() + btndownload = QPushButton(icon=qtawesome.icon("fa.question")) + btndownload.clicked.connect( + lambda: gobject.baseobject.openlink( + dynamiclink("{docs_server}/#/zh/useapis/ocrapi?id=windowsocr") + ) + ) + lst.append(btndownload) + formLayout.addRow( + "添加语言包", + getboxlayout(lst, makewidget=True, margin0=True), ) - formLayout.addWidget(LLabel("当前支持的语言")) - formLayout.addWidget(supportlang) - formLayout.addWidget(btndownload) return dialog