diff --git a/py/LunaTranslator/LunaTranslator.py b/py/LunaTranslator/LunaTranslator.py index 8bde5a9d..684170a3 100644 --- a/py/LunaTranslator/LunaTranslator.py +++ b/py/LunaTranslator/LunaTranslator.py @@ -11,6 +11,7 @@ from myutils.config import ( getlanguse, set_font_default, _TR, + isascii, ) from myutils.utils import ( minmaxmoveobservefunc, @@ -606,8 +607,8 @@ class MAINUI: else: if item["condition"] == 1: if ( - res.isascii() - and item["key"].isascii() + isascii(res) + and isascii(item["key"]) and (" " not in item["key"]) ): # 目标可能有空格 resx = res.split(" ") diff --git a/py/LunaTranslator/cishu/mdict.py b/py/LunaTranslator/cishu/mdict.py index c3ab1912..200b1a81 100644 --- a/py/LunaTranslator/cishu/mdict.py +++ b/py/LunaTranslator/cishu/mdict.py @@ -1,6 +1,6 @@ import math, base64, uuid, gobject from cishu.cishubase import DictTree - +from myutils.config import isascii class FlexBuffer: @@ -2039,9 +2039,9 @@ class mdict(cishubase): t: str = os.path.basename(f)[:-4] if index._title.strip() != "": t1 = index._title.strip() - if (t1.isascii()) and (t.isascii()): + if (isascii(t1)) and (isascii(t)): t = t1 - elif not t1.isascii(): + elif not isascii(t1): t = t1 title = t return title diff --git a/py/LunaTranslator/cishu/youdao.py b/py/LunaTranslator/cishu/youdao.py index bf1dd163..eec2be84 100644 --- a/py/LunaTranslator/cishu/youdao.py +++ b/py/LunaTranslator/cishu/youdao.py @@ -1,10 +1,9 @@ -from myutils.utils import getlangsrc import requests from urllib.parse import quote import re, os from cishu.cishubase import cishubase -from myutils.utils import simplehtmlparser -from myutils.utils import get_element_by +from myutils.utils import get_element_by, simplehtmlparser, getlangsrc +from myutils.config import isascii class youdao(cishubase): @@ -12,7 +11,7 @@ class youdao(cishubase): def search(self, word: str): lang = getlangsrc() if lang == "auto": - if word.isascii(): + if isascii(word): lang = "en" else: lang = "ja" diff --git a/py/LunaTranslator/gui/dialog_savedgame_v3.py b/py/LunaTranslator/gui/dialog_savedgame_v3.py index 4c969cee..b6de9415 100644 --- a/py/LunaTranslator/gui/dialog_savedgame_v3.py +++ b/py/LunaTranslator/gui/dialog_savedgame_v3.py @@ -185,7 +185,7 @@ class ImageDelegate(QStyledItemDelegate): def initStyleOption(self, opt: QStyleOptionViewItem, index: QModelIndex): super().initStyleOption(opt, index) if not index.data(ImageRequestedRole): - opt.features |= opt.ViewItemFeature.HasDecoration + opt.features |= QStyleOptionViewItem.ViewItemFeature.HasDecoration opt.decorationSize = QSize(100, 100) diff --git a/py/LunaTranslator/gui/selecthook.py b/py/LunaTranslator/gui/selecthook.py index b506d1ee..1554268d 100644 --- a/py/LunaTranslator/gui/selecthook.py +++ b/py/LunaTranslator/gui/selecthook.py @@ -3,7 +3,7 @@ import functools, binascii from collections import OrderedDict from traceback import print_exc import qtawesome, windows, winsharedutils, gobject -from myutils.config import savehook_new_data, static_data, globalconfig, _TR +from myutils.config import savehook_new_data, static_data, globalconfig, _TR, isascii from myutils.utils import checkchaos, get_time_stamp, dynamiclink, is_ascii_control from myutils.wrapper import Singleton_close from gui.dialog_savedgame import dialog_setting_game @@ -706,7 +706,7 @@ class hookselect(closeashidewindow): def gethide(self, res: str): if self.checkfilt_notascii.isChecked(): - if res.isascii(): + if isascii(res): return True if self.checkfilt_notshiftjis.isChecked(): if checkchaos(res): diff --git a/py/LunaTranslator/gui/translatorUI.py b/py/LunaTranslator/gui/translatorUI.py index 4c75c106..01f0b911 100644 --- a/py/LunaTranslator/gui/translatorUI.py +++ b/py/LunaTranslator/gui/translatorUI.py @@ -921,19 +921,20 @@ class TranslatorWindow(resizableframeless): self.changeshowhidetranssig.connect(self.changeshowhidetrans) def __init__(self): - + flags = ( + Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowMinimizeButtonHint + ) + if globalconfig["keepontop"]: + flags |= Qt.WindowType.WindowStaysOnTopHint super(TranslatorWindow, self).__init__( None, - flags=Qt.WindowType.FramelessWindowHint - | Qt.WindowType.WindowMinimizeButtonHint, + flags=flags, poslist=globalconfig["transuigeo"], ) # 设置为顶级窗口,无边框 icon = getExeIcon(getcurrexe()) #'./LunaTranslator.exe')# QIcon() # icon.addPixmap(QPixmap('./files/luna.png'), QIcon.Normal, QIcon.On) self.setWindowIcon(icon) self.firstshow = True - if globalconfig["keepontop"]: - self.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint) self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) self.setAttribute(Qt.WidgetAttribute.WA_ShowWithoutActivating, True) self.setWindowTitle("LunaTranslator") @@ -1193,7 +1194,9 @@ class TranslatorWindow(resizableframeless): globalconfig["isshowrawtext"] ) try: - gobject.baseobject.settin_ui.fenyinsettings.setEnabled(globalconfig["isshowrawtext"]) + gobject.baseobject.settin_ui.fenyinsettings.setEnabled( + globalconfig["isshowrawtext"] + ) except: pass diff --git a/py/LunaTranslator/hiraparse/mecab.py b/py/LunaTranslator/hiraparse/mecab.py index 73589e44..33d61a88 100644 --- a/py/LunaTranslator/hiraparse/mecab.py +++ b/py/LunaTranslator/hiraparse/mecab.py @@ -1,6 +1,7 @@ import winsharedutils import os, csv, gobject from hiraparse.basehira import basehira +from myutils.config import isascii # # 2.1.2 src schema # UnidicFeatures17 = namedtuple('UnidicFeatures17', @@ -109,7 +110,7 @@ class mecab(basehira): if "-" in origorig: try: hira_ = origorig.split("-")[1] - if hira_.isascii(): # 腰を引いて-->引く-他動詞 + if isascii(hira): # 腰を引いて-->引く-他動詞 hira = hira_ origorig = origorig.split("-")[0] except: diff --git a/py/LunaTranslator/myutils/config.py b/py/LunaTranslator/myutils/config.py index cb7f86d0..19e79727 100644 --- a/py/LunaTranslator/myutils/config.py +++ b/py/LunaTranslator/myutils/config.py @@ -4,6 +4,17 @@ from traceback import print_exc from qtsymbols import * +def isascii(s: str): + try: + return s.isascii() + except: + try: + s.encode("ascii") + return True + except: + return False + + def namemapcast(namemap): bettermap = namemap.copy() for k, v in namemap.items(): @@ -576,7 +587,7 @@ def loadlanguage(): def _TR(k: str): if not k: return "" - if k.isascii(): + if isascii(k): return k loadlanguage() if "_" in k: diff --git a/py/LunaTranslator/myutils/languageguesser.py b/py/LunaTranslator/myutils/languageguesser.py index 8d69c271..2cbfb88b 100644 --- a/py/LunaTranslator/myutils/languageguesser.py +++ b/py/LunaTranslator/myutils/languageguesser.py @@ -1,12 +1,12 @@ from collections import defaultdict import re from myutils.utils import cinranges - +from myutils.config import isascii def guess(string: str): if not string: return "en" - if string.isascii(): + if isascii(string): return "en" checkers = { "ru": lambda c: cinranges( diff --git a/py/LunaTranslator/windows.py b/py/LunaTranslator/windows.py index f81ad411..52f1ac20 100644 --- a/py/LunaTranslator/windows.py +++ b/py/LunaTranslator/windows.py @@ -275,9 +275,9 @@ _SendMessage = _user32.SendMessageW _SendMessage.argtypes = c_int, c_uint, c_void_p, c_void_p _keybd_event = _user32.keybd_event _keybd_event.argtypes = c_byte, c_byte, c_uint, c_void_p -RegisterWindowMessageW = _user32.RegisterWindowMessageW -RegisterWindowMessageW.argtypes = (LPCWSTR,) -RegisterWindowMessageW.restype = UINT +RegisterWindowMessage = _user32.RegisterWindowMessageW +RegisterWindowMessage.argtypes = (LPCWSTR,) +RegisterWindowMessage.restype = UINT _GetWindowThreadProcessId = _user32.GetWindowThreadProcessId _GetWindowThreadProcessId.argtypes = HWND, c_void_p GetClipboardOwner = _user32.GetClipboardOwner diff --git a/py/generate_xp_code.py b/py/generate_xp_code.py new file mode 100644 index 00000000..e4989d6c --- /dev/null +++ b/py/generate_xp_code.py @@ -0,0 +1,35 @@ +import os, re + + +def parsecode(code: str): + # PyQt + code = code.replace( + "QApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling)", "" + ) + code = code.replace("QApplication.setHighDpiScaleFactorRoundingPolicy", "print") + code = code.replace("Qt.HighDpiScaleFactorRoundingPolicy.PassThrough", "") + code = code.replace("self.screen().geometry().height()", "99999") + code = code.replace( + '"./files/runtime/PyQt5/Qt5/plugins"', + r'"C:\Python34\Lib\site-packages\PyQt5\plugins"', + ) + code = code.replace("self.parent().devicePixelRatioF()", "1") + code = code.replace("self.devicePixelRatioF()", "1") + code = re.sub( + r"(Q[a-zA-Z0-9_]+)\.[a-zA-Z0-9_]+\.([a-zA-Z0-9_]+)([ \)\n,:])", r"\1.\2\3", code + ) + # 移除类型注解 + code = re.sub(r": [a-zA-Z0-9_]+\)", ")", code) + code = re.sub(r": [a-zA-Z0-9_]+ = ", " = ", code) + return code + + +for _dir, _, _fs in os.walk("./LunaTranslator"): + for _f in _fs: + if not _f.endswith(".py"): + continue + path = os.path.normpath(os.path.abspath(os.path.join(_dir, _f))) + with open(path, "r", encoding="utf8") as ff: + code = ff.read() + with open(path, "w", encoding="utf8") as ff: + ff.write(parsecode(code))